xref: /openbmc/linux/tools/perf/Documentation/perf-script-python.txt (revision 26ddb8722df865aa67fbe459107d2f3f8e5c6829)
1133dc4c3SIngo Molnarperf-script-python(1)
2133dc4c3SIngo Molnar====================
3133dc4c3SIngo Molnar
4133dc4c3SIngo MolnarNAME
5133dc4c3SIngo Molnar----
6133dc4c3SIngo Molnarperf-script-python - Process trace data with a Python script
7133dc4c3SIngo Molnar
8133dc4c3SIngo MolnarSYNOPSIS
9133dc4c3SIngo Molnar--------
10133dc4c3SIngo Molnar[verse]
11133dc4c3SIngo Molnar'perf script' [-s [Python]:script[.py] ]
12133dc4c3SIngo Molnar
13133dc4c3SIngo MolnarDESCRIPTION
14133dc4c3SIngo Molnar-----------
15133dc4c3SIngo Molnar
16133dc4c3SIngo MolnarThis perf script option is used to process perf script data using perf's
17133dc4c3SIngo Molnarbuilt-in Python interpreter.  It reads and processes the input file and
18133dc4c3SIngo Molnardisplays the results of the trace analysis implemented in the given
19133dc4c3SIngo MolnarPython script, if any.
20133dc4c3SIngo Molnar
21133dc4c3SIngo MolnarA QUICK EXAMPLE
22133dc4c3SIngo Molnar---------------
23133dc4c3SIngo Molnar
24133dc4c3SIngo MolnarThis section shows the process, start to finish, of creating a working
25133dc4c3SIngo MolnarPython script that aggregates and extracts useful information from a
26133dc4c3SIngo Molnarraw perf script stream.  You can avoid reading the rest of this
27133dc4c3SIngo Molnardocument if an example is enough for you; the rest of the document
28133dc4c3SIngo Molnarprovides more details on each step and lists the library functions
29133dc4c3SIngo Molnaravailable to script writers.
30133dc4c3SIngo Molnar
31133dc4c3SIngo MolnarThis example actually details the steps that were used to create the
32133dc4c3SIngo Molnar'syscall-counts' script you see when you list the available perf script
33133dc4c3SIngo Molnarscripts via 'perf script -l'.  As such, this script also shows how to
34133dc4c3SIngo Molnarintegrate your script into the list of general-purpose 'perf script'
35133dc4c3SIngo Molnarscripts listed by that command.
36133dc4c3SIngo Molnar
37133dc4c3SIngo MolnarThe syscall-counts script is a simple script, but demonstrates all the
38133dc4c3SIngo Molnarbasic ideas necessary to create a useful script.  Here's an example
39133dc4c3SIngo Molnarof its output (syscall names are not yet supported, they will appear
40133dc4c3SIngo Molnaras numbers):
41133dc4c3SIngo Molnar
42133dc4c3SIngo Molnar----
43133dc4c3SIngo Molnarsyscall events:
44133dc4c3SIngo Molnar
45133dc4c3SIngo Molnarevent                                          count
46133dc4c3SIngo Molnar----------------------------------------  -----------
47133dc4c3SIngo Molnarsys_write                                     455067
48133dc4c3SIngo Molnarsys_getdents                                    4072
49133dc4c3SIngo Molnarsys_close                                       3037
50133dc4c3SIngo Molnarsys_swapoff                                     1769
51133dc4c3SIngo Molnarsys_read                                         923
52133dc4c3SIngo Molnarsys_sched_setparam                               826
53133dc4c3SIngo Molnarsys_open                                         331
54133dc4c3SIngo Molnarsys_newfstat                                     326
55133dc4c3SIngo Molnarsys_mmap                                         217
56133dc4c3SIngo Molnarsys_munmap                                       216
57133dc4c3SIngo Molnarsys_futex                                        141
58133dc4c3SIngo Molnarsys_select                                       102
59133dc4c3SIngo Molnarsys_poll                                          84
60133dc4c3SIngo Molnarsys_setitimer                                     12
61133dc4c3SIngo Molnarsys_writev                                         8
62133dc4c3SIngo Molnar15                                                 8
63133dc4c3SIngo Molnarsys_lseek                                          7
64133dc4c3SIngo Molnarsys_rt_sigprocmask                                 6
65133dc4c3SIngo Molnarsys_wait4                                          3
66133dc4c3SIngo Molnarsys_ioctl                                          3
67133dc4c3SIngo Molnarsys_set_robust_list                                1
68133dc4c3SIngo Molnarsys_exit                                           1
69133dc4c3SIngo Molnar56                                                 1
70133dc4c3SIngo Molnarsys_access                                         1
71133dc4c3SIngo Molnar----
72133dc4c3SIngo Molnar
73133dc4c3SIngo MolnarBasically our task is to keep a per-syscall tally that gets updated
74133dc4c3SIngo Molnarevery time a system call occurs in the system.  Our script will do
75133dc4c3SIngo Molnarthat, but first we need to record the data that will be processed by
76133dc4c3SIngo Molnarthat script.  Theoretically, there are a couple of ways we could do
77133dc4c3SIngo Molnarthat:
78133dc4c3SIngo Molnar
79133dc4c3SIngo Molnar- we could enable every event under the tracing/events/syscalls
80133dc4c3SIngo Molnar  directory, but this is over 600 syscalls, well beyond the number
81133dc4c3SIngo Molnar  allowable by perf.  These individual syscall events will however be
82133dc4c3SIngo Molnar  useful if we want to later use the guidance we get from the
83133dc4c3SIngo Molnar  general-purpose scripts to drill down and get more detail about
84133dc4c3SIngo Molnar  individual syscalls of interest.
85133dc4c3SIngo Molnar
86133dc4c3SIngo Molnar- we can enable the sys_enter and/or sys_exit syscalls found under
87133dc4c3SIngo Molnar  tracing/events/raw_syscalls.  These are called for all syscalls; the
88133dc4c3SIngo Molnar  'id' field can be used to distinguish between individual syscall
89133dc4c3SIngo Molnar  numbers.
90133dc4c3SIngo Molnar
91133dc4c3SIngo MolnarFor this script, we only need to know that a syscall was entered; we
92133dc4c3SIngo Molnardon't care how it exited, so we'll use 'perf record' to record only
93133dc4c3SIngo Molnarthe sys_enter events:
94133dc4c3SIngo Molnar
95133dc4c3SIngo Molnar----
96133dc4c3SIngo Molnar# perf record -a -e raw_syscalls:sys_enter
97133dc4c3SIngo Molnar
98133dc4c3SIngo Molnar^C[ perf record: Woken up 1 times to write data ]
99133dc4c3SIngo Molnar[ perf record: Captured and wrote 56.545 MB perf.data (~2470503 samples) ]
100133dc4c3SIngo Molnar----
101133dc4c3SIngo Molnar
102133dc4c3SIngo MolnarThe options basically say to collect data for every syscall event
103133dc4c3SIngo Molnarsystem-wide and multiplex the per-cpu output into a single stream.
104133dc4c3SIngo MolnarThat single stream will be recorded in a file in the current directory
105133dc4c3SIngo Molnarcalled perf.data.
106133dc4c3SIngo Molnar
107133dc4c3SIngo MolnarOnce we have a perf.data file containing our data, we can use the -g
108133dc4c3SIngo Molnar'perf script' option to generate a Python script that will contain a
109133dc4c3SIngo Molnarcallback handler for each event type found in the perf.data trace
110133dc4c3SIngo Molnarstream (for more details, see the STARTER SCRIPTS section).
111133dc4c3SIngo Molnar
112133dc4c3SIngo Molnar----
113133dc4c3SIngo Molnar# perf script -g python
114133dc4c3SIngo Molnargenerated Python script: perf-script.py
115133dc4c3SIngo Molnar
116133dc4c3SIngo MolnarThe output file created also in the current directory is named
117133dc4c3SIngo Molnarperf-script.py.  Here's the file in its entirety:
118133dc4c3SIngo Molnar
119133dc4c3SIngo Molnar# perf script event handlers, generated by perf script -g python
120133dc4c3SIngo Molnar# Licensed under the terms of the GNU GPL License version 2
121133dc4c3SIngo Molnar
122133dc4c3SIngo Molnar# The common_* event handler fields are the most useful fields common to
123133dc4c3SIngo Molnar# all events.  They don't necessarily correspond to the 'common_*' fields
124133dc4c3SIngo Molnar# in the format files.  Those fields not available as handler params can
125133dc4c3SIngo Molnar# be retrieved using Python functions of the form common_*(context).
126133dc4c3SIngo Molnar# See the perf-script-python Documentation for the list of available functions.
127133dc4c3SIngo Molnar
128133dc4c3SIngo Molnarimport os
129133dc4c3SIngo Molnarimport sys
130133dc4c3SIngo Molnar
131133dc4c3SIngo Molnarsys.path.append(os.environ['PERF_EXEC_PATH'] + \
132e8d0f400SDavid Ahern	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
133133dc4c3SIngo Molnar
134133dc4c3SIngo Molnarfrom perf_trace_context import *
135133dc4c3SIngo Molnarfrom Core import *
136133dc4c3SIngo Molnar
137133dc4c3SIngo Molnardef trace_begin():
138133dc4c3SIngo Molnar	print "in trace_begin"
139133dc4c3SIngo Molnar
140133dc4c3SIngo Molnardef trace_end():
141133dc4c3SIngo Molnar	print "in trace_end"
142133dc4c3SIngo Molnar
143133dc4c3SIngo Molnardef raw_syscalls__sys_enter(event_name, context, common_cpu,
144133dc4c3SIngo Molnar	common_secs, common_nsecs, common_pid, common_comm,
145133dc4c3SIngo Molnar	id, args):
146133dc4c3SIngo Molnar		print_header(event_name, common_cpu, common_secs, common_nsecs,
147133dc4c3SIngo Molnar			common_pid, common_comm)
148133dc4c3SIngo Molnar
149133dc4c3SIngo Molnar		print "id=%d, args=%s\n" % \
150133dc4c3SIngo Molnar		(id, args),
151133dc4c3SIngo Molnar
152133dc4c3SIngo Molnardef trace_unhandled(event_name, context, common_cpu, common_secs, common_nsecs,
153133dc4c3SIngo Molnar		common_pid, common_comm):
154133dc4c3SIngo Molnar		print_header(event_name, common_cpu, common_secs, common_nsecs,
155133dc4c3SIngo Molnar		common_pid, common_comm)
156133dc4c3SIngo Molnar
157133dc4c3SIngo Molnardef print_header(event_name, cpu, secs, nsecs, pid, comm):
158133dc4c3SIngo Molnar	print "%-20s %5u %05u.%09u %8u %-20s " % \
159133dc4c3SIngo Molnar	(event_name, cpu, secs, nsecs, pid, comm),
160133dc4c3SIngo Molnar----
161133dc4c3SIngo Molnar
162133dc4c3SIngo MolnarAt the top is a comment block followed by some import statements and a
163133dc4c3SIngo Molnarpath append which every perf script script should include.
164133dc4c3SIngo Molnar
165133dc4c3SIngo MolnarFollowing that are a couple generated functions, trace_begin() and
166133dc4c3SIngo Molnartrace_end(), which are called at the beginning and the end of the
167133dc4c3SIngo Molnarscript respectively (for more details, see the SCRIPT_LAYOUT section
168133dc4c3SIngo Molnarbelow).
169133dc4c3SIngo Molnar
170133dc4c3SIngo MolnarFollowing those are the 'event handler' functions generated one for
171133dc4c3SIngo Molnarevery event in the 'perf record' output.  The handler functions take
172133dc4c3SIngo Molnarthe form subsystem__event_name, and contain named parameters, one for
173133dc4c3SIngo Molnareach field in the event; in this case, there's only one event,
174133dc4c3SIngo Molnarraw_syscalls__sys_enter().  (see the EVENT HANDLERS section below for
175133dc4c3SIngo Molnarmore info on event handlers).
176133dc4c3SIngo Molnar
177133dc4c3SIngo MolnarThe final couple of functions are, like the begin and end functions,
178133dc4c3SIngo Molnargenerated for every script.  The first, trace_unhandled(), is called
179133dc4c3SIngo Molnarevery time the script finds an event in the perf.data file that
180133dc4c3SIngo Molnardoesn't correspond to any event handler in the script.  This could
181133dc4c3SIngo Molnarmean either that the record step recorded event types that it wasn't
182133dc4c3SIngo Molnarreally interested in, or the script was run against a trace file that
183133dc4c3SIngo Molnardoesn't correspond to the script.
184133dc4c3SIngo Molnar
185133dc4c3SIngo MolnarThe script generated by -g option simply prints a line for each
186133dc4c3SIngo Molnarevent found in the trace stream i.e. it basically just dumps the event
187133dc4c3SIngo Molnarand its parameter values to stdout.  The print_header() function is
188133dc4c3SIngo Molnarsimply a utility function used for that purpose.  Let's rename the
189133dc4c3SIngo Molnarscript and run it to see the default output:
190133dc4c3SIngo Molnar
191133dc4c3SIngo Molnar----
192133dc4c3SIngo Molnar# mv perf-script.py syscall-counts.py
193133dc4c3SIngo Molnar# perf script -s syscall-counts.py
194133dc4c3SIngo Molnar
195133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847582083     7506 perf                  id=1, args=
196133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847595764     7506 perf                  id=1, args=
197133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847620860     7506 perf                  id=1, args=
198133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847710478     6533 npviewer.bin          id=78, args=
199133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847719204     6533 npviewer.bin          id=142, args=
200133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847755445     6533 npviewer.bin          id=3, args=
201133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847775601     6533 npviewer.bin          id=3, args=
202133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847781820     6533 npviewer.bin          id=3, args=
203133dc4c3SIngo Molnar.
204133dc4c3SIngo Molnar.
205133dc4c3SIngo Molnar.
206133dc4c3SIngo Molnar----
207133dc4c3SIngo Molnar
208133dc4c3SIngo MolnarOf course, for this script, we're not interested in printing every
209133dc4c3SIngo Molnartrace event, but rather aggregating it in a useful way.  So we'll get
210133dc4c3SIngo Molnarrid of everything to do with printing as well as the trace_begin() and
211133dc4c3SIngo Molnartrace_unhandled() functions, which we won't be using.  That leaves us
212133dc4c3SIngo Molnarwith this minimalistic skeleton:
213133dc4c3SIngo Molnar
214133dc4c3SIngo Molnar----
215133dc4c3SIngo Molnarimport os
216133dc4c3SIngo Molnarimport sys
217133dc4c3SIngo Molnar
218133dc4c3SIngo Molnarsys.path.append(os.environ['PERF_EXEC_PATH'] + \
219e8d0f400SDavid Ahern	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
220133dc4c3SIngo Molnar
221133dc4c3SIngo Molnarfrom perf_trace_context import *
222133dc4c3SIngo Molnarfrom Core import *
223133dc4c3SIngo Molnar
224133dc4c3SIngo Molnardef trace_end():
225133dc4c3SIngo Molnar	print "in trace_end"
226133dc4c3SIngo Molnar
227133dc4c3SIngo Molnardef raw_syscalls__sys_enter(event_name, context, common_cpu,
228133dc4c3SIngo Molnar	common_secs, common_nsecs, common_pid, common_comm,
229133dc4c3SIngo Molnar	id, args):
230133dc4c3SIngo Molnar----
231133dc4c3SIngo Molnar
232133dc4c3SIngo MolnarIn trace_end(), we'll simply print the results, but first we need to
233133dc4c3SIngo Molnargenerate some results to print.  To do that we need to have our
234133dc4c3SIngo Molnarsys_enter() handler do the necessary tallying until all events have
235133dc4c3SIngo Molnarbeen counted.  A hash table indexed by syscall id is a good way to
236133dc4c3SIngo Molnarstore that information; every time the sys_enter() handler is called,
237133dc4c3SIngo Molnarwe simply increment a count associated with that hash entry indexed by
238133dc4c3SIngo Molnarthat syscall id:
239133dc4c3SIngo Molnar
240133dc4c3SIngo Molnar----
241133dc4c3SIngo Molnar  syscalls = autodict()
242133dc4c3SIngo Molnar
243133dc4c3SIngo Molnar  try:
244133dc4c3SIngo Molnar    syscalls[id] += 1
245133dc4c3SIngo Molnar  except TypeError:
246133dc4c3SIngo Molnar    syscalls[id] = 1
247133dc4c3SIngo Molnar----
248133dc4c3SIngo Molnar
249133dc4c3SIngo MolnarThe syscalls 'autodict' object is a special kind of Python dictionary
250133dc4c3SIngo Molnar(implemented in Core.py) that implements Perl's 'autovivifying' hashes
251133dc4c3SIngo Molnarin Python i.e. with autovivifying hashes, you can assign nested hash
252133dc4c3SIngo Molnarvalues without having to go to the trouble of creating intermediate
253133dc4c3SIngo Molnarlevels if they don't exist e.g syscalls[comm][pid][id] = 1 will create
254133dc4c3SIngo Molnarthe intermediate hash levels and finally assign the value 1 to the
255133dc4c3SIngo Molnarhash entry for 'id' (because the value being assigned isn't a hash
256133dc4c3SIngo Molnarobject itself, the initial value is assigned in the TypeError
257133dc4c3SIngo Molnarexception.  Well, there may be a better way to do this in Python but
258133dc4c3SIngo Molnarthat's what works for now).
259133dc4c3SIngo Molnar
260133dc4c3SIngo MolnarPutting that code into the raw_syscalls__sys_enter() handler, we
261133dc4c3SIngo Molnareffectively end up with a single-level dictionary keyed on syscall id
262133dc4c3SIngo Molnarand having the counts we've tallied as values.
263133dc4c3SIngo Molnar
264133dc4c3SIngo MolnarThe print_syscall_totals() function iterates over the entries in the
265133dc4c3SIngo Molnardictionary and displays a line for each entry containing the syscall
26696355f2cSMasanari Iidaname (the dictionary keys contain the syscall ids, which are passed to
267133dc4c3SIngo Molnarthe Util function syscall_name(), which translates the raw syscall
268133dc4c3SIngo Molnarnumbers to the corresponding syscall name strings).  The output is
269133dc4c3SIngo Molnardisplayed after all the events in the trace have been processed, by
270133dc4c3SIngo Molnarcalling the print_syscall_totals() function from the trace_end()
271133dc4c3SIngo Molnarhandler called at the end of script processing.
272133dc4c3SIngo Molnar
273133dc4c3SIngo MolnarThe final script producing the output shown above is shown in its
274133dc4c3SIngo Molnarentirety below (syscall_name() helper is not yet available, you can
275133dc4c3SIngo Molnaronly deal with id's for now):
276133dc4c3SIngo Molnar
277133dc4c3SIngo Molnar----
278133dc4c3SIngo Molnarimport os
279133dc4c3SIngo Molnarimport sys
280133dc4c3SIngo Molnar
281133dc4c3SIngo Molnarsys.path.append(os.environ['PERF_EXEC_PATH'] + \
282e8d0f400SDavid Ahern	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
283133dc4c3SIngo Molnar
284133dc4c3SIngo Molnarfrom perf_trace_context import *
285133dc4c3SIngo Molnarfrom Core import *
286133dc4c3SIngo Molnarfrom Util import *
287133dc4c3SIngo Molnar
288133dc4c3SIngo Molnarsyscalls = autodict()
289133dc4c3SIngo Molnar
290133dc4c3SIngo Molnardef trace_end():
291133dc4c3SIngo Molnar	print_syscall_totals()
292133dc4c3SIngo Molnar
293133dc4c3SIngo Molnardef raw_syscalls__sys_enter(event_name, context, common_cpu,
294133dc4c3SIngo Molnar	common_secs, common_nsecs, common_pid, common_comm,
295133dc4c3SIngo Molnar	id, args):
296133dc4c3SIngo Molnar	try:
297133dc4c3SIngo Molnar		syscalls[id] += 1
298133dc4c3SIngo Molnar	except TypeError:
299133dc4c3SIngo Molnar		syscalls[id] = 1
300133dc4c3SIngo Molnar
301133dc4c3SIngo Molnardef print_syscall_totals():
302133dc4c3SIngo Molnar    if for_comm is not None:
303133dc4c3SIngo Molnar	    print "\nsyscall events for %s:\n\n" % (for_comm),
304133dc4c3SIngo Molnar    else:
305133dc4c3SIngo Molnar	    print "\nsyscall events:\n\n",
306133dc4c3SIngo Molnar
307133dc4c3SIngo Molnar    print "%-40s  %10s\n" % ("event", "count"),
308133dc4c3SIngo Molnar    print "%-40s  %10s\n" % ("----------------------------------------", \
309133dc4c3SIngo Molnar                                 "-----------"),
310133dc4c3SIngo Molnar
311133dc4c3SIngo Molnar    for id, val in sorted(syscalls.iteritems(), key = lambda(k, v): (v, k), \
312133dc4c3SIngo Molnar				  reverse = True):
313133dc4c3SIngo Molnar	    print "%-40s  %10d\n" % (syscall_name(id), val),
314133dc4c3SIngo Molnar----
315133dc4c3SIngo Molnar
316133dc4c3SIngo MolnarThe script can be run just as before:
317133dc4c3SIngo Molnar
318133dc4c3SIngo Molnar  # perf script -s syscall-counts.py
319133dc4c3SIngo Molnar
320133dc4c3SIngo MolnarSo those are the essential steps in writing and running a script.  The
321133dc4c3SIngo Molnarprocess can be generalized to any tracepoint or set of tracepoints
322133dc4c3SIngo Molnaryou're interested in - basically find the tracepoint(s) you're
323133dc4c3SIngo Molnarinterested in by looking at the list of available events shown by
32434d4453dSSeongJae Park'perf list' and/or look in /sys/kernel/debug/tracing/events/ for
325133dc4c3SIngo Molnardetailed event and field info, record the corresponding trace data
326133dc4c3SIngo Molnarusing 'perf record', passing it the list of interesting events,
327133dc4c3SIngo Molnargenerate a skeleton script using 'perf script -g python' and modify the
328133dc4c3SIngo Molnarcode to aggregate and display it for your particular needs.
329133dc4c3SIngo Molnar
330133dc4c3SIngo MolnarAfter you've done that you may end up with a general-purpose script
331133dc4c3SIngo Molnarthat you want to keep around and have available for future use.  By
332133dc4c3SIngo Molnarwriting a couple of very simple shell scripts and putting them in the
333133dc4c3SIngo Molnarright place, you can have your script listed alongside the other
334133dc4c3SIngo Molnarscripts listed by the 'perf script -l' command e.g.:
335133dc4c3SIngo Molnar
336133dc4c3SIngo Molnar----
337133dc4c3SIngo Molnarroot@tropicana:~# perf script -l
338133dc4c3SIngo MolnarList of available trace scripts:
339133dc4c3SIngo Molnar  wakeup-latency                       system-wide min/max/avg wakeup latency
340133dc4c3SIngo Molnar  rw-by-file <comm>                    r/w activity for a program, by file
341133dc4c3SIngo Molnar  rw-by-pid                            system-wide r/w activity
342133dc4c3SIngo Molnar----
343133dc4c3SIngo Molnar
344133dc4c3SIngo MolnarA nice side effect of doing this is that you also then capture the
345133dc4c3SIngo Molnarprobably lengthy 'perf record' command needed to record the events for
346133dc4c3SIngo Molnarthe script.
347133dc4c3SIngo Molnar
348133dc4c3SIngo MolnarTo have the script appear as a 'built-in' script, you write two simple
349133dc4c3SIngo Molnarscripts, one for recording and one for 'reporting'.
350133dc4c3SIngo Molnar
351133dc4c3SIngo MolnarThe 'record' script is a shell script with the same base name as your
352133dc4c3SIngo Molnarscript, but with -record appended.  The shell script should be put
353133dc4c3SIngo Molnarinto the perf/scripts/python/bin directory in the kernel source tree.
354133dc4c3SIngo MolnarIn that script, you write the 'perf record' command-line needed for
355133dc4c3SIngo Molnaryour script:
356133dc4c3SIngo Molnar
357133dc4c3SIngo Molnar----
358133dc4c3SIngo Molnar# cat kernel-source/tools/perf/scripts/python/bin/syscall-counts-record
359133dc4c3SIngo Molnar
360133dc4c3SIngo Molnar#!/bin/bash
361133dc4c3SIngo Molnarperf record -a -e raw_syscalls:sys_enter
362133dc4c3SIngo Molnar----
363133dc4c3SIngo Molnar
364133dc4c3SIngo MolnarThe 'report' script is also a shell script with the same base name as
365133dc4c3SIngo Molnaryour script, but with -report appended.  It should also be located in
366133dc4c3SIngo Molnarthe perf/scripts/python/bin directory.  In that script, you write the
367133dc4c3SIngo Molnar'perf script -s' command-line needed for running your script:
368133dc4c3SIngo Molnar
369133dc4c3SIngo Molnar----
370133dc4c3SIngo Molnar# cat kernel-source/tools/perf/scripts/python/bin/syscall-counts-report
371133dc4c3SIngo Molnar
372133dc4c3SIngo Molnar#!/bin/bash
373133dc4c3SIngo Molnar# description: system-wide syscall counts
374133dc4c3SIngo Molnarperf script -s ~/libexec/perf-core/scripts/python/syscall-counts.py
375133dc4c3SIngo Molnar----
376133dc4c3SIngo Molnar
377133dc4c3SIngo MolnarNote that the location of the Python script given in the shell script
378133dc4c3SIngo Molnaris in the libexec/perf-core/scripts/python directory - this is where
379133dc4c3SIngo Molnarthe script will be copied by 'make install' when you install perf.
380133dc4c3SIngo MolnarFor the installation to install your script there, your script needs
381133dc4c3SIngo Molnarto be located in the perf/scripts/python directory in the kernel
382133dc4c3SIngo Molnarsource tree:
383133dc4c3SIngo Molnar
384133dc4c3SIngo Molnar----
385133dc4c3SIngo Molnar# ls -al kernel-source/tools/perf/scripts/python
386133dc4c3SIngo Molnar
387133dc4c3SIngo Molnarroot@tropicana:/home/trz/src/tip# ls -al tools/perf/scripts/python
388133dc4c3SIngo Molnartotal 32
389133dc4c3SIngo Molnardrwxr-xr-x 4 trz trz 4096 2010-01-26 22:30 .
390133dc4c3SIngo Molnardrwxr-xr-x 4 trz trz 4096 2010-01-26 22:29 ..
391133dc4c3SIngo Molnardrwxr-xr-x 2 trz trz 4096 2010-01-26 22:29 bin
392133dc4c3SIngo Molnar-rw-r--r-- 1 trz trz 2548 2010-01-26 22:29 check-perf-script.py
393e8d0f400SDavid Aherndrwxr-xr-x 3 trz trz 4096 2010-01-26 22:49 Perf-Trace-Util
394133dc4c3SIngo Molnar-rw-r--r-- 1 trz trz 1462 2010-01-26 22:30 syscall-counts.py
395133dc4c3SIngo Molnar----
396133dc4c3SIngo Molnar
397133dc4c3SIngo MolnarOnce you've done that (don't forget to do a new 'make install',
398133dc4c3SIngo Molnarotherwise your script won't show up at run-time), 'perf script -l'
399133dc4c3SIngo Molnarshould show a new entry for your script:
400133dc4c3SIngo Molnar
401133dc4c3SIngo Molnar----
402133dc4c3SIngo Molnarroot@tropicana:~# perf script -l
403133dc4c3SIngo MolnarList of available trace scripts:
404133dc4c3SIngo Molnar  wakeup-latency                       system-wide min/max/avg wakeup latency
405133dc4c3SIngo Molnar  rw-by-file <comm>                    r/w activity for a program, by file
406133dc4c3SIngo Molnar  rw-by-pid                            system-wide r/w activity
407133dc4c3SIngo Molnar  syscall-counts                       system-wide syscall counts
408133dc4c3SIngo Molnar----
409133dc4c3SIngo Molnar
410133dc4c3SIngo MolnarYou can now perform the record step via 'perf script record':
411133dc4c3SIngo Molnar
412133dc4c3SIngo Molnar  # perf script record syscall-counts
413133dc4c3SIngo Molnar
414133dc4c3SIngo Molnarand display the output using 'perf script report':
415133dc4c3SIngo Molnar
416133dc4c3SIngo Molnar  # perf script report syscall-counts
417133dc4c3SIngo Molnar
418133dc4c3SIngo MolnarSTARTER SCRIPTS
419133dc4c3SIngo Molnar---------------
420133dc4c3SIngo Molnar
421133dc4c3SIngo MolnarYou can quickly get started writing a script for a particular set of
422133dc4c3SIngo Molnartrace data by generating a skeleton script using 'perf script -g
423133dc4c3SIngo Molnarpython' in the same directory as an existing perf.data trace file.
424133dc4c3SIngo MolnarThat will generate a starter script containing a handler for each of
425133dc4c3SIngo Molnarthe event types in the trace file; it simply prints every available
426133dc4c3SIngo Molnarfield for each event in the trace file.
427133dc4c3SIngo Molnar
428133dc4c3SIngo MolnarYou can also look at the existing scripts in
429133dc4c3SIngo Molnar~/libexec/perf-core/scripts/python for typical examples showing how to
430133dc4c3SIngo Molnardo basic things like aggregate event data, print results, etc.  Also,
431133dc4c3SIngo Molnarthe check-perf-script.py script, while not interesting for its results,
432133dc4c3SIngo Molnarattempts to exercise all of the main scripting features.
433133dc4c3SIngo Molnar
434133dc4c3SIngo MolnarEVENT HANDLERS
435133dc4c3SIngo Molnar--------------
436133dc4c3SIngo Molnar
437133dc4c3SIngo MolnarWhen perf script is invoked using a trace script, a user-defined
438133dc4c3SIngo Molnar'handler function' is called for each event in the trace.  If there's
439133dc4c3SIngo Molnarno handler function defined for a given event type, the event is
44034d4453dSSeongJae Parkignored (or passed to a 'trace_unhandled' function, see below) and the
441133dc4c3SIngo Molnarnext event is processed.
442133dc4c3SIngo Molnar
443133dc4c3SIngo MolnarMost of the event's field values are passed as arguments to the
444133dc4c3SIngo Molnarhandler function; some of the less common ones aren't - those are
445133dc4c3SIngo Molnaravailable as calls back into the perf executable (see below).
446133dc4c3SIngo Molnar
447133dc4c3SIngo MolnarAs an example, the following perf record command can be used to record
448133dc4c3SIngo Molnarall sched_wakeup events in the system:
449133dc4c3SIngo Molnar
450133dc4c3SIngo Molnar # perf record -a -e sched:sched_wakeup
451133dc4c3SIngo Molnar
452133dc4c3SIngo MolnarTraces meant to be processed using a script should be recorded with
453133dc4c3SIngo Molnarthe above option: -a to enable system-wide collection.
454133dc4c3SIngo Molnar
455133dc4c3SIngo MolnarThe format file for the sched_wakep event defines the following fields
456133dc4c3SIngo Molnar(see /sys/kernel/debug/tracing/events/sched/sched_wakeup/format):
457133dc4c3SIngo Molnar
458133dc4c3SIngo Molnar----
459133dc4c3SIngo Molnar format:
460133dc4c3SIngo Molnar        field:unsigned short common_type;
461133dc4c3SIngo Molnar        field:unsigned char common_flags;
462133dc4c3SIngo Molnar        field:unsigned char common_preempt_count;
463133dc4c3SIngo Molnar        field:int common_pid;
464133dc4c3SIngo Molnar
465133dc4c3SIngo Molnar        field:char comm[TASK_COMM_LEN];
466133dc4c3SIngo Molnar        field:pid_t pid;
467133dc4c3SIngo Molnar        field:int prio;
468133dc4c3SIngo Molnar        field:int success;
469133dc4c3SIngo Molnar        field:int target_cpu;
470133dc4c3SIngo Molnar----
471133dc4c3SIngo Molnar
472133dc4c3SIngo MolnarThe handler function for this event would be defined as:
473133dc4c3SIngo Molnar
474133dc4c3SIngo Molnar----
475133dc4c3SIngo Molnardef sched__sched_wakeup(event_name, context, common_cpu, common_secs,
476133dc4c3SIngo Molnar       common_nsecs, common_pid, common_comm,
477133dc4c3SIngo Molnar       comm, pid, prio, success, target_cpu):
478133dc4c3SIngo Molnar       pass
479133dc4c3SIngo Molnar----
480133dc4c3SIngo Molnar
481133dc4c3SIngo MolnarThe handler function takes the form subsystem__event_name.
482133dc4c3SIngo Molnar
483133dc4c3SIngo MolnarThe common_* arguments in the handler's argument list are the set of
484133dc4c3SIngo Molnararguments passed to all event handlers; some of the fields correspond
485133dc4c3SIngo Molnarto the common_* fields in the format file, but some are synthesized,
486133dc4c3SIngo Molnarand some of the common_* fields aren't common enough to to be passed
487133dc4c3SIngo Molnarto every event as arguments but are available as library functions.
488133dc4c3SIngo Molnar
489133dc4c3SIngo MolnarHere's a brief description of each of the invariant event args:
490133dc4c3SIngo Molnar
491133dc4c3SIngo Molnar event_name 	  	    the name of the event as text
492133dc4c3SIngo Molnar context		    an opaque 'cookie' used in calls back into perf
493133dc4c3SIngo Molnar common_cpu		    the cpu the event occurred on
494133dc4c3SIngo Molnar common_secs		    the secs portion of the event timestamp
495133dc4c3SIngo Molnar common_nsecs		    the nsecs portion of the event timestamp
496133dc4c3SIngo Molnar common_pid		    the pid of the current task
497133dc4c3SIngo Molnar common_comm		    the name of the current process
498133dc4c3SIngo Molnar
499133dc4c3SIngo MolnarAll of the remaining fields in the event's format file have
500133dc4c3SIngo Molnarcounterparts as handler function arguments of the same name, as can be
501133dc4c3SIngo Molnarseen in the example above.
502133dc4c3SIngo Molnar
503133dc4c3SIngo MolnarThe above provides the basics needed to directly access every field of
504133dc4c3SIngo Molnarevery event in a trace, which covers 90% of what you need to know to
505133dc4c3SIngo Molnarwrite a useful trace script.  The sections below cover the rest.
506133dc4c3SIngo Molnar
507133dc4c3SIngo MolnarSCRIPT LAYOUT
508133dc4c3SIngo Molnar-------------
509133dc4c3SIngo Molnar
510133dc4c3SIngo MolnarEvery perf script Python script should start by setting up a Python
511133dc4c3SIngo Molnarmodule search path and 'import'ing a few support modules (see module
512133dc4c3SIngo Molnardescriptions below):
513133dc4c3SIngo Molnar
514133dc4c3SIngo Molnar----
515133dc4c3SIngo Molnar import os
516133dc4c3SIngo Molnar import sys
517133dc4c3SIngo Molnar
518133dc4c3SIngo Molnar sys.path.append(os.environ['PERF_EXEC_PATH'] + \
519e8d0f400SDavid Ahern	      '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
520133dc4c3SIngo Molnar
521133dc4c3SIngo Molnar from perf_trace_context import *
522133dc4c3SIngo Molnar from Core import *
523133dc4c3SIngo Molnar----
524133dc4c3SIngo Molnar
525133dc4c3SIngo MolnarThe rest of the script can contain handler functions and support
526133dc4c3SIngo Molnarfunctions in any order.
527133dc4c3SIngo Molnar
528133dc4c3SIngo MolnarAside from the event handler functions discussed above, every script
529133dc4c3SIngo Molnarcan implement a set of optional functions:
530133dc4c3SIngo Molnar
531133dc4c3SIngo Molnar*trace_begin*, if defined, is called before any event is processed and
532133dc4c3SIngo Molnargives scripts a chance to do setup tasks:
533133dc4c3SIngo Molnar
534133dc4c3SIngo Molnar----
535*26ddb872SSeongJae Parkdef trace_begin():
536133dc4c3SIngo Molnar    pass
537133dc4c3SIngo Molnar----
538133dc4c3SIngo Molnar
539133dc4c3SIngo Molnar*trace_end*, if defined, is called after all events have been
540133dc4c3SIngo Molnar processed and gives scripts a chance to do end-of-script tasks, such
541133dc4c3SIngo Molnar as display results:
542133dc4c3SIngo Molnar
543133dc4c3SIngo Molnar----
544*26ddb872SSeongJae Parkdef trace_end():
545133dc4c3SIngo Molnar    pass
546133dc4c3SIngo Molnar----
547133dc4c3SIngo Molnar
548133dc4c3SIngo Molnar*trace_unhandled*, if defined, is called after for any event that
549133dc4c3SIngo Molnar doesn't have a handler explicitly defined for it.  The standard set
550133dc4c3SIngo Molnar of common arguments are passed into it:
551133dc4c3SIngo Molnar
552133dc4c3SIngo Molnar----
553133dc4c3SIngo Molnardef trace_unhandled(event_name, context, common_cpu, common_secs,
554133dc4c3SIngo Molnar        common_nsecs, common_pid, common_comm):
555133dc4c3SIngo Molnar    pass
556133dc4c3SIngo Molnar----
557133dc4c3SIngo Molnar
558133dc4c3SIngo MolnarThe remaining sections provide descriptions of each of the available
559133dc4c3SIngo Molnarbuilt-in perf script Python modules and their associated functions.
560133dc4c3SIngo Molnar
561133dc4c3SIngo MolnarAVAILABLE MODULES AND FUNCTIONS
562133dc4c3SIngo Molnar-------------------------------
563133dc4c3SIngo Molnar
564133dc4c3SIngo MolnarThe following sections describe the functions and variables available
565133dc4c3SIngo Molnarvia the various perf script Python modules.  To use the functions and
566133dc4c3SIngo Molnarvariables from the given module, add the corresponding 'from XXXX
567133dc4c3SIngo Molnarimport' line to your perf script script.
568133dc4c3SIngo Molnar
569133dc4c3SIngo MolnarCore.py Module
570133dc4c3SIngo Molnar~~~~~~~~~~~~~~
571133dc4c3SIngo Molnar
572133dc4c3SIngo MolnarThese functions provide some essential functions to user scripts.
573133dc4c3SIngo Molnar
574133dc4c3SIngo MolnarThe *flag_str* and *symbol_str* functions provide human-readable
575133dc4c3SIngo Molnarstrings for flag and symbolic fields.  These correspond to the strings
576133dc4c3SIngo Molnarand values parsed from the 'print fmt' fields of the event format
577133dc4c3SIngo Molnarfiles:
578133dc4c3SIngo Molnar
57996355f2cSMasanari Iida  flag_str(event_name, field_name, field_value) - returns the string representation corresponding to field_value for the flag field field_name of event event_name
58096355f2cSMasanari Iida  symbol_str(event_name, field_name, field_value) - returns the string representation corresponding to field_value for the symbolic field field_name of event event_name
581133dc4c3SIngo Molnar
582133dc4c3SIngo MolnarThe *autodict* function returns a special kind of Python
583133dc4c3SIngo Molnardictionary that implements Perl's 'autovivifying' hashes in Python
584133dc4c3SIngo Molnari.e. with autovivifying hashes, you can assign nested hash values
585133dc4c3SIngo Molnarwithout having to go to the trouble of creating intermediate levels if
586133dc4c3SIngo Molnarthey don't exist.
587133dc4c3SIngo Molnar
588133dc4c3SIngo Molnar  autodict() - returns an autovivifying dictionary instance
589133dc4c3SIngo Molnar
590133dc4c3SIngo Molnar
591133dc4c3SIngo Molnarperf_trace_context Module
592133dc4c3SIngo Molnar~~~~~~~~~~~~~~~~~~~~~~~~~
593133dc4c3SIngo Molnar
594133dc4c3SIngo MolnarSome of the 'common' fields in the event format file aren't all that
595133dc4c3SIngo Molnarcommon, but need to be made accessible to user scripts nonetheless.
596133dc4c3SIngo Molnar
597133dc4c3SIngo Molnarperf_trace_context defines a set of functions that can be used to
598133dc4c3SIngo Molnaraccess this data in the context of the current event.  Each of these
599133dc4c3SIngo Molnarfunctions expects a context variable, which is the same as the
600133dc4c3SIngo Molnarcontext variable passed into every event handler as the second
601133dc4c3SIngo Molnarargument.
602133dc4c3SIngo Molnar
603133dc4c3SIngo Molnar common_pc(context) - returns common_preempt count for the current event
604133dc4c3SIngo Molnar common_flags(context) - returns common_flags for the current event
605133dc4c3SIngo Molnar common_lock_depth(context) - returns common_lock_depth for the current event
606133dc4c3SIngo Molnar
607133dc4c3SIngo MolnarUtil.py Module
608133dc4c3SIngo Molnar~~~~~~~~~~~~~~
609133dc4c3SIngo Molnar
610133dc4c3SIngo MolnarVarious utility functions for use with perf script:
611133dc4c3SIngo Molnar
612133dc4c3SIngo Molnar  nsecs(secs, nsecs) - returns total nsecs given secs/nsecs pair
613133dc4c3SIngo Molnar  nsecs_secs(nsecs) - returns whole secs portion given nsecs
614133dc4c3SIngo Molnar  nsecs_nsecs(nsecs) - returns nsecs remainder given nsecs
615133dc4c3SIngo Molnar  nsecs_str(nsecs) - returns printable string in the form secs.nsecs
616133dc4c3SIngo Molnar  avg(total, n) - returns average given a sum and a total number of values
617133dc4c3SIngo Molnar
618133dc4c3SIngo MolnarSEE ALSO
619133dc4c3SIngo Molnar--------
620133dc4c3SIngo Molnarlinkperf:perf-script[1]
621