From nobody Mon May 25 04:34:00 2026 Received: from mail-dl1-f73.google.com (mail-dl1-f73.google.com [74.125.82.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C16F039282C for ; Mon, 18 May 2026 20:14:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779135274; cv=none; b=g9kBYTf3kXnsJqLSp1En6BY7qD2DX0FFZOOygRw1CtcRUULIFz4bqMTXssCqQW3kY/Mhwra57BQAQIzJh0Dd0ig1ASBeA19W5oNMEkq7Mqsdv1M3AfXi44hk0npu2SLQ2nSECcdHTXdAjg3dcGDcWx5mgE6Mxhsr5MX9+9aVROw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779135274; c=relaxed/simple; bh=IjQpx23TT2al9Ew+UmvJuIv86PdRBHcEC+CieppgCEI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tvVs36jQoLsARZeHxT8q73Nz0dtIKnKjAsnGLnajsoFa6gSx8DFHBOLSGywXp9ORKawzex+7H/K5dnASpkQY7eWpdS5Z7TxlxiXRSu+aZLfpLCgYjTbb4TxPgaMDOJc/Z6PBwgQmFGZDapd/1XAFgyrG+8yv4rbYpSl1cPFPc2I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gt+virpF; arc=none smtp.client-ip=74.125.82.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gt+virpF" Received: by mail-dl1-f73.google.com with SMTP id a92af1059eb24-13537722193so12800820c88.0 for ; Mon, 18 May 2026 13:14:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779135272; x=1779740072; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xe2bM9nnKE/PY7F0qPFua0eXxZKM5lremDBfaHiyg2A=; b=gt+virpF5cj+Cszi4kSiN+unbHfPHyYv8RGgbww3y1cxH44B5Rwdx/WujCbJ18g8si WXXK/30/AhZtWgzl9JYSpzTDyhaUwfN3IUp8XZSsmJ+vYr2nlQtMaJC1DMiNMjfmVfMH OItSeA8KhsDdaYWG4ZPTK1wVrilhkvqQlkfNgIUlUj7XaT2hYYKVn5ZY9bvBTt8c2YJu tUksABa+yaieMMgP38gFICA+o0Rw/Yc+Rdklz5OFyXFHrHZPiKQjoOaPzfEF2Ky8ASlm gXXuHPwP5mh4vBIEBsF8l9ucTJQ+9S426j+R89DXPVT/BV78COS7g6da6JpDqpkRk/Ic LMow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779135272; x=1779740072; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=xe2bM9nnKE/PY7F0qPFua0eXxZKM5lremDBfaHiyg2A=; b=kgvMfE+Vt4f0D1dXTxwNlUGYabDSON6bdziVfGNraCM+njqnAt4+oKMqM4O0ePgY5d 1W4q6sF6+N2LCo9tBbsiuJw51NG1ytG4ns9tex0Iu0e+FRKyHtutmeOIDKnaV/stdsBb tt6wUN50o1fvwdeSdEhHgTuDik3AQvBgDwwVCy/8CJZ1fBug6iat7f+v/v5bdM4bc/wy 8U2t0cSO/x6KAefkli3Bl9Db0by18ED7rm2OG9WWw9Kl1LTVI48hBf5e15iwHZUGS6Ag 53xuAWZ4/BiCylcEXXmTXGDjpABL9rb8bHNLJk++a/3caivqEndnrmBL2wil/PkT5dMe 0sPQ== X-Forwarded-Encrypted: i=1; AFNElJ9MdJ3ObzrX08CBeAgzrBkTbRNuVGrYZxma9HZ+VRcJarnaVaRvAr9o3UTe1vFRxOe3e+9G/BDizwh7l+Y=@vger.kernel.org X-Gm-Message-State: AOJu0YwS62YWjrAfGTgNdGBQayWROSdRgNMIlsHjKfKUBvQ2VX3jyzBo avKg7hTGeGO5aHuPQnFioHrrtdncVGnoPFOGFNX9gDW/rXqqqoX4fzra1/dVZl1YUtZ84Jr1Ovm NiGSMQahFoQ== X-Received: from dlbtu23.prod.google.com ([2002:a05:7022:3c17:b0:12d:f9c6:4512]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:7f04:b0:135:512f:850c with SMTP id a92af1059eb24-135512f857dmr2621124c88.29.1779135271688; Mon, 18 May 2026 13:14:31 -0700 (PDT) Date: Mon, 18 May 2026 13:14:13 -0700 In-Reply-To: <20260518201419.2943132-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260518183920.2894502-1-irogers@google.com> <20260518201419.2943132-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.631.ge1b05301d1-goog Message-ID: <20260518201419.2943132-2-irogers@google.com> Subject: [PATCH v2 1/2] perf tool_pmu: Make tool PMU events respect enable/disable From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, nigro.fra@gmail.com, peterz@infradead.org, tmricht@linux.ibm.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Tool PMU events (duration_time, user_time, system_time) currently count from when the event is opened to when it is read. This causes issues with features like the delay option (-D) or control fd, where events are opened but should not start counting immediately. Make these events behave more like regular counters by implementing proper enable and disable support. Add accumulated_time to struct evsel to track time while enabled, and implement enable/disable CPU callbacks to start/stop counting. Fixes: b71f46a6a708 ("perf stat: Remove hard coded shadow metrics") Reported-by: Francesco Nigro Closes: https://lore.kernel.org/linux-perf-users/20260517093650.2540920-1-n= igro.fra@gmail.com/ Signed-off-by: Ian Rogers Assisted-by: Antigravity:gemini-3-flash --- tools/perf/util/evsel.c | 190 ++++++++++++++++++++++------- tools/perf/util/evsel.h | 10 +- tools/perf/util/tool_pmu.c | 244 +++++++++++++++++++++++++++++-------- tools/perf/util/tool_pmu.h | 4 + 4 files changed, 351 insertions(+), 97 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 2ee87fd84d3e..63bfb03b6b13 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -11,68 +11,71 @@ */ #define __SANE_USERSPACE_TYPES__ =20 -#include +#include "evsel.h" + #include #include +#include + +#include #include -#include -#include -#include -#include #include +#include #include +#include +#include #include #include #include #include #include -#include -#include + +#include +#include +#include +#include +#include +#include +#include #include + +#include "../perf-sys.h" #include "asm/bug.h" +#include "bpf-filter.h" #include "bpf_counter.h" #include "callchain.h" #include "cgroup.h" #include "counts.h" +#include "debug.h" +#include "drm_pmu.h" #include "dwarf-regs.h" +#include "env.h" #include "event.h" -#include "evsel.h" -#include "time-utils.h" -#include "util/env.h" -#include "util/evsel_config.h" -#include "util/evsel_fprintf.h" #include "evlist.h" -#include -#include "thread_map.h" -#include "target.h" +#include "evsel_config.h" +#include "evsel_fprintf.h" +#include "hashmap.h" +#include "hist.h" +#include "hwmon_pmu.h" +#include "intel-tpebs.h" +#include "memswap.h" +#include "off_cpu.h" +#include "parse-branch-options.h" #include "perf_regs.h" +#include "pmu.h" +#include "pmus.h" #include "record.h" -#include "debug.h" -#include "trace-event.h" +#include "rlimit.h" #include "session.h" #include "stat.h" #include "string2.h" -#include "memswap.h" -#include "util.h" -#include "util/hashmap.h" -#include "off_cpu.h" -#include "pmu.h" -#include "pmus.h" -#include "drm_pmu.h" -#include "hwmon_pmu.h" +#include "target.h" +#include "thread_map.h" +#include "time-utils.h" #include "tool_pmu.h" #include "tp_pmu.h" -#include "rlimit.h" -#include "../perf-sys.h" -#include "util/parse-branch-options.h" -#include "util/bpf-filter.h" -#include "util/hist.h" -#include -#include -#include -#include "util/intel-tpebs.h" - -#include +#include "trace-event.h" +#include "util.h" =20 #ifdef HAVE_LIBTRACEEVENT #include @@ -1795,12 +1798,63 @@ int evsel__append_addr_filter(struct evsel *evsel, = const char *filter) /* Caller has to clear disabled after going through all CPUs. */ int evsel__enable_cpu(struct evsel *evsel, int cpu_map_idx) { - return perf_evsel__enable_cpu(&evsel->core, cpu_map_idx); + int err; + + if (evsel__is_tool(evsel)) + err =3D evsel__tool_pmu_enable_cpu(evsel, cpu_map_idx); + else + err =3D perf_evsel__enable_cpu(&evsel->core, cpu_map_idx); + + if (!err && evsel__is_group_leader(evsel)) { + struct evsel *member; + + for_each_group_member(member, evsel) { + if (evsel__is_tool(member)) { + /* + * In a mixed PMU group, tool PMU events are not + * grouped in the kernel (opened with group_fd =3D -1) + * and are skipped by the kernel when enabling the + * group leader. We must manually enable them in + * userspace. + */ + int mem_err =3D evsel__tool_pmu_enable_cpu(member, cpu_map_idx); + + if (mem_err) + return mem_err; + } + } + } + return err; } =20 int evsel__enable(struct evsel *evsel) { - int err =3D perf_evsel__enable(&evsel->core); + int err; + + if (evsel__is_tool(evsel)) + err =3D evsel__tool_pmu_enable(evsel); + else + err =3D perf_evsel__enable(&evsel->core); + + if (!err && evsel__is_group_leader(evsel)) { + struct evsel *member; + + for_each_group_member(member, evsel) { + if (evsel__is_tool(member)) { + /* + * In a mixed PMU group, tool PMU events are not + * grouped in the kernel (opened with group_fd =3D -1) + * and are skipped by the kernel when enabling the + * group leader. We must manually enable them in + * userspace. + */ + int mem_err =3D evsel__tool_pmu_enable(member); + + if (mem_err) + return mem_err; + } + } + } =20 if (!err) evsel->disabled =3D false; @@ -1810,12 +1864,62 @@ int evsel__enable(struct evsel *evsel) /* Caller has to set disabled after going through all CPUs. */ int evsel__disable_cpu(struct evsel *evsel, int cpu_map_idx) { - return perf_evsel__disable_cpu(&evsel->core, cpu_map_idx); + int err; + + if (evsel__is_tool(evsel)) + err =3D evsel__tool_pmu_disable_cpu(evsel, cpu_map_idx); + else + err =3D perf_evsel__disable_cpu(&evsel->core, cpu_map_idx); + + if (!err && evsel__is_group_leader(evsel)) { + struct evsel *member; + + for_each_group_member(member, evsel) { + if (evsel__is_tool(member)) { + /* + * In a mixed PMU group, tool PMU events are not + * grouped in the kernel and are skipped by the + * kernel when disabling the group leader. We must + * manually disable them in userspace. + */ + int mem_err =3D evsel__tool_pmu_disable_cpu(member, cpu_map_idx); + + if (mem_err) + return mem_err; + } + } + } + return err; } =20 int evsel__disable(struct evsel *evsel) { - int err =3D perf_evsel__disable(&evsel->core); + int err; + + if (evsel__is_tool(evsel)) + err =3D evsel__tool_pmu_disable(evsel); + else + err =3D perf_evsel__disable(&evsel->core); + + if (!err && evsel__is_group_leader(evsel)) { + struct evsel *member; + + for_each_group_member(member, evsel) { + if (evsel__is_tool(member)) { + /* + * In a mixed PMU group, tool PMU events are not + * grouped in the kernel and are skipped by the + * kernel when disabling the group leader. We must + * manually disable them in userspace. + */ + int mem_err =3D evsel__tool_pmu_disable(member); + + if (mem_err) + return mem_err; + } + } + } + /* * We mark it disabled here so that tools that disable a event can * ignore events after they disable it. I.e. the ring buffer may have @@ -1885,8 +1989,10 @@ void evsel__exit(struct evsel *evsel) evsel__priv_destructor(evsel->priv); perf_evsel__object.fini(evsel); if (evsel__tool_event(evsel) =3D=3D TOOL_PMU__EVENT_SYSTEM_TIME || - evsel__tool_event(evsel) =3D=3D TOOL_PMU__EVENT_USER_TIME) - xyarray__delete(evsel->start_times); + evsel__tool_event(evsel) =3D=3D TOOL_PMU__EVENT_USER_TIME) { + xyarray__delete(evsel->process_time.start_times); + xyarray__delete(evsel->process_time.accumulated_times); + } } =20 void evsel__delete(struct evsel *evsel) diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 339b5c08a33d..0b6a6df0e4ef 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -190,12 +190,18 @@ struct evsel { double max; } retirement_latency; /* duration_time is a single global time. */ - __u64 start_time; + struct { + __u64 start_time; + __u64 accumulated_time; + } duration_time; /* * user_time and system_time read an initial value potentially * per-CPU or per-pid. */ - struct xyarray *start_times; + struct { + struct xyarray *start_times; + struct xyarray *accumulated_times; + } process_time; }; /* Is the tool's fd for /proc/pid/stat or /proc/stat. */ bool pid_stat; diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c index 6a9df3dc0e07..bb398fe82f6d 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -17,6 +17,8 @@ #include #include =20 +#define INVALID_START_TIME ~0ULL + static const char *const tool_pmu__event_names[TOOL_PMU__EVENT_MAX] =3D { NULL, "duration_time", @@ -205,20 +207,57 @@ int evsel__tool_pmu_prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus, int nthreads) { - if ((evsel__tool_event(evsel) =3D=3D TOOL_PMU__EVENT_SYSTEM_TIME || - evsel__tool_event(evsel) =3D=3D TOOL_PMU__EVENT_USER_TIME) && - !evsel->start_times) { - evsel->start_times =3D xyarray__new(perf_cpu_map__nr(cpus), - nthreads, - sizeof(__u64)); - if (!evsel->start_times) - return -ENOMEM; + enum tool_pmu_event ev =3D evsel__tool_event(evsel); + + if (ev =3D=3D TOOL_PMU__EVENT_SYSTEM_TIME || ev =3D=3D TOOL_PMU__EVENT_US= ER_TIME) { + if (!evsel->process_time.start_times) { + evsel->process_time.start_times =3D + xyarray__new(perf_cpu_map__nr(cpus), nthreads, sizeof(__u64)); + if (!evsel->process_time.start_times) + return -ENOMEM; + } + if (!evsel->process_time.accumulated_times) { + evsel->process_time.accumulated_times =3D + xyarray__new(perf_cpu_map__nr(cpus), nthreads, sizeof(__u64)); + if (!evsel->process_time.accumulated_times) + return -ENOMEM; + } } return 0; } =20 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) =20 +static int tool_pmu__read_stat(struct evsel *evsel, int cpu_map_idx, int t= hread, __u64 *val) +{ + enum tool_pmu_event ev =3D evsel__tool_event(evsel); + bool system =3D ev =3D=3D TOOL_PMU__EVENT_SYSTEM_TIME; + int fd =3D FD(evsel, cpu_map_idx, thread); + int err =3D 0; + + if (fd < 0) { + *val =3D 0; + return 0; + } + + lseek(fd, 0, SEEK_SET); + if (evsel->pid_stat) { + if (cpu_map_idx =3D=3D 0) + err =3D read_pid_stat_field(fd, system ? 15 : 14, val); + else + *val =3D 0; + } else { + if (thread =3D=3D 0) { + struct perf_cpu cpu =3D perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx= ); + + err =3D read_stat_field(fd, cpu, system ? 3 : 1, val); + } else { + *val =3D 0; + } + } + return err; +} + int evsel__tool_pmu_open(struct evsel *evsel, struct perf_thread_map *threads, int start_cpu_map_idx, int end_cpu_map_idx) @@ -232,7 +271,14 @@ int evsel__tool_pmu_open(struct evsel *evsel, if (ev =3D=3D TOOL_PMU__EVENT_DURATION_TIME) { if (evsel->core.attr.sample_period) /* no sampling */ return -EINVAL; - evsel->start_time =3D rdclock(); + evsel->duration_time.accumulated_time =3D 0; + if (evsel->core.attr.disabled) { + evsel->disabled =3D true; + evsel->duration_time.start_time =3D INVALID_START_TIME; + } else { + evsel->disabled =3D false; + evsel->duration_time.start_time =3D rdclock(); + } return 0; } =20 @@ -246,8 +292,8 @@ int evsel__tool_pmu_open(struct evsel *evsel, pid =3D perf_thread_map__pid(threads, thread); =20 if (ev =3D=3D TOOL_PMU__EVENT_USER_TIME || ev =3D=3D TOOL_PMU__EVENT_SY= STEM_TIME) { - bool system =3D ev =3D=3D TOOL_PMU__EVENT_SYSTEM_TIME; __u64 *start_time =3D NULL; + __u64 *accumulated_time =3D NULL; int fd; =20 if (evsel->core.attr.sample_period) { @@ -269,21 +315,22 @@ int evsel__tool_pmu_open(struct evsel *evsel, err =3D -errno; goto out_close; } - start_time =3D xyarray__entry(evsel->start_times, idx, thread); - if (pid > -1) { - err =3D read_pid_stat_field(fd, system ? 15 : 14, - start_time); + start_time =3D xyarray__entry(evsel->process_time.start_times, idx, + thread); + accumulated_time =3D xyarray__entry( + evsel->process_time.accumulated_times, idx, thread); + *accumulated_time =3D 0; + + if (evsel->core.attr.disabled) { + evsel->disabled =3D true; + *start_time =3D INVALID_START_TIME; } else { - struct perf_cpu cpu; - - cpu =3D perf_cpu_map__cpu(evsel->core.cpus, idx); - err =3D read_stat_field(fd, cpu, system ? 3 : 1, - start_time); + evsel->disabled =3D false; + err =3D tool_pmu__read_stat(evsel, idx, thread, start_time); + if (err) + goto out_close; } - if (err) - goto out_close; } - } } return 0; @@ -467,10 +514,108 @@ static void perf_counts__update(struct perf_counts_v= alues *count, count->lost =3D 0; } } +int evsel__tool_pmu_enable_cpu(struct evsel *evsel, int cpu_map_idx) +{ + enum tool_pmu_event ev =3D evsel__tool_event(evsel); + int thread, nthreads; + + if (!evsel->disabled) + return 0; + + if (ev =3D=3D TOOL_PMU__EVENT_DURATION_TIME) { + if (cpu_map_idx =3D=3D 0) { + evsel->duration_time.start_time =3D rdclock(); + evsel->disabled =3D false; + } + return 0; + } + + if (ev =3D=3D TOOL_PMU__EVENT_USER_TIME || ev =3D=3D TOOL_PMU__EVENT_SYST= EM_TIME) { + nthreads =3D xyarray__max_y(evsel->process_time.start_times); + for (thread =3D 0; thread < nthreads; thread++) { + __u64 *start_time =3D xyarray__entry(evsel->process_time.start_times, + cpu_map_idx, thread); + __u64 val; + int err; + + err =3D tool_pmu__read_stat(evsel, cpu_map_idx, thread, &val); + if (!err) + *start_time =3D val; + else + *start_time =3D INVALID_START_TIME; + } + } + return 0; +} + +int evsel__tool_pmu_enable(struct evsel *evsel) +{ + unsigned int idx; + int err =3D 0; + + for (idx =3D 0; idx < perf_cpu_map__nr(evsel->core.cpus); idx++) { + err =3D evsel__tool_pmu_enable_cpu(evsel, idx); + if (err) + break; + } + return err; +} + +int evsel__tool_pmu_disable_cpu(struct evsel *evsel, int cpu_map_idx) +{ + enum tool_pmu_event ev =3D evsel__tool_event(evsel); + int thread, nthreads; + + if (evsel->disabled) + return 0; + + if (ev =3D=3D TOOL_PMU__EVENT_DURATION_TIME) { + if (cpu_map_idx =3D=3D 0) { + __u64 delta =3D rdclock() - evsel->duration_time.start_time; + + evsel->duration_time.accumulated_time +=3D delta; + evsel->disabled =3D true; + } + return 0; + } + + if (ev =3D=3D TOOL_PMU__EVENT_USER_TIME || ev =3D=3D TOOL_PMU__EVENT_SYST= EM_TIME) { + nthreads =3D xyarray__max_y(evsel->process_time.start_times); + for (thread =3D 0; thread < nthreads; thread++) { + __u64 *start_time =3D xyarray__entry(evsel->process_time.start_times, + cpu_map_idx, thread); + __u64 *accumulated_time =3D xyarray__entry( + evsel->process_time.accumulated_times, cpu_map_idx, thread); + __u64 val; + int err; + + err =3D tool_pmu__read_stat(evsel, cpu_map_idx, thread, &val); + if (!err) { + if (*start_time !=3D INVALID_START_TIME && val >=3D *start_time) + *accumulated_time +=3D (val - *start_time); + *start_time =3D INVALID_START_TIME; + } + } + } + return 0; +} + +int evsel__tool_pmu_disable(struct evsel *evsel) +{ + unsigned int idx; + int err =3D 0; + + for (idx =3D 0; idx < perf_cpu_map__nr(evsel->core.cpus); idx++) { + err =3D evsel__tool_pmu_disable_cpu(evsel, idx); + if (err) + break; + } + return err; +} =20 int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread) { - __u64 *start_time, cur_time, delta_start; + __u64 delta_start =3D 0; int err =3D 0; struct perf_counts_values *count, *old_count =3D NULL; bool adjust =3D false; @@ -507,39 +652,33 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu= _map_idx, int thread) return 0; } case TOOL_PMU__EVENT_DURATION_TIME: - /* - * Pretend duration_time is only on the first CPU and thread, or - * else aggregation will scale duration_time by the number of - * CPUs/threads. - */ - start_time =3D &evsel->start_time; - if (cpu_map_idx =3D=3D 0 && thread =3D=3D 0) - cur_time =3D rdclock(); - else - cur_time =3D *start_time; + if (cpu_map_idx =3D=3D 0 && thread =3D=3D 0) { + delta_start =3D evsel->duration_time.accumulated_time; + if (!evsel->disabled && + evsel->duration_time.start_time !=3D INVALID_START_TIME) + delta_start +=3D (rdclock() - evsel->duration_time.start_time); + } else { + delta_start =3D 0; + } break; case TOOL_PMU__EVENT_USER_TIME: case TOOL_PMU__EVENT_SYSTEM_TIME: { - bool system =3D evsel__tool_event(evsel) =3D=3D TOOL_PMU__EVENT_SYSTEM_T= IME; - int fd =3D FD(evsel, cpu_map_idx, thread); - - start_time =3D xyarray__entry(evsel->start_times, cpu_map_idx, thread); - lseek(fd, SEEK_SET, 0); - if (evsel->pid_stat) { - /* The event exists solely on 1 CPU. */ - if (cpu_map_idx =3D=3D 0) - err =3D read_pid_stat_field(fd, system ? 15 : 14, &cur_time); - else - cur_time =3D 0; - } else { - /* The event is for all threads. */ - if (thread =3D=3D 0) { - struct perf_cpu cpu =3D perf_cpu_map__cpu(evsel->core.cpus, - cpu_map_idx); + __u64 accumulated =3D *(__u64 *)xyarray__entry(evsel->process_time.accum= ulated_times, + cpu_map_idx, thread); =20 - err =3D read_stat_field(fd, cpu, system ? 3 : 1, &cur_time); - } else { - cur_time =3D 0; + if (evsel->disabled) { + delta_start =3D accumulated; + } else { + __u64 *start_time =3D xyarray__entry(evsel->process_time.start_times, + cpu_map_idx, thread); + __u64 cur_time; + + err =3D tool_pmu__read_stat(evsel, cpu_map_idx, thread, &cur_time); + if (!err) { + if (*start_time !=3D INVALID_START_TIME && cur_time >=3D *start_time) + delta_start =3D accumulated + (cur_time - *start_time); + else + delta_start =3D accumulated; } } adjust =3D true; @@ -553,7 +692,6 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_m= ap_idx, int thread) if (err) return err; =20 - delta_start =3D cur_time - *start_time; if (adjust) { __u64 ticks_per_sec =3D sysconf(_SC_CLK_TCK); =20 diff --git a/tools/perf/util/tool_pmu.h b/tools/perf/util/tool_pmu.h index f1714001bc1d..f66d24cf3502 100644 --- a/tools/perf/util/tool_pmu.h +++ b/tools/perf/util/tool_pmu.h @@ -56,6 +56,10 @@ int evsel__tool_pmu_prepare_open(struct evsel *evsel, int evsel__tool_pmu_open(struct evsel *evsel, struct perf_thread_map *threads, int start_cpu_map_idx, int end_cpu_map_idx); +int evsel__tool_pmu_enable_cpu(struct evsel *evsel, int cpu_map_idx); +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); =20 struct perf_pmu *tool_pmu__new(void); --=20 2.54.0.631.ge1b05301d1-goog From nobody Mon May 25 04:34:00 2026 Received: from mail-dl1-f74.google.com (mail-dl1-f74.google.com [74.125.82.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E8CF392C4B for ; Mon, 18 May 2026 20:14:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779135276; cv=none; b=lPQCuTn5oN5FIfERQjpjlb/mxtdVylJFO3VE6VCLonInVEICCPB+5ezoGmq1G5hKp3LaRNN4zCOdkhrmP9T42xlEm8F9sJgepoq0oaCsdfFhtINVT58h3FummCw7AM+GzI8t6SNmAjzshODs5Bj8DBPa/c43dlfYWmDggX4lb/8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779135276; c=relaxed/simple; bh=FpUJiLkcnDUY6MDiOfrzffRQj9kMl4QOrpN2SDHOTGE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aIyM6n5sfLNm0tFYfunwTlHVZGvR02ij2Axqz39XCmCm6JQbDjcDBJuhd7bTQSPpovDwlu9DBg7hv9yiD4ZYztiiA7It7fG/H7s+zAGZ19GlvRJ5fBVH+f6D0gz76+IvT5Ls1wWr/AgjmGPiiPGM4dZd660Vz/rDL9J2mnHg//w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=CV/FrISJ; arc=none smtp.client-ip=74.125.82.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CV/FrISJ" Received: by mail-dl1-f74.google.com with SMTP id a92af1059eb24-12dece274b1so3876077c88.1 for ; Mon, 18 May 2026 13:14:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779135274; x=1779740074; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wDGrYR09FiJUwVsBSs6pa3l2SWK4RVY/PO4SX5MlSuM=; b=CV/FrISJHx8WaK/f3sYjSxmJe2rHVS3vTttUP65gZEzRDAdtReOi5ZhdF7mmKH1WVB NASjbOG1SXG0qBpQ1F4v3djM8CHLOlNFsO3g4ZK0OCUZuARZDU0qS3GaXK+moz9hFbX9 QmlqiN8aojyMAv2rG3FxJ2xyhKvdut0TklSOt5r0vfKlWuXRywI3ASn/QaAQMoy6TKPe Sp+6PPpSYaTvPx8gx6jdo8Cq/ELHDOkYr7NSqStLHMZQkfi3cfvGh9G2wxr8SuTEYUA/ h72DLcgjnzs2AvP/m9H0661nheLpvCUWGQ1ZvuvH0/Vkf4wp6GlgEZp5piaKM2TMBE/p 6B/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779135274; x=1779740074; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wDGrYR09FiJUwVsBSs6pa3l2SWK4RVY/PO4SX5MlSuM=; b=IMPW6C0EzWL/v95me7dHYACE1sx4bFjxCNBm4Jnl5BKfd4Ugs6f5UjuMQkWoN4itIT LAMKBECSIsyoxjNqeneVkGsgbkWvQLbdhjvOOBQyvZiFlcdaf7ZNJMe3omWOHgbDtDMI //jhrxajibXY6doxZSGwPr47UZ0VEkf+EheLVqOi8jLtD2KboGAbPu9+gqfIwO4BQQOe oJKgePTBnWZC4R5b/C7I76KbqfjWl6OiFwSKUxkBGEX8bgQHpZEjpBILJK6LNfmpi5cC lJWWckI1aGY3bIluPNDfIX74LYnxsKT12Ui+6LzMhoqLWjC9+c7/sLG/HMIgWY25pCq7 XwNg== X-Forwarded-Encrypted: i=1; AFNElJ8wT+l19yJmh01RtLKxTAwWnQQclRvoPaFTf8V0B5XvVjBh4w8C7PgJxwWsU6LoHAEifBcWQJO6PTfHn+I=@vger.kernel.org X-Gm-Message-State: AOJu0YxQyZkMi+ZUZNgAOiqy1mHtK9T6xNxVPqwzz5gaGjlBLcplpfrs Xer5+JINQamzPzHKj28uqHfevyAKvI27+f0Gg07jv1By6IueR9K3E39ly219v56sgHGUshvFzDv wzKRxVocWTg== X-Received: from dly10-n1.prod.google.com ([2002:a05:701b:204a:10b0:12d:b7d3:3e31]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:6628:b0:128:d5bd:3572 with SMTP id a92af1059eb24-1350484c6dbmr7524306c88.31.1779135273564; Mon, 18 May 2026 13:14:33 -0700 (PDT) Date: Mon, 18 May 2026 13:14:14 -0700 In-Reply-To: <20260518201419.2943132-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260518183920.2894502-1-irogers@google.com> <20260518201419.2943132-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.631.ge1b05301d1-goog Message-ID: <20260518201419.2943132-3-irogers@google.com> Subject: [PATCH v2 2/2] perf tests: Add test for stat delay option with duration_time From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, nigro.fra@gmail.com, peterz@infradead.org, tmricht@linux.ibm.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a new test case `test_stat_delay` to `stat.sh` to verify that `duration_time` correctly excludes the delay period when using the delay option (-D). The test runs `perf stat -D 1000 -e duration_time sleep 2` and verifies that `duration_time` is ~1s (excluding the 1s delay), not ~2s. Signed-off-by: Ian Rogers Assisted-by: Antigravity:gemini-3-flash --- tools/perf/tests/shell/stat.sh | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tools/perf/tests/shell/stat.sh b/tools/perf/tests/shell/stat.sh index 4edb04039036..a9eadd57e164 100755 --- a/tools/perf/tests/shell/stat.sh +++ b/tools/perf/tests/shell/stat.sh @@ -483,6 +483,53 @@ test_stat_pid() { wait $pid 2>/dev/null || true } =20 +test_stat_delay() { + echo "stat -D test" + if ! env LC_ALL=3DC perf stat -D 1000 -e duration_time sleep 2 > "${stat= _output}" 2>&1 + then + echo "stat -D test [Failed - command failed]" + cat "${stat_output}" + err=3D1 + return + fi + + duration=3D$(grep "duration_time" "${stat_output}" | awk '{print $1}' | = tr -d ',') + elapsed=3D$(grep "seconds time elapsed" "${stat_output}" | awk '{print $= 1}') + + if [ -z "$duration" ] || [ -z "$elapsed" ] + then + echo "stat -D test [Failed - failed to find duration_time or time elap= sed in output]" + cat "${stat_output}" + err=3D1 + return + fi + + # Compare duration (ns) and elapsed (s) using awk to handle float and al= low tolerance. + if ! awk -v d=3D"$duration" -v e=3D"$elapsed" ' + BEGIN { + diff =3D d - (e * 1e9); + if (diff < 0) diff =3D -diff; + # Allow 200ms tolerance (200,000,000 ns) for loaded CI machines. + if (diff > 200000000) { + printf "Fail: duration (%d ns) and elapsed (%f s) mismatch (diff %= d ns)\n", d, e, diff; + exit 1; + } + # Lower bound check: must be at least 0.5s. + if (d < 500000000) { + printf "Fail: duration (%d ns) is abnormally small\n", d; + exit 1; + } + exit 0; + }' + then + echo "stat -D test [Failed - validation failed]" + cat "${stat_output}" + err=3D1 + return + fi + echo "stat -D test [Success]" +} + test_default_stat test_null_stat test_offline_cpu_stat @@ -498,6 +545,7 @@ test_stat_no_aggr test_stat_detailed test_stat_repeat test_stat_pid +test_stat_delay =20 cleanup exit $err --=20 2.54.0.631.ge1b05301d1-goog