From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
Thomas Richter <tmricht@linux.ibm.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>
Subject: [PATCH v2 7/7] perf stat: Improve handling of termination by signal
Date: Wed, 3 Dec 2025 13:47:06 -0800 [thread overview]
Message-ID: <20251203214706.112174-8-irogers@google.com> (raw)
In-Reply-To: <20251203214706.112174-1-irogers@google.com>
When interrupting perf stat in repeat mode with a signal the signal is
passed to the child process but the repeat doesn't terminate:
```
$ perf stat -v --null --repeat 10 sleep 1
Control descriptor is not initialized
[ perf stat: executing run #1 ... ]
[ perf stat: executing run #2 ... ]
^Csleep: Interrupt
[ perf stat: executing run #3 ... ]
[ perf stat: executing run #4 ... ]
[ perf stat: executing run #5 ... ]
[ perf stat: executing run #6 ... ]
[ perf stat: executing run #7 ... ]
[ perf stat: executing run #8 ... ]
[ perf stat: executing run #9 ... ]
[ perf stat: executing run #10 ... ]
Performance counter stats for 'sleep 1' (10 runs):
0.9500 +- 0.0512 seconds time elapsed ( +- 5.39% )
0.01user 0.02system 0:09.53elapsed 0%CPU (0avgtext+0avgdata 18940maxresident)k
29944inputs+0outputs (0major+2629minor)pagefaults 0swaps
```
Terminate the repeated run and give a reasonable exit value:
```
$ perf stat -v --null --repeat 10 sleep 1
Control descriptor is not initialized
[ perf stat: executing run #1 ... ]
[ perf stat: executing run #2 ... ]
[ perf stat: executing run #3 ... ]
^Csleep: Interrupt
Performance counter stats for 'sleep 1' (10 runs):
0.680 +- 0.321 seconds time elapsed ( +- 47.16% )
Command exited with non-zero status 130
0.00user 0.01system 0:02.05elapsed 0%CPU (0avgtext+0avgdata 70688maxresident)k
0inputs+0outputs (0major+5002minor)pagefaults 0swaps
```
Note, this also changes the exit value for non-repeat runs when
interrupted by a signal.
Reported-by: Ingo Molnar <mingo@kernel.org>
Closes: https://lore.kernel.org/lkml/aS5wjmbAM9ka3M2g@gmail.com/
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-stat.c | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index bd3c3de8d200..ab40d85fb125 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -1007,10 +1007,20 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
goto err_out;
}
- if (WIFSIGNALED(status))
+ if (WIFSIGNALED(status)) {
+ /*
+ * We want to indicate failure to stop a repeat run,
+ * hence negative. We want the value to be the exit code
+ * of perf, which for termination by a signal is 128
+ * plus the signal number.
+ */
+ err = 0 - (128 + WTERMSIG(status));
psignal(WTERMSIG(status), argv[0]);
+ } else {
+ err = WEXITSTATUS(status);
+ }
} else {
- status = dispatch_events(forks, timeout, interval, ×);
+ err = dispatch_events(forks, timeout, interval, ×);
}
disable_counters();
@@ -1050,7 +1060,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
if (!STAT_RECORD)
evlist__close(evsel_list);
- return WEXITSTATUS(status);
+ return err;
err_out:
if (forks)
@@ -2969,7 +2979,7 @@ int cmd_stat(int argc, const char **argv)
evlist__reset_prev_raw_counts(evsel_list);
status = run_perf_stat(argc, argv, run_idx);
- if (status == -1)
+ if (status < 0)
break;
if (forever && !interval) {
@@ -3039,5 +3049,6 @@ int cmd_stat(int argc, const char **argv)
evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack, &stat_config.ctl_fd_close);
- return status;
+ /* Only the low byte of status becomes the exit code. */
+ return abs(status);
}
--
2.52.0.177.g9f829587af-goog
next prev parent reply other threads:[~2025-12-03 21:47 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-03 21:46 [PATCH v2 0/7] Perf stat --null/offline CPU segv related fixes/tests Ian Rogers
2025-12-03 21:47 ` [PATCH v2 1/7] perf stat: Allow no events to open if this is a "--null" run Ian Rogers
2025-12-03 21:47 ` [PATCH v2 2/7] libperf cpumap: Fix perf_cpu_map__max for an empty/NULL map Ian Rogers
2025-12-03 21:47 ` [PATCH v2 3/7] perf cpumap: Add "any" CPU handling to cpu_map__snprint_mask Ian Rogers
2025-12-03 21:47 ` [PATCH v2 4/7] perf tests stat: Add "--null" coverage Ian Rogers
2025-12-03 21:47 ` [PATCH v2 5/7] perf stat: When no events, don't report an error if there is none Ian Rogers
2025-12-03 21:47 ` [PATCH v2 6/7] perf tests stat: Add test for error for an offline CPU Ian Rogers
2025-12-03 21:47 ` Ian Rogers [this message]
2025-12-04 7:51 ` [PATCH v2 0/7] Perf stat --null/offline CPU segv related fixes/tests Thomas Richter
2025-12-04 19:10 ` Namhyung Kim
2025-12-06 9:35 ` Ingo Molnar
2025-12-06 11:20 ` Ingo Molnar
2025-12-07 1:43 ` Ian Rogers
2025-12-09 8:44 ` Ingo Molnar
2025-12-09 17:38 ` Ian Rogers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251203214706.112174-8-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=tmricht@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).