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>
This commit is contained in:
Namhyung Kim 2022-11-23 10:02:08 -08:00 committed by Arnaldo Carvalho de Melo
parent c4b41b83c2
commit 765d4e497f
1 changed files with 17 additions and 10 deletions

View File

@ -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: