flamegraph

Fork of flamegraph without j***s*****
git clone git://git.laack.co/flamegraph.git
Log | Files | Refs | README

stackcollapse-ibmjava.pl (4831B)


      1 #!/usr/bin/perl -w
      2 #
      3 # stackcollapse-ibmjava.pl	collapse jstack samples into single lines.
      4 #
      5 # Parses Java stacks generated by IBM Java with methods separated by semicolons, 
      6 # and then a space and an occurrence count.
      7 #
      8 # USAGE: ./stackcollapse-ibmjava.pl infile > outfile
      9 #
     10 # Example input:
     11 #
     12 #  NULL           
     13 #  1XMTHDINFO     Thread Details
     14 #  NULL           
     15 #  NULL
     16 #  3XMTHREADINFO      "Default Executor-thread-149164" J9VMThread:0x0000000008132B00, j9thread_t:0x000000001A810B90, java/lang/Thread:0x0000000712BE8E48, state:R, prio=5
     17 #  3XMJAVALTHREAD            (java/lang/Thread getId:0x3493E, isDaemon:true)
     18 #  3XMTHREADINFO1            (native thread ID:0x3158, native priority:0x5, native policy:UNKNOWN, vmstate:R, vm thread flags:0x00000001)
     19 #  3XMCPUTIME               CPU usage total: 0.421875000 secs, user: 0.343750000 secs, system: 0.078125000 secs, current category="Application"
     20 #  3XMHEAPALLOC             Heap bytes allocated since last GC cycle=0 (0x0)
     21 #  3XMTHREADINFO3           Java callstack:
     22 #  4XESTACKTRACE                at java/net/SocketInputStream.socketRead0(Native Method)
     23 #  4XESTACKTRACE                at java/net/SocketInputStream.socketRead(SocketInputStream.java:127(Compiled Code))
     24 #  4XESTACKTRACE                at java/net/SocketInputStream.read(SocketInputStream.java:182(Compiled Code))
     25 #  4XESTACKTRACE                at java/net/SocketInputStream.read(SocketInputStream.java:152(Compiled Code))
     26 #  4XESTACKTRACE                at java/io/FilterInputStream.read(FilterInputStream.java:144(Compiled Code))
     27 #  ...
     28 #  4XESTACKTRACE                at java/lang/Thread.run(Thread.java:785(Compiled Code))
     29 #
     30 # Example output:
     31 #
     32 #  Default Executor-thread-149164;java/lang/Thread.run;java/net/SocketInputStream/read;java/net/SocketInputStream.socketRead0 1
     33 #
     34 #
     35 # Copyright 2014 Federico Juinio.  All rights reserved.
     36 #
     37 #  This program is free software; you can redistribute it and/or
     38 #  modify it under the terms of the GNU General Public License
     39 #  as published by the Free Software Foundation; either version 2
     40 #  of the License, or (at your option) any later version.
     41 #
     42 #  This program is distributed in the hope that it will be useful,
     43 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
     44 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     45 #  GNU General Public License for more details.
     46 #
     47 #  You should have received a copy of the GNU General Public License
     48 #  along with this program; if not, write to the Free Software Foundation,
     49 #  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     50 #
     51 #  (http://www.gnu.org/copyleft/gpl.html)
     52 #
     53 # 23-Aug-2023   Federico Juinio created this based from stackcollapse-jstack.pl
     54 
     55 use strict;
     56 
     57 use Getopt::Long;
     58 
     59 # tunables
     60 my $include_tname = 1;		# include thread names in stacks
     61 my $include_tid = 0;		# include thread IDs in stacks
     62 my $shorten_pkgs = 0;		# shorten package names
     63 my $help = 0;
     64 
     65 sub usage {
     66 	die <<USAGE_END;
     67 USAGE: $0 [options] infile > outfile\n
     68 	--include-tname
     69 	--no-include-tname # include/omit thread names in stacks (default: include)
     70 	--include-tid
     71 	--no-include-tid   # include/omit thread IDs in stacks (default: omit)
     72 	--shorten-pkgs
     73 	--no-shorten-pkgs  # (don't) shorten package names (default: don't shorten)
     74 
     75 	eg,
     76 	$0 --no-include-tname stacks.txt > collapsed.txt
     77 USAGE_END
     78 }
     79 
     80 GetOptions(
     81 	'include-tname!'  => \$include_tname,
     82 	'include-tid!'    => \$include_tid,
     83 	'shorten-pkgs!'   => \$shorten_pkgs,
     84 	'help'            => \$help,
     85 ) or usage();
     86 $help && usage();
     87 
     88 
     89 # internals
     90 my %collapsed;
     91 
     92 sub remember_stack {
     93 	my ($stack, $count) = @_;
     94 	$collapsed{$stack} += $count;
     95 }
     96 
     97 my @stack;
     98 my $tname;
     99 my $state = "?";
    100 
    101 foreach (<>) {
    102 	next if m/^#/;
    103 	chomp;
    104 
    105 	if (m/^3XMTHREADINFO3           Native callstack:/) {
    106 		# save stack
    107 		if (defined $tname) { unshift @stack, $tname; }
    108 		remember_stack(join(";", @stack), 1) if @stack;
    109 		undef @stack;
    110 		undef $tname;
    111 		$state = "?";
    112 		next;
    113 	}
    114 
    115 	# look for thread header line and parse thread name and state
    116 	if (/^3XMTHREADINFO      "([^"]*).* state:(.*), /) {
    117 		my $name = $1;
    118 		if ($include_tname) {
    119 			$tname = $name;
    120 		}
    121 		$state = $2;
    122 	# special handling for "Anonymous native threads"
    123 	} elsif (/3XMTHREADINFO      Anonymous native thread/) {
    124 		$tname = "Anonymous native thread";
    125 	# look for thread id
    126 	} elsif (/^3XMTHREADINFO1            \(native thread ID:([^ ]*), native priority/) {
    127 		if ($include_tname && $include_tid) {
    128 			$tname = $tname . "-" . $1
    129 		}
    130 	# collect stack frames
    131 	} elsif (/^4XESTACKTRACE                at ([^\(]*)/) {
    132 		my $func = $1;
    133 		if ($shorten_pkgs) {
    134 			my ($pkgs, $clsFunc) = ( $func =~ m/(.*\.)([^.]+\.[^.]+)$/ );
    135 			$pkgs =~ s/(\w)\w*/$1/g;
    136 			$func = $pkgs . $clsFunc;
    137 		}
    138 		unshift @stack, $func;
    139 
    140 	}
    141 }
    142 
    143 foreach my $k (sort { $a cmp $b } keys %collapsed) {
    144 	print "$k $collapsed{$k}\n";
    145 }