stackcollapse-java-exceptions.pl (1786B)
1 #!/usr/bin/perl -w 2 # 3 # stackcolllapse-java-exceptions.pl collapse java exceptions (found in logs) into single lines. 4 # 5 # Parses Java error stacks found in a log file and outputs them as 6 # single lines, with methods separated by semicolons, and then a space and an 7 # occurrence count. Inspired by stackcollapse-jstack.pl except that it does 8 # not act as a performance profiler. 9 # 10 # It can be useful if a Java process dumps a lot of different stacks in its logs 11 # and you want to quickly identify the biggest culprits. 12 # 13 # USAGE: ./stackcollapse-java-exceptions.pl infile > outfile 14 # 15 # Copyright 2018 Paul de Verdiere. All rights reserved. 16 17 use strict; 18 use Getopt::Long; 19 20 # tunables 21 my $shorten_pkgs = 0; # shorten package names 22 my $no_pkgs = 0; # really shorten package names!! 23 my $help = 0; 24 25 sub usage { 26 die <<USAGE_END; 27 USAGE: $0 [options] infile > outfile\n 28 --shorten-pkgs : shorten package names 29 --no-pkgs : suppress package names (makes SVG much more readable) 30 31 USAGE_END 32 } 33 34 GetOptions( 35 'shorten-pkgs!' => \$shorten_pkgs, 36 'no-pkgs!' => \$no_pkgs, 37 'help' => \$help, 38 ) or usage(); 39 $help && usage(); 40 41 my %collapsed; 42 43 sub remember_stack { 44 my ($stack, $count) = @_; 45 $collapsed{$stack} += $count; 46 } 47 48 my @stack; 49 50 foreach (<>) { 51 chomp; 52 53 if (/^\s*at ([^\(]*)/) { 54 my $func = $1; 55 if ($shorten_pkgs || $no_pkgs) { 56 my ($pkgs, $clsFunc) = ( $func =~ m/(.*\.)([^.]+\.[^.]+)$/ ); 57 $pkgs =~ s/(\w)\w*/$1/g; 58 $func = $no_pkgs ? $clsFunc: $pkgs . $clsFunc; 59 } 60 unshift @stack, $func; 61 } elsif (@stack ) { 62 next if m/.*waiting on .*/; 63 remember_stack(join(";", @stack), 1) if @stack; 64 undef @stack; 65 } 66 } 67 68 remember_stack(join(";", @stack), 1) if @stack; 69 70 foreach my $k (sort { $a cmp $b } keys %collapsed) { 71 print "$k $collapsed{$k}\n"; 72 }