blib/lib/Devel/DProfLB.pm

filter: Statementstmt 59.7% 55 / 92 Branchbran 23.6% 9 / 38 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 75.0% 6 / 8 Podpod 33.3% 1 / 3 totaltotal 50.0% 72 / 144 CC 21 SCAR 40.2
CC
21
Cov
49%
sub
132.0
END
11.0
BEGIN
3.6
CRAP 55.9
↑ Summary
1 # -*- perl -*-
2  
3 # Copyright (c) 2006 by Jeff Weisberg
4 # Author: Jeff Weisberg <jaw+dprof @ tcp4me.com>
5 # Created: 2006-Mar-11 22:16 (EST)
6 # Function: code profiler
7 #
8 # $Id: DProfLB.pm,v 1.3 2006/05/27 17:39:48 jaw Exp jaw $
9  
10 package Devel::DProfLB;
11 # use strict; - does not play well with the debugger
12 1 our $VERSION = '0.01';
13  
14 =head1 NAME
15  
16 Devel::DProfLB - tell me why my perl program runs so slowly
17  
18 =head1 SYNOPSIS
19  
20 perl -d:DProfLB program.pl
21 dprofpp
22  
23 =head1 DESCRIPTION
24  
25 The Devel::DProfLB package is a Perl code profiler.
26  
27 It is intended to produce output similar to, and
28 compatible with, Devel::DProf, but be 'Less Bad'.
29  
30 This will collect information on the execution time of a Perl
31 script and of the subs in that script. This information
32 can be used to determine which subroutines are using the
33 most time and which subroutines are being called most
34 often. This information can also be used to create an
35 execution graph of the script, showing subroutine
36 relationships.
37  
38 To profile a Perl script run the perl interpreter with the
39 -d debugging switch. The profiler uses the debugging
40 hooks. So to profile script test.pl the following command
41 should be used:
42  
43 perl5 -d:DProfLB test.pl
44  
45 When the script terminates the profiler will dump the profile information to
46 a file called tmon.out. A tool like dprofpp can be used
47 to interpret the information which is in that profile.
48 The following command will print the top 15 subroutines
49 which used the most time:
50  
51 dprofpp
52  
53 To print an execution graph of the subroutines in the
54 script use the following command:
55  
56 dprofpp -T
57  
58 Consult the dprofpp manpage for other options.
59  
60 =head1 ENVIRONMENT
61  
62 =over 4
63  
64 =item C<PERL_DPROF_OUT_FILE_NAME>
65  
66 Filename to save profile data to, default is F<tmon.out>
67  
68 =back
69  
70 =head1 BUGS
71  
72 Valid profiling data is not saved until the application
73 terminates and runs this modules END{} block. Applications
74 which cause END{} blocks not to run (such as call _exit
75 or exec) will leave a corrupt and/or incomplete temporary data file.
76  
77 On most systems, the timing data recorded by this profiler has
78 a granularity of 0.01 second. This may or may not be precise
79 enough for your application.
80  
81 If the program being profiled contains subroutines which do
82 not return in a normal manner (such as by throwing an exception),
83 the timing data is estimated and may be attributed incorrectly.
84  
85 Et cetera.
86  
87 =head1 SECURITY ISSUES
88  
89 The standard dprofpp program blindly C<eval>s portions
90 of the tmon.out datafile.
91  
92 =head1 SEE ALSO
93  
94 Devel::Profile
95 Devel::DProf
96 dprofpp
97 Yellowstone National Park
98
99 =head1 AUTHOR
100  
101 Jeff Weisberg - http://www.tcp4me.com/
102
103 =cut
104 ;
105  
106 package DB;
107  
108 1 ▶use POSIX 'times', # different than the builtin times
Subroutine BEGIN: called CC=1 SCAR=0.0
109 1 'sysconf', '_SC_CLK_TCK';
110  
111 1 my @prof_stack = (); # call stack, to account for subs that haven't returned
112 1 my $realtime_adj; # because it overflows an int32
113 my $hz; # clock ticks per second
114 1 my $prof_pid; # process id of process being profiled
115 1 my @overhead; # calibration overhead
116 1 my $tmpfile; # temporary data file
117 1 ▶my $monfile = $ENV{PERL_DPROF_OUT_FILE_NAME} || 'tmon.out';
Condition50%1/2
$ENV{'PERL_DPROF_OUT_FILE_NAME'} || 'tmon.out'
l!l
01
MC/DC0%0/1
$ENV{'PERL_DPROF_OUT_FILE_NAME'} || 'tmon.out'
$ENV{'PERL_DPROF_OUT_FILE_NAME'}
Truth table50%1/2
A
A= $ENV{'PERL_DPROF_OUT_FILE_NAME'}
Aresult
11
00
118 1 my $NCALOOP = 1000;
119 1 my $calibrated = 0;
120 1 our $sub;
121  
122 0 ▶sub DB {}
Subroutine DB: not called CC=1 SCAR=0.0
Pod undocumented
123  
124 1 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.7
125  
126 1 $prof_pid = $$;
127 1 $tmpfile = "tmon$$.out";
128 1 ▶ open(PROF, ">$tmpfile") || die "cannot open $tmpfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open PROF, ">$tmpfile" or die "cannot open ${tmpfile}: $!\n" 1 0 1
129
130 # calculate hertz
131 1 eval { $hz = sysconf( _SC_CLK_TCK ) };
132 1 ▶ unless( $hz ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($hz) 0 1 1
133 # if unavailable, estimate
134 0 my($st) = times();
135 0 sleep 1;
136 0 my($et) = times();
137 0 $hz = $et - $st;
138 }
139
140 1 ($realtime_adj) = times();
141  
142 }
143 1 ▶END {
Subroutine END: called CC=9 SCAR=23.9
144  
145 # original process only, if we fork()
146 1 ▶ return unless $$ == $prof_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $$ == $prof_pid 0 1 1
147
148 1 my($rt, $ut, $st) = prof_times();
149  
150 # generate data for any unfinished subs
151 1 ▶ if( @prof_stack ){
Branch: 1 branch
BranchTrueFalseTotal
if (@prof_stack) 0 1 1
152 0 print PROF "# the following did not return, due to program termination\n";
153 0 for my $asx (reverse @prof_stack){
154 0 print PROF "- $ut $st $rt $asx\n";
155 }
156 }
157
158 1 close PROF;
159  
160 # reopen data, add headers, output new file
161 1 ▶ open(TMP, $tmpfile) || warn "cannot open $tmpfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open TMP, $tmpfile or warn "cannot open ${tmpfile}: $!\n" 1 0 1
162 1 ▶ open(PROF, ">$monfile") || warn "cannot open $monfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open PROF, ">$monfile" or warn "cannot open ${monfile}: $!\n" 1 0 1
163
164 # output header
165 1 print PROF "#fOrTyTwO\n";
166 # this portion of the header is blindly evaled by dprofpp
167 # any valid perl may be placed here
168 # print PR0F "`echo pwned, rm -rf /`;\n"; # Yikes!
169 # print PROF 'warn "SECURITY WARNING: this version of $0 may be unsafe. upgrade?\n";', "\n";
170 1 print PROF "\$hz=$hz;\n\$XS_VERSION='DProfLB-$Devel::DProfLB::VERSION';\n";
171 1 print PROF "# All timing values are given in HZ\n";
172 1 print PROF "\$over_utime=$overhead[1]; \$over_stime=$overhead[2]; \$over_rtime=$overhead[0];\n";
173 1 print PROF "\$over_tests=$NCALOOP;\n";
174 1 print PROF "\$rrun_utime=$ut; \$rrun_stime=$st; \$rrun_rtime=$rt;\n";
175 1 print PROF "PART2\n";
176  
177 # remove calibration artifacts
178 1 ▶ while(<TMP>){ last if /^\#CALIBRATED/ }
Branch: 2 branches
BranchTrueFalseTotal
if (defined($_ = readline TMP)) 1 0 1
if /^\#CALIBRATED/ 1 0 1
179 # copy the rest
180 0 ▶ while(<TMP>){ print PROF }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline TMP)) 0 1 1
181
182 1 close TMP;
183 1 close PROF;
184 1 unlink $tmpfile;
185 1 $prof_pid = undef;
186 }
187  
188 3 ▶sub prof_times {
Subroutine prof_times: called CC=1 SCAR=0.0
Pod undocumented
189 3 my @t = times;
190  
191 # NB: ^T * HZ > MAX_INT32
192 # => force positive
193 3 $t[0] -= $realtime_adj;
194 3 @t;
195 }
196  
197 0 ▶sub sub {
Subroutine sub: not called CC=11 SCAR=48.8
Pod documented
198  
199 0 my($rt, $ut, $st) = prof_times();
200
201 0 my $sx = $sub;
202 0 ▶ if( ref $sx ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sx) 0 0 0
203 0 my @c = caller;
204 # was 0, now 1
205 # nb: @c = (pkg, file, line, ...)
206 0 $sx = "<anon>:$c[1]:$c[2]";
207 }
208  
209 # do not corrupt data on fork()
210 0 my $noprof = $$ != $prof_pid;
211 0 ▶ if( $noprof ){
Branch: 1 branch
BranchTrueFalseTotal
if ($noprof) 0 0 0
212 0 close PROF;
213 }
214
215 0 ▶ print PROF "+ $ut $st $rt $sx\n" unless $noprof;
Branch: 1 branch
BranchTrueFalseTotal
unless $noprof 0 0 0
216
217 0 push @prof_stack, $sx;
218 0 my $ss = @prof_stack;
219
220 0 my( $wa, $r, @r );
221 0 $wa = wantarray;
222 0 ▶ if( $wa ){
Branch: 2 branches
BranchTrueFalseTotal
if ($wa) { } 0 0 0
elsif (defined $wa) { } 0 0 0
223 0 @r = &$sub;
224 }elsif( defined $wa ){
225 0 $r = &$sub;
226 }else{
227 0 &$sub;
228 }
229  
230 0 ($rt, $ut, $st) = prof_times();
231
232 0 ▶ if( $ss < @prof_stack ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ss < @prof_stack) 0 0 0
233 # we took an exception - account for aborted subs
234  
235 0 ▶ while( $ss < @prof_stack ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ss < @prof_stack) 0 0 0
236 0 my $asx = pop @prof_stack;
237 0 ▶ next if $noprof;
Branch: 1 branch
BranchTrueFalseTotal
if $noprof 0 0 0
238 0 print PROF "# $asx did not return normally\n";
239 0 print PROF "- $ut $st $rt $asx\n";
240 }
241 }
242
243 0 pop @prof_stack;
244 0 ▶ print PROF "- $ut $st $rt $sx\n" unless $noprof;
Branch: 1 branch
BranchTrueFalseTotal
unless $noprof 0 0 0
245  
246 0 ▶ if( $wa ){
Branch: 1 branch
BranchTrueFalseTotal
if ($wa) { } 0 0 0
247 0 @r;
248 }else{
249 0 $r;
250 }
251 }
252  
253 # calculate (estimate) profiler overhead
254 package Devel::DProfLB;
255 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
256 1 my @st = DB::prof_times();
257 1000 ▶sub __db_calibrate_adj {
Subroutine __db_calibrate_adj: called CC=1 SCAR=0.0
258 1000 my $x = shift;
259 }
260 1 for my $i (1..$NCALOOP){
261 1000 __db_calibrate_adj();
262 }
263 1 my @et = DB::prof_times();
264 1 for my $i (0..2){ $overhead[$i] = $et[$i] - $st[$i] }
265 1 print DB::PROF "#CALIBRATED\n";
266  
267 1 1;
↑ Summary