BCC – 用於Linux性能監視,網絡和更多的動態跟蹤工具

來源:http://blog.chinaunix.net/uid-22954220-id-5763682.html


BCC(BPF編譯器集合 )是用於創建足智多謀內核跟蹤和操作程序一套功能強大的適當的工具和示例文件。 它採用擴展BPF( Berkeley包過濾器 ),最初被稱爲eBPF這是在Linux的3.15的新功能之一。

BCC / BPF - 用於Linux性能監視的動態跟蹤工具

BCC / BPF – 用於Linux性能監視的動態跟蹤工具

實際上,大多數由BCC使用的組件的需要的Linux 4.1或以上,和其值得注意的特點包括:

  1. 不需要第三方內核模塊,因爲所有的工具都基於內置於內核的BPF工作,BCC使用Linux 4.x系列中添加的功能。
  2. 支持觀察軟件執行。
  3. 包括幾個性能分析工具與示例文件和手冊頁。

建議閱讀: 20命令行工具來監控Linux性能

最適合用於高級Linux用戶,BCC可以很容易地編寫使用內核儀表用C BPF程序和前端在PythonLua。此外,它支持多個任務,如性能分析,監控,網絡流量控制等。

如何在Linux系統中安裝BCC

請記住,BCC使用在Linux內核版本4.1中加入以上功能,並作爲一項要求,內核應該已經編譯如下設置標誌:

CONFIG_BPF=y CONFIG_BPF_SYSCALL=y # [optional, for tc filters] CONFIG_NET_CLS_BPF=m # [optional, for tc actions] CONFIG_NET_ACT_BPF=m CONFIG_BPF_JIT=y CONFIG_HAVE_BPF_JIT=y # [optional, for kprobes] CONFIG_BPF_EVENTS=y 

要檢查你的核心標誌,查看文件了/proc/config.gz或在下面的例子中運行命令:

howtoing@HowToing ~ $ grep CONFIG_BPF= /boot/config-`uname -r` CONFIG_BPF=y howtoing@HowToing ~ $ grep CONFIG_BPF_SYSCALL= /boot/config-`uname -r` CONFIG_BPF_SYSCALL=y howtoing@HowToing ~ $ grep CONFIG_NET_CLS_BPF= /boot/config-`uname -r` CONFIG_NET_CLS_BPF=m howtoing@HowToing ~ $ grep CONFIG_NET_ACT_BPF= /boot/config-`uname -r` CONFIG_NET_ACT_BPF=m howtoing@HowToing ~ $ grep CONFIG_BPF_JIT= /boot/config-`uname -r` CONFIG_BPF_JIT=y howtoing@HowToing ~ $ grep CONFIG_HAVE_BPF_JIT= /boot/config-`uname -r` CONFIG_HAVE_BPF_JIT=y howtoing@HowToing ~ $ grep CONFIG_BPF_EVENTS= /boot/config-`uname -r` CONFIG_BPF_EVENTS=y 

驗證內核標誌後,它的時間在Linux系統中安裝BCC工具。

在Ubuntu 16.04

只有夜間包的Ubuntu 16.04創建,但安裝說明都非常簡單。無需內核升級或從源代碼編譯。

$ echo "deb [trusted=yes] https://repo.iovisor.org/apt/xenial xenial-nightly main" | sudo tee /etc/apt/sources.list.d/iovisor.list
$ sudo apt-get update
$ sudo apt-get install bcc-tools

在Ubuntu 14.04

通過安裝4.3以上版本的 Linux內核,從開始http://kernel.ubuntu.com/~kernel-ppa/mainline 。

作爲一個例子,寫一個小的shell腳本“bcc-install.sh”與下面的內容。

注意 :更新PREFIX值的最晚日期,以及瀏覽提供獲得實際的REL值,在shell腳本代替它們的前綴網址的文件。

#!/bin/bash VER=4.5.1-040501 PREFIX=http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.5.1-wily/ REL=201604121331 wget ${PREFIX}/linux-headers-${VER}-generic_${VER}.${REL}_amd64.deb
wget ${PREFIX}/linux-headers-${VER}_${VER}.${REL}_all.deb
wget ${PREFIX}/linux-image-${VER}-generic_${VER}.${REL}_amd64.deb
sudo dpkg -i linux-*${VER}.${REL}*.deb

