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