xref: /OK3568_Linux_fs/kernel/tools/perf/scripts/python/failed-syscalls-by-pid.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun# failed system call counts, by pid
2*4882a593Smuzhiyun# (c) 2010, Tom Zanussi <tzanussi@gmail.com>
3*4882a593Smuzhiyun# Licensed under the terms of the GNU GPL License version 2
4*4882a593Smuzhiyun#
5*4882a593Smuzhiyun# Displays system-wide failed system call totals, broken down by pid.
6*4882a593Smuzhiyun# If a [comm] arg is specified, only syscalls called by [comm] are displayed.
7*4882a593Smuzhiyun
8*4882a593Smuzhiyunfrom __future__ import print_function
9*4882a593Smuzhiyun
10*4882a593Smuzhiyunimport os
11*4882a593Smuzhiyunimport sys
12*4882a593Smuzhiyun
13*4882a593Smuzhiyunsys.path.append(os.environ['PERF_EXEC_PATH'] + \
14*4882a593Smuzhiyun	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
15*4882a593Smuzhiyun
16*4882a593Smuzhiyunfrom perf_trace_context import *
17*4882a593Smuzhiyunfrom Core import *
18*4882a593Smuzhiyunfrom Util import *
19*4882a593Smuzhiyun
20*4882a593Smuzhiyunusage = "perf script -s syscall-counts-by-pid.py [comm|pid]\n";
21*4882a593Smuzhiyun
22*4882a593Smuzhiyunfor_comm = None
23*4882a593Smuzhiyunfor_pid = None
24*4882a593Smuzhiyun
25*4882a593Smuzhiyunif len(sys.argv) > 2:
26*4882a593Smuzhiyun	sys.exit(usage)
27*4882a593Smuzhiyun
28*4882a593Smuzhiyunif len(sys.argv) > 1:
29*4882a593Smuzhiyun	try:
30*4882a593Smuzhiyun		for_pid = int(sys.argv[1])
31*4882a593Smuzhiyun	except:
32*4882a593Smuzhiyun		for_comm = sys.argv[1]
33*4882a593Smuzhiyun
34*4882a593Smuzhiyunsyscalls = autodict()
35*4882a593Smuzhiyun
36*4882a593Smuzhiyundef trace_begin():
37*4882a593Smuzhiyun	print("Press control+C to stop and show the summary")
38*4882a593Smuzhiyun
39*4882a593Smuzhiyundef trace_end():
40*4882a593Smuzhiyun	print_error_totals()
41*4882a593Smuzhiyun
42*4882a593Smuzhiyundef raw_syscalls__sys_exit(event_name, context, common_cpu,
43*4882a593Smuzhiyun	common_secs, common_nsecs, common_pid, common_comm,
44*4882a593Smuzhiyun	common_callchain, id, ret):
45*4882a593Smuzhiyun	if (for_comm and common_comm != for_comm) or \
46*4882a593Smuzhiyun	   (for_pid  and common_pid  != for_pid ):
47*4882a593Smuzhiyun		return
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun	if ret < 0:
50*4882a593Smuzhiyun		try:
51*4882a593Smuzhiyun			syscalls[common_comm][common_pid][id][ret] += 1
52*4882a593Smuzhiyun		except TypeError:
53*4882a593Smuzhiyun			syscalls[common_comm][common_pid][id][ret] = 1
54*4882a593Smuzhiyun
55*4882a593Smuzhiyundef syscalls__sys_exit(event_name, context, common_cpu,
56*4882a593Smuzhiyun	common_secs, common_nsecs, common_pid, common_comm,
57*4882a593Smuzhiyun	id, ret):
58*4882a593Smuzhiyun	raw_syscalls__sys_exit(**locals())
59*4882a593Smuzhiyun
60*4882a593Smuzhiyundef print_error_totals():
61*4882a593Smuzhiyun	if for_comm is not None:
62*4882a593Smuzhiyun		print("\nsyscall errors for %s:\n" % (for_comm))
63*4882a593Smuzhiyun	else:
64*4882a593Smuzhiyun		print("\nsyscall errors:\n")
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun	print("%-30s  %10s" % ("comm [pid]", "count"))
67*4882a593Smuzhiyun	print("%-30s  %10s" % ("------------------------------", "----------"))
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun	comm_keys = syscalls.keys()
70*4882a593Smuzhiyun	for comm in comm_keys:
71*4882a593Smuzhiyun		pid_keys = syscalls[comm].keys()
72*4882a593Smuzhiyun		for pid in pid_keys:
73*4882a593Smuzhiyun			print("\n%s [%d]" % (comm, pid))
74*4882a593Smuzhiyun			id_keys = syscalls[comm][pid].keys()
75*4882a593Smuzhiyun			for id in id_keys:
76*4882a593Smuzhiyun				print("  syscall: %-16s" % syscall_name(id))
77*4882a593Smuzhiyun				ret_keys = syscalls[comm][pid][id].keys()
78*4882a593Smuzhiyun				for ret, val in sorted(syscalls[comm][pid][id].items(), key = lambda kv: (kv[1], kv[0]), reverse = True):
79*4882a593Smuzhiyun					print("    err = %-20s  %10d" % (strerror(ret), val))
80