保存文件並退出。使其可執行,然後如圖所示運行:

$ chmod +x bcc-install.sh
$ sh bcc-install.sh

然後,重新啓動系統。

$ reboot

接下來,運行以下命令安裝簽名的BCC軟件包:

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys D4284CDD
$ echo "deb https://repo.iovisor.org/apt trusty main" | sudo tee /etc/apt/sources.list.d/iovisor.list
$ sudo apt-get update
$ sudo apt-get install binutils bcc bcc-tools libbcc-examples python-bcc

在Fedora 24-23

安裝從4.2+內核http://alt.fedoraproject.org/pub/alt/rawhide-kernel-nodebug ,如果您的系統具有比需要什麼更低版本。下面是一個如何做的例子:

$ sudo dnf config-manager --add-repo=http://alt.fedoraproject.org/pub/alt/rawhide-kernel-nodebug/fedora-rawhide-kernel-nodebug.repo $ sudo dnf update
$ reboot

在此之後,添加BBC工具庫,更新你的系統,並執行下一個命令系列的安裝工具:

$ echo -e '[iovisor]\nbaseurl=https://repo.iovisor.org/yum/nightly/f23/$basearch\nenabled=1\ngpgcheck=0' | sudo tee /etc/yum.repos.d/iovisor.repo
$ sudo dnf update
$ sudo dnf install bcc-tools

Arch Linux – AUR

您應該將內核升級到至少4.3.1-1版本開始,隨後安裝使用以下任何拱包管理器,如包pacaur,yaourt, 畏縮等。

bcc bcc-tools python-bcc python2-bcc

如何在Linux系統中使用BCC工具

所有BCC工具將被安裝/usr/share/bcc/tools目錄下。 但是,您可以替代地根據BCC Github上庫運行它們/tools在那裏他們有結束.py擴展。

$ ls /usr/share/bcc/tools  argdist       capable     filetop         offwaketime  stackcount  vfscount
bashreadline  cpudist     funccount       old          stacksnoop  vfsstat
biolatency    dcsnoop     funclatency     oomkill      statsnoop   wakeuptime
biosnoop      dcstat      gethostlatency  opensnoop    syncsnoop   xfsdist
biotop        doc         hardirqs        pidpersec    tcpaccept   xfsslower
bitesize      execsnoop   killsnoop       profile      tcpconnect  zfsdist
btrfsdist     ext4dist    mdflush         runqlat      tcpconnlat  zfsslower
btrfsslower   ext4slower  memleak         softirqs     tcpretrans
cachestat     filelife    mysqld_qslower  solisten     tplist
cachetop      fileslower  offcputime      sslsniff     trace

我們應包括以下幾個例子-監測的一般Linux系統的性能和網絡。

跟蹤open()系統調用

讓我們通過追蹤所有啓動open()使用opensnoop系統調用。這使我們能夠通過識別他們的數據文件,配置文件等等告訴我們各種應用程序如何工作:

