perf stat: Tidy up JSON metric-only output when no metrics
authorNamhyung Kim <namhyung@kernel.org>
Wed, 23 Nov 2022 18:02:08 +0000 (10:02 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 24 Nov 2022 12:41:42 +0000 (09:41 -0300)
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>
tools/perf/util/stat-display.c

index 7a39a1a..847acdb 100644 (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: