hmp: add filtering of statistics by name

Allow the user to request only a specific subset of statistics.
This can be useful when working on a feature or optimization that is
known to affect that statistic.

Example:

   (qemu) info stats vcpu halt_poll_fail_ns
   provider: kvm
       halt_poll_fail_ns (cumulative, ns): 0

In case multiple providers have the same statistic, the provider can be
specified too:

   (qemu) info stats vcpu halt_poll_fail_ns kvm
   provider: kvm
       halt_poll_fail_ns (cumulative, ns): 0

Extracted from a patch by Mark Kanda.

Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
master
Paolo Bonzini 2022-04-26 14:09:31 +02:00
parent cf7405bc02
commit 39cd0c7f12
2 changed files with 30 additions and 13 deletions

View File

@ -897,10 +897,10 @@ ERST
{
.name = "stats",
.args_type = "target:s,provider:s?",
.params = "target [provider]",
.help = "show statistics for the given target (vm or vcpu); optionally filter by "
"provider",
.args_type = "target:s,names:s?,provider:s?",
.params = "target [names] [provider]",
.help = "show statistics for the given target (vm or vcpu); optionally filter by"
"name (comma-separated list, or * for all) and provider",
.cmd = hmp_info_stats,
},

View File

@ -2359,10 +2359,12 @@ static void print_stats_results(Monitor *mon, StatsTarget target,
}
/* Create the StatsFilter that is needed for an "info stats" invocation. */
static StatsFilter *stats_filter(StatsTarget target, int cpu_index,
StatsProvider provider)
static StatsFilter *stats_filter(StatsTarget target, const char *names,
int cpu_index, StatsProvider provider)
{
StatsFilter *filter = g_malloc0(sizeof(*filter));
StatsProvider provider_idx;
StatsRequestList *request_list = NULL;
filter->target = target;
switch (target) {
@ -2383,15 +2385,29 @@ static StatsFilter *stats_filter(StatsTarget target, int cpu_index,
break;
}
if (provider == STATS_PROVIDER__MAX) {
if (!names && provider == STATS_PROVIDER__MAX) {
return filter;
}
/* "info stats" can only query either one or all the providers. */
/*
* "info stats" can only query either one or all the providers. Querying
* by name, but not by provider, requires the creation of one filter per
* provider.
*/
for (provider_idx = 0; provider_idx < STATS_PROVIDER__MAX; provider_idx++) {
if (provider == STATS_PROVIDER__MAX || provider == provider_idx) {
StatsRequest *request = g_new0(StatsRequest, 1);
request->provider = provider_idx;
if (names && !g_str_equal(names, "*")) {
request->has_names = true;
request->names = strList_from_comma_list(names);
}
QAPI_LIST_PREPEND(request_list, request);
}
}
filter->has_providers = true;
filter->providers = g_new0(StatsRequestList, 1);
filter->providers->value = g_new0(StatsRequest, 1);
filter->providers->value->provider = provider;
filter->providers = request_list;
return filter;
}
@ -2399,6 +2415,7 @@ void hmp_info_stats(Monitor *mon, const QDict *qdict)
{
const char *target_str = qdict_get_str(qdict, "target");
const char *provider_str = qdict_get_try_str(qdict, "provider");
const char *names = qdict_get_try_str(qdict, "names");
StatsProvider provider = STATS_PROVIDER__MAX;
StatsTarget target;
@ -2429,11 +2446,11 @@ void hmp_info_stats(Monitor *mon, const QDict *qdict)
switch (target) {
case STATS_TARGET_VM:
filter = stats_filter(target, -1, provider);
filter = stats_filter(target, names, -1, provider);
break;
case STATS_TARGET_VCPU: {}
int cpu_index = monitor_get_cpu_index(mon);
filter = stats_filter(target, cpu_index, provider);
filter = stats_filter(target, names, cpu_index, provider);
break;
default:
abort();