From nobody Thu Nov 28 13:47:40 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 CA8D239FD6 for ; Wed, 2 Oct 2024 03:20:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727839246; cv=none; b=UAGJtMSXPZTKDd3HUZXoUwiEWARRHLcgszRX+fWcxza7ivp9kjOYe9rSS0PE36FhBdLGq7xDohcYCOBLHgUiItwaCpAx3C7A1vuai3zW/2GOuz1lUBOJnH9ywKvla0BxABo4ca+fTaFOV9DY+D+CO/aL2RDCBzuqLOyddtyNkYg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727839246; c=relaxed/simple; bh=U1ihmZxImzymMR83S4d4i4eQ/Rj8mWMZst8NNO7Uu6c=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=pkIuaHlELAteRg0BuW6Kjf4Xh3iFSd1UZ7EMka09+MRU2Qrf3mKxqIXBvRVWxTMW1dNS9gjGDGNbtRY47xaOQxCziHyFpwObjXUbM7dTpjNbWZ4g2CUo4eQLhsnnNLfpnb1YZ+TYgvjwZWXJ+kTIWzZG55w4fE+23q6/C3AQbuQ= 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=nDpkUB5x; arc=none smtp.client-ip=209.85.219.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="nDpkUB5x" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e24b43799e9so6268939276.2 for ; Tue, 01 Oct 2024 20:20:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727839244; x=1728444044; darn=vger.kernel.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=nDpkUB5x2gLLfkSVi0lUaNtsbsh+JuKeJa/Zr2WIDD6u75bX2rGDPsXas7OnvczQId eHLcLa376FeUyng+NxzE6o9Lctso6pf8GrjscHCrpelqDtvVOaBJpbqGRbzNIe9QY8Tj dleIHsTFXcNzdvMnm3BgVFTnx3ifxk8/DEp8mduS+f/0CPhxEPulewzclP9K2dUlB4sc 1IzqE6ROT4vbwy3t+e/uHv7PP4y4vGhCFLSPyv2wCmDpmrInUP3lQ93seg8Y8M9n10uN lvX/Ua6nSOkUuVjuB9/UGg93yzwFq4mG7/gGZAdW/ut+jMS+7MaeTs09yDMOQwJ+MKZ0 wOcw== 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=j64CMxnqMcagr7892NvsSogjUwQW5ohvCyQyEHTjzPDLCWnJpLQTm8QSVL31nIY7wd TJiftD3XoiQwIcET0K0KHIL2D+MRHGVoFzBXApcQeUnkgl87pB0Z299dgzHEp5sWaRsk VGeX9idG6lIKWzcFJnag3B/pjPHbH0H5zVcI3si5II3buIaHWEttC6TLZ/zLHp3paAsd RoeqDJFOdYIKzuFXL4WX4hNyshoL7gMi+XG4WceXN1tTGQYzvLcsCCqqQdU7GKRph5T9 liW2R3SLJKJJCJ9x/LtM46DPPOwfD5ZMvjRLy/cFjQVMN6dfnveTYupj7pu61vbYSMrU vDlg== X-Forwarded-Encrypted: i=1; AJvYcCUzFPLlo3x5hFmMqyx3F7aMivWG6EpgCIw0aPASWZPWnIYzEZAseWErZVTLMzJ5xZ016LgH9lAe1O+S1n8=@vger.kernel.org X-Gm-Message-State: AOJu0YzB8lm9QipeZxdp+tqrAvM/U2lG1qy3qNXVNZ45FR/TPkjTVAX5 zMmdDL523s0kStaV1TI5gtZkGSSz1TK6A/E30mUdlyB8CcPqRHPMK8Pqr+hVvSNIPlsOtygbTEF 3SVpwRQ== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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=20Le=20Goffic?=" , 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 per= f_event_attr *attr, { bool zero =3D !!pmu->perf_event_attr_init_default; =20 - 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, struc= t parse_events_terms *head_ info->scale =3D 0.0; info->snapshot =3D false; =20 - /* 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; =20 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) !=3D 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=3D*/ true) !=3D 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; =20 - if (perf_pmu__is_tool(pmu)) - return tool_pmu__num_events(); - pmu_aliases_parse(pmu); nr =3D pmu->sysfs_aliases + pmu->sys_json_aliases; =20 @@ -1861,6 +1855,9 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu) else assert(pmu->cpu_json_aliases =3D=3D 0); =20 + if (perf_pmu__is_tool(pmu)) + nr -=3D tool_pmu__num_skip_events(); + return pmu->selectable ? nr + 1 : nr; } =20 @@ -1911,15 +1908,15 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, = bool skip_duplicate_pmus, int ret =3D 0; struct strbuf sb; =20 - if (perf_pmu__is_tool(pmu)) - return tool_pmu__for_each_event_cb(pmu, state, cb); - strbuf_init(&sb, /*hint=3D*/ 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; =20 + if (perf_pmu__is_tool(pmu) && tool_pmu__skip_event(event->name)) + continue; + info.pmu_name =3D event->pmu_name ?: pmu->name; pmu_name_len =3D pmu_deduped_name_len(pmu, info.pmu_name, skip_duplicate_pmus); @@ -2325,9 +2322,6 @@ const char *perf_pmu__name_from_config(struct perf_pm= u *pmu, u64 config) if (!pmu) return NULL; =20 - 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 !=3D NULL || info->name !=3D NULL); s =3D &state->aliases[state->index]; s->pmu =3D info->pmu; #define COPY_STR(str) s->str =3D 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 =3D pmu_name_len_no_suffix(pmu->name); const char *desc =3D "(see 'man perf-list' or 'man perf-record' on how t= o encode it)"; =20 - if (perf_pmu__is_tool(pmu)) - continue; - if (!pmu->is_core) desc =3D NULL; =20 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_PM= U__EVENT_MAX] =3D { "system_tsc_freq", }; =20 - -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 =3D=3D TOOL_PMU__EVENT_SLOTS) - return TOOL_PMU__EVENT_NONE; + /* The slots event should only appear on arm64. */ + if (strcasecmp(name, "slots") =3D=3D 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") =3D=3D 0) + return true; +#endif + return false; } =20 -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 =3D=3D PARSE_EVENTS__TERM_TYPE_USER) { - enum tool_pmu_event ev =3D tool_pmu__str_to_event(term->config); + int num =3D 0; =20 - if (ev =3D=3D TOOL_PMU__EVENT_NONE) - goto err_out; - - attr->config =3D 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; } =20 -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]; =20 + return NULL; } =20 -int tool_pmu__for_each_event_cb(struct perf_pmu *pmu, void *state, pmu_eve= nt_callback cb) +enum tool_pmu_event tool_pmu__str_to_event(const char *str) { - struct pmu_event_info info =3D { - .pmu =3D pmu, - .event_type_desc =3D "Tool event", - }; int i; =20 + if (tool_pmu__skip_event(str)) + return TOOL_PMU__EVENT_NONE; + tool_pmu__for_each_event(i) { - int ret; - - info.name =3D tool_pmu__event_to_str(i); - info.alias =3D NULL; - info.scale_unit =3D NULL; - info.desc =3D NULL; - info.long_desc =3D NULL; - info.encoding_desc =3D NULL; - info.topic =3D NULL; - info.pmu_name =3D pmu->name; - info.deprecated =3D false; - ret =3D cb(state, &info); - if (ret) - return ret; + if (!strcasecmp(str, tool_pmu__event_names[i])) + return i; } - return 0; + return TOOL_PMU__EVENT_NONE; } =20 bool perf_pmu__is_tool(const struct perf_pmu *pmu) @@ -546,6 +499,8 @@ struct perf_pmu *perf_pmus__tool_pmu(void) .caps =3D LIST_HEAD_INIT(tool.caps), .format =3D LIST_HEAD_INIT(tool.format), }; + if (!tool.events_table) + tool.events_table =3D find_core_events_table("common", "common"); =20 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) =3D TOOL_PMU__EVENT_DURATION_TIME; (ev) < TOOL_PMU__EVENT_MAX; = ev++) =20 -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_eve= nt_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); =20 u64 tool_pmu__cpu_slots_per_cycle(void); --=20 2.46.1.824.gd892dcdcdd-goog