xref: /openbmc/linux/tools/perf/Documentation/perf-script-python.txt (revision 1bf8d5a4a5da19b1f6e7958fe67db4118fa7a1c1)
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
152*1bf8d5a4SSeongJae Parkdef trace_unhandled(event_name, context, event_fields_dict):
153*1bf8d5a4SSeongJae Park		print ' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())])
154133dc4c3SIngo Molnar
155133dc4c3SIngo Molnardef print_header(event_name, cpu, secs, nsecs, pid, comm):
156133dc4c3SIngo Molnar	print "%-20s %5u %05u.%09u %8u %-20s " % \
157133dc4c3SIngo Molnar	(event_name, cpu, secs, nsecs, pid, comm),
158133dc4c3SIngo Molnar----
159133dc4c3SIngo Molnar
160133dc4c3SIngo MolnarAt the top is a comment block followed by some import statements and a
161133dc4c3SIngo Molnarpath append which every perf script script should include.
162133dc4c3SIngo Molnar
163133dc4c3SIngo MolnarFollowing that are a couple generated functions, trace_begin() and
164133dc4c3SIngo Molnartrace_end(), which are called at the beginning and the end of the
165133dc4c3SIngo Molnarscript respectively (for more details, see the SCRIPT_LAYOUT section
166133dc4c3SIngo Molnarbelow).
167133dc4c3SIngo Molnar
168133dc4c3SIngo MolnarFollowing those are the 'event handler' functions generated one for
169133dc4c3SIngo Molnarevery event in the 'perf record' output.  The handler functions take
170133dc4c3SIngo Molnarthe form subsystem__event_name, and contain named parameters, one for
171133dc4c3SIngo Molnareach field in the event; in this case, there's only one event,
172133dc4c3SIngo Molnarraw_syscalls__sys_enter().  (see the EVENT HANDLERS section below for
173133dc4c3SIngo Molnarmore info on event handlers).
174133dc4c3SIngo Molnar
175133dc4c3SIngo MolnarThe final couple of functions are, like the begin and end functions,
176133dc4c3SIngo Molnargenerated for every script.  The first, trace_unhandled(), is called
177133dc4c3SIngo Molnarevery time the script finds an event in the perf.data file that
178133dc4c3SIngo Molnardoesn't correspond to any event handler in the script.  This could
179133dc4c3SIngo Molnarmean either that the record step recorded event types that it wasn't
180133dc4c3SIngo Molnarreally interested in, or the script was run against a trace file that
181133dc4c3SIngo Molnardoesn't correspond to the script.
182133dc4c3SIngo Molnar
183133dc4c3SIngo MolnarThe script generated by -g option simply prints a line for each
184133dc4c3SIngo Molnarevent found in the trace stream i.e. it basically just dumps the event
185133dc4c3SIngo Molnarand its parameter values to stdout.  The print_header() function is
186133dc4c3SIngo Molnarsimply a utility function used for that purpose.  Let's rename the
187133dc4c3SIngo Molnarscript and run it to see the default output:
188133dc4c3SIngo Molnar
189133dc4c3SIngo Molnar----
190133dc4c3SIngo Molnar# mv perf-script.py syscall-counts.py
191133dc4c3SIngo Molnar# perf script -s syscall-counts.py
192133dc4c3SIngo Molnar
193133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847582083     7506 perf                  id=1, args=
194133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847595764     7506 perf                  id=1, args=
195133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847620860     7506 perf                  id=1, args=
196133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847710478     6533 npviewer.bin          id=78, args=
197133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847719204     6533 npviewer.bin          id=142, args=
198133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847755445     6533 npviewer.bin          id=3, args=
199133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847775601     6533 npviewer.bin          id=3, args=
200133dc4c3SIngo Molnarraw_syscalls__sys_enter     1 00840.847781820     6533 npviewer.bin          id=3, args=
201133dc4c3SIngo Molnar.
202133dc4c3SIngo Molnar.
203133dc4c3SIngo Molnar.
204133dc4c3SIngo Molnar----
205133dc4c3SIngo Molnar
206133dc4c3SIngo MolnarOf course, for this script, we're not interested in printing every
207133dc4c3SIngo Molnartrace event, but rather aggregating it in a useful way.  So we'll get
208133dc4c3SIngo Molnarrid of everything to do with printing as well as the trace_begin() and
209133dc4c3SIngo Molnartrace_unhandled() functions, which we won't be using.  That leaves us
210133dc4c3SIngo Molnarwith this minimalistic skeleton:
211133dc4c3SIngo Molnar
212133dc4c3SIngo Molnar----
213133dc4c3SIngo Molnarimport os
214133dc4c3SIngo Molnarimport sys
215133dc4c3SIngo Molnar
216133dc4c3SIngo Molnarsys.path.append(os.environ['PERF_EXEC_PATH'] + \
217e8d0f400SDavid Ahern	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
218133dc4c3SIngo Molnar
219133dc4c3SIngo Molnarfrom perf_trace_context import *
220133dc4c3SIngo Molnarfrom Core import *
221133dc4c3SIngo Molnar
222133dc4c3SIngo Molnardef trace_end():
223133dc4c3SIngo Molnar	print "in trace_end"
224133dc4c3SIngo Molnar
225133dc4c3SIngo Molnardef raw_syscalls__sys_enter(event_name, context, common_cpu,
226133dc4c3SIngo Molnar	common_secs, common_nsecs, common_pid, common_comm,
227133dc4c3SIngo Molnar	id, args):
228133dc4c3SIngo Molnar----
229133dc4c3SIngo Molnar
230133dc4c3SIngo MolnarIn trace_end(), we'll simply print the results, but first we need to
231133dc4c3SIngo Molnargenerate some results to print.  To do that we need to have our
232133dc4c3SIngo Molnarsys_enter() handler do the necessary tallying until all events have
233133dc4c3SIngo Molnarbeen counted.  A hash table indexed by syscall id is a good way to
234133dc4c3SIngo Molnarstore that information; every time the sys_enter() handler is called,
235133dc4c3SIngo Molnarwe simply increment a count associated with that hash entry indexed by
236133dc4c3SIngo Molnarthat syscall id:
237133dc4c3SIngo Molnar
238133dc4c3SIngo Molnar----
239133dc4c3SIngo Molnar  syscalls = autodict()
240133dc4c3SIngo Molnar
241133dc4c3SIngo Molnar  try:
242133dc4c3SIngo Molnar    syscalls[id] += 1
243133dc4c3SIngo Molnar  except TypeError:
244133dc4c3SIngo Molnar    syscalls[id] = 1
245133dc4c3SIngo Molnar----
246133dc4c3SIngo Molnar
247133dc4c3SIngo MolnarThe syscalls 'autodict' object is a special kind of Python dictionary
248133dc4c3SIngo Molnar(implemented in Core.py) that implements Perl's 'autovivifying' hashes
249133dc4c3SIngo Molnarin Python i.e. with autovivifying hashes, you can assign nested hash
250133dc4c3SIngo Molnarvalues without having to go to the trouble of creating intermediate
251133dc4c3SIngo Molnarlevels if they don't exist e.g syscalls[comm][pid][id] = 1 will create
252133dc4c3SIngo Molnarthe intermediate hash levels and finally assign the value 1 to the
253133dc4c3SIngo Molnarhash entry for 'id' (because the value being assigned isn't a hash
254133dc4c3SIngo Molnarobject itself, the initial value is assigned in the TypeError
255133dc4c3SIngo Molnarexception.  Well, there may be a better way to do this in Python but
256133dc4c3SIngo Molnarthat's what works for now).
257133dc4c3SIngo Molnar
258133dc4c3SIngo MolnarPutting that code into the raw_syscalls__sys_enter() handler, we
259133dc4c3SIngo Molnareffectively end up with a single-level dictionary keyed on syscall id
260133dc4c3SIngo Molnarand having the counts we've tallied as values.
261133dc4c3SIngo Molnar
262133dc4c3SIngo MolnarThe print_syscall_totals() function iterates over the entries in the
263133dc4c3SIngo Molnardictionary and displays a line for each entry containing the syscall
26496355f2cSMasanari Iidaname (the dictionary keys contain the syscall ids, which are passed to
265133dc4c3SIngo Molnarthe Util function syscall_name(), which translates the raw syscall
266133dc4c3SIngo Molnarnumbers to the corresponding syscall name strings).  The output is
267133dc4c3SIngo Molnardisplayed after all the events in the trace have been processed, by
268133dc4c3SIngo Molnarcalling the print_syscall_totals() function from the trace_end()
269133dc4c3SIngo Molnarhandler called at the end of script processing.
270133dc4c3SIngo Molnar
271133dc4c3SIngo MolnarThe final script producing the output shown above is shown in its
272133dc4c3SIngo Molnarentirety below (syscall_name() helper is not yet available, you can
273133dc4c3SIngo Molnaronly deal with id's for now):
274133dc4c3SIngo Molnar
275133dc4c3SIngo Molnar----
276133dc4c3SIngo Molnarimport os
277133dc4c3SIngo Molnarimport sys
278133dc4c3SIngo Molnar
279133dc4c3SIngo Molnarsys.path.append(os.environ['PERF_EXEC_PATH'] + \
280e8d0f400SDavid Ahern	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
281133dc4c3SIngo Molnar
282133dc4c3SIngo Molnarfrom perf_trace_context import *
283133dc4c3SIngo Molnarfrom Core import *
284133dc4c3SIngo Molnarfrom Util import *
285133dc4c3SIngo Molnar
286133dc4c3SIngo Molnarsyscalls = autodict()
287133dc4c3SIngo Molnar
288133dc4c3SIngo Molnardef trace_end():
289133dc4c3SIngo Molnar	print_syscall_totals()
290133dc4c3SIngo Molnar
291133dc4c3SIngo Molnardef raw_syscalls__sys_enter(event_name, context, common_cpu,
292133dc4c3SIngo Molnar	common_secs, common_nsecs, common_pid, common_comm,
293133dc4c3SIngo Molnar	id, args):
294133dc4c3SIngo Molnar	try:
295133dc4c3SIngo Molnar		syscalls[id] += 1
296133dc4c3SIngo Molnar	except TypeError:
297133dc4c3SIngo Molnar		syscalls[id] = 1
298133dc4c3SIngo Molnar
299133dc4c3SIngo Molnardef print_syscall_totals():
300133dc4c3SIngo Molnar    if for_comm is not None:
301133dc4c3SIngo Molnar	    print "\nsyscall events for %s:\n\n" % (for_comm),
302133dc4c3SIngo Molnar    else:
303133dc4c3SIngo Molnar	    print "\nsyscall events:\n\n",
304133dc4c3SIngo Molnar
305133dc4c3SIngo Molnar    print "%-40s  %10s\n" % ("event", "count"),
306133dc4c3SIngo Molnar    print "%-40s  %10s\n" % ("----------------------------------------", \
307133dc4c3SIngo Molnar                                 "-----------"),
308133dc4c3SIngo Molnar
309133dc4c3SIngo Molnar    for id, val in sorted(syscalls.iteritems(), key = lambda(k, v): (v, k), \
310133dc4c3SIngo Molnar				  reverse = True):
311133dc4c3SIngo Molnar	    print "%-40s  %10d\n" % (syscall_name(id), val),
312133dc4c3SIngo Molnar----
313133dc4c3SIngo Molnar
314133dc4c3SIngo MolnarThe script can be run just as before:
315133dc4c3SIngo Molnar
316133dc4c3SIngo Molnar  # perf script -s syscall-counts.py
317133dc4c3SIngo Molnar
318133dc4c3SIngo MolnarSo those are the essential steps in writing and running a script.  The
319133dc4c3SIngo Molnarprocess can be generalized to any tracepoint or set of tracepoints
320133dc4c3SIngo Molnaryou're interested in - basically find the tracepoint(s) you're
321133dc4c3SIngo Molnarinterested in by looking at the list of available events shown by
32234d4453dSSeongJae Park'perf list' and/or look in /sys/kernel/debug/tracing/events/ for
323133dc4c3SIngo Molnardetailed event and field info, record the corresponding trace data
324133dc4c3SIngo Molnarusing 'perf record', passing it the list of interesting events,
325133dc4c3SIngo Molnargenerate a skeleton script using 'perf script -g python' and modify the
326133dc4c3SIngo Molnarcode to aggregate and display it for your particular needs.
327133dc4c3SIngo Molnar
328133dc4c3SIngo MolnarAfter you've done that you may end up with a general-purpose script
329133dc4c3SIngo Molnarthat you want to keep around and have available for future use.  By
330133dc4c3SIngo Molnarwriting a couple of very simple shell scripts and putting them in the
331133dc4c3SIngo Molnarright place, you can have your script listed alongside the other
332133dc4c3SIngo Molnarscripts listed by the 'perf script -l' command e.g.:
333133dc4c3SIngo Molnar
334133dc4c3SIngo Molnar----
335133dc4c3SIngo Molnarroot@tropicana:~# perf script -l
336133dc4c3SIngo MolnarList of available trace scripts:
337133dc4c3SIngo Molnar  wakeup-latency                       system-wide min/max/avg wakeup latency
338133dc4c3SIngo Molnar  rw-by-file <comm>                    r/w activity for a program, by file
339133dc4c3SIngo Molnar  rw-by-pid                            system-wide r/w activity
340133dc4c3SIngo Molnar----
341133dc4c3SIngo Molnar
342133dc4c3SIngo MolnarA nice side effect of doing this is that you also then capture the
343133dc4c3SIngo Molnarprobably lengthy 'perf record' command needed to record the events for
344133dc4c3SIngo Molnarthe script.
345133dc4c3SIngo Molnar
346133dc4c3SIngo MolnarTo have the script appear as a 'built-in' script, you write two simple
347133dc4c3SIngo Molnarscripts, one for recording and one for 'reporting'.
348133dc4c3SIngo Molnar
349133dc4c3SIngo MolnarThe 'record' script is a shell script with the same base name as your
350133dc4c3SIngo Molnarscript, but with -record appended.  The shell script should be put
351133dc4c3SIngo Molnarinto the perf/scripts/python/bin directory in the kernel source tree.
352133dc4c3SIngo MolnarIn that script, you write the 'perf record' command-line needed for
353133dc4c3SIngo Molnaryour script:
354133dc4c3SIngo Molnar
355133dc4c3SIngo Molnar----
356133dc4c3SIngo Molnar# cat kernel-source/tools/perf/scripts/python/bin/syscall-counts-record
357133dc4c3SIngo Molnar
358133dc4c3SIngo Molnar#!/bin/bash
359133dc4c3SIngo Molnarperf record -a -e raw_syscalls:sys_enter
360133dc4c3SIngo Molnar----
361133dc4c3SIngo Molnar
362133dc4c3SIngo MolnarThe 'report' script is also a shell script with the same base name as
363133dc4c3SIngo Molnaryour script, but with -report appended.  It should also be located in
364133dc4c3SIngo Molnarthe perf/scripts/python/bin directory.  In that script, you write the
365133dc4c3SIngo Molnar'perf script -s' command-line needed for running your script:
366133dc4c3SIngo Molnar
367133dc4c3SIngo Molnar----
368133dc4c3SIngo Molnar# cat kernel-source/tools/perf/scripts/python/bin/syscall-counts-report
369133dc4c3SIngo Molnar
370133dc4c3SIngo Molnar#!/bin/bash
371133dc4c3SIngo Molnar# description: system-wide syscall counts
372133dc4c3SIngo Molnarperf script -s ~/libexec/perf-core/scripts/python/syscall-counts.py
373133dc4c3SIngo Molnar----
374133dc4c3SIngo Molnar
375133dc4c3SIngo MolnarNote that the location of the Python script given in the shell script
376133dc4c3SIngo Molnaris in the libexec/perf-core/scripts/python directory - this is where
377133dc4c3SIngo Molnarthe script will be copied by 'make install' when you install perf.
378133dc4c3SIngo MolnarFor the installation to install your script there, your script needs
379133dc4c3SIngo Molnarto be located in the perf/scripts/python directory in the kernel
380133dc4c3SIngo Molnarsource tree:
381133dc4c3SIngo Molnar
382133dc4c3SIngo Molnar----
383133dc4c3SIngo Molnar# ls -al kernel-source/tools/perf/scripts/python
384133dc4c3SIngo Molnar
385133dc4c3SIngo Molnarroot@tropicana:/home/trz/src/tip# ls -al tools/perf/scripts/python
386133dc4c3SIngo Molnartotal 32
387133dc4c3SIngo Molnardrwxr-xr-x 4 trz trz 4096 2010-01-26 22:30 .
388133dc4c3SIngo Molnardrwxr-xr-x 4 trz trz 4096 2010-01-26 22:29 ..
389133dc4c3SIngo Molnardrwxr-xr-x 2 trz trz 4096 2010-01-26 22:29 bin
390133dc4c3SIngo Molnar-rw-r--r-- 1 trz trz 2548 2010-01-26 22:29 check-perf-script.py
391e8d0f400SDavid Aherndrwxr-xr-x 3 trz trz 4096 2010-01-26 22:49 Perf-Trace-Util
392133dc4c3SIngo Molnar-rw-r--r-- 1 trz trz 1462 2010-01-26 22:30 syscall-counts.py
393133dc4c3SIngo Molnar----
394133dc4c3SIngo Molnar
395133dc4c3SIngo MolnarOnce you've done that (don't forget to do a new 'make install',
396133dc4c3SIngo Molnarotherwise your script won't show up at run-time), 'perf script -l'
397133dc4c3SIngo Molnarshould show a new entry for your script:
398133dc4c3SIngo Molnar
399133dc4c3SIngo Molnar----
400133dc4c3SIngo Molnarroot@tropicana:~# perf script -l
401133dc4c3SIngo MolnarList of available trace scripts:
402133dc4c3SIngo Molnar  wakeup-latency                       system-wide min/max/avg wakeup latency
403133dc4c3SIngo Molnar  rw-by-file <comm>                    r/w activity for a program, by file
404133dc4c3SIngo Molnar  rw-by-pid                            system-wide r/w activity
405133dc4c3SIngo Molnar  syscall-counts                       system-wide syscall counts
406133dc4c3SIngo Molnar----
407133dc4c3SIngo Molnar
408133dc4c3SIngo MolnarYou can now perform the record step via 'perf script record':
409133dc4c3SIngo Molnar
410133dc4c3SIngo Molnar  # perf script record syscall-counts
411133dc4c3SIngo Molnar
412133dc4c3SIngo Molnarand display the output using 'perf script report':
413133dc4c3SIngo Molnar
414133dc4c3SIngo Molnar  # perf script report syscall-counts
415133dc4c3SIngo Molnar
416133dc4c3SIngo MolnarSTARTER SCRIPTS
417133dc4c3SIngo Molnar---------------
418133dc4c3SIngo Molnar
419133dc4c3SIngo MolnarYou can quickly get started writing a script for a particular set of
420133dc4c3SIngo Molnartrace data by generating a skeleton script using 'perf script -g
421133dc4c3SIngo Molnarpython' in the same directory as an existing perf.data trace file.
422133dc4c3SIngo MolnarThat will generate a starter script containing a handler for each of
423133dc4c3SIngo Molnarthe event types in the trace file; it simply prints every available
424133dc4c3SIngo Molnarfield for each event in the trace file.
425133dc4c3SIngo Molnar
426133dc4c3SIngo MolnarYou can also look at the existing scripts in
427133dc4c3SIngo Molnar~/libexec/perf-core/scripts/python for typical examples showing how to
428133dc4c3SIngo Molnardo basic things like aggregate event data, print results, etc.  Also,
429133dc4c3SIngo Molnarthe check-perf-script.py script, while not interesting for its results,
430133dc4c3SIngo Molnarattempts to exercise all of the main scripting features.
431133dc4c3SIngo Molnar
432133dc4c3SIngo MolnarEVENT HANDLERS
433133dc4c3SIngo Molnar--------------
434133dc4c3SIngo Molnar
435133dc4c3SIngo MolnarWhen perf script is invoked using a trace script, a user-defined
436133dc4c3SIngo Molnar'handler function' is called for each event in the trace.  If there's
437133dc4c3SIngo Molnarno handler function defined for a given event type, the event is
43834d4453dSSeongJae Parkignored (or passed to a 'trace_unhandled' function, see below) and the
439133dc4c3SIngo Molnarnext event is processed.
440133dc4c3SIngo Molnar
441133dc4c3SIngo MolnarMost of the event's field values are passed as arguments to the
442133dc4c3SIngo Molnarhandler function; some of the less common ones aren't - those are
443133dc4c3SIngo Molnaravailable as calls back into the perf executable (see below).
444133dc4c3SIngo Molnar
445133dc4c3SIngo MolnarAs an example, the following perf record command can be used to record
446133dc4c3SIngo Molnarall sched_wakeup events in the system:
447133dc4c3SIngo Molnar
448133dc4c3SIngo Molnar # perf record -a -e sched:sched_wakeup
449133dc4c3SIngo Molnar
450133dc4c3SIngo MolnarTraces meant to be processed using a script should be recorded with
451133dc4c3SIngo Molnarthe above option: -a to enable system-wide collection.
452133dc4c3SIngo Molnar
453133dc4c3SIngo MolnarThe format file for the sched_wakep event defines the following fields
454133dc4c3SIngo Molnar(see /sys/kernel/debug/tracing/events/sched/sched_wakeup/format):
455133dc4c3SIngo Molnar
456133dc4c3SIngo Molnar----
457133dc4c3SIngo Molnar format:
458133dc4c3SIngo Molnar        field:unsigned short common_type;
459133dc4c3SIngo Molnar        field:unsigned char common_flags;
460133dc4c3SIngo Molnar        field:unsigned char common_preempt_count;
461133dc4c3SIngo Molnar        field:int common_pid;
462133dc4c3SIngo Molnar
463133dc4c3SIngo Molnar        field:char comm[TASK_COMM_LEN];
464133dc4c3SIngo Molnar        field:pid_t pid;
465133dc4c3SIngo Molnar        field:int prio;
466133dc4c3SIngo Molnar        field:int success;
467133dc4c3SIngo Molnar        field:int target_cpu;
468133dc4c3SIngo Molnar----
469133dc4c3SIngo Molnar
470133dc4c3SIngo MolnarThe handler function for this event would be defined as:
471133dc4c3SIngo Molnar
472133dc4c3SIngo Molnar----
473133dc4c3SIngo Molnardef sched__sched_wakeup(event_name, context, common_cpu, common_secs,
474133dc4c3SIngo Molnar       common_nsecs, common_pid, common_comm,
475133dc4c3SIngo Molnar       comm, pid, prio, success, target_cpu):
476133dc4c3SIngo Molnar       pass
477133dc4c3SIngo Molnar----
478133dc4c3SIngo Molnar
479133dc4c3SIngo MolnarThe handler function takes the form subsystem__event_name.
480133dc4c3SIngo Molnar
481133dc4c3SIngo MolnarThe common_* arguments in the handler's argument list are the set of
482133dc4c3SIngo Molnararguments passed to all event handlers; some of the fields correspond
483133dc4c3SIngo Molnarto the common_* fields in the format file, but some are synthesized,
484133dc4c3SIngo Molnarand some of the common_* fields aren't common enough to to be passed
485133dc4c3SIngo Molnarto every event as arguments but are available as library functions.
486133dc4c3SIngo Molnar
487133dc4c3SIngo MolnarHere's a brief description of each of the invariant event args:
488133dc4c3SIngo Molnar
489133dc4c3SIngo Molnar event_name 	  	    the name of the event as text
490133dc4c3SIngo Molnar context		    an opaque 'cookie' used in calls back into perf
491133dc4c3SIngo Molnar common_cpu		    the cpu the event occurred on
492133dc4c3SIngo Molnar common_secs		    the secs portion of the event timestamp
493133dc4c3SIngo Molnar common_nsecs		    the nsecs portion of the event timestamp
494133dc4c3SIngo Molnar common_pid		    the pid of the current task
495133dc4c3SIngo Molnar common_comm		    the name of the current process
496133dc4c3SIngo Molnar
497133dc4c3SIngo MolnarAll of the remaining fields in the event's format file have
498133dc4c3SIngo Molnarcounterparts as handler function arguments of the same name, as can be
499133dc4c3SIngo Molnarseen in the example above.
500133dc4c3SIngo Molnar
501133dc4c3SIngo MolnarThe above provides the basics needed to directly access every field of
502133dc4c3SIngo Molnarevery event in a trace, which covers 90% of what you need to know to
503133dc4c3SIngo Molnarwrite a useful trace script.  The sections below cover the rest.
504133dc4c3SIngo Molnar
505133dc4c3SIngo MolnarSCRIPT LAYOUT
506133dc4c3SIngo Molnar-------------
507133dc4c3SIngo Molnar
508133dc4c3SIngo MolnarEvery perf script Python script should start by setting up a Python
509133dc4c3SIngo Molnarmodule search path and 'import'ing a few support modules (see module
510133dc4c3SIngo Molnardescriptions below):
511133dc4c3SIngo Molnar
512133dc4c3SIngo Molnar----
513133dc4c3SIngo Molnar import os
514133dc4c3SIngo Molnar import sys
515133dc4c3SIngo Molnar
516133dc4c3SIngo Molnar sys.path.append(os.environ['PERF_EXEC_PATH'] + \
517e8d0f400SDavid Ahern	      '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
518133dc4c3SIngo Molnar
519133dc4c3SIngo Molnar from perf_trace_context import *
520133dc4c3SIngo Molnar from Core import *
521133dc4c3SIngo Molnar----
522133dc4c3SIngo Molnar
523133dc4c3SIngo MolnarThe rest of the script can contain handler functions and support
524133dc4c3SIngo Molnarfunctions in any order.
525133dc4c3SIngo Molnar
526133dc4c3SIngo MolnarAside from the event handler functions discussed above, every script
527133dc4c3SIngo Molnarcan implement a set of optional functions:
528133dc4c3SIngo Molnar
529133dc4c3SIngo Molnar*trace_begin*, if defined, is called before any event is processed and
530133dc4c3SIngo Molnargives scripts a chance to do setup tasks:
531133dc4c3SIngo Molnar
532133dc4c3SIngo Molnar----
53326ddb872SSeongJae Parkdef trace_begin():
534133dc4c3SIngo Molnar    pass
535133dc4c3SIngo Molnar----
536133dc4c3SIngo Molnar
537133dc4c3SIngo Molnar*trace_end*, if defined, is called after all events have been
538133dc4c3SIngo Molnar processed and gives scripts a chance to do end-of-script tasks, such
539133dc4c3SIngo Molnar as display results:
540133dc4c3SIngo Molnar
541133dc4c3SIngo Molnar----
54226ddb872SSeongJae Parkdef trace_end():
543133dc4c3SIngo Molnar    pass
544133dc4c3SIngo Molnar----
545133dc4c3SIngo Molnar
546133dc4c3SIngo Molnar*trace_unhandled*, if defined, is called after for any event that
547133dc4c3SIngo Molnar doesn't have a handler explicitly defined for it.  The standard set
548133dc4c3SIngo Molnar of common arguments are passed into it:
549133dc4c3SIngo Molnar
550133dc4c3SIngo Molnar----
551*1bf8d5a4SSeongJae Parkdef trace_unhandled(event_name, context, event_fields_dict):
552133dc4c3SIngo Molnar    pass
553133dc4c3SIngo Molnar----
554133dc4c3SIngo Molnar
555133dc4c3SIngo MolnarThe remaining sections provide descriptions of each of the available
556133dc4c3SIngo Molnarbuilt-in perf script Python modules and their associated functions.
557133dc4c3SIngo Molnar
558133dc4c3SIngo MolnarAVAILABLE MODULES AND FUNCTIONS
559133dc4c3SIngo Molnar-------------------------------
560133dc4c3SIngo Molnar
561133dc4c3SIngo MolnarThe following sections describe the functions and variables available
562133dc4c3SIngo Molnarvia the various perf script Python modules.  To use the functions and
563133dc4c3SIngo Molnarvariables from the given module, add the corresponding 'from XXXX
564133dc4c3SIngo Molnarimport' line to your perf script script.
565133dc4c3SIngo Molnar
566133dc4c3SIngo MolnarCore.py Module
567133dc4c3SIngo Molnar~~~~~~~~~~~~~~
568133dc4c3SIngo Molnar
569133dc4c3SIngo MolnarThese functions provide some essential functions to user scripts.
570133dc4c3SIngo Molnar
571133dc4c3SIngo MolnarThe *flag_str* and *symbol_str* functions provide human-readable
572133dc4c3SIngo Molnarstrings for flag and symbolic fields.  These correspond to the strings
573133dc4c3SIngo Molnarand values parsed from the 'print fmt' fields of the event format
574133dc4c3SIngo Molnarfiles:
575133dc4c3SIngo Molnar
57696355f2cSMasanari 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
57796355f2cSMasanari 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
578133dc4c3SIngo Molnar
579133dc4c3SIngo MolnarThe *autodict* function returns a special kind of Python
580133dc4c3SIngo Molnardictionary that implements Perl's 'autovivifying' hashes in Python
581133dc4c3SIngo Molnari.e. with autovivifying hashes, you can assign nested hash values
582133dc4c3SIngo Molnarwithout having to go to the trouble of creating intermediate levels if
583133dc4c3SIngo Molnarthey don't exist.
584133dc4c3SIngo Molnar
585133dc4c3SIngo Molnar  autodict() - returns an autovivifying dictionary instance
586133dc4c3SIngo Molnar
587133dc4c3SIngo Molnar
588133dc4c3SIngo Molnarperf_trace_context Module
589133dc4c3SIngo Molnar~~~~~~~~~~~~~~~~~~~~~~~~~
590133dc4c3SIngo Molnar
591133dc4c3SIngo MolnarSome of the 'common' fields in the event format file aren't all that
592133dc4c3SIngo Molnarcommon, but need to be made accessible to user scripts nonetheless.
593133dc4c3SIngo Molnar
594133dc4c3SIngo Molnarperf_trace_context defines a set of functions that can be used to
595133dc4c3SIngo Molnaraccess this data in the context of the current event.  Each of these
596133dc4c3SIngo Molnarfunctions expects a context variable, which is the same as the
597133dc4c3SIngo Molnarcontext variable passed into every event handler as the second
598133dc4c3SIngo Molnarargument.
599133dc4c3SIngo Molnar
600133dc4c3SIngo Molnar common_pc(context) - returns common_preempt count for the current event
601133dc4c3SIngo Molnar common_flags(context) - returns common_flags for the current event
602133dc4c3SIngo Molnar common_lock_depth(context) - returns common_lock_depth for the current event
603133dc4c3SIngo Molnar
604133dc4c3SIngo MolnarUtil.py Module
605133dc4c3SIngo Molnar~~~~~~~~~~~~~~
606133dc4c3SIngo Molnar
607133dc4c3SIngo MolnarVarious utility functions for use with perf script:
608133dc4c3SIngo Molnar
609133dc4c3SIngo Molnar  nsecs(secs, nsecs) - returns total nsecs given secs/nsecs pair
610133dc4c3SIngo Molnar  nsecs_secs(nsecs) - returns whole secs portion given nsecs
611133dc4c3SIngo Molnar  nsecs_nsecs(nsecs) - returns nsecs remainder given nsecs
612133dc4c3SIngo Molnar  nsecs_str(nsecs) - returns printable string in the form secs.nsecs
613133dc4c3SIngo Molnar  avg(total, n) - returns average given a sum and a total number of values
614133dc4c3SIngo Molnar
615133dc4c3SIngo MolnarSEE ALSO
616133dc4c3SIngo Molnar--------
617133dc4c3SIngo Molnarlinkperf:perf-script[1]
618