Skip to content

Commit 7542b76

Browse files
committed
perf bpf: Add kprobe example to catch 5s naps
Description: . Disable strace like syscall tracing (--no-syscalls), or try tracing just some (-e *sleep). . Attach a filter function to a kernel function, returning when it should be considered, i.e. appear on the output: $ cat tools/perf/examples/bpf/5sec.c #include <bpf.h> SEC("func=hrtimer_nanosleep rqtp->tv_sec") int func(void *ctx, int err, long sec) { return sec == 5; } char _license[] SEC("license") = "GPL"; int _version SEC("version") = LINUX_VERSION_CODE; $ . Run it system wide, so that any sleep of >= 5 seconds and < than 6 seconds gets caught. . Ask for callgraphs using DWARF info, so that userspace can be unwound . While this is running, run something like "sleep 5s". # perf trace --no-syscalls -e tools/perf/examples/bpf/5sec.c/call-graph=dwarf/ 0.000 perf_bpf_probe:func:(ffffffff9811b5f0) tv_sec=5 hrtimer_nanosleep ([kernel.kallsyms]) __x64_sys_nanosleep ([kernel.kallsyms]) do_syscall_64 ([kernel.kallsyms]) entry_SYSCALL_64 ([kernel.kallsyms]) __GI___nanosleep (/usr/lib64/libc-2.26.so) rpl_nanosleep (/usr/bin/sleep) xnanosleep (/usr/bin/sleep) main (/usr/bin/sleep) __libc_start_main (/usr/lib64/libc-2.26.so) _start (/usr/bin/sleep) ^C# Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Wang Nan <wangnan0@huawei.com> Link: https://lkml.kernel.org/n/tip-2nmxth2l2h09f9gy85lyexcq@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent dd8e4ea commit 7542b76

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

‎tools/perf/examples/bpf/5sec.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
Description:
4+
5+
. Disable strace like syscall tracing (--no-syscalls), or try tracing
6+
just some (-e *sleep).
7+
8+
. Attach a filter function to a kernel function, returning when it should
9+
be considered, i.e. appear on the output.
10+
11+
. Run it system wide, so that any sleep of >= 5 seconds and < than 6
12+
seconds gets caught.
13+
14+
. Ask for callgraphs using DWARF info, so that userspace can be unwound
15+
16+
. While this is running, run something like "sleep 5s".
17+
18+
# perf trace --no-syscalls -e tools/perf/examples/bpf/5sec.c/call-graph=dwarf/
19+
0.000 perf_bpf_probe:func:(ffffffff9811b5f0) tv_sec=5
20+
hrtimer_nanosleep ([kernel.kallsyms])
21+
__x64_sys_nanosleep ([kernel.kallsyms])
22+
do_syscall_64 ([kernel.kallsyms])
23+
entry_SYSCALL_64 ([kernel.kallsyms])
24+
__GI___nanosleep (/usr/lib64/libc-2.26.so)
25+
rpl_nanosleep (/usr/bin/sleep)
26+
xnanosleep (/usr/bin/sleep)
27+
main (/usr/bin/sleep)
28+
__libc_start_main (/usr/lib64/libc-2.26.so)
29+
_start (/usr/bin/sleep)
30+
^C#
31+
32+
Copyright (C) 2018 Red Hat, Inc., Arnaldo Carvalho de Melo <acme@redhat.com>
33+
*/
34+
35+
#include <bpf.h>
36+
37+
SEC("func=hrtimer_nanosleep rqtp->tv_sec")
38+
int func(void *ctx, int err, long sec)
39+
{
40+
return sec == 5;
41+
}
42+
43+
char _license[] SEC("license") = "GPL";
44+
int _version SEC("version") = LINUX_VERSION_CODE;

0 commit comments

Comments
 (0)