From: Michael Petlan <mpetlan@redhat.com>
To: acme@kernel.org
Cc: linux-perf-users@vger.kernel.org, acme@redhat.com,
qzhao@redhat.com, cjense@google.com, irogers@google.com
Subject: Re: [PATCH 1/2] perf stat: Fix JSON metric printout for multiple metrics per line
Date: Mon, 22 May 2023 14:11:36 +0200 (CEST) [thread overview]
Message-ID: <alpine.LRH.2.20.2305221405430.13334@Diego> (raw)
In-Reply-To: <20230120134039.17788-2-mpetlan@redhat.com>
Hello Ian and Arnaldo,
I've recently got back to the JSON test failures. In January, I post two
patches within a patchset. I see that Ian has updated the testcase code,
so the second patch is probably no more needed. However I don't see this
one applied upstream yet, although it seemed on the list, it had got some
acks. Has it been forgotten somehow?
To remind, since the JSON printer engine wasn't ready to print multiple
metrics per line, the JSON output got screwed up in that situation. The
situation happened on some SPR CPUs, where Top-Down metrics are supported.
The patch below fixes the printer to handle this situation correctly.
I remember someone saying that the whole printer engine needs to be
refactored some day, but I'd think that before that happens, it'd be
nice to have it fixed...
What do you think?
Regards,
Michael
On Fri, 20 Jan 2023, Michael Petlan wrote:
> JSON printing engine used to always print metric (even when there should
> be none and other engines (std, csv) would not print it) and the metric
> used to always close the line by printing a closing curly bracket.
>
> This caused invalid JSON output being generated for top-down metrics,
> when multiple metrics are printed on a single line, so the broken output
> might have looked like:
>
> ... "metric-value" : 15.564203, "metric-unit" : \
> "Memory Bound"}"metric-value" : 14.007787, "metric-unit" : "Core Bound"}
>
> To fix it, print always the separating comma BEFORE the key:value pairs
> and close the line outside of the JSON metric printing routine.
>
> Fixes: df936cadfb58 ("perf stat: Add JSON output option")
>
> Signed-off-by: Michael Petlan <mpetlan@redhat.com>
> ---
> tools/perf/util/stat-display.c | 28 ++++++++++++++++------------
> 1 file changed, 16 insertions(+), 12 deletions(-)
>
> diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
> index 8bd8b0142630..8f80f6b566d0 100644
> --- a/tools/perf/util/stat-display.c
> +++ b/tools/perf/util/stat-display.c
> @@ -86,7 +86,7 @@ static void print_running_json(struct perf_stat_config *config, u64 run, u64 ena
>
> if (run != ena)
> enabled_percent = 100 * run / ena;
> - fprintf(config->output, "\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f, ",
> + fprintf(config->output, ", \"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f",
> run, enabled_percent);
> }
>
> @@ -121,7 +121,7 @@ static void print_noise_pct_csv(struct perf_stat_config *config,
> static void print_noise_pct_json(struct perf_stat_config *config,
> double pct)
> {
> - fprintf(config->output, "\"variance\" : %.2f, ", pct);
> + fprintf(config->output, ", \"variance\" : %.2f", pct);
> }
>
> static void print_noise_pct(struct perf_stat_config *config,
> @@ -165,7 +165,7 @@ static void print_cgroup_csv(struct perf_stat_config *config, const char *cgrp_n
>
> static void print_cgroup_json(struct perf_stat_config *config, const char *cgrp_name)
> {
> - fprintf(config->output, "\"cgroup\" : \"%s\", ", cgrp_name);
> + fprintf(config->output, ", \"cgroup\" : \"%s\"", cgrp_name);
> }
>
> static void print_cgroup(struct perf_stat_config *config, struct cgroup *cgrp)
> @@ -431,10 +431,11 @@ static void print_metric_json(struct perf_stat_config *config __maybe_unused,
> struct outstate *os = ctx;
> FILE *out = os->fh;
>
> - fprintf(out, "\"metric-value\" : %f, ", val);
> - fprintf(out, "\"metric-unit\" : \"%s\"", unit);
> - if (!config->metric_only)
> - fprintf(out, "}");
> + if (unit == NULL || fmt == NULL)
> + return;
> +
> + fprintf(out, ", \"metric-value\" : %f", val);
> + fprintf(out, ", \"metric-unit\" : \"%s\"", unit);
> }
>
> static void new_line_json(struct perf_stat_config *config, void *ctx)
> @@ -623,14 +624,14 @@ static void print_counter_value_json(struct perf_stat_config *config,
> const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
>
> if (ok)
> - fprintf(output, "\"counter-value\" : \"%f\", ", avg);
> + fprintf(output, "\"counter-value\" : \"%f\"", avg);
> else
> - fprintf(output, "\"counter-value\" : \"%s\", ", bad_count);
> + fprintf(output, "\"counter-value\" : \"%s\"", bad_count);
>
> if (evsel->unit)
> - fprintf(output, "\"unit\" : \"%s\", ", evsel->unit);
> + fprintf(output, ", \"unit\" : \"%s\"", evsel->unit);
>
> - fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
> + fprintf(output, ", \"event\" : \"%s\"", evsel__name(evsel));
> }
>
> static void print_counter_value(struct perf_stat_config *config,
> @@ -835,8 +836,11 @@ static void print_counter_aggrdata(struct perf_stat_config *config,
>
> printout(config, os, uval, run, ena, avg, s);
>
> - if (!metric_only)
> + if (!metric_only) {
> + if (config->json_output)
> + fputc('}', output);
> fputc('\n', output);
> + }
> }
>
> static void print_metric_begin(struct perf_stat_config *config,
> --
> 2.18.4
>
>
next prev parent reply other threads:[~2023-05-22 12:12 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-20 13:40 [PATCH 0/2] perf test: Fix JSON linter Michael Petlan
2023-01-20 13:40 ` [PATCH 1/2] perf stat: Fix JSON metric printout for multiple metrics per line Michael Petlan
2023-01-23 6:31 ` Namhyung Kim
2023-05-22 12:11 ` Michael Petlan [this message]
2023-06-06 11:16 ` Michael Petlan
2023-01-20 13:40 ` [PATCH 2/2] perf test: Fix JSON format linter test checks Michael Petlan
2023-01-23 6:36 ` Namhyung Kim
2023-01-24 16:49 ` Michael Petlan
2023-01-24 17:26 ` Namhyung Kim
2023-01-27 12:26 ` Arnaldo Carvalho de Melo
2023-01-27 12:30 ` Arnaldo Carvalho de Melo
2023-01-31 17:14 ` Michael Petlan
2023-02-02 1:18 ` Arnaldo Carvalho de Melo
2023-01-20 17:37 ` [PATCH 0/2] perf test: Fix JSON linter Ian Rogers
2023-01-23 6:48 ` Namhyung Kim
2023-01-23 13:38 ` Arnaldo Carvalho de Melo
2023-01-24 17:39 ` Michael Petlan
2023-01-25 0:37 ` Ian Rogers
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=alpine.LRH.2.20.2305221405430.13334@Diego \
--to=mpetlan@redhat.com \
--cc=acme@kernel.org \
--cc=acme@redhat.com \
--cc=cjense@google.com \
--cc=irogers@google.com \
--cc=linux-perf-users@vger.kernel.org \
--cc=qzhao@redhat.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).