Commit 765d4e49 authored by Namhyung Kim's avatar Namhyung Kim Committed by Arnaldo Carvalho de Melo

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: default avatarNamhyung Kim <namhyung@kernel.org>
Acked-by: default avatarIan 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.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent c4b41b83
......@@ -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:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment