From patchwork Wed Oct 2 03:20:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 13819287 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 34DFFCF3194 for ; Wed, 2 Oct 2024 03:34:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:To:From:Subject :References:Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4m9hxaxVGb2/DjCTUbZ0ZYfS7f+prrXxgLhCXKtfp/w=; b=v8LQ8L2slbYTyb2ceL2jtj0Bnq 5o7RGInY76auy9PoxGHSx7lIh8upx35/17k96n6TYvyroHxlIzKkZQzGqgDDGjH3Zn9vQmSOZm9Q1 iwrvkN0LfEWZF2GwKk3Ow04+jMO6j5mH/ub6vUwSfYzL0pZrTan2AXofOQGxkVNqMmrbZolgCSGY+ 9huC74dDq03s5aBc7oc3Ea3tM0gwtbHPpStL8Ai1O0V+Ewooi5ywnjqquXvntrvsDIlxYmyTVvbuy kzp06BYCiABhQLvl78iCU5KFqdfd0DGUEUHPv8G9B87CSYi5xFyR8qaxm6xXftJ25objUlma1SU3H ewAtkCkg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1svq7l-00000004hgP-1Cju; Wed, 02 Oct 2024 03:34:01 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1svpuy-00000004g8a-2knP for linux-arm-kernel@bombadil.infradead.org; Wed, 02 Oct 2024 03:20:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:To:From:Subject: References:Mime-Version:Message-Id:In-Reply-To:Date:Sender:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description; bh=4m9hxaxVGb2/DjCTUbZ0ZYfS7f+prrXxgLhCXKtfp/w=; b=WkHaEoWlT0lEfx/gp0F4HA95Go mae0FWxTe9482XDkzstZWcku1g4TLgYRJi5bf5uZD3AB54LSvxBVg0gEjV1yk09wt9IVNvlozlqqS 39omt/KOK2vJ9IBoMQkQzU3xDhahktcMx0vbhQnu9vtmkehIBYGNKPH/ILssmqfSY+UsOh8kQWXan tMaaeIFJnL6EpGp+8NBGddhcMTAXRbd1jTsgoAf0pC/ezTZIiJdPQJ+D7jCLs6w9aVrimPlOkULFn OeP+/75CZRUTr6OGSeWmdyQ+VpzacnQJBsBXmbRrSg1B3vDGRyoXi4zljxWtzSdGw2BkKEce3f7G5 dIt38DEQ==; Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1svpuv-00000003Q9u-2SR0 for linux-arm-kernel@lists.infradead.org; Wed, 02 Oct 2024 03:20:47 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-e25d62bfe12so9400026276.0 for ; Tue, 01 Oct 2024 20:20:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727839244; x=1728444044; darn=lists.infradead.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=4m9hxaxVGb2/DjCTUbZ0ZYfS7f+prrXxgLhCXKtfp/w=; b=xVaeGlX1uz0lGuVuKEvjjnxaz1HNX3OUV0TXphPRzAtKj9It68v7uLmOyIjpaKfVIP 36kAkXEgfT0f9OcWET2z0kCCTjeALZ5zG/4cf/iThunn/isqAFJCmlBfqCXqb8DTYK7u ZOsOyoH6vZ4CK/CYLevbcqYox/g+VLQ9tTG4Vb8VBkhDL6iazJNI4Yph+lR/qYG5E4Yy xQfJWjG03rUECsjh3pNgdOtVzh5Hm0gAXj7RXLmFRh7MZLekn/EkmX9DuzYTthYnL4Kv ViIqwnDlJONtHFmyWI95gMUnI6bQpmluRv9zJnjo49bv8KVnT7NLsXZ1wB2oEamfKKOR f2Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727839244; x=1728444044; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4m9hxaxVGb2/DjCTUbZ0ZYfS7f+prrXxgLhCXKtfp/w=; b=ejtHiI4yBlOls8SNMQbPLJEVCDi6vi/IN2q7uYyX2KTvmWF373bNecaEPRUVRt9sSC TBOutEYYcDs6I7A9SMYggxShBFY0XO/VYm+mHrtwoRO+kxrT3BWV3rETX9Gf9Ka0XUyZ UmlQaSYmcm5AVFkCoQZ008dmCxeBIWIJkpk/LZs/9WCirAYWc1qQKLa2QyPoaQiOOmL8 70a0ZO0d5jyz4DkHGJzdGEG3y6UhnBygZ3WVOPuvQuWDXiAN8ogzzJIoiDnAmAfRaunU Ecgkc2ewbS7xHoJpUR35F3r+pJfmiIkSdBhBmqn0tYmORemcrUTKHRA3egeaFEzLtthf OKuw== X-Forwarded-Encrypted: i=1; AJvYcCVwPEk+aAfEPYQFAzE1F5bKoZeyQvUvJqPTzs7m0JA/uBjkDKBlqUdeottzooYqHf8pNrIdFNRAJo5IQPFjUWLV@lists.infradead.org X-Gm-Message-State: AOJu0YwAMSo+zXcNOozehtzC/L/XZEZf3NWawPjWA7kbKZy7VFPBKdUr FTqQCd5rz5BReqqGFNNBt5U7R5iXs9Uk/+Z3Iwr4CLD3h5+2schX2ftvCJkhD3QmdYBv1JPIJDc 8bEwnTA== X-Google-Smtp-Source: AGHT+IGk8tH/JZalGU9H7+VDhh7I7rqK7Uv6KfFFeGynkaBAN4j3eCeW/GXJA5L2Www6c63c9f7yzaeMXMDz X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:ffa1:6d00:2acb:535f]) (user=irogers job=sendgmr) by 2002:a05:6902:10c7:b0:e24:a00a:518e with SMTP id 3f1490d57ef6-e263840d766mr9241276.7.1727839243484; Tue, 01 Oct 2024 20:20:43 -0700 (PDT) Date: Tue, 1 Oct 2024 20:20:12 -0700 In-Reply-To: <20241002032016.333748-1-irogers@google.com> Message-Id: <20241002032016.333748-10-irogers@google.com> Mime-Version: 1.0 References: <20241002032016.333748-1-irogers@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Subject: [PATCH v3 09/13] perf tool_pmu: Switch to standard pmu functions and json descriptions 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 , John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Weilin Wang , Ravi Bangoria , Sandipan Das , Xu Yang , Benjamin Gray , Athira Jajeev , Howard Chu , Veronika Molnarova , "Dr. David Alan Gilbert" , Oliver Upton , Changbin Du , "Steinar H. Gunderson" , Ze Gao , Dominique Martinet , " =?utf-8?q?Cl=C3=A9ment_Le_?= =?utf-8?q?Goffic?= " , Sun Haiyong , Junhao He , Tiezhu Yang , Yicong Yang , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241002_042045_750478_C65DE5C9 X-CRM114-Status: GOOD ( 23.27 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use the regular PMU approaches with tool json events to reduce the amount of special tool_pmu code - tool_pmu__config_terms and tool_pmu__for_each_event_cb are removed. Some functions remain, like tool_pmu__str_to_event, as conveniences to metricgroups. Add tool_pmu__skip_event/tool_pmu__num_skip_events to handle the case that tool json events shouldn't appear on certain architectures. This isn't done in jevents.py due to complexity in the empty-pmu-events.c and when all vendor json is built into the tool. Signed-off-by: Ian Rogers Acked-by: Namhyung Kim --- tools/perf/util/pmu.c | 26 ++++----- tools/perf/util/pmus.c | 4 +- tools/perf/util/tool_pmu.c | 109 +++++++++++-------------------------- tools/perf/util/tool_pmu.h | 12 +--- 4 files changed, 46 insertions(+), 105 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 9228650a62d2..0789758598c0 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1549,9 +1549,6 @@ int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, { bool zero = !!pmu->perf_event_attr_init_default; - if (perf_pmu__is_tool(pmu)) - return tool_pmu__config_terms(attr, head_terms, err); - /* Fake PMU doesn't have proper terms so nothing to configure in attr. */ if (perf_pmu__is_fake(pmu)) return 0; @@ -1664,8 +1661,8 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_ info->scale = 0.0; info->snapshot = false; - /* Tool/fake PMU doesn't rewrite terms. */ - if (perf_pmu__is_tool(pmu) || perf_pmu__is_fake(pmu)) + /* Fake PMU doesn't rewrite terms. */ + if (perf_pmu__is_fake(pmu)) goto out; list_for_each_entry_safe(term, h, &head_terms->terms, list) { @@ -1835,8 +1832,8 @@ bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name) { if (!name) return false; - if (perf_pmu__is_tool(pmu)) - return tool_pmu__str_to_event(name) != TOOL_PMU__EVENT_NONE; + if (perf_pmu__is_tool(pmu) && tool_pmu__skip_event(name)) + return false; if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL) return true; if (pmu->cpu_aliases_added || !pmu->events_table) @@ -1848,9 +1845,6 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu) { size_t nr; - if (perf_pmu__is_tool(pmu)) - return tool_pmu__num_events(); - pmu_aliases_parse(pmu); nr = pmu->sysfs_aliases + pmu->sys_json_aliases; @@ -1861,6 +1855,9 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu) else assert(pmu->cpu_json_aliases == 0); + if (perf_pmu__is_tool(pmu)) + nr -= tool_pmu__num_skip_events(); + return pmu->selectable ? nr + 1 : nr; } @@ -1911,15 +1908,15 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus, int ret = 0; struct strbuf sb; - if (perf_pmu__is_tool(pmu)) - return tool_pmu__for_each_event_cb(pmu, state, cb); - strbuf_init(&sb, /*hint=*/ 0); pmu_aliases_parse(pmu); pmu_add_cpu_aliases(pmu); list_for_each_entry(event, &pmu->aliases, list) { size_t buf_used, pmu_name_len; + if (perf_pmu__is_tool(pmu) && tool_pmu__skip_event(event->name)) + continue; + info.pmu_name = event->pmu_name ?: pmu->name; pmu_name_len = pmu_deduped_name_len(pmu, info.pmu_name, skip_duplicate_pmus); @@ -2325,9 +2322,6 @@ const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config) if (!pmu) return NULL; - if (perf_pmu__is_tool(pmu)) - return tool_pmu__event_to_str(config); - pmu_aliases_parse(pmu); pmu_add_cpu_aliases(pmu); list_for_each_entry(event, &pmu->aliases, list) { diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index 6e657c44de58..107de86c2637 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -440,6 +440,7 @@ static int perf_pmus__print_pmu_events__callback(void *vstate, pr_err("Unexpected event %s/%s/\n", info->pmu->name, info->name); return 1; } + assert(info->pmu != NULL || info->name != NULL); s = &state->aliases[state->index]; s->pmu = info->pmu; #define COPY_STR(str) s->str = info->str ? strdup(info->str) : NULL @@ -590,9 +591,6 @@ void perf_pmus__print_raw_pmu_events(const struct print_callbacks *print_cb, voi int len = pmu_name_len_no_suffix(pmu->name); const char *desc = "(see 'man perf-list' or 'man perf-record' on how to encode it)"; - if (perf_pmu__is_tool(pmu)) - continue; - if (!pmu->is_core) desc = NULL; diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c index cfdc09dece1e..ea9d50f02520 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -33,101 +33,54 @@ static const char *const tool_pmu__event_names[TOOL_PMU__EVENT_MAX] = { "system_tsc_freq", }; - -const char *tool_pmu__event_to_str(enum tool_pmu_event ev) -{ - if (ev > TOOL_PMU__EVENT_NONE && ev < TOOL_PMU__EVENT_MAX) - return tool_pmu__event_names[ev]; - - return NULL; -} - -enum tool_pmu_event tool_pmu__str_to_event(const char *str) +bool tool_pmu__skip_event(const char *name __maybe_unused) { - int i; - - tool_pmu__for_each_event(i) { - if (!strcasecmp(str, tool_pmu__event_names[i])) { #if !defined(__aarch64__) - /* The slots event should only appear on arm64. */ - if (i == TOOL_PMU__EVENT_SLOTS) - return TOOL_PMU__EVENT_NONE; + /* The slots event should only appear on arm64. */ + if (strcasecmp(name, "slots") == 0) + return true; #endif - return i; - } - } - return TOOL_PMU__EVENT_NONE; +#if !defined(__i386__) && !defined(__x86_64__) + /* The system_tsc_freq event should only appear on x86. */ + if (strcasecmp(name, "system_tsc_freq") == 0) + return true; +#endif + return false; } -static int tool_pmu__config_term(struct perf_event_attr *attr, - struct parse_events_term *term, - struct parse_events_error *err) +int tool_pmu__num_skip_events(void) { - if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER) { - enum tool_pmu_event ev = tool_pmu__str_to_event(term->config); + int num = 0; - if (ev == TOOL_PMU__EVENT_NONE) - goto err_out; - - attr->config = ev; - return 0; - } -err_out: - if (err) { - char *err_str; - - parse_events_error__handle(err, term->err_val, - asprintf(&err_str, - "unexpected tool event term (%s) %s", - parse_events__term_type_str(term->type_term), - term->config) < 0 - ? strdup("unexpected tool event term") - : err_str, - NULL); - } - return -EINVAL; +#if !defined(__aarch64__) + num++; +#endif +#if !defined(__i386__) && !defined(__x86_64__) + num++; +#endif + return num; } -int tool_pmu__config_terms(struct perf_event_attr *attr, - struct parse_events_terms *terms, - struct parse_events_error *err) +const char *tool_pmu__event_to_str(enum tool_pmu_event ev) { - struct parse_events_term *term; - - list_for_each_entry(term, &terms->terms, list) { - if (tool_pmu__config_term(attr, term, err)) - return -EINVAL; - } - - return 0; + if (ev > TOOL_PMU__EVENT_NONE && ev < TOOL_PMU__EVENT_MAX) + return tool_pmu__event_names[ev]; + return NULL; } -int tool_pmu__for_each_event_cb(struct perf_pmu *pmu, void *state, pmu_event_callback cb) +enum tool_pmu_event tool_pmu__str_to_event(const char *str) { - struct pmu_event_info info = { - .pmu = pmu, - .event_type_desc = "Tool event", - }; int i; + if (tool_pmu__skip_event(str)) + return TOOL_PMU__EVENT_NONE; + tool_pmu__for_each_event(i) { - int ret; - - info.name = tool_pmu__event_to_str(i); - info.alias = NULL; - info.scale_unit = NULL; - info.desc = NULL; - info.long_desc = NULL; - info.encoding_desc = NULL; - info.topic = NULL; - info.pmu_name = pmu->name; - info.deprecated = false; - ret = cb(state, &info); - if (ret) - return ret; + if (!strcasecmp(str, tool_pmu__event_names[i])) + return i; } - return 0; + return TOOL_PMU__EVENT_NONE; } bool perf_pmu__is_tool(const struct perf_pmu *pmu) @@ -546,6 +499,8 @@ struct perf_pmu *perf_pmus__tool_pmu(void) .caps = LIST_HEAD_INIT(tool.caps), .format = LIST_HEAD_INIT(tool.format), }; + if (!tool.events_table) + tool.events_table = find_core_events_table("common", "common"); return &tool; } diff --git a/tools/perf/util/tool_pmu.h b/tools/perf/util/tool_pmu.h index ecdf316525bb..a60184859080 100644 --- a/tools/perf/util/tool_pmu.h +++ b/tools/perf/util/tool_pmu.h @@ -29,17 +29,11 @@ enum tool_pmu_event { #define tool_pmu__for_each_event(ev) \ for ((ev) = TOOL_PMU__EVENT_DURATION_TIME; (ev) < TOOL_PMU__EVENT_MAX; ev++) -static inline size_t tool_pmu__num_events(void) -{ - return TOOL_PMU__EVENT_MAX - 1; -} - const char *tool_pmu__event_to_str(enum tool_pmu_event ev); enum tool_pmu_event tool_pmu__str_to_event(const char *str); -int tool_pmu__config_terms(struct perf_event_attr *attr, - struct parse_events_terms *terms, - struct parse_events_error *err); -int tool_pmu__for_each_event_cb(struct perf_pmu *pmu, void *state, pmu_event_callback cb); +bool tool_pmu__skip_event(const char *name); +int tool_pmu__num_skip_events(void); + bool tool_pmu__read_event(enum tool_pmu_event ev, u64 *result); u64 tool_pmu__cpu_slots_per_cycle(void);