Skip to content

Instantly share code, notes, and snippets.

@iprebeg
Created October 13, 2013 14:25
Show Gist options
  • Save iprebeg/6962927 to your computer and use it in GitHub Desktop.
Save iprebeg/6962927 to your computer and use it in GitHub Desktop.
gprof2eps
#!/usr/bin/perl -w
###########################################
# script for generation of gnuplot graphs #
# from profiler output #
# #
# 2009, iprebeg@lss.hr #
###########################################
use vars qw/ %opt /;
$bargraph = 1;
$pdf = 0;
$color = 1;
$graph_out = "stacked_histogram";
$verbose = 0;
$ext = ".prof"; # default assumed extension for profiler output
$max_funcs = 5; # reading first $max_funcs from profiler output
%func_names = (); # storing function names and stuff
############### SUBROUTINES ####################
sub init_getopts()
{
use Getopt::Std;
my $optstring = 'bgho:pv';
getopts ($optstring,\%opt) or usage();
usage() if $opt{h}
}
sub usage()
{
print STDERR "usage: $0 [-b] [-g] [-o output] gprof1.out gprof2.out ... gprofN.out\n";
print STDERR " -b : do not use colors \n";
print STDERR " -g : use gnuplot directly, instead of bargraph.pl script\n";
print STDERR " -h : help (this text) \n";
print STDERR " -o out : specify output file (stacked_histogram is default)\n";
print STDERR " -p : generate pdf also \n";
print STDERR " -v : verbose output \n";
print STDERR " ** OPTIONS MUST BE STATED BEFORE ARGUMENTS, IN UNIX STYLE\n";
exit;
}
# generate pdf from ps
sub ps2pdf($) {
($ps_file) = @_;
exec_command("ps2pdf $ps_file","exec_log");
}
sub gen_bg_file_header($)
{
($bg_file) = @_;
open BGF, ">>", $bg_file or die $!;
print BGF "=stacked;Label1;Label2;Label3;Label4;Label5\n";
if (!$color) {
print BGF "=patterns\n";
print BGF "colors=black,grey1,grey2,grey3,grey4,grey5,grey6\n";
}
print BGF "=table\n";
}
# generates gnuplot script
sub gen_gpl_script($$$)
{
($gpl_file,$dat_file,$eps_file) = @_;
$eps_file .= ".eps";
open GPF, ">", $gpl_file or die $!;
print "generating gnuplot script [$gpl_file]\n" if $verbose;
print "setting eps output file to [$eps_file]\n" if $verbose;
if ($color) {
print GPF "set term postscript eps enhanced color\n";
} else {
print GPF "set term postscript eps enhanced monochrome\n";
}
print GPF "set output \"$eps_file\"\n";
print GPF "set boxwidth 0.3 absolute\n";
print GPF "set autoscale\n";
print GPF "set grid\n";
print GPF "set style fill solid 1.00 border -1\n";
print GPF "set style histogram rowstacked\n";
print GPF "set style data histograms\n";
print GPF "set ytics 10\n";
print GPF "set yrange [0:100]\n";
print GPF "set ylabel \"Percentage\"\n";
print GPF "set xlabel \"Program\"\n";
print GPF "plot \'$dat_file\' using 2 t \"label1\", ".
"\'\' using 4 t \"label2\", ".
"\'\' using 6 t \"label3\", ".
"\'\' using 8 t \"label4\", ".
"\'\' using 10:xtic(1) t \"label5\"\n";
#print GPF "pause -1 \"Hit key to continue\"\n";
close GPF;
}
# tries to be smart when it comes to shell command execution
sub exec_command($$)
{
($command,$logfile) = @_;
if ($logfile ne "") {
@args = ("$command 2> $logfile")
} else {
@args = ("$command");
}
system(@args);
}
sub generate_profiling_bargraph($$)
{
($gprof_out,$index) = @_;
open GPO, "<", $gprof_out or die $!;
# skip lines until you came to percentages
while (<GPO>) {
last if ($_ =~ " time seconds seconds calls s/call s/call name");
}
$percs = $prog_names[$index-1];
$funcs = "";
$i = 0;
for ($i=0; $i<$max_funcs; $i++)
{
$_ = <GPO>;
chomp;
@chunks = split(/ +/);
$percs .= " ".$chunks[1];
#$percs .= " ".$chunks[@chunks-1];
$funcs .= $chunks[@chunks-1]." ";
}
$func_names{$index} = $funcs;
return $percs;
}
sub generate_profiling($$)
{
($gprof_out,$index) = @_;
open GPO, "<", $gprof_out or die $!;
# skip lines until you came to percentages
while (<GPO>) {
last if ($_ =~ " time seconds seconds calls s/call s/call name");
}
$percs = $prog_names[$index-1];
$funcs = "";
$i = 0;
for ($i=0; $i<$max_funcs; $i++)
{
$_ = <GPO>;
chomp;
@chunks = split(/ +/);
$percs .= " ".$chunks[1];
$percs .= " ".$chunks[@chunks-1];
$funcs .= $chunks[@chunks-1]." ";
}
$func_names{$index} = $funcs;
return $percs;
}
################# MAIN ######################
# for every program name stated in @ARGV, add
# default assumed extension and open log
# then generate gnuplot script and data file
init_getopts();
$eps_file = "";
if ($opt{o}) {
$graph_out = $opt{o};
$eps_file = $graph_out.".eps";
} else {
$eps_file = $graph_out.".eps";
}
$color = 0 if $opt{b};
$pdf = 1 if $opt{p};
$bargraph = 0 if $opt{g};
$verbose = 1 if $opt{v};
usage() if ($#ARGV < 0);
$dat_file = "";
$gpl_file = "";
$bg_file = "";
if (! $bargraph ) {
$dat_file = `mktemp`;
$gpl_file = `mktemp`;
chomp $dat_file;
chomp $gpl_file;
open DAT, ">>", $dat_file or die $!;
print "writing dat file to [$dat_file]\n" if $verbose;
} else {
$bg_file = `mktemp`;
chomp $bg_file;
open BGF, ">>", $bg_file or die $!;
print "writing bg file to [$bg_file]\n" if $verbose;
}
if (! $bargraph) {
gen_gpl_script($gpl_file,$dat_file,$graph_out);
} else {
gen_bg_file_header($bg_file);
}
$cnt = 0;
@prog_names = @ARGV;
while ($prof_log = shift @ARGV)
{
$cnt++;
$prof_log .= $ext;
print "parsing profiler output [$prof_log]\n" if $verbose;
if (! $bargraph) {
$line = generate_profiling($prof_log,$cnt);
print DAT "$line\n";
} else {
$line = generate_profiling_bargraph($prof_log,$cnt);
print BGF "$line\n";
}
}
if (! $bargraph) {
print "calling gnuplot with script [$gpl_file] on data [$dat_file]\n" if $verbose;
exec_command("gnuplot $gpl_file","");
} else {
print "calling bargraph on data [$bg_file]\n" if $verbose;
exec_command("bargraph.pl $bg_file 1> $eps_file","");
}
if ($pdf) {
print "generating pdf from [$eps_file]\n" if $verbose;
ps2pdf($eps_file);
}
if (!$bargraph) {
print "removing temporary files [$dat_file] [$gpl_file]\n" if $verbose;
`rm -f $dat_file $gpl_file`;
} else {
print "removing temporary files [$bg_file]\n" if $verbose;
`rm -f $bg_file`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment