flamegraph

Fork of flamegraph without j***s*****
git clone git://git.laack.co/flamegraph.git
Log | Files | Refs | README

difffolded.pl (3640B)


      1 #!/usr/bin/perl -w
      2 #
      3 # difffolded.pl 	diff two folded stack files. Use this for generating
      4 #			flame graph differentials.
      5 #
      6 # USAGE: ./difffolded.pl [-hns] folded1 folded2 | ./flamegraph.pl > diff2.svg
      7 #
      8 # Options are described in the usage message (-h).
      9 #
     10 # The flamegraph will be colored based on higher samples (red) and smaller
     11 # samples (blue). The frame widths will be based on the 2nd folded file.
     12 # This might be confusing if stack frames disappear entirely; it will make
     13 # the most sense to ALSO create a differential based on the 1st file widths,
     14 # while switching the hues; eg:
     15 #
     16 #  ./difffolded.pl folded2 folded1 | ./flamegraph.pl --negate > diff1.svg
     17 #
     18 # Here's what they mean when comparing a before and after profile:
     19 #
     20 # diff1.svg: widths show the before profile, colored by what WILL happen
     21 # diff2.svg: widths show the after profile, colored by what DID happen
     22 #
     23 # INPUT: See stackcollapse* programs.
     24 #
     25 # OUTPUT: The full list of stacks, with two columns, one from each file.
     26 # If a stack wasn't present in a file, the column value is zero.
     27 #
     28 # folded_stack_trace count_from_folded1 count_from_folded2
     29 #
     30 # eg:
     31 #
     32 # funca;funcb;funcc 31 33
     33 # ...
     34 #
     35 # COPYRIGHT: Copyright (c) 2014 Brendan Gregg.
     36 #
     37 #  This program is free software; you can redistribute it and/or
     38 #  modify it under the terms of the GNU General Public License
     39 #  as published by the Free Software Foundation; either version 2
     40 #  of the License, or (at your option) any later version.
     41 #
     42 #  This program is distributed in the hope that it will be useful,
     43 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
     44 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     45 #  GNU General Public License for more details.
     46 #
     47 #  You should have received a copy of the GNU General Public License
     48 #  along with this program; if not, write to the Free Software Foundation,
     49 #  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     50 #
     51 #  (http://www.gnu.org/copyleft/gpl.html)
     52 #
     53 # 28-Oct-2014	Brendan Gregg	Created this.
     54 
     55 use strict;
     56 use Getopt::Std;
     57 
     58 # defaults
     59 my $normalize = 0;	# make sample counts equal
     60 my $striphex = 0;	# strip hex numbers
     61 
     62 sub usage {
     63 	print STDERR <<USAGE_END;
     64 USAGE: $0 [-hns] folded1 folded2 | flamegraph.pl > diff2.svg
     65 	    -h       # help message
     66 	    -n       # normalize sample counts
     67 	    -s       # strip hex numbers (addresses)
     68 See stackcollapse scripts for generating folded files.
     69 Also consider flipping the files and hues to highlight reduced paths:
     70 $0 folded2 folded1 | ./flamegraph.pl --negate > diff1.svg
     71 USAGE_END
     72 	exit 2;
     73 }
     74 
     75 usage() if @ARGV < 2;
     76 our($opt_h, $opt_n, $opt_s);
     77 getopts('ns') or usage();
     78 usage() if $opt_h;
     79 $normalize = 1 if defined $opt_n;
     80 $striphex = 1 if defined $opt_s;
     81 
     82 my ($total1, $total2) = (0, 0);
     83 my %Folded;
     84 
     85 my $file1 = $ARGV[0];
     86 my $file2 = $ARGV[1];
     87 
     88 open FILE, $file1 or die "ERROR: Can't read $file1\n";
     89 while (<FILE>) {
     90 	chomp;
     91 	my ($stack, $count) = (/^(.*)\s+?(\d+(?:\.\d*)?)$/);
     92 	$stack =~ s/0x[0-9a-fA-F]+/0x.../g if $striphex;
     93 	$Folded{$stack}{1} += $count;
     94 	$total1 += $count;
     95 }
     96 close FILE;
     97 
     98 open FILE, $file2 or die "ERROR: Can't read $file2\n";
     99 while (<FILE>) {
    100 	chomp;
    101 	my ($stack, $count) = (/^(.*)\s+?(\d+(?:\.\d*)?)$/);
    102 	$stack =~ s/0x[0-9a-fA-F]+/0x.../g if $striphex;
    103 	$Folded{$stack}{2} += $count;
    104 	$total2 += $count;
    105 }
    106 close FILE;
    107 
    108 foreach my $stack (keys %Folded) {
    109 	$Folded{$stack}{1} = 0 unless defined $Folded{$stack}{1};
    110 	$Folded{$stack}{2} = 0 unless defined $Folded{$stack}{2};
    111 	if ($normalize && $total1 != $total2) {
    112 		$Folded{$stack}{1} = int($Folded{$stack}{1} * $total2 / $total1);
    113 	}
    114 	print "$stack $Folded{$stack}{1} $Folded{$stack}{2}\n";
    115 }