stackcollapse-vtune.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/perl -w
  2. #
  3. # stackcollapse-vtune.pl
  4. #
  5. # Parses the CSV file containing a call tree from Intel VTune hotspots profiler and produces an output suitable for flamegraph.pl.
  6. #
  7. # USAGE: perl stackcollapse-vtune.pl 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_find_hotspots (example shipped with Intel VTune 2013):
  14. #
  15. # amplxe-cl -collect hotspots -r result_vtune_tachyon -- ./tachyon_find_hotspots
  16. #
  17. # 2. Export raw VTune data to csv file:
  18. # ###for Intel VTune 2013
  19. # amplxe-cl -R top-down -call-stack-mode all -report-out result_vtune_tachyon.csv -filter "Function Stack" -format csv -csv-delimiter comma -r result_vtune_tachyon
  20. #
  21. # ###for Intel VTune 2015 & 2016
  22. # amplxe-cl -R top-down -call-stack-mode all -column="CPU Time:Self","Module" -report-out result_vtune_tachyon.csv -filter "Function Stack" -format csv -csv-delimiter comma -r result_vtune_tachyon
  23. #
  24. # 3. Generate a flamegraph:
  25. #
  26. # perl stackcollapse-vtune result_vtune_tachyon.csv | perl flamegraph.pl > result_vtune_tachyon.svg
  27. #
  28. # AUTHOR: Rohith Bakkannagari
  29. use strict;
  30. # data initialization
  31. my @stack = ();
  32. my $rowCounter = 0; #flag for row number
  33. my $numArgs = $#ARGV + 1;
  34. if ($numArgs != 1)
  35. {
  36. print "$ARGV[0]\n";
  37. print "Usage : stackcollapse-vtune.pl <out.cvs> > out.txt\n";
  38. exit;
  39. }
  40. my $inputCSVFile = $ARGV[0];
  41. open(my $fh, '<', $inputCSVFile) or die "Can't read file '$inputCSVFile' [$!]\n";
  42. while (my $currLine = <$fh>){
  43. $rowCounter = $rowCounter + 1;
  44. # to discard first row which typically contains headers
  45. next if $rowCounter == 1;
  46. chomp $currLine;
  47. #VTune - sometimes the call stack information is enclosed in double quotes (?). To remove double quotes.
  48. $currLine =~ s/\"//g;
  49. ### for Intel VTune 2013
  50. #$currLine =~ /(\s*)(.*),(.*),[0-9]*\.?[0-9]+[%],([0-9]*\.?[0-9]+)/ or die "Error in regular expression on the current line\n";
  51. #my $func = $3.'!'.$2;
  52. #my $depth = length ($1);
  53. #my $selfTime = $4*1000; # selfTime in msec
  54. ### for Intel VTune 2015 & 2016
  55. $currLine =~ /(\s*)(.*?),([0-9]*\.?[0-9]+?),(.*)/ or die "Error in regular expression on the current line $currLine\n";
  56. my $func = $4.'!'.$2;
  57. my $depth = length ($1);
  58. my $selfTime = $3*1000; # selfTime in msec
  59. my $tempString = '';
  60. $stack [$depth] = $func;
  61. foreach my $i (0 .. $depth - 1) {
  62. $tempString = $tempString.$stack[$i].";";
  63. }
  64. $tempString = $tempString.$func." $selfTime\n";
  65. if ($selfTime != 0){
  66. print "$tempString";
  67. }
  68. }