stackcollapse-stap.pl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/perl -w
  2. #
  3. # stackcollapse-stap.pl collapse multiline SystemTap stacks
  4. # into single lines.
  5. #
  6. # Parses a multiline stack followed by a number on a separate line, and
  7. # outputs a semicolon separated stack followed by a space and the number.
  8. # If memory addresses (+0xd) are present, they are stripped, and resulting
  9. # identical stacks are colased with their counts summed.
  10. #
  11. # USAGE: ./stackcollapse.pl infile > outfile
  12. #
  13. # Example input:
  14. #
  15. # 0xffffffff8103ce3b : native_safe_halt+0xb/0x10 [kernel]
  16. # 0xffffffff8101c6a3 : default_idle+0x53/0x1d0 [kernel]
  17. # 0xffffffff81013236 : cpu_idle+0xd6/0x120 [kernel]
  18. # 0xffffffff815bf03e : rest_init+0x72/0x74 [kernel]
  19. # 0xffffffff81aebbfe : start_kernel+0x3ba/0x3c5 [kernel]
  20. # 2404
  21. #
  22. # Example output:
  23. #
  24. # start_kernel;rest_init;cpu_idle;default_idle;native_safe_halt 2404
  25. #
  26. # Input may contain many stacks as generated from SystemTap.
  27. #
  28. # Copyright 2011 Joyent, Inc. All rights reserved.
  29. # Copyright 2011 Brendan Gregg. All rights reserved.
  30. #
  31. # CDDL HEADER START
  32. #
  33. # The contents of this file are subject to the terms of the
  34. # Common Development and Distribution License (the "License").
  35. # You may not use this file except in compliance with the License.
  36. #
  37. # You can obtain a copy of the license at docs/cddl1.txt or
  38. # http://opensource.org/licenses/CDDL-1.0.
  39. # See the License for the specific language governing permissions
  40. # and limitations under the License.
  41. #
  42. # When distributing Covered Code, include this CDDL HEADER in each
  43. # file and include the License file at docs/cddl1.txt.
  44. # If applicable, add the following below this CDDL HEADER, with the
  45. # fields enclosed by brackets "[]" replaced with your own identifying
  46. # information: Portions Copyright [yyyy] [name of copyright owner]
  47. #
  48. # CDDL HEADER END
  49. #
  50. # 16-Feb-2012 Brendan Gregg Created this.
  51. use strict;
  52. my %collapsed;
  53. sub remember_stack {
  54. my ($stack, $count) = @_;
  55. $collapsed{$stack} += $count;
  56. }
  57. my @stack;
  58. foreach (<>) {
  59. chomp;
  60. if (m/^\s*(\d+)+$/) {
  61. remember_stack(join(";", @stack), $1);
  62. @stack = ();
  63. next;
  64. }
  65. next if (m/^\s*$/);
  66. my $frame = $_;
  67. $frame =~ s/^\s*//;
  68. $frame =~ s/\+[^+]*$//;
  69. $frame =~ s/.* : //;
  70. $frame = "-" if $frame eq "";
  71. unshift @stack, $frame;
  72. }
  73. foreach my $k (sort { $a cmp $b } keys %collapsed) {
  74. printf "$k $collapsed{$k}\n";
  75. }