perf stat: Display "none" for NaN with metric only json
authorIan Rogers <irogers@google.com>
Thu, 17 Oct 2024 17:53:52 +0000 (10:53 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Thu, 17 Oct 2024 19:44:26 +0000 (12:44 -0700)
Return earlier for an empty unit case. If snprintf of the fmt doesn't
produce digits between vals and ends, as happens with NaN, make the
value "none" as happens in print_metric_end.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Yicong Yang <yangyicong@hisilicon.com>
Cc: Weilin Wang <weilin.wang@intel.com>
Cc: Will Deacon <will@kernel.org>
Cc: James Clark <james.clark@linaro.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Leo Yan <leo.yan@linux.dev>
Cc: Sumanth Korikkar <sumanthk@linux.ibm.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: John Garry <john.g.garry@oracle.com>
Link: https://lore.kernel.org/r/20241017175356.783793-4-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/util/stat-display.c

index ef0ca8cd802dd71947add10363631fa48e10d341..a137b377efa4902c2591ed086a48a7ad2bc58458 100644 (file)
@@ -610,19 +610,22 @@ static void print_metric_only_json(struct perf_stat_config *config __maybe_unuse
 {
        struct outstate *os = ctx;
        FILE *out = os->fh;
-       char buf[64], *vals, *ends;
+       char buf[64], *ends;
        char tbuf[1024];
+       const char *vals;
 
        if (!valid_only_metric(unit))
                return;
        unit = fixunit(tbuf, os->evsel, unit);
+       if (!unit[0])
+               return;
        snprintf(buf, sizeof(buf), fmt ?: "", val);
-       ends = vals = skip_spaces(buf);
+       vals = ends = skip_spaces(buf);
        while (isdigit(*ends) || *ends == '.')
                ends++;
        *ends = 0;
-       if (!unit[0] || !vals[0])
-               return;
+       if (!vals[0])
+               vals = "none";
        fprintf(out, "%s\"%s\" : \"%s\"", os->first ? "" : ", ", unit, vals);
        os->first = false;
 }