stackcollapse-vtune-mc.pl (3434B)
1 #!/usr/bin/perl -w 2 # 3 # stackcollapse-vtune-mc.pl 4 # 5 # Parses the CSV file containing a call tree from Intel VTune memory-consumption profiler and produces an output suitable for flamegraph.pl. 6 # 7 # USAGE: perl stackcollapse-vtune-mc.pl [options] infile > outfile 8 # 9 # WORKFLOW: 10 # 11 # This assumes you have Intel VTune installed and on path (using Command Line) 12 # 13 # 1. Profile C++ application tachyon (example shipped with Intel VTune 2019): 14 # 15 # amplxe-cl -collect memory-consumption -r mc_tachyon -- ./tachyon 16 # 17 # 2. Export raw VTune data to csv file: 18 # ### for Intel VTune 2019 19 # amplxe-cl -R top-down -call-stack-mode all \ 20 # -column="Allocations:Self","Allocation Size:Self","Module" \ 21 # -report-out allocations.csv -format csv \ 22 # -csv-delimiter comma -r mc_tachyon 23 # 24 # 3. Generate a flamegraph: 25 # ## Generate for allocations amount. 26 # perl stackcollapse-vtune-mc.pl allocations.csv > out.folded 27 # perl flamegraph.pl --countname=allocations out.folded > vtune_tachyon_mc.svg 28 # 29 # ## Or you can generate for allocation size in bytes. 30 # perl stackcollapse-vtune-mc.pl -s allocations.csv > out.folded 31 # perl flamegraph.pl --countname=allocations out.folded > vtune_tachyon_mc_size.svg 32 # 33 # AUTHOR: Rohith Bakkannagari 34 # 27-Nov-2019 UnpluggedCoder Forked from stackcollapse-vtune.pl, for memory-consumption flamegraph 35 36 use strict; 37 use Getopt::Long; 38 39 sub usage { 40 die <<USAGE_END; 41 Usage : $0 [options] allocations.csv > out.folded\n 42 --size # Accumulate allocation size in bytes instead of allocation counts.\n 43 NOTE : The csv file should exported by `amplxe-cl` tool with the exact -column parameter shows below. 44 amplxe-cl -R top-down -call-stack-mode all \ 45 -column="Allocations:Self","Allocation Size:Self","Module" \ 46 -report-out allocations.csv -format csv \ 47 -csv-delimiter comma -r mc_tachyon 48 USAGE_END 49 } 50 51 # data initialization 52 my @stack = (); 53 my $rowCounter = 0; # flag for row number 54 55 my $accSize = ''; 56 GetOptions ('size' => \$accSize) 57 or usage(); 58 59 my $numArgs = $#ARGV + 1; 60 if ($numArgs != 1){ 61 usage(); 62 exit; 63 } 64 65 my $inputCSVFile = $ARGV[0]; 66 open(my $fh, '<', $inputCSVFile) or die "Can't read file '$inputCSVFile' [$!]\n"; 67 68 while (my $currLine = <$fh>){ 69 # discard warning line 70 next if $rowCounter == 0 && rindex($currLine, "war:", 0) == 0; 71 $rowCounter = $rowCounter + 1; 72 # to discard first row which typically contains headers 73 next if $rowCounter == 1; 74 chomp $currLine; 75 #VTune - sometimes the call stack information is enclosed in double quotes (?). To remove double quotes. 76 $currLine =~ s/\"//g; 77 78 ### for Intel VTune 2019 79 ### CSV header should be like below 80 ### Function Stack,Allocation Size:Self,Deallocation Size:Self,Allocations:Self,Module 81 $currLine =~ /(\s*)(.*?),([0-9]*?\.?[0-9]*?),([0-9]*?\.?[0-9]*?),([0-9]*?\.?[0-9]*?),(.*)/ or die "Error in regular expression on the current line $currLine\n"; 82 my $func = $2.'('.$6.')'; # function(module) 83 my $depth = length ($1); 84 my $allocBytes = $3; # allocation size 85 my $allocs = $5; # allocations 86 87 my $tempString = ''; 88 $stack [$depth] = $func; 89 if ($accSize){ 90 next if $allocBytes eq ''; 91 foreach my $i (0 .. $depth - 1) { 92 $tempString = $tempString.$stack[$i].";"; 93 } 94 $tempString = $tempString.$func." $allocBytes\n"; 95 } else { 96 next if $allocs == 0; 97 foreach my $i (0 .. $depth - 1) { 98 $tempString = $tempString.$stack[$i].";"; 99 } 100 $tempString = $tempString.$func." $allocs\n"; 101 } 102 print "$tempString"; 103 }