flamegraph

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

stackcollapse-vsprof.pl (2983B)


      1 #!/usr/bin/perl -w
      2 #
      3 # stackcollapse-vsprof.pl
      4 #
      5 # Parses the CSV file containing a call tree from a visual studio profiler and produces an output suitable for flamegraph.pl.
      6 #
      7 # USAGE: perl stackcollapse-vsprof.pl infile > outfile
      8 #
      9 # WORKFLOW:
     10 #
     11 # This example assumes you have visual studio 2015 installed.
     12 # 
     13 # 1. Profile C++ your application using visual studio
     14 # 2. On visual studio, choose export the call tree as csv
     15 # 3. Generate a flamegraph: perl stackcollapse-vsprof CallTreeSummary.csv | perl flamegraph.pl > result_vsprof.svg
     16 #
     17 # INPUT EXAMPLE :
     18 #
     19 # Level,Function Name,Inclusive Samples,Exclusive Samples,Inclusive Samples %,Exclusive Samples %,Module Name,
     20 # 1,"main","8,735",0,100.00,0.00,"an_executable.exe",
     21 # 2,"testing::UnitTest::Run","8,735",0,100.00,0.00,"an_executable.exe",
     22 # 3,"boost::trim_end_iter_select<std::iterator<std::val<std::types<char> > >,boost::is_classifiedF>",306,16,3.50,0.18,"an_executable.exe",
     23 #
     24 # OUTPUT EXAMPLE :
     25 #
     26 # main;testing::UnitTest::Run;boost::trim_end_iter_select<std::iterator<std::val<std::types<char>>>,boost::is_classifiedF> 306
     27 
     28 use strict;
     29 
     30 sub massage_function_names;
     31 sub parse_integer;
     32 sub print_stack_trace;
     33 
     34 # data initialization
     35 my @stack = ();
     36 my $line_number = 0;
     37 my $previous_samples = 0;
     38 
     39 my $num_args = $#ARGV + 1;
     40 if ($num_args != 1) {
     41   print "$ARGV[0]\n";
     42   print "Usage : stackcollapse-vsprof.pl <in.cvs> > out.txt\n";
     43   exit;
     44 }
     45 
     46 my $input_csv_file = $ARGV[0];
     47 my $line_parser_rx = qr{
     48   ^\s*(\d+?),            # level in the stack
     49   ("[^"]+" | [^,]+),     # function name (beware of spaces)
     50   ("[^"]+" | [^,]+),     # number of samples (beware of locale number formatting)
     51 }ox;
     52 
     53 open(my $fh, '<', $input_csv_file) or die "Can't read file '$input_csv_file' [$!]\n";
     54 
     55 while (my $current_line = <$fh>){
     56   $line_number = $line_number + 1;
     57 
     58   # to discard first line which typically contains headers
     59   next if $line_number == 1;
     60   next if $current_line =~ /^\s*$/o;
     61  
     62   ($current_line =~ $line_parser_rx) or die "Error in regular expression at line $line_number : $current_line\n";
     63 
     64   my $level = int $1;
     65   my $function = massage_function_names($2);
     66   my $samples = parse_integer($3);
     67   my $stack_len = @stack;
     68  
     69   #print "[DEBUG] $line_number : $level $function $samples $stack_len\n";
     70 
     71   next if not $level;
     72   ($level <= $stack_len + 1) or die "Error in stack at line $line_number : $current_line\n";
     73 
     74   if ($level <= $stack_len) {
     75 		print_stack_trace(\@stack, $previous_samples);
     76     my $to_remove = $level - $stack_len - 1;
     77     splice(@stack, $to_remove);
     78   }
     79 
     80   $stack_len < 1000 or die "Stack overflow at line $line_number";
     81   push(@stack, $function);
     82   $previous_samples = $samples;
     83 }
     84 print_stack_trace(\@stack, $previous_samples);
     85 
     86 sub massage_function_names {
     87   return ($_[0] =~ s/\s*|^"|"$//gro);
     88 }
     89 
     90 sub parse_integer {
     91   return int ($_[0] =~ s/[., ]|^"|"$//gro);
     92 }
     93 
     94 sub print_stack_trace {
     95   my ($stack_ref, $sample) = @_;
     96 	my $stack_trace = join(";", @$stack_ref);
     97 	print "$stack_trace $sample\n";
     98 }