hcstackcollapse.pl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/perl -w
  2. #
  3. # hcstackcollapse.pl collapse hot/cold multiline stacks into single lines.
  4. #
  5. # EXPERIMENTAL: This is a work in progress, and may not work properly.
  6. #
  7. # Parses a multiline stack followed by oncpu status and ms on a separate line
  8. # (see example below) and outputs a comma separated stack followed by a space
  9. # and the number. If memory addresses (+0xd) are present, they are stripped,
  10. # and resulting identical stacks are colased with their counts summed.
  11. #
  12. # USAGE: ./hcstackcollapse.pl infile > outfile
  13. #
  14. # Example input:
  15. #
  16. # mysqld`_Z10do_commandP3THD+0xd4
  17. # mysqld`handle_one_connection+0x1a6
  18. # libc.so.1`_thrp_setup+0x8d
  19. # libc.so.1`_lwp_start
  20. # oncpu:1 ms:2664
  21. #
  22. # Example output:
  23. #
  24. # libc.so.1`_lwp_start,libc.so.1`_thrp_setup,mysqld`handle_one_connection,mysqld`_Z10do_commandP3THD oncpu:1 ms:2664
  25. #
  26. # Input may contain many stacks, and can be generated using DTrace. The
  27. # first few lines of input are skipped (see $headerlines).
  28. #
  29. # Copyright 2013 Joyent, Inc. All rights reserved.
  30. # Copyright 2013 Brendan Gregg. All rights reserved.
  31. #
  32. # CDDL HEADER START
  33. #
  34. # The contents of this file are subject to the terms of the
  35. # Common Development and Distribution License (the "License").
  36. # You may not use this file except in compliance with the License.
  37. #
  38. # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  39. # or http://opensource.org/licenses/CDDL-1.0.
  40. # See the License for the specific language governing permissions
  41. # and limitations under the License.
  42. #
  43. # When distributing Covered Code, include this CDDL HEADER in each
  44. # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  45. # If applicable, add the following below this CDDL HEADER, with the
  46. # fields enclosed by brackets "[]" replaced with your own identifying
  47. # information: Portions Copyright [yyyy] [name of copyright owner]
  48. #
  49. # CDDL HEADER END
  50. #
  51. # 14-Aug-2011 Brendan Gregg Created this.
  52. use strict;
  53. my %collapsed;
  54. my $headerlines = 2;
  55. sub remember_stack {
  56. my ($stack, $oncpu, $count) = @_;
  57. $collapsed{"$stack $oncpu"} += $count;
  58. }
  59. my $nr = 0;
  60. my @stack;
  61. foreach (<>) {
  62. next if $nr++ < $headerlines;
  63. chomp;
  64. if (m/^oncpu:(\d+) ms:(\d+)$/) {
  65. remember_stack(join(",", @stack), $1, $2) unless $2 == 0;
  66. @stack = ();
  67. next;
  68. }
  69. next if (m/^\s*$/);
  70. my $frame = $_;
  71. $frame =~ s/^\s*//;
  72. $frame =~ s/\+.*$//;
  73. $frame = "-" if $frame eq "";
  74. unshift @stack, $frame;
  75. }
  76. foreach my $k (sort { $a cmp $b } keys %collapsed) {
  77. print "$k $collapsed{$k}\n";
  78. }