stackcollapse-java-exceptions.pl 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. use strict;
  17. use Getopt::Long;
  18. # tunables
  19. my $shorten_pkgs = 0; # shorten package names
  20. my $no_pkgs = 0; # really shorten package names!!
  21. my $help = 0;
  22. sub usage {
  23. die <<USAGE_END;
  24. USAGE: $0 [options] infile > outfile\n
  25. --shorten-pkgs : shorten package names
  26. --no-pkgs : suppress package names (makes SVG much more readable)
  27. USAGE_END
  28. }
  29. GetOptions(
  30. 'shorten-pkgs!' => \$shorten_pkgs,
  31. 'no-pkgs!' => \$no_pkgs,
  32. 'help' => \$help,
  33. ) or usage();
  34. $help && usage();
  35. my %collapsed;
  36. sub remember_stack {
  37. my ($stack, $count) = @_;
  38. $collapsed{$stack} += $count;
  39. }
  40. my @stack;
  41. foreach (<>) {
  42. chomp;
  43. if (/^\s*at ([^\(]*)/) {
  44. my $func = $1;
  45. if ($shorten_pkgs || $no_pkgs) {
  46. my ($pkgs, $clsFunc) = ( $func =~ m/(.*\.)([^.]+\.[^.]+)$/ );
  47. $pkgs =~ s/(\w)\w*/$1/g;
  48. $func = $no_pkgs ? $clsFunc: $pkgs . $clsFunc;
  49. }
  50. unshift @stack, $func;
  51. } elsif (@stack ) {
  52. next if m/.*waiting on .*/;
  53. remember_stack(join(";", @stack), 1) if @stack;
  54. undef @stack;
  55. }
  56. }
  57. remember_stack(join(";", @stack), 1) if @stack;
  58. foreach my $k (sort { $a cmp $b } keys %collapsed) {
  59. print "$k $collapsed{$k}\n";
  60. }