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.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
accel/kvm/kvm-all.c | 9 +++++++--
include/monitor/stats.h | 9 ++++++++-
monitor/qmp-cmds.c | 34 +++++++++++++++++++++++++++++++++-
qapi/stats.json | 16 ++++++++++++++--
4 files changed, 62 insertions(+), 6 deletions(-)
diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index 4a753d4445..bfd81039a1 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)
@@ -4049,7 +4050,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;
@@ -4073,6 +4075,9 @@ static void query_stats_cb(StatsResultList **result, StatsTarget target, Error *
stats_args.result.stats = result;
stats_args.errp = errp;
CPU_FOREACH(cpu) {
+ if (!str_in_list(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 89552ab06f..92a1df3072 100644
--- a/include/monitor/stats.h
+++ b/include/monitor/stats.h
@@ -10,7 +10,8 @@
#include "qapi/qapi-types-stats.h"
-typedef void StatRetrieveFunc(StatsResultList **result, StatsTarget target, Error **errp);
+typedef void StatRetrieveFunc(StatsResultList **result, StatsTarget target,
+ strList *targets, Error **errp);
typedef void SchemaRetrieveFunc(StatsSchemaList **result, Error **errp);
/*
@@ -30,4 +31,10 @@ 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.
+ */
+bool str_in_list(const char *string, strList *list);
+
#endif /* STATS_H */
diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c
index 97825b25fa..5e3f4c9685 100644
--- a/monitor/qmp-cmds.c
+++ b/monitor/qmp-cmds.c
@@ -448,13 +448,30 @@ void add_stats_callbacks(StatRetrieveFunc *stats_fn,
QTAILQ_INSERT_TAIL(&stats_callbacks, entry, next);
}
+static strList *stats_target_filter(StatsFilter *filter)
+{
+ switch (filter->target) {
+ case STATS_TARGET_VM:
+ return NULL;
+ case STATS_TARGET_VCPU:
+ if (!filter->u.vcpu.has_vcpus) {
+ return NULL;
+ }
+ return filter->u.vcpu.vcpus;
+ break;
+ default:
+ abort();
+ }
+}
+
StatsResultList *qmp_query_stats(StatsFilter *filter, Error **errp)
{
StatsResultList *stats_results = NULL;
+ strList *targets = stats_target_filter(filter);
StatsCallbacks *entry;
QTAILQ_FOREACH(entry, &stats_callbacks, next) {
- entry->stats_cb(&stats_results, filter->target, errp);
+ entry->stats_cb(&stats_results, filter->target, targets, errp);
}
return stats_results;
@@ -497,3 +514,18 @@ void add_stats_schema(StatsSchemaList **schema_results,
entry->stats = stats_list;
QAPI_LIST_PREPEND(*schema_results, entry);
}
+
+bool str_in_list(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 bcc897258a..26ee69588f 100644
--- a/qapi/stats.json
+++ b/qapi/stats.json
@@ -65,6 +65,16 @@
{ '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:
#
@@ -73,8 +83,10 @@
#
# Since: 7.1
##
-{ 'struct': 'StatsFilter',
- 'data': { 'target': 'StatsTarget' } }
+{ 'union': 'StatsFilter',
+ 'base': { 'target': 'StatsTarget' },
+ 'discriminator': 'target',
+ 'data': { 'vcpu': 'StatsVCPUFilter' } }
##
# @StatsValue:
--
2.35.1
Paolo Bonzini <pbonzini@redhat.com> writes:
> 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]" ] } }
What heartless people put these poor vCPUs in the orphanage?
>
> Extracted from a patch by Mark Kanda.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
[...]
> diff --git a/qapi/stats.json b/qapi/stats.json
> index bcc897258a..26ee69588f 100644
> --- a/qapi/stats.json
> +++ b/qapi/stats.json
> @@ -65,6 +65,16 @@
> { 'enum': 'StatsTarget',
> 'data': [ 'vm', 'vcpu' ] }
>
> +##
> +# @StatsVCPUFilter:
> +#
> +# @vcpus: list of qom paths for the desired vCPU objects.
"QOM paths", because that's how we spell it elsewhere.
> +#
> +# Since: 7.1
> +##
> +{ 'struct': 'StatsVCPUFilter',
> + 'data': { '*vcpus': [ 'str' ] } }
> +
> ##
> # @StatsFilter:
> #
> @@ -73,8 +83,10 @@
> #
> # Since: 7.1
> ##
> -{ 'struct': 'StatsFilter',
> - 'data': { 'target': 'StatsTarget' } }
> +{ 'union': 'StatsFilter',
> + 'base': { 'target': 'StatsTarget' },
> + 'discriminator': 'target',
> + 'data': { 'vcpu': 'StatsVCPUFilter' } }
>
> ##
> # @StatsValue:
On 5/5/22 15:45, Markus Armbruster wrote:
> Paolo Bonzini <pbonzini@redhat.com> writes:
>
>> 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]" ] } }
>
> What heartless people put these poor vCPUs in the orphanage?
Phil tried to fix it but the testsuites hung in weird ways.
Paolo
>>
>> Extracted from a patch by Mark Kanda.
>>
>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>
> [...]
>
>> diff --git a/qapi/stats.json b/qapi/stats.json
>> index bcc897258a..26ee69588f 100644
>> --- a/qapi/stats.json
>> +++ b/qapi/stats.json
>> @@ -65,6 +65,16 @@
>> { 'enum': 'StatsTarget',
>> 'data': [ 'vm', 'vcpu' ] }
>>
>> +##
>> +# @StatsVCPUFilter:
>> +#
>> +# @vcpus: list of qom paths for the desired vCPU objects.
>
> "QOM paths", because that's how we spell it elsewhere.
>
>> +#
>> +# Since: 7.1
>> +##
>> +{ 'struct': 'StatsVCPUFilter',
>> + 'data': { '*vcpus': [ 'str' ] } }
>> +
>> ##
>> # @StatsFilter:
>> #
>> @@ -73,8 +83,10 @@
>> #
>> # Since: 7.1
>> ##
>> -{ 'struct': 'StatsFilter',
>> - 'data': { 'target': 'StatsTarget' } }
>> +{ 'union': 'StatsFilter',
>> + 'base': { 'target': 'StatsTarget' },
>> + 'discriminator': 'target',
>> + 'data': { 'vcpu': 'StatsVCPUFilter' } }
>>
>> ##
>> # @StatsValue:
>
>
© 2016 - 2026 Red Hat, Inc.