Systemtap Timer probes

2 minute read

背景

You can use intervals defined by the standard kernel jiffies timer to trigger probe handlers asynchronously.  
A jiffy is a kernel-defined unit of time typically between 1 and 60 msec.   
timer属于异步探针, 利用standard kernel jiffies timer触发.  
standard kernel jiffies timer一般内核定义1个jiffy为1到60毫秒.  
  
Two probe point variants are supported by the translator:  
timer.jiffies(N)  
timer.jiffies(N).randomize(M)  
The probe handler runs every N jiffies.   
If the randomize component is given, a linearly distributed random value in the range [-M ... +M] is added to N every time the handler executes.   
N is restricted to a reasonable range (1 to approximately 1,000,000), and M is restricted to be less than N.   
There are no target variables provided in either context.   
Probes can be run concurrently on multiple processors.  
timer探针的用法之一 :   
timer.jiffies(N), 每隔N个jiffy后触发.  
timer.jiffies(N).randomize(M) , 每隔N个jiffy+(-M到M之间的一个随机数) , N必须大于M  
例如N=10, M=3, 那么每隔10+(-3到3之间的一个随机数)个jiffy后触发.  
N的取值范围1 to approximately 1,000,000  
timer探针没有上下文相关变量, 同时timer探针支持同时被多个处理器触发(如多核CPU).  
  
Intervals may be specified in units of time.   
There are two probe point variants similar to the jiffies timer:  
timer.ms(N)  
timer.ms(N).randomize(M)  
Here, N and M are specified in milliseconds, but the full options for units are:  
seconds (s or sec),   
milliseconds (ms or msec),   
microseconds (us or usec),   
nanoseconds (ns or nsec),   
hertz (hz).   
Randomization is not supported for hertz timers. // 时间单位为赫兹时不能使用随机数加减.  
timer计数器除了可以使用jiffy单位, 还可以使用其他时间单位如s, ms, us, ns, hz(赫兹).  
  
The resolution of the timers depends on the target kernel.   
For kernels prior to 2.6.17, timers are limited to jiffies resolution, so intervals are rounded up to the nearest jiffies interval.   
After 2.6.17, the implementation uses hrtimers for greater precision, though the resulting resolution will be dependent upon architecture.   
In either case, if the randomize component is given, then the random value will be added to the interval before any rounding occurs.  
不同的内核版本支持的时间精度不一样, 例如2.6.17以前的内核版本, 最小的时间单位为jiffy, 所以如果使用us, ns, hz时会round为最接近的jiffy. 将丢失probe中定义的精度.  
2.6.17以后的版本, 使用hrtimer得到更高的精度.  
不管使用什么内核版本, 如果使用了randomize, 都是先计算timer+随机数, 然后做round.  
  
Profiling timers are available to provide probes that execute on all CPUs at each system tick.   
This probe takes no parameters, as follows.  
timer.profile  
还有一个时间探针是timer.profile, 间隔每个系统时钟周期触发.  
  
Full context information of the interrupted process is available, making this probe suitable for implementing a time-based sampling profiler.  
  
The following is an example of timer usage.  
# Refers to a periodic interrupt, every 1000 jiffies:  
timer.jiffies(1000)  
# Fires every 5 seconds:  
timer.sec(5)  
# Refers to a periodic interrupt, every 1000 +/- 200 jiffies:  
timer.jiffies(1000).randomize(200)  

timer.profile举例, 8核cpu :

[root@db-172-16-3-39 ~]# stap -e 'probe timer.profile {printf("cpu:%d, ns:%d, execname:%s, pid:%d\n", cpu(), cpu_clock_ns(cpu()), execname(), pid())}'  
cpu:6, ns:1381147076271836282, execname:swapper, pid:0  
cpu:5, ns:1381147076272759039, execname:swapper, pid:0  
cpu:3, ns:1381147076272759641, execname:swapper, pid:0  
cpu:7, ns:1381147076272758724, execname:swapper, pid:0  
cpu:1, ns:1381147076272759556, execname:swapper, pid:0  
cpu:0, ns:1381147076272830348, execname:swapper, pid:0  
cpu:4, ns:1381147076272834135, execname:swapper, pid:0  
cpu:6, ns:1381147076272834370, execname:swapper, pid:0  
cpu:2, ns:1381147076272834220, execname:swapper, pid:0  
cpu:3, ns:1381147076273714522, execname:stapio, pid:18710  

时间探针支持的格式 :

timer.jiffies(N)[.randomize(M)]  
timer.s(N)[.randomize(M)]  
timer.ms(N)[.randomize(M)]  
timer.us(N)[.randomize(M)]  
timer.ns(N)[.randomize(M)]  
timer.hz(N)  
timer.profile  

参考

1. https://sourceware.org/systemtap/langref/Probe_points.html

2. http://blog.163.com/digoal@126/blog/static/163877040201062810748700/

3. https://sourceware.org/systemtap/tapsets/API-cpu-clock-ns.html

4. https://sourceware.org/systemtap/tapsets/timestamp_stp.html

Flag Counter

digoal’s 大量PostgreSQL文章入口