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

perf stat: Fix --metric-only --json output

Currently it prints all metric headers for JSON output.  But actually it
skips some metrics with valid_only_metric().  So the output looks like:

  $ perf stat --metric-only --json true
  {"unit" : "CPUs utilized", "unit" : "/sec", "unit" : "/sec", "unit" : "/sec", "unit" : "GHz", "unit" : "insn per cycle", "unit" : "/sec", "unit" : "branch-misses of all branches"}
  {"metric-value" : "3.861"}{"metric-value" : "0.79"}{"metric-value" : "3.04"}

As you can see there are 8 units in the header but only 3 metric-values
are there.  It should skip the unused headers as well.  Also each unit
should be printed as a separate object like metric values.

With this patch:

  $ perf stat --metric-only --json true
  {"unit" : "GHz"}{"unit" : "insn per cycle"}{"unit" : "branch-misses of all branches"}
  {"metric-value" : "4.166"}{"metric-value" : "0.73"}{"metric-value" : "2.96"}

Fixes: df936cad ("perf stat: Add JSON output option")
Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Claire Jensen <cjense@google.com>
Cc: Ian Rogers <irogers@google.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/20221107213314.3239159-6-namhyung@kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent f4e55f88
...@@ -430,12 +430,12 @@ static void print_metric_header(struct perf_stat_config *config, ...@@ -430,12 +430,12 @@ static void print_metric_header(struct perf_stat_config *config,
os->evsel->priv != os->evsel->evlist->selected->priv) os->evsel->priv != os->evsel->evlist->selected->priv)
return; return;
if (!valid_only_metric(unit) && !config->json_output) if (!valid_only_metric(unit))
return; return;
unit = fixunit(tbuf, os->evsel, unit); unit = fixunit(tbuf, os->evsel, unit);
if (config->json_output) if (config->json_output)
fprintf(os->fh, "\"unit\" : \"%s\"", unit); fprintf(os->fh, "{\"unit\" : \"%s\"}", unit);
else if (config->csv_output) else if (config->csv_output)
fprintf(os->fh, "%s%s", unit, config->csv_sep); fprintf(os->fh, "%s%s", unit, config->csv_sep);
else else
...@@ -845,10 +845,6 @@ static void print_metric_headers(struct perf_stat_config *config, ...@@ -845,10 +845,6 @@ static void print_metric_headers(struct perf_stat_config *config,
.new_line = new_line_metric, .new_line = new_line_metric,
.force_header = true, .force_header = true,
}; };
bool first = true;
if (config->json_output && !config->interval)
fprintf(config->output, "{");
if (prefix && !config->json_output) if (prefix && !config->json_output)
fprintf(config->output, "%s", prefix); fprintf(config->output, "%s", prefix);
...@@ -869,18 +865,12 @@ static void print_metric_headers(struct perf_stat_config *config, ...@@ -869,18 +865,12 @@ static void print_metric_headers(struct perf_stat_config *config,
evlist__for_each_entry(evlist, counter) { evlist__for_each_entry(evlist, counter) {
os.evsel = counter; os.evsel = counter;
if (!first && config->json_output)
fprintf(config->output, ", ");
first = false;
perf_stat__print_shadow_stats(config, counter, 0, perf_stat__print_shadow_stats(config, counter, 0,
0, 0,
&out, &out,
&config->metric_events, &config->metric_events,
&rt_stat); &rt_stat);
} }
if (config->json_output)
fprintf(config->output, "}");
fputc('\n', config->output); fputc('\n', config->output);
} }
...@@ -952,14 +942,8 @@ static void print_interval(struct perf_stat_config *config, ...@@ -952,14 +942,8 @@ static void print_interval(struct perf_stat_config *config,
} }
} }
if ((num_print_interval == 0 || config->interval_clear) if ((num_print_interval == 0 || config->interval_clear) && metric_only)
&& metric_only && !config->json_output)
print_metric_headers(config, evlist, " ", true); print_metric_headers(config, evlist, " ", true);
if ((num_print_interval == 0 || config->interval_clear)
&& metric_only && config->json_output) {
fprintf(output, "{");
print_metric_headers(config, evlist, " ", true);
}
if (++num_print_interval == 25) if (++num_print_interval == 25)
num_print_interval = 0; num_print_interval = 0;
} }
......
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