[libvirt] [PATCHv2 09/10] tools: show cpu cache occupancy information in domstats

Wang Huaqiang huaqiang.wang at intel.com
Mon Jul 9 07:00:57 UTC 2018


add cache occupancy information in command virsh domstats
for domains has resource monitoring groups.
---
 include/libvirt/libvirt-domain.h |   1 +
 src/qemu/qemu_driver.c           | 105 +++++++++++++++++++++++++++++++++++++++
 tools/virsh-domain-monitor.c     |   7 +++
 3 files changed, 113 insertions(+)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index c703346..c7ee425 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -2041,6 +2041,7 @@ typedef enum {
     VIR_DOMAIN_STATS_INTERFACE = (1 << 4), /* return domain interfaces info */
     VIR_DOMAIN_STATS_BLOCK = (1 << 5), /* return domain block info */
     VIR_DOMAIN_STATS_PERF = (1 << 6), /* return domain perf event info */
+    VIR_DOMAIN_STATS_CPU_RES = (1<<7), /*  return CPU resource info */
 } virDomainStatsTypes;
 
 typedef enum {
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 647d864..9b6e3fe 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -20330,6 +20330,110 @@ qemuDomainGetStatsPerf(virQEMUDriverPtr driver ATTRIBUTE_UNUSED,
     return ret;
 }
 
+
+static int
+qemuDomainGetStatsCPUResmon(virQEMUDriverPtr driver ATTRIBUTE_UNUSED,
+                            virDomainObjPtr vm,
+                            virDomainStatsRecordPtr record,
+                            int *maxparams,
+                            unsigned int privflags ATTRIBUTE_UNUSED)
+{
+    char param_name[VIR_TYPED_PARAM_FIELD_LENGTH];
+    size_t i = 0;
+    size_t l = 0;
+    unsigned int llc_occu = 0;
+    int ret = -1;
+    char *vcpustr = NULL;
+
+    for (i = 0; i < vm->def->nresmons; i++) {
+        virDomainCpuResmonDefPtr resmon = vm->def->resmons[i];
+
+        llc_occu = 0;
+        if (virResctrlMonIsRunning(resmon->mon)) {
+            if (virResctrlMonGetCacheOccupancy(resmon->mon, &llc_occu) < 0)
+                goto cleanup;
+        }
+
+        const char *mon_id = virResctrlMonGetID(resmon->mon);
+        if (!mon_id)
+            goto cleanup;
+        if (!(vcpustr = virBitmapFormat(resmon->vcpus)))
+            goto cleanup;
+
+        /* for vcpu string, both '1-3' and '1,3' are valid format and
+         * representing different vcpu set. But it is not easy to
+         * differentiate them at first galance, to avoid this case
+         * substituting all '-' with ',', e.g. substitute '1-3' with
+         * '1,2,3'  */
+        for (l = 0; l < strlen(vcpustr); l++) {
+            if (vcpustr[l] == '-') {
+                char strbuf[256];
+                unsigned int cpul = 0;
+                unsigned int cpur = 0;
+                virBuffer buf = VIR_BUFFER_INITIALIZER;
+                unsigned int icpu = 0;
+                char *tmp = NULL;
+
+                /* virStrToLong_ui is very tricking in processing '-'. to
+                 * avoid to trigger error, replace '-' with '_' */
+                vcpustr[l] = '_';
+
+                if (virStrToLong_ui(vcpustr, &tmp, 10, &cpul) < 0)
+                    goto cleanup;
+                if (virStrToLong_ui(vcpustr + l + 1, &tmp, 10, &cpur) < 0)
+                    goto cleanup;
+                if (cpur < cpul)
+                    goto cleanup;
+
+                for (icpu = cpul; icpu <= cpur; icpu++) {
+                    snprintf(strbuf, 256, "%d", icpu);
+                    virBufferStrcat(&buf, strbuf, NULL);
+                    if (icpu != cpur)
+                        virBufferStrcat(&buf, ",", NULL);
+                }
+
+                VIR_FREE(vcpustr);
+                vcpustr = virBufferContentAndReset(&buf);
+
+                break;
+            }
+        }
+
+        snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
+                 "cpu.cacheoccupancy.%s.value",
+                 mon_id);
+
+        if (virTypedParamsAddInt(&record->params,
+                    &record->nparams,
+                    maxparams,
+                    param_name,
+                    llc_occu) < 0) {
+            goto cleanup;
+        }
+
+        snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
+                 "cpu.cacheoccupancy.%s.vcpus",
+                 mon_id);
+
+        if (virTypedParamsAddString(&record->params,
+                                 &record->nparams,
+                                 maxparams,
+                                 param_name,
+                                 vcpustr) < 0) {
+            goto cleanup;
+        }
+
+        VIR_FREE(vcpustr);
+        vcpustr = NULL;
+    }
+
+    ret = 0;
+ cleanup:
+    VIR_FREE(vcpustr);
+    return ret;
+}
+
+
 typedef int
 (*qemuDomainGetStatsFunc)(virQEMUDriverPtr driver,
                           virDomainObjPtr dom,
@@ -20351,6 +20455,7 @@ static struct qemuDomainGetStatsWorker qemuDomainGetStatsWorkers[] = {
     { qemuDomainGetStatsInterface, VIR_DOMAIN_STATS_INTERFACE, false },
     { qemuDomainGetStatsBlock, VIR_DOMAIN_STATS_BLOCK, true },
     { qemuDomainGetStatsPerf, VIR_DOMAIN_STATS_PERF, false },
+    { qemuDomainGetStatsCPUResmon, VIR_DOMAIN_STATS_CPU_RES, false },
     { NULL, 0, false }
 };
 
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 87660ee..5f65f3d 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -2099,6 +2099,10 @@ static const vshCmdOptDef opts_domstats[] = {
      .type = VSH_OT_BOOL,
      .help = N_("report only stats that are accessible instantly"),
     },
+    {.name = "cpu-resource",
+     .type = VSH_OT_BOOL,
+     .help = N_("report cpu resource information"),
+    },
     VIRSH_COMMON_OPT_DOMAIN_OT_ARGV(N_("list of domains to get stats for"), 0),
     {.name = NULL}
 };
@@ -2164,6 +2168,9 @@ cmdDomstats(vshControl *ctl, const vshCmd *cmd)
     if (vshCommandOptBool(cmd, "perf"))
         stats |= VIR_DOMAIN_STATS_PERF;
 
+    if (vshCommandOptBool(cmd, "cpu-resource"))
+        stats |= VIR_DOMAIN_STATS_CPU_RES;
+
     if (vshCommandOptBool(cmd, "list-active"))
         flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_ACTIVE;
 
-- 
2.7.4




More information about the libvir-list mailing list