The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9705 hornik 1
#! @PERL@
9869 ripley 2
#-*- perl -*- (for Rprof.in)
9705 hornik 3
 
9869 ripley 4
# Post-process profiling files generated by Rprof().
5
 
6
# Copyright (C) 2000 R Development Core Team
7
#
8
# This program is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 2, or (at your option)
11
# any later version.
12
#
13
# This program is distributed in the hope that it will be useful, but
14
# WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
16
# General Public License for more details.
17
#
18
# A copy of the GNU General Public License is available via WWW at
19
# http://www.gnu.org/copyleft/gpl.html.	 You can also obtain it by
20
# writing to the Free Software Foundation, Inc., 59 Temple Place,
21
# Suite 330, Boston, MA  02111-1307  USA.
22
 
10633 hornik 23
# Send any bug reports to r-bugs@r-project.org.
9869 ripley 24
 
11515 hornik 25
use Getopt::Long;
26
use R::Utils;
27
 
28
my $revision = ' $Revision: 1.4 $ ';
29
my $version;
30
my $name;
31
 
32
$revision =~ / ([\d\.]*) /;
33
$version = $1;
34
($name = $0) =~ s|.*/||;
35
 
36
sub usage {
37
    print STDERR <<END;
38
Usage: R CMD Rprof [options] file
39
 
40
Post-process profiling information in file generated by Rprof().
41
 
42
Options:
43
  -h, --help		print short help message and exit
31770 ripley 44
  -v, --version		print Rprof version info and exit
11515 hornik 45
 
46
Email bug reports to <r-bugs\@r-project.org>.
47
END
48
  exit 0;
49
}
50
 
51
@knownoptions = ("h|help", "v|version");
52
GetOptions(@knownoptions) || &usage();
53
&R_version("R profiling post-processor", $version) if $opt_v;
54
&usage() if $opt_h;
55
 
9705 hornik 56
%leafcounts = ();
57
%totalcounts = ();
58
 
59
while (<>) {
9869 ripley 60
    if (/^sample\.interval=/) {
61
	s/sample\.interval=//;
62
	$sample = $_ / 1e6;
63
    } else {
9705 hornik 64
	chomp;
65
	@line = split(/ /);
66
	%names = ();
67
	$leaf = @line[0];
68
	foreach $name (@line) {
9869 ripley 69
	    $names{$name} = 1;
9705 hornik 70
	}
9869 ripley 71
	$total = $total + $sample;
9705 hornik 72
	foreach $name (keys %names) {
9869 ripley 73
	    $totalcounts{$name} = $totalcounts{$name} + $sample;
9705 hornik 74
	}
9869 ripley 75
	$leafcounts{$leaf} = $leafcounts{$leaf} + $sample;
9705 hornik 76
    }
77
}
78
 
79
print "\n";
80
print "Each sample represents $sample seconds.\n";
9869 ripley 81
print "Total run time: $total seconds.\n\n";
9705 hornik 82
print "Total seconds: time spent in function and callees.\n";
83
print "Self seconds: time spent in function alone.\n";
84
 
85
print "\n";
86
print "   %       total       %       self\n";
87
print " total    seconds     self    seconds    name\n";
88
 
89
@order = sort { $totalcounts{$b} <=> $totalcounts{$a} } keys %totalcounts;
90
foreach $name (@order) {
91
    printf "%6.2f%10.2f%10.2f%10.2f     %s\n",
9869 ripley 92
    100 * $totalcounts{$name}/$total, $totalcounts{$name},
93
    100 * $leafcounts{$name}/$total, $leafcounts{$name},
9705 hornik 94
    $name;
95
}
96
 
97
print "\n";
98
print "   %       self        %       total\n";
99
print " self     seconds    total    seconds    name\n";
100
 
101
@order = sort { $leafcounts{$b} <=> $leafcounts{$a} } keys %leafcounts;
102
foreach $name (@order) {
103
    printf "%6.2f%10.2f%10.2f%10.2f     %s\n",
9869 ripley 104
    100 * $leafcounts{$name}/$total, $leafcounts{$name},
105
    100 * $totalcounts{$name}/$total, $totalcounts{$name},
9705 hornik 106
    $name;
107
}