1 perf-script-python(1) 2 ==================== 3 4 NAME 5 ---- 6 perf-script-python - Process trace data with a 7 8 SYNOPSIS 9 -------- 10 [verse] 11 'perf script' [-s [Python]:script[.py] ] 12 13 DESCRIPTION 14 ----------- 15 16 This perf script option is used to process per 17 built-in Python interpreter. It reads and pro 18 displays the results of the trace analysis imp 19 Python script, if any. 20 21 A QUICK EXAMPLE 22 --------------- 23 24 This section shows the process, start to finis 25 Python script that aggregates and extracts use 26 raw perf script stream. You can avoid reading 27 document if an example is enough for you; the 28 provides more details on each step and lists t 29 available to script writers. 30 31 This example actually details the steps that w 32 'syscall-counts' script you see when you list 33 scripts via 'perf script -l'. As such, this s 34 integrate your script into the list of general 35 scripts listed by that command. 36 37 The syscall-counts script is a simple script, 38 basic ideas necessary to create a useful scrip 39 of its output (syscall names are not yet suppo 40 as numbers): 41 42 ---- 43 syscall events: 44 45 event 46 ---------------------------------------- ---- 47 sys_write 48 sys_getdents 49 sys_close 50 sys_swapoff 51 sys_read 52 sys_sched_setparam 53 sys_open 54 sys_newfstat 55 sys_mmap 56 sys_munmap 57 sys_futex 58 sys_select 59 sys_poll 60 sys_setitimer 61 sys_writev 62 15 63 sys_lseek 64 sys_rt_sigprocmask 65 sys_wait4 66 sys_ioctl 67 sys_set_robust_list 68 sys_exit 69 56 70 sys_access 71 ---- 72 73 Basically our task is to keep a per-syscall ta 74 every time a system call occurs in the system. 75 that, but first we need to record the data tha 76 that script. Theoretically, there are a coupl 77 that: 78 79 - we could enable every event under the tracin 80 directory, but this is over 600 syscalls, we 81 allowable by perf. These individual syscall 82 useful if we want to later use the guidance 83 general-purpose scripts to drill down and ge 84 individual syscalls of interest. 85 86 - we can enable the sys_enter and/or sys_exit 87 tracing/events/raw_syscalls. These are call 88 'id' field can be used to distinguish betwee 89 numbers. 90 91 For this script, we only need to know that a s 92 don't care how it exited, so we'll use 'perf r 93 the sys_enter events: 94 95 ---- 96 # perf record -a -e raw_syscalls:sys_enter 97 98 ^C[ perf record: Woken up 1 times to write dat 99 [ perf record: Captured and wrote 56.545 MB pe 100 ---- 101 102 The options basically say to collect data for 103 system-wide and multiplex the per-cpu output i 104 That single stream will be recorded in a file 105 called perf.data. 106 107 Once we have a perf.data file containing our d 108 'perf script' option to generate a Python scri 109 callback handler for each event type found in 110 stream (for more details, see the STARTER SCRI 111 112 ---- 113 # perf script -g python 114 generated Python script: perf-script.py 115 116 The output file created also in the current di 117 perf-script.py. Here's the file in its entire 118 119 # perf script event handlers, generated by per 120 # Licensed under the terms of the GNU GPL Lice 121 122 # The common_* event handler fields are the mo 123 # all events. They don't necessarily correspo 124 # in the format files. Those fields not avail 125 # be retrieved using Python functions of the f 126 # See the perf-script-python Documentation for 127 128 import os 129 import sys 130 131 sys.path.append(os.environ['PERF_EXEC_PATH'] + 132 '/scripts/python/Perf-Trace-Util/lib/P 133 134 from perf_trace_context import * 135 from Core import * 136 137 def trace_begin(): 138 print "in trace_begin" 139 140 def trace_end(): 141 print "in trace_end" 142 143 def raw_syscalls__sys_enter(event_name, contex 144 common_secs, common_nsecs, common_pid, 145 id, args): 146 print_header(event_name, commo 147 common_pid, common_com 148 149 print "id=%d, args=%s\n" % \ 150 (id, args), 151 152 def trace_unhandled(event_name, context, event 153 print ' '.join(['%s=%s'%(k,str 154 155 def print_header(event_name, cpu, secs, nsecs, 156 print "%-20s %5u %05u.%09u %8u %-20s " 157 (event_name, cpu, secs, nsecs, pid, co 158 ---- 159 160 At the top is a comment block followed by some 161 path append which every perf script script sho 162 163 Following that are a couple generated function 164 trace_end(), which are called at the beginning 165 script respectively (for more details, see the 166 below). 167 168 Following those are the 'event handler' functi 169 every event in the 'perf record' output. The 170 the form subsystem\__event_name, and contain n 171 each field in the event; in this case, there's 172 raw_syscalls__sys_enter(). (see the EVENT HAN 173 more info on event handlers). 174 175 The final couple of functions are, like the be 176 generated for every script. The first, trace_ 177 every time the script finds an event in the pe 178 doesn't correspond to any event handler in the 179 mean either that the record step recorded even 180 really interested in, or the script was run ag 181 doesn't correspond to the script. 182 183 The script generated by -g option simply print 184 event found in the trace stream i.e. it basica 185 and its parameter values to stdout. The print 186 simply a utility function used for that purpos 187 script and run it to see the default output: 188 189 ---- 190 # mv perf-script.py syscall-counts.py 191 # perf script -s syscall-counts.py 192 193 raw_syscalls__sys_enter 1 00840.847582083 194 raw_syscalls__sys_enter 1 00840.847595764 195 raw_syscalls__sys_enter 1 00840.847620860 196 raw_syscalls__sys_enter 1 00840.847710478 197 raw_syscalls__sys_enter 1 00840.847719204 198 raw_syscalls__sys_enter 1 00840.847755445 199 raw_syscalls__sys_enter 1 00840.847775601 200 raw_syscalls__sys_enter 1 00840.847781820 201 . 202 . 203 . 204 ---- 205 206 Of course, for this script, we're not interest 207 trace event, but rather aggregating it in a us 208 rid of everything to do with printing as well 209 trace_unhandled() functions, which we won't be 210 with this minimalistic skeleton: 211 212 ---- 213 import os 214 import sys 215 216 sys.path.append(os.environ['PERF_EXEC_PATH'] + 217 '/scripts/python/Perf-Trace-Util/lib/P 218 219 from perf_trace_context import * 220 from Core import * 221 222 def trace_end(): 223 print "in trace_end" 224 225 def raw_syscalls__sys_enter(event_name, contex 226 common_secs, common_nsecs, common_pid, 227 id, args): 228 ---- 229 230 In trace_end(), we'll simply print the results 231 generate some results to print. To do that we 232 sys_enter() handler do the necessary tallying 233 been counted. A hash table indexed by syscall 234 store that information; every time the sys_ent 235 we simply increment a count associated with th 236 that syscall id: 237 238 ---- 239 syscalls = autodict() 240 241 try: 242 syscalls[id] += 1 243 except TypeError: 244 syscalls[id] = 1 245 ---- 246 247 The syscalls 'autodict' object is a special ki 248 (implemented in Core.py) that implements Perl' 249 in Python i.e. with autovivifying hashes, you 250 values without having to go to the trouble of 251 levels if they don't exist e.g syscalls[comm][ 252 the intermediate hash levels and finally assig 253 hash entry for 'id' (because the value being a 254 object itself, the initial value is assigned i 255 exception. Well, there may be a better way to 256 that's what works for now). 257 258 Putting that code into the raw_syscalls__sys_e 259 effectively end up with a single-level diction 260 and having the counts we've tallied as values. 261 262 The print_syscall_totals() function iterates o 263 dictionary and displays a line for each entry 264 name (the dictionary keys contain the syscall 265 the Util function syscall_name(), which transl 266 numbers to the corresponding syscall name stri 267 displayed after all the events in the trace ha 268 calling the print_syscall_totals() function fr 269 handler called at the end of script processing 270 271 The final script producing the output shown ab 272 entirety below (syscall_name() helper is not y 273 only deal with id's for now): 274 275 ---- 276 import os 277 import sys 278 279 sys.path.append(os.environ['PERF_EXEC_PATH'] + 280 '/scripts/python/Perf-Trace-Util/lib/P 281 282 from perf_trace_context import * 283 from Core import * 284 from Util import * 285 286 syscalls = autodict() 287 288 def trace_end(): 289 print_syscall_totals() 290 291 def raw_syscalls__sys_enter(event_name, contex 292 common_secs, common_nsecs, common_pid, 293 id, args): 294 try: 295 syscalls[id] += 1 296 except TypeError: 297 syscalls[id] = 1 298 299 def print_syscall_totals(): 300 if for_comm is not None: 301 print "\nsyscall events for %s:\n\ 302 else: 303 print "\nsyscall events:\n\n", 304 305 print "%-40s %10s\n" % ("event", "count") 306 print "%-40s %10s\n" % ("---------------- 307 "-----------" 308 309 for id, val in sorted(syscalls.iteritems() 310 reverse = Tr 311 print "%-40s %10d\n" % (syscall_n 312 ---- 313 314 The script can be run just as before: 315 316 # perf script -s syscall-counts.py 317 318 So those are the essential steps in writing an 319 process can be generalized to any tracepoint o 320 you're interested in - basically find the trac 321 interested in by looking at the list of availa 322 'perf list' and/or look in /sys/kernel/tracing 323 detailed event and field info, record the corr 324 using 'perf record', passing it the list of in 325 generate a skeleton script using 'perf script 326 code to aggregate and display it for your part 327 328 After you've done that you may end up with a g 329 that you want to keep around and have availabl 330 writing a couple of very simple shell scripts 331 right place, you can have your script listed a 332 scripts listed by the 'perf script -l' command 333 334 ---- 335 # perf script -l 336 List of available trace scripts: 337 wakeup-latency system- 338 rw-by-file <comm> r/w act 339 rw-by-pid system- 340 ---- 341 342 A nice side effect of doing this is that you a 343 probably lengthy 'perf record' command needed 344 the script. 345 346 To have the script appear as a 'built-in' scri 347 scripts, one for recording and one for 'report 348 349 The 'record' script is a shell script with the 350 script, but with -record appended. The shell 351 into the perf/scripts/python/bin directory in 352 In that script, you write the 'perf record' co 353 your script: 354 355 ---- 356 # cat kernel-source/tools/perf/scripts/python/ 357 358 #!/bin/bash 359 perf record -a -e raw_syscalls:sys_enter 360 ---- 361 362 The 'report' script is also a shell script wit 363 your script, but with -report appended. It sh 364 the perf/scripts/python/bin directory. In tha 365 'perf script -s' command-line needed for runni 366 367 ---- 368 # cat kernel-source/tools/perf/scripts/python/ 369 370 #!/bin/bash 371 # description: system-wide syscall counts 372 perf script -s ~/libexec/perf-core/scripts/pyt 373 ---- 374 375 Note that the location of the Python script gi 376 is in the libexec/perf-core/scripts/python dir 377 the script will be copied by 'make install' wh 378 For the installation to install your script th 379 to be located in the perf/scripts/python direc 380 source tree: 381 382 ---- 383 # ls -al kernel-source/tools/perf/scripts/pyth 384 total 32 385 drwxr-xr-x 4 trz trz 4096 2010-01-26 22:30 . 386 drwxr-xr-x 4 trz trz 4096 2010-01-26 22:29 .. 387 drwxr-xr-x 2 trz trz 4096 2010-01-26 22:29 bin 388 -rw-r--r-- 1 trz trz 2548 2010-01-26 22:29 che 389 drwxr-xr-x 3 trz trz 4096 2010-01-26 22:49 Per 390 -rw-r--r-- 1 trz trz 1462 2010-01-26 22:30 sys 391 ---- 392 393 Once you've done that (don't forget to do a ne 394 otherwise your script won't show up at run-tim 395 should show a new entry for your script: 396 397 ---- 398 # perf script -l 399 List of available trace scripts: 400 wakeup-latency system- 401 rw-by-file <comm> r/w act 402 rw-by-pid system- 403 syscall-counts system- 404 ---- 405 406 You can now perform the record step via 'perf 407 408 # perf script record syscall-counts 409 410 and display the output using 'perf script repo 411 412 # perf script report syscall-counts 413 414 STARTER SCRIPTS 415 --------------- 416 417 You can quickly get started writing a script f 418 trace data by generating a skeleton script usi 419 python' in the same directory as an existing p 420 That will generate a starter script containing 421 the event types in the trace file; it simply p 422 field for each event in the trace file. 423 424 You can also look at the existing scripts in 425 ~/libexec/perf-core/scripts/python for typical 426 do basic things like aggregate event data, pri 427 the check-perf-script.py script, while not int 428 attempts to exercise all of the main scripting 429 430 EVENT HANDLERS 431 -------------- 432 433 When perf script is invoked using a trace scri 434 'handler function' is called for each event in 435 no handler function defined for a given event 436 ignored (or passed to a 'trace_unhandled' func 437 next event is processed. 438 439 Most of the event's field values are passed as 440 handler function; some of the less common ones 441 available as calls back into the perf executab 442 443 As an example, the following perf record comma 444 all sched_wakeup events in the system: 445 446 # perf record -a -e sched:sched_wakeup 447 448 Traces meant to be processed using a script sh 449 the above option: -a to enable system-wide col 450 451 The format file for the sched_wakeup event def 452 (see /sys/kernel/tracing/events/sched/sched_wa 453 454 ---- 455 format: 456 field:unsigned short common_type; 457 field:unsigned char common_flags; 458 field:unsigned char common_preempt_cou 459 field:int common_pid; 460 461 field:char comm[TASK_COMM_LEN]; 462 field:pid_t pid; 463 field:int prio; 464 field:int success; 465 field:int target_cpu; 466 ---- 467 468 The handler function for this event would be d 469 470 ---- 471 def sched__sched_wakeup(event_name, context, c 472 common_nsecs, common_pid, common_comm, 473 comm, pid, prio, success, target_cpu): 474 pass 475 ---- 476 477 The handler function takes the form subsystem_ 478 479 The common_* arguments in the handler's argume 480 arguments passed to all event handlers; some o 481 to the common_* fields in the format file, but 482 and some of the common_* fields aren't common 483 to every event as arguments but are available 484 485 Here's a brief description of each of the inva 486 487 event_name the name of the ev 488 context an opaque 'cookie' 489 common_cpu the cpu the event 490 common_secs the secs portion o 491 common_nsecs the nsecs portion 492 common_pid the pid of the cur 493 common_comm the name of the cu 494 495 All of the remaining fields in the event's for 496 counterparts as handler function arguments of 497 seen in the example above. 498 499 The above provides the basics needed to direct 500 every event in a trace, which covers 90% of wh 501 write a useful trace script. The sections bel 502 503 SCRIPT LAYOUT 504 ------------- 505 506 Every perf script Python script should start b 507 module search path and 'import'ing a few suppo 508 descriptions below): 509 510 ---- 511 import os 512 import sys 513 514 sys.path.append(os.environ['PERF_EXEC_PATH'] 515 '/scripts/python/Perf-Trace-Util 516 517 from perf_trace_context import * 518 from Core import * 519 ---- 520 521 The rest of the script can contain handler fun 522 functions in any order. 523 524 Aside from the event handler functions discuss 525 can implement a set of optional functions: 526 527 *trace_begin*, if defined, is called before an 528 gives scripts a chance to do setup tasks: 529 530 ---- 531 def trace_begin(): 532 pass 533 ---- 534 535 *trace_end*, if defined, is called after all e 536 processed and gives scripts a chance to do en 537 as display results: 538 539 ---- 540 def trace_end(): 541 pass 542 ---- 543 544 *trace_unhandled*, if defined, is called after 545 doesn't have a handler explicitly defined for 546 of common arguments are passed into it: 547 548 ---- 549 def trace_unhandled(event_name, context, event 550 pass 551 ---- 552 553 *process_event*, if defined, is called for any 554 555 ---- 556 def process_event(param_dict): 557 pass 558 ---- 559 560 *context_switch*, if defined, is called for an 561 562 ---- 563 def context_switch(ts, cpu, pid, tid, np_pid, 564 pass 565 ---- 566 567 *auxtrace_error*, if defined, is called for an 568 569 ---- 570 def auxtrace_error(typ, code, cpu, pid, tid, i 571 pass 572 ---- 573 574 The remaining sections provide descriptions of 575 built-in perf script Python modules and their 576 577 AVAILABLE MODULES AND FUNCTIONS 578 ------------------------------- 579 580 The following sections describe the functions 581 via the various perf script Python modules. T 582 variables from the given module, add the corre 583 import' line to your perf script script. 584 585 Core.py Module 586 ~~~~~~~~~~~~~~ 587 588 These functions provide some essential functio 589 590 The *flag_str* and *symbol_str* functions prov 591 strings for flag and symbolic fields. These c 592 and values parsed from the 'print fmt' fields 593 files: 594 595 flag_str(event_name, field_name, field_value 596 symbol_str(event_name, field_name, field_val 597 598 The *autodict* function returns a special kind 599 dictionary that implements Perl's 'autovivifyi 600 i.e. with autovivifying hashes, you can assign 601 without having to go to the trouble of creatin 602 they don't exist. 603 604 autodict() - returns an autovivifying dictio 605 606 607 perf_trace_context Module 608 ~~~~~~~~~~~~~~~~~~~~~~~~~ 609 610 Some of the 'common' fields in the event forma 611 common, but need to be made accessible to user 612 613 perf_trace_context defines a set of functions 614 access this data in the context of the current 615 functions expects a context variable, which is 616 context variable passed into every tracepoint 617 argument. For non-tracepoint events, the conte 618 as perf_trace_context.perf_script_context . 619 620 common_pc(context) - returns common_preempt c 621 common_flags(context) - returns common_flags 622 common_lock_depth(context) - returns common_l 623 perf_sample_insn(context) - returns the machi 624 perf_set_itrace_options(context, itrace_optio 625 perf_sample_srcline(context) - returns source 626 perf_sample_srccode(context) - returns source 627 628 629 Util.py Module 630 ~~~~~~~~~~~~~~ 631 632 Various utility functions for use with perf sc 633 634 nsecs(secs, nsecs) - returns total nsecs giv 635 nsecs_secs(nsecs) - returns whole secs porti 636 nsecs_nsecs(nsecs) - returns nsecs remainder 637 nsecs_str(nsecs) - returns printable string 638 avg(total, n) - returns average given a sum 639 640 SUPPORTED FIELDS 641 ---------------- 642 643 Currently supported fields: 644 645 ev_name, comm, id, stream_id, pid, tid, cpu, i 646 addr, symbol, symoff, dso, time_enabled, time_ 647 brstack, brstacksym, datasrc, datasrc_decode, 648 weight, transaction, raw_buf, attr, cpumode. 649 650 Fields that may also be present: 651 652 flags - sample flags 653 flags_disp - sample flags display 654 insn_cnt - instruction count for determining 655 cyc_cnt - cycle count for determining IPC 656 addr_correlates_sym - addr can correlate to a 657 addr_dso - addr dso 658 addr_symbol - addr symbol 659 addr_symoff - addr symbol offset 660 661 Some fields have sub items: 662 663 brstack: 664 from, to, from_dsoname, to_dsoname, mispre 665 predicted, in_tx, abort, cycles. 666 667 brstacksym: 668 items: from, to, pred, in_tx, abort (conve 669 670 For example, 671 We can use this code to print brstack "from", 672 673 if 'brstack' in dict: 674 for entry in dict['brstack']: 675 print "from %s, to %s, cycles 676 677 SEE ALSO 678 -------- 679 linkperf:perf-script[1]
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.