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