1# system call top 2# (c) 2010, Tom Zanussi <tzanussi@gmail.com> 3# Licensed under the terms of the GNU GPL License version 2 4# 5# Periodically displays system-wide system call totals, broken down by 6# syscall. If a [comm] arg is specified, only syscalls called by 7# [comm] are displayed. If an [interval] arg is specified, the display 8# will be refreshed every [interval] seconds. The default interval is 9# 3 seconds. 10 11from __future__ import print_function 12 13import os, sys, time 14 15try: 16 import thread 17except ImportError: 18 import _thread as thread 19 20sys.path.append(os.environ['PERF_EXEC_PATH'] + \ 21 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace') 22 23from perf_trace_context import * 24from Core import * 25from Util import * 26 27usage = "perf script -s sctop.py [comm] [interval]\n"; 28 29for_comm = None 30default_interval = 3 31interval = default_interval 32 33if len(sys.argv) > 3: 34 sys.exit(usage) 35 36if len(sys.argv) > 2: 37 for_comm = sys.argv[1] 38 interval = int(sys.argv[2]) 39elif len(sys.argv) > 1: 40 try: 41 interval = int(sys.argv[1]) 42 except ValueError: 43 for_comm = sys.argv[1] 44 interval = default_interval 45 46syscalls = autodict() 47 48def trace_begin(): 49 thread.start_new_thread(print_syscall_totals, (interval,)) 50 pass 51 52def raw_syscalls__sys_enter(event_name, context, common_cpu, 53 common_secs, common_nsecs, common_pid, common_comm, 54 common_callchain, id, args): 55 if for_comm is not None: 56 if common_comm != for_comm: 57 return 58 try: 59 syscalls[id] += 1 60 except TypeError: 61 syscalls[id] = 1 62 63def syscalls__sys_enter(event_name, context, common_cpu, 64 common_secs, common_nsecs, common_pid, common_comm, 65 id, args): 66 raw_syscalls__sys_enter(**locals()) 67 68def print_syscall_totals(interval): 69 while 1: 70 clear_term() 71 if for_comm is not None: 72 print("\nsyscall events for %s:\n" % (for_comm)) 73 else: 74 print("\nsyscall events:\n") 75 76 print("%-40s %10s" % ("event", "count")) 77 print("%-40s %10s" % 78 ("----------------------------------------", 79 "----------")) 80 81 for id, val in sorted(syscalls.items(), key = lambda kv: (kv[1], kv[0]), \ 82 reverse = True): 83 try: 84 print("%-40s %10d" % (syscall_name(id), val)) 85 except TypeError: 86 pass 87 syscalls.clear() 88 time.sleep(interval) 89