From: Michael Petlan <mpetlan@redhat.com>
To: linux-perf-users@vger.kernel.org, acme@kernel.org
Cc: acme@redhat.com, qzhao@redhat.com, cjense@google.com
Subject: [PATCH 1/2] perf stat: Fix JSON metric printout for multiple metrics per line
Date: Fri, 20 Jan 2023 14:40:38 +0100 [thread overview]
Message-ID: <20230120134039.17788-2-mpetlan@redhat.com> (raw)
In-Reply-To: <20230120134039.17788-1-mpetlan@redhat.com>
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-01-20 13:41 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 ` Michael Petlan [this message]
2023-01-23 6:31 ` [PATCH 1/2] perf stat: Fix JSON metric printout for multiple metrics per line Namhyung Kim
2023-05-22 12:11 ` Michael Petlan
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=20230120134039.17788-2-mpetlan@redhat.com \
--to=mpetlan@redhat.com \
--cc=acme@kernel.org \
--cc=acme@redhat.com \
--cc=cjense@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).