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 <armbru@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2022-04-26 14:59:44 +02:00
parent cc01a3f4ca
commit 467ef823d8
4 changed files with 69 additions and 9 deletions

View file

@ -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;
}