hotcoldgraph.pl (7909B)
1 #!/usr/bin/perl -w 2 # 3 # hotcoldgraph.pl flame/cold stack grapher. 4 # 5 # EXPERIMENTAL: This is a work in progress, and may not work properly. 6 # 7 # This takes on and off-cpu stack timings (see hcstackcollapse.pl) and 8 # renders a call graph, allowing latency in codepaths to be quickly identified. 9 # 10 # USAGE: ./hotcoldgraph.pl input.txt > graph.svg 11 # 12 # grep funcA input.txt | ./hotcoldgraph.pl > graph.svg 13 # 14 # The input is stack frames and sample counts formatted as single lines. Each 15 # frame in the stack is comma separated, with a space and count at the end of 16 # the line. These can be generated using DTrace with stackcollapse.pl. 17 # 18 # The output graph shows relative presense of functions in stack samples. The 19 # ordering on the x-axis has no meaning; since the data is samples, time order 20 # of events is not known. The order used sorts function names alphabeticly. 21 # 22 # HISTORY 23 # 24 # This was inspired by Neelakanth Nadgir's excellent function_call_graph.rb 25 # program, which visualized function entry and return trace events. As Neel 26 # wrote: "The output displayed is inspired by Roch's CallStackAnalyzer which 27 # was in turn inspired by the work on vftrace by Jan Boerhout". See: 28 # http://blogs.sun.com/realneel/entry/visualizing_callstacks_via_dtrace_and 29 # 30 # For the on-CPU graph only, see flamegraph.pl. 31 # 32 # Copyright 2013 Joyent, Inc. All rights reserved. 33 # Copyright 2013 Brendan Gregg. All rights reserved. 34 # 35 # CDDL HEADER START 36 # 37 # The contents of this file are subject to the terms of the 38 # Common Development and Distribution License (the "License"). 39 # You may not use this file except in compliance with the License. 40 # 41 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 42 # or http://opensource.org/licenses/CDDL-1.0. 43 # See the License for the specific language governing permissions 44 # and limitations under the License. 45 # 46 # When distributing Covered Code, include this CDDL HEADER in each 47 # file and include the License file at usr/src/OPENSOLARIS.LICENSE. 48 # If applicable, add the following below this CDDL HEADER, with the 49 # fields enclosed by brackets "[]" replaced with your own identifying 50 # information: Portions Copyright [yyyy] [name of copyright owner] 51 # 52 # CDDL HEADER END 53 # 54 # 10-Sep-2011 Brendan Gregg Created this. 55 56 use strict; 57 58 # tunables 59 my $fonttype = "Verdana"; 60 my $imagewidth = 1200; # max width, pixels 61 my $frameheight = 16; # max height is dynamic 62 my $fontsize = 12; # base text size 63 my $minwidth = 0.1; # min function width, pixels 64 65 # internals 66 my $ypad1 = $fontsize * 4; # pad top, include title 67 my $ypad2 = $fontsize * 2 + 10; # pad bottom, include labels 68 my $xpad = 10; # pad lefm and right 69 my $timemax = 0; 70 my $depthmax = 0; 71 my %Events; 72 73 # SVG functions 74 { package SVG; 75 sub new { 76 my $class = shift; 77 my $self = {}; 78 bless ($self, $class); 79 return $self; 80 } 81 82 sub header { 83 my ($self, $w, $h) = @_; 84 $self->{svg} .= <<SVG; 85 <?xml version="1.0" standalone="no"?> 86 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 87 <svg version="1.1" width="$w" height="$h" viewBox="0 0 $w $h" xmlns="http://www.w3.org/2000/svg" > 88 SVG 89 } 90 91 sub include { 92 my ($self, $content) = @_; 93 $self->{svg} .= $content; 94 } 95 96 sub colorAllocate { 97 my ($self, $r, $g, $b) = @_; 98 return "rgb($r,$g,$b)"; 99 } 100 101 sub filledRectangle { 102 my ($self, $x1, $y1, $x2, $y2, $fill, $extra) = @_; 103 $x1 = sprintf "%0.1f", $x1; 104 $x2 = sprintf "%0.1f", $x2; 105 my $w = sprintf "%0.1f", $x2 - $x1; 106 my $h = sprintf "%0.1f", $y2 - $y1; 107 $extra = defined $extra ? $extra : ""; 108 $self->{svg} .= qq/<rect x="$x1" y="$y1" width="$w" height="$h" fill="$fill" $extra \/>\n/; 109 } 110 111 sub stringTTF { 112 my ($self, $color, $font, $size, $angle, $x, $y, $str, $loc, $extra) = @_; 113 $loc = defined $loc ? $loc : "left"; 114 $extra = defined $extra ? $extra : ""; 115 $self->{svg} .= qq/<text text-anchor="$loc" x="$x" y="$y" font-size="$size" font-family="$font" fill="$color" $extra >$str<\/text>\n/; 116 } 117 118 sub svg { 119 my $self = shift; 120 return "$self->{svg}</svg>\n"; 121 } 122 1; 123 } 124 125 sub color { 126 my $type = shift; 127 if (defined $type and $type eq "hot") { 128 my $r = 205 + int(rand(50)); 129 my $g = 0 + int(rand(230)); 130 my $b = 0 + int(rand(55)); 131 return "rgb($r,$g,$b)"; 132 } 133 if (defined $type and $type eq "cold") { 134 my $r = 0 + int(rand(40)); 135 my $b = 205 + int(rand(50)); 136 my $g = 0 + int(rand(150)); 137 return "rgb($r,$g,$b)"; 138 } 139 return "rgb(0,0,0)"; 140 } 141 142 my %Node; 143 my %Tmp; 144 145 sub flow { 146 my ($a, $b, $ca, $cb, $v) = @_; 147 my @A = split ",", $a; 148 my @B = split ",", $b; 149 150 my $len_a = $#A; 151 my $len_b = $#B; 152 $depthmax = $len_b if $len_b > $depthmax; 153 154 my $i = 0; 155 my $len_same = 0; 156 for (; $i <= $len_a; $i++) { 157 last if $i > $len_b; 158 last if $A[$i] ne $B[$i]; 159 } 160 $len_same = $i; 161 $len_same = 0 if $ca != $cb; 162 163 for ($i = $len_a; $i >= $len_same; $i--) { 164 my $k = "$A[$i]-$i"; 165 # a unique ID is constructed from func-depth-etime; 166 # func-depth isn't unique, it may be repeated later. 167 $Node{"$k-$v-$ca"}->{stime} = $Tmp{$k}->{stime}; 168 delete $Tmp{$k}->{stime}; 169 delete $Tmp{$k}; 170 } 171 172 for ($i = $len_same; $i <= $len_b; $i++) { 173 my $k = "$B[$i]-$i"; 174 $Tmp{$k}->{stime} = $v; 175 } 176 } 177 178 # Parse input 179 my @Data = <>; 180 my $laststack = ""; 181 my $lastcpu = 0; 182 my $time = 0; 183 foreach (sort @Data) { 184 chomp; 185 my ($stack, $cpu, $samples) = split ' '; 186 $stack = ",$stack"; 187 next unless defined $samples; 188 flow($laststack, $stack, $lastcpu, $cpu, $time); 189 $time += $samples; 190 $laststack = $stack; 191 $lastcpu = $cpu; 192 } 193 flow($laststack, "", $lastcpu, 0, $time); 194 $timemax = $time or die "ERROR: No stack counts found\n"; 195 196 # Draw canvas 197 my $widthpertime = ($imagewidth - 2 * $xpad) / $timemax; 198 my $imageheight = ($depthmax * $frameheight) + $ypad1 + $ypad2; 199 my $im = SVG->new(); 200 $im->header($imagewidth, $imageheight); 201 my $inc = <<INC; 202 <defs > 203 <linearGradient id="background" y1="0" y2="1" x1="0" x2="0" > 204 <stop stop-color="#eeeeee" offset="5%" /> 205 <stop stop-color="#eeeeb0" offset="95%" /> 206 </linearGradient> 207 </defs> 208 <style type="text/css"> 209 rect[rx]:hover { stroke:black; stroke-width:1; } 210 text:hover { stroke:black; stroke-width:1; stroke-opacity:0.35; } 211 </style> 212 INC 213 $im->include($inc); 214 $im->filledRectangle(0, 0, $imagewidth, $imageheight, 'url(#background)'); 215 my ($white, $black, $vvdgrey, $vdgrey) = ( 216 $im->colorAllocate(255, 255, 255), 217 $im->colorAllocate(0, 0, 0), 218 $im->colorAllocate(40, 40, 40), 219 $im->colorAllocate(160, 160, 160), 220 ); 221 $im->stringTTF($black, $fonttype, $fontsize + 5, 0.0, int($imagewidth / 2), $fontsize * 2, "Flame Graph", "middle"); 222 $im->stringTTF($black, $fonttype, $fontsize, 0.0, $xpad, $imageheight - ($ypad2 / 2), 'Function:'); 223 $im->stringTTF($black, $fonttype, $fontsize, 0.0, $xpad + 60, $imageheight - ($ypad2 / 2), " ", "", 'id="details"'); 224 225 # Draw frames 226 foreach my $id (keys %Node) { 227 my ($func, $depth, $etime, $cpu) = split "-", $id; 228 die "missing start for $id" if !defined $Node{$id}->{stime}; 229 my $stime = $Node{$id}->{stime}; 230 my $samples = $etime - $stime; 231 232 my $x1 = $xpad + $stime * $widthpertime; 233 my $x2 = $xpad + $etime * $widthpertime; 234 my $width = $x2 - $x1; 235 next if $width < $minwidth; 236 237 my $y1 = $imageheight - $ypad2 - ($depth + 1) * $frameheight + 1; 238 my $y2 = $imageheight - $ypad2 - $depth * $frameheight; 239 240 my $info; 241 if ($func eq "" and $depth == 0) { 242 $info = "all ($samples ms, 100%)"; 243 } else { 244 my $pct = sprintf "%.2f", ((100 * $samples) / $timemax); 245 $info = "$func ($samples ms, $pct%)"; 246 } 247 my $color = $cpu ? "hot" : "cold"; 248 $im->filledRectangle($x1, $y1, $x2, $y2, color($color), 'rx="2" ry="2" onmouseover="s(' . "'$info'" . ')" onmouseout="c()"'); 249 250 if ($width > 50) { 251 my $chars = int($width / (0.7 * $fontsize)); 252 my $text = substr $func, 0, $chars; 253 $text .= ".." if $chars < length $func; 254 $im->stringTTF($black, $fonttype, $fontsize, 0.0, $x1 + 3, 3 + ($y1 + $y2) / 2, $text, "", 255 'onmouseover="s(' . "'$info'" . ')" onmouseout="c()"'); 256 } 257 } 258 259 print $im->svg;