public inbox for linux-perf-users@vger.kernel.org
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: tmricht@linux.ibm.com
Cc: irogers@google.com, acme@kernel.org, agordeev@linux.ibm.com,
	 gor@linux.ibm.com, hca@linux.ibm.com, japo@linux.ibm.com,
	 linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	 linux-s390@vger.kernel.org, namhyung@kernel.org,
	sumanthk@linux.ibm.com
Subject: [PATCH v5 1/5] perf evsel: Improve falling back from cycles
Date: Mon, 16 Mar 2026 22:53:30 -0700	[thread overview]
Message-ID: <20260317055334.760347-2-irogers@google.com> (raw)
In-Reply-To: <20260317055334.760347-1-irogers@google.com>

Switch to using evsel__match rather than comparing perf_event_attr
values, this is robust on hybrid architectures.
Ensure evsel->pmu matches the evsel->core.attr.
Remove exclude bits that get set in other fallback attempts when
switching the event.
Log the event name with modifiers when switching the event on fallback.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/evsel.c | 45 ++++++++++++++++++++++++++++-------------
 tools/perf/util/evsel.h |  2 ++
 2 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index f59228c1a39e..bd14d9bbc91f 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -3785,25 +3785,42 @@ bool evsel__fallback(struct evsel *evsel, struct target *target, int err,
 {
 	int paranoid;
 
-	if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
-	    evsel->core.attr.type   == PERF_TYPE_HARDWARE &&
-	    evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) {
+	if ((err == ENODEV || err == ENOENT || err == ENXIO) &&
+	    evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
 		/*
-		 * If it's cycles then fall back to hrtimer based cpu-clock sw
-		 * counter, which is always available even if no PMU support.
-		 *
-		 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
-		 * b0a873e).
+		 * If it's the legacy hardware cycles event fails then fall back
+		 * to hrtimer based cpu-clock sw counter, which is always
+		 * available even if no PMU support. PPC returned ENXIO rather
+		 * than ENODEV or ENOENT until 2.6.37.
 		 */
-		evsel->core.attr.type   = PERF_TYPE_SOFTWARE;
+		evsel->pmu = perf_pmus__find_by_type(PERF_TYPE_SOFTWARE);
+		assert(evsel->pmu); /* software is a "well-known" and can't fail PMU type. */
+
+		/* Configure the event. */
+		evsel->core.attr.type = PERF_TYPE_SOFTWARE;
 		evsel->core.attr.config = target__has_cpu(target)
 			? PERF_COUNT_SW_CPU_CLOCK
 			: PERF_COUNT_SW_TASK_CLOCK;
-		scnprintf(msg, msgsize,
-			"The cycles event is not supported, trying to fall back to %s",
-			target__has_cpu(target) ? "cpu-clock" : "task-clock");
+		evsel->core.is_pmu_core = false;
+
+		/* Remove excludes for new event. */
+		if (evsel->fallenback_eacces) {
+			evsel->core.attr.exclude_kernel = 0;
+			evsel->core.attr.exclude_hv     = 0;
+			evsel->fallenback_eacces = false;
+		}
+		if (evsel->fallenback_eopnotsupp) {
+			evsel->core.attr.exclude_guest = 0;
+			evsel->fallenback_eopnotsupp = false;
+		}
 
+		/* Name is recomputed by evsel__name. */
 		zfree(&evsel->name);
+
+		/* Log message. */
+		scnprintf(msg, msgsize,
+			  "The cycles event is not supported, trying to fall back to %s",
+			  evsel__name(evsel));
 		return true;
 	} else if (err == EACCES && !evsel->core.attr.exclude_kernel &&
 		   (paranoid = perf_event_paranoid()) > 1) {
@@ -3830,7 +3847,7 @@ bool evsel__fallback(struct evsel *evsel, struct target *target, int err,
 			  " samples", paranoid);
 		evsel->core.attr.exclude_kernel = 1;
 		evsel->core.attr.exclude_hv     = 1;
-
+		evsel->fallenback_eacces = true;
 		return true;
 	} else if (err == EOPNOTSUPP && !evsel->core.attr.exclude_guest &&
 		   !evsel->exclude_GH) {
@@ -3851,7 +3868,7 @@ bool evsel__fallback(struct evsel *evsel, struct target *target, int err,
 		/* Apple M1 requires exclude_guest */
 		scnprintf(msg, msgsize, "Trying to fall back to excluding guest samples");
 		evsel->core.attr.exclude_guest = 1;
-
+		evsel->fallenback_eopnotsupp = true;
 		return true;
 	}
 no_fallback:
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index a3d754c029a0..97f57fab28ce 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -124,6 +124,8 @@ struct evsel {
 	bool			default_metricgroup; /* A member of the Default metricgroup */
 	bool			default_show_events; /* If a default group member, show the event */
 	bool			needs_uniquify;
+	bool			fallenback_eacces;
+	bool			fallenback_eopnotsupp;
 	struct hashmap		*per_pkg_mask;
 	int			err;
 	int			script_output_type;
-- 
2.53.0.851.ga537e3e6e9-goog


  reply	other threads:[~2026-03-17  5:53 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-06  7:10 [PATCH] perf test: Fix test case 120 and 121 for s390 Thomas Richter
2026-03-06 15:51 ` Jan Polensky
2026-03-06 16:53   ` Ian Rogers
2026-03-09 12:59     ` Thomas Richter
2026-03-09 18:18       ` Ian Rogers
2026-03-11  6:09         ` Namhyung Kim
2026-03-11  7:21           ` Thomas Richter
2026-03-12  3:19             ` [PATCH v1 0/2] perf evsel fallback changes Ian Rogers
2026-03-12  3:19               ` [PATCH v1 1/2] perf evsel: Improve falling back from cycles Ian Rogers
2026-03-12  3:19               ` [PATCH v1 2/2] perf evsel: Don't configure framepointer callchains on s390 Ian Rogers
2026-03-12  6:16               ` [PATCH v2 0/2] perf evsel fallback changes Ian Rogers
2026-03-12  6:16                 ` [PATCH v2 1/2] perf evsel: Improve falling back from cycles Ian Rogers
2026-03-12  6:16                 ` [PATCH v2 2/2] perf evsel: Don't configure framepointer callchains on s390 Ian Rogers
2026-03-12 12:45                   ` Thomas Richter
2026-03-12 15:54                     ` Ian Rogers
2026-03-12 16:46                       ` Ian Rogers
2026-03-12 18:00                         ` Namhyung Kim
2026-03-13  9:46                         ` Thomas Richter
2026-03-13 21:01                           ` Namhyung Kim
2026-03-13 20:28                 ` [PATCH v3 0/3] perf evsel fallback changes Ian Rogers
2026-03-13 20:28                   ` [PATCH v3 1/3] perf evsel: Improve falling back from cycles Ian Rogers
2026-03-13 20:28                   ` [PATCH v3 2/3] perf target: Constify simple check functions Ian Rogers
2026-03-13 20:28                   ` [PATCH v3 3/3] perf evlist: Improve default event for s390 Ian Rogers
2026-03-16 12:13                     ` Thomas Richter
2026-03-16 18:41                       ` Ian Rogers
2026-03-17  3:05                         ` [PATCH v4 0/5] perf evsel fallback changes Ian Rogers
2026-03-17  3:05                           ` [PATCH v4 1/5] perf evsel: Improve falling back from cycles Ian Rogers
2026-03-17  3:05                           ` [PATCH v4 2/5] perf target: Constify simple check functions Ian Rogers
2026-03-17  3:05                           ` [PATCH v4 3/5] perf evsel: Constify option arguments to config functions Ian Rogers
2026-03-17  3:06                           ` [PATCH v4 4/5] perf callchain: Move callchain option parsing out of builtin Ian Rogers
2026-03-17  3:06                           ` [PATCH v4 5/5] perf evlist: Improve default event for s390 Ian Rogers
2026-03-17  5:53                           ` [PATCH v5 0/5] perf evsel fallback changes Ian Rogers
2026-03-17  5:53                             ` Ian Rogers [this message]
2026-03-17  5:53                             ` [PATCH v5 2/5] perf target: Constify simple check functions Ian Rogers
2026-03-17  5:53                             ` [PATCH v5 3/5] perf evsel: Constify option arguments to config functions Ian Rogers
2026-03-17  5:53                             ` [PATCH v5 4/5] perf callchain: Refactor callchain option parsing Ian Rogers
2026-03-17  5:53                             ` [PATCH v5 5/5] perf evlist: Improve default event for s390 Ian Rogers
2026-03-17  7:52                               ` Thomas Richter
2026-03-17 15:54                                 ` Ian Rogers
2026-03-17 17:56                                   ` [PATCH v6 0/5] perf evsel fallback changes, better s390 defaults Ian Rogers
2026-03-17 17:56                                     ` [PATCH v6 1/5] perf evsel: Improve falling back from cycles Ian Rogers
2026-03-17 17:56                                     ` [PATCH v6 2/5] perf target: Constify simple check functions Ian Rogers
2026-03-17 17:56                                     ` [PATCH v6 3/5] perf evsel: Constify option arguments to config functions Ian Rogers
2026-03-17 17:56                                     ` [PATCH v6 4/5] perf callchain: Refactor callchain option parsing Ian Rogers
2026-03-17 17:56                                     ` [PATCH v6 5/5] perf evlist: Improve default event for s390 Ian Rogers
2026-03-18  8:20                                     ` [PATCH v6 0/5] perf evsel fallback changes, better s390 defaults Thomas Richter
2026-03-18 16:29                                       ` Ian Rogers
2026-03-18 17:58                                         ` [PATCH v7 " Ian Rogers
2026-03-18 17:58                                           ` [PATCH v7 1/5] perf evsel: Improve falling back from cycles Ian Rogers
2026-03-18 17:58                                           ` [PATCH v7 2/5] perf target: Constify simple check functions Ian Rogers
2026-03-18 17:58                                           ` [PATCH v7 3/5] perf evsel: Constify option arguments to config functions Ian Rogers
2026-03-18 17:58                                           ` [PATCH v7 4/5] perf callchain: Refactor callchain option parsing Ian Rogers
2026-03-18 17:58                                           ` [PATCH v7 5/5] perf evlist: Improve default event for s390 Ian Rogers
2026-03-18 23:45                                           ` [PATCH v8 0/5] perf evsel fallback changes, better s390 defaults Ian Rogers
2026-03-18 23:45                                             ` [PATCH v8 1/5] perf evsel: Improve falling back from cycles Ian Rogers
2026-03-18 23:45                                             ` [PATCH v8 2/5] perf target: Constify simple check functions Ian Rogers
2026-03-18 23:45                                             ` [PATCH v8 3/5] perf evsel: Constify option arguments to config functions Ian Rogers
2026-03-18 23:45                                             ` [PATCH v8 4/5] perf callchain: Refactor callchain option parsing Ian Rogers
2026-03-18 23:46                                             ` [PATCH v8 5/5] perf evlist: Improve default event for s390 Ian Rogers
2026-03-19  7:53                                               ` Thomas Richter
2026-03-19  5:39                                             ` [PATCH v8 0/5] perf evsel fallback changes, better s390 defaults Ian Rogers
2026-03-19  8:02                                               ` Thomas Richter
2026-03-20 18:12                                             ` Namhyung Kim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260317055334.760347-2-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=japo@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=namhyung@kernel.org \
    --cc=sumanthk@linux.ibm.com \
    --cc=tmricht@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox