From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org, armbru@redhat.com,
Mark Kanda <mark.kanda@oracle.com>
Subject: Re: [PATCH v5 06/10] hmp: add basic "info stats" implementation
Date: Tue, 7 Jun 2022 19:35:49 +0100 [thread overview]
Message-ID: <Yp+aha8olzKWwOCz@work-vm> (raw)
In-Reply-To: <20220530150714.756954-7-pbonzini@redhat.com>
* Paolo Bonzini (pbonzini@redhat.com) wrote:
> From: Mark Kanda <mark.kanda@oracle.com>
>
> 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.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> hmp-commands-info.hx | 13 +++
> include/monitor/hmp.h | 1 +
> monitor/hmp-cmds.c | 190 ++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 204 insertions(+)
>
> 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..55b83c0a3a 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 >= -9 && value->exponent <= 0 &&
> + value->exponent % 3 == 0) {
> + monitor_printf(mon, "%s", si_prefix(value->exponent));
> + } else if (unit && value->base == 2 &&
> + value->exponent >= 0 && value->exponent <= 40 &&
> + value->exponent % 10 == 0) {
> +
> + monitor_printf(mon, "%s", iec_binary_prefix(value->exponent));
OK that's better; but it's a shame the limits don't come from something
shared; iec_binary_prefix goes upto 60 and si_prefix goes way below -9
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> + } 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);
> +}
> --
> 2.36.1
>
>
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
next prev parent reply other threads:[~2022-06-07 18:37 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-30 15:07 [PATCH v5 00/10] qmp, hmp: statistics subsystem and KVM suport Paolo Bonzini
2022-05-30 15:07 ` [PATCH v5 01/10] qmp: Support for querying stats Paolo Bonzini
2022-05-30 15:07 ` [PATCH v5 02/10] kvm: Support for querying fd-based stats Paolo Bonzini
2022-06-07 17:44 ` Dr. David Alan Gilbert
2022-06-08 14:13 ` Paolo Bonzini
2022-06-08 14:52 ` Dr. David Alan Gilbert
2022-06-08 15:58 ` Paolo Bonzini
2022-06-08 16:01 ` Dr. David Alan Gilbert
2022-06-08 16:10 ` Paolo Bonzini
2022-06-08 16:17 ` Dr. David Alan Gilbert
2022-05-30 15:07 ` [PATCH v5 03/10] qmp: add filtering of statistics by target vCPU Paolo Bonzini
2022-05-30 15:07 ` [PATCH v5 04/10] cutils: fix case for "kilo" and "kibi" Paolo Bonzini
2022-05-30 21:56 ` Philippe Mathieu-Daudé via
2022-05-30 15:07 ` [PATCH v5 05/10] cutils: add functions for IEC and SI prefixes Paolo Bonzini
2022-05-30 21:59 ` Philippe Mathieu-Daudé via
2022-05-31 10:28 ` Paolo Bonzini
2022-05-30 15:07 ` [PATCH v5 06/10] hmp: add basic "info stats" implementation Paolo Bonzini
2022-06-07 18:35 ` Dr. David Alan Gilbert [this message]
2022-06-08 14:27 ` Paolo Bonzini
2022-06-08 15:23 ` Dr. David Alan Gilbert
2022-05-30 15:07 ` [PATCH v5 07/10] qmp: add filtering of statistics by provider Paolo Bonzini
2022-06-08 11:52 ` Dr. David Alan Gilbert
2022-05-30 15:07 ` [PATCH v5 08/10] hmp: " Paolo Bonzini
2022-06-08 12:06 ` Dr. David Alan Gilbert
2022-05-30 15:07 ` [PATCH v5 09/10] qmp: add filtering of statistics by name Paolo Bonzini
2022-06-08 13:36 ` Dr. David Alan Gilbert
2022-05-30 15:07 ` [PATCH v5 10/10] hmp: " Paolo Bonzini
2022-06-08 13:40 ` Dr. David Alan Gilbert
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Yp+aha8olzKWwOCz@work-vm \
--to=dgilbert@redhat.com \
--cc=armbru@redhat.com \
--cc=mark.kanda@oracle.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.