From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796334759; b=XgezU+1VyAWfBrOtDHqMnDUBVqIb+/X++CEzoVJMVWILYz9XmDl7uCGa5Q1joeJx+4Zo Jlp/s7DKhha2O6zfLs7YpzR3zx8u+HcXpJVNK99Lejo91QZXtAVPPfMl59lgrHF4SXcTh WJ+XqadcOCY69i5lMvsKi9Dp5OWFYTTVGul9nD1/f4fFCJXFY7M8T18uWR7QmjMOaZOEE iXhXON7bEGUSxC9vVYi5xl7kzvOzBk262gC4BXdBzMDOVbP2jDbK8Zts/NIzK0I8pDo2W BbxZe1UCOXo5Ws3y71mTnOO5EExJHtXM9sk85I7ZRYC+2YGO6Hf6oEFSH918DZvjRhA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796334759; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=oNlId/V8sNpGgvDnDR0bMFHwFv6YFFwR+bBzonr4I+w=; b=OAGtitX6wgdLYRSlNRk7HJPEjXNkdzG0EO2zpuPskBAyeYrG52WerxaT2BOAe8B6RyXd ZLNa7vXuKS+VRS4gO0LSMMlYmTMijD40Jxn6UaCmDzE1lid+iJTAlWpm4l/O87EnTg1Ay EhVEZ5X9vkUulQ5nnuYoyg3ix6BUTHJY9xoYxa/QH2lkrQqAatSZbIGkrglri/sKwgdCI K95EkPy4J4420GkrCPFRqEonl9V0zKHUGndIASdYr1bmarrI0fd2qXcJVcKU/v4WmuJD8 D7ZSE7Tw5+GKcoYs8ybiseQopH3WZmnShyzjVc+aXKyIsp2HXFV6xxrw1whZj9DK7hQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=TTG9GM+I; arc=none smtp.remote-ip=209.85.160.69 Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B60E0606A6A0 for ; Thu, 3 Dec 2026 21:52:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B60E0606A6A0 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.a=rsa-sha256 header.s=20260707 header.b=TTG9GM+I Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-47cfa971c9eso2157679fac.0 for ; Thu, 03 Dec 2026 13:52:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796334757; x=1796939557; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=oNlId/V8sNpGgvDnDR0bMFHwFv6YFFwR+bBzonr4I+w=; b=TTG9GM+IFwJogosxyA6/6NF1x1H0jzg2IIzpG2Xj2wGOqW0+1DziMv58vIZ5+C49P2 pidGy7a5SAYfOyoWhFs/ByN7tAWtufFeiYnMcUwcmy/20DuCtGAzPgo3swTm4uk1hYfb Xp1iLFnz8fvF1KXJNw1eTxDEmfMu0WHCUBAnskQrJqxUginSeXrb3TUua78c9UoRIpL2 AbzwlEi3lzS0brYZqHJCpNfuZ576JUbs3hqin7Ork8AABMWefRfTUJKNDUyndHr4GjEk /W5PV3ycqC0qsGVvvzhz7WBcsZarakTVGMEjWimWr3i7D2MwSua0bYgHvVZUksYA0Jal i3EA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796334757; x=1796939557; h=content-type: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:content-type; bh=oNlId/V8sNpGgvDnDR0bMFHwFv6YFFwR+bBzonr4I+w=; b=kZxYKO0xgpSq9LqMtA+bm3W4WMHAE5HBsKy6QTRPWPBuBwOrO6NUGQYxVe8to7SA7D r0CCYIftPdOlXju/UHIwbHnL6P3thAmvyyixyN1wa8OYJRuo31B1rAb18MF6m1LZKKak jsLfsJ4aw/HIGTfh5Lz304KSJWFMy3LKWTag0us2lZQ5dNHFYV5PtSWSSbFHs/SIQSbI pLIYfpfphwO8hsk8AhM+VwTZi6jja6ymQ3YaQEMObpiAHykji5HMlQjElBEgBkLoZNI+ wGVCkkKKnyRpSrOX73fwX5OE6jFGpxqgWUw+fXWjOCj4f0kg+8GSP/iAVLulHdDfrz2i RgWw== X-Forwarded-Encrypted: i=1; AL+hErH/hsTrXZbylb7/nBTlzx+6kJ2kuhB9JvtCY3aWXQhhATsY8FHIXtG4hB1Z4NvQnQS3fVneMFBlrfWC1nw=@vger.kernel.org X-Gm-Message-State: APuU4Z/6+NsjmFbPgyIyUNpNQDgTaAfU+3AvFVysxZErqU2szz0Z+A7t 7nCUzC33CpVl/gzMbbFi9QvRUrqt+vA6BpCvlmgECdGlKxXYOoncJrmO55WrAYS/jrSSCWtLjxi dfoH/6R0S5w== X-Received: from oacpx19-n1.prod.google.com ([2002:a05:6871:2913:10b0:4ad:bdb3:7d44]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:a40c:b0:457:f4e1:a1e3 with SMTP id 586e51a60fabf-4e0dfc24766mr2609544fac.7.1796334757507; Thu, 03 Dec 2026 13:52:37 -0800 (PST) Date: Thu, 3 Dec 2026 13:52:13 -0800 In-Reply-To: <20261203215222.1392035-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261203215222.1392035-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261203215222.1392035-5-irogers@google.com> Subject: [PATCH v2 04/13] perf stat: Give non-perf_event_open events unique IDs for stat record From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Athira Rajeev , TAKANO Mitsuhiro , Hang Zhengyang , Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" store_evsel_ids() skips tool, hwmon, DRM and retire latency events as there is no perf event file descriptor to read an ID from. Their perf_sample_id entries are left with an ID of 0, so the stat events written by perf stat record all have an ID of 0. When reading the file, evlist__id2evsel() maps an ID of 0 to the first evsel, so the counts of all such events are attributed to the first evsel and the other events are reported as not counted. For example: $ perf stat record -e duration_time,num_cpus,task-clock true $ perf stat report 28 duration_time num_cpus 0.84 msec task-clock Give each CPU and thread of these events a unique synthetic ID. Kernel IDs count up from 1, so the synthetic IDs count down from ~0ULL - 1 (avoiding the ~0ULL unset sample.id sentinel and 0) to be as far from them as possible; reaching them would need ~2^63 perf events to have been created. As a further guard, skip any ID that is already in use by the evlist. Fixes: d53b4996584b ("perf evsel: Skip store_evsel_ids for non-perf-event PMUs") Signed-off-by: Ian Rogers --- tools/perf/util/evsel.c | 43 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index dbc217f5011e..708e6cbc05ea 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -4810,15 +4810,52 @@ struct perf_env *evsel__env(struct evsel *evsel) return session ? perf_session__env(session) : NULL; } +/* + * Get an ID for an event not opened with perf_event_open, such as a tool PMU + * event. Kernel assigned IDs count up from 1, so count down from ~0ULL - 1 + * (~0ULL is the unset sample.id sentinel) to keep as far away from them as + * possible. Reaching the kernel's IDs would need ~2^63 perf events to have + * been created, but also skip 0, ~0ULL, and any ID already in use by the + * evlist. + */ +static u64 evlist__next_synthetic_id(struct evlist *evlist) +{ + static u64 next_synthetic_id = ~0ULL - 1; + u64 id; + + do { + id = next_synthetic_id--; + } while (id == 0 || id == ~0ULL || evlist__id2sid(evlist, id)); + + return id; +} + static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist) { int cpu_map_idx, thread; - if (evsel__is_retire_lat(evsel)) - return 0; + if (evsel__is_retire_lat(evsel) || + perf_pmu__kind(evsel->pmu) != PERF_PMU_KIND_PE) { + struct xyarray *sample_id = evsel->core.sample_id; - if (perf_pmu__kind(evsel->pmu) != PERF_PMU_KIND_PE) + if (!sample_id) + return 0; + + /* + * There's no perf event to read an ID from. Give each + * CPU/thread a unique synthetic ID, otherwise all such events + * have ID 0 and stat events written by perf stat record are + * attributed to the first evsel. + */ + for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(sample_id); cpu_map_idx++) { + for (thread = 0; thread < xyarray__max_y(sample_id); thread++) { + perf_evlist__id_add(evlist__core(evlist), &evsel->core, + cpu_map_idx, thread, + evlist__next_synthetic_id(evlist)); + } + } return 0; + } for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->core.fd); cpu_map_idx++) { for (thread = 0; thread < xyarray__max_y(evsel->core.fd); -- 2.56.1.915.g69d5ec7298-goog