Systemtap examples, Profiling - 1 Counting Function Calls Made
背景
接下来开始对systemtap example的另一个分类Profiling做一些例子的讲解.
例子来自functioncallcount.stp 脚本, 该脚本通过函数的调用情况分析内核活动. 由于没有终止时间, 同时使用统计类型性能不如数组自增, 所以我做了相应的修改. (但是请注意数组以及全局标量自增是需要锁的, 所以在多CPU(SMP)的系统中并行的自增会带来一定问题.而统计类型不会有这个问题)
修改后的脚本内容以及注解 :
[root@db-172-16-3-150 network]# cd /usr/share/systemtap/testsuite/systemtap.examples/profiling
[root@db-172-16-3-150 profiling]# cat functioncallcount.stp
#!/usr/bin/stap
# The following line command will probe all the functions
# in kernel's memory management code:
#
# stap functioncallcount.stp "*@mm/*.c"
probe kernel.function(@1).call { # probe functions listed on commandline
# called[probefunc()] <<< 1 # add a count efficiently
called[probefunc()]++
// 这里当然可以不使用统计类型, 使用数组自增就可以了.
// 数组在这里的性能比使用统计类型要好
}
// 第一个stap命令行传入参数为字符串, 指定函数或函数通配符
// 注意尽量不要使用*, 如果匹配的函数太多, 可能会导致操作系统无响应.
global called
// 最好限定一个统计时间, 否则需要等用户来退出这个模块.
// 这里相当于10秒后退出.
probe timer.s(10) {
exit()
}
probe end {
foreach (fn in called- limit 10) # Sort by call count (in decreasing order)
# (fn+ in called) # Sort by function name
#printf("%s %d\n", fn, @count(called[fn]))
printf("%s %d\n", fn, called[fn])
exit()
}
// end事件触发时, 输出调用次数排名前10的函数名以及调用次数.
// 或者按照函数名来排序. 输出所有的函数以及调用次数.
执行输出举例 :
[root@db-172-16-3-150 profiling]# stap functioncallcount.stp "*@mm/*.c"
kfree 271266
__phys_addr 187769
kmem_cache_free 156818
__inc_zone_state 122738
kmem_cache_alloc 109292
page_waitqueue 96884
lookup_page_cgroup 79088
kmem_cache_alloc_node 70086
__kmalloc_node 70056
kmem_cache_alloc_node_trace 70056
本文用到的几个函数 :
https://sourceware.org/systemtap/tapsets/API-probefunc.html
Name
function::probefunc — Return the probe point's function name, if known
Synopsis
probefunc:string()
Arguments
None
Description
This function returns the name of the function being probed
based on the current address, as computed by symname(addr)
or usymname(uaddr) depending on probe context (whether the
probe is a user probe or a kernel probe).
Please note
this function's behaviour differs between SystemTap
2.0 and earlier versions. Prior to 2.0, probefunc
obtained the function name from the probe point
string as returned by pp, and used the current
address as a fallback.
参考
1. https://sourceware.org/systemtap/SystemTap_Beginners_Guide/mainsect-profiling.html
2. https://sourceware.org/systemtap/examples/
3. /usr/share/systemtap/testsuite/systemtap.examples
4. systemtap-testsuite
5. /usr/share/systemtap/testsuite/systemtap.examples/index.txt
6. /usr/share/systemtap/testsuite/systemtap.examples/keyword-index.txt
7. /usr/share/systemtap/tapset