$ cd /usr/share/bcc/tools  $ sudo ./opensnoop PID    COMM               FD ERR PATH 1 systemd 35 0 /proc/self/mountinfo 2797 udisksd 13 0 /proc/self/mountinfo 1 systemd 35 0 /sys/devices/pci0000:00/0000:00:0d.0/ata3/host2/target2:0:0/2:0:0:0/block/sda/sda1/uevent 1 systemd 35 0 /run/udev/data/b8:1 1 systemd -1 2 /etc/systemd/system/sys-kernel-debug-tracing.mount 1 systemd -1 2 /run/systemd/system/sys-kernel-debug-tracing.mount 1 systemd -1 2 /run/systemd/generator/sys-kernel-debug-tracing.mount 1 systemd -1 2 /usr/local/lib/systemd/system/sys-kernel-debug-tracing.mount 2247 systemd 15 0 /proc/self/mountinfo 1 systemd -1 2 /lib/systemd/system/sys-kernel-debug-tracing.mount 1 systemd -1 2 /usr/lib/systemd/system/sys-kernel-debug-tracing.mount 1 systemd -1 2 /run/systemd/generator.late/sys-kernel-debug-tracing.mount 1 systemd -1 2 /etc/systemd/system/sys-kernel-debug-tracing.mount.wants 1 systemd -1 2 /etc/systemd/system/sys-kernel-debug-tracing.mount.requires 1 systemd -1 2 /run/systemd/system/sys-kernel-debug-tracing.mount.wants 1 systemd -1 2 /run/systemd/system/sys-kernel-debug-tracing.mount.requires 1 systemd -1 2 /run/systemd/generator/sys-kernel-debug-tracing.mount.wants 1 systemd -1 2 /run/systemd/generator/sys-kernel-debug-tracing.mount.requires 1 systemd -1 2 /usr/local/lib/systemd/system/sys-kernel-debug-tracing.mount.wants 1 systemd -1 2 /usr/local/lib/systemd/system/sys-kernel-debug-tracing.mount.requires 1 systemd -1 2 /lib/systemd/system/sys-kernel-debug-tracing.mount.wants 1 systemd -1 2 /lib/systemd/system/sys-kernel-debug-tracing.mount.requires 1 systemd -1 2 /usr/lib/systemd/system/sys-kernel-debug-tracing.mount.wants 1 systemd -1 2 /usr/lib/systemd/system/sys-kernel-debug-tracing.mount.requires 1 systemd -1 2 /run/systemd/generator.late/sys-kernel-debug-tracing.mount.wants 1 systemd -1 2 /run/systemd/generator.late/sys-kernel-debug-tracing.mount.requires 1 systemd -1 2 /etc/systemd/system/sys-kernel-debug-tracing.mount.d 1 systemd -1 2 /run/systemd/system/sys-kernel-debug-tracing.mount.d 1 systemd -1 2 /run/systemd/generator/sys-kernel-debug-tracing.mount.d ....

總結塊設備I / O延遲

在本示例中,它顯示使用生物分解的磁盤I / O延遲的彙總分佈。執行該命令後,等待幾分鐘,然後按Ctrl-C來結束它,並查看輸出。

$ sudo ./biolatecncy Tracing block device I/O... Hit Ctrl-C to end. ^C
usecs : count     distribution 0 -> 1 : 0 | | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 0 | | 16 -> 31 : 0 | | 32 -> 63 : 0 | | 64 -> 127 : 0 | | 128 -> 255 : 3 |****************************************| 256 -> 511 : 3 |****************************************| 512 -> 1023 : 1 |************* |

通過exec()Syscalls跟蹤新進程

在本節中,我們將移動到使用execsnoop工具執行跟蹤新工藝。 每一個過程是由兩岔時間fork()exec()系統調用,它示出在輸出中。但是,並非所有進程都被捕獲。

$ sudo ./execsnoop PCOMM            PID    PPID   RET ARGS
gnome-screensho 14882 14881 0 /usr/bin/gnome-screenshot --gapplication-service
systemd-hostnam 14892 1 0 /lib/systemd/systemd-hostnamed
nautilus 14897 2767 -2 /home/howtoing/bin/net usershare info
nautilus 14897 2767 -2 /home/howtoing/.local/bin/net usershare info
nautilus 14897 2767 -2 /usr/local/sbin/net usershare info
nautilus 14897 2767 -2 /usr/local/bin/net usershare info
nautilus 14897 2767 -2 /usr/sbin/net usershare info
nautilus 14897 2767 -2 /usr/bin/net usershare info
nautilus 14897 2767 -2 /sbin/net usershare info
nautilus 14897 2767 -2 /bin/net usershare info
nautilus 14897 2767 -2 /usr/games/net usershare info
nautilus 14897 2767 -2 /usr/local/games/net usershare info
nautilus 14897 2767 -2 /snap/bin/net usershare info
compiz 14899 14898 -2 /home/howtoing/bin/libreoffice --calc
compiz 14899 14898 -2 /home/howtoing/.local/bin/libreoffice --calc
compiz 14899 14898 -2 /usr/local/sbin/libreoffice --calc
compiz 14899 14898 -2 /usr/local/bin/libreoffice --calc
compiz 14899 14898 -2 /usr/sbin/libreoffice --calc
libreoffice 14899 2252 0 /usr/bin/libreoffice --calc
dirname 14902 14899 0 /usr/bin/dirname /usr/bin/libreoffice
basename 14903 14899 0 /usr/bin/basename /usr/bin/libreoffice ...

