diff options
author | Namhyung Kim <namhyung@kernel.org> | 2022-11-23 21:02:08 +0300 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2022-11-24 15:41:42 +0300 |
commit | 765d4e497fc51c64b50e5947d0b63bc3ccbd83d3 (patch) | |
tree | 14123472bb01581721e59c86190c6d0bc3256c37 /tools/perf/util/stat-display.c | |
parent | c4b41b83c25073c09bfcc4e5ec496c9dd316656b (diff) | |
download | linux-765d4e497fc51c64b50e5947d0b63bc3ccbd83d3.tar.xz |
perf stat: Tidy up JSON metric-only output when no metrics
It printed empty strings for each metric. I guess it's needed for CSV
output to match the column number. We could just ignore the empty
metrics in JSON but it ended up with a broken JSON object with a
trailing comma.
So I added a dummy '"metric-value" : "none"' part. To do that, it
needs to pass struct outstate to print_metric_end() to check if any
metric value is printed or not.
Before:
# perf stat -aj --metric-only --per-socket --for-each-cgroup system.slice true
{"socket" : "S0", "cpu-count" : 8, "cgroup" : "system.slice", "" : "", "" : "", "" : "", "" : "", "" : "", "" : "", "" : "", "" : ""}
After:
# perf stat -aj --metric-only --per-socket --for-each-cgroup system.slice true
{"socket" : "S0", "cpu-count" : 8, "cgroup" : "system.slice", "metric-value" : "none"}
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Xing Zhengjun <zhengjun.xing@linux.intel.com>
Link: https://lore.kernel.org/r/20221123180208.2068936-16-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/stat-display.c')
-rw-r--r-- | tools/perf/util/stat-display.c | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c index 7a39a1a7261d..847acdb5dc40 100644 --- a/tools/perf/util/stat-display.c +++ b/tools/perf/util/stat-display.c @@ -532,6 +532,8 @@ static void print_metric_only_json(struct perf_stat_config *config __maybe_unuse while (isdigit(*ends) || *ends == '.') ends++; *ends = 0; + if (!unit[0] || !vals[0]) + return; fprintf(out, "%s\"%s\" : \"%s\"", os->first ? "" : ", ", unit, vals); os->first = false; } @@ -864,14 +866,19 @@ static void print_metric_begin(struct perf_stat_config *config, print_cgroup(config, os->cgrp ? : evsel->cgrp); } -static void print_metric_end(struct perf_stat_config *config) +static void print_metric_end(struct perf_stat_config *config, struct outstate *os) { + FILE *output = config->output; + if (!config->metric_only) return; - if (config->json_output) - fputc('}', config->output); - fputc('\n', config->output); + if (config->json_output) { + if (os->first) + fputs("\"metric-value\" : \"none\"", output); + fputc('}', output); + } + fputc('\n', output); } static void print_aggr(struct perf_stat_config *config, @@ -897,7 +904,7 @@ static void print_aggr(struct perf_stat_config *config, print_counter_aggrdata(config, counter, s, os); } - print_metric_end(config); + print_metric_end(config, os); } } @@ -929,7 +936,7 @@ static void print_aggr_cgroup(struct perf_stat_config *config, print_counter_aggrdata(config, counter, s, os); } - print_metric_end(config); + print_metric_end(config, os); } } } @@ -985,7 +992,7 @@ static void print_no_aggr_metric(struct perf_stat_config *config, printout(config, os, uval, run, ena, 1.0, counter_idx); } if (!first) - print_metric_end(config); + print_metric_end(config, os); } } @@ -1348,7 +1355,7 @@ static void print_cgroup_counter(struct perf_stat_config *config, struct evlist evlist__for_each_entry(evlist, counter) { if (os->cgrp != counter->cgrp) { if (os->cgrp != NULL) - print_metric_end(config); + print_metric_end(config, os); os->cgrp = counter->cgrp; print_metric_begin(config, evlist, os, /*aggr_idx=*/0); @@ -1357,7 +1364,7 @@ static void print_cgroup_counter(struct perf_stat_config *config, struct evlist print_counter(config, counter, os); } if (os->cgrp) - print_metric_end(config); + print_metric_end(config, os); } void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config, @@ -1405,7 +1412,7 @@ void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *conf evlist__for_each_entry(evlist, counter) { print_counter(config, counter, &os); } - print_metric_end(config); + print_metric_end(config, &os); } break; case AGGR_NONE: |