Back to home page

OSCL-LXR

 
 

    


0001 # system call counts, by pid
0002 # (c) 2010, Tom Zanussi <tzanussi@gmail.com>
0003 # Licensed under the terms of the GNU GPL License version 2
0004 #
0005 # Displays system-wide system call totals, broken down by syscall.
0006 # If a [comm] arg is specified, only syscalls called by [comm] are displayed.
0007 
0008 from __future__ import print_function
0009 
0010 import os, sys
0011 
0012 sys.path.append(os.environ['PERF_EXEC_PATH'] + \
0013     '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
0014 
0015 from perf_trace_context import *
0016 from Core import *
0017 from Util import syscall_name
0018 
0019 usage = "perf script -s syscall-counts-by-pid.py [comm]\n";
0020 
0021 for_comm = None
0022 for_pid = None
0023 
0024 if len(sys.argv) > 2:
0025     sys.exit(usage)
0026 
0027 if len(sys.argv) > 1:
0028     try:
0029         for_pid = int(sys.argv[1])
0030     except:
0031         for_comm = sys.argv[1]
0032 
0033 syscalls = autodict()
0034 
0035 def trace_begin():
0036     print("Press control+C to stop and show the summary")
0037 
0038 def trace_end():
0039     print_syscall_totals()
0040 
0041 def raw_syscalls__sys_enter(event_name, context, common_cpu,
0042         common_secs, common_nsecs, common_pid, common_comm,
0043         common_callchain, id, args):
0044     if (for_comm and common_comm != for_comm) or \
0045         (for_pid and common_pid != for_pid ):
0046         return
0047     try:
0048         syscalls[common_comm][common_pid][id] += 1
0049     except TypeError:
0050         syscalls[common_comm][common_pid][id] = 1
0051 
0052 def syscalls__sys_enter(event_name, context, common_cpu,
0053         common_secs, common_nsecs, common_pid, common_comm,
0054         id, args):
0055     raw_syscalls__sys_enter(**locals())
0056 
0057 def print_syscall_totals():
0058     if for_comm is not None:
0059         print("\nsyscall events for %s:\n" % (for_comm))
0060     else:
0061         print("\nsyscall events by comm/pid:\n")
0062 
0063     print("%-40s  %10s" % ("comm [pid]/syscalls", "count"))
0064     print("%-40s  %10s" % ("----------------------------------------",
0065                 "----------"))
0066 
0067     comm_keys = syscalls.keys()
0068     for comm in comm_keys:
0069         pid_keys = syscalls[comm].keys()
0070         for pid in pid_keys:
0071             print("\n%s [%d]" % (comm, pid))
0072             id_keys = syscalls[comm][pid].keys()
0073             for id, val in sorted(syscalls[comm][pid].items(),
0074                 key = lambda kv: (kv[1], kv[0]), reverse = True):
0075                 print("  %-38s  %10d" % (syscall_name(id), val))