perf python scripting: Improve the syscalls-by-pid script
Arnaldo Carvalho de Melo [Mon, 25 Oct 2010 20:44:09 +0000 (18:44 -0200)]
. Print message at script start telling how to get te summary
. Print the syscall names
. Accept both pid (if numeric) or COMM name

Now it looks like this:

[root@emilia tmp]# perf trace syscall-counts-by-pid
Press control+C to stop and show the summary
^C
syscall events by comm/pid:

comm [pid]/syscalls                            count
----------------------------------------  ----------

automount [1670]
  futex                                            2

sshd [2322]
  rt_sigprocmask                                   4
  select                                           2
  write                                            1
  read                                             1

perf [15178]
  read                                          2506
  open                                           794
  close                                          769
  write                                          240
  getdents                                       112
  lseek                                           16
  stat                                             9
  perf_counter_open                                5
  fcntl                                            5
  mmap                                             5
  statfs                                           2

perf [15179]
  read                                         56701
  open                                           499
  stat                                           176
  fstat                                          149
  close                                          109
  mmap                                            98
  brk                                             75
  rt_sigaction                                    66
  munmap                                          42
  mprotect                                        24
  lstat                                            7
  lseek                                            5
  getdents                                         4
  ioctl                                            3
  readlink                                         2
  futex                                            1
  statfs                                           1
  getegid                                          1
  geteuid                                          1
  getgid                                           1
  getuid                                           1
  getrlimit                                        1
  fcntl                                            1
  uname                                            1
  write                                            1
[root@emilia tmp]# fg
-bash: fg: current: no such job
[root@emilia tmp]# perf trace syscall-counts-by-pid 2322
Press control+C to stop and show the summary
^C
syscall events by comm/pid:

comm [pid]/syscalls                            count
----------------------------------------  ----------

sshd [2322]
  rt_sigprocmask                                   4
  select                                           2
  write                                            1
  read                                             1
[root@emilia tmp]# perf trace syscall-counts-by-pid sshd
Press control+C to stop and show the summary
^C
syscall events for sshd:

comm [pid]/syscalls                            count
----------------------------------------  ----------

sshd [2322]
  rt_sigprocmask                                   4
  select                                           2
  write                                            1
  read                                             1
[root@emilia tmp]#

Cc: David S. Miller <davem@davemloft.net>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Tom Zanussi <tzanussi@gmail.com>
LKML-Reference: <new-submission>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

tools/perf/scripts/python/syscall-counts-by-pid.py

index af722d6..d1ee3ec 100644 (file)
@@ -5,29 +5,33 @@
 # Displays system-wide system call totals, broken down by syscall.
 # If a [comm] arg is specified, only syscalls called by [comm] are displayed.
 
-import os
-import sys
+import os, sys
 
 sys.path.append(os.environ['PERF_EXEC_PATH'] + \
        '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
 
 from perf_trace_context import *
 from Core import *
+from Util import syscall_name
 
 usage = "perf trace -s syscall-counts-by-pid.py [comm]\n";
 
 for_comm = None
+for_pid = None
 
 if len(sys.argv) > 2:
        sys.exit(usage)
 
 if len(sys.argv) > 1:
-       for_comm = sys.argv[1]
+       try:
+               for_pid = int(sys.argv[1])
+       except:
+               for_comm = sys.argv[1]
 
 syscalls = autodict()
 
 def trace_begin():
-       pass
+       print "Press control+C to stop and show the summary"
 
 def trace_end():
        print_syscall_totals()
@@ -35,9 +39,10 @@ def trace_end():
 def raw_syscalls__sys_enter(event_name, context, common_cpu,
        common_secs, common_nsecs, common_pid, common_comm,
        id, args):
-       if for_comm is not None:
-               if common_comm != for_comm:
-                       return
+
+       if (for_comm and common_comm != for_comm) or \
+          (for_pid  and common_pid  != for_pid ):
+               return
        try:
                syscalls[common_comm][common_pid][id] += 1
        except TypeError:
@@ -61,4 +66,4 @@ def print_syscall_totals():
                    id_keys = syscalls[comm][pid].keys()
                    for id, val in sorted(syscalls[comm][pid].iteritems(), \
                                  key = lambda(k, v): (v, k),  reverse = True):
-                           print "  %-38d  %10d\n" % (id, val),
+                           print "  %-38s  %10d\n" % (syscall_name(id), val),