From nobody Sun Feb 8 21:28:04 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 90B5D35970C for ; Thu, 13 Nov 2025 18:05:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763057151; cv=none; b=otLFuATu70kGtGu07nDHvUvl+s5YMSJgIzL0EoajXYI27Zq7LKikVsQXa49ZguqE7vmlkVFQj+Rv8hLBtzM+c7A/GzBHL2I6k8odVcftYVoqdejIcKtkxMOxp8hh1f8vxBMGAzybcfkp2NKIw91nR6Ts9NWP5LGYW11vbtMdNrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763057151; c=relaxed/simple; bh=74ic2NR19Qj1jkhqoFr5/GAzNGfc9K6j6Uwf4Hjc/ss=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=KYLQJincppqa5OLzqY1o93rAUggqNx+fjWVPfZTps+GwJ8evpA/b2yaInnfUYgXUIL6p6sdkN+V1rhAHD+SPW8gxDQop6h24L9NXmRSkSceTooFVrUuQ1OiuLhiwT369AT7rieysgknoABFDkt/XTCKrKjkadVvoaBVCDljgNtI= 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=mri/KYzG; arc=none smtp.client-ip=209.85.215.201 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="mri/KYzG" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b99763210e5so1078997a12.3 for ; Thu, 13 Nov 2025 10:05:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1763057149; x=1763661949; 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=JvaqfEqqQj/hZXmBgRInugRb7NiwpIIhCyLsGdqnH4o=; b=mri/KYzGgwNibHkgXkR6nA8YW7d7+yF4GCCymoKxubSUAMemno6qhBFsk+x6Ng8MC3 bo2W5Har5/+mQrt0JDaTCT12rV1YUFBsWYdIjOUcZWi9Vr8NqNt1jtIAdsT8VbTbVaKI wacYDUBBMHc4uUPMrwT6nbe3oMhW1J58g8FcHCkYNW4XSsrq6CPQlUzkw6IS3K9Gz6Ol LusZ43K+033Pm3IEKJcZQj7FKCCDrEy6p8DU7cS67OECI6hg5VgmZhQl+8yHIanof7Og zSbHeeItWQhY4XNFAXabmDIlUSAQBB6yaC85ZRIexyofaEWG2Jb54tWA5F73ztpXbBaF dzRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763057149; x=1763661949; 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=JvaqfEqqQj/hZXmBgRInugRb7NiwpIIhCyLsGdqnH4o=; b=goYvWjuP0dw6C5sgn5FPcGCvoy90ZiNwMC9C/tdYmXvu91zIdioJBcyibUf5UQ+Ibk CDDcw/y5CbS5Q6j6YxcC3ZfLHdgi6GjgQdmV93tAxJu8hAMEwi+DtJpphawF7At3aF5V v7eN1JZnhslrA43Md0CsS2z3Gauxjt67dOEOWcsQzyuQU3BZ0+2Pzss7PgwjXA4Geaoo HnFRLdqgFITCzsRb5xlQHWSyZf7M6VMlEJlcTqBXe/qSRz4q+a0Veo5Vp/7DdZEdqWU1 vH2Fn4g0qVwq64Iq4aGZE2qLr/IS7dzbL909iwwoalUBo4QL/3gNIe4JIu1amXyg5Rr5 FTcw== X-Forwarded-Encrypted: i=1; AJvYcCXSV3o6VVMWWuDKhHQRKOGFB+LwX62xlPS4ak99IBJPqNUS/NSZt8x0rTA+TYERIsk7Zdi3Lm0DoN/KdVA=@vger.kernel.org X-Gm-Message-State: AOJu0YzCR/EkY2TAZ6HFI0wcpV1zpcelT8X5QR6Au25FakfhCcgijT+9 GzVK6OTAYjQlTqQf2QcA9KejSb+Zhkfiunu4A0uMvZYs19jsB9PzfqZTqNkP57jXv9hq9rARCBf jUTtVeWZ9hw== X-Google-Smtp-Source: AGHT+IF0NwPOIq3TmGs4pfWbaVyz7eK77f2zPBftHfCowiKCgIk6oaj+NWaIDUfX61uCgGqUhOiGvhsMQt2A X-Received: from dycne3.prod.google.com ([2002:a05:7301:c83:b0:2a4:51e8:c5c8]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:bc1a:b0:2a4:3593:6466 with SMTP id 5a478bee46e88-2a4abb1cf05mr160511eec.22.1763057148729; Thu, 13 Nov 2025 10:05:48 -0800 (PST) Date: Thu, 13 Nov 2025 10:05:09 -0800 In-Reply-To: <20251113180517.44096-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: <20251113180517.44096-1-irogers@google.com> X-Mailer: git-send-email 2.52.0.rc1.455.g30608eb744-goog Message-ID: <20251113180517.44096-4-irogers@google.com> Subject: [PATCH v4 03/10] perf tool_pmu: Use old_count when computing count values for time events From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Dr. David Alan Gilbert" , Yang Li , James Clark , Thomas Falcon , Thomas Richter , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Andi Kleen , Dapeng Mi Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When running in interval mode every third count of a time event isn't showing properly: ``` $ perf stat -e duration_time -a -I 1000 1.001082862 1,002,290,425 duration_time 2.004264262 1,003,183,516 duration_time 3.007381401 duration_time 4.011160141 1,003,705,631 duration_time 5.014515385 1,003,290,110 duration_time 6.018539680 duration_time 7.022065321 1,003,591,720 duration_time ``` The regression came in with a different fix, found through bisection, commit 68cb1567439f ("perf tool_pmu: Fix aggregation on duration_time"). The issue is caused by the enabled and running time of the event matching the old_count's and creating a delta of 0, which is indicative of an error. Fixes: 68cb1567439f ("perf tool_pmu: Fix aggregation on duration_time") Signed-off-by: Ian Rogers --- tools/perf/builtin-stat.c | 16 +++++++++-- tools/perf/util/tool_pmu.c | 59 +++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 095016b2209e..654f840f7a2f 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -283,17 +283,27 @@ static int read_single_counter(struct evsel *counter,= int cpu_map_idx, int threa if (err && cpu_map_idx =3D=3D 0 && (evsel__tool_event(counter) =3D=3D TOOL_PMU__EVENT_USER_TIME || evsel__tool_event(counter) =3D=3D TOOL_PMU__EVENT_SYSTEM_TIME)) { - u64 val, *start_time; struct perf_counts_values *count =3D perf_counts(counter->counts, cpu_map_idx, thread); + struct perf_counts_values *old_count =3D NULL; + u64 val; + + if (counter->prev_raw_counts) + old_count =3D perf_counts(counter->prev_raw_counts, cpu_map_idx, thread= ); =20 - start_time =3D xyarray__entry(counter->start_times, cpu_map_idx, thread); if (evsel__tool_event(counter) =3D=3D TOOL_PMU__EVENT_USER_TIME) val =3D ru_stats.ru_utime_usec_stat.mean; else val =3D ru_stats.ru_stime_usec_stat.mean; - count->ena =3D count->run =3D *start_time + val; + count->val =3D val; + if (old_count) { + count->run =3D old_count->run + 1; + count->ena =3D old_count->ena + 1; + } else { + count->run++; + count->ena++; + } return 0; } return err; diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c index a72c665ee644..6a9df3dc0e07 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -446,16 +446,39 @@ bool tool_pmu__read_event(enum tool_pmu_event ev, } } =20 +static void perf_counts__update(struct perf_counts_values *count, + const struct perf_counts_values *old_count, + bool raw, u64 val) +{ + /* + * The values of enabled and running must make a ratio of 100%. The + * exact values don't matter as long as they are non-zero to avoid + * issues with evsel__count_has_error. + */ + if (old_count) { + count->val =3D raw ? val : old_count->val + val; + count->run =3D old_count->run + 1; + count->ena =3D old_count->ena + 1; + count->lost =3D old_count->lost; + } else { + count->val =3D val; + count->run++; + count->ena++; + count->lost =3D 0; + } +} + int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread) { __u64 *start_time, cur_time, delta_start; - u64 val; - int fd, err =3D 0; + int err =3D 0; struct perf_counts_values *count, *old_count =3D NULL; bool adjust =3D false; enum tool_pmu_event ev =3D evsel__tool_event(evsel); =20 count =3D perf_counts(evsel->counts, cpu_map_idx, thread); + if (evsel->prev_raw_counts) + old_count =3D perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread); =20 switch (ev) { case TOOL_PMU__EVENT_HAS_PMEM: @@ -466,12 +489,11 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu= _map_idx, int thread) case TOOL_PMU__EVENT_NUM_PACKAGES: case TOOL_PMU__EVENT_SLOTS: case TOOL_PMU__EVENT_SMT_ON: - case TOOL_PMU__EVENT_SYSTEM_TSC_FREQ: case TOOL_PMU__EVENT_CORE_WIDE: case TOOL_PMU__EVENT_TARGET_CPU: - if (evsel->prev_raw_counts) - old_count =3D perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread); - val =3D 0; + case TOOL_PMU__EVENT_SYSTEM_TSC_FREQ: { + u64 val =3D 0; + if (cpu_map_idx =3D=3D 0 && thread =3D=3D 0) { if (!tool_pmu__read_event(ev, evsel, stat_config.system_wide, @@ -481,16 +503,9 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_= map_idx, int thread) val =3D 0; } } - if (old_count) { - count->val =3D old_count->val + val; - count->run =3D old_count->run + 1; - count->ena =3D old_count->ena + 1; - } else { - count->val =3D val; - count->run++; - count->ena++; - } + perf_counts__update(count, old_count, /*raw=3D*/false, val); return 0; + } case TOOL_PMU__EVENT_DURATION_TIME: /* * Pretend duration_time is only on the first CPU and thread, or @@ -506,9 +521,9 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_m= ap_idx, int thread) 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); =20 start_time =3D xyarray__entry(evsel->start_times, cpu_map_idx, thread); - fd =3D FD(evsel, cpu_map_idx, thread); lseek(fd, SEEK_SET, 0); if (evsel->pid_stat) { /* The event exists solely on 1 CPU. */ @@ -542,17 +557,9 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_= map_idx, int thread) if (adjust) { __u64 ticks_per_sec =3D sysconf(_SC_CLK_TCK); =20 - delta_start *=3D 1000000000 / ticks_per_sec; + delta_start *=3D 1e9 / ticks_per_sec; } - count->val =3D delta_start; - count->lost =3D 0; - /* - * The values of enabled and running must make a ratio of 100%. The - * exact values don't matter as long as they are non-zero to avoid - * issues with evsel__count_has_error. - */ - count->ena++; - count->run++; + perf_counts__update(count, old_count, /*raw=3D*/true, delta_start); return 0; } =20 --=20 2.51.2.1041.gc1ab5b90ca-goog