Share
Embed
Transcript
118 slides
-
3 $ systemctl restart super-important.service Job for super-important.service failed because
the control process exited with error code. See "systemctl status super-important.service" and "journalctl -xe" for details. -
$ systemctl status super-important.service • super-important.service Loaded: loaded (/etc/systemd/system/super-important.service; disabled…
Active: failed (Result: exit-code) since Thu 2025-07-12 00:00:01 UTC Process: 80054 ExecStart=/usr/sbin/super-important (code=exited, status… Jul 12 00:00:01 system systemd[1]: Starting Your Own Service... Jul 12 00:00:01 system super-important[80054]: Configuration file not found! 4 -
$ man super-important No manual entry for super-important Possibly, man
page is not installed, try online at: https://manpages.opensuse.org/something 6 -
$ systemctl status super-important.service • super-important.service Loaded: loaded (/etc/systemd/system/super-important.service; disabled…
Active: failed (Result: exit-code) since Thu 2025-07-12 00:00:01 UTC Process: 80054 ExecStart=/usr/sbin/super-important (code=exited, status… Jul 12 00:00:01 system systemd[1]: Starting Your Own Service... Jul 12 00:00:01 system super-important[80054]: Configuration file not found! 9 -
11 $ bpftrace -e ' tracepoint:syscalls:sys_enter_open, /comm == "super-important"/ {
printf("%s", str(args->filename)) }' Attaching 1 probes… /etc/super-important-special.conf -
16 Tracing - Is this function called? What function is
called? - What are the arguments? - What is the return value? - How long does something take? -
17 bpftrace Language - Event-driven - Awk-like language, inspired by
DTrace - C-like data structure definition & usage -
20 Probe - an event (usually with wildecard/* support) -
specific function called/returns - “tracepoint” (declared by developer) - bpftrace started, timer firing, etc… -
21 $ bpftrace -e ' tracepoint:syscalls:sys_enter_open, /comm == "super-important"/ {
printf("%s", str(args->filename)) }' Attaching 1 probes… /etc/super-important-special.conf -
37 Built-ins - Special variables - args: arguments associated with
probe - comm: program name of current process - pid: ID of current process -
40 $ bpftrace -vl 'tracepoint:syscalls:sys_enter_open' tracepoint:syscalls:sys_enter_open int __syscall_nr const char
* filename /* string pointer, need str() to read */ … -
43 Functions - Helpers provided by bpftrace - printf(): printing
with formatting - str(): convert string pointer to actual string -
46 Built-ins - Special variables - args: arguments associated with
probe - comm: program name of current process - pid: ID of current process -
50 tracepoint:syscalls:sys_enter_open { if (comm != "super-important") { return; }
printf("%s", str(args->filename)); -
57 Built-ins - Special variables - $1, $2, …: nth
positional parameter passed to the bpftrace program -
68 tracepoint:syscalls:sys_enter_open … /comm == str($1)/ { printf("%s", str(args->filename)); }
tracepoint:syscalls:sys_exit_open … /comm == str($1)/ { printf("%d", args->ret); } -
71 tracepoint:syscalls:sys_enter_open … /comm == str($1)/ { str(args->filename)??? } tracepoint:syscalls:sys_exit_open
… /comm == str($1)/ { printf("%s %d", args->filename???, args->ret); } -
- A BPF memory object, e.g. @filename - storage area
- (usually) key-value map - i.e. similar to a global variable 72 Map Variable -
73 tracepoint:syscalls:sys_enter_open … /comm == str($1)/ { @filename = str(args->filename);
} tracepoint:syscalls:sys_exit_open … /comm == str($1)/ { printf("%s: %d", @filename, args->ret); } -
75 tracepoint:syscalls:sys_enter_open … /comm == str($1)/ { @filename = str(args->filename);
} tracepoint:syscalls:sys_exit_open … /… && args->ret < 0 / { printf("%s: %d", @filename, args->ret); } -
77 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1) && !!@files[tid]/ { printf("%s:
%d\n", args->filename, args->ret); delete(@files[tid]); /* Remove used value */ -
78 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1) && !!@files[tid]/ { printf("%s:
%d\n", @files[tid], args->ret); delete(@files[tid]); /* Remove used value */ -
80 Operators and Expressions - Supports arithmetic operators - +
- * / - Logical (&&), Bitwise (^ |), and Relational (<= !=) works, too -
81 Latency - How long does it take? - Get
a timestamp when started - Get a timestamp when ended - Calculate difference between the timestamps -
82 Functions - Helpers provided by bpftrace - nsecs(): returns
a timestamp in nanoseconds - can drop parenthesis when no argument -
85 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1)/ { $duration = nsecs()
- @start; /* end - start */ printf("Took %d\n", $duration); -
86 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1)/ { $duration = nsecs()
- @start; /* end - start */ printf("Took %d\n", $duration); -
87 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1)/ { $duration = nsecs()
- @start; /* end - start */ printf("Took %d\n", $duration); -
88 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1)/ { $duration = nsecs()
- @start; /* end - start */ printf("Took %d\n", $duration); -
- Lexical-scoped variable, e.g. $duration - stores simple values -
numbers (int, long, …), strings - i.e. similar to a local variable 89 Scratch Variable -
90 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1)/ { $duration = nsecs()
- @start; /* end - start */ printf("Took %d\n", $duration); -
91 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1)/ { $duration = nsecs()
- @start; /* end - start */ printf("Took %d\n", $duration); -
92 $ ./sys_latency.bt 'super-important' Attaching 6 probes... Took 18791ns Took
8102ns Took 115726ns Took 9762ns -
93 Dealing with Lots of Data - What if open()
was calls 1k times per seconds? - Statistics to the rescue: - minimum, maximum, average, sum - quantile, histogram, time-series -
94 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1)/ { $duration = nsecs
- @start; /* end - start */ printf("Took %d\n", $duration); -
95 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1)/ { $duration = nsecs
- @start; /* end - start */ printf("Took %d\n", $duration); -
96 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1)/ { $duration = nsecs
- @start; /* end - start */ @latency = hist($duration); -
97 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1)/ { $duration = nsecs
- @start; /* end - start */ @latency = hist($duration); -
98 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1)/ { $duration = nsecs
- @start; /* end - start */ @latency = hist($duration); -
$ ./sys_latency_hist.bt 'super-important' @latency: [256, 512) 64 | | [512,
1K) 216818 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [1K, 2K) 160007 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [2K, 4K) 30255 |@@@@@@@ | [4K, 8K) 7114 |@ | [8K, 16K) 318 | | [16K, 32K) 150 | | 99 -
101 Probe - Any (non-inlined) function in kernel or application
- TCP connection change -> kernel’s tcp_set_state() - read/write to disk -> tracepoint:block:block_io - database query -> MySQL/Postgres query__start() - interactive shell usage -> bash’s readline() -
103 bpftrace Strengths 1. Safe 2. Dynamic Tracing 3. Low
Overhead 4. Easy -
104 bpftrace Strengths 1. Safe 2. Dynamic Tracing 3. Low
Overhead 4. Easy eBPF -
105 bpftrace Strengths 1. Safe 2. Dynamic Tracing 3. Low
Overhead 4. Easy eBPF Make kernel programming possible for everyone -
106 bpftrace Strengths 1. Safe 2. Dynamic Tracing 3. Low
Overhead 4. Easy eBPF Image from https://ebpf.io/what-is-ebpf/ -
107 bpftrace Strengths 1. Safe 2. Dynamic Tracing 3. Low
Overhead 4. Easy -
108 Comparing with Alternatives 1. Adding debug statements/printf() 2. Use
debugger 3. Similar Tools -
109 Adding printf() - Needs to recompile and restart the
process - more than once (?) - No Heisenbug -
111 Similar Tools - strace/ltrace: high overhead, as high as
100x - ftrace: less dynamic, restricted process - LTTng: requires out-of-tree kernel module - bcc: less up-to-date in terms of features -
113 Install bpftrace openSUSE/SLES zypper install bpftrace bpftrace-tools Debian/Ubuntu apt-get
install bpftrace Alma/CentOS/RHEL/Rocky dnf install bpftrace -
$ sudo bpftrace -e 'BEGIN { print("Hello World!"); exit(0); }'
Attaching 1 probe... Hello World! 114 -
116 Resources Book - BPF Performance Tools Videos - An
introduction to bpftrace tracing language - bpftrace: a path to the ultimate Linux tracing… Texts - A thorough introduction to bpftrace - bpftrace(8) Manual Page