Skip to contentSkip to conversation

perf stat: Fix trailing comma when there is no metric unit

PATCH Applied James Clark 3 participants 9 messages Nov 12, 2024 – Dec 26, 2024 perf events

lore
Share & tools
lore

James Clark

author reviewer
#0 lore raw

v3 cover perf stat: Fix trailing comma when there is no metric unit

The first commit is failing on Arm and I think the fix should stop more trailing comma issues which keep happening.

The second one I just noticed when looking at it. I don't feel strongly about it so not sure if we should do it or not, but seems like the empty metric-units exclusion from the JSON should be consistent if we're going to have it at all.

Changes since v2:
* Do more documentation and tidyups around struct outstate

Changes since v1:
* Don't skip printing when the metric-unit string is empty but pass NULL instead of an empty string.

James Clark (5):
perf stat: Fix trailing comma when there is no metric unit perf stat: Also hide metric-units from JSON when event didn't run perf stat: Remove empty new_line_metric function
perf stat: Document and simplify interval timestamps perf stat: Document and clarify outstate members

 tools/perf/arch/x86/util/iostat.c             |   4 +
 .../tests/shell/lib/perf_json_output_lint.py  |  14 +-
 tools/perf/util/stat-display.c                | 242 ++++++++++--------
 tools/perf/util/stat-shadow.c                 |   5 +-
 4 files changed, 147 insertions(+), 118 deletions(-)
Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH v3 1/5 perf stat: Fix trailing comma when there is no metric unit

Now that printing metric-value and metric-unit is optional, print_running_json() shouldn't add the comma in case it becomes trailing.

Replace all manual json comma stuff with a json_out() function that uses the existing os->first tracking and auto inserts a comma if it's needed. Update the test to handle that two of the fields can be missing.

This fixes the following test failure on Cortex A57 where the branch misses metric is missing a required event:

 $ perf test -vvv "json output"

106: perf stat JSON output linter:
--- start ---
test child forked, pid 665682
Checking json output: no args Test failed for input:
...
{"counter-value" : "3112.000000", "unit" : "",
"event" : "armv8_pmuv3_1/branch-misses/", "event-runtime" : 20699340, "pcnt-running" : 100.00, }
...
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 12 column 144 (char 2109)
---- end(-1) ----
106: perf stat JSON output linter : FAILED!

Fixes e1cc918b6cfd ("perf stat: Drop metric-unit if unit is NULL")
---
 .../tests/shell/lib/perf_json_output_lint.py  |  14 +-
 tools/perf/util/stat-display.c                | 177 ++++++++++--------
 2 files changed, 104 insertions(+), 87 deletions(-)
tools/perf/tests/shell/lib/perf_json_output_lint.py +7 -7
index 8ddb85586131..b066d721f897 100644
@@ -69,16 +69,16 @@ def check_json_output(expected_items):
   for item in json.loads(input):
     if expected_items != -1:
       count = len(item)
-      if count != expected_items and count >= 1 and count <= 7 and 'metric-value' in item:
+      if count not in expected_items and count >= 1 and count <= 7 and 'metric-value' in item:
         # Events that generate >1 metric may have isolated metric
         # values and possibly other prefixes like interval, core,
         # aggregate-number, or event-runtime/pcnt-running from multiplexing.
         pass
-      elif count != expected_items and count >= 1 and count <= 5 and 'metricgroup' in item:
+      elif count not in expected_items and count >= 1 and count <= 5 and 'metricgroup' in item:
         pass
-      elif count == expected_items + 1 and 'metric-threshold' in item:
+      elif count - 1 in expected_items and 'metric-threshold' in item:
           pass
-      elif count != expected_items:
+      elif count not in expected_items:
         raise RuntimeError(f'wrong number of fields. counted {count} expected {expected_items}'
                            f' in \'{item}\'')
     for key, value in item.items():
@@ -90,11 +90,11 @@ def check_json_output(expected_items):
 
 try:
   if args.no_args or args.system_wide or args.event:
-    expected_items = 7
+    expected_items = [5, 7]
   elif args.interval or args.per_thread or args.system_wide_no_aggr:
-    expected_items = 8
+    expected_items = [6, 8]
   elif args.per_core or args.per_socket or args.per_node or args.per_die or args.per_cluster or args.per_cache:
-    expected_items = 9
+    expected_items = [7, 9]
   else:
     # If no option is specified, don't check the number of items.
     expected_items = -1
tools/perf/util/stat-display.c +97 -80

409 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#2 lore raw

PATCH v3 2/5 perf stat: Also hide metric-units from JSON when event didn't run

In reply to James Clark

