Merge tag 'hw-misc-20240716' of https://github.com/philmd/qemu into staging
[qemu/armbru.git] / stats / stats-qmp-cmds.c
blobe214b964fda6aa3896e868a06f1652ba89d2e306
1 /*
2 * QMP commands related to stats
4 * This work is licensed under the terms of the GNU GPL, version 2 or
5 * (at your option) any later version.
6 */
8 #include "qemu/osdep.h"
9 #include "sysemu/stats.h"
10 #include "qapi/qapi-commands-stats.h"
11 #include "qemu/queue.h"
12 #include "qapi/error.h"
14 typedef struct StatsCallbacks {
15 StatsProvider provider;
16 StatRetrieveFunc *stats_cb;
17 SchemaRetrieveFunc *schemas_cb;
18 QTAILQ_ENTRY(StatsCallbacks) next;
19 } StatsCallbacks;
21 static QTAILQ_HEAD(, StatsCallbacks) stats_callbacks =
22 QTAILQ_HEAD_INITIALIZER(stats_callbacks);
24 void add_stats_callbacks(StatsProvider provider,
25 StatRetrieveFunc *stats_fn,
26 SchemaRetrieveFunc *schemas_fn)
28 StatsCallbacks *entry = g_new(StatsCallbacks, 1);
29 entry->provider = provider;
30 entry->stats_cb = stats_fn;
31 entry->schemas_cb = schemas_fn;
33 QTAILQ_INSERT_TAIL(&stats_callbacks, entry, next);
36 static bool invoke_stats_cb(StatsCallbacks *entry,
37 StatsResultList **stats_results,
38 StatsFilter *filter, StatsRequest *request,
39 Error **errp)
41 ERRP_GUARD();
42 strList *targets = NULL;
43 strList *names = NULL;
45 if (request) {
46 if (request->provider != entry->provider) {
47 return true;
49 if (request->has_names && !request->names) {
50 return true;
52 names = request->has_names ? request->names : NULL;
55 switch (filter->target) {
56 case STATS_TARGET_VM:
57 break;
58 case STATS_TARGET_VCPU:
59 if (filter->u.vcpu.has_vcpus) {
60 if (!filter->u.vcpu.vcpus) {
61 /* No targets allowed? Return no statistics. */
62 return true;
64 targets = filter->u.vcpu.vcpus;
66 break;
67 case STATS_TARGET_CRYPTODEV:
68 break;
69 default:
70 abort();
73 entry->stats_cb(stats_results, filter->target, names, targets, errp);
74 if (*errp) {
75 qapi_free_StatsResultList(*stats_results);
76 *stats_results = NULL;
77 return false;
79 return true;
82 StatsResultList *qmp_query_stats(StatsFilter *filter, Error **errp)
84 StatsResultList *stats_results = NULL;
85 StatsCallbacks *entry;
86 StatsRequestList *request;
88 QTAILQ_FOREACH(entry, &stats_callbacks, next) {
89 if (filter->has_providers) {
90 for (request = filter->providers; request; request = request->next) {
91 if (!invoke_stats_cb(entry, &stats_results, filter,
92 request->value, errp)) {
93 break;
96 } else {
97 if (!invoke_stats_cb(entry, &stats_results, filter, NULL, errp)) {
98 break;
103 return stats_results;
106 StatsSchemaList *qmp_query_stats_schemas(bool has_provider,
107 StatsProvider provider,
108 Error **errp)
110 ERRP_GUARD();
111 StatsSchemaList *stats_results = NULL;
112 StatsCallbacks *entry;
114 QTAILQ_FOREACH(entry, &stats_callbacks, next) {
115 if (!has_provider || provider == entry->provider) {
116 entry->schemas_cb(&stats_results, errp);
117 if (*errp) {
118 qapi_free_StatsSchemaList(stats_results);
119 return NULL;
124 return stats_results;
127 void add_stats_entry(StatsResultList **stats_results, StatsProvider provider,
128 const char *qom_path, StatsList *stats_list)
130 StatsResult *entry = g_new0(StatsResult, 1);
132 entry->provider = provider;
133 entry->qom_path = g_strdup(qom_path);
134 entry->stats = stats_list;
136 QAPI_LIST_PREPEND(*stats_results, entry);
139 void add_stats_schema(StatsSchemaList **schema_results,
140 StatsProvider provider, StatsTarget target,
141 StatsSchemaValueList *stats_list)
143 StatsSchema *entry = g_new0(StatsSchema, 1);
145 entry->provider = provider;
146 entry->target = target;
147 entry->stats = stats_list;
148 QAPI_LIST_PREPEND(*schema_results, entry);
151 bool apply_str_list_filter(const char *string, strList *list)
153 strList *str_list = NULL;
155 if (!list) {
156 return true;
158 for (str_list = list; str_list; str_list = str_list->next) {
159 if (g_str_equal(string, str_list->value)) {
160 return true;
163 return false;