perf stat: reject --field-separator and --json-output combination
PATCH 1/1 perf stat: reject --field-separator and --json-output combination
Specifying --field-separator option is stating you want CSV output. Passing both --field-separator and --json-output is then stating you want output to be in CSV and JSON format at same time.
Currently this combination is not rejected, and the outcome is a malformed combination of CSV and JSON output.
This is because of inconsistencies in various printing functions, some of them have if-else chains that start with "Should I print JSON?", and some start with "Should I print CSV?".
Example of current output:
$ tools/perf/perf stat -x , -j -e cpu-migrations true
{"counter-value" : "0.000000", "unit" : "", "event" : "cpu-migrations", "event-runtime" : 474817, "pcnt-running" : 100.00,,Instead reject the option combination,
with a helpful error message and non-zero exit code.
Example of new output:
$ tools/perf/perf stat -x , -j true cannot use both --field-separator and --json-output
Usage: perf stat [<options>] [<command>]
-x, --field-separator <separator>
print counts with custom separator
-j, --json-output print counts in JSON format
--- tools/perf/builtin-stat.c | 7 +++++++ tools/perf/tests/shell/stat.sh | 12 ++++++++++++ 2 files changed, 19 insertions(+)
tools/perf/builtin-stat.c +7 -0
@@ -2637,6 +2637,13 @@ int cmd_stat(int argc, const char **argv) stat_config.aggr_mode =opt_aggr_mode_to_aggr_mode (&opt_mode); +if (stat_config.csv_sep && stat_config.json_output) { +fprintf (stderr,"cannot use both --field-separator and --json-output\n" ); +parse_options_usage (stat_usage, stat_options,"x" ,1 ); +parse_options_usage (NULL , stat_options,"j" ,1 ); + goto out; + } +if (stat_config.csv_sep) { stat_config.csv_output = true;if (!strcmp (stat_config.csv_sep,"\\t" ))
tools/perf/tests/shell/stat.sh +12 -0
@@ -483,6 +483,17 @@ test_stat_pid() { wait $pid 2>/dev/null || true } +test_csv_json_fail() { + echo "stat -x <sep> -j test" + if perf stat -x , -j true > /dev/null 2>&1 + then + echo "stat -x <sep> -j test [Failed - command should have errored]" + err=1 + else + echo "stat -x <sep> -j test [Success]" + fi +} + test_default_stat test_null_stat test_offline_cpu_stat
@@ -498,6 +509,7 @@ test_stat_no_aggr test_stat_detailed test_stat_repeat test_stat_pid +test_csv_json_fail cleanup exit $err
Signature · 1 line
2.43.0
Namhyung Kim
maintainerHello,
Quoted 21 lines · Ivan Lazaric
Specifying --field-separator option is stating you want CSV output. Passing both --field-separator and --json-output is then stating you want output to be in CSV and JSON format at same time.
Currently this combination is not rejected, and the outcome is a malformed combination of CSV and JSON output.
This is because of inconsistencies in various printing functions, some of them have if-else chains that start with "Should I print JSON?", and some start with "Should I print CSV?".Example of current output:
$ tools/perf/perf stat -x , -j -e cpu-migrations true {"counter-value" : "0.000000", "unit" : "", "event" : "cpu-migrations", "event-runtime" : 474817, "pcnt-running" : 100.00,,Instead reject the option combination,
with a helpful error message and non-zero exit code.Example of new output:
$ tools/perf/perf stat -x , -j true cannot use both --field-separator and --json-output
Quoted 7 lines
Usage: perf stat [<options>] [<command>]
-x, --field-separator <separator>
print counts with custom separator
-j, --json-output print counts in JSON formatSigned-off-by Ivan Lazaric <[email protected]>
Thanks for the patch, but it doesn't apply cleanly. Can you please rebase it on to the latest perf-tools-next branch?
Thanks,
Namhyung
PATCH v2 1/1 perf stat: reject --field-separator and --json-output combination
Specifying --field-separator option is stating you want CSV output. Passing both --field-separator and --json-output is then stating you want output to be in CSV and JSON format at same time.
Currently this combination is not rejected, and the outcome is a malformed combination of CSV and JSON output.
This is because of inconsistencies in various printing functions, some of them have if-else chains that start with "Should I print JSON?", and some start with "Should I print CSV?".
Example of current output:
$ tools/perf/perf stat -x , -j -e cpu-migrations true
{"counter-value" : "0.000000", "unit" : "", "event" : "cpu-migrations", "event-runtime" : 474817, "pcnt-running" : 100.00,,Instead reject the option combination,
with a helpful error message and non-zero exit code.
Example of new output:
$ tools/perf/perf stat -x , -j true cannot use both --field-separator and --json-output
Usage: perf stat [<options>] [<command>]
-x, --field-separator <separator>
print counts with custom separator
-j, --json-output print counts in JSON format
---
Changes since v1
- Rebase on top of perf-tools-next
I was working off of master base branch mistakenly.
Applying the patch perf build: Fix compiler errors with old capstone to fix the build (link: https://lore.kernel.org/linux-perf-users/[email protected]/ ), this builds and passes the test shell/stat.sh (currently number 121). I have not tested other tests.
tools/perf/builtin-stat.c | 7 +++++++ tools/perf/tests/shell/stat.sh | 12 ++++++++++++ 2 files changed, 19 insertions(+)
tools/perf/builtin-stat.c +7 -0
@@ -2655,6 +2655,13 @@ int cmd_stat(int argc, const char **argv) stat_config.aggr_mode =opt_aggr_mode_to_aggr_mode (&opt_mode); +if (stat_config.csv_sep && stat_config.json_output) { +fprintf (stderr,"cannot use both --field-separator and --json-output\n" ); +parse_options_usage (stat_usage, stat_options,"x" ,1 ); +parse_options_usage (NULL , stat_options,"j" ,1 ); + goto out; + } +if (stat_config.csv_sep) { stat_config.csv_output = true;if (!strcmp (stat_config.csv_sep,"\\t" ))
tools/perf/tests/shell/stat.sh +12 -0
@@ -535,6 +535,17 @@ test_stat_delay() { echo "stat -D test [Success]" } +test_csv_json_fail() { + echo "stat -x <sep> -j test" + if perf stat -x , -j true > /dev/null 2>&1 + then + echo "stat -x <sep> -j test [Failed - command should have errored]" + err=1 + else + echo "stat -x <sep> -j test [Success]" + fi +} + test_default_stat test_null_stat test_offline_cpu_stat
@@ -551,6 +562,7 @@ test_stat_detailed test_stat_repeat test_stat_pid test_stat_delay +test_csv_json_fail cleanup exit $err
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing explicit inclusion of `<stdio.h>` for standard I/O functions and types. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing explicit inclusion of <stdio.h> for standard I/O functions and types. --
--- Patch [1]: [PATCH v2] perf stat: reject --field-separator and --json-output combination --- commit 14ca4ce3700a93ed48a3336a633292f7e45ae462
Author: Ivan Lazaric <[email protected]>
perf stat: reject --field-separator and --json-output combination
This patch explicitly rejects using both the --field-separator and --json-output options in perf stat. It adds a check to output an error message and exit cleanly, preventing malformed mixed formats.
tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv) · line 2647
Quoted 10 lines
tools/perf/builtin-stat.c +2
-0@@ -2643,6 +2643,13 @@ int cmd_stat(int argc, const char **argv) stat_config.aggr_mode =opt_aggr_mode_to_aggr_mode (&opt_mode); +if (stat_config.csv_sep && stat_config.json_output) { +fprintf (stderr,"cannot use both --field-separator and --json-output\n" );
^^^^^^^
[Severity: Low]
Could this cause a build failure when compiling perf with musl libc?
While glibc often implicitly includes standard headers, musl has stricter header separation.
Since this code introduces a new call to fprintf and uses stderr, does builtin-stat.c need an explicit #include <stdio.h> in its global includes block to guarantee musl compatibility?
Quoted 4 lines
tools/perf/builtin-stat.c +4
-0+parse_options_usage (stat_usage, stat_options,"x" ,1 ); +parse_options_usage (NULL , stat_options,"j" ,1 ); + goto out; + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Namhyung Kim
maintainerQuoted 29 lines · On Wed, Jul 08, 2026 at 01:36:55PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing explicit inclusion of<stdio.h>for standard I/O functions and types. ----- Patch [1]: [PATCH v2] perf stat: reject --field-separator and --json-output combination --- commit
14ca4ce3700a93ed48a3336a633292f7e45ae462
Author: Ivan Lazaric <[email protected]>perf stat: reject --field-separator and --json-output combination
This patch explicitly rejects using both the --field-separator and --json-output options in perf stat. It adds a check to output an error message and exit cleanly, preventing malformed mixed formats.
tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv) · line 2647
Quoted 10 lines
tools/perf/builtin-stat.c +2
-0@@ -2643,6 +2643,13 @@ int cmd_stat(int argc, const char **argv) stat_config.aggr_mode =opt_aggr_mode_to_aggr_mode (&opt_mode); +if (stat_config.csv_sep && stat_config.json_output) { +fprintf (stderr,"cannot use both --field-separator and --json-output\n" );^^^^^^^[Severity: Low]
Could this cause a build failure when compiling perf with musl libc?
Quoted 6 lines
While glibc often implicitly includes standard headers, musl has stricter header separation.
Since this code introduces a new call to fprintf and uses stderr, does
builtin-stat.cneed an explicit #include <stdio.h> in its global includes block to guarantee musl compatibility?
It should be fine as it's used in other places already.
Thanks,
Namhyung
Quoted 8 lines
Quoted 5 lines
tools/perf/builtin-stat.c +4
-0+parse_options_usage (stat_usage, stat_options,"x" ,1 ); +parse_options_usage (NULL , stat_options,"j" ,1 ); + goto out; + }Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 11 lines · Ivan Lazaric
Specifying --field-separator option is stating you want CSV output. Passing both --field-separator and --json-output is then stating you want output to be in CSV and JSON format at same time.
Currently this combination is not rejected, and the outcome is a malformed combination of CSV and JSON output.
This is because of inconsistencies in various printing functions, some of them have if-else chains that start with "Should I print JSON?", and some start with "Should I print CSV?".[...]
Applied to perf-tools-next, thanks!
Best regards,
Namhyung