range-perf.pl (4517B)
1 #!/usr/bin/perl -w 2 # 3 # range-perf Extract a time range from Linux "perf script" output. 4 # 5 # USAGE EXAMPLE: 6 # 7 # perf record -F 100 -a -- sleep 60 8 # perf script | ./perf2range.pl 10 20 # range 10 to 20 seconds only 9 # perf script | ./perf2range.pl 0 0.5 # first half second only 10 # 11 # MAKING A SERIES OF FLAME GRAPHS: 12 # 13 # Let's say you had the output of "perf script" in a file, out.stacks01, which 14 # was for a 180 second profile. The following command creates a series of 15 # flame graphs for each 10 second interval: 16 # 17 # for i in `seq 0 10 170`; do cat out.stacks01 | \ 18 # ./perf2range.pl $i $((i + 10)) | ./stackcollapse-perf.pl | \ 19 # grep -v cpu_idle | ./flamegraph.pl --hash --color=java \ 20 # --title="range $i $((i + 10))" > out.range_$i.svg; echo $i done; done 21 # 22 # In that example, I used "--color=java" for the Java palette, and excluded 23 # the idle CPU task. Customize as needed. 24 # 25 # Copyright 2017 Netflix, Inc. 26 # Licensed under the Apache License, Version 2.0 (the "License") 27 # 28 # 21-Feb-2017 Brendan Gregg Created this. 29 30 use strict; 31 use Getopt::Long; 32 use POSIX 'floor'; 33 34 sub usage { 35 die <<USAGE_END; 36 USAGE: $0 [options] min_seconds max_seconds 37 --timeraw # use raw timestamps from perf 38 --timezerosecs # time starts at 0 secs, but keep offset from perf 39 eg, 40 $0 10 20 # only include samples between 10 and 20 seconds 41 USAGE_END 42 } 43 44 my $timeraw = 0; 45 my $timezerosecs = 0; 46 GetOptions( 47 'timeraw' => \$timeraw, 48 'timezerosecs' => \$timezerosecs, 49 ) or usage(); 50 51 if (@ARGV < 2 || $ARGV[0] eq "-h" || $ARGV[0] eq "--help") { 52 usage(); 53 exit; 54 } 55 my $begin = $ARGV[0]; 56 my $end = $ARGV[1]; 57 58 # 59 # Parsing 60 # 61 # IP only examples: 62 # 63 # java 52025 [026] 99161.926202: cycles: 64 # java 14341 [016] 252732.474759: cycles: 7f36571947c0 nmethod::is_nmethod() const (/... 65 # java 14514 [022] 28191.353083: cpu-clock: 7f92b4fdb7d4 Ljava_util_List$size$0;::call (/tmp/perf-11936.map) 66 # swapper 0 [002] 6035557.056977: 10101010 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (/lib/modules/4.9-virtual/build/vmlinux) 67 # bash 25370 603are 6036.991603: 10101010 cpu-clock: 4b931e [unknown] (/bin/bash) 68 # bash 25370/25370 6036036.799684: cpu-clock: 4b913b [unknown] (/bin/bash) 69 # other combinations are possible. 70 # 71 # Stack examples (-g): 72 # 73 # swapper 0 [021] 28648.467059: cpu-clock: 74 # ffffffff810013aa xen_hypercall_sched_op ([kernel.kallsyms]) 75 # ffffffff8101cb2f default_idle ([kernel.kallsyms]) 76 # ffffffff8101d406 arch_cpu_idle ([kernel.kallsyms]) 77 # ffffffff810bf475 cpu_startup_entry ([kernel.kallsyms]) 78 # ffffffff81010228 cpu_bringup_and_idle ([kernel.kallsyms]) 79 # 80 # java 14375 [022] 28648.467079: cpu-clock: 81 # 7f92bdd98965 Ljava/io/OutputStream;::write (/tmp/perf-11936.map) 82 # 7f8808cae7a8 [unknown] ([unknown]) 83 # 84 # swapper 0 [005] 5076.836336: cpu-clock: 85 # ffffffff81051586 native_safe_halt ([kernel.kallsyms]) 86 # ffffffff8101db4f default_idle ([kernel.kallsyms]) 87 # ffffffff8101e466 arch_cpu_idle ([kernel.kallsyms]) 88 # ffffffff810c2b31 cpu_startup_entry ([kernel.kallsyms]) 89 # ffffffff810427cd start_secondary ([kernel.kallsyms]) 90 # 91 # swapper 0 [002] 6034779.719110: 10101010 cpu-clock: 92 # 2013aa xen_hypercall_sched_op+0xfe20000a (/lib/modules/4.9-virtual/build/vmlinux) 93 # a72f0e default_idle+0xfe20001e (/lib/modules/4.9-virtual/build/vmlinux) 94 # 2392bf arch_cpu_idle+0xfe20000f (/lib/modules/4.9-virtual/build/vmlinux) 95 # a73333 default_idle_call+0xfe200023 (/lib/modules/4.9-virtual/build/vmlinux) 96 # 2c91a4 cpu_startup_entry+0xfe2001c4 (/lib/modules/4.9-virtual/build/vmlinux) 97 # 22b64a cpu_bringup_and_idle+0xfe20002a (/lib/modules/4.9-virtual/build/vmlinux) 98 # 99 # bash 25370/25370 6035935.188539: cpu-clock: 100 # b9218 [unknown] (/bin/bash) 101 # 2037fe8 [unknown] ([unknown]) 102 # other combinations are possible. 103 # 104 # This regexp matches the event line, and puts time in $1, and the event name 105 # in $2: 106 # 107 my $event_regexp = qr/ +([0-9\.]+): *\S* *(\S+):/; 108 109 my $line; 110 my $start = 0; 111 my $ok = 0; 112 my $time; 113 114 while (1) { 115 $line = <STDIN>; 116 last unless defined $line; 117 next if $line =~ /^#/; # skip comments 118 119 if ($line =~ $event_regexp) { 120 my ($ts, $event) = ($1, $2, $3); 121 $start = $ts if $start == 0; 122 123 if ($timezerosecs) { 124 $time = $ts - floor($start); 125 } elsif (!$timeraw) { 126 $time = $ts - $start; 127 } else { 128 $time = $ts; # raw times 129 } 130 131 $ok = 1 if $time >= $begin; 132 # assume samples are in time order: 133 exit if $time > $end; 134 } 135 136 print $line if $ok; 137 }