Contrary to most APIs returning typed parameters, there are no constants defined for the domain stats data keys. This is was because many of the keys needs to be dynamically constructed using one or more array index values. It is possible to define constants while still supporting dynamic array indexes by simply defining the prefixes and suffixes as constants. The consuming code can then combine the constants with array index value. With this approach, it is practical to add constants for the domain stats API keys. Signed-off-by: Daniel P. Berrangé <berrange@xxxxxxxxxx> --- include/libvirt/libvirt-domain.h | 128 +++++++++++++++++++++++++++++++ src/libvirt-domain.c | 30 +------- src/qemu/qemu_driver.c | 44 +++++++---- 3 files changed, 162 insertions(+), 40 deletions(-) diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h index 5b014adcd0..7e9f998f2f 100644 --- a/include/libvirt/libvirt-domain.h +++ b/include/libvirt/libvirt-domain.h @@ -2802,6 +2802,134 @@ struct _virDomainStatsRecord { */ #define VIR_DOMAIN_STATS_STATE_REASON "state.reason" + +/** + * VIR_DOMAIN_STATS_CPU_TIME: + * + * Total cpu time spent for this domain in nanoseconds as unsigned long long. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_TIME "cpu.time" + +/** + * VIR_DOMAIN_STATS_CPU_USER: + * + * User cpu time spent in nanoseconds as unsigned long long. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_USER "cpu.user" + +/** + * VIR_DOMAIN_STATS_CPU_SYSTEM: + * + * System cpu time spent in nanoseconds as unsigned long long. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_SYSTEM "cpu.system" + +/** + * VIR_DOMAIN_STATS_CPU_HALTPOLL_SUCCESS_TIME: + * + * Halt-polling cpu usage about the VCPU polled until a virtual interrupt was + * delivered in nanoseconds as unsigned long long. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_HALTPOLL_SUCCESS_TIME "cpu.haltpoll.success.time" + +/** + * VIR_DOMAIN_STATS_CPU_HALTPOLL_FAIL_TIME: + * + * Halt-polling cpu usage about the VCPU had to schedule out (either because + * the maximum poll time was reached or it needed to yield the CPU) in + * nanoseconds as unsigned long long. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_HALTPOLL_FAIL_TIME "cpu.haltpoll.fail.time" + +/** + * VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_COUNT: + * + * The number of cache monitors for this domain as an unsigned int. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_COUNT "cpu.cache.monitor.count" + +/** + * VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_PREFIX: + * + * The parameter name prefix to access each cache monitor entry. Concatenate + * the prefix, the entry number formatted as an unsigned integer and one of + * the cache monitor suffix parameters to form a complete parameter name. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_PREFIX "cpu.cache.monitor." + +/** + * VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_NAME: + * + * The name of cache monitor as a string. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_NAME ".name" + +/** + * VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_VCPUS: + * + * Vcpu list of cache monitor as a string. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_VCPUS ".vcpus" + +/** + * VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_COUNT: + * + * The number of cache banks in cache monitor as an unsigned int. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_COUNT ".bank.count" + +/** + * VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_PREFIX: + * + * The parameter name prefix to access each cache monitor bank entry. + * Concatenate the cache monitor prefix, the cache monitor entry number + * formatted as an unsigned integer, the bank prefix, the bank entry number + * formatted as an unsigned integer and one of the cache monitor bank suffix + * parameters to form a complete parameter name. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_PREFIX ".bank." + +/** + * VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_SUFFIX_ID: + * + * Host allocated cache id for the bank as an unsigned int. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_SUFFIX_ID ".id" + +/** + * VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_SUFFIX_BYTES: + * + * The number of bytes of last level cache that the domain is using as an + * unsigned long long. + * + * Since: 11.2.0 + */ +#define VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_SUFFIX_BYTES ".bytes" + /** * virDomainStatsTypes: * diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c index b33b12374b..6b80206f25 100644 --- a/src/libvirt-domain.c +++ b/src/libvirt-domain.c @@ -12242,33 +12242,9 @@ virConnectGetDomainCapabilities(virConnectPtr conn, * parameter keys. * * VIR_DOMAIN_STATS_CPU_TOTAL: - * Return CPU statistics and usage information. The typed parameter keys - * are in this format: - * - * "cpu.time" - total cpu time spent for this domain in nanoseconds - * as unsigned long long. - * "cpu.user" - user cpu time spent in nanoseconds as unsigned long long. - * "cpu.system" - system cpu time spent in nanoseconds as unsigned long - * long. - * "cpu.haltpoll.success.time" - halt-polling cpu usage about the VCPU polled - * until a virtual interrupt was delivered in - * nanoseconds as unsigned long long. - * "cpu.haltpoll.fail.time" - halt-polling cpu usage about the VCPU had to schedule - * out (either because the maximum poll time was reached - * or it needed to yield the CPU) in nanoseconds as - * unsigned long long. - * "cpu.cache.monitor.count" - the number of cache monitors for this domain - * "cpu.cache.monitor.<num>.name" - the name of cache monitor <num> - * "cpu.cache.monitor.<num>.vcpus" - vcpu list of cache monitor <num> - * "cpu.cache.monitor.<num>.bank.count" - the number of cache banks in - * cache monitor <num> - * "cpu.cache.monitor.<num>.bank.<index>.id" - host allocated cache id for - * bank <index> in cache - * monitor <num> - * "cpu.cache.monitor.<num>.bank.<index>.bytes" - the number of bytes of - * last level cache that the - * domain is using on cache - * bank <index> + * Return CPU statistics and usage information. + * The VIR_DOMAIN_STATS_CPU_* constants define the known typed + * parameter keys. * * VIR_DOMAIN_STATS_BALLOON: * Return memory balloon device information. diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 55fc45fef7..ff149a46ef 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -16778,16 +16778,26 @@ qemuDomainGetStatsCpuCache(virQEMUDriver *driver, return; } - virTypedParamListAddUInt(params, nresdata, "cpu.cache.monitor.count"); + virTypedParamListAddUInt(params, nresdata, + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_COUNT); for (i = 0; i < nresdata; i++) { - virTypedParamListAddString(params, resdata[i]->name, "cpu.cache.monitor.%zu.name", i); - virTypedParamListAddString(params, resdata[i]->vcpus, "cpu.cache.monitor.%zu.vcpus", i); - virTypedParamListAddUInt(params, resdata[i]->nstats, "cpu.cache.monitor.%zu.bank.count", i); + virTypedParamListAddString(params, resdata[i]->name, + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_PREFIX "%zu" + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_NAME, i); + virTypedParamListAddString(params, resdata[i]->vcpus, + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_PREFIX "%zu" + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_VCPUS, i); + virTypedParamListAddUInt(params, resdata[i]->nstats, + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_PREFIX "%zu" + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_COUNT, i); for (j = 0; j < resdata[i]->nstats; j++) { - virTypedParamListAddUInt(params, resdata[i]->stats[j]->id, - "cpu.cache.monitor.%zu.bank.%zu.id", i, j); + virTypedParamListAddUInt( + params, resdata[i]->stats[j]->id, + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_PREFIX "%zu" + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_PREFIX "%zu" + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_SUFFIX_ID, i, j); /* 'resdata[i]->stats[j]->vals[0]' keeps the value of how many last * level cache in bank j currently occupied by the vcpus listed in @@ -16798,8 +16808,11 @@ qemuDomainGetStatsCpuCache(virQEMUDriver *driver, * than 4G bytes in size, to keep the 'domstats' interface * historically consistent, it is safe to report the value with a * truncated 'UInt' data type here. */ - virTypedParamListAddUInt(params, (unsigned int)resdata[i]->stats[j]->vals[0], - "cpu.cache.monitor.%zu.bank.%zu.bytes", i, j); + virTypedParamListAddUInt( + params, (unsigned int)resdata[i]->stats[j]->vals[0], + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_PREFIX "%zu" + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_PREFIX "%zu" + VIR_DOMAIN_STATS_CPU_CACHE_MONITOR_SUFFIX_BANK_SUFFIX_BYTES, i, j); } } @@ -16822,11 +16835,14 @@ qemuDomainGetStatsCpuCgroup(virDomainObj *dom, return; if (virCgroupGetCpuacctUsage(priv->cgroup, &cpu_time) == 0) - virTypedParamListAddULLong(params, cpu_time, "cpu.time"); + virTypedParamListAddULLong(params, cpu_time, + VIR_DOMAIN_STATS_CPU_TIME); if (virCgroupGetCpuacctStat(priv->cgroup, &user_time, &sys_time) == 0) { - virTypedParamListAddULLong(params, user_time, "cpu.user"); - virTypedParamListAddULLong(params, sys_time, "cpu.system"); + virTypedParamListAddULLong(params, user_time, + VIR_DOMAIN_STATS_CPU_USER); + virTypedParamListAddULLong(params, sys_time, + VIR_DOMAIN_STATS_CPU_SYSTEM); } } @@ -16933,8 +16949,10 @@ qemuDomainGetStatsCpuHaltPollTime(virDomainObj *dom, virHostCPUGetHaltPollTime(dom->pid, &haltPollSuccess, &haltPollFail) < 0) return; - virTypedParamListAddULLong(params, haltPollSuccess, "cpu.haltpoll.success.time"); - virTypedParamListAddULLong(params, haltPollFail, "cpu.haltpoll.fail.time"); + virTypedParamListAddULLong(params, haltPollSuccess, + VIR_DOMAIN_STATS_CPU_HALTPOLL_SUCCESS_TIME); + virTypedParamListAddULLong(params, haltPollFail, + VIR_DOMAIN_STATS_CPU_HALTPOLL_FAIL_TIME); return; } -- 2.48.1