From 9549a8967a9c1f23ee82b413f97a127952a66986 Mon Sep 17 00:00:00 2001 From: Wang Huaqiang Date: Tue, 11 Jun 2019 11:31:14 +0800 Subject: [PATCH] util: Extend virresctl API to retrieve multiple monitor statistics Export virResctrlMonitorGetStats and make virResctrlMonitorGetCacheOccupancy obsoleted. Signed-off-by: Wang Huaqiang Reviewed-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/qemu/qemu_driver.c | 33 ++++++++++++++++++++++-------- src/util/virresctrl.c | 44 +++++++++++++++++++++++----------------- src/util/virresctrl.h | 6 ++++++ 4 files changed, 57 insertions(+), 27 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d61e810fcd..b039e09817 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2835,6 +2835,7 @@ virResctrlMonitorCreate; virResctrlMonitorDeterminePath; virResctrlMonitorGetCacheOccupancy; virResctrlMonitorGetID; +virResctrlMonitorGetStats; virResctrlMonitorNew; virResctrlMonitorRemove; virResctrlMonitorSetAlloc; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index ab92f4ab73..3c5ec848ba 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -20710,6 +20710,7 @@ qemuDomainFreeResctrlMonData(virQEMUResctrlMonDataPtr resdata) /** * qemuDomainGetResctrlMonData: * @dom: Pointer for the domain that the resctrl monitors reside in + * @driver: Pointer to qemu driver * @resdata: Pointer of virQEMUResctrlMonDataPtr pointer for receiving the * virQEMUResctrlMonDataPtr array. Caller is responsible for * freeing the array. @@ -20727,16 +20728,32 @@ qemuDomainFreeResctrlMonData(virQEMUResctrlMonDataPtr resdata) * Returns -1 on failure, or 0 on success. */ static int -qemuDomainGetResctrlMonData(virDomainObjPtr dom, +qemuDomainGetResctrlMonData(virQEMUDriverPtr driver, + virDomainObjPtr dom, virQEMUResctrlMonDataPtr **resdata, size_t *nresdata, virResctrlMonitorType tag) { virDomainResctrlDefPtr resctrl = NULL; virQEMUResctrlMonDataPtr res = NULL; + char **features = NULL; + virCapsPtr caps = NULL; size_t i = 0; size_t j = 0; + caps = virQEMUDriverGetCapabilities(driver, false); + + if (tag == VIR_RESCTRL_MONITOR_TYPE_CACHE) { + features = caps->host.cache.monitor->features; + } else { + virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", + _("Unsupported resctrl monitor type")); + return -1; + } + + if (virStringListLength((const char * const *)features) == 0) + return 0; + for (i = 0; i < dom->def->nresctrls; i++) { resctrl = dom->def->resctrls[i]; @@ -20763,9 +20780,8 @@ qemuDomainGetResctrlMonData(virDomainObjPtr dom, if (VIR_STRDUP(res->name, virResctrlMonitorGetID(monitor)) < 0) goto error; - if (virResctrlMonitorGetCacheOccupancy(monitor, - &res->stats, - &res->nstats) < 0) + if (virResctrlMonitorGetStats(monitor, (const char **)features, + &res->stats, &res->nstats) < 0) goto error; if (VIR_APPEND_ELEMENT(*resdata, *nresdata, res) < 0) @@ -20782,7 +20798,8 @@ qemuDomainGetResctrlMonData(virDomainObjPtr dom, static int -qemuDomainGetStatsCpuCache(virDomainObjPtr dom, +qemuDomainGetStatsCpuCache(virQEMUDriverPtr driver, + virDomainObjPtr dom, virDomainStatsRecordPtr record, int *maxparams) { @@ -20796,7 +20813,7 @@ qemuDomainGetStatsCpuCache(virDomainObjPtr dom, if (!virDomainObjIsActive(dom)) return 0; - if (qemuDomainGetResctrlMonData(dom, &resdata, &nresdata, + if (qemuDomainGetResctrlMonData(driver, dom, &resdata, &nresdata, VIR_RESCTRL_MONITOR_TYPE_CACHE) < 0) goto cleanup; @@ -20897,7 +20914,7 @@ qemuDomainGetStatsCpuCgroup(virDomainObjPtr dom, static int -qemuDomainGetStatsCpu(virQEMUDriverPtr driver ATTRIBUTE_UNUSED, +qemuDomainGetStatsCpu(virQEMUDriverPtr driver, virDomainObjPtr dom, virDomainStatsRecordPtr record, int *maxparams, @@ -20906,7 +20923,7 @@ qemuDomainGetStatsCpu(virQEMUDriverPtr driver ATTRIBUTE_UNUSED, if (qemuDomainGetStatsCpuCgroup(dom, record, maxparams) < 0) return -1; - if (qemuDomainGetStatsCpuCache(dom, record, maxparams) < 0) + if (qemuDomainGetStatsCpuCache(driver, dom, record, maxparams) < 0) return -1; return 0; diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c index 0117b8f60d..80acb709ce 100644 --- a/src/util/virresctrl.c +++ b/src/util/virresctrl.c @@ -2669,8 +2669,7 @@ virResctrlMonitorStatsSorter(const void *a, * virResctrlMonitorGetStats * * @monitor: The monitor that the statistic data will be retrieved from. - * @resource: The name for resource name. 'llc_occupancy' for cache resource. - * "mbm_total_bytes" and "mbm_local_bytes" for memory bandwidth resource. + * @resources: A string list for the monitor feature names. * @stats: Pointer of of virResctrlMonitorStatsPtr array for holding cache or * memory bandwidth usage data. * @nstats: A size_t pointer to hold the returned array length of @stats @@ -2679,14 +2678,15 @@ virResctrlMonitorStatsSorter(const void *a, * * Returns 0 on success, -1 on error. */ -static int +int virResctrlMonitorGetStats(virResctrlMonitorPtr monitor, - const char *resource, + const char **resources, virResctrlMonitorStatsPtr **stats, size_t *nstats) { int rv = -1; int ret = -1; + size_t i = 0; unsigned int val = 0; DIR *dirp = NULL; char *datapath = NULL; @@ -2744,21 +2744,23 @@ virResctrlMonitorGetStats(virResctrlMonitorPtr monitor, if (virStrToLong_uip(node_id, NULL, 0, &stat->id) < 0) goto cleanup; - rv = virFileReadValueUint(&val, "%s/%s/%s", datapath, - ent->d_name, resource); - if (rv == -2) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("File '%s/%s/%s' does not exist."), - datapath, ent->d_name, resource); - } - if (rv < 0) - goto cleanup; + for (i = 0; resources[i]; i++) { + rv = virFileReadValueUint(&val, "%s/%s/%s", datapath, + ent->d_name, resources[i]); + if (rv == -2) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("File '%s/%s/%s' does not exist."), + datapath, ent->d_name, resources[i]); + } + if (rv < 0) + goto cleanup; - if (VIR_APPEND_ELEMENT(stat->vals, stat->nvals, val) < 0) - goto cleanup; + if (VIR_APPEND_ELEMENT(stat->vals, stat->nvals, val) < 0) + goto cleanup; - if (virStringListAdd(&stat->features, resource) < 0) - goto cleanup; + if (virStringListAdd(&stat->features, resources[i]) < 0) + goto cleanup; + } if (VIR_APPEND_ELEMENT(*stats, *nstats, stat) < 0) goto cleanup; @@ -2808,6 +2810,10 @@ virResctrlMonitorGetCacheOccupancy(virResctrlMonitorPtr monitor, virResctrlMonitorStatsPtr **stats, size_t *nstats) { - return virResctrlMonitorGetStats(monitor, "llc_occupancy", - stats, nstats); + int ret = -1; + const char *features[2] = {"llc_occupancy", NULL}; + + ret = virResctrlMonitorGetStats(monitor, features, stats, nstats); + + return ret; } diff --git a/src/util/virresctrl.h b/src/util/virresctrl.h index 4c5f9f854d..ff49926b53 100644 --- a/src/util/virresctrl.h +++ b/src/util/virresctrl.h @@ -234,6 +234,12 @@ virResctrlMonitorSetAlloc(virResctrlMonitorPtr monitor, int virResctrlMonitorRemove(virResctrlMonitorPtr monitor); +int +virResctrlMonitorGetStats(virResctrlMonitorPtr monitor, + const char **resources, + virResctrlMonitorStatsPtr **stats, + size_t *nstats); + int virResctrlMonitorGetCacheOccupancy(virResctrlMonitorPtr monitor, virResctrlMonitorStatsPtr **stats, -- GitLab