From c946a30af21c46673de83f7b1c51394db085d06c Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 29 Sep 2016 16:37:35 +0200 Subject: [PATCH] virsh: Extract fallback handling in cmdVcpuinfo Put it into a separate function so that more fallback handling can be added without making a mess. --- tools/virsh-domain.c | 75 +++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 21 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 8e1b9ed11e..84a4854102 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -6310,6 +6310,49 @@ virshVcpuinfoPrintAffinity(vshControl *ctl, } +static bool +virshVcpuinfoInactive(vshControl *ctl, + virDomainPtr dom, + int nvcpus, + int maxcpu, + bool pretty) +{ + unsigned char *cpumaps = NULL; + size_t cpumaplen; + int ncpus; + size_t i; + bool ret = false; + + cpumaplen = VIR_CPU_MAPLEN(maxcpu); + cpumaps = vshMalloc(ctl, nvcpus * cpumaplen); + + if ((ncpus = virDomainGetVcpuPinInfo(dom, nvcpus, + cpumaps, cpumaplen, + VIR_DOMAIN_AFFECT_CONFIG)) < 0) + goto cleanup; + + for (i = 0; i < ncpus; i++) { + if (i != 0) + vshPrint(ctl, "\n"); + + vshPrint(ctl, "%-15s %zu\n", _("VCPU:"), i); + vshPrint(ctl, "%-15s %s\n", _("CPU:"), _("N/A")); + vshPrint(ctl, "%-15s %s\n", _("State:"), _("N/A")); + vshPrint(ctl, "%-15s %s\n", _("CPU time"), _("N/A")); + + if (virshVcpuinfoPrintAffinity(ctl, VIR_GET_CPUMAP(cpumaps, cpumaplen, i), + maxcpu, pretty) < 0) + goto cleanup; + } + + ret = true; + + cleanup: + VIR_FREE(cpumaps); + return ret; +} + + static bool cmdVcpuinfo(vshControl *ctl, const vshCmd *cmd) { @@ -6343,32 +6386,22 @@ cmdVcpuinfo(vshControl *ctl, const vshCmd *cmd) if (info.state != VIR_DOMAIN_SHUTOFF) goto cleanup; - /* fall back to virDomainGetVcpuPinInfo and free cpuinfo to mark this */ - VIR_FREE(cpuinfo); - if ((ncpus = virDomainGetVcpuPinInfo(dom, info.nrVirtCpu, - cpumaps, cpumaplen, - VIR_DOMAIN_AFFECT_CONFIG)) < 0) - goto cleanup; + /* for offline VMs we can return pinning information */ + ret = virshVcpuinfoInactive(ctl, dom, info.nrVirtCpu, maxcpu, pretty); + goto cleanup; } for (n = 0; n < ncpus; n++) { - if (cpuinfo) { - vshPrint(ctl, "%-15s %d\n", _("VCPU:"), cpuinfo[n].number); - vshPrint(ctl, "%-15s %d\n", _("CPU:"), cpuinfo[n].cpu); - vshPrint(ctl, "%-15s %s\n", _("State:"), - virshDomainVcpuStateToString(cpuinfo[n].state)); - if (cpuinfo[n].cpuTime != 0) { - double cpuUsed = cpuinfo[n].cpuTime; + vshPrint(ctl, "%-15s %d\n", _("VCPU:"), cpuinfo[n].number); + vshPrint(ctl, "%-15s %d\n", _("CPU:"), cpuinfo[n].cpu); + vshPrint(ctl, "%-15s %s\n", _("State:"), + virshDomainVcpuStateToString(cpuinfo[n].state)); + if (cpuinfo[n].cpuTime != 0) { + double cpuUsed = cpuinfo[n].cpuTime; - cpuUsed /= 1000000000.0; + cpuUsed /= 1000000000.0; - vshPrint(ctl, "%-15s %.1lfs\n", _("CPU time:"), cpuUsed); - } - } else { - vshPrint(ctl, "%-15s %d\n", _("VCPU:"), n); - vshPrint(ctl, "%-15s %s\n", _("CPU:"), _("N/A")); - vshPrint(ctl, "%-15s %s\n", _("State:"), _("N/A")); - vshPrint(ctl, "%-15s %s\n", _("CPU time"), _("N/A")); + vshPrint(ctl, "%-15s %.1lfs\n", _("CPU time:"), cpuUsed); } if (virshVcpuinfoPrintAffinity(ctl, VIR_GET_CPUMAP(cpumaps, cpumaplen, n),