跟蹤慢ext4操作

使用ext4slower跟蹤ext4文件系統是大於10ms較慢的常用操作,幫助我們通過文件系統識別獨立較慢的磁盤I / O。

建議閱讀: 13 Linux的性能監視工具

它只輸出超過閾值的操作:

$ sudo ./execslower Tracing ext4 operations slower than 10 ms
TIME     COMM           PID    T BYTES   OFF_KB   LAT(ms) FILENAME 11:59:13 upstart 2252 W 48 1 10.76 dbus.log 11:59:13 gnome-screensh 14993 R 144 0 10.96 settings.ini 11:59:13 gnome-screensh 14993 R 28 0 16.02 gtk.css 11:59:13 gnome-screensh 14993 R 3389 0 18.32 gtk-main.css 11:59:25 rs:main Q:Reg 1826 W 156 60 31.85 syslog 11:59:25 pool 15002 R 208 0 14.98 .xsession-errors 11:59:25 pool 15002 R 644 0 12.28 .ICEauthority 11:59:25 pool 15002 R 220 0 13.38 .bash_logout 11:59:27 dconf-service 2599 S 0 0 22.75 user.BHDKOY 11:59:33 compiz 2548 R 4096 0 19.03 firefox.desktop 11:59:34 compiz 15008 R 128 0 27.52 firefox.sh 11:59:34 firefox 15008 R 128 0 36.48 firefox 11:59:34 zeitgeist-daem 2988 S 0 0 62.23 activity.sqlite-wal 11:59:34 zeitgeist-fts 2996 R 8192 40 15.67 postlist.DB 11:59:34 firefox 15008 R 140 0 18.05 dependentlibs.list 11:59:34 zeitgeist-fts 2996 S 0 0 25.96 position.tmp 11:59:34 firefox 15008 R 4096 0 10.67 libplc4.so 11:59:34 zeitgeist-fts 2996 S 0 0 11.29 termlist.tmp ...

跟蹤塊設備I / O,帶PID和延遲

下一步,讓我們每秒鐘爲每個磁盤I / O打印一行,其中包括進程ID,扇區,字節,使用biosnoop的其他延遲等細節:

$ sudo ./biosnoop TIME(s) COMM           PID    DISK    T  SECTOR    BYTES   LAT(ms) 0.000000000 ? 0 R -1 8 0.26 2.047897000 ? 0 R -1 8 0.21 3.280028000 kworker/u4:0 14871 sda     W 30552896 4096 0.24 3.280271000 jbd2/sda1-8 545 sda     W 29757720 12288 0.40 3.298318000 jbd2/sda1-8 545 sda     W 29757744 4096 0.14 4.096084000 ? 0 R -1 8 0.27 6.143977000 ? 0 R -1 8 0.27 8.192006000 ? 0 R -1 8 0.26 8.303938000 kworker/u4:2 15084 sda     W 12586584 4096 0.14 8.303965000 kworker/u4:2 15084 sda     W 25174736 4096 0.14 10.239961000 ? 0 R -1 8 0.26 12.292057000 ? 0 R -1 8 0.20 14.335990000 ? 0 R -1 8 0.26 16.383798000 ? 0 R -1 8 0.17 ...

跟蹤頁面緩存命中/未命中比率

此後,我們繼續使用cachestat到顯示器彙總統計的一行從系統緩存每一秒。這通過指出低緩存命中率和高錯失率來實現系統調整操作:

