stackcollapse-pmc.pl (2675B)
1 #!/usr/bin/env perl 2 # 3 # Copyright (c) 2014 Ed Maste. All rights reserved. 4 # 5 # Redistribution and use in source and binary forms, with or without 6 # modification, are permitted provided that the following conditions 7 # are met: 8 # 9 # 1. Redistributions of source code must retain the above copyright 10 # notice, this list of conditions and the following disclaimer. 11 # 2. Redistributions in binary form must reproduce the above copyright 12 # notice, this list of conditions and the following disclaimer in the 13 # documentation and/or other materials provided with the distribution. 14 # 15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND 16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 # SUCH DAMAGE. 26 # 27 # stackcollapse-pmc.pl collapse hwpmc samples into single lines. 28 # 29 # Parses a list of multiline stacks generated by "hwpmc -G", and outputs a 30 # semicolon-separated stack followed by a space and a count. 31 # 32 # Usage: 33 # pmcstat -S unhalted-cycles -O pmc.out 34 # pmcstat -R pmc.out -z16 -G pmc.graph 35 # stackcollapse-pmc.pl pmc.graph > pmc.stack 36 # 37 # Example input: 38 # 39 # 03.07% [17] witness_unlock @ /boot/kernel/kernel 40 # 70.59% [12] __mtx_unlock_flags 41 # 16.67% [2] selfdfree 42 # 100.0% [2] sys_poll 43 # 100.0% [2] amd64_syscall 44 # 08.33% [1] pmap_ts_referenced 45 # 100.0% [1] vm_pageout 46 # 100.0% [1] fork_exit 47 # ... 48 # 49 # Example output: 50 # 51 # amd64_syscall;sys_poll;selfdfree;__mtx_unlock_flags;witness_unlock 2 52 # amd64_syscall;sys_poll;pmap_ts_referenced;__mtx_unlock_flagsgeout;fork_exit 1 53 # ... 54 55 use warnings; 56 use strict; 57 58 my @stack; 59 my $prev_count; 60 my $prev_indent = -1; 61 62 while (defined($_ = <>)) { 63 if (m/^( *)[0-9.]+% \[([0-9]+)\]\s*(\S+)/) { 64 my $indent = length($1); 65 if ($indent <= $prev_indent) { 66 print join(';', reverse(@stack[0 .. $prev_indent])) . 67 " $prev_count\n"; 68 } 69 $stack[$indent] = $3; 70 $prev_count = $2; 71 $prev_indent = $indent; 72 } 73 } 74 print join(';', reverse(@stack[0 .. $prev_indent])) . " $prev_count\n";