We decided to hide NULL metric-units rather than showing it as "(null)" when a dependent event for a metric doesn't exist. But on hybrid systems if the process doesn't hit a PMU you get an empty string metric unit instead. To make it consistent change all empty strings to NULL.

Note that metric-threshold is already hidden in this case without this change.

Where a process only runs on cpu_core and never hits cpu_atom:
Before:

 $ perf stat -j -- true
 ...
 {"counter-value" : "<not counted>", "unit" : "", "event" : "cpu_atom/branch-misses/", "event-runtime" : 0, "pcnt-running" : 0.00, "metric-value" : "0.000000", "metric-unit" : ""}
 {"counter-value" : "6326.000000", "unit" : "", "event" : "cpu_core/branch-misses/", "event-runtime" : 293786, "pcnt-running" : 100.00, "metric-value" : "3.553394", "metric-unit" : "of all branches", "metric-threshold" : "good"}
 ...

After:
...
{"counter-value" : "<not counted>", "unit" : "", "event" : "cpu_atom/branch-misses/", "event-runtime" : 0, "pcnt-running" : 0.00}
{"counter-value" : "5778.000000", "unit" : "", "event" : "cpu_core/branch-misses/", "event-runtime" : 282240, "pcnt-running" : 100.00, "metric-value" : "3.226797", "metric-unit" : "of all branches", "metric-threshold" : "good"}
...

---
 tools/perf/util/stat-display.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/util/stat-display.c +3 -2
