From nobody Mon May 25 04:34:27 2026 Received: from mail-dy1-f202.google.com (mail-dy1-f202.google.com [74.125.82.202]) (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 E4847382F28 for ; Mon, 18 May 2026 18:39:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779129569; cv=none; b=Fx4nlkh3eIC6wU63vh255rn72DlZcTqlSFvsYwDNZ0mJdtR2aIRUSVVqlKF/z1PqYX53dgP1Q7lEbtzDFLGobPJ9M3aSyBRuDmuuZb3dCxR7EJ6zTMTpr1UhBsFL85rzCxfFC9I3SJSQPhiJdFlrqOoE8OI2/2ZV1UiT9cihg8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779129569; c=relaxed/simple; bh=LsBQXsSiB/GKoRhEFwa3JkDTcgSVcFo6Ml3134QUq6Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=TjaYHAN3fk7rCI+5YcgUJ+ch86AVVPClpIWvMzIkzOTB6UMr1n/rusMUg4BdWrULpO1WVgBMBPeojKhE8GYP7EoFKXhjVTIOp0z8JwS47LIdHlZwDYdmEQaHpPb1uZfItnnoRdqrUFSrJ2AK3idN1sFqDM1NODYpNdQpYF+OlhY= 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=hJZJVlsJ; arc=none smtp.client-ip=74.125.82.202 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="hJZJVlsJ" Received: by mail-dy1-f202.google.com with SMTP id 5a478bee46e88-2f1383b7439so2806233eec.1 for ; Mon, 18 May 2026 11:39:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779129567; x=1779734367; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=RanyjF1WhhXjyMap27LhI/CwQlITPbD9ctkTROJXpOg=; b=hJZJVlsJE3CR2aWT2BsEz4YM2/eTrCgrUETtWGnkdv8giRgO4fGZ1SxOfvWr1fHTkx R9dIiiAkItAgPuanWxAiN1U2fYKr2LUHGgRzu/i7yOMMjmFv0sZ1VBGe1fxA3tl6bhl/ 0XThexmbXlFxLKEWmsLgehnVPMAlDhjrpBjhkcyCZJMD94IHrpWJMki39OXyrRAiOu8q hY7dbs3a+p8gKt4YjteF6fCyhUm+7GEqX+D/MrvSIFsJD6PsDP18q5KmsOymIriAzdwn 0rZZUi8k2ObF8f0MQERCRVms7iMCYK9gmZXLR9s06u/MP8nAyIT+FqqsQvDAbUhcv+F5 CGdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779129567; x=1779734367; h=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=RanyjF1WhhXjyMap27LhI/CwQlITPbD9ctkTROJXpOg=; b=r3xe00i5KQT5dlTUHWFiGuZyFmpEUgxOmFHOn6tHofdvJK8JvpbVgbWQe886C3Mp6r 0HeJJuIhKj7YJWBkqSxNhYHl7DRinpCQ1GXtD/ayhCyymYOJO6FfzfaLEdSI/GvLsN7r oSeFS60LYmhQpJBEelC4ZGQpLYe+uStpjcTOIGNT2b9nG+GjF+cjYFbpyVOlM6ssL0Yv 3nCkSjogYcDp9w21bkCsa8a1UW0F1IN7LpklMUofntFIMMO/j02YzqGa0kipUgTj6giq 7f9cbDkX9NBRcXrtk9LFT9aNDLlhirwsD6WEh+7fyXVPFUfJU+1MN/PrBT5zdA8K29b3 D0/w== X-Forwarded-Encrypted: i=1; AFNElJ+Osx5vEfOz/xCSJhEbSj0f+9R/noNDgoAU9np97HK0gbQXr2v39tarlwVKVcUt+H9Zc+rZWzFIAU5HTAY=@vger.kernel.org X-Gm-Message-State: AOJu0YzNHTMc+60VG6e+H5Pk4K3eKlNA5auniRYE/x4PhmA4sOei09B/ /mlSe1Xc9K5xxv/xcR/9P+iWAkNtJeg+9sF/BzzLuiEsY+VO7jsxOZ7lR2df2XVY4iMwNVy5YJK 4DMxcc6BHpw== X-Received: from dlboy10.prod.google.com ([2002:a05:7022:128a:b0:134:feba:1ec8]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:41c:b0:135:246d:c4f5 with SMTP id a92af1059eb24-135246de58emr4635967c88.18.1779129566840; Mon, 18 May 2026 11:39:26 -0700 (PDT) Date: Mon, 18 May 2026 11:39:19 -0700 In-Reply-To: <20260518183920.2894502-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> X-Mailer: git-send-email 2.54.0.631.ge1b05301d1-goog Message-ID: <20260518183920.2894502-2-irogers@google.com> Subject: [PATCH v1 1/2] perf tool_pmu: Make tool PMU events respect enable/disable From: Ian Rogers To: Francesco Nigro , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Thomas Richter , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org 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 | 85 ++++++++------- tools/perf/util/evsel.h | 10 +- tools/perf/util/tool_pmu.c | 212 +++++++++++++++++++++++++++---------- tools/perf/util/tool_pmu.h | 2 + 4 files changed, 216 insertions(+), 93 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 2ee87fd84d3e..3646156500f6 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,6 +1798,8 @@ int evsel__append_addr_filter(struct evsel *evsel, co= nst char *filter) /* Caller has to clear disabled after going through all CPUs. */ int evsel__enable_cpu(struct evsel *evsel, int cpu_map_idx) { + if (evsel__is_tool(evsel)) + return evsel__tool_pmu_enable_cpu(evsel, cpu_map_idx); return perf_evsel__enable_cpu(&evsel->core, cpu_map_idx); } =20 @@ -1810,6 +1815,8 @@ 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) { + if (evsel__is_tool(evsel)) + return evsel__tool_pmu_disable_cpu(evsel, cpu_map_idx); return perf_evsel__disable_cpu(&evsel->core, cpu_map_idx); } =20 @@ -1885,8 +1892,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..cfefd97cb95b 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -205,20 +205,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, SEEK_SET, 0); + 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 +269,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 0; + } else { + evsel->disabled =3D false; + evsel->duration_time.start_time =3D rdclock(); + } return 0; } =20 @@ -246,8 +290,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 +313,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 0; } 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 +512,79 @@ static void perf_counts__update(struct perf_counts_va= lues *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; + } + } + return 0; +} + +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 (val >=3D *start_time) + *accumulated_time +=3D (val - *start_time); + } + } + } + return 0; +} =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 +621,32 @@ 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) + 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 (cur_time >=3D *start_time) + delta_start =3D accumulated + (cur_time - *start_time); + else + delta_start =3D accumulated; } } adjust =3D true; @@ -553,7 +660,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..f06ef5bce1c1 100644 --- a/tools/perf/util/tool_pmu.h +++ b/tools/perf/util/tool_pmu.h @@ -56,6 +56,8 @@ 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_disable_cpu(struct evsel *evsel, int cpu_map_idx); 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:27 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 7F10E38D3F6 for ; Mon, 18 May 2026 18:39:30 +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=1779129571; cv=none; b=osL4atP9Q6RRMohu7j5KhJaWm6cI5X+GsMwrpLrd4bD/6RkH9HX1hwbbKEu08Ko7Ja/sPswxR9Gw5myu6GA5Y3u3AN/ifP7Lv5ct34rY2qayAkAzoMuHq9/hr6mJ3IIdKmehcUGQ8G28lohwwHwCdI5ZyLN+bLsCVUAXkBYZcX4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779129571; c=relaxed/simple; bh=XFDHjZs7ib/AZkcV51y1cZbqwQFeIAPBj3G+QAfL+hI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=vBXucDdg6bI68aw1CdMpDyxiJLJ9zJz5ga/UbIkx1pLKdR1nCO+Ftui/U8X2u7bWqLT+Doa8VLp2qha5JduUtje33/hBJtWhALfju9RVC5JHB6K7XdXAeXQ3uZxi9hcrC6VPyBTpQzwHeEEydA38OtnwiJEtXl/MhG6DBP8U9GM= 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=ob7WqipC; 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="ob7WqipC" Received: by mail-dl1-f74.google.com with SMTP id a92af1059eb24-1329791f18fso4644204c88.1 for ; Mon, 18 May 2026 11:39:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779129570; x=1779734370; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=WjmjkNIBm6JKJOik2uUZR4eykueHeYyOAeYgNJxvIjk=; b=ob7WqipCLSaGd3sfn5MVtUN+fLw6P9pJRyEIpXy3nHpPEAakavEIEbP6M3qlXUMF1t hPRitsROPzgJX+hlGJ13067loKPfLm7M0AtvDlLA5Ema8CFsz3TFe2aWReP4GnV/LmRi Iv5l6iUq4zN+6HT04DtkOlqtRFYfd4pTz58yui48L5hk3yxnBan2dSoeW7m6XV5x+vYn wqmFRVgMIr8zxK2EwebZw74i6e3SD1iGl3lvIdOMbUy2HFHvYxCAqnJc9t1+j1BhA6PW /mKldUSd375PimNfN3RLFmLYTe81rIA4aLSKP4XrwBPV+Hup4zR7oLQ3HpJDy6hJadpk KLxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779129570; x=1779734370; h=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=WjmjkNIBm6JKJOik2uUZR4eykueHeYyOAeYgNJxvIjk=; b=YFFrtjB20FLBchPqNEYo7+v5OQ3KzYXmXU9MaOABYMxQvIYedKKsx7ht8CiACoVWan u5Ysp52z+LAY1DuxcPBgORZmgz8Z1Rmyg44VVLqgkyf9R1J4ve+hTMsvAz3h06dHVlCq 9sPsmpzcpSGFLGvAAAWAjLbpL0yibo0TGPYMKUdJrw0XZ8pRuwfkIy0hUcBcBcrqDSUG ipbG+M+3AJk7gqDL/Ac3aYeaKDcLdvRj4nThwAhm5ni/Hop1ZA8RZW9wEYdMunugwbYD rBxvhi3lYEW3+z+aytbLnLMRqwGNNfFvjpLXx43Nv4jx3Tc4FO7Iv3zzrmmRlvPl8cWl A6pg== X-Forwarded-Encrypted: i=1; AFNElJ87GT2qqoQx2ZcLc5/HP0Lb26w8KpTS0U/uKmqbije8+z6MoM+0C/EStqjgdDeQjr/k8LvmvlglVCpy2mU=@vger.kernel.org X-Gm-Message-State: AOJu0Ywbh6j5g7nrJNTFBhFNaJ2KOohoWbtNurd74sxB7BYMBc0FvcsJ yBsM0m1GfpbT7Wmk1mG6BkHh+89Dyi6jMt2wFWoddplqZA7LikAZex5+Fut0ER1M0lZl5ULaeQe XPgsrvrV3eg== X-Received: from dlbpt3.prod.google.com ([2002:a05:7022:e803:b0:135:3471:e411]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:419f:b0:128:ca83:5aa1 with SMTP id a92af1059eb24-1350451b07dmr6514932c88.16.1779129568940; Mon, 18 May 2026 11:39:28 -0700 (PDT) Date: Mon, 18 May 2026 11:39:20 -0700 In-Reply-To: <20260518183920.2894502-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> X-Mailer: git-send-email 2.54.0.631.ge1b05301d1-goog Message-ID: <20260518183920.2894502-3-irogers@google.com> Subject: [PATCH v1 2/2] perf tests: Add test for stat delay option with duration_time From: Ian Rogers To: Francesco Nigro , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Thomas Richter , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org 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 | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tools/perf/tests/shell/stat.sh b/tools/perf/tests/shell/stat.sh index 4edb04039036..e9a9b2c91cae 100755 --- a/tools/perf/tests/shell/stat.sh +++ b/tools/perf/tests/shell/stat.sh @@ -483,6 +483,38 @@ test_stat_pid() { wait $pid 2>/dev/null || true } =20 +test_stat_delay() { + echo "stat -D test" + if ! 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 ',') + if [ -z "$duration" ] + then + echo "stat -D test [Failed - failed to find duration_time in output]" + cat "${stat_output}" + err=3D1 + return + fi + + # duration is in ns. We expect it to be ~1s (1,000,000,000 ns) because + # sleep 2 runs for 2s, and we delay by 1s, so measured window is 1s. + # If it includes delay, it will be > 2s (2,000,000,000 ns). + if [ "$duration" -gt 1500000000 ] + then + echo "stat -D test [Failed - duration_time ($duration ns) includes del= ay]" + cat "${stat_output}" + err=3D1 + return + fi + echo "stat -D test [Success]" +} + test_default_stat test_null_stat test_offline_cpu_stat @@ -498,6 +530,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