[PATCH v1 4/8] perf stat: Drop metric-unit if unit is NULL

Ian Rogers posted 8 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH v1 4/8] perf stat: Drop metric-unit if unit is NULL
Posted by Ian Rogers 1 month, 3 weeks ago
Avoid cases like:
```
$ perf stat -a -M topdownl1 -j -I 1000
...
{"interval" : 11.127757275, "counter-value" : "85715898.000000", "unit" : "", "event" : "IDQ.MITE_UOPS", "event-runtime" : 988376123, "pcnt-running" : 100.00, "metric-value" : "0.000000", "metric-unit" : "(null)"}
...
```

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/stat-display.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
index e392ee5efb45..9b65968e37d1 100644
--- a/tools/perf/util/stat-display.c
+++ b/tools/perf/util/stat-display.c
@@ -470,8 +470,9 @@ static void print_metric_json(struct perf_stat_config *config __maybe_unused,
 	struct outstate *os = ctx;
 	FILE *out = os->fh;
 
-	fprintf(out, "\"metric-value\" : \"%f\", ", val);
-	fprintf(out, "\"metric-unit\" : \"%s\"", unit);
+	fprintf(out, "\"metric-value\" : \"%f\"", val);
+	if (unit)
+		fprintf(out, ", \"metric-unit\" : \"%s\"", unit);
 	if (!config->metric_only)
 		fprintf(out, "}");
 }
-- 
2.47.0.rc0.187.ge670bccf7e-goog
Re: [PATCH v1 4/8] perf stat: Drop metric-unit if unit is NULL
Posted by Tim Chen 1 month, 2 weeks ago
On Fri, 2024-10-04 at 16:41 -0700, Ian Rogers wrote:
> Avoid cases like:
> ```
> $ perf stat -a -M topdownl1 -j -I 1000
> ...
> {"interval" : 11.127757275, "counter-value" : "85715898.000000", "unit" : "", "event" : "IDQ.MITE_UOPS", "event-runtime" : 988376123, "pcnt-running" : 100.00, "metric-value" : "0.000000", "metric-unit" : "(null)"}
> ...
> ```
> 
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/util/stat-display.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
> index e392ee5efb45..9b65968e37d1 100644
> --- a/tools/perf/util/stat-display.c
> +++ b/tools/perf/util/stat-display.c
> @@ -470,8 +470,9 @@ static void print_metric_json(struct perf_stat_config *config __maybe_unused,
>  	struct outstate *os = ctx;
>  	FILE *out = os->fh;
>  
> -	fprintf(out, "\"metric-value\" : \"%f\", ", val);
> -	fprintf(out, "\"metric-unit\" : \"%s\"", unit);
> +	fprintf(out, "\"metric-value\" : \"%f\"", val);
> +	if (unit)
> +		fprintf(out, ", \"metric-unit\" : \"%s\"", unit);

I think if there's no metric unit, we should skip printing metric value as a metric
of 0 has no meaning.

Tim
>  	if (!config->metric_only)
>  		fprintf(out, "}");
>  }
Re: [PATCH v1 4/8] perf stat: Drop metric-unit if unit is NULL
Posted by Ian Rogers 1 month, 2 weeks ago
On Tue, Oct 8, 2024 at 4:27 PM Tim Chen <tim.c.chen@linux.intel.com> wrote:
>
> On Fri, 2024-10-04 at 16:41 -0700, Ian Rogers wrote:
> > Avoid cases like:
> > ```
> > $ perf stat -a -M topdownl1 -j -I 1000
> > ...
> > {"interval" : 11.127757275, "counter-value" : "85715898.000000", "unit" : "", "event" : "IDQ.MITE_UOPS", "event-runtime" : 988376123, "pcnt-running" : 100.00, "metric-value" : "0.000000", "metric-unit" : "(null)"}
> > ...
> > ```
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> >  tools/perf/util/stat-display.c | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
> > index e392ee5efb45..9b65968e37d1 100644
> > --- a/tools/perf/util/stat-display.c
> > +++ b/tools/perf/util/stat-display.c
> > @@ -470,8 +470,9 @@ static void print_metric_json(struct perf_stat_config *config __maybe_unused,
> >       struct outstate *os = ctx;
> >       FILE *out = os->fh;
> >
> > -     fprintf(out, "\"metric-value\" : \"%f\", ", val);
> > -     fprintf(out, "\"metric-unit\" : \"%s\"", unit);
> > +     fprintf(out, "\"metric-value\" : \"%f\"", val);
> > +     if (unit)
> > +             fprintf(out, ", \"metric-unit\" : \"%s\"", unit);
>
> I think if there's no metric unit, we should skip printing metric value as a metric
> of 0 has no meaning.

Makes sense to me too. It requires some test fixing to make work. I'll
take a look.

Thanks,
Ian

> >       if (!config->metric_only)
> >               fprintf(out, "}");
> >  }
>