stackcollapse-jstack.pl (5207B)
1 #!/usr/bin/perl -w 2 # 3 # stackcollapse-jstack.pl collapse jstack samples into single lines. 4 # 5 # Parses Java stacks generated by jstack(1) and outputs RUNNABLE stacks as 6 # single lines, with methods separated by semicolons, and then a space and an 7 # occurrence count. This also filters some other "RUNNABLE" states that we 8 # know are probably not running, such as epollWait. For use with flamegraph.pl. 9 # 10 # You want this to process the output of at least 100 jstack(1)s. ie, run it 11 # 100 times with a sleep interval, and append to a file. This is really a poor 12 # man's Java profiler, due to the overheads of jstack(1), and how it isn't 13 # capturing stacks asynchronously. For a better profiler, see: 14 # http://www.brendangregg.com/blog/2014-06-12/java-flame-graphs.html 15 # 16 # USAGE: ./stackcollapse-jstack.pl infile > outfile 17 # 18 # Example input: 19 # 20 # "MyProg" #273 daemon prio=9 os_prio=0 tid=0x00007f273c038800 nid=0xe3c runnable [0x00007f28a30f2000] 21 # java.lang.Thread.State: RUNNABLE 22 # at java.net.SocketInputStream.socketRead0(Native Method) 23 # at java.net.SocketInputStream.read(SocketInputStream.java:121) 24 # ... 25 # at java.lang.Thread.run(Thread.java:744) 26 # 27 # Example output: 28 # 29 # MyProg;java.lang.Thread.run;java.net.SocketInputStream.read;java.net.SocketInputStream.socketRead0 1 30 # 31 # Input may be created and processed using: 32 # 33 # i=0; while (( i++ < 200 )); do jstack PID >> out.jstacks; sleep 10; done 34 # cat out.jstacks | ./stackcollapse-jstack.pl > out.stacks-folded 35 # 36 # WARNING: jstack(1) incurs overheads. Test before use, or use a real profiler. 37 # 38 # Copyright 2014 Brendan Gregg. All rights reserved. 39 # 40 # This program is free software; you can redistribute it and/or 41 # modify it under the terms of the GNU General Public License 42 # as published by the Free Software Foundation; either version 2 43 # of the License, or (at your option) any later version. 44 # 45 # This program is distributed in the hope that it will be useful, 46 # but WITHOUT ANY WARRANTY; without even the implied warranty of 47 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 48 # GNU General Public License for more details. 49 # 50 # You should have received a copy of the GNU General Public License 51 # along with this program; if not, write to the Free Software Foundation, 52 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 53 # 54 # (http://www.gnu.org/copyleft/gpl.html) 55 # 56 # 14-Sep-2014 Brendan Gregg Created this. 57 58 use strict; 59 60 use Getopt::Long; 61 62 # tunables 63 my $include_tname = 1; # include thread names in stacks 64 my $include_tid = 0; # include thread IDs in stacks 65 my $shorten_pkgs = 0; # shorten package names 66 my $help = 0; 67 68 sub usage { 69 die <<USAGE_END; 70 USAGE: $0 [options] infile > outfile\n 71 --include-tname 72 --no-include-tname # include/omit thread names in stacks (default: include) 73 --include-tid 74 --no-include-tid # include/omit thread IDs in stacks (default: omit) 75 --shorten-pkgs 76 --no-shorten-pkgs # (don't) shorten package names (default: don't shorten) 77 78 eg, 79 $0 --no-include-tname stacks.txt > collapsed.txt 80 USAGE_END 81 } 82 83 GetOptions( 84 'include-tname!' => \$include_tname, 85 'include-tid!' => \$include_tid, 86 'shorten-pkgs!' => \$shorten_pkgs, 87 'help' => \$help, 88 ) or usage(); 89 $help && usage(); 90 91 92 # internals 93 my %collapsed; 94 95 sub remember_stack { 96 my ($stack, $count) = @_; 97 $collapsed{$stack} += $count; 98 } 99 100 my @stack; 101 my $tname; 102 my $state = "?"; 103 104 foreach (<>) { 105 next if m/^#/; 106 chomp; 107 108 if (m/^$/) { 109 # only include RUNNABLE states 110 goto clear if $state ne "RUNNABLE"; 111 112 # save stack 113 if (defined $tname) { unshift @stack, $tname; } 114 remember_stack(join(";", @stack), 1) if @stack; 115 clear: 116 undef @stack; 117 undef $tname; 118 $state = "?"; 119 next; 120 } 121 122 # 123 # While parsing jstack output, the $state variable may be altered from 124 # RUNNABLE to other states. This causes the stacks to be filtered later, 125 # since only RUNNABLE stacks are included. 126 # 127 128 if (/^"([^"]*)/) { 129 my $name = $1; 130 131 if ($include_tname) { 132 $tname = $name; 133 unless ($include_tid) { 134 $tname =~ s/-\d+$//; 135 } 136 } 137 138 # set state for various background threads 139 $state = "BACKGROUND" if $name =~ /C. CompilerThread/; 140 $state = "BACKGROUND" if $name =~ /Signal Dispatcher/; 141 $state = "BACKGROUND" if $name =~ /Service Thread/; 142 $state = "BACKGROUND" if $name =~ /Attach Listener/; 143 144 } elsif (/java.lang.Thread.State: (\S+)/) { 145 $state = $1 if $state eq "?"; 146 } elsif (/^\s*at ([^\(]*)/) { 147 my $func = $1; 148 if ($shorten_pkgs) { 149 my ($pkgs, $clsFunc) = ( $func =~ m/(.*\.)([^.]+\.[^.]+)$/ ); 150 $pkgs =~ s/(\w)\w*/$1/g; 151 $func = $pkgs . $clsFunc; 152 } 153 unshift @stack, $func; 154 155 # fix state for epollWait 156 $state = "WAITING" if $func =~ /epollWait/; 157 $state = "WAITING" if $func =~ /EPoll\.wait/; 158 159 160 # fix state for various networking functions 161 $state = "NETWORK" if $func =~ /socketAccept$/; 162 $state = "NETWORK" if $func =~ /Socket.*accept0$/; 163 $state = "NETWORK" if $func =~ /socketRead0$/; 164 165 } elsif (/^\s*-/ or /^2\d\d\d-/ or /^Full thread dump/ or 166 /^JNI global references:/) { 167 # skip these info lines 168 next; 169 } else { 170 warn "Unrecognized line: $_"; 171 } 172 } 173 174 foreach my $k (sort { $a cmp $b } keys %collapsed) { 175 print "$k $collapsed{$k}\n"; 176 }