Skip to content
Newer
Older
100755 97 lines (83 sloc) 2.49 KB
c536e6a setup
Brendan Gregg authored Dec 15, 2011
1 #!/usr/bin/perl -w
2 #
3 # stackcolllapse.pl collapse multiline stacks into single lines.
4 #
5 # Parses a multiline stack followed by a number on a separate line, and
e51f401 comments for comma to semicolon change
Brendan Gregg authored Jun 15, 2012
6 # outputs a semicolon separated stack followed by a space and the number.
c536e6a setup
Brendan Gregg authored Dec 16, 2011
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 #
e51f401 comments for comma to semicolon change
Brendan Gregg authored Jun 15, 2012
22 # unix`thread_start;unix`idle;unix`cpu_idle_mwait;unix`i86_mwait 1641
c536e6a setup
Brendan Gregg authored Dec 16, 2011
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 #
76719a4 USAGE message and license file reference
Brendan Gregg authored May 18, 2013
36 # You can obtain a copy of the license at docs/cddl1.txt or
37 # http://opensource.org/licenses/CDDL-1.0.
c536e6a setup
Brendan Gregg authored Dec 16, 2011
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
76719a4 USAGE message and license file reference
Brendan Gregg authored May 18, 2013
42 # file and include the License file at docs/cddl1.txt.
c536e6a setup
Brendan Gregg authored Dec 16, 2011
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
51 use strict;
52
e5de8f0 @brendangregg provide a way to retain function offsets
authored Mar 12, 2015
53 my $headerlines = 3; # number of input lines to skip
54 my $includeoffset = 0; # include function offset (except leafs)
c536e6a setup
Brendan Gregg authored Dec 16, 2011
55 my %collapsed;
56
57 sub remember_stack {
58 my ($stack, $count) = @_;
59 $collapsed{$stack} += $count;
60 }
61
62 my $nr = 0;
63 my @stack;
64
65 foreach (<>) {
66 next if $nr++ < $headerlines;
67 chomp;
68
69 if (m/^\s*(\d+)+$/) {
e5de8f0 @brendangregg provide a way to retain function offsets
authored Mar 13, 2015
70 my $count = $1;
71 my $joined = join(";", @stack);
72
73 # trim leaf offset if these were retained:
74 $joined =~ s/\+[^+]*$// if $includeoffset;
75
76 remember_stack($joined, $count);
c536e6a setup
Brendan Gregg authored Dec 16, 2011
77 @stack = ();
78 next;
79 }
80
81 next if (m/^\s*$/);
82
83 my $frame = $_;
84 $frame =~ s/^\s*//;
e5de8f0 @brendangregg provide a way to retain function offsets
authored Mar 13, 2015
85 $frame =~ s/\+[^+]*$// unless $includeoffset;
86
87 # Remove arguments from C++ function names:
9875854 @KingOfBrian Ensure that the paren is in a C++ namespace, and not an objective-c c…
KingOfBrian authored Mar 28, 2015
88 $frame =~ s/(::.*)[(<].*/$1/;
e5de8f0 @brendangregg provide a way to retain function offsets
authored Mar 13, 2015
89
c536e6a setup
Brendan Gregg authored Dec 16, 2011
90 $frame = "-" if $frame eq "";
91 unshift @stack, $frame;
92 }
93
94 foreach my $k (sort { $a cmp $b } keys %collapsed) {
42b3fdc @randomstuff Make stackcollapse happy when a % is in the function name
randomstuff authored Jan 1, 2015
95 print "$k $collapsed{$k}\n";
c536e6a setup
Brendan Gregg authored Dec 16, 2011
96 }
Something went wrong with that request. Please try again.