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