perf stat: Fix trailing comma when there is no metric unit
James Clark
reviewerv3 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
James Clark
reviewerPATCH 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!
--- .../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
@@ -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
James Clark
reviewerPATCH v3 2/5 perf stat: Also hide metric-units from JSON when event didn't run
In reply to James ClarkWe 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
@@ -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
James Clark
reviewerPATCH v3 3/5 perf stat: Remove empty new_line_metric function
In reply to James ClarkDespite 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
@@ -673,11 +673,6 @@ static void print_metric_only_json(struct perf_stat_config *config __maybe_unusejson_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
@@ -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
James Clark
reviewerPATCH v3 4/5 perf stat: Document and simplify interval timestamps
In reply to James ClarkRename '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
@@ -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
James Clark
reviewerPATCH v3 5/5 perf stat: Document and clarify outstate members
In reply to James ClarkNot 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
@@ -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
Arnaldo Carvalho de Melo
maintainerQuoted 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 emptynew_line_metricfunction
perf stat: Document and simplify interval timestamps perf stat: Document and clarify outstate memberstools/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
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 outstateIan, 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 emptynew_line_metricfunction
perf stat: Document and simplify interval timestamps perf stat: Document and clarify outstate memberstools/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
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 outstateIan, 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.
Tested-by Ian Rogers <[email protected]>
Thanks, applied to perf-tools-next,
- Arnaldo