14d161f03STom Zanussi# system call counts, by pid 24d161f03STom Zanussi# (c) 2010, Tom Zanussi <tzanussi@gmail.com> 34d161f03STom Zanussi# Licensed under the terms of the GNU GPL License version 2 44d161f03STom Zanussi# 54d161f03STom Zanussi# Displays system-wide system call totals, broken down by syscall. 64d161f03STom Zanussi# If a [comm] arg is specified, only syscalls called by [comm] are displayed. 74d161f03STom Zanussi 8de667cceSTony Jonesfrom __future__ import print_function 9de667cceSTony Jones 10a64fa198SArnaldo Carvalho de Meloimport os, sys 114d161f03STom Zanussi 124d161f03STom Zanussisys.path.append(os.environ['PERF_EXEC_PATH'] + \ 134d161f03STom Zanussi '/scripts/python/Perf-Trace-Util/lib/Perf/Trace') 144d161f03STom Zanussi 154d161f03STom Zanussifrom perf_trace_context import * 164d161f03STom Zanussifrom Core import * 17a64fa198SArnaldo Carvalho de Melofrom Util import syscall_name 184d161f03STom Zanussi 19765532c8SArnaldo Carvalho de Melousage = "perf script -s syscall-counts-by-pid.py [comm]\n"; 204d161f03STom Zanussi 214d161f03STom Zanussifor_comm = None 22a64fa198SArnaldo Carvalho de Melofor_pid = None 234d161f03STom Zanussi 244d161f03STom Zanussiif len(sys.argv) > 2: 254d161f03STom Zanussi sys.exit(usage) 264d161f03STom Zanussi 274d161f03STom Zanussiif len(sys.argv) > 1: 28a64fa198SArnaldo Carvalho de Melo try: 29a64fa198SArnaldo Carvalho de Melo for_pid = int(sys.argv[1]) 30a64fa198SArnaldo Carvalho de Melo except: 314d161f03STom Zanussi for_comm = sys.argv[1] 324d161f03STom Zanussi 334d161f03STom Zanussisyscalls = autodict() 344d161f03STom Zanussi 354d161f03STom Zanussidef trace_begin(): 36de667cceSTony Jones print("Press control+C to stop and show the summary") 374d161f03STom Zanussi 384d161f03STom Zanussidef trace_end(): 394d161f03STom Zanussi print_syscall_totals() 404d161f03STom Zanussi 414d161f03STom Zanussidef raw_syscalls__sys_enter(event_name, context, common_cpu, 424d161f03STom Zanussi common_secs, common_nsecs, common_pid, common_comm, 430f5f5bcdSJoseph Schuchart common_callchain, id, args): 44a64fa198SArnaldo Carvalho de Melo if (for_comm and common_comm != for_comm) or \ 45a64fa198SArnaldo Carvalho de Melo (for_pid and common_pid != for_pid ): 464d161f03STom Zanussi return 474d161f03STom Zanussi try: 484d161f03STom Zanussi syscalls[common_comm][common_pid][id] += 1 494d161f03STom Zanussi except TypeError: 504d161f03STom Zanussi syscalls[common_comm][common_pid][id] = 1 514d161f03STom Zanussi 5207100877SDaniel Bristot de Oliveiradef syscalls__sys_enter(event_name, context, common_cpu, 5307100877SDaniel Bristot de Oliveira common_secs, common_nsecs, common_pid, common_comm, 5407100877SDaniel Bristot de Oliveira id, args): 5507100877SDaniel Bristot de Oliveira raw_syscalls__sys_enter(**locals()) 5607100877SDaniel Bristot de Oliveira 574d161f03STom Zanussidef print_syscall_totals(): 584d161f03STom Zanussi if for_comm is not None: 59de667cceSTony Jones print("\nsyscall events for %s:\n" % (for_comm)) 604d161f03STom Zanussi else: 61de667cceSTony Jones print("\nsyscall events by comm/pid:\n") 624d161f03STom Zanussi 63de667cceSTony Jones print("%-40s %10s" % ("comm [pid]/syscalls", "count")) 64de667cceSTony Jones print("%-40s %10s" % ("----------------------------------------", 65de667cceSTony Jones "----------")) 664d161f03STom Zanussi 674d161f03STom Zanussi comm_keys = syscalls.keys() 684d161f03STom Zanussi for comm in comm_keys: 694d161f03STom Zanussi pid_keys = syscalls[comm].keys() 704d161f03STom Zanussi for pid in pid_keys: 71de667cceSTony Jones print("\n%s [%d]" % (comm, pid)) 724d161f03STom Zanussi id_keys = syscalls[comm][pid].keys() 73*b504d7f6STony Jones for id, val in sorted(syscalls[comm][pid].items(), 74de667cceSTony Jones key = lambda kv: (kv[1], kv[0]), reverse = True): 75de667cceSTony Jones print(" %-38s %10d" % (syscall_name(id), val)) 76