diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt index b72a29c9223c..f334aabdc809 100644 --- a/tools/perf/Documentation/perf-stat.txt +++ b/tools/perf/Documentation/perf-stat.txt @@ -162,6 +162,9 @@ null run - Don't start any counters. This can be useful to measure just elapsed wall-clock time - or to assess the raw overhead of perf stat itself, without running any counters. +--hide-zero-events:: +Do not show events with a zero count. + -v:: --verbose:: be more verbose (show counter open errors, etc) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index a85a4f30221a..d68a2617a920 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -2505,6 +2505,8 @@ int cmd_stat(int argc, const char **argv) "display details about each run (only with -r option)"), OPT_BOOLEAN('n', "null", &stat_config.null_run, "null run - dont start any counters"), + OPT_BOOLEAN(0, "hide-zero-events", &stat_config.hide_zero, + "Do not show events with a zero count"), OPT_INCR('d', "detailed", &detailed_run, "detailed run - start a lot of events"), OPT_BOOLEAN('S', "sync", &sync_run, diff --git a/tools/perf/tests/shell/stat.sh b/tools/perf/tests/shell/stat.sh index 649de1166fed..298ce454b45a 100755 --- a/tools/perf/tests/shell/stat.sh +++ b/tools/perf/tests/shell/stat.sh @@ -546,6 +546,51 @@ test_csv_json_fail() { fi } +test_hide_zero_events_stat() { + echo "Hide zero events stat test" + if ! perf stat -e context-switches,cpu-migrations true > "${stat_output}" 2>&1 + then + echo "Hide zero events stat test [Skipped event parsing failed]" + return + fi + + zero_event="" + if grep -q -E "[[:space:]]+0[[:space:]]+context-switches" "${stat_output}"; then + zero_event="context-switches" + elif grep -q -E "[[:space:]]+0[[:space:]]+cpu-migrations" "${stat_output}"; then + zero_event="cpu-migrations" + fi + + if [ -z "$zero_event" ]; then + echo "Hide zero events stat test [Skipped - no zero count event found]" + return + fi + + if ! perf stat --hide-zero-events -e context-switches,cpu-migrations true > "${stat_output}" 2>&1 + then + echo "Hide zero events stat test [Failed - command failed]" + err=1 + return + fi + + if grep -q -E "$zero_event" "${stat_output}" + then + echo "Hide zero events stat test [Failed - zero event $zero_event was not hidden]" + err=1 + return + fi + + # Check that --metric-only works with --hide-zero-events + if ! perf stat --hide-zero-events --metric-only -e instructions,cycles true > "${stat_output}" 2>&1 + then + echo "Hide zero events stat test [Failed - metric-only command failed]" + err=1 + return + fi + + echo "Hide zero events stat test [Success]" +} + test_default_stat test_null_stat test_offline_cpu_stat @@ -563,6 +608,7 @@ test_stat_repeat test_stat_pid test_stat_delay test_csv_json_fail +test_hide_zero_events_stat cleanup exit $err diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c index f94f1324d24a..b337cc23f413 100644 --- a/tools/perf/util/stat-display.c +++ b/tools/perf/util/stat-display.c @@ -907,6 +907,9 @@ static bool should_skip_zero_counter(struct perf_stat_config *config, /* Metric only counts won't be displayed but the metric wants to be computed. */ if (config->metric_only) return false; + + if (config->hide_zero && counter->supported) + return true; /* * Skip value 0 when enabling --per-thread globally, * otherwise it will have too many 0 output. diff --git a/tools/perf/util/stat.h b/tools/perf/util/stat.h index 4bced233d2fc..e3598037a6aa 100644 --- a/tools/perf/util/stat.h +++ b/tools/perf/util/stat.h @@ -69,6 +69,7 @@ struct perf_stat_config { bool interval_clear; bool metric_only; bool null_run; + bool hide_zero; bool ru_display; bool big_num; bool hybrid_merge;