stackcollapse.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/perl -w
  2. #
  3. # stackcollapse.pl collapse multiline stacks into single lines.
  4. #
  5. # Parses a multiline stack followed by a number on a separate line, and
  6. # outputs a semicolon separated stack followed by a space and the number.
  7. # If memory addresses (+0xd) are present, they are stripped, and resulting
  8. # identical stacks are colased with their counts summed.
  9. #
  10. # USAGE: ./stackcollapse.pl infile > outfile
  11. #
  12. # Example input:
  13. #
  14. # unix`i86_mwait+0xd
  15. # unix`cpu_idle_mwait+0xf1
  16. # unix`idle+0x114
  17. # unix`thread_start+0x8
  18. # 1641
  19. #
  20. # Example output:
  21. #
  22. # unix`thread_start;unix`idle;unix`cpu_idle_mwait;unix`i86_mwait 1641
  23. #
  24. # Input may contain many stacks, and can be generated using DTrace. The
  25. # first few lines of input are skipped (see $headerlines).
  26. #
  27. # Copyright 2011 Joyent, Inc. All rights reserved.
  28. # Copyright 2011 Brendan Gregg. All rights reserved.
  29. #
  30. # CDDL HEADER START
  31. #
  32. # The contents of this file are subject to the terms of the
  33. # Common Development and Distribution License (the "License").
  34. # You may not use this file except in compliance with the License.
  35. #
  36. # You can obtain a copy of the license at docs/cddl1.txt or
  37. # http://opensource.org/licenses/CDDL-1.0.
  38. # See the License for the specific language governing permissions
  39. # and limitations under the License.
  40. #
  41. # When distributing Covered Code, include this CDDL HEADER in each
  42. # file and include the License file at docs/cddl1.txt.
  43. # If applicable, add the following below this CDDL HEADER, with the
  44. # fields enclosed by brackets "[]" replaced with your own identifying
  45. # information: Portions Copyright [yyyy] [name of copyright owner]
  46. #
  47. # CDDL HEADER END
  48. #
  49. # 14-Aug-2011 Brendan Gregg Created this.
  50. use strict;
  51. my $headerlines = 3; # number of input lines to skip
  52. my $includeoffset = 0; # include function offset (except leafs)
  53. my %collapsed;
  54. sub remember_stack {
  55. my ($stack, $count) = @_;
  56. $collapsed{$stack} += $count;
  57. }
  58. my $nr = 0;
  59. my @stack;
  60. foreach (<>) {
  61. next if $nr++ < $headerlines;
  62. chomp;
  63. if (m/^\s*(\d+)+$/) {
  64. my $count = $1;
  65. my $joined = join(";", @stack);
  66. # trim leaf offset if these were retained:
  67. $joined =~ s/\+[^+]*$// if $includeoffset;
  68. remember_stack($joined, $count);
  69. @stack = ();
  70. next;
  71. }
  72. next if (m/^\s*$/);
  73. my $frame = $_;
  74. $frame =~ s/^\s*//;
  75. $frame =~ s/\+[^+]*$// unless $includeoffset;
  76. # Remove arguments from C++ function names:
  77. $frame =~ s/(::.*)[(<].*/$1/;
  78. $frame = "-" if $frame eq "";
  79. my @inline;
  80. for (split /\->/, $frame) {
  81. my $func = $_;
  82. # Strip out L and ; included in java stacks
  83. $func =~ tr/\;/:/;
  84. $func =~ s/^L//;
  85. $func .= "_[i]" if scalar(@inline) > 0; #inlined
  86. push @inline, $func;
  87. }
  88. unshift @stack, @inline;
  89. }
  90. foreach my $k (sort { $a cmp $b } keys %collapsed) {
  91. print "$k $collapsed{$k}\n";
  92. }