Systemtap examples, Profiling - 6 Tracking System Call Volume Per Process

1 minute read

背景

例子来自syscalls_by_pid_procs.stp 脚本, 用以跟踪系统中所有的系统调用, 输出系统调用数前5的进程信息, 以及系统调用次数, 系统调用名.

在前面的例子中讲过按照系统调用次数排序输出,

http://blog.163.com/digoal@126/blog/static/163877040201310203393080/

选取某些系统调用输出等待的系统调用排名以及进程信息.

http://blog.163.com/digoal@126/blog/static/163877040201310201070658/

修改后的脚本内容以及注解 :   
[root@db-172-16-3-150 network]# cd /usr/share/systemtap/testsuite/systemtap.examples/profiling  
vi syscalls_by_pid_procs.stp  
#! /usr/bin/env stap  
  
# Copyright (C) 2006 IBM Corp.  
#  
# This file is part of systemtap, and is free software.  You can  
# redistribute it and/or modify it under the terms of the GNU General  
# Public License (GPL); either version 2, or (at your option) any  
# later version.  
# modified by digoal.  
  
#  
# Print the system call count by process name in descending order.  
#  
  
global syscalls1[10240]  // 存储进程相关的系统调用次数  
global syscalls2[10240]  // 存储进程, 以及系统调用名相关的系统调用次数  
  
probe syscall.* {  
  syscalls1[pid(),execname()]++  
  syscalls2[pid(),execname(),name]++  
}  
  
probe timer.s($1) {  
  ansi_clear_screen()  
  printf ("%-20s %-15s %-15s %-15s %-15s\n", "#ProcTotalSysCalls", "#ThisSysCalls", "SysCallsName", "#PID", "Process Name")  
  foreach ([pid,proc] in syscalls1- limit 5) {  
    foreach ([a,b,c] in syscalls2-) {  
      if (pid == a && proc == b)  
        printf("%-20d %-15d %-15s %-15d %-15s\n", syscalls1[pid,proc], syscalls2[pid,proc,c], c, pid, proc)  
    }  
  }  
  delete syscalls1  
  delete syscalls2  
}  
// #ProcTotalSysCalls   #ThisSysCalls   SysCallsName    #PID            Process Name     
// 该进程的所有系统调用次数, 此行系统调用次数, 此行系统调用名, 进程id, 进程名  
  
执行输出举例 :   
[root@db-172-16-3-150 profiling]# stap --vp 10000 syscalls_by_pid_procs.stp 5  
#ProcTotalSysCalls   #ThisSysCalls   SysCallsName    #PID            Process Name     
390                  62              open            1653            irqbalance       
390                  62              read            1653            irqbalance       
390                  53              fstat           1653            irqbalance       
390                  53              mmap            1653            irqbalance       
390                  53              mmap2           1653            irqbalance       
390                  53              close           1653            irqbalance       
390                  53              munmap          1653            irqbalance       
390                  1               nanosleep       1653            irqbalance       
130                  50              write           1778            avahi-daemon     
130                  25              read            1778            avahi-daemon     
130                  25              poll            1778            avahi-daemon     
130                  15              ioctl           1778            avahi-daemon     
130                  15              recvmsg         1778            avahi-daemon     
52                   26              read            22429           stapio           
52                   25              ppoll           22429           stapio           
52                   1               write           22429           stapio           
40                   20              read            7121            postgres         
40                   20              poll            7121            postgres         
40                   20              read            7120            postgres         
40                   20              poll            7120            postgres   

参考

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

Flag Counter

digoal’s 大量PostgreSQL文章入口