Solaris上有Dtrace可以对如Oracle,MySQL进行调试,那Linux也有,那就是systemtap。如Oracle和MySQL出了问题,发现是system的问题,那只能等官方的说法,很被动。如果有这个工具,那就能挖得更深一点,对解决问题很有帮助。
内核引入了一种Kprobe机制,可以用来动态地收集调试和性能信息的工具,是一种非破坏性的工具,用户可以用它跟踪运行中内核任何函数或执行的指令等。systemtap 是利用Kprobe 提供的API来实现动态地监控和跟踪运行中的Linux内核的工具,相比Kprobe,systemtap更加简单,提供给用户简单的命令行接口,以及编写内核指令的脚本语言。
安装这个功能颇费周折,需要安装两个rpm。要想绕过坑,就要执行stap-prep,看下系统需要安装那个版本。[root@localhost shm]# stap-prep
Need to install the following packages:
kernel-debuginfo-2.6.32-431.el6.x86_64
...........................................
可以到http://debuginfo.centos.org/6/x86_64/下找到相应的版本,kernel-debuginfo-common-x86_64-2.6.32-431.el6.x86_64.rpm这个有260M。
[root@localhost shm]# rpm -ivh kernel-debuginfo-common-x86_64-2.6.32-431.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:kernel-debuginfo-common########################################### [100%]
安装kernel-debuginfo-2.6.32-431.el6.x86_64.rpm这个也是,如果发现版本不对,系统会提示你需要安装的版本。
[root@localhost shm]# rpm -ivh kernel-debuginfo-2.6.32-431.el6.x86_64.rpm
error: Failed dependencies:
kernel-debuginfo-common-x86_64 = 2.6.32-431.el6 is needed by kernel-debuginfo-2.6.32-431.el6.x86_64
[root@localhost shm]# rpm -ivh kernel-debuginfo-2.6.32-431.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:kernel-debuginfo ########################################### [100%]
测试一下是否安装成功
[root@localhost mysql-5.6.14]# stap -v -e 'probe vfs.read {printf("read performed\n"); exit()}'
Pass 1: parsed user script and 112 library script(s) using 206604virt/34432res/3212shr/31532data kb, in 30usr/460sys/607real ms.
Pass 2: analyzed script: 1 probe(s), 1 function(s), 4 embed(s), 0 global(s) using 303212virt/132016res/4244shr/128140data kb, in 1180usr/1420sys/5393real ms.
Pass 3: translated to C into "/tmp/stapOq9UI9/stap_de5b8012cef23568586d0744abf80b8d_1625_src.c" using 303212virt/132372res/4600shr/128140data kb, in 0usr/0sys/7real ms.
Pass 4: compiled C into "stap_de5b8012cef23568586d0744abf80b8d_1625.ko" in 1250usr/600sys/2528real ms.
Pass 5: starting run.
read performed
Pass 5: run completed in 10usr/20sys/337real ms.
测试一下,想知道5s探针一下top 20系统命令。
systemtap_t.sh
#!/usr/bin/env stap
# This script continuously lists the top 20 systemcalls on the system
#
global syscalls
function print_top () {
cnt=0
log ("SYSCALL\t\t\t\tCOUNT")
foreach ([name] in syscalls-) {
printf("%-20s\t\t%5d\n",name, syscalls[name])
if (cnt++ == 20)
break
}
printf("--------------------------------------\n")
delete syscalls
}
probe kernel.function("sys_*") {
syscalls[probefunc()]++
}
# print top syscalls every 5 seconds
probe timer.ms(5000) {
print_top ()
}
[root@localhost root]# stap systemtap_t.sh
SYSCALL COUNT
sys_times 100
sys_poll 56
sys_read 42
sys_restart_syscall 34
sys_ppoll 28
sys_rt_sigprocmask 15
sys_select 11
sys_close 5
sys_munmap 5
sys_epoll_wait 4
sys_fcntl 4
sys_write 4
sys_open 4
sys_newfstat 4
sys_mmap 4
sys_mmap_pgoff 4
sys_wait4 3
sys_alarm 3
sys_rt_sigpending 3
sys_nanosleep 3
sys_setresuid 2
--------------------------------------
https://sourceware.org/systemtap/ftp/releases 源码下载。