xref: /linux-6.15/tools/perf/scripts/python/sctop.py (revision 47902f36)
1*47902f36STom Zanussi# system call top
2*47902f36STom Zanussi# (c) 2010, Tom Zanussi <[email protected]>
3*47902f36STom Zanussi# Licensed under the terms of the GNU GPL License version 2
4*47902f36STom Zanussi#
5*47902f36STom Zanussi# Periodically displays system-wide system call totals, broken down by
6*47902f36STom Zanussi# syscall.  If a [comm] arg is specified, only syscalls called by
7*47902f36STom Zanussi# [comm] are displayed. If an [interval] arg is specified, the display
8*47902f36STom Zanussi# will be refreshed every [interval] seconds.  The default interval is
9*47902f36STom Zanussi# 3 seconds.
10*47902f36STom Zanussi
11*47902f36STom Zanussiimport thread
12*47902f36STom Zanussiimport time
13*47902f36STom Zanussiimport os
14*47902f36STom Zanussiimport sys
15*47902f36STom Zanussi
16*47902f36STom Zanussisys.path.append(os.environ['PERF_EXEC_PATH'] + \
17*47902f36STom Zanussi	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
18*47902f36STom Zanussi
19*47902f36STom Zanussifrom perf_trace_context import *
20*47902f36STom Zanussifrom Core import *
21*47902f36STom Zanussifrom Util import *
22*47902f36STom Zanussi
23*47902f36STom Zanussiusage = "perf trace -s syscall-counts.py [comm] [interval]\n";
24*47902f36STom Zanussi
25*47902f36STom Zanussifor_comm = None
26*47902f36STom Zanussidefault_interval = 3
27*47902f36STom Zanussiinterval = default_interval
28*47902f36STom Zanussi
29*47902f36STom Zanussiif len(sys.argv) > 3:
30*47902f36STom Zanussi	sys.exit(usage)
31*47902f36STom Zanussi
32*47902f36STom Zanussiif len(sys.argv) > 2:
33*47902f36STom Zanussi	for_comm = sys.argv[1]
34*47902f36STom Zanussi	interval = int(sys.argv[2])
35*47902f36STom Zanussielif len(sys.argv) > 1:
36*47902f36STom Zanussi	try:
37*47902f36STom Zanussi		interval = int(sys.argv[1])
38*47902f36STom Zanussi	except ValueError:
39*47902f36STom Zanussi		for_comm = sys.argv[1]
40*47902f36STom Zanussi		interval = default_interval
41*47902f36STom Zanussi
42*47902f36STom Zanussisyscalls = autodict()
43*47902f36STom Zanussi
44*47902f36STom Zanussidef trace_begin():
45*47902f36STom Zanussi	thread.start_new_thread(print_syscall_totals, (interval,))
46*47902f36STom Zanussi	pass
47*47902f36STom Zanussi
48*47902f36STom Zanussidef raw_syscalls__sys_enter(event_name, context, common_cpu,
49*47902f36STom Zanussi	common_secs, common_nsecs, common_pid, common_comm,
50*47902f36STom Zanussi	id, args):
51*47902f36STom Zanussi	if for_comm is not None:
52*47902f36STom Zanussi		if common_comm != for_comm:
53*47902f36STom Zanussi			return
54*47902f36STom Zanussi	try:
55*47902f36STom Zanussi		syscalls[id] += 1
56*47902f36STom Zanussi	except TypeError:
57*47902f36STom Zanussi		syscalls[id] = 1
58*47902f36STom Zanussi
59*47902f36STom Zanussidef print_syscall_totals(interval):
60*47902f36STom Zanussi	while 1:
61*47902f36STom Zanussi		clear_term()
62*47902f36STom Zanussi		if for_comm is not None:
63*47902f36STom Zanussi			print "\nsyscall events for %s:\n\n" % (for_comm),
64*47902f36STom Zanussi		else:
65*47902f36STom Zanussi			print "\nsyscall events:\n\n",
66*47902f36STom Zanussi
67*47902f36STom Zanussi		print "%-40s  %10s\n" % ("event", "count"),
68*47902f36STom Zanussi		print "%-40s  %10s\n" % ("----------------------------------------", \
69*47902f36STom Zanussi						 "----------"),
70*47902f36STom Zanussi
71*47902f36STom Zanussi		for id, val in sorted(syscalls.iteritems(), key = lambda(k, v): (v, k), \
72*47902f36STom Zanussi					      reverse = True):
73*47902f36STom Zanussi			try:
74*47902f36STom Zanussi				print "%-40d  %10d\n" % (id, val),
75*47902f36STom Zanussi			except TypeError:
76*47902f36STom Zanussi				pass
77*47902f36STom Zanussi		syscalls.clear()
78*47902f36STom Zanussi		time.sleep(interval)
79