stackcollapse-go.pl (3631B)
1 #!/usr/bin/perl -w 2 # 3 # stackcollapse-go.pl collapse golang samples into single lines. 4 # 5 # Parses golang smaples generated by "go tool pprof" and outputs stacks as 6 # single lines, with methods separated by semicolons, and then a space and an 7 # occurrence count. For use with flamegraph.pl. 8 # 9 # USAGE: ./stackcollapse-go.pl infile > outfile 10 # 11 # Example Input: 12 # ... 13 # Samples: 14 # samples/count cpu/nanoseconds 15 # 1 10000000: 1 2 16 # 2 10000000: 3 2 17 # 1 10000000: 4 2 18 # ... 19 # Locations 20 # 1: 0x58b265 scanblock :0 s=0 21 # 2: 0x599530 GC :0 s=0 22 # 3: 0x58a999 flushptrbuf :0 s=0 23 # 4: 0x58d6a8 runtime.MSpan_Sweep :0 s=0 24 # ... 25 # Mappings 26 # ... 27 # 28 # Example Output: 29 # 30 # GC;flushptrbuf 2 31 # GC;runtime.MSpan_Sweep 1 32 # GC;scanblock 1 33 # 34 # Input may contain many stacks as generated from go tool pprof: 35 # 36 # go tool pprof -seconds=60 -raw -output=a.pprof http://$ADDR/debug/pprof/profile 37 # 38 # For format of text profile, See golang/src/internal/pprof/profile/profile.go 39 # 40 # Copyright 2017 Sijie Yang (yangsijie@baidu.com). All rights reserved. 41 # 42 # This program is free software; you can redistribute it and/or 43 # modify it under the terms of the GNU General Public License 44 # as published by the Free Software Foundation; either version 2 45 # of the License, or (at your option) any later version. 46 # 47 # This program is distributed in the hope that it will be useful, 48 # but WITHOUT ANY WARRANTY; without even the implied warranty of 49 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 50 # GNU General Public License for more details. 51 # 52 # You should have received a copy of the GNU General Public License 53 # along with this program; if not, write to the Free Software Foundation, 54 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 55 # 56 # (http://www.gnu.org/copyleft/gpl.html) 57 # 58 # 16-Jan-2017 Sijie Yang Created this. 59 60 use strict; 61 62 use Getopt::Long; 63 64 # tunables 65 my $help = 0; 66 67 sub usage { 68 die <<USAGE_END; 69 USAGE: $0 infile > outfile\n 70 USAGE_END 71 } 72 73 GetOptions( 74 'help' => \$help, 75 ) or usage(); 76 $help && usage(); 77 78 # internals 79 my $state = "ignore"; 80 my %stacks; 81 my %frames; 82 my %collapsed; 83 84 sub remember_stack { 85 my ($stack, $count) = @_; 86 $stacks{$stack} += $count; 87 } 88 89 # 90 # Output stack string in required format. For example, for the following samples, 91 # format_statck() would return GC;runtime.MSpan_Sweep for stack "4 2" 92 # 93 # Locations 94 # 1: 0x58b265 scanblock :0 s=0 95 # 2: 0x599530 GC :0 s=0 96 # 3: 0x58a999 flushptrbuf :0 s=0 97 # 4: 0x58d6a8 runtime.MSpan_Sweep :0 s=0 98 # 99 sub format_statck { 100 my ($stack) = @_; 101 my @loc_list = split(/ /, $stack); 102 103 for (my $i=0; $i<=$#loc_list; $i++) { 104 my $loc_name = $frames{$loc_list[$i]}; 105 $loc_list[$i] = $loc_name if ($loc_name); 106 } 107 return join(";", reverse(@loc_list)); 108 } 109 110 foreach (<>) { 111 next if m/^#/; 112 chomp; 113 114 if ($state eq "ignore") { 115 if (/Samples:/) { 116 $state = "sample"; 117 next; 118 } 119 120 } elsif ($state eq "sample") { 121 if (/^\s*([0-9]+)\s*[0-9]+: ([0-9 ]+)/) { 122 my $samples = $1; 123 my $stack = $2; 124 remember_stack($stack, $samples); 125 } elsif (/Locations/) { 126 $state = "location"; 127 next; 128 } 129 130 } elsif ($state eq "location") { 131 if (/^\s*([0-9]*): 0x[0-9a-f]+ (M=[0-9]+ )?([^ ]+) .*/) { 132 my $loc_id = $1; 133 my $loc_name = $3; 134 $frames{$loc_id} = $loc_name; 135 } elsif (/Mappings/) { 136 $state = "mapping"; 137 last; 138 } 139 } 140 } 141 142 foreach my $k (keys %stacks) { 143 my $stack = format_statck($k); 144 my $count = $stacks{$k}; 145 $collapsed{$stack} += $count; 146 } 147 148 foreach my $k (sort { $a cmp $b } keys %collapsed) { 149 print "$k $collapsed{$k}\n"; 150 }