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 1521bf8d5a4SSeongJae Parkdef trace_unhandled(event_name, context, event_fields_dict): 1531bf8d5a4SSeongJae 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 170b7ae6d43SStephen Brennanthe 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 322*1df49ef9SRoss Zwisler'perf list' and/or look in /sys/kernel/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---- 33514fc42faSSeongJae Park# 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 Molnartotal 32 385133dc4c3SIngo Molnardrwxr-xr-x 4 trz trz 4096 2010-01-26 22:30 . 386133dc4c3SIngo Molnardrwxr-xr-x 4 trz trz 4096 2010-01-26 22:29 .. 387133dc4c3SIngo Molnardrwxr-xr-x 2 trz trz 4096 2010-01-26 22:29 bin 388133dc4c3SIngo Molnar-rw-r--r-- 1 trz trz 2548 2010-01-26 22:29 check-perf-script.py 389e8d0f400SDavid Aherndrwxr-xr-x 3 trz trz 4096 2010-01-26 22:49 Perf-Trace-Util 390133dc4c3SIngo Molnar-rw-r--r-- 1 trz trz 1462 2010-01-26 22:30 syscall-counts.py 391133dc4c3SIngo Molnar---- 392133dc4c3SIngo Molnar 393133dc4c3SIngo MolnarOnce you've done that (don't forget to do a new 'make install', 394133dc4c3SIngo Molnarotherwise your script won't show up at run-time), 'perf script -l' 395133dc4c3SIngo Molnarshould show a new entry for your script: 396133dc4c3SIngo Molnar 397133dc4c3SIngo Molnar---- 39814fc42faSSeongJae Park# perf script -l 399133dc4c3SIngo MolnarList of available trace scripts: 400133dc4c3SIngo Molnar wakeup-latency system-wide min/max/avg wakeup latency 401133dc4c3SIngo Molnar rw-by-file <comm> r/w activity for a program, by file 402133dc4c3SIngo Molnar rw-by-pid system-wide r/w activity 403133dc4c3SIngo Molnar syscall-counts system-wide syscall counts 404133dc4c3SIngo Molnar---- 405133dc4c3SIngo Molnar 406133dc4c3SIngo MolnarYou can now perform the record step via 'perf script record': 407133dc4c3SIngo Molnar 408133dc4c3SIngo Molnar # perf script record syscall-counts 409133dc4c3SIngo Molnar 410133dc4c3SIngo Molnarand display the output using 'perf script report': 411133dc4c3SIngo Molnar 412133dc4c3SIngo Molnar # perf script report syscall-counts 413133dc4c3SIngo Molnar 414133dc4c3SIngo MolnarSTARTER SCRIPTS 415133dc4c3SIngo Molnar--------------- 416133dc4c3SIngo Molnar 417133dc4c3SIngo MolnarYou can quickly get started writing a script for a particular set of 418133dc4c3SIngo Molnartrace data by generating a skeleton script using 'perf script -g 419133dc4c3SIngo Molnarpython' in the same directory as an existing perf.data trace file. 420133dc4c3SIngo MolnarThat will generate a starter script containing a handler for each of 421133dc4c3SIngo Molnarthe event types in the trace file; it simply prints every available 422133dc4c3SIngo Molnarfield for each event in the trace file. 423133dc4c3SIngo Molnar 424133dc4c3SIngo MolnarYou can also look at the existing scripts in 425133dc4c3SIngo Molnar~/libexec/perf-core/scripts/python for typical examples showing how to 426133dc4c3SIngo Molnardo basic things like aggregate event data, print results, etc. Also, 427133dc4c3SIngo Molnarthe check-perf-script.py script, while not interesting for its results, 428133dc4c3SIngo Molnarattempts to exercise all of the main scripting features. 429133dc4c3SIngo Molnar 430133dc4c3SIngo MolnarEVENT HANDLERS 431133dc4c3SIngo Molnar-------------- 432133dc4c3SIngo Molnar 433133dc4c3SIngo MolnarWhen perf script is invoked using a trace script, a user-defined 434133dc4c3SIngo Molnar'handler function' is called for each event in the trace. If there's 435133dc4c3SIngo Molnarno handler function defined for a given event type, the event is 43634d4453dSSeongJae Parkignored (or passed to a 'trace_unhandled' function, see below) and the 437133dc4c3SIngo Molnarnext event is processed. 438133dc4c3SIngo Molnar 439133dc4c3SIngo MolnarMost of the event's field values are passed as arguments to the 440133dc4c3SIngo Molnarhandler function; some of the less common ones aren't - those are 441133dc4c3SIngo Molnaravailable as calls back into the perf executable (see below). 442133dc4c3SIngo Molnar 443133dc4c3SIngo MolnarAs an example, the following perf record command can be used to record 444133dc4c3SIngo Molnarall sched_wakeup events in the system: 445133dc4c3SIngo Molnar 446133dc4c3SIngo Molnar # perf record -a -e sched:sched_wakeup 447133dc4c3SIngo Molnar 448133dc4c3SIngo MolnarTraces meant to be processed using a script should be recorded with 449133dc4c3SIngo Molnarthe above option: -a to enable system-wide collection. 450133dc4c3SIngo Molnar 4514da6552cSLike XuThe format file for the sched_wakeup event defines the following fields 452*1df49ef9SRoss Zwisler(see /sys/kernel/tracing/events/sched/sched_wakeup/format): 453133dc4c3SIngo Molnar 454133dc4c3SIngo Molnar---- 455133dc4c3SIngo Molnar format: 456133dc4c3SIngo Molnar field:unsigned short common_type; 457133dc4c3SIngo Molnar field:unsigned char common_flags; 458133dc4c3SIngo Molnar field:unsigned char common_preempt_count; 459133dc4c3SIngo Molnar field:int common_pid; 460133dc4c3SIngo Molnar 461133dc4c3SIngo Molnar field:char comm[TASK_COMM_LEN]; 462133dc4c3SIngo Molnar field:pid_t pid; 463133dc4c3SIngo Molnar field:int prio; 464133dc4c3SIngo Molnar field:int success; 465133dc4c3SIngo Molnar field:int target_cpu; 466133dc4c3SIngo Molnar---- 467133dc4c3SIngo Molnar 468133dc4c3SIngo MolnarThe handler function for this event would be defined as: 469133dc4c3SIngo Molnar 470133dc4c3SIngo Molnar---- 471133dc4c3SIngo Molnardef sched__sched_wakeup(event_name, context, common_cpu, common_secs, 472133dc4c3SIngo Molnar common_nsecs, common_pid, common_comm, 473133dc4c3SIngo Molnar comm, pid, prio, success, target_cpu): 474133dc4c3SIngo Molnar pass 475133dc4c3SIngo Molnar---- 476133dc4c3SIngo Molnar 477133dc4c3SIngo MolnarThe handler function takes the form subsystem__event_name. 478133dc4c3SIngo Molnar 479133dc4c3SIngo MolnarThe common_* arguments in the handler's argument list are the set of 480133dc4c3SIngo Molnararguments passed to all event handlers; some of the fields correspond 481133dc4c3SIngo Molnarto the common_* fields in the format file, but some are synthesized, 482133dc4c3SIngo Molnarand some of the common_* fields aren't common enough to to be passed 483133dc4c3SIngo Molnarto every event as arguments but are available as library functions. 484133dc4c3SIngo Molnar 485133dc4c3SIngo MolnarHere's a brief description of each of the invariant event args: 486133dc4c3SIngo Molnar 487133dc4c3SIngo Molnar event_name the name of the event as text 488133dc4c3SIngo Molnar context an opaque 'cookie' used in calls back into perf 489133dc4c3SIngo Molnar common_cpu the cpu the event occurred on 490133dc4c3SIngo Molnar common_secs the secs portion of the event timestamp 491133dc4c3SIngo Molnar common_nsecs the nsecs portion of the event timestamp 492133dc4c3SIngo Molnar common_pid the pid of the current task 493133dc4c3SIngo Molnar common_comm the name of the current process 494133dc4c3SIngo Molnar 495133dc4c3SIngo MolnarAll of the remaining fields in the event's format file have 496133dc4c3SIngo Molnarcounterparts as handler function arguments of the same name, as can be 497133dc4c3SIngo Molnarseen in the example above. 498133dc4c3SIngo Molnar 499133dc4c3SIngo MolnarThe above provides the basics needed to directly access every field of 500133dc4c3SIngo Molnarevery event in a trace, which covers 90% of what you need to know to 501133dc4c3SIngo Molnarwrite a useful trace script. The sections below cover the rest. 502133dc4c3SIngo Molnar 503133dc4c3SIngo MolnarSCRIPT LAYOUT 504133dc4c3SIngo Molnar------------- 505133dc4c3SIngo Molnar 506133dc4c3SIngo MolnarEvery perf script Python script should start by setting up a Python 507133dc4c3SIngo Molnarmodule search path and 'import'ing a few support modules (see module 508133dc4c3SIngo Molnardescriptions below): 509133dc4c3SIngo Molnar 510133dc4c3SIngo Molnar---- 511133dc4c3SIngo Molnar import os 512133dc4c3SIngo Molnar import sys 513133dc4c3SIngo Molnar 514133dc4c3SIngo Molnar sys.path.append(os.environ['PERF_EXEC_PATH'] + \ 515e8d0f400SDavid Ahern '/scripts/python/Perf-Trace-Util/lib/Perf/Trace') 516133dc4c3SIngo Molnar 517133dc4c3SIngo Molnar from perf_trace_context import * 518133dc4c3SIngo Molnar from Core import * 519133dc4c3SIngo Molnar---- 520133dc4c3SIngo Molnar 521133dc4c3SIngo MolnarThe rest of the script can contain handler functions and support 522133dc4c3SIngo Molnarfunctions in any order. 523133dc4c3SIngo Molnar 524133dc4c3SIngo MolnarAside from the event handler functions discussed above, every script 525133dc4c3SIngo Molnarcan implement a set of optional functions: 526133dc4c3SIngo Molnar 527133dc4c3SIngo Molnar*trace_begin*, if defined, is called before any event is processed and 528133dc4c3SIngo Molnargives scripts a chance to do setup tasks: 529133dc4c3SIngo Molnar 530133dc4c3SIngo Molnar---- 53126ddb872SSeongJae Parkdef trace_begin(): 532133dc4c3SIngo Molnar pass 533133dc4c3SIngo Molnar---- 534133dc4c3SIngo Molnar 535133dc4c3SIngo Molnar*trace_end*, if defined, is called after all events have been 536133dc4c3SIngo Molnar processed and gives scripts a chance to do end-of-script tasks, such 537133dc4c3SIngo Molnar as display results: 538133dc4c3SIngo Molnar 539133dc4c3SIngo Molnar---- 54026ddb872SSeongJae Parkdef trace_end(): 541133dc4c3SIngo Molnar pass 542133dc4c3SIngo Molnar---- 543133dc4c3SIngo Molnar 544133dc4c3SIngo Molnar*trace_unhandled*, if defined, is called after for any event that 545133dc4c3SIngo Molnar doesn't have a handler explicitly defined for it. The standard set 546133dc4c3SIngo Molnar of common arguments are passed into it: 547133dc4c3SIngo Molnar 548133dc4c3SIngo Molnar---- 5491bf8d5a4SSeongJae Parkdef trace_unhandled(event_name, context, event_fields_dict): 550133dc4c3SIngo Molnar pass 551133dc4c3SIngo Molnar---- 552133dc4c3SIngo Molnar 5531a329b1cSAdrian Hunter*process_event*, if defined, is called for any non-tracepoint event 5541a329b1cSAdrian Hunter 5551a329b1cSAdrian Hunter---- 5561a329b1cSAdrian Hunterdef process_event(param_dict): 5571a329b1cSAdrian Hunter pass 5581a329b1cSAdrian Hunter---- 5591a329b1cSAdrian Hunter 5601a329b1cSAdrian Hunter*context_switch*, if defined, is called for any context switch 5611a329b1cSAdrian Hunter 5621a329b1cSAdrian Hunter---- 5631a329b1cSAdrian Hunterdef context_switch(ts, cpu, pid, tid, np_pid, np_tid, machine_pid, out, out_preempt, *x): 5641a329b1cSAdrian Hunter pass 5651a329b1cSAdrian Hunter---- 5661a329b1cSAdrian Hunter 5671a329b1cSAdrian Hunter*auxtrace_error*, if defined, is called for any AUX area tracing error 5681a329b1cSAdrian Hunter 5691a329b1cSAdrian Hunter---- 5701a329b1cSAdrian Hunterdef auxtrace_error(typ, code, cpu, pid, tid, ip, ts, msg, cpumode, *x): 5711a329b1cSAdrian Hunter pass 5721a329b1cSAdrian Hunter---- 5731a329b1cSAdrian Hunter 574133dc4c3SIngo MolnarThe remaining sections provide descriptions of each of the available 575133dc4c3SIngo Molnarbuilt-in perf script Python modules and their associated functions. 576133dc4c3SIngo Molnar 577133dc4c3SIngo MolnarAVAILABLE MODULES AND FUNCTIONS 578133dc4c3SIngo Molnar------------------------------- 579133dc4c3SIngo Molnar 580133dc4c3SIngo MolnarThe following sections describe the functions and variables available 581133dc4c3SIngo Molnarvia the various perf script Python modules. To use the functions and 582133dc4c3SIngo Molnarvariables from the given module, add the corresponding 'from XXXX 583133dc4c3SIngo Molnarimport' line to your perf script script. 584133dc4c3SIngo Molnar 585133dc4c3SIngo MolnarCore.py Module 586133dc4c3SIngo Molnar~~~~~~~~~~~~~~ 587133dc4c3SIngo Molnar 588133dc4c3SIngo MolnarThese functions provide some essential functions to user scripts. 589133dc4c3SIngo Molnar 590133dc4c3SIngo MolnarThe *flag_str* and *symbol_str* functions provide human-readable 591133dc4c3SIngo Molnarstrings for flag and symbolic fields. These correspond to the strings 592133dc4c3SIngo Molnarand values parsed from the 'print fmt' fields of the event format 593133dc4c3SIngo Molnarfiles: 594133dc4c3SIngo Molnar 59596355f2cSMasanari 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 59696355f2cSMasanari 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 597133dc4c3SIngo Molnar 598133dc4c3SIngo MolnarThe *autodict* function returns a special kind of Python 599133dc4c3SIngo Molnardictionary that implements Perl's 'autovivifying' hashes in Python 600133dc4c3SIngo Molnari.e. with autovivifying hashes, you can assign nested hash values 601133dc4c3SIngo Molnarwithout having to go to the trouble of creating intermediate levels if 602133dc4c3SIngo Molnarthey don't exist. 603133dc4c3SIngo Molnar 604133dc4c3SIngo Molnar autodict() - returns an autovivifying dictionary instance 605133dc4c3SIngo Molnar 606133dc4c3SIngo Molnar 607133dc4c3SIngo Molnarperf_trace_context Module 608133dc4c3SIngo Molnar~~~~~~~~~~~~~~~~~~~~~~~~~ 609133dc4c3SIngo Molnar 610133dc4c3SIngo MolnarSome of the 'common' fields in the event format file aren't all that 611133dc4c3SIngo Molnarcommon, but need to be made accessible to user scripts nonetheless. 612133dc4c3SIngo Molnar 613133dc4c3SIngo Molnarperf_trace_context defines a set of functions that can be used to 614133dc4c3SIngo Molnaraccess this data in the context of the current event. Each of these 615133dc4c3SIngo Molnarfunctions expects a context variable, which is the same as the 6161a329b1cSAdrian Huntercontext variable passed into every tracepoint event handler as the second 6171a329b1cSAdrian Hunterargument. For non-tracepoint events, the context variable is also present 6181a329b1cSAdrian Hunteras perf_trace_context.perf_script_context . 619133dc4c3SIngo Molnar 620133dc4c3SIngo Molnar common_pc(context) - returns common_preempt count for the current event 621133dc4c3SIngo Molnar common_flags(context) - returns common_flags for the current event 622133dc4c3SIngo Molnar common_lock_depth(context) - returns common_lock_depth for the current event 6231a329b1cSAdrian Hunter perf_sample_insn(context) - returns the machine code instruction 6241a329b1cSAdrian Hunter perf_set_itrace_options(context, itrace_options) - set --itrace options if they have not been set already 6251a329b1cSAdrian Hunter perf_sample_srcline(context) - returns source_file_name, line_number 6261a329b1cSAdrian Hunter perf_sample_srccode(context) - returns source_file_name, line_number, source_line 6271a329b1cSAdrian Hunter 628133dc4c3SIngo Molnar 629133dc4c3SIngo MolnarUtil.py Module 630133dc4c3SIngo Molnar~~~~~~~~~~~~~~ 631133dc4c3SIngo Molnar 632133dc4c3SIngo MolnarVarious utility functions for use with perf script: 633133dc4c3SIngo Molnar 634133dc4c3SIngo Molnar nsecs(secs, nsecs) - returns total nsecs given secs/nsecs pair 635133dc4c3SIngo Molnar nsecs_secs(nsecs) - returns whole secs portion given nsecs 636133dc4c3SIngo Molnar nsecs_nsecs(nsecs) - returns nsecs remainder given nsecs 637133dc4c3SIngo Molnar nsecs_str(nsecs) - returns printable string in the form secs.nsecs 638133dc4c3SIngo Molnar avg(total, n) - returns average given a sum and a total number of values 639133dc4c3SIngo Molnar 640ac56aa45SJin YaoSUPPORTED FIELDS 641ac56aa45SJin Yao---------------- 642ac56aa45SJin Yao 643ac56aa45SJin YaoCurrently supported fields: 644ac56aa45SJin Yao 645ac56aa45SJin Yaoev_name, comm, pid, tid, cpu, ip, time, period, phys_addr, addr, 6461a329b1cSAdrian Huntersymbol, symoff, dso, time_enabled, time_running, values, callchain, 647ac56aa45SJin Yaobrstack, brstacksym, datasrc, datasrc_decode, iregs, uregs, 6481a329b1cSAdrian Hunterweight, transaction, raw_buf, attr, cpumode. 6491a329b1cSAdrian Hunter 6501a329b1cSAdrian HunterFields that may also be present: 6511a329b1cSAdrian Hunter 6521a329b1cSAdrian Hunter flags - sample flags 6531a329b1cSAdrian Hunter flags_disp - sample flags display 6541a329b1cSAdrian Hunter insn_cnt - instruction count for determining instructions-per-cycle (IPC) 6551a329b1cSAdrian Hunter cyc_cnt - cycle count for determining IPC 6561a329b1cSAdrian Hunter addr_correlates_sym - addr can correlate to a symbol 6571a329b1cSAdrian Hunter addr_dso - addr dso 6581a329b1cSAdrian Hunter addr_symbol - addr symbol 6591a329b1cSAdrian Hunter addr_symoff - addr symbol offset 660ac56aa45SJin Yao 661ac56aa45SJin YaoSome fields have sub items: 662ac56aa45SJin Yao 663ac56aa45SJin Yaobrstack: 664ac56aa45SJin Yao from, to, from_dsoname, to_dsoname, mispred, 665ac56aa45SJin Yao predicted, in_tx, abort, cycles. 666ac56aa45SJin Yao 667ac56aa45SJin Yaobrstacksym: 668ac56aa45SJin Yao items: from, to, pred, in_tx, abort (converted string) 669ac56aa45SJin Yao 670ac56aa45SJin YaoFor example, 671ac56aa45SJin YaoWe can use this code to print brstack "from", "to", "cycles". 672ac56aa45SJin Yao 673ac56aa45SJin Yaoif 'brstack' in dict: 674ac56aa45SJin Yao for entry in dict['brstack']: 675ac56aa45SJin Yao print "from %s, to %s, cycles %s" % (entry["from"], entry["to"], entry["cycles"]) 676ac56aa45SJin Yao 677133dc4c3SIngo MolnarSEE ALSO 678133dc4c3SIngo Molnar-------- 679133dc4c3SIngo Molnarlinkperf:perf-script[1] 680