[PATCH v1 08/13] perf stat: Respect the enabled time of exited user_time/system_time events
From: Ian Rogers
Date: Mon Nov 23 2026 - 02:20:07 EST
When a workload has exited and been reaped by wait4, user_time and
system_time can no longer be read from /proc/<pid>/stat and perf stat
falls back to using the rusage values from wait4. These are the totals
for the whole run of the workload and ignore when the event was enabled,
such as after a delay. For example:
$ perf stat -D 500 -e duration_time,user_time,task-clock -- perf test -w noploop
524,466,208 duration_time
1,004,578,000 user_time
522.86 msec task-clock
Add evsel__tool_pmu_exited_process_time() to compute the time the event
was enabled for from the process's total time, the start time when it
was last enabled and the accumulated time, and use it for the rusage
fallback.
Fixes: 0b18bced5444 ("perf tool_pmu: Make tool PMU events respect enable/disable")
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-stat.c | 3 +++
tools/perf/util/tool_pmu.c | 24 ++++++++++++++++++++++++
tools/perf/util/tool_pmu.h | 2 ++
3 files changed, 29 insertions(+)
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 4fe4da20aae8..964bf0864796 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -302,6 +302,9 @@ static int read_single_counter(struct evsel *counter, int cpu_map_idx, int threa
else
val = ru_stats.ru_stime_usec_stat.mean;
+ /* Only count the time the event was enabled, such as after a delay. */
+ val = evsel__tool_pmu_exited_process_time(counter, cpu_map_idx, thread, val);
+
count->val = val;
if (old_count) {
count->run = old_count->run + 1;
diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c
index a62a7cb390ae..24ff27099d32 100644
--- a/tools/perf/util/tool_pmu.c
+++ b/tools/perf/util/tool_pmu.c
@@ -13,9 +13,11 @@
#include <api/fs/fs.h>
#include <api/io.h>
#include <internal/threadmap.h>
+#include <internal/xyarray.h>
#include <perf/threadmap.h>
#include <fcntl.h>
#include <strings.h>
+#include <unistd.h>
#define INVALID_START_TIME ~0ULL
@@ -709,6 +711,28 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread)
return 0;
}
+/*
+ * When the process has exited its user or system time can no longer be read
+ * from /proc. Given the process's total time, such as from wait4, compute the
+ * time the event was enabled for.
+ */
+u64 evsel__tool_pmu_exited_process_time(struct evsel *evsel, int cpu_map_idx, int thread,
+ u64 total_ns)
+{
+ u64 ns_per_tick = 1000000000ULL / sysconf(_SC_CLK_TCK);
+ __u64 start_time = *(__u64 *)xyarray__entry(evsel->process_time.start_times,
+ cpu_map_idx, thread);
+ __u64 accumulated = *(__u64 *)xyarray__entry(evsel->process_time.accumulated_times,
+ cpu_map_idx, thread);
+ u64 val = accumulated * ns_per_tick;
+
+ if (!evsel->disabled && start_time != INVALID_START_TIME &&
+ total_ns >= start_time * ns_per_tick)
+ val += total_ns - start_time * ns_per_tick;
+
+ return val;
+}
+
struct perf_pmu *tool_pmu__new(void)
{
struct perf_pmu *tool = zalloc(sizeof(struct perf_pmu));
diff --git a/tools/perf/util/tool_pmu.h b/tools/perf/util/tool_pmu.h
index f66d24cf3502..137b0a06a852 100644
--- a/tools/perf/util/tool_pmu.h
+++ b/tools/perf/util/tool_pmu.h
@@ -61,6 +61,8 @@ int evsel__tool_pmu_enable(struct evsel *evsel);
int evsel__tool_pmu_disable_cpu(struct evsel *evsel, int cpu_map_idx);
int evsel__tool_pmu_disable(struct evsel *evsel);
int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread);
+u64 evsel__tool_pmu_exited_process_time(struct evsel *evsel, int cpu_map_idx, int thread,
+ u64 total_ns);
struct perf_pmu *tool_pmu__new(void);
--
2.56.1.750.gf70dc682cf-goog