Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf top: Allow filters on events

Allow filters to be added to perf top events. One use is to workaround
issues with:
```
$ perf top --uid="$(id -u)"
```
which tries to scan /proc find processes belonging to the uid and can
fail in such a pid terminates between the scan and the
perf_event_open reporting:
```
Error:
The sys_perf_event_open() syscall returned with 3 (No such process) for event (cycles:P).
/bin/dmesg | grep -i perf may provide additional information.
```
A similar filter:
```
$ perf top -e cycles:P --filter "uid == $(id -u)"
```
doesn't fail this way.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: bpf@vger.kernel.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240524205227.244375-4-irogers@google.com

authored by

Ian Rogers and committed by
Namhyung Kim
af752016 d92aa899

+13
+4
tools/perf/Documentation/perf-top.txt
··· 43 43 encoding with the layout of the event control registers as described 44 44 by entries in /sys/bus/event_source/devices/cpu/format/*. 45 45 46 + --filter=<filter>:: 47 + Event filter. This option should follow an event selector (-e). For 48 + syntax see linkperf:perf-record[1]. 49 + 46 50 -E <entries>:: 47 51 --entries=<entries>:: 48 52 Display this many functions.
+9
tools/perf/builtin-top.c
··· 1055 1055 } 1056 1056 } 1057 1057 1058 + if (evlist__apply_filters(evlist, &counter)) { 1059 + pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", 1060 + counter->filter ?: "BPF", evsel__name(counter), errno, 1061 + str_error_r(errno, msg, sizeof(msg))); 1062 + goto out_err; 1063 + } 1064 + 1058 1065 if (evlist__mmap(evlist, opts->mmap_pages) < 0) { 1059 1066 ui__error("Failed to mmap with %d (%s)\n", 1060 1067 errno, str_error_r(errno, msg, sizeof(msg))); ··· 1469 1462 OPT_CALLBACK('e', "event", &parse_events_option_args, "event", 1470 1463 "event selector. use 'perf list' to list available events", 1471 1464 parse_events_option), 1465 + OPT_CALLBACK(0, "filter", &top.evlist, "filter", 1466 + "event filter", parse_filter), 1472 1467 OPT_U64('c', "count", &opts->user_interval, "event period to sample"), 1473 1468 OPT_STRING('p', "pid", &target->pid, "pid", 1474 1469 "profile events on existing process id"),