perf top: Allow filters on events
authorIan Rogers <irogers@google.com>
Fri, 24 May 2024 20:52:27 +0000 (13:52 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Thu, 30 May 2024 17:05:57 +0000 (10:05 -0700)
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
tools/perf/Documentation/perf-top.txt
tools/perf/builtin-top.c

index a754875fa5bbbf2b53481ae98ec2d63e7327a8a8..667e5102075e28daeca7da4e0d982afc2cb03ffa 100644 (file)
@@ -43,6 +43,10 @@ Default is to monitor all CPUS.
        encoding with the layout of the event control registers as described
        by entries in /sys/bus/event_source/devices/cpu/format/*.
 
+--filter=<filter>::
+       Event filter.  This option should follow an event selector (-e). For
+       syntax see linkperf:perf-record[1].
+
 -E <entries>::
 --entries=<entries>::
        Display this many functions.
index 1d6aef51c122c4207116377f13c63df4a7e27926..e8cbbf10d361e70d421753d38dc27f6de4571758 100644 (file)
@@ -1055,6 +1055,13 @@ try_again:
                }
        }
 
+       if (evlist__apply_filters(evlist, &counter)) {
+               pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
+                       counter->filter ?: "BPF", evsel__name(counter), errno,
+                       str_error_r(errno, msg, sizeof(msg)));
+               goto out_err;
+       }
+
        if (evlist__mmap(evlist, opts->mmap_pages) < 0) {
                ui__error("Failed to mmap with %d (%s)\n",
                            errno, str_error_r(errno, msg, sizeof(msg)));
@@ -1462,6 +1469,8 @@ int cmd_top(int argc, const char **argv)
        OPT_CALLBACK('e', "event", &parse_events_option_args, "event",
                     "event selector. use 'perf list' to list available events",
                     parse_events_option),
+       OPT_CALLBACK(0, "filter", &top.evlist, "filter",
+                    "event filter", parse_filter),
        OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
        OPT_STRING('p', "pid", &target->pid, "pid",
                    "profile events on existing process id"),