Systemtap(2.4) Example : array aggregate elements sorted by statistic operator (EXP. output TOPn IO processes)
背景
在Systemtap 2.x版本的man stap中看到这样一则说明
foreach (VAR in ARRAY [ limit EXP ]) STMT
Loop over each element of the named global array, assigning current key to VAR. The array may not be modified within the statement. By adding a single + or - operator after the VAR or the ARRAY identifier, the iteration will proceed in a sorted order, by ascending or descending index or value. If the array contains statistics aggregates, adding the desired @operator between the ARRAY identifier and the + or - will specify the sorting aggregate function. See the STATISTICS section below for the ones available. Default is @count. Using the optional limit keyword limits the number of loop iterations to EXP times. EXP is evaluated once at the beginning of the loop.
当数组中存储的元素为统计类型时, 使用foreach的sort功能, 可以选择按照统计类型的sum, avg, min, max, count进行排序.
这样就大大提高了可观性, 如果不指定排序操作符, 默认排序为@count.
例如, 我们每个几秒输出一次操作系统ioread的排名进程, 结束时输出中的ioread排名. 取前n名.
以下每隔3秒输出一次前5的ioread进程信息, 以及IO的字节数. 结束时输出总io前5总的进程信息以及字节数.
排序使用@sum.
[root@db-172-16-3-150 ~]# /opt/systemtap/bin/stap -e '
global var1%[60000], var2%[60000]
probe syscall.read {
var1[pid(),execname(),cmdline_str()] <<< $count
var2[pid(),execname(),cmdline_str()] <<< $count
}
probe timer.s($1) {
println("\npid, execname, cmdline_str, io_bytes\n----------------------")
foreach([x,y,z] in var1 @sum - limit $2)
printf("%d, %s, %s, %d\n", x,y,z,@sum(var1[x,y,z]))
delete var1
}
probe end {
println("\nEND\npid, execname, cmdline_str, io_bytes\n----------------------")
foreach([x,y,z] in var2 @sum - limit $2)
printf("%d, %s, %s, %d\n", x,y,z,@sum(var2[x,y,z]))
delete var1
delete var2
}' 3 5
输出结果 :
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 1982472
1494, rsyslogd, /sbin/rsyslogd -i /var/run/syslogd.pid -c 5, 4095
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
28931, sshd, sshd: root@pts/0 "", 32768
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
1523, irqbalance, irqbalance, 63488
28931, sshd, sshd: root@pts/0 "", 16384
22116, postgres, postgres: wal writer process "" "" "", 192
22115, postgres, postgres: writer process "" "" "" "" "" "" "", 32
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
28931, sshd, sshd: root@pts/0 "", 16384
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
28931, sshd, sshd: root@pts/0 "", 16384
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 1966080
28931, sshd, sshd: root@pts/0 "", 16384
22116, postgres, postgres: wal writer process "" "" "", 192
22115, postgres, postgres: writer process "" "" "" "" "" "" "", 32
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
1523, irqbalance, irqbalance, 63488
28931, sshd, sshd: root@pts/0 "", 16384
28823, pickup, pickup -l -t fifo -u, 1024
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
598, postgres, postgres: autovacuum worker process , 148480
598, postgres, postgres: autovacuum worker process digoal, 28672
28931, sshd, sshd: root@pts/0 "", 16384
22117, postgres, postgres: autovacuum launcher process , 12368
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
28931, sshd, sshd: root@pts/0 "", 49152
22116, postgres, postgres: wal writer process "" "" "", 192
22115, postgres, postgres: writer process "" "" "" "" "" "" "", 32
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
599, crond, crond, 138207
1523, irqbalance, irqbalance, 63488
600, sadc, /usr/lib64/sa/sadc -F -L -S DISK 1 1 -, 61716
1978, crond, crond, 20480
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
28931, sshd, sshd: root@pts/0 "", 16384
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
28931, sshd, sshd: root@pts/0 "", 16384
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
1523, irqbalance, irqbalance, 63488
28931, sshd, sshd: root@pts/0 "", 16384
22116, postgres, postgres: wal writer process "" "" "", 192
22115, postgres, postgres: writer process "" "" "" "" "" "" "", 32
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
28931, sshd, sshd: root@pts/0 "", 16384
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
28931, sshd, sshd: root@pts/0 "", 16384
1834, automount, automount --pid-file /var/run/autofs.pid, 6144
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
28931, sshd, sshd: root@pts/0 "", 32768
22116, postgres, postgres: wal writer process "" "" "", 192
22115, postgres, postgres: writer process "" "" "" "" "" "" "", 32
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
1523, irqbalance, irqbalance, 63488
28931, sshd, sshd: root@pts/0 "", 16384
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
28931, sshd, sshd: root@pts/0 "", 16384
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
28931, sshd, sshd: root@pts/0 "", 16384
22116, postgres, postgres: wal writer process "" "" "", 192
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 2097152
1523, irqbalance, irqbalance, 63488
28931, sshd, sshd: root@pts/0 "", 32768
1636, NetworkManager, NetworkManager --pid-file=/var/run/NetworkManager/NetworkManager.pid, 8192
1834, automount, automount --pid-file /var/run/autofs.pid, 6144
^C
END
pid, execname, cmdline_str, io_bytes
----------------------
595, stapio, /opt/systemtap/libexec/systemtap/stapio -R stap_3cd961dcade6503fded62967da1685de__595 -F3, 42090504
28931, sshd, sshd: root@pts/0 "", 425984
1523, irqbalance, irqbalance, 380928
598, postgres, postgres: autovacuum worker process , 148480
599, crond, crond, 138207
写的统计把syscall.read改成syscall.write就可以了.
参考
1. http://blog.163.com/digoal@126/blog/static/1638770402013999511424/
2. https://sourceware.org/systemtap/man/stap.1.html
foreach (VAR in ARRAY [ limit EXP ]) STMT
Loop over each element of the named global array,
assigning current key to VAR. The array may not be modified within the statement.
By adding a single + or - operator after the VAR or the ARRAY identifier,
the iteration will proceed in a sorted order, by ascending or descending
index or value. If the array contains statistics aggregates, adding the
desired @operator between the ARRAY identifier and the + or - will specify
the sorting aggregate function. See the STATISTICS section below for the ones
available. Default is @count. Using the optional limit keyword limits the number
of loop iterations to EXP times. EXP is evaluated once at the beginning of the loop.