pkgsplit-perf.pl (2710B)
1 #!/usr/bin/perl -w 2 # 3 # pkgsplit-perf.pl Split IP samples on package names "/", eg, Java. 4 # 5 # This is for the creation of Java package flame graphs. Example steps: 6 # 7 # perf record -F 199 -a -- sleep 30; ./jmaps 8 # perf script | ./pkgsplit-perf.pl | ./flamegraph.pl > out.svg 9 # 10 # Note that stack traces are not sampled (no -g), as we split Java package 11 # names into frames rather than stack frames. 12 # 13 # (jmaps is a helper script for automating perf-map-agent: Java symbol dumps.) 14 # 15 # The default output of "perf script" varies between kernel versions, so we'll 16 # need to deal with that here. I could make people use the perf script option 17 # to pick fields, so our input is static, but A) I prefer the simplicity of 18 # just saying: run "perf script", and B) the option to choose fields itself 19 # changed between kernel versions! -f became -F. 20 # 21 # Copyright 2017 Netflix, Inc. 22 # Licensed under the Apache License, Version 2.0 (the "License") 23 # 24 # 20-Sep-2016 Brendan Gregg Created this. 25 26 use strict; 27 28 my $include_pname = 1; # include process names in stacks 29 my $include_pid = 0; # include process ID with process name 30 my $include_tid = 0; # include process & thread ID with process name 31 32 while (<>) { 33 # filter comments 34 next if /^#/; 35 36 # filter idle events 37 next if /xen_hypercall_sched_op|cpu_idle|native_safe_halt/; 38 39 my ($pid, $tid, $pname); 40 41 # Linux 3.13: 42 # java 13905 [000] 8048.096572: cpu-clock: 7fd781ac3053 Ljava/util/Arrays$ArrayList;::toArray (/tmp/perf-12149.map) 43 # java 8301 [050] 13527.392454: cycles: 7fa8a80d9bff Dictionary::find(int, unsigned int, Symbol*, ClassLoaderData*, Handle, Thread*) (/usr/lib/jvm/java-8-oracle-1.8.0.121/jre/lib/amd64/server/libjvm.so) 44 # java 4567/8603 [023] 13527.389886: cycles: 7fa863349895 Lcom/google/gson/JsonObject;::add (/tmp/perf-4567.map) 45 # 46 # Linux 4.8: 47 # java 30894 [007] 452884.077440: 10101010 cpu-clock: 7f0acc8eff67 Lsun/nio/ch/SocketChannelImpl;::read+0x27 (/tmp/perf-30849.map) 48 # bash 26858/26858 [006] 5440237.995639: cpu-clock: 433573 [unknown] (/bin/bash) 49 # 50 if (/^\s+(\S.+?)\s+(\d+)\/*(\d+)*\s.*?:.*:/) { 51 # parse process name and pid/tid 52 if ($3) { 53 ($pid, $tid) = ($2, $3); 54 } else { 55 ($pid, $tid) = ("?", $2); 56 } 57 58 if ($include_tid) { 59 $pname = "$1-$pid/$tid"; 60 } elsif ($include_pid) { 61 $pname = "$1-$pid"; 62 } else { 63 $pname = $1; 64 } 65 $pname =~ tr/ /_/; 66 } else { 67 # not a match 68 next; 69 } 70 71 # parse rest of line 72 s/^.*?:.*?:\s+//; 73 s/ \(.*?\)$//; 74 chomp; 75 my ($addr, $func) = split(' ', $_, 2); 76 77 # strip Java's leading "L" 78 $func =~ s/^L//; 79 80 # replace numbers with X 81 $func =~ s/[0-9]/X/g; 82 83 # colon delimitered 84 $func =~ s:/:;:g; 85 print "$pname;$func 1\n"; 86 }