commit 465ac0c46db04cba413ae593d473181d694ea681
parent 76087eb5e5892e1f65af06ff9629cb965f29ec1a
Author: Brendan Gregg <brendan.d.gregg@gmail.com>
Date: Tue, 28 Oct 2014 17:07:49 -0700
flame graph differentials
Diffstat:
| A | difffolded.pl | | | 106 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | flamegraph.pl | | | 83 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------- |
2 files changed, 176 insertions(+), 13 deletions(-)
diff --git a/difffolded.pl b/difffolded.pl
@@ -0,0 +1,106 @@
+#!/usr/bin/perl -w
+#
+# difffolded.pl diff two folded stack files. Use this for generating
+# flame graph differentials.
+#
+# USAGE: ./difffolded.pl folded1 folded2 | ./flamegraph.pl > diff2.svg
+#
+# The flamegraph will be colored based on higher samples (red) and smaller
+# samples (green). The frame widths will be based on the 2nd folded file.
+# This might be confusing if stack frames disappear entirely; it will make
+# the most sense to ALSO create a differential based on the 1st file widths,
+# while switching the hues; eg:
+#
+# ./difffolded.pl folded2 folded1 | ./flamegraph.pl --negate > diff1.svg
+#
+# Here's what they mean when comparing a before and after profile:
+#
+# diff1.svg: widths show the before profile, colored by what WILL happen
+# diff2.svg: widths show the after profile, colored by what DID happen
+#
+# INPUT: See stackcollapse* programs.
+#
+# OUTPUT: The full list of stacks, with two columns, one from each file.
+# If a stack wasn't present in a file, the column value is zero.
+#
+# folded_stack_trace count_from_folded1 count_from_folded2
+#
+# eg:
+#
+# funca;funcb;funcc 31 33
+# ...
+#
+# COPYRIGHT: Copyright (c) 2014 Brendan Gregg.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# (http://www.gnu.org/copyleft/gpl.html)
+#
+# 28-Oct-2014 Brendan Gregg Created this.
+
+use strict;
+use Getopt::Long;
+
+my $normalize = 0; # make sample counts equal
+
+sub usage {
+ print STDERR <<USAGE_END;
+USAGE: $0 [-n] folded1 folded2 | flamegraph.pl > diff2.svg
+ -n # normalize sample counts
+See stackcollapse scripts for generating folded files.
+Also consider flipping the files and hues to highlight reduced paths:
+$0 folded2 folded1 | ./flamegraph.pl --negate > diff1.svg
+USAGE_END
+ exit 2;
+}
+
+usage if @ARGV < 2;
+GetOptions(
+ 'normalize|n' => \$normalize
+) or usage();
+
+
+my ($total1, $total2) = (0, 0);
+my %Folded;
+
+my $file1 = $ARGV[0];
+my $file2 = $ARGV[1];
+
+open FILE, $file1 or die "ERROR: Can't read $file1\n";
+while (<FILE>) {
+ chomp;
+ my ($stack, $count) = (/^(.*)\s+?(\d+(?:\.\d*)?)$/);
+ $Folded{$stack}{1} += $count;
+ $total1 += $count;
+}
+close FILE;
+
+open FILE, $file2 or die "ERROR: Can't read $file2\n";
+while (<FILE>) {
+ chomp;
+ my ($stack, $count) = (/^(.*)\s+?(\d+(?:\.\d*)?)$/);
+ $Folded{$stack}{2} += $count;
+ $total2 += $count;
+}
+close FILE;
+
+foreach my $stack (keys %Folded) {
+ $Folded{$stack}{1} = 0 unless defined $Folded{$stack}{1};
+ $Folded{$stack}{2} = 0 unless defined $Folded{$stack}{2};
+ if ($normalize && $total1 != $total2) {
+ $Folded{$stack}{1} = int($Folded{$stack}{1} * $total2 / $total1);
+ }
+ print "$stack $Folded{$stack}{1} $Folded{$stack}{2}\n";
+}
diff --git a/flamegraph.pl b/flamegraph.pl
@@ -91,6 +91,7 @@ my %palette_map; # palette map hash
my $pal_file = "palette.map"; # palette map file name
my $stackreverse = 0; # reverse stack order, switching merge end
my $inverted = 0; # icicle graph
+my $negate = 0; # switch differential hues
my $titletext = ""; # centered heading
my $titledefault = "Flame Graph"; # overwritten by --title
my $titleinverted = "Icicle Graph"; # " "
@@ -114,6 +115,7 @@ GetOptions(
'cp' => \$palette,
'reverse' => \$stackreverse,
'inverted' => \$inverted,
+ 'negate' => \$negate,
) or die <<USAGE_END;
USAGE: $0 [options] infile > outfile.svg\n
--title # change title text
@@ -129,6 +131,7 @@ USAGE: $0 [options] infile > outfile.svg\n
--cp # use consistent palette (palette.map)
--reverse # generate stack-reversed flame graph
--inverted # icicle graph
+ --negate # switch differential hues (green<->red)
eg,
$0 --title="Flame Graph: malloc()" trace.txt > graph.svg
@@ -304,6 +307,18 @@ sub color {
return "rgb(0,0,0)";
}
+sub color_scale {
+ my ($value, $max) = @_;
+ my ($r, $g, $b) = (255, 255, 255);
+ $value = -$value if $negate;
+ if ($value > 0) {
+ $g = $b = int(210 * ($max - $value) / $max);
+ } elsif ($value < 0) {
+ $r = $b = int(210 * ($max + $value) / $max);
+ }
+ return "rgb($r,$g,$b)";
+}
+
sub color_map {
my ($colors, $func) = @_;
if (exists $palette_map{$func}) {
@@ -338,7 +353,7 @@ my %Node;
my %Tmp;
sub flow {
- my ($last, $this, $v) = @_;
+ my ($last, $this, $v, $d) = @_;
my $len_a = @$last - 1;
my $len_b = @$this - 1;
@@ -356,12 +371,18 @@ sub flow {
# a unique ID is constructed from "func;depth;etime";
# func-depth isn't unique, it may be repeated later.
$Node{"$k;$v"}->{stime} = delete $Tmp{$k}->{stime};
+ if (defined $Tmp{$k}->{delta}) {
+ $Node{"$k;$v"}->{delta} = delete $Tmp{$k}->{delta};
+ }
delete $Tmp{$k};
}
for ($i = $len_same; $i <= $len_b; $i++) {
my $k = "$this->[$i];$i";
$Tmp{$k}->{stime} = $v;
+ if (defined $d) {
+ $Tmp{$k}->{delta} += $i == $len_b ? $d : 0;
+ }
}
return $this;
@@ -371,16 +392,25 @@ sub flow {
my @Data;
my $last = [];
my $time = 0;
+my $delta = undef;
my $ignored = 0;
my $line;
+my $maxdelta = 1;
# reverse if needed
foreach (<>) {
chomp;
$line = $_;
if ($stackreverse) {
- my ($stack, $samples) = (/^(.*)\s+(\d+(?:\.\d*)?)$/);
- unshift @Data, join(";", reverse split(";", $stack)) . " $samples";
+ # there may be an extra samples column for differentials
+ # XXX todo: redo these REs as one. It's repeated below.
+ my ($stack, $samples) = (/^(.*)\s+?(\d+(?:\.\d*)?)$/);
+ my $samples2 = undef;
+ if ($stack =~ /^(.*)\s+?(\d+(?:\.\d*)?)$/) {
+ $samples2 = $samples;
+ ($stack, $samples) = $stack =~ (/^(.*)\s+?(\d+(?:\.\d*)?)$/);
+ }
+ unshift @Data, join(";", reverse split(";", $stack)) . " $samples $samples2";
} else {
unshift @Data, $line;
}
@@ -389,16 +419,31 @@ foreach (<>) {
# process and merge frames
foreach (sort @Data) {
chomp;
- my ($stack, $samples) = (/^(.*)\s+(\d+(?:\.\d*)?)$/);
+ # there may be an extra samples column for differentials
+ my ($stack, $samples) = (/^(.*)\s+?(\d+(?:\.\d*)?)$/);
+ my $samples2 = undef;
+ if ($stack =~ /^(.*)\s+?(\d+(?:\.\d*)?)$/) {
+ $samples2 = $samples;
+ ($stack, $samples) = $stack =~ (/^(.*)\s+?(\d+(?:\.\d*)?)$/);
+ }
+ $delta = undef;
+ if (defined $samples2) {
+ $delta = $samples2 - $samples;
+ $maxdelta = abs($delta) if abs($delta) > $maxdelta;
+ }
unless (defined $samples) {
++$ignored;
next;
}
$stack =~ tr/<>/()/;
- $last = flow($last, [ '', split ";", $stack ], $time);
- $time += $samples;
+ $last = flow($last, [ '', split ";", $stack ], $time, $delta);
+ if (defined $samples2) {
+ $time += $samples2;
+ } else {
+ $time += $samples;
+ }
}
-flow($last, [], $time);
+flow($last, [], $time, $delta);
warn "Ignored $ignored lines with invalid format\n" if $ignored;
die "ERROR: No stack counts found\n" unless $time;
@@ -545,7 +590,7 @@ my $inc = <<INC;
var ymin = parseFloat(attr["y"].value);
var ratio = (svg.width.baseVal.value - 2*$xpad) / width;
- // Workaround for JavaScript float issues (fix me)
+ // XXX: Workaround for JavaScript float issues (fix me)
var fudge = 0.0001;
var unzoombtn = document.getElementById("unzoom");
@@ -624,6 +669,7 @@ if ($palette) {
while (my ($id, $node) = each %Node) {
my ($func, $depth, $etime) = split ";", $id;
my $stime = $node->{stime};
+ my $delta = $node->{delta};
$etime = $timemax if $func eq "" and $depth == 0;
@@ -651,7 +697,13 @@ while (my ($id, $node) = each %Node) {
$escaped_func =~ s/&/&/g;
$escaped_func =~ s/</</g;
$escaped_func =~ s/>/>/g;
- $info = "$escaped_func ($samples_txt $countname, $pct%)";
+ unless (defined $delta) {
+ $info = "$escaped_func ($samples_txt $countname, $pct%)";
+ } else {
+ my $deltapct = sprintf "%.2f", ((100 * $delta) / ($timemax * $factor));
+ $deltapct = $delta > 0 ? "+$deltapct" : $deltapct;
+ $info = "$escaped_func ($samples_txt $countname, $pct%; $deltapct%)";
+ }
}
my $nameattr = { %{ $nameattr{$func}||{} } }; # shallow clone
@@ -662,12 +714,17 @@ while (my ($id, $node) = each %Node) {
$nameattr->{title} ||= $info;
$im->group_start($nameattr);
- if ($palette) {
- $im->filledRectangle($x1, $y1, $x2, $y2, color_map($colors, $func), 'rx="2" ry="2"');
+ my $color;
+ if ($func eq "-") {
+ $color = $vdgrey;
+ } elsif (defined $delta) {
+ $color = color_scale($delta, $maxdelta);
+ } elsif ($palette) {
+ $color = color_map($colors, $func);
} else {
- my $color = $func eq "-" ? $vdgrey : color($colors, $hash, $func);
- $im->filledRectangle($x1, $y1, $x2, $y2, $color, 'rx="2" ry="2"');
+ $color = color($colors, $hash, $func);
}
+ $im->filledRectangle($x1, $y1, $x2, $y2, $color, 'rx="2" ry="2"');
my $chars = int( ($x2 - $x1) / ($fontsize * $fontwidth));
my $text = "";