$ sudo ./cachestat HITS   MISSES  DIRTIES  READ_HIT% WRITE_HIT% BUFFERS_MB  CACHED_MB 0 0 0 0.0% 0.0% 19 544 4 4 2 25.0% 25.0% 19 544 1321 33 4 97.3% 2.3% 19 545 7476 0 2 100.0% 0.0% 19 545 6228 15 2 99.7% 0.2% 19 545 0 0 0 0.0% 0.0% 19 545 7391 253 108 95.3% 2.7% 19 545 33608 5382 28 86.1% 13.8% 19 567 25098 37 36 99.7% 0.0% 19 566 17624 239 416 96.3% 0.5% 19 520 ...

跟蹤TCP活動連接

監測TCP連接,每秒使用tcpconnect。其輸出包括源和目標地址以及端口號。此工具可用於跟蹤意外的TCP連接,從而幫助我們識別應用程序配置或攻擊者的低效率。

$ sudo ./tcpconnect PID    COMM         IP SADDR            DADDR            DPORT 15272 Socket Threa 4 10.0.2.15 91.189.89.240 80 15272 Socket Threa 4 10.0.2.15 216.58.199.142 443 15272 Socket Threa 4 10.0.2.15 216.58.199.142 80 15272 Socket Threa 4 10.0.2.15 216.58.199.174 443 15272 Socket Threa 4 10.0.2.15 54.200.62.216 443 15272 Socket Threa 4 10.0.2.15 54.200.62.216 443 15272 Socket Threa 4 10.0.2.15 117.18.237.29 80 15272 Socket Threa 4 10.0.2.15 216.58.199.142 80 15272 Socket Threa 4 10.0.2.15 216.58.199.131 80 15272 Socket Threa 4 10.0.2.15 216.58.199.131 443 15272 Socket Threa 4 10.0.2.15 52.222.135.52 443 15272 Socket Threa 4 10.0.2.15 216.58.199.131 443 15272 Socket Threa 4 10.0.2.15 54.200.62.216 443 15272 Socket Threa 4 10.0.2.15 54.200.62.216 443 15272 Socket Threa 4 10.0.2.15 216.58.199.132 443 15272 Socket Threa 4 10.0.2.15 216.58.199.131 443 15272 Socket Threa 4 10.0.2.15 216.58.199.142 443 15272 Socket Threa 4 10.0.2.15 54.69.17.198 443 15272 Socket Threa 4 10.0.2.15 54.69.17.198 443 ...

以上所有的工具,也可以與各種選項一起使用,使一個給定的工具的幫助頁面,利用的-h選項,例如:

$ sudo ./tcpconnect -h usage: tcpconnect [-h] [-t] [-p PID] [-P PORT] Trace TCP connects
optional arguments: -h, --help            show this help message and exit -t, --timestamp       include timestamp on output -p PID, --pid PID     trace this PID only -P PORT, --port PORT  comma-separated list of destination ports to trace. examples: ./tcpconnect # trace all TCP connect()s ./tcpconnect -t # include timestamps ./tcpconnect -p 181 # only trace PID 181 ./tcpconnect -P 80 # only trace port 80 ./tcpconnect -P 80,81 # only trace port 80 and 81

跟蹤失敗exec()s Syscalls

要跟蹤失敗的exec()秒的系統調用,應用-x具有如下opensnoop選項:

