From nobody Sat Oct 11 11:49:40 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.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 1E388231839 for ; Wed, 11 Jun 2025 03:46:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749613599; cv=none; b=lXHNGmEJDD0MOkA6+fe66rH/01Re35rxamigRFRZctTM58xOz/uFw6QDCUg7j9D+rYIH8vhh37Q0Q7eZ6ry2SX4KDZxdPZ8Mf2nkBC+W8LSP9c4fysmhmt1UXKbXzDGCjuDkttHSnP9nQ2YFzHiyNGWiJ71zRljriRPymoLTmS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749613599; c=relaxed/simple; bh=nSK+MJS6SH9Co4DmYc4OGfrXdlBuLJyqUpUW8xhR6oY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=cXOkcdQCxZ0IAwK39YUAHqiaSUF07vp/wtCZk4NDjp8DCO1FTux9LjlEhyHE3oPclZmuzlru/+HSTBW4zUohB20QIICS5Sqkg95IWQ7qiovzKBACOt71/JC++shXV001ggMQzCoV2wEmQJtV33WdwScB5GUNG0XupYIWtNja+Pc= 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=sfBYxO1S; arc=none smtp.client-ip=209.85.216.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="sfBYxO1S" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-313b0a63c41so522952a91.0 for ; Tue, 10 Jun 2025 20:46:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749613597; x=1750218397; 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=xla2HbqGho1le28PO1Za1G5voxqgVcrF/8Iaq6OoFlM=; b=sfBYxO1S0i9yWEOBWMYf2NQefuk7V28oTYdpQ//85DTRklNo/ofg8FJ5MeF/p1Dl7F dOFDUMYfRzKwp6S432vKg+EoT8CVl2ZvxwtAGV9HWUAegsjVvKNdboCoKUiWVQfpk91a yIzgdGhjKHe7jzYU7XOmjIPKG3SPrYKf9zgn3lfps0lH0swgKPOryRHnGRnR68vxI15v QnrFftfxj3cKVb93GH+9MPSgwiWLJxmn8fGkVfPLMyRbky8KjSltQSY9X3jjNATlYxTS PNtCO50+0Zk/Qj4xENAmBY78Rr/QjHgRgtMHc++GR9cOXyrLLzflhbfvq10KLAtbQQIE Puyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749613597; x=1750218397; 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=xla2HbqGho1le28PO1Za1G5voxqgVcrF/8Iaq6OoFlM=; b=Ne9yrOVOd3AhHCxYmYoSlJpJpYfklsa/omfrXTrj/UXIIo0/9H2shpGDqdrrzWuE6X VEvDQt7v8A6WqTDFJk1/CrRsjoHi/kUuMpg+kqdRFTmVpv+rhumhBtfUbuRzWd7aC3cQ yw7MkeLmp5Mg9VtgnMRkapmUT899SXqCA1pO5ZxN4SegrVBZyc1+Eq/RXOcqy4OwuSpB LKP3J2iQTPsoDPZlwqi4aLX/T2R2NhC/yThU8rtCp31t+3jlVHjoJUrrb2KjJmfNJoiG Ru0CXIAyhYjIgp1MIeZwT4pG5suHT70Ms7tvsVg7J/0JZGX0LtSQngafcSeWGT538Y/w p0Qw== X-Forwarded-Encrypted: i=1; AJvYcCVrqieWDLP6FIeNKQiW55p5106Qbn5V8lxXvl2dw6vU0gFiyDAv7k/V0ACG5QA+RB4GEzVmJz8jZwgQd2Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yy7uXWEwVtmIDTCTqm1RjEVkdNoFSb+QucHHvt2i6m5Y/q1Vdyn p1UyUSVQWlfrt3kABImlbL/oG7qOYkhxc7v+6vQrRJiJg9naTW3o3FRMZ9bpMGEe0mmAPMQLkXK 6kCBfbDnHMQ== X-Google-Smtp-Source: AGHT+IEgJpBwPa7Io1URIRCdRhJG+f2C0WT1hbC7N2CjgpgchYeQi9bzRPdc/k1h3Noo/9vlG9a21RCVqqbh X-Received: from pjbsz14.prod.google.com ([2002:a17:90b:2d4e:b0:311:d264:6f5d]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3fc8:b0:311:df4b:4b81 with SMTP id 98e67ed59e1d1-313af208395mr2427428a91.25.1749613597492; Tue, 10 Jun 2025 20:46:37 -0700 (PDT) Date: Tue, 10 Jun 2025 20:45:27 -0700 In-Reply-To: <20250611034529.508842-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: <20250611034529.508842-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.rc0.642.g800a2b2222-goog Message-ID: <20250611034529.508842-14-irogers@google.com> Subject: [PATCH v1 13/15] perf tp_pmu: Add event APIs From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Xu Yang , John Garry , "Masami Hiramatsu (Google)" , Howard Chu , Weilin Wang , Thomas Richter , Andi Kleen , Tiezhu Yang , Gautam Menghani , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add event APIs for the tracepoint PMU allowing things like perf list to function using it. For perf list add the tracepoint format in the long description (shown with -v). Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 7 +++ tools/perf/util/tp_pmu.c | 114 +++++++++++++++++++++++++++++++++++++++ tools/perf/util/tp_pmu.h | 7 +++ 3 files changed, 128 insertions(+) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 4164c2278051..d4be0195ea74 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -23,6 +23,7 @@ #include "hwmon_pmu.h" #include "pmus.h" #include "tool_pmu.h" +#include "tp_pmu.h" #include #include #include "parse-events.h" @@ -1977,6 +1978,8 @@ bool perf_pmu__have_event(struct perf_pmu *pmu, const= char *name) return false; if (perf_pmu__is_tool(pmu) && tool_pmu__skip_event(name)) return false; + if (perf_pmu__is_tracepoint(pmu)) + return tp_pmu__have_event(pmu, name); if (perf_pmu__is_hwmon(pmu)) return hwmon_pmu__have_event(pmu, name); if (perf_pmu__find_alias(pmu, name, /*load=3D*/ true) !=3D NULL) @@ -1990,6 +1993,8 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu) { size_t nr; =20 + if (perf_pmu__is_tracepoint(pmu)) + return tp_pmu__num_events(pmu); if (perf_pmu__is_hwmon(pmu)) return hwmon_pmu__num_events(pmu); =20 @@ -2058,6 +2063,8 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, bo= ol skip_duplicate_pmus, struct hashmap_entry *entry; size_t bkt; =20 + if (perf_pmu__is_tracepoint(pmu)) + return tp_pmu__for_each_event(pmu, state, cb); if (perf_pmu__is_hwmon(pmu)) return hwmon_pmu__for_each_event(pmu, state, cb); =20 diff --git a/tools/perf/util/tp_pmu.c b/tools/perf/util/tp_pmu.c index fd83164f8763..9d68a1da17f6 100644 --- a/tools/perf/util/tp_pmu.c +++ b/tools/perf/util/tp_pmu.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) #include "tp_pmu.h" +#include "pmus.h" #include #include #include @@ -93,3 +94,116 @@ int tp_pmu__for_each_tp_sys(void *state, tp_sys_callbac= k cb) close(events_dir.dirfd); return ret; } + +bool perf_pmu__is_tracepoint(const struct perf_pmu *pmu) +{ + return pmu->type =3D=3D PERF_TYPE_TRACEPOINT; +} + +struct for_each_event_args { + void *state; + pmu_event_callback cb; + const struct perf_pmu *pmu; +}; + +static int for_each_event_cb(void *state, const char *sys_name, const char= *evt_name) +{ + struct for_each_event_args *args =3D state; + char name[2 * FILENAME_MAX + 2]; + /* 16 possible hex digits and 22 other characters and \0. */ + char encoding[16 + 22]; + char *format =3D NULL; + size_t format_size; + struct pmu_event_info info =3D { + .pmu =3D args->pmu, + .pmu_name =3D args->pmu->name, + .event_type_desc =3D "Tracepoint event", + }; + char *tp_dir =3D get_events_file(sys_name); + char path[PATH_MAX]; + int id, err; + + if (!tp_dir) + return -1; + + scnprintf(path, sizeof(path), "%s/%s/id", tp_dir, evt_name); + err =3D filename__read_int(path, &id); + if (err =3D=3D 0) { + snprintf(encoding, sizeof(encoding), "tracepoint/config=3D0x%x/", id); + info.encoding_desc =3D encoding; + } + + scnprintf(path, sizeof(path), "%s/%s/format", tp_dir, evt_name); + put_events_file(tp_dir); + err =3D filename__read_str(path, &format, &format_size); + if (err =3D=3D 0) { + info.long_desc =3D format; + for (size_t i =3D 0 ; i < format_size; i++) { + /* Swap tabs to spaces due to some rendering issues. */ + if (format[i] =3D=3D '\t') + format[i] =3D ' '; + } + } + snprintf(name, sizeof(name), "%s:%s", sys_name, evt_name); + info.name =3D name; + err =3D args->cb(args->state, &info); + free(format); + return err; +} + +static int for_each_event_sys_cb(void *state, const char *sys_name) +{ + return tp_pmu__for_each_tp_event(sys_name, state, for_each_event_cb); +} + +int tp_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_ca= llback cb) +{ + struct for_each_event_args args =3D { + .state =3D state, + .cb =3D cb, + .pmu =3D pmu, + }; + + return tp_pmu__for_each_tp_sys(&args, for_each_event_sys_cb); +} + +static int num_events_cb(void *state, const char *sys_name __maybe_unused, + const char *evt_name __maybe_unused) +{ + size_t *count =3D state; + + (*count)++; + return 0; +} + +static int num_events_sys_cb(void *state, const char *sys_name) +{ + return tp_pmu__for_each_tp_event(sys_name, state, num_events_cb); +} + +size_t tp_pmu__num_events(struct perf_pmu *pmu __maybe_unused) +{ + size_t count =3D 0; + + tp_pmu__for_each_tp_sys(&count, num_events_sys_cb); + return count; +} + +bool tp_pmu__have_event(struct perf_pmu *pmu __maybe_unused, const char *n= ame) +{ + char *dup_name, *colon; + int id; + + if (strchr(name, ':') =3D=3D NULL) + return false; + + dup_name =3D strdup(name); + if (!dup_name) + return false; + + colon =3D strchr(dup_name, ':'); + *colon =3D '\0'; + id =3D tp_pmu__id(dup_name, colon + 1); + free(dup_name); + return id >=3D 0; +} diff --git a/tools/perf/util/tp_pmu.h b/tools/perf/util/tp_pmu.h index 49537303bd73..30456bd6943d 100644 --- a/tools/perf/util/tp_pmu.h +++ b/tools/perf/util/tp_pmu.h @@ -2,6 +2,8 @@ #ifndef __TP_PMU_H #define __TP_PMU_H =20 +#include "pmu.h" + typedef int (*tp_sys_callback)(void *state, const char *sys_name); typedef int (*tp_event_callback)(void *state, const char *sys_name, const = char *evt_name); =20 @@ -9,4 +11,9 @@ int tp_pmu__id(const char *sys, const char *name); int tp_pmu__for_each_tp_event(const char *sys, void *state, tp_event_callb= ack cb); int tp_pmu__for_each_tp_sys(void *state, tp_sys_callback cb); =20 +bool perf_pmu__is_tracepoint(const struct perf_pmu *pmu); +int tp_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_ca= llback cb); +size_t tp_pmu__num_events(struct perf_pmu *pmu); +bool tp_pmu__have_event(struct perf_pmu *pmu, const char *name); + #endif /* __TP_PMU_H */ --=20 2.50.0.rc0.642.g800a2b2222-goog