mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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>, Jiri Olsa <jolsa@kernel.org>,
	 Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	 James Clark <james.clark@linaro.org>
Cc: Howard Chu <howardchu95@gmail.com>,
	Thomas Falcon <thomas.falcon@intel.com>,
	 Song Liu <songliubraving@fb.com>,
	linux-perf-users@vger.kernel.org,  linux-kernel@vger.kernel.org
Subject: [PATCH v1 08/13] perf stat: Respect the enabled time of exited user_time/system_time events
Date: Sun, 22 Nov 2026 23:19:08 -0800	[thread overview]
Message-ID: <20261123071913.653315-9-irogers@google.com> (raw)
In-Reply-To: <20261123071913.653315-1-irogers@google.com>

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@google.com>
---
 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


  parent reply	other threads:[~2026-11-23  7:20 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  7:19 [PATCH v1 00/13] perf fixes: off-cpu, stat, tool PMU, bperf, and bpf-filter fixes Ian Rogers
2026-11-23  7:19 ` [PATCH v1 01/13] perf off-cpu: Bound callchain length in off_cpu_write() Ian Rogers
2026-11-23  7:19 ` [PATCH v1 02/13] perf off-cpu: Fix swapped PID and TID in direct off_cpu_dump() samples Ian Rogers
2026-11-23  7:19 ` [PATCH v1 03/13] libperf evlist: Recompute CPU map when an evsel goes system wide Ian Rogers
2026-11-23  7:19 ` [PATCH v1 04/13] perf stat: Give non-perf_event_open events unique IDs for stat record Ian Rogers
2026-11-23  7:19 ` [PATCH v1 05/13] perf stat: Preserve per-evsel CPU maps across stat record and report Ian Rogers
2026-11-23  7:19 ` [PATCH v1 06/13] perf pmu: Don't treat tool PMU as a kernel software PMU for event grouping Ian Rogers
2026-11-23  7:19 ` [PATCH v1 07/13] perf evlist: Enable tool PMU events when starting a workload Ian Rogers
2026-11-23  7:19 ` Ian Rogers [this message]
2026-11-23  7:19 ` [PATCH v1 09/13] perf stat: Fix --bpf-counters with non-bperf, hybrid, and mixed events Ian Rogers
2026-11-23  7:19 ` [PATCH v1 10/13] perf bpf-filter: Fix bpf_link leak by using PERF_EVENT_IOC_SET_BPF Ian Rogers
2026-11-23  7:19 ` [PATCH v1 11/13] perf bpf-filter: Copy bpf_filters in evsel__clone() and close on evsel__close() Ian Rogers
2026-11-23  7:19 ` [PATCH v1 12/13] perf bpf-filter: Support BPF sample filters on tracepoint events Ian Rogers
2026-11-23  7:19 ` [PATCH v1 13/13] perf parse-events: Route non-field tracepoint filter terms to BPF filter 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=20261123071913.653315-9-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=howardchu95@gmail.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@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=songliubraving@fb.com \
    --cc=thomas.falcon@intel.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

all inboxes | Powered by JetHome®