History

perf trace/scripting: Add rwtop and sctop scripts

A couple of scripts, one in Python and the other in Perl, that demonstrate 'live mode' tracing. For each, the output of the perf event stream is fed continuously to the script, which continuously aggregates the data and reports the current results every 3 seconds, or at the optionally specified interval. After the current results are displayed, the aggregations are cleared and the cycle begins anew. To run the scripts, simply pipe the output of the 'perf trace record' step as input to the corresponding 'perf trace report' step, using '-' as the filename to -o and -i: $ perf trace record sctop -o - | perf trace report sctop -i - Also adds clear_term() utility functions to the Util.pm and Util.py utility modules, for use by any script to clear the screen.

Tom Zanussi committed 16 years ago

Commit 47902f31 parent c7929e48 files · +315 −0Browse files

Showing the changes to tools/perf/scripts/python (4 of 8 files). Show all

Unified · Split

4 files
  1. tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Util.py +3 −0
  2. tools/perf/scripts/python/bin/sctop-record +2 −0
  3. tools/perf/scripts/python/bin/sctop-report +24 −0
  4. tools/perf/scripts/python/sctop.py +78 −0
tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Util.py+3−0
@@ -23,3 +23,6 @@ def nsecs_nsecs(nsecs): · 22 unmodified lines
2323 def nsecs_str(nsecs):
2424 str = "%5u.%09u" % (nsecs_secs(nsecs), nsecs_nsecs(nsecs)),
2525 return str
26+
27+def clear_term():
28+ print("\x1b[H\x1b[2J")
tools/perf/scripts/python/bin/sctop-record+2−0
@@ -0,0 +1,2 @@
1+#!/bin/bash
2+perf record -c 1 -f -a -M -R -e raw_syscalls:sys_enter $@
tools/perf/scripts/python/bin/sctop-report+24−0
@@ -0,0 +1,24 @@
1+#!/bin/bash
2+# description: syscall top
3+# args: [comm] [interval]
4+n_args=0
5+for i in "$@"
6+do
7+ if expr match "$i" "-" > /dev/null ; then
8+ break
9+ fi
10+ n_args=$(( $n_args + 1 ))
11+done
12+if [ "$n_args" -gt 2 ] ; then
13+ echo "usage: sctop-report [comm] [interval]"
14+ exit
15+fi
16+if [ "$n_args" -gt 1 ] ; then
17+ comm=$1
18+ interval=$2
19+ shift 2
20+elif [ "$n_args" -gt 0 ] ; then
21+ interval=$1
22+ shift
23+fi
24+perf trace $@ -s ~/libexec/perf-core/scripts/python/sctop.py $comm $interval
tools/perf/scripts/python/sctop.py+78−0
@@ -0,0 +1,78 @@
1+# system call top
2+# (c) 2010, Tom Zanussi <tzanussi@gmail.com>
3+# Licensed under the terms of the GNU GPL License version 2
4+#
5+# Periodically displays system-wide system call totals, broken down by
6+# syscall. If a [comm] arg is specified, only syscalls called by
7+# [comm] are displayed. If an [interval] arg is specified, the display
8+# will be refreshed every [interval] seconds. The default interval is
9+# 3 seconds.
10+
11+import thread
12+import time
13+import os
14+import sys
15+
16+sys.path.append(os.environ['PERF_EXEC_PATH'] + \
17+ '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
18+
19+from perf_trace_context import *
20+from Core import *
21+from Util import *
22+
23+usage = "perf trace -s syscall-counts.py [comm] [interval]\n";
24+
25+for_comm = None
26+default_interval = 3
27+interval = default_interval
28+
29+if len(sys.argv) > 3:
30+ sys.exit(usage)
31+
32+if len(sys.argv) > 2:
33+ for_comm = sys.argv[1]
34+ interval = int(sys.argv[2])
35+elif len(sys.argv) > 1:
36+ try:
37+ interval = int(sys.argv[1])
38+ except ValueError:
39+ for_comm = sys.argv[1]
40+ interval = default_interval
41+
42+syscalls = autodict()
43+
44+def trace_begin():
45+ thread.start_new_thread(print_syscall_totals, (interval,))
46+ pass
47+
48+def raw_syscalls__sys_enter(event_name, context, common_cpu,
49+ common_secs, common_nsecs, common_pid, common_comm,
50+ id, args):
51+ if for_comm is not None:
52+ if common_comm != for_comm:
53+ return
54+ try:
55+ syscalls[id] += 1
56+ except TypeError:
57+ syscalls[id] = 1
58+
59+def print_syscall_totals(interval):
60+ while 1:
61+ clear_term()
62+ if for_comm is not None:
63+ print "\nsyscall events for %s:\n\n" % (for_comm),
64+ else:
65+ print "\nsyscall events:\n\n",
66+
67+ print "%-40s %10s\n" % ("event", "count"),
68+ print "%-40s %10s\n" % ("----------------------------------------", \
69+ "----------"),
70+
71+ for id, val in sorted(syscalls.iteritems(), key = lambda(k, v): (v, k), \
72+ reverse = True):
73+ try:
74+ print "%-40d %10d\n" % (id, val),
75+ except TypeError:
76+ pass
77+ syscalls.clear()
78+ time.sleep(interval)