Systemtap statistics type example

1 minute read

背景

systemtap统计数据类型, 同array一样, 必须定义为global变量. ( 换句话说, 统计元素可以存储在全局变量(scalar)或者 全局array中. )

Aggregate instances are used to collect statistics on numerical values, when it is important to accumulate new data quickly and in large volume. These instances operate without exclusive locks, and store only aggregated stream statistics. Aggregates make sense only for global variables. They are stored individually or as elements of an associative array.

相比array的好处是, 操作时不需要exclusive lock. 在执行统计时处理所有数据.

For each instance of a distinct extraction function operating on a given identifier, the translator computes a set of statistics. With each execution of an extraction function, the aggregation is computed for that moment across all processors. The first argument of each function is the same style of l-value as used on the left side of the aggregation operation.

统计类型可以想象成存储一串整型, 目前统计类型支持count, max, min, avg, sum这种统计操作. 同时还支持柱状图输出, hist_linear(自定义low, high和width), hist_log(2^n次方) .

例如 :

[root@db-172-16-3-39 ~]# vi test.stp   
global var  
probe begin {  
  var <<< 12  
  var <<< 12  
  var <<< 33  
  var <<< 44  
  var <<< 55  
  var <<< 16  
  printf("count, min, max, avg, sum\n")  
  printf("%d, %d, %d, %d, %d\n", @count(var), @min(var), @max(var), @avg(var), @sum(var))  
  //print(@hist_linear(var,0,100,10))  
  print(@hist_log(var))  
  exit()  
}  

输出

[root@db-172-16-3-39 ~]# stap test.stp   
count, min, max, avg, sum  
6, 12, 55, 28, 172  
value |-------------------------------------------------- count  
    2 |                                                   0  -- >=2 && <4 的元素有多少个.      
    4 |                                                   0  -- >=4 && <8 的元素有多少个.      
    8 |@@                                                 2  -- >=8 && <16 的元素有多少个.     
   16 |@                                                  1  -- >=16 && <32 的元素有多少个.    
   32 |@@@                                                3  -- >=32 && <64 的元素有多少个.    
   64 |                                                   0  -- >=64 && <128 的元素有多少个.   
  128 |                                                   0  -- >=128的元素有多少个.  

稍微解释一下

count : 存储的元素个数.  
min : 所有元素中的最小值.  
max : 所有元素中的最大值.  
avg : 平均值  
sum : 总和  
hist_log : 以2^n次方为动态width. 输出每个bucket中元素的个数. @表示有元素, ~表示没有元素(中间省略的bucket),   

自定义width, 使用hist_linear :

[root@db-172-16-3-39 ~]# cat test.stp   
global var  
probe begin {  
  var <<< 12  
  var <<< 12  
  var <<< 33  
  var <<< 44  
  var <<< 55  
  var <<< 16  
  printf("count, min, max, avg, sum\n")  
  printf("%d, %d, %d, %d, %d\n", @count(var), @min(var), @max(var), @avg(var), @sum(var))  
  print(@hist_linear(var,0,1000,10))  
  //print(@hist_log(var))  
  exit()  
}  

输出 :

[root@db-172-16-3-39 ~]# stap test.stp   
count, min, max, avg, sum  
6, 12, 55, 28, 172  
value |-------------------------------------------------- count  
    0 |                                                   0  
   10 |@@@                                                3  
   20 |                                                   0  
   30 |@                                                  1  
   40 |@                                                  1  
   50 |@                                                  1  
   60 |                                                   0  
   70 |                                                   0  

解释 :

print(@hist_linear(var,0,1000,10))  
lower=0  
high=1000  
width=10  

那么一共有101个bucket.

参考

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

2. https://sourceware.org/systemtap/tutorial/Analysis.html

Flag Counter

digoal’s 大量PostgreSQL文章入口