From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 2D89D2FE302 for ; Thu, 10 Jul 2025 20:37:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752179833; cv=none; b=N8RSNhILhAcOl85Ropl8rAn4PAO1RWi/zTxhefLKl5fMfAwb3pS+RbNCgDrTJ7E2oCIkd96UNfxztch/i5SaRO9DdkTMBMjmROkPv8CcX65+z5fb5SX/TQznHANWSuyrrGQhLzXqd2+3TYH/JPy/+UdpAkQuIt7+7wWGATa60YE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752179833; c=relaxed/simple; bh=RZ/4wZ7BLS3RqD09YBHOjY7W3qZV5Jmx+MPGWahCs1s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=pLORgUEqSmdNM8i2ieqyDwn4nlcSXd+Iv1NhsVIP/ktwDmUigMZaF5k3Mttfq4RFj6+AmfMhMD8dWtwaLXwjqqwA6y4cqGRrbaQq9A2f4YbMdlXW5WL0OP5W1WpTJoI0GWgb5peS+qSaNRFpZy3zX32fL2df31mwvJm+rAtwW04= 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=WrkQpsAz; arc=none smtp.client-ip=209.85.210.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="WrkQpsAz" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-74913a4f606so1145679b3a.1 for ; Thu, 10 Jul 2025 13:37:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752179831; x=1752784631; 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=O5/KtDdH9KZtizmnT+G+DMzo0YjRxsCsauFrBz7enXg=; b=WrkQpsAzSpcoKi5ZpzVOvxd/rsH0OzbtagxN1dDbJaLxe1PLvfJ0ceogoJ+QtzC0Ye TnjKIuFf5jO6m7IYFNmaAsdvVKkbtWsZ/pcO0x46lLk/CC8Z2652q81CrBXSL086Ng12 HOhYttUFpo5vgh73k13+0iqBcjrCwoVk2u4VUHFiZorYfFwZUR9F0vGSPPFyriGou/P+ H4Ki5GrF6ntYlD6ZFifPsoztcqCnzi0zHBwUSstolPDhFVq87IAOAdOM5IFqmSh1b2O8 zKOMHLPl17ANy8rgFPgIpDZ7tk5Tu5JuDMwc6i+qwn6Qw0i+aEnLa/yy5W9QQeknCm5t cA4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752179831; x=1752784631; 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=O5/KtDdH9KZtizmnT+G+DMzo0YjRxsCsauFrBz7enXg=; b=tQ0FZhnve9S5mXj/wmudoL4HRmj7Qg+wWT2yussMV/FJJScc1N+qMOFhPvVCB1y2Os 3Okz6sloWtlm+SwOSPQ516W0hKi55sDb8avMsOlCZYY5qkzvFRAuf89RYncNZp2ohLum GWRYRHf838EwdpAsegnsyQ9Z1wm/kMCDPmdj7zSGTzEPcdXcxc1H+TS0W3e+PI2UqUqV VVgn8xqyPDdswP8yGa1LY+7j/IN2QuHX7GOYlLuuUxyv0JhYMWKCofiJYR7UCThooc5b 7BLB26aWfKx9XY7HD99LHs+7T3BgLucalEM4KkbfEebNt+8qKQw5vP/9nvt6SMOwpqJZ +vnQ== X-Forwarded-Encrypted: i=1; AJvYcCWU++2Jca1mHqKqFsLm3lXxmQxfuNa6sbzD+jnnLEV9ZbXBwYmFt1NAUvUIyGydyQNIbe6x8CRLP/updjQ=@vger.kernel.org X-Gm-Message-State: AOJu0Ywp/3Xr3ifiJ7+cVwLuJR09CszqpZE8lD4rwLlhZeRib6SJHLDC WqR7+e2OJDKB9SwIDGTzPOu1TiCA60Ll4LPa/b6jU/To7gCAre804ka8WuK5L+r0yuw/O+rZe2g JkdGGu+aziQ== X-Google-Smtp-Source: AGHT+IFU6LBITwCARUNdRvCeSrmREtr/H02L8Gm8eE9WG/JSJHeF+Pqj34ByZLh1hY3vIXkaTWns9raxggpd X-Received: from pgbcz3.prod.google.com ([2002:a05:6a02:2303:b0:b2e:beba:356]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:998d:b0:230:930b:3a4d with SMTP id adf61e73a8af0-2311f431f37mr1167603637.28.1752179831506; Thu, 10 Jul 2025 13:37:11 -0700 (PDT) Date: Thu, 10 Jul 2025 13:36:46 -0700 In-Reply-To: <20250710203656.1963075-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: <20250710203656.1963075-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250710203656.1963075-5-irogers@google.com> Subject: [PATCH v5 04/14] 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 , "Masami Hiramatsu (Google)" , Collin Funk , Howard Chu , Weilin Wang , Andi Kleen , "Dr. David Alan Gilbert" , Thomas Richter , Tiezhu Yang , Gautam Menghani , Thomas Falcon , Chun-Tse Shao , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org 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 b09b2ea2407a..dc05233e8232 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -24,6 +24,7 @@ #include "hwmon_pmu.h" #include "pmus.h" #include "tool_pmu.h" +#include "tp_pmu.h" #include #include #include "parse-events.h" @@ -1983,6 +1984,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__is_drm(pmu)) @@ -1998,6 +2001,8 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu) { size_t nr; + if (perf_pmu__is_tracepoint(pmu)) + return tp_pmu__num_events(pmu); if (perf_pmu__is_hwmon(pmu)) return hwmon_pmu__num_events(pmu); if (perf_pmu__is_drm(pmu)) @@ -2068,6 +2073,8 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus, struct hashmap_entry *entry; size_t bkt; + 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); if (perf_pmu__is_drm(pmu)) 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_callback cb) close(events_dir.dirfd); return ret; } + +bool perf_pmu__is_tracepoint(const struct perf_pmu *pmu) +{ + return pmu->type == 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 = state; + char name[2 * FILENAME_MAX + 2]; + /* 16 possible hex digits and 22 other characters and \0. */ + char encoding[16 + 22]; + char *format = NULL; + size_t format_size; + struct pmu_event_info info = { + .pmu = args->pmu, + .pmu_name = args->pmu->name, + .event_type_desc = "Tracepoint event", + }; + char *tp_dir = 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 = filename__read_int(path, &id); + if (err == 0) { + snprintf(encoding, sizeof(encoding), "tracepoint/config=0x%x/", id); + info.encoding_desc = encoding; + } + + scnprintf(path, sizeof(path), "%s/%s/format", tp_dir, evt_name); + put_events_file(tp_dir); + err = filename__read_str(path, &format, &format_size); + if (err == 0) { + info.long_desc = format; + for (size_t i = 0 ; i < format_size; i++) { + /* Swap tabs to spaces due to some rendering issues. */ + if (format[i] == '\t') + format[i] = ' '; + } + } + snprintf(name, sizeof(name), "%s:%s", sys_name, evt_name); + info.name = name; + err = 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_callback cb) +{ + struct for_each_event_args args = { + .state = state, + .cb = cb, + .pmu = 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 = 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 = 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 *name) +{ + char *dup_name, *colon; + int id; + + if (strchr(name, ':') == NULL) + return false; + + dup_name = strdup(name); + if (!dup_name) + return false; + + colon = strchr(dup_name, ':'); + *colon = '\0'; + id = tp_pmu__id(dup_name, colon + 1); + free(dup_name); + return id >= 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 +#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); @@ -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_callback cb); int tp_pmu__for_each_tp_sys(void *state, tp_sys_callback cb); +bool perf_pmu__is_tracepoint(const struct perf_pmu *pmu); +int tp_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callback 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 */ -- 2.50.0.727.gbf7dc18ff4-goog