index a5d72f4a515c..0e16eecfbad8 100644
@@ -854,7 +854,8 @@ static void printout(struct perf_stat_config *config, struct outstate *os,
 
 	if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
 		if (config->metric_only) {
-			pm(config, os, METRIC_THRESHOLD_UNKNOWN, "", "", 0);
+			pm(config, os, METRIC_THRESHOLD_UNKNOWN, /*format=*/NULL,
+			   /*unit=*/NULL, /*val=*/0);
 			return;
 		}
 
@@ -909,7 +910,7 @@ static void printout(struct perf_stat_config *config, struct outstate *os,
 			perf_stat__print_shadow_stats(config, counter, uval, aggr_idx,
 						      &out, &config->metric_events);
 	} else {
-		pm(config, os, METRIC_THRESHOLD_UNKNOWN, /*format=*/NULL, /*unit=*/"", /*val=*/0);
+		pm(config, os, METRIC_THRESHOLD_UNKNOWN, /*format=*/NULL, /*unit=*/NULL, /*val=*/0);
 	}
 
 	if (!config->metric_only) {
Signature · 1 line
2.34.1
author reviewer
#3 lore raw

PATCH v3 3/5 perf stat: Remove empty new_line_metric function

In reply to James Clark

Despite the name new_line_metric doesn't make a new line, it actually does nothing. Change it to NULL to avoid confusion.

---
 tools/perf/util/stat-display.c | 13 ++++---------
 tools/perf/util/stat-shadow.c  |  5 +++--
 2 files changed, 7 insertions(+), 11 deletions(-)
tools/perf/util/stat-display.c +4 -9
index 0e16eecfbad8..aa74543ae298 100644
@@ -673,11 +673,6 @@ static void print_metric_only_json(struct perf_stat_config *config __maybe_unuse
 	json_out(os, "\"%s\" : \"%s\"", unit, vals);
 }
 
-static void new_line_metric(struct perf_stat_config *config __maybe_unused,
-			    void *ctx __maybe_unused)
-{
-}
-
 static void print_metric_header(struct perf_stat_config *config,
 				void *ctx,
 				enum metric_threshold_classify thresh __maybe_unused,
@@ -839,16 +834,16 @@ static void printout(struct perf_stat_config *config, struct outstate *os,
 
 	if (config->csv_output) {
 		pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
-		nl = config->metric_only ? new_line_metric : new_line_csv;
+		nl = config->metric_only ? NULL : new_line_csv;
 		pmh = print_metricgroup_header_csv;
 		os->nfields = 4 + (counter->cgrp ? 1 : 0);
 	} else if (config->json_output) {
 		pm = config->metric_only ? print_metric_only_json : print_metric_json;
-		nl = config->metric_only ? new_line_metric : new_line_json;
+		nl = config->metric_only ? NULL : new_line_json;
 		pmh = print_metricgroup_header_json;
 	} else {
 		pm = config->metric_only ? print_metric_only : print_metric_std;
-		nl = config->metric_only ? new_line_metric : new_line_std;
+		nl = config->metric_only ? NULL : new_line_std;
 		pmh = print_metricgroup_header_std;
 	}
 
@@ -1319,7 +1314,7 @@ static void print_metric_headers(struct perf_stat_config *config,
 	struct perf_stat_output_ctx out = {
 		.ctx = &os,
 		.print_metric = print_metric_header,
-		.new_line = new_line_metric,
+		.new_line = NULL,
 		.force_header = true,
 	};
 
tools/perf/util/stat-shadow.c +3 -2
index 47718610d5d8..fa8b2a1048ff 100644
@@ -327,7 +327,8 @@ static void print_instructions(struct perf_stat_config *config,
 			     "insn per cycle", 0);
 	}
 	if (max_stalled && instructions) {
-		out->new_line(config, ctxp);
+		if (out->new_line)
+			out->new_line(config, ctxp);
 		print_metric(config, ctxp, METRIC_THRESHOLD_UNKNOWN, "%7.2f ",
 			     "stalled cycles per insn", max_stalled / instructions);
 	}
@@ -670,7 +671,7 @@ void *perf_stat__print_shadow_stats_metricgroup(struct perf_stat_config *config,
 			}
 		}
 
-		if ((*num)++ > 0)
+		if ((*num)++ > 0 && out->new_line)
 			out->new_line(config, ctxp);
 		generic_metric(config, mexp, evsel, aggr_idx, out);
 	}
Signature · 1 line
2.34.1
author reviewer
#4 lore raw

PATCH v3 4/5 perf stat: Document and simplify interval timestamps

In reply to James Clark

Rename 'prefix' to 'timestamp' because that's all it does, except in iostat mode where it's slightly overloaded, but still includes a timestamp. This reveals a problem with iostat and JSON mode so document this.

Make it more explicit that these are printed in interval mode by changing 'if (prefix)' to 'if (interval)' which reveals an unnecessary 'else if (... && !interval)' which can be removed.

---
 tools/perf/arch/x86/util/iostat.c |  4 +++
 tools/perf/util/stat-display.c    | 45 +++++++++++++++----------------
 2 files changed, 25 insertions(+), 24 deletions(-)
tools/perf/arch/x86/util/iostat.c +4 -0
index 366b44d0bb7e..00f645a0c18a 100644
@@ -403,6 +403,10 @@ void iostat_prefix(struct evlist *evlist,
 	struct iio_root_port *rp = evlist->selected->priv;
 
 	if (rp) {
+		/*
+		 * TODO: This is the incorrect format in JSON mode.
+		 *       See prepare_timestamp()
+		 */
 		if (ts)
 			sprintf(prefix, "%6lu.%09lu%s%04x:%02x%s",
 				ts->tv_sec, ts->tv_nsec,
tools/perf/util/stat-display.c +21 -24

114 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#5 lore raw

PATCH v3 5/5 perf stat: Document and clarify outstate members

In reply to James Clark

Not all of these are "state" so separate them into two sections. Rename and document to make all clearer.

---
 tools/perf/util/stat-display.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
tools/perf/util/stat-display.c +21 -7
index 8377e24602dd..ba79f73e1cf5 100644
@@ -115,15 +115,29 @@ static void print_running_csv(struct perf_stat_config *config, u64 run, u64 ena)
 		config->csv_sep, run, config->csv_sep, enabled_percent);
 }
 struct outstate {
-	FILE *fh;
+	/* Std mode: insert a newline before the next metric */
 	bool newline;
+	/* JSON mode: track need for comma for a previous field or not */
 	bool first;
+	/* Num CSV separators remaining to pad out when not all fields are printed */
+	int  csv_col_pad;
+
+	/*
+	 * The following don't track state across fields, but are here as a shortcut to
+	 * pass data to the print functions. The alternative would be to update the
+	 * function signatures of the entire print stack to pass them through.
+	 */
+	/* Place to output to */
+	FILE * const fh;
 	/* Lines are timestamped in --interval-print mode */
 	char timestamp[64];
-	int  nfields;
-	int  aggr_nr;
+	/* Num items aggregated in current line. See struct perf_stat_aggr.nr */
+	int aggr_nr;
+	/* Core/socket/die etc ID for the current line */
 	struct aggr_cpu_id id;
+	/* Event for current line */
 	struct evsel *evsel;
+	/* Cgroup for current line */
 	struct cgroup *cgrp;
 };
 
@@ -473,7 +487,7 @@ static void new_line_csv(struct perf_stat_config *config, void *ctx)
 	int i;
 
 	__new_line_std_csv(config, os);
-	for (i = 0; i < os->nfields; i++)
+	for (i = 0; i < os->csv_col_pad; i++)
 		fputs(config->csv_sep, os->fh);
 }
 
@@ -550,12 +564,12 @@ static void print_metricgroup_header_csv(struct perf_stat_config *config,
 
 	if (!metricgroup_name) {
 		/* Leave space for running and enabling */
-		for (i = 0; i < os->nfields - 2; i++)
+		for (i = 0; i < os->csv_col_pad - 2; i++)
 			fputs(config->csv_sep, os->fh);
 		return;
 	}
 
-	for (i = 0; i < os->nfields; i++)
+	for (i = 0; i < os->csv_col_pad; i++)
 		fputs(config->csv_sep, os->fh);
 	fprintf(config->output, "%s", metricgroup_name);
 	new_line_csv(config, ctx);
@@ -837,7 +851,7 @@ static void printout(struct perf_stat_config *config, struct outstate *os,
 		pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
 		nl = config->metric_only ? NULL : new_line_csv;
 		pmh = print_metricgroup_header_csv;
-		os->nfields = 4 + (counter->cgrp ? 1 : 0);
+		os->csv_col_pad = 4 + (counter->cgrp ? 1 : 0);
 	} else if (config->json_output) {
 		pm = config->metric_only ? print_metric_only_json : print_metric_json;
 		nl = config->metric_only ? NULL : new_line_json;
Signature · 1 line
2.34.1
#6 lore raw
In reply to James Clark
Quoted 10 lines · James Clark

The first commit is failing on Arm and I think the fix should stop more trailing comma issues which keep happening.

The second one I just noticed when looking at it. I don't feel strongly about it so not sure if we should do it or not, but seems like the empty metric-units exclusion from the JSON should be consistent if we're going to have it at all.

Changes since v2:
* Do more documentation and tidyups around struct outstate

Ian, have you had the chance of going over this series?

- Arnaldo

Quoted 19 lines

Changes since v1:
* Don't skip printing when the metric-unit string is empty but pass NULL instead of an empty string.

James Clark (5):
perf stat: Fix trailing comma when there is no metric unit perf stat: Also hide metric-units from JSON when event didn't run perf stat: Remove empty new_line_metric function
perf stat: Document and simplify interval timestamps perf stat: Document and clarify outstate members

 tools/perf/arch/x86/util/iostat.c             |   4 +
 .../tests/shell/lib/perf_json_output_lint.py  |  14 +-
 tools/perf/util/stat-display.c                | 242 ++++++++++--------
 tools/perf/util/stat-shadow.c                 |   5 +-
 4 files changed, 147 insertions(+), 118 deletions(-)
Signature · 1 line
2.34.1
reviewer Tested-by
#7 lore raw
Quoted 10 lines · Arnaldo Carvalho de Melo
Quoted 8 lines · James Clark

The first commit is failing on Arm and I think the fix should stop more trailing comma issues which keep happening.

The second one I just noticed when looking at it. I don't feel strongly about it so not sure if we should do it or not, but seems like the empty metric-units exclusion from the JSON should be consistent if we're going to have it at all.

Quoted 4 lines
Quoted 2 lines

Changes since v2:
* Do more documentation and tidyups around struct outstate

Ian, have you had the chance of going over this series?

It looks good to me, I particularly appreciate the additional comments/documentation. Tested with sanitizers.

Thanks,
Ian

Quoted 19 lines
Quoted 19 lines

Changes since v1:
* Don't skip printing when the metric-unit string is empty but pass NULL instead of an empty string.

James Clark (5):
perf stat: Fix trailing comma when there is no metric unit perf stat: Also hide metric-units from JSON when event didn't run perf stat: Remove empty new_line_metric function
perf stat: Document and simplify interval timestamps perf stat: Document and clarify outstate members

 tools/perf/arch/x86/util/iostat.c             |   4 +
 .../tests/shell/lib/perf_json_output_lint.py  |  14 +-
 tools/perf/util/stat-display.c                | 242 ++++++++++--------
 tools/perf/util/stat-shadow.c                 |   5 +-
 4 files changed, 147 insertions(+), 118 deletions(-)

--
2.34.1

maintainer Applied
#8 lore raw
Quoted 12 lines · Ian Rogers
Quoted 10 lines · Arnaldo Carvalho de Melo
Quoted 8 lines · James Clark

The first commit is failing on Arm and I think the fix should stop more trailing comma issues which keep happening.

The second one I just noticed when looking at it. I don't feel strongly about it so not sure if we should do it or not, but seems like the empty metric-units exclusion from the JSON should be consistent if we're going to have it at all.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Changes since v2:
* Do more documentation and tidyups around struct outstate

Ian, have you had the chance of going over this series?

It looks good to me, I particularly appreciate the additional

Indeed.

Quoted 3 lines

comments/documentation. Tested with sanitizers.

Thanks, applied to perf-tools-next,

- Arnaldo