17讲案例篇:如何利⽤系统缓存优化程序的运⾏效率
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4052245BD4284CDD echo "deb https://repo.iovisor.org/apt/xenial xenial main" | sudo tee /etc/apt/sources.list.d/iovisor.list sudo apt-get update sudo apt-get install -y bcc-tools libbcc-examples linux-headers-$(uname -r)
centos安装:
下载BCC的依赖: yum install -y elfutils-libelf-devel flex
安装bcc bcc-tools:yum --enablerepo=elrepo-kernel install bcc bcc-tools
添加环境变量:export PATH=$PATH:/usr/share/bcc/tools
查看:
[root@test ~]# ll /usr/share/bcc/tools/
total 844
-rwxr-xr-x 1 root root 34534 Jul 31 2019 argdist
-rwxr-xr-x 1 root root 2179 Jul 31 2019 bashreadline
-rwxr-xr-x 1 root root 6229 Jul 31 2019 biolatency
-rwxr-xr-x 1 root root 5522 Jul 31 2019 biosnoop
-rwxr-xr-x 1 root root 6391 Jul 31 2019 biotop
-rwxr-xr-x 1 root root 1150 Jul 31 2019 bitesize
-rwxr-xr-x 1 root root 2451 Jul 31 2019 bpflist
-rwxr-xr-x 1 root root 6330 Apr 1 2020 btrfsdist
-rwxr-xr-x 1 root root 9581 Apr 1 2020 btrfsslower
-rwxr-xr-x 1 root root 4715 Jul 31 2019 cachestat
-rwxr-xr-x 1 root root 7300 Jul 31 2019 cachetop
-rwxr-xr-x 1 root root 6291 Jul 31 2019 capable
-rwxr-xr-x 1 root root 57 Apr 1 2020 cobjnew
-rwxr-xr-x 1 root root 5125 Apr 1 2020 cpudist
-rwxr-xr-x 1 root root 14595 Jul 31 2019 cpuunclaimed
-rwxr-xr-x 1 root root 7093 Jul 31 2019 dbslower
-rwxr-xr-x 1 root root 3778 Jul 31 2019 dbstat
-rwxr-xr-x 1 root root 3936 Jul 31 2019 dcsnoop
-rwxr-xr-x 1 root root 3918 Jul 31 2019 dcstat
-rwxr-xr-x 1 root root 19928 Jul 31 2019 deadlock
-rw-r--r-- 1 root root 7087 Jul 31 2019 deadlock.c
drwxr-xr-x 3 root root 8192 May 17 10:29 doc
-rwxr-xr-x 1 root root 6828 Jul 31 2019 drsnoop
-rwxr-xr-x 1 root root 7252 Jul 31 2019 execsnoop
-rwxr-xr-x 1 root root 6490 Apr 1 2020 ext4dist
-rwxr-xr-x 1 root root 9916 Apr 1 2020 ext4slower
-rwxr-xr-x 1 root root 3616 Jul 31 2019 filelife
-rwxr-xr-x 1 root root 7319 Apr 1 2020 fileslower
-rwxr-xr-x 1 root root 6029 Jul 31 2019 filetop
-rwxr-xr-x 1 root root 12457 Jul 31 2019 funccount
-rwxr-xr-x 1 root root 7973 Jul 31 2019 funclatency
-rwxr-xr-x 1 root root 10124 Jul 31 2019 funcslower
-rwxr-xr-x 1 root root 3802 Jul 31 2019 gethostlatency
-rwxr-xr-x 1 root root 5195 Jul 31 2019 hardirqs
-rwxr-xr-x 1 root root 59 Apr 1 2020 javacalls
-rwxr-xr-x 1 root root 58 Apr 1 2020 javaflow
-rwxr-xr-x 1 root root 56 Apr 1 2020 javagc
-rwxr-xr-x 1 root root 60 Apr 1 2020 javaobjnew
-rwxr-xr-x 1 root root 58 Apr 1 2020 javastat
-rwxr-xr-x 1 root root 61 Apr 1 2020 javathreads
-rwxr-xr-x 1 root root 3406 Jul 31 2019 killsnoop
drwxr-xr-x 2 root root 88 May 17 10:29 lib
-rwxr-xr-x 1 root root 3689 Jul 31 2019 llcstat
-rwxr-xr-x 1 root root 2061 Jul 31 2019 mdflush
-rwxr-xr-x 1 root root 19032 Apr 1 2020 memleak
-rwxr-xr-x 1 root root 12645 Apr 1 2020 mountsnoop
-rwxr-xr-x 1 root root 3054 Jul 31 2019 mysqld_qslower
-rwxr-xr-x 1 root root 4726 Jul 31 2019 nfsdist
-rwxr-xr-x 1 root root 9032 Apr 1 2020 nfsslower
-rwxr-xr-x 1 root root 56 Apr 1 2020 nodegc
-rwxr-xr-x 1 root root 58 Apr 1 2020 nodestat
-rwxr-xr-x 1 root root 11775 Apr 1 2020 offcputime
-rwxr-xr-x 1 root root 14371 Apr 1 2020 offwaketime
-rwxr-xr-x 1 root root 2107 Apr 1 2020 oomkill
-rwxr-xr-x 1 root root 7219 Jul 31 2019 opensnoop
-rwxr-xr-x 1 root root 59 Apr 1 2020 perlcalls
-rwxr-xr-x 1 root root 58 Apr 1 2020 perlflow
-rwxr-xr-x 1 root root 58 Apr 1 2020 perlstat
-rwxr-xr-x 1 root root 58 Apr 1 2020 phpcalls
-rwxr-xr-x 1 root root 57 Apr 1 2020 phpflow
-rwxr-xr-x 1 root root 57 Apr 1 2020 phpstat
-rwxr-xr-x 1 root root 1137 Jul 31 2019 pidpersec
-rwxr-xr-x 1 root root 12752 Jul 31 2019 profile
-rwxr-xr-x 1 root root 61 Apr 1 2020 pythoncalls
-rwxr-xr-x 1 root root 60 Apr 1 2020 pythonflow
-rwxr-xr-x 1 root root 58 Apr 1 2020 pythongc
-rwxr-xr-x 1 root root 60 Apr 1 2020 pythonstat
-rwxr-xr-x 1 root root 3496 Jul 31 2019 reset-trace
-rwxr-xr-x 1 root root 59 Apr 1 2020 rubycalls
-rwxr-xr-x 1 root root 58 Apr 1 2020 rubyflow
-rwxr-xr-x 1 root root 56 Apr 1 2020 rubygc
-rwxr-xr-x 1 root root 60 Apr 1 2020 rubyobjnew
-rwxr-xr-x 1 root root 58 Apr 1 2020 rubystat
-rwxr-xr-x 1 root root 8051 Apr 1 2020 runqlat
-rwxr-xr-x 1 root root 7799 Jul 31 2019 runqlen
-rwxr-xr-x 1 root root 7072 Apr 1 2020 runqslower
-rwxr-xr-x 1 root root 7983 Jul 31 2019 shmsnoop
-rwxr-xr-x 1 root root 3635 Jul 31 2019 slabratetop
-rwxr-xr-x 1 root root 8246 Jul 31 2019 sofdsnoop
-rwxr-xr-x 1 root root 4116 Jul 31 2019 softirqs
-rwxr-xr-x 1 root root 6074 Apr 1 2020 solisten
-rwxr-xr-x 1 root root 7120 Jul 31 2019 sslsniff
-rwxr-xr-x 1 root root 15924 Jul 31 2019 stackcount
-rwxr-xr-x 1 root root 4621 Jul 31 2019 statsnoop
-rwxr-xr-x 1 root root 1264 Jul 31 2019 syncsnoop
-rwxr-xr-x 1 root root 6193 Jul 31 2019 syscount
-rwxr-xr-x 1 root root 58 Apr 1 2020 tclcalls
-rwxr-xr-x 1 root root 57 Apr 1 2020 tclflow
-rwxr-xr-x 1 root root 59 Apr 1 2020 tclobjnew
-rwxr-xr-x 1 root root 57 Apr 1 2020 tclstat
-rwxr-xr-x 1 root root 7868 Jul 31 2019 tcpaccept
-rwxr-xr-x 1 root root 7382 Jul 31 2019 tcpconnect
-rwxr-xr-x 1 root root 7361 Jul 31 2019 tcpconnlat
-rwxr-xr-x 1 root root 5839 Jul 31 2019 tcpdrop
-rwxr-xr-x 1 root root 16283 Jul 31 2019 tcplife
-rwxr-xr-x 1 root root 8770 Jul 31 2019 tcpretrans
-rwxr-xr-x 1 root root 7825 Apr 1 2020 tcpsubnet
-rwxr-xr-x 1 root root 9358 Jul 31 2019 tcptop
-rwxr-xr-x 1 root root 16438 Apr 1 2020 tcptracer
-rwxr-xr-x 1 root root 4157 Jul 31 2019 tplist
-rwxr-xr-x 1 root root 37908 Jul 31 2019 trace
-rwxr-xr-x 1 root root 3014 Jul 31 2019 ttysnoop
-rwxr-xr-x 1 root root 1381 Apr 1 2020 vfscount
-rwxr-xr-x 1 root root 2634 Jul 31 2019 vfsstat
-rwxr-xr-x 1 root root 6897 Jul 31 2019 wakeuptime
-rwxr-xr-x 1 root root 4550 Apr 1 2020 xfsdist
-rwxr-xr-x 1 root root 7882 Apr 1 2020 xfsslower
$ export PATH=$PATH:/usr/share/bcc/tools
$ cachestat 1 3 TOTAL MISSES HITS DIRTIES BUFFERS_MB CACHED_MB 2 0 2 1 17 279 2 0 2 1 17 279 2 0 2 1 17 279
$ cachetop 11:58:50 Buffers MB: 258 / Cached MB: 347 / Sort: HITS / Order: ascending PID UID CMD HITS MISSES DIRTIES READ_HIT% WRITE_HIT% 13029 root python 1 0 0 100.0% 0.0%
$ export GOPATH=~/go $ export PATH=~/go/bin:$PATH $ go get golang.org/x/sys/unix $ go get github.com/tobert/pcstat/pcstat
备注:
pcstat安装参考:https://www.cnblogs.com/Courage129/p/14282282.html
$ pcstat /bin/ls +---------+----------------+------------+-----------+---------+ | Name | Size (bytes) | Pages | Cached | Percent | |---------+----------------+------------+-----------+---------| | /bin/ls | 133792 | 33 | 0 | 000.000 | +---------+----------------+------------+-----------+---------+
$ ls $ pcstat /bin/ls +---------+----------------+------------+-----------+---------+ | Name | Size (bytes) | Pages | Cached | Percent | |---------+----------------+------------+-----------+---------| | /bin/ls | 133792 | 33 | 33 | 100.000 | +---------+----------------+------------+-----------+---------+
# ⽣成⼀个512MB的临时⽂件 $ dd if=/dev/sda1 of=file bs=1M count=512 # 清理缓存 $ echo 3 > /proc/sys/vm/drop_caches
备注:
$ pcstat file +-------+----------------+------------+-----------+---------+ | Name | Size (bytes) | Pages | Cached | Percent | |-------+----------------+------------+-----------+---------| | file | 536870912 | 131072 | 0 | 000.000 | +-------+----------------+------------+-----------+---------+
# 每隔5秒刷新⼀次数据 $ cachetop 5
$ dd if=file of=/dev/null bs=1M 512+0 records in 512+0 records out 536870912 bytes (537 MB, 512 MiB) copied, 16.0509 s, 33.4 MB/s
PID UID CMD HITS MISSES DIRTIES READ_HIT% WRITE_HIT% \.\.\.
3264 root dd 37077 37330 0 49.8% 50.2%
$ dd if=file of=/dev/null bs=1M 512+0 records in 512+0 records out 536870912 bytes (537 MB, 512 MiB) copied, 0.118415 s, 4.5 GB/s
10:45:22 Buffers MB: 4 / Cached MB: 719 / Sort: HITS / Order: ascending PID UID CMD HITS MISSES DIRTIES READ_HIT% WRITE_HIT% \.\.\. 32642 root dd 131637 0 0 100.0% 0.0%
$ pcstat file +-------+----------------+------------+-----------+---------+ | Name | Size (bytes) | Pages | Cached | Percent | |-------+----------------+------------+-----------+---------| | file | 536870912 | 131072 | 131072 | 100.000 | +-------+----------------+------------+-----------+---------+
# 每隔5秒刷新⼀次数据 $ cachetop 5
$ docker run --privileged --name=app -itd feisky/app:io-direct
$ docker logs app Reading data from disk /dev/sdb1 with buffer size 33554432 Time used: 0.929935 s to read 33554432 bytes Time used: 0.949625 s to read 33554432 bytes
16:39:18 Buffers MB: 73 / Cached MB: 281 / Sort: HITS / Order: ascending PID UID CMD HITS MISSES DIRTIES READ_HIT% WRITE_HIT% 21881 root app 1024 0 0 100.0% 0.0%
# strace -p $(pgrep app) strace: Process 4988 attached restart_syscall(<\.\.\. resuming interrupted nanosleep \.\.\.>) = 0 openat(AT_FDCWD, "/dev/sdb1", O_RDONLY|O_DIRECT) = 4 mmap(NULL, 33558528, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f448d240000 read(4, "8vq\213\314\264u\373\4\336K\224\25@\371\1\252\2\262\252q\221\n0\30\225bD\252\266@J"\.\.\., 33554432) = 33554432 write(1, "Time used: 0.948897 s to read 33"\.\.\., 45) = 45 close(4)
int flags = O_RDONLY | O_LARGEFILE | O_DIRECT; int fd = open(disk, flags, 0755);
# 删除上述案例应⽤ $ docker rm -f app # 运⾏修复后的应⽤ $ docker run --privileged --name=app -itd feisky/app:io-cached
$ docker logs app Reading data from disk /dev/sdb1 with buffer size 33554432 Time used: 0.037342 s s to read 33554432 bytes Time used: 0.029676 s to read 33554432 bytes
16:40:08 Buffers MB: 73 / Cached MB: 281 / Sort: HITS / Order: ascending PID UID CMD HITS MISSES DIRTIES READ_HIT% WRITE_HIT% 22106 root app 40960 0 0 100.0% 0.0%