xref: /linux-6.15/tools/perf/scripts/python/sctop.py (revision 2e7d1e3f)
147902f36STom Zanussi# system call top
247902f36STom Zanussi# (c) 2010, Tom Zanussi <[email protected]>
347902f36STom Zanussi# Licensed under the terms of the GNU GPL License version 2
447902f36STom Zanussi#
547902f36STom Zanussi# Periodically displays system-wide system call totals, broken down by
647902f36STom Zanussi# syscall.  If a [comm] arg is specified, only syscalls called by
747902f36STom Zanussi# [comm] are displayed. If an [interval] arg is specified, the display
847902f36STom Zanussi# will be refreshed every [interval] seconds.  The default interval is
947902f36STom Zanussi# 3 seconds.
1047902f36STom Zanussi
11*2e7d1e3fSArnaldo Carvalho de Meloimport os, sys, thread, time
1247902f36STom Zanussi
1347902f36STom Zanussisys.path.append(os.environ['PERF_EXEC_PATH'] + \
1447902f36STom Zanussi	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
1547902f36STom Zanussi
1647902f36STom Zanussifrom perf_trace_context import *
1747902f36STom Zanussifrom Core import *
1847902f36STom Zanussifrom Util import *
1947902f36STom Zanussi
2047902f36STom Zanussiusage = "perf trace -s syscall-counts.py [comm] [interval]\n";
2147902f36STom Zanussi
2247902f36STom Zanussifor_comm = None
2347902f36STom Zanussidefault_interval = 3
2447902f36STom Zanussiinterval = default_interval
2547902f36STom Zanussi
2647902f36STom Zanussiif len(sys.argv) > 3:
2747902f36STom Zanussi	sys.exit(usage)
2847902f36STom Zanussi
2947902f36STom Zanussiif len(sys.argv) > 2:
3047902f36STom Zanussi	for_comm = sys.argv[1]
3147902f36STom Zanussi	interval = int(sys.argv[2])
3247902f36STom Zanussielif len(sys.argv) > 1:
3347902f36STom Zanussi	try:
3447902f36STom Zanussi		interval = int(sys.argv[1])
3547902f36STom Zanussi	except ValueError:
3647902f36STom Zanussi		for_comm = sys.argv[1]
3747902f36STom Zanussi		interval = default_interval
3847902f36STom Zanussi
3947902f36STom Zanussisyscalls = autodict()
4047902f36STom Zanussi
4147902f36STom Zanussidef trace_begin():
4247902f36STom Zanussi	thread.start_new_thread(print_syscall_totals, (interval,))
4347902f36STom Zanussi	pass
4447902f36STom Zanussi
4547902f36STom Zanussidef raw_syscalls__sys_enter(event_name, context, common_cpu,
4647902f36STom Zanussi	common_secs, common_nsecs, common_pid, common_comm,
4747902f36STom Zanussi	id, args):
4847902f36STom Zanussi	if for_comm is not None:
4947902f36STom Zanussi		if common_comm != for_comm:
5047902f36STom Zanussi			return
5147902f36STom Zanussi	try:
5247902f36STom Zanussi		syscalls[id] += 1
5347902f36STom Zanussi	except TypeError:
5447902f36STom Zanussi		syscalls[id] = 1
5547902f36STom Zanussi
5647902f36STom Zanussidef print_syscall_totals(interval):
5747902f36STom Zanussi	while 1:
5847902f36STom Zanussi		clear_term()
5947902f36STom Zanussi		if for_comm is not None:
6047902f36STom Zanussi			print "\nsyscall events for %s:\n\n" % (for_comm),
6147902f36STom Zanussi		else:
6247902f36STom Zanussi			print "\nsyscall events:\n\n",
6347902f36STom Zanussi
6447902f36STom Zanussi		print "%-40s  %10s\n" % ("event", "count"),
6547902f36STom Zanussi		print "%-40s  %10s\n" % ("----------------------------------------", \
6647902f36STom Zanussi						 "----------"),
6747902f36STom Zanussi
6847902f36STom Zanussi		for id, val in sorted(syscalls.iteritems(), key = lambda(k, v): (v, k), \
6947902f36STom Zanussi					      reverse = True):
7047902f36STom Zanussi			try:
71*2e7d1e3fSArnaldo Carvalho de Melo				print "%-40s  %10d\n" % (syscall_name(id), val),
7247902f36STom Zanussi			except TypeError:
7347902f36STom Zanussi				pass
7447902f36STom Zanussi		syscalls.clear()
7547902f36STom Zanussi		time.sleep(interval)
76