$ sudo ./opensnoop -x PID    COMM               FD ERR PATH 15414 pool -1 2 /home/.hidden 15415 (ostnamed) -1 2 /sys/fs/cgroup/cpu/system.slice/systemd-hostnamed.service/cgroup.procs 15415 (ostnamed) -1 2 /sys/fs/cgroup/cpu/system.slice/cgroup.procs 15415 (ostnamed) -1 2 /sys/fs/cgroup/cpuacct/system.slice/systemd-hostnamed.service/cgroup.procs 15415 (ostnamed) -1 2 /sys/fs/cgroup/cpuacct/system.slice/cgroup.procs 15415 (ostnamed) -1 2 /sys/fs/cgroup/blkio/system.slice/systemd-hostnamed.service/cgroup.procs 15415 (ostnamed) -1 2 /sys/fs/cgroup/blkio/system.slice/cgroup.procs 15415 (ostnamed) -1 2 /sys/fs/cgroup/memory/system.slice/systemd-hostnamed.service/cgroup.procs 15415 (ostnamed) -1 2 /sys/fs/cgroup/memory/system.slice/cgroup.procs 15415 (ostnamed) -1 2 /sys/fs/cgroup/pids/system.slice/systemd-hostnamed.service/cgroup.procs 2548 compiz -1 2 15416 systemd-cgroups -1 2 /run/systemd/container 15416 systemd-cgroups -1 2 /sys/fs/kdbus/0-system/bus 15415 systemd-hostnam -1 2 /run/systemd/container 15415 systemd-hostnam -1 13 /proc/1/environ 15415 systemd-hostnam -1 2 /sys/fs/kdbus/0-system/bus 1695 dbus-daemon -1 2 /run/systemd/users/0 15415 systemd-hostnam -1 2 /etc/machine-info 15414 pool -1 2 /home/howtoing/.hidden 15414 pool -1 2 /home/howtoing/Binary/.hidden 2599 dconf-service -1 2 /run/user/1000/dconf/user ...

跟蹤特定的過程函數

下面的最後一個示例演示瞭如何執行自定義跟蹤操作。我們正在使用其PID跟蹤特定進程。

建議閱讀: Netdata -適用於Linux的實時性能監控工具

首先確定進程ID:

$ pidof firefox 15437

後來,運行定製trace命令。 在下面的命令: -p指定的進程ID, do_sys_open()是動態跟蹤包括它的第二個參數是一個字符串核函數。

$ sudo ./trace -p 4095 'do_sys_open "%s", arg2' TIME     PID    COMM         FUNC - 12:17:14 15437 firefox      do_sys_open /run/user/1000/dconf/user 12:17:14 15437 firefox      do_sys_open /home/howtoing/.config/dconf/user 12:18:07 15437 firefox      do_sys_open /run/user/1000/dconf/user 12:18:07 15437 firefox      do_sys_open /home/howtoing/.config/dconf/user 12:18:13 15437 firefox      do_sys_open /sys/devices/system/cpu/present 12:18:13 15437 firefox      do_sys_open /dev/urandom 12:18:13 15437 firefox      do_sys_open /dev/urandom 12:18:14 15437 firefox      do_sys_open /usr/share/fonts/truetype/liberation/LiberationSans-Italic.ttf 12:18:14 15437 firefox      do_sys_open /usr/share/fonts/truetype/liberation/LiberationSans-Italic.ttf 12:18:14 15437 firefox      do_sys_open /usr/share/fonts/truetype/liberation/LiberationSans-Italic.ttf 12:18:14 15437 firefox      do_sys_open /sys/devices/system/cpu/present 12:18:14 15437 firefox      do_sys_open /dev/urandom 12:18:14 15437 firefox      do_sys_open /dev/urandom 12:18:14 15437 firefox      do_sys_open /dev/urandom 12:18:14 15437 firefox      do_sys_open /dev/urandom 12:18:15 15437 firefox      do_sys_open /sys/devices/system/cpu/present 12:18:15 15437 firefox      do_sys_open /dev/urandom 12:18:15 15437 firefox      do_sys_open /dev/urandom 12:18:15 15437 firefox      do_sys_open /sys/devices/system/cpu/present 12:18:15 15437 firefox      do_sys_open /dev/urandom 12:18:15 15437 firefox      do_sys_open /dev/urandom ....

概要

BCC是各種系統管理任務,例如追蹤系統性能監控,跟蹤塊設備一個功能強大且易於使用的工具包I / O,TCP的功能,文件系統操作,系統調用,Node.js的探頭,再加上其它更多地方。重要的是,它提供了幾個示例文件和手冊頁的工具來指導你,使其用戶友好和可靠。

最後但同樣重要的是,您可以通過下面的評論部分分享您對主題的想法,提出問題,提出有用的建議或任何建設性的反饋,以回覆我們。

欲瞭解更多信息和使用情況,請訪問: https://iovisor.github.io/bcc/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章