Skip to content

Commit

Permalink
perf tools: Add fallback for exclude_guest
Browse files Browse the repository at this point in the history
Commit 7b10098 ("perf evlist: Remove __evlist__add_default")
changed to parse "cycles:P" event instead of creating a new cycles
event for perf record.  But it also changed the way how modifiers are
handled so it doesn't set the exclude_guest bit by default.

It seems Apple M1 PMU requires exclude_guest set and returns EOPNOTSUPP
if not.  Let's add a fallback so that it can work with default events.

Also update perf stat hybrid tests to handle possible u or H modifiers.

Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Reviewed-by: Ravi Bangoria <ravi.bangoria@amd.com>
Acked-by: Kan Liang <kan.liang@linux.intel.com>
Cc: James Clark <james.clark@arm.com>
Cc: Atish Patra <atishp@atishpatra.org>
Cc: Mingwei Zhang <mizhang@google.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Palmer Dabbelt <palmer@rivosinc.com>
Link: https://lore.kernel.org/r/20241016062359.264929-2-namhyung@kernel.org
Fixes: 7b10098 ("perf evlist: Remove __evlist__add_default")
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
  • Loading branch information
Namhyung Kim committed Oct 22, 2024
1 parent 3e2d4df commit bb6e7cb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
18 changes: 15 additions & 3 deletions tools/perf/builtin-stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,7 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
* (behavior changed with commit b0a873e).
*/
if (errno == EINVAL || errno == ENOSYS ||
errno == ENOENT || errno == EOPNOTSUPP ||
errno == ENXIO) {
errno == ENOENT || errno == ENXIO) {
if (verbose > 0)
ui__warning("%s event is not supported by the kernel.\n",
evsel__name(counter));
Expand All @@ -659,7 +658,7 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
if (verbose > 0)
ui__warning("%s\n", msg);
return COUNTER_RETRY;
} else if (target__has_per_thread(&target) &&
} else if (target__has_per_thread(&target) && errno != EOPNOTSUPP &&
evsel_list->core.threads &&
evsel_list->core.threads->err_thread != -1) {
/*
Expand All @@ -680,6 +679,19 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
return COUNTER_SKIP;
}

if (errno == EOPNOTSUPP) {
if (verbose > 0) {
ui__warning("%s event is not supported by the kernel.\n",
evsel__name(counter));
}
counter->supported = false;
counter->errored = true;

if ((evsel__leader(counter) != counter) ||
!(counter->core.leader->nr_members > 1))
return COUNTER_SKIP;
}

evsel__open_strerror(counter, &target, errno, msg, sizeof(msg));
ui__error("%s\n", msg);

Expand Down
2 changes: 1 addition & 1 deletion tools/perf/tests/shell/stat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ test_hybrid() {
fi

# Run default Perf stat
cycles_events=$(perf stat -- true 2>&1 | grep -E "/cycles/| cycles " | wc -l)
cycles_events=$(perf stat -- true 2>&1 | grep -E "/cycles/[uH]*| cycles[:uH]* " -c)

if [ "$pmus" -ne "$cycles_events" ]
then
Expand Down
21 changes: 21 additions & 0 deletions tools/perf/util/evsel.c
Original file line number Diff line number Diff line change
Expand Up @@ -3156,6 +3156,27 @@ bool evsel__fallback(struct evsel *evsel, struct target *target, int err,
evsel->core.attr.exclude_kernel = 1;
evsel->core.attr.exclude_hv = 1;

return true;
} else if (err == EOPNOTSUPP && !evsel->core.attr.exclude_guest &&
!evsel->exclude_GH) {
const char *name = evsel__name(evsel);
char *new_name;
const char *sep = ":";

/* Is there already the separator in the name. */
if (strchr(name, '/') ||
(strchr(name, ':') && !evsel->is_libpfm_event))
sep = "";

if (asprintf(&new_name, "%s%sH", name, sep) < 0)
return false;

free(evsel->name);
evsel->name = new_name;
/* Apple M1 requires exclude_guest */
scnprintf(msg, msgsize, "trying to fall back to excluding guest samples");
evsel->core.attr.exclude_guest = 1;

return true;
}

Expand Down

0 comments on commit bb6e7cb

Please sign in to comment.