bpftrace: This super easy-to-use Linux performance tool, you don't know yet?
Introduction
bpftrace is an advanced tracing tool and language for Linux. Based on eBPF and BBC, the tool collects the kernel and program running information through the probe mechanism, and then displays the information in charts and other ways to help developers find deeply hidden bugs, security issues and performance bottlenecks.
bpftrace uses LLVM as a backend, compiles scripts to BPF bytecode, and leverages BCC to interact with the Linux BPF system, as well as existing Linux tracing features: kernel dynamic tracing (kprobes), user-level dynamic tracing (uprobes), and tracing point. The bpftrace language is inspired by awk and C, and front-end probes such as DTrace and SystemTap.
The project address is:
https://github.com/iovisor/bpftrace
Download and install
The official recommendation is to run the Linux 4.9 kernel or higher. Some tools may work on older kernels, but those older kernels are no longer tested.
Ubuntu
# 适用于 Ubuntu 19.04 及更高版本
sudo apt-get install -y bpftrace
# 在 Ubuntu 16.04 及更高版本上,bpftrace 也可用作 snap 包
sudo snap install --devmode bpftrace
sudo snap connect bpftrace:system-trace
Fedora
# 对于 Fedora 28(及更高版本),bpftrace 已包含在官方仓库中。只需使用 dnf 安装软件包
sudo dnf install -y bpftrace
Gentoo
# 在 Gentoo 上,bpftrace 包含在官方仓库中。可以通过 emerge 安装
sudo emerge -av bpftrace
sudo emerge -av bpftrace
other
# 其他如 Debian、openSUSE、CentOS 可以分别在以下链接中进行下载安装
https://tracker.debian.org/pkg/bpftrace
https://software.opensuse.org/package/bpftrace
https://github.com/fbs/el7-bpf-specs/blob/master/README.md#repository
Docker
$ docker run -v $(pwd):/output quay.io/iovisor/bpftrace:master-vanilla_llvm_clang_glibc2.23 \
/bin/bash -c "cp /usr/bin/bpftrace /output"
$ ./bpftrace -V
v0.9.4
tool
bpftrace contains various tools that also serve as examples of programming in the bpftrace language. Briefly introduce a few tools.
bashreadline.bt
To print the entered bash command system-wide:
# ./bashreadline.bt
Attaching 2 probes...
Tracing bash commands... Hit Ctrl-C to end.
TIME PID COMMAND
06:40:06 5526 df -h
06:40:09 5526 ls -l
06:40:18 5526 echo hello bpftrace
06:40:42 5526 echooo this is a failed command, but we can see it anyway
^C
biolatency.bt
I/O Latency Histogram:
# ./biolatency.bt
Attaching 3 probes...
Tracing block device I/O... Hit Ctrl-C to end.
^C
@usecs:
[256, 512) 2 | |
[512, 1K) 10 |@ |
[1K, 2K) 426 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[2K, 4K) 230 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[4K, 8K) 9 |@ |
[8K, 16K) 128 |@@@@@@@@@@@@@@@ |
[16K, 32K) 68 |@@@@@@@@ |
[32K, 64K) 0 | |
[64K, 128K) 0 | |
[128K, 256K) 10 |@ |
bitesize.bt
Display disk I/O size as a histogram:
# ./bitesize.bt
Attaching 3 probes...
Tracing block device I/O... Hit Ctrl-C to end.
^C
I/O size (bytes) histograms by process name:
@[cleanup]:
[4K, 8K) 2 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
@[postdrop]:
[4K, 8K) 2 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
@[jps]:
[4K, 8K) 1 |@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[8K, 16K) 0 | |
[16K, 32K) 0 | |
[32K, 64K) 2 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
@[kworker/2:1H]:
[0] 3 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[1] 0 | |
[2, 4) 0 | |
[4, 8) 0 | |
[8, 16) 0 | |
[16, 32) 0 | |
[32, 64) 0 | |
[64, 128) 0 | |
[128, 256) 0 | |
[256, 512) 0 | |
[512, 1K) 0 | |
[1K, 2K) 0 | |
[2K, 4K) 0 | |
[4K, 8K) 0 | |
[8K, 16K) 0 | |
[16K, 32K) 0 | |
[32K, 64K) 0 | |
[64K, 128K) 1 |@@@@@@@@@@@@@@@@@ |
@[jbd2/nvme0n1-8]:
[4K, 8K) 3 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[8K, 16K) 0 | |
[16K, 32K) 0 | |
[32K, 64K) 2 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[64K, 128K) 1 |@@@@@@@@@@@@@@@@@ |
@[dd]:
[16K, 32K) 921 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
cpuwalk.bt
See which CPUs are executing processes:
# ./cpuwalk.bt
Attaching 2 probes...
Sampling CPU at 99hz... Hit Ctrl-C to end.
^C
@cpu:
[0, 1) 130 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[1, 2) 137 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[2, 3) 99 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[3, 4) 99 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[4, 5) 82 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[5, 6) 34 |@@@@@@@@@@@@ |
[6, 7) 67 |@@@@@@@@@@@@@@@@@@@@@@@@ |
[7, 8) 41 |@@@@@@@@@@@@@@@ |
[8, 9) 97 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[9, 10) 140 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[10, 11) 105 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[11, 12) 77 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[12, 13) 39 |@@@@@@@@@@@@@@ |
[13, 14) 58 |@@@@@@@@@@@@@@@@@@@@@ |
[14, 15) 64 |@@@@@@@@@@@@@@@@@@@@@@@ |
[15, 16) 57 |@@@@@@@@@@@@@@@@@@@@@ |
[16, 17) 99 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[17, 18) 56 |@@@@@@@@@@@@@@@@@@@@ |
[18, 19) 44 |@@@@@@@@@@@@@@@@ |
[19, 20) 80 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[20, 21) 64 |@@@@@@@@@@@@@@@@@@@@@@@ |
[21, 22) 59 |@@@@@@@@@@@@@@@@@@@@@ |
[22, 23) 88 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[23, 24) 84 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[24, 25) 29 |@@@@@@@@@@ |
[25, 26) 48 |@@@@@@@@@@@@@@@@@ |
[26, 27) 62 |@@@@@@@@@@@@@@@@@@@@@@@ |
[27, 28) 66 |@@@@@@@@@@@@@@@@@@@@@@@@ |
[28, 29) 57 |@@@@@@@@@@@@@@@@@@@@@ |
[29, 30) 59 |@@@@@@@@@@@@@@@@@@@@@ |
[30, 31) 56 |@@@@@@@@@@@@@@@@@@@@ |
[31, 32) 23 |@@@@@@@@ |
[32, 33) 90 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[33, 34) 62 |@@@@@@@@@@@@@@@@@@@@@@@ |
[34, 35) 39 |@@@@@@@@@@@@@@ |
[35, 36) 68 |@@@@@@@@@@@@@@@@@@@@@@@@@ |
tcpconnect.bt
Track TCP active connections (connect()):
# ./tcpconnect.bt
TIME PID COMM SADDR SPORT DADDR DPORT
00:36:45 1798396 agent 127.0.0.1 5001 10.229.20.82 56114
00:36:45 1798396 curl 127.0.0.1 10255 10.229.20.82 56606
00:36:45 3949059 nginx 127.0.0.1 8000 127.0.0.1 37780
开源前哨
Daily sharing of popular, interesting and useful open source projects. Participate in the maintenance of 100,000+ Star open source technology resource libraries, including: Python, Java, C/C++, Go, JS, CSS, Node.js, PHP, .NET, etc.