From b9f88dc0715a6b47afc4b06d569d1d693cdb6fc6 Mon Sep 17 00:00:00 2001 From: Mark Kanda Date: Tue, 15 Feb 2022 09:04:31 -0600 Subject: qmp: Support for querying stats Gathering statistics is important for development, for monitoring and for performance measurement. There are tools such as kvm_stat that do this and they rely on the _user_ knowing the interesting data points rather than the tool (which can treat them as opaque). The commands introduced in this commit introduce QMP support for querying stats; the goal is to take the capabilities of these tools and making them available throughout the whole virtualization stack, so that one can observe, monitor and measure virtual machines without having shell access + root on the host that runs them. query-stats returns a list of all stats per target type (only VM and vCPU to start); future commits add extra options for specifying stat names, vCPU qom paths, and providers. All these are used by the HMP command "info stats". Because of the development usecases around statistics, a good HMP interface is important. query-stats-schemas returns a list of stats included in each target type, with an option for specifying the provider. The concepts in the schema are based on the KVM binary stats' own introspection data, just translated to QAPI. There are two reasons to have a separate schema that is not tied to the QAPI schema. The first is the contents of the schemas: the new introspection data provides different information than the QAPI data, namely unit of measurement, how the numbers are gathered and change (peak/instant/cumulative/histogram), and histogram bucket sizes. There's really no reason to have this kind of metadata in the QAPI introspection schema (except possibly for the unit of measure, but there's a very weak justification). Another reason is the dynamicity of the schema. The QAPI introspection data is very much static; and while QOM is somewhat more dynamic, generally we consider that to be a bug rather than a feature these days. On the other hand, the statistics that are exposed by QEMU might be passed through from another source, such as KVM, and the disadvantages of manually updating the QAPI schema for outweight the benefits from vetting the statistics and filtering out anything that seems "too unstable". Running old QEMU with new kernel is a supported usecase; if old QEMU cannot expose statistics from a new kernel, or if a kernel developer needs to change QEMU before gathering new info from the new kernel, then that is a poor user interface. The framework provides a method to register callbacks for these QMP commands. Most of the work in fact is done by the callbacks, and a large majority of this patch is new QAPI structs and commands. Examples (with KVM stats): - Query all VM stats: { "execute": "query-stats", "arguments" : { "target": "vm" } } { "return": [ { "provider": "kvm", "stats": [ { "name": "max_mmu_page_hash_collisions", "value": 0 }, { "name": "max_mmu_rmap_size", "value": 0 }, { "name": "nx_lpage_splits", "value": 148 }, ... ] }, { "provider": "xyz", "stats": [ ... ] } ] } - Query all vCPU stats: { "execute": "query-stats", "arguments" : { "target": "vcpu" } } { "return": [ { "provider": "kvm", "qom_path": "/machine/unattached/device[0]" "stats": [ { "name": "guest_mode", "value": 0 }, { "name": "directed_yield_successful", "value": 0 }, { "name": "directed_yield_attempted", "value": 106 }, ... ] }, { "provider": "kvm", "qom_path": "/machine/unattached/device[1]" "stats": [ { "name": "guest_mode", "value": 0 }, { "name": "directed_yield_successful", "value": 0 }, { "name": "directed_yield_attempted", "value": 106 }, ... ] }, ] } - Retrieve the schemas: { "execute": "query-stats-schemas" } { "return": [ { "provider": "kvm", "target": "vcpu", "stats": [ { "name": "guest_mode", "unit": "none", "base": 10, "exponent": 0, "type": "instant" }, { "name": "directed_yield_successful", "unit": "none", "base": 10, "exponent": 0, "type": "cumulative" }, ... ] }, { "provider": "kvm", "target": "vm", "stats": [ { "name": "max_mmu_page_hash_collisions", "unit": "none", "base": 10, "exponent": 0, "type": "peak" }, ... ] }, { "provider": "xyz", "target": "vm", "stats": [ ... ] } ] } Signed-off-by: Mark Kanda Reviewed-by: Markus Armbruster Signed-off-by: Paolo Bonzini --- include/monitor/stats.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 include/monitor/stats.h (limited to 'include') diff --git a/include/monitor/stats.h b/include/monitor/stats.h new file mode 100644 index 0000000000..912eeadb2f --- /dev/null +++ b/include/monitor/stats.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 Oracle and/or its affiliates. + * + * This work is licensed under the terms of the GNU GPL, version 2. + * See the COPYING file in the top-level directory. + */ + +#ifndef STATS_H +#define STATS_H + +#include "qapi/qapi-types-stats.h" + +typedef void StatRetrieveFunc(StatsResultList **result, StatsTarget target, + Error **errp); +typedef void SchemaRetrieveFunc(StatsSchemaList **result, Error **errp); + +/* + * Register callbacks for the QMP query-stats command. + * + * @stats_fn: routine to query stats: + * @schema_fn: routine to query stat schemas: + */ +void add_stats_callbacks(StatRetrieveFunc *stats_fn, + SchemaRetrieveFunc *schemas_fn); + +/* + * Helper routines for adding stats entries to the results lists. + */ +void add_stats_entry(StatsResultList **, StatsProvider, const char *id, + StatsList *stats_list); +void add_stats_schema(StatsSchemaList **, StatsProvider, StatsTarget, + StatsSchemaValueList *); + +#endif /* STATS_H */ -- cgit 1.4.1 From 467ef823d83ed7ba68cc92e1a23938726b8c4e9d Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Tue, 26 Apr 2022 14:59:44 +0200 Subject: qmp: add filtering of statistics by target vCPU Introduce a simple filtering of statistics, that allows to retrieve statistics for a subset of the guest vCPUs. This will be used for example by the HMP monitor, in order to retrieve the statistics for the currently selected CPU. Example: { "execute": "query-stats", "arguments": { "target": "vcpu", "vcpus": [ "/machine/unattached/device[2]", "/machine/unattached/device[4]" ] } } Extracted from a patch by Mark Kanda. Reviewed-by: Markus Armbruster Signed-off-by: Paolo Bonzini --- accel/kvm/kvm-all.c | 9 +++++++-- include/monitor/stats.h | 11 ++++++++++- monitor/qmp-cmds.c | 34 +++++++++++++++++++++++++++++++++- qapi/stats.json | 20 +++++++++++++++++--- 4 files changed, 67 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 7cc9e33bab..547de842fd 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -2311,7 +2311,8 @@ bool kvm_dirty_ring_enabled(void) return kvm_state->kvm_dirty_ring_size ? true : false; } -static void query_stats_cb(StatsResultList **result, StatsTarget target, Error **errp); +static void query_stats_cb(StatsResultList **result, StatsTarget target, + strList *targets, Error **errp); static void query_stats_schemas_cb(StatsSchemaList **result, Error **errp); static int kvm_init(MachineState *ms) @@ -4038,7 +4039,8 @@ static void query_stats_schema_vcpu(CPUState *cpu, run_on_cpu_data data) close(stats_fd); } -static void query_stats_cb(StatsResultList **result, StatsTarget target, Error **errp) +static void query_stats_cb(StatsResultList **result, StatsTarget target, + strList *targets, Error **errp) { KVMState *s = kvm_state; CPUState *cpu; @@ -4062,6 +4064,9 @@ static void query_stats_cb(StatsResultList **result, StatsTarget target, Error * stats_args.result.stats = result; stats_args.errp = errp; CPU_FOREACH(cpu) { + if (!apply_str_list_filter(cpu->parent_obj.canonical_path, targets)) { + continue; + } run_on_cpu(cpu, query_stats_vcpu, RUN_ON_CPU_HOST_PTR(&stats_args)); } break; diff --git a/include/monitor/stats.h b/include/monitor/stats.h index 912eeadb2f..8c50feeaa9 100644 --- a/include/monitor/stats.h +++ b/include/monitor/stats.h @@ -11,7 +11,7 @@ #include "qapi/qapi-types-stats.h" typedef void StatRetrieveFunc(StatsResultList **result, StatsTarget target, - Error **errp); + strList *targets, Error **errp); typedef void SchemaRetrieveFunc(StatsSchemaList **result, Error **errp); /* @@ -31,4 +31,13 @@ void add_stats_entry(StatsResultList **, StatsProvider, const char *id, void add_stats_schema(StatsSchemaList **, StatsProvider, StatsTarget, StatsSchemaValueList *); +/* + * True if a string matches the filter passed to the stats_fn callabck, + * false otherwise. + * + * Note that an empty list means no filtering, i.e. all strings will + * return true. + */ +bool apply_str_list_filter(const char *string, strList *list); + #endif /* STATS_H */ diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c index a6ac8d7473..5f8f1e620b 100644 --- a/monitor/qmp-cmds.c +++ b/monitor/qmp-cmds.c @@ -468,9 +468,26 @@ static bool invoke_stats_cb(StatsCallbacks *entry, StatsFilter *filter, Error **errp) { + strList *targets = NULL; ERRP_GUARD(); - entry->stats_cb(stats_results, filter->target, errp); + switch (filter->target) { + case STATS_TARGET_VM: + break; + case STATS_TARGET_VCPU: + if (filter->u.vcpu.has_vcpus) { + if (!filter->u.vcpu.vcpus) { + /* No targets allowed? Return no statistics. */ + return true; + } + targets = filter->u.vcpu.vcpus; + } + break; + default: + abort(); + } + + entry->stats_cb(stats_results, filter->target, targets, errp); if (*errp) { qapi_free_StatsResultList(*stats_results); *stats_results = NULL; @@ -536,3 +553,18 @@ void add_stats_schema(StatsSchemaList **schema_results, entry->stats = stats_list; QAPI_LIST_PREPEND(*schema_results, entry); } + +bool apply_str_list_filter(const char *string, strList *list) +{ + strList *str_list = NULL; + + if (!list) { + return true; + } + for (str_list = list; str_list; str_list = str_list->next) { + if (g_str_equal(string, str_list->value)) { + return true; + } + } + return false; +} diff --git a/qapi/stats.json b/qapi/stats.json index df7c4d886c..8c9abb57f1 100644 --- a/qapi/stats.json +++ b/qapi/stats.json @@ -69,16 +69,30 @@ { 'enum': 'StatsTarget', 'data': [ 'vm', 'vcpu' ] } +## +# @StatsVCPUFilter: +# +# @vcpus: list of QOM paths for the desired vCPU objects. +# +# Since: 7.1 +## +{ 'struct': 'StatsVCPUFilter', + 'data': { '*vcpus': [ 'str' ] } } + ## # @StatsFilter: # # The arguments to the query-stats command; specifies a target for which to -# request statistics. +# request statistics and optionally the required subset of information for +# that target: +# - which vCPUs to request statistics for # # Since: 7.1 ## -{ 'struct': 'StatsFilter', - 'data': { 'target': 'StatsTarget' } } +{ 'union': 'StatsFilter', + 'base': { 'target': 'StatsTarget' }, + 'discriminator': 'target', + 'data': { 'vcpu': 'StatsVCPUFilter' } } ## # @StatsValue: -- cgit 1.4.1 From cfb344892209783a600e80053dba1cfeee4bd16a Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Wed, 25 May 2022 15:38:48 +0200 Subject: cutils: add functions for IEC and SI prefixes Extract the knowledge of IEC and SI prefixes out of size_to_str and freq_to_str, so that it can be reused when printing statistics. Signed-off-by: Paolo Bonzini --- include/qemu/cutils.h | 18 +++++++++++++++++ tests/unit/test-cutils.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 34 ++++++++++++++++++++++--------- 3 files changed, 95 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h index 40e10e19a7..d3e532b64c 100644 --- a/include/qemu/cutils.h +++ b/include/qemu/cutils.h @@ -1,6 +1,24 @@ #ifndef QEMU_CUTILS_H #define QEMU_CUTILS_H +/* + * si_prefix: + * @exp10: exponent of 10, a multiple of 3 between -18 and 18 inclusive. + * + * Return a SI prefix (n, u, m, K, M, etc.) corresponding + * to the given exponent of 10. + */ +const char *si_prefix(unsigned int exp10); + +/* + * iec_binary_prefix: + * @exp2: exponent of 2, a multiple of 10 between 0 and 60 inclusive. + * + * Return an IEC binary prefix (Ki, Mi, etc.) corresponding + * to the given exponent of 2. + */ +const char *iec_binary_prefix(unsigned int exp2); + /** * pstrcpy: * @buf: buffer to copy string into diff --git a/tests/unit/test-cutils.c b/tests/unit/test-cutils.c index 98671f1ac3..f5b780f012 100644 --- a/tests/unit/test-cutils.c +++ b/tests/unit/test-cutils.c @@ -2450,6 +2450,50 @@ static void test_qemu_strtosz_metric(void) g_assert(endptr == str + 7); } +static void test_freq_to_str(void) +{ + g_assert_cmpstr(freq_to_str(999), ==, "999 Hz"); + g_assert_cmpstr(freq_to_str(1000), ==, "1 KHz"); + g_assert_cmpstr(freq_to_str(1010), ==, "1.01 KHz"); +} + +static void test_size_to_str(void) +{ + g_assert_cmpstr(size_to_str(0), ==, "0 B"); + g_assert_cmpstr(size_to_str(1), ==, "1 B"); + g_assert_cmpstr(size_to_str(1016), ==, "0.992 KiB"); + g_assert_cmpstr(size_to_str(1024), ==, "1 KiB"); + g_assert_cmpstr(size_to_str(512ull << 20), ==, "512 MiB"); +} + +static void test_iec_binary_prefix(void) +{ + g_assert_cmpstr(iec_binary_prefix(0), ==, ""); + g_assert_cmpstr(iec_binary_prefix(10), ==, "Ki"); + g_assert_cmpstr(iec_binary_prefix(20), ==, "Mi"); + g_assert_cmpstr(iec_binary_prefix(30), ==, "Gi"); + g_assert_cmpstr(iec_binary_prefix(40), ==, "Ti"); + g_assert_cmpstr(iec_binary_prefix(50), ==, "Pi"); + g_assert_cmpstr(iec_binary_prefix(60), ==, "Ei"); +} + +static void test_si_prefix(void) +{ + g_assert_cmpstr(si_prefix(-18), ==, "a"); + g_assert_cmpstr(si_prefix(-15), ==, "f"); + g_assert_cmpstr(si_prefix(-12), ==, "p"); + g_assert_cmpstr(si_prefix(-9), ==, "n"); + g_assert_cmpstr(si_prefix(-6), ==, "u"); + g_assert_cmpstr(si_prefix(-3), ==, "m"); + g_assert_cmpstr(si_prefix(0), ==, ""); + g_assert_cmpstr(si_prefix(3), ==, "K"); + g_assert_cmpstr(si_prefix(6), ==, "M"); + g_assert_cmpstr(si_prefix(9), ==, "G"); + g_assert_cmpstr(si_prefix(12), ==, "T"); + g_assert_cmpstr(si_prefix(15), ==, "P"); + g_assert_cmpstr(si_prefix(18), ==, "E"); +} + int main(int argc, char **argv) { g_test_init(&argc, &argv, NULL); @@ -2729,5 +2773,13 @@ int main(int argc, char **argv) g_test_add_func("/cutils/strtosz/metric", test_qemu_strtosz_metric); + g_test_add_func("/cutils/size_to_str", + test_size_to_str); + g_test_add_func("/cutils/freq_to_str", + test_freq_to_str); + g_test_add_func("/cutils/iec_binary_prefix", + test_iec_binary_prefix); + g_test_add_func("/cutils/si_prefix", + test_si_prefix); return g_test_run(); } diff --git a/util/cutils.c b/util/cutils.c index a58bcfd80e..6d04e52907 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -872,6 +872,25 @@ int parse_debug_env(const char *name, int max, int initial) return debug; } +const char *si_prefix(unsigned int exp10) +{ + static const char *prefixes[] = { + "a", "f", "p", "n", "u", "m", "", "K", "M", "G", "T", "P", "E" + }; + + exp10 += 18; + assert(exp10 % 3 == 0 && exp10 / 3 < ARRAY_SIZE(prefixes)); + return prefixes[exp10 / 3]; +} + +const char *iec_binary_prefix(unsigned int exp2) +{ + static const char *prefixes[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei" }; + + assert(exp2 % 10 == 0 && exp2 / 10 < ARRAY_SIZE(prefixes)); + return prefixes[exp2 / 10]; +} + /* * Return human readable string for size @val. * @val can be anything that uint64_t allows (no more than "16 EiB"). @@ -880,7 +899,6 @@ int parse_debug_env(const char *name, int max, int initial) */ char *size_to_str(uint64_t val) { - static const char *suffixes[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei" }; uint64_t div; int i; @@ -891,25 +909,23 @@ char *size_to_str(uint64_t val) * (see e41b509d68afb1f for more info) */ frexp(val / (1000.0 / 1024.0), &i); - i = (i - 1) / 10; - div = 1ULL << (i * 10); + i = (i - 1) / 10 * 10; + div = 1ULL << i; - return g_strdup_printf("%0.3g %sB", (double)val / div, suffixes[i]); + return g_strdup_printf("%0.3g %sB", (double)val / div, iec_binary_prefix(i)); } char *freq_to_str(uint64_t freq_hz) { - static const char *const suffixes[] = { "", "K", "M", "G", "T", "P", "E" }; double freq = freq_hz; - size_t idx = 0; + size_t exp10 = 0; while (freq >= 1000.0) { freq /= 1000.0; - idx++; + exp10 += 3; } - assert(idx < ARRAY_SIZE(suffixes)); - return g_strdup_printf("%0.3g %sHz", freq, suffixes[idx]); + return g_strdup_printf("%0.3g %sHz", freq, si_prefix(exp10)); } int qemu_pstrcmp0(const char **str1, const char **str2) -- cgit 1.4.1 From 433815f5bdf71b5b2c47d9f1104816b95b551c49 Mon Sep 17 00:00:00 2001 From: Mark Kanda Date: Tue, 26 Apr 2022 12:17:35 +0200 Subject: hmp: add basic "info stats" implementation Add an HMP command to retrieve statistics collected at run-time. The command will retrieve and print either all VM-level statistics, or all vCPU-level statistics for the currently selected CPU. Reviewed-by: Dr. David Alan Gilbert Signed-off-by: Paolo Bonzini --- hmp-commands-info.hx | 13 ++++ include/monitor/hmp.h | 1 + monitor/hmp-cmds.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 204 insertions(+) (limited to 'include') diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx index 834bed089e..28757768f7 100644 --- a/hmp-commands-info.hx +++ b/hmp-commands-info.hx @@ -894,3 +894,16 @@ SRST ``info via`` Show guest mos6522 VIA devices. ERST + + { + .name = "stats", + .args_type = "target:s", + .params = "target", + .help = "show statistics; target is either vm or vcpu", + .cmd = hmp_info_stats, + }, + +SRST + ``stats`` + Show runtime-collected statistics +ERST diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h index 96d014826a..2e89a97bd6 100644 --- a/include/monitor/hmp.h +++ b/include/monitor/hmp.h @@ -133,5 +133,6 @@ void hmp_info_dirty_rate(Monitor *mon, const QDict *qdict); void hmp_calc_dirty_rate(Monitor *mon, const QDict *qdict); void hmp_human_readable_text_helper(Monitor *mon, HumanReadableText *(*qmp_handler)(Error **)); +void hmp_info_stats(Monitor *mon, const QDict *qdict); #endif diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c index 622c783c32..04d5ee8fb7 100644 --- a/monitor/hmp-cmds.c +++ b/monitor/hmp-cmds.c @@ -40,6 +40,7 @@ #include "qapi/qapi-commands-pci.h" #include "qapi/qapi-commands-rocker.h" #include "qapi/qapi-commands-run-state.h" +#include "qapi/qapi-commands-stats.h" #include "qapi/qapi-commands-tpm.h" #include "qapi/qapi-commands-ui.h" #include "qapi/qapi-visit-net.h" @@ -52,6 +53,7 @@ #include "ui/console.h" #include "qemu/cutils.h" #include "qemu/error-report.h" +#include "hw/core/cpu.h" #include "hw/intc/intc.h" #include "migration/snapshot.h" #include "migration/misc.h" @@ -2239,3 +2241,191 @@ void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict) } hmp_handle_error(mon, err); } + +static void print_stats_schema_value(Monitor *mon, StatsSchemaValue *value) +{ + const char *unit = NULL; + monitor_printf(mon, " %s (%s%s", value->name, StatsType_str(value->type), + value->has_unit || value->exponent ? ", " : ""); + + if (value->has_unit) { + if (value->unit == STATS_UNIT_SECONDS) { + unit = "s"; + } else if (value->unit == STATS_UNIT_BYTES) { + unit = "B"; + } + } + + if (unit && value->base == 10 && + value->exponent >= -18 && value->exponent <= 18 && + value->exponent % 3 == 0) { + monitor_printf(mon, "%s", si_prefix(value->exponent)); + } else if (unit && value->base == 2 && + value->exponent >= 0 && value->exponent <= 60 && + value->exponent % 10 == 0) { + + monitor_printf(mon, "%s", iec_binary_prefix(value->exponent)); + } else if (value->exponent) { + /* Use exponential notation and write the unit's English name */ + monitor_printf(mon, "* %d^%d%s", + value->base, value->exponent, + value->has_unit ? " " : ""); + unit = NULL; + } + + if (value->has_unit) { + monitor_printf(mon, "%s", unit ? unit : StatsUnit_str(value->unit)); + } + + /* Print bucket size for linear histograms */ + if (value->type == STATS_TYPE_LINEAR_HISTOGRAM && value->has_bucket_size) { + monitor_printf(mon, ", bucket size=%d", value->bucket_size); + } + monitor_printf(mon, ")"); +} + +static StatsSchemaValueList *find_schema_value_list( + StatsSchemaList *list, StatsProvider provider, + StatsTarget target) +{ + StatsSchemaList *node; + + for (node = list; node; node = node->next) { + if (node->value->provider == provider && + node->value->target == target) { + return node->value->stats; + } + } + return NULL; +} + +static void print_stats_results(Monitor *mon, StatsTarget target, + StatsResult *result, + StatsSchemaList *schema) +{ + /* Find provider schema */ + StatsSchemaValueList *schema_value_list = + find_schema_value_list(schema, result->provider, target); + StatsList *stats_list; + + if (!schema_value_list) { + monitor_printf(mon, "failed to find schema list for %s\n", + StatsProvider_str(result->provider)); + return; + } + + monitor_printf(mon, "provider: %s\n", + StatsProvider_str(result->provider)); + + for (stats_list = result->stats; stats_list; + stats_list = stats_list->next, + schema_value_list = schema_value_list->next) { + + Stats *stats = stats_list->value; + StatsValue *stats_value = stats->value; + StatsSchemaValue *schema_value = schema_value_list->value; + + /* Find schema entry */ + while (!g_str_equal(stats->name, schema_value->name)) { + if (!schema_value_list->next) { + monitor_printf(mon, "failed to find schema entry for %s\n", + stats->name); + return; + } + schema_value_list = schema_value_list->next; + schema_value = schema_value_list->value; + } + + print_stats_schema_value(mon, schema_value); + + if (stats_value->type == QTYPE_QNUM) { + monitor_printf(mon, ": %" PRId64 "\n", stats_value->u.scalar); + } else if (stats_value->type == QTYPE_QLIST) { + uint64List *list; + int i; + + monitor_printf(mon, ": "); + for (list = stats_value->u.list, i = 1; + list; + list = list->next, i++) { + monitor_printf(mon, "[%d]=%" PRId64 " ", i, list->value); + } + monitor_printf(mon, "\n"); + } + } +} + +/* Create the StatsFilter that is needed for an "info stats" invocation. */ +static StatsFilter *stats_filter(StatsTarget target, int cpu_index) +{ + StatsFilter *filter = g_malloc0(sizeof(*filter)); + + filter->target = target; + switch (target) { + case STATS_TARGET_VM: + break; + case STATS_TARGET_VCPU: + { + strList *vcpu_list = NULL; + CPUState *cpu = qemu_get_cpu(cpu_index); + char *canonical_path = object_get_canonical_path(OBJECT(cpu)); + + QAPI_LIST_PREPEND(vcpu_list, canonical_path); + filter->u.vcpu.has_vcpus = true; + filter->u.vcpu.vcpus = vcpu_list; + break; + } + default: + break; + } + return filter; +} + +void hmp_info_stats(Monitor *mon, const QDict *qdict) +{ + const char *target_str = qdict_get_str(qdict, "target"); + StatsTarget target; + Error *err = NULL; + g_autoptr(StatsSchemaList) schema = NULL; + g_autoptr(StatsResultList) stats = NULL; + g_autoptr(StatsFilter) filter = NULL; + StatsResultList *entry; + + target = qapi_enum_parse(&StatsTarget_lookup, target_str, -1, &err); + if (err) { + monitor_printf(mon, "invalid stats target %s\n", target_str); + goto exit_no_print; + } + + schema = qmp_query_stats_schemas(&err); + if (err) { + goto exit; + } + + switch (target) { + case STATS_TARGET_VM: + filter = stats_filter(target, -1); + break; + case STATS_TARGET_VCPU: {} + int cpu_index = monitor_get_cpu_index(mon); + filter = stats_filter(target, cpu_index); + break; + default: + abort(); + } + + stats = qmp_query_stats(filter, &err); + if (err) { + goto exit; + } + for (entry = stats; entry; entry = entry->next) { + print_stats_results(mon, target, entry->value, schema); + } + +exit: + if (err) { + monitor_printf(mon, "%s\n", error_get_pretty(err)); + } +exit_no_print: + error_free(err); +} -- cgit 1.4.1 From 068cc51d42f771d2a453d628c10e199e7d104edd Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Tue, 26 Apr 2022 11:49:33 +0200 Subject: qmp: add filtering of statistics by provider Allow retrieving the statistics from a specific provider only. This can be used in the future by HMP commands such as "info sync-profile" or "info profile". The next patch also adds filter-by-provider capabilities to the HMP equivalent of query-stats, "info stats". Example: { "execute": "query-stats", "arguments": { "target": "vm", "providers": [ { "provider": "kvm" } ] } } The QAPI is a bit more verbose than just a list of StatsProvider, so that it can be subsequently extended with filtering of statistics by name. If a provider is specified more than once in the filter, each request will be included separately in the output. Extracted from a patch by Mark Kanda. Reviewed-by: Markus Armbruster Reviewed-by: Dr. David Alan Gilbert Signed-off-by: Paolo Bonzini --- accel/kvm/kvm-all.c | 3 ++- include/monitor/stats.h | 4 +++- monitor/hmp-cmds.c | 2 +- monitor/qmp-cmds.c | 41 ++++++++++++++++++++++++++++++++--------- qapi/stats.json | 19 +++++++++++++++++-- 5 files changed, 55 insertions(+), 14 deletions(-) (limited to 'include') diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 547de842fd..2e819beaeb 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -2644,7 +2644,8 @@ static int kvm_init(MachineState *ms) } if (kvm_check_extension(kvm_state, KVM_CAP_BINARY_STATS_FD)) { - add_stats_callbacks(query_stats_cb, query_stats_schemas_cb); + add_stats_callbacks(STATS_PROVIDER_KVM, query_stats_cb, + query_stats_schemas_cb); } return 0; diff --git a/include/monitor/stats.h b/include/monitor/stats.h index 8c50feeaa9..80a523dd29 100644 --- a/include/monitor/stats.h +++ b/include/monitor/stats.h @@ -17,10 +17,12 @@ typedef void SchemaRetrieveFunc(StatsSchemaList **result, Error **errp); /* * Register callbacks for the QMP query-stats command. * + * @provider: stats provider checked against QMP command arguments * @stats_fn: routine to query stats: * @schema_fn: routine to query stat schemas: */ -void add_stats_callbacks(StatRetrieveFunc *stats_fn, +void add_stats_callbacks(StatsProvider provider, + StatRetrieveFunc *stats_fn, SchemaRetrieveFunc *schemas_fn); /* diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c index 04d5ee8fb7..9180cf1841 100644 --- a/monitor/hmp-cmds.c +++ b/monitor/hmp-cmds.c @@ -2397,7 +2397,7 @@ void hmp_info_stats(Monitor *mon, const QDict *qdict) goto exit_no_print; } - schema = qmp_query_stats_schemas(&err); + schema = qmp_query_stats_schemas(false, STATS_PROVIDER__MAX, &err); if (err) { goto exit; } diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c index 5f8f1e620b..e49ab345d7 100644 --- a/monitor/qmp-cmds.c +++ b/monitor/qmp-cmds.c @@ -445,6 +445,7 @@ HumanReadableText *qmp_x_query_irq(Error **errp) } typedef struct StatsCallbacks { + StatsProvider provider; StatRetrieveFunc *stats_cb; SchemaRetrieveFunc *schemas_cb; QTAILQ_ENTRY(StatsCallbacks) next; @@ -453,10 +454,12 @@ typedef struct StatsCallbacks { static QTAILQ_HEAD(, StatsCallbacks) stats_callbacks = QTAILQ_HEAD_INITIALIZER(stats_callbacks); -void add_stats_callbacks(StatRetrieveFunc *stats_fn, +void add_stats_callbacks(StatsProvider provider, + StatRetrieveFunc *stats_fn, SchemaRetrieveFunc *schemas_fn) { StatsCallbacks *entry = g_new(StatsCallbacks, 1); + entry->provider = provider; entry->stats_cb = stats_fn; entry->schemas_cb = schemas_fn; @@ -465,12 +468,18 @@ void add_stats_callbacks(StatRetrieveFunc *stats_fn, static bool invoke_stats_cb(StatsCallbacks *entry, StatsResultList **stats_results, - StatsFilter *filter, + StatsFilter *filter, StatsRequest *request, Error **errp) { strList *targets = NULL; ERRP_GUARD(); + if (request) { + if (request->provider != entry->provider) { + return true; + } + } + switch (filter->target) { case STATS_TARGET_VM: break; @@ -500,27 +509,41 @@ StatsResultList *qmp_query_stats(StatsFilter *filter, Error **errp) { StatsResultList *stats_results = NULL; StatsCallbacks *entry; + StatsRequestList *request; QTAILQ_FOREACH(entry, &stats_callbacks, next) { - if (!invoke_stats_cb(entry, &stats_results, filter, errp)) { - break; + if (filter->has_providers) { + for (request = filter->providers; request; request = request->next) { + if (!invoke_stats_cb(entry, &stats_results, filter, + request->value, errp)) { + break; + } + } + } else { + if (!invoke_stats_cb(entry, &stats_results, filter, NULL, errp)) { + break; + } } } return stats_results; } -StatsSchemaList *qmp_query_stats_schemas(Error **errp) +StatsSchemaList *qmp_query_stats_schemas(bool has_provider, + StatsProvider provider, + Error **errp) { StatsSchemaList *stats_results = NULL; StatsCallbacks *entry; ERRP_GUARD(); QTAILQ_FOREACH(entry, &stats_callbacks, next) { - entry->schemas_cb(&stats_results, errp); - if (*errp) { - qapi_free_StatsSchemaList(stats_results); - return NULL; + if (!has_provider || provider == entry->provider) { + entry->schemas_cb(&stats_results, errp); + if (*errp) { + qapi_free_StatsSchemaList(stats_results); + return NULL; + } } } diff --git a/qapi/stats.json b/qapi/stats.json index 8c9abb57f1..503918ea4c 100644 --- a/qapi/stats.json +++ b/qapi/stats.json @@ -69,6 +69,18 @@ { 'enum': 'StatsTarget', 'data': [ 'vm', 'vcpu' ] } +## +# @StatsRequest: +# +# Indicates a set of statistics that should be returned by query-stats. +# +# @provider: provider for which to return statistics. +# +# Since: 7.1 +## +{ 'struct': 'StatsRequest', + 'data': { 'provider': 'StatsProvider' } } + ## # @StatsVCPUFilter: # @@ -86,11 +98,14 @@ # request statistics and optionally the required subset of information for # that target: # - which vCPUs to request statistics for +# - which providers to request statistics from # # Since: 7.1 ## { 'union': 'StatsFilter', - 'base': { 'target': 'StatsTarget' }, + 'base': { + 'target': 'StatsTarget', + '*providers': [ 'StatsRequest' ] }, 'discriminator': 'target', 'data': { 'vcpu': 'StatsVCPUFilter' } } @@ -226,5 +241,5 @@ # Since: 7.1 ## { 'command': 'query-stats-schemas', - 'data': { }, + 'data': { '*provider': 'StatsProvider' }, 'returns': [ 'StatsSchema' ] } -- cgit 1.4.1 From cf7405bc0228c795557e19bacbaa3b145bb17370 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Tue, 24 May 2022 19:13:16 +0200 Subject: qmp: add filtering of statistics by name Allow retrieving only a subset of statistics. This can be useful for example in order to plot a subset of the statistics many times a second: KVM publishes ~40 statistics for each vCPU on x86; retrieving and serializing all of them would be useless. Another use will be in HMP in the following patch; implementing the filter in the backend is easy enough that it was deemed okay to make this a public interface. Example: { "execute": "query-stats", "arguments": { "target": "vcpu", "vcpus": [ "/machine/unattached/device[2]", "/machine/unattached/device[4]" ], "providers": [ { "provider": "kvm", "names": [ "l1d_flush", "exits" ] } } } { "return": { "vcpus": [ { "path": "/machine/unattached/device[2]" "providers": [ { "provider": "kvm", "stats": [ { "name": "l1d_flush", "value": 41213 }, { "name": "exits", "value": 74291 } ] } ] }, { "path": "/machine/unattached/device[4]" "providers": [ { "provider": "kvm", "stats": [ { "name": "l1d_flush", "value": 16132 }, { "name": "exits", "value": 57922 } ] } ] } ] } } Extracted from a patch by Mark Kanda. Reviewed-by: Dr. David Alan Gilbert Signed-off-by: Paolo Bonzini --- accel/kvm/kvm-all.c | 17 +++++++++++------ include/monitor/stats.h | 2 +- monitor/qmp-cmds.c | 7 ++++++- qapi/stats.json | 6 +++++- 4 files changed, 23 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 2e819beaeb..ba3210b1c1 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -2312,7 +2312,7 @@ bool kvm_dirty_ring_enabled(void) } static void query_stats_cb(StatsResultList **result, StatsTarget target, - strList *targets, Error **errp); + strList *names, strList *targets, Error **errp); static void query_stats_schemas_cb(StatsSchemaList **result, Error **errp); static int kvm_init(MachineState *ms) @@ -3713,6 +3713,7 @@ typedef struct StatsArgs { StatsResultList **stats; StatsSchemaList **schema; } result; + strList *names; Error **errp; } StatsArgs; @@ -3916,7 +3917,7 @@ static StatsDescriptors *find_stats_descriptors(StatsTarget target, int stats_fd } static void query_stats(StatsResultList **result, StatsTarget target, - int stats_fd, Error **errp) + strList *names, int stats_fd, Error **errp) { struct kvm_stats_desc *kvm_stats_desc; struct kvm_stats_header *kvm_stats_header; @@ -3958,6 +3959,9 @@ static void query_stats(StatsResultList **result, StatsTarget target, /* Add entry to the list */ stats = (void *)stats_data + pdesc->offset; + if (!apply_str_list_filter(pdesc->name, names)) { + continue; + } stats_list = add_kvmstat_entry(pdesc, stats, stats_list, errp); } @@ -4019,8 +4023,8 @@ static void query_stats_vcpu(CPUState *cpu, run_on_cpu_data data) error_propagate(kvm_stats_args->errp, local_err); return; } - query_stats(kvm_stats_args->result.stats, STATS_TARGET_VCPU, stats_fd, - kvm_stats_args->errp); + query_stats(kvm_stats_args->result.stats, STATS_TARGET_VCPU, + kvm_stats_args->names, stats_fd, kvm_stats_args->errp); close(stats_fd); } @@ -4041,7 +4045,7 @@ static void query_stats_schema_vcpu(CPUState *cpu, run_on_cpu_data data) } static void query_stats_cb(StatsResultList **result, StatsTarget target, - strList *targets, Error **errp) + strList *names, strList *targets, Error **errp) { KVMState *s = kvm_state; CPUState *cpu; @@ -4055,7 +4059,7 @@ static void query_stats_cb(StatsResultList **result, StatsTarget target, error_setg_errno(errp, errno, "KVM stats: ioctl failed"); return; } - query_stats(result, target, stats_fd, errp); + query_stats(result, target, names, stats_fd, errp); close(stats_fd); break; } @@ -4063,6 +4067,7 @@ static void query_stats_cb(StatsResultList **result, StatsTarget target, { StatsArgs stats_args; stats_args.result.stats = result; + stats_args.names = names; stats_args.errp = errp; CPU_FOREACH(cpu) { if (!apply_str_list_filter(cpu->parent_obj.canonical_path, targets)) { diff --git a/include/monitor/stats.h b/include/monitor/stats.h index 80a523dd29..fcf0983154 100644 --- a/include/monitor/stats.h +++ b/include/monitor/stats.h @@ -11,7 +11,7 @@ #include "qapi/qapi-types-stats.h" typedef void StatRetrieveFunc(StatsResultList **result, StatsTarget target, - strList *targets, Error **errp); + strList *names, strList *targets, Error **errp); typedef void SchemaRetrieveFunc(StatsSchemaList **result, Error **errp); /* diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c index e49ab345d7..7314cd813d 100644 --- a/monitor/qmp-cmds.c +++ b/monitor/qmp-cmds.c @@ -472,12 +472,17 @@ static bool invoke_stats_cb(StatsCallbacks *entry, Error **errp) { strList *targets = NULL; + strList *names = NULL; ERRP_GUARD(); if (request) { if (request->provider != entry->provider) { return true; } + if (request->has_names && !request->names) { + return true; + } + names = request->has_names ? request->names : NULL; } switch (filter->target) { @@ -496,7 +501,7 @@ static bool invoke_stats_cb(StatsCallbacks *entry, abort(); } - entry->stats_cb(stats_results, filter->target, targets, errp); + entry->stats_cb(stats_results, filter->target, names, targets, errp); if (*errp) { qapi_free_StatsResultList(*stats_results); *stats_results = NULL; diff --git a/qapi/stats.json b/qapi/stats.json index 503918ea4c..2f8bfe8fdb 100644 --- a/qapi/stats.json +++ b/qapi/stats.json @@ -75,11 +75,14 @@ # Indicates a set of statistics that should be returned by query-stats. # # @provider: provider for which to return statistics. + +# @names: statistics to be returned (all if omitted). # # Since: 7.1 ## { 'struct': 'StatsRequest', - 'data': { 'provider': 'StatsProvider' } } + 'data': { 'provider': 'StatsProvider', + '*names': [ 'str' ] } } ## # @StatsVCPUFilter: @@ -99,6 +102,7 @@ # that target: # - which vCPUs to request statistics for # - which providers to request statistics from +# - which named values to return within each provider # # Since: 7.1 ## -- cgit 1.4.1