stackcollapse-stap.pl (2309B)
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 52 use strict; 53 54 my %collapsed; 55 56 sub remember_stack { 57 my ($stack, $count) = @_; 58 $collapsed{$stack} += $count; 59 } 60 61 my @stack; 62 63 foreach (<>) { 64 chomp; 65 66 if (m/^\s*(\d+)+$/) { 67 remember_stack(join(";", @stack), $1); 68 @stack = (); 69 next; 70 } 71 72 next if (m/^\s*$/); 73 74 my $frame = $_; 75 $frame =~ s/^\s*//; 76 $frame =~ s/\+[^+]*$//; 77 $frame =~ s/.* : //; 78 $frame = "-" if $frame eq ""; 79 unshift @stack, $frame; 80 } 81 82 foreach my $k (sort { $a cmp $b } keys %collapsed) { 83 printf "$k $collapsed{$k}\n"; 84 }