blib/lib/Devel/NYTProf/Reader.pm

filter: Statementstmt 89.2% 248 / 278 Branchbran 68.7% 99 / 144 Conditioncond 77.2% 68 / 88 MC/DCmcdc 51.0% 24 / 47 Subroutinesub 100% 25 / 25 Podpod 0.0% 0 / 12 totaltotal 78.1% 464 / 594 CC 117 SCAR 46.9
CC
117
Cov
81%
_generate_report
178.5
fname_for_fileinfo
7.1
url_for_file
6.1
CRAP 109.0
« blib/script/flamegraph.pl ↑ Summary blib/lib/Devel/NYTProf/FileInfo.pm »
1 # vim: ts=8 sw=4 expandtab:
2 ##########################################################
3 ## This script is part of the Devel::NYTProf distribution
4 ##
5 ## Copyright, contact and other information can be found
6 ## at the bottom of this file, or by going to:
7 ## http://metacpan.org/release/Devel-NYTProf/
8 ##
9 ###########################################################
10 package Devel::NYTProf::Reader;
11  
12 351 our $VERSION = '6.16';
13  
14 351 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 351 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 351 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 351 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 351 ▶use List::Util qw(sum max);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 351 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 351 ▶use Devel::NYTProf::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 351 ▶use Devel::NYTProf::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
24 html_safe_filename
25 calculate_median_absolute_deviation
26 trace_level
27 351 );
28  
29 # These control the limits for what the script will consider ok to severe times
30 # specified in standard deviations from the mean time
31 351 ▶use constant SEVERITY_SEVERE => 2.0; # above this deviation, a bottleneck
Subroutine BEGIN: called CC=1 SCAR=0.0
32 351 ▶use constant SEVERITY_BAD => 1.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 351 ▶use constant SEVERITY_GOOD => 0.5; # within this deviation, okay
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35  
36 # Static class variables
37 351 our $FLOAT_FORMAT = $Config{nvfformat};
38 351 $FLOAT_FORMAT =~ s/"//g;
39  
40 # Class methods
41 307 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
42 307 my $class = shift;
43 307 my $file = shift;
44 307 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
3304
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
45  
46 307 ▶ my $self = {
Condition50%1/2
$file || 'nytprof.out'
l!l
3070
MC/DC0%0/1
$file || 'nytprof.out'
$file
Truth table50%1/2
A
A= $file
Aresult
11
00
47 file => $file || 'nytprof.out',
48 output_dir => '.',
49 suffix => '.csv',
50 header => "# Profile data generated by Devel::NYTProf::Reader\n"
51 . "# Version: v$Devel::NYTProf::Core::VERSION\n"
52 . "# More information at http://metacpan.org/release/Devel-NYTProf/\n"
53 . "# Format: time,calls,time/call,code\n",
54 datastart => '',
55 mk_report_source_line => undef,
56 mk_report_xsub_line => undef,
57 mk_report_separator_line => undef,
58 line => [
59 {},
60 {value => 'time', end => ',', default => '0'},
61 {value => 'calls', end => ',', default => '0'},
62 {value => 'time/call', end => ',', default => '0'},
63 {value => 'source', end => '', default => ''},
64 {end => "\n"}
65 ],
66 dataend => '',
67 footer => '',
68 merged_fids => '',
69 taintmsg => "# WARNING!\n"
70 . "# The source file used in generating this report has been modified\n"
71 . "# since generating the profiler database. It might be out of sync\n",
72 sawampersand => "# NOTE!\n"
73 . "# This file uses special regexp match variables that impact the performance\n"
74 . "# of all regular expression in the program!\n"
75 . "# See WARNING in http://perldoc.perl.org/perlre.html#Capture-buffers\n",
76 current_level => '',
77 };
78  
79 307 bless($self, $class);
80 $self->{profile} = Devel::NYTProf::Data->new({
81 %$opts,
82 filename => $self->{file},
83 307 });
84  
85 307 return $self;
86 }
87  
88  
89  
90 ##
91 612 ▶sub set_param {
Subroutine set_param: called CC=3 SCAR=11.0
Pod undocumented
92 612 my ($self, $param, $value) = @_;
93  
94 612 ▶ if (!exists $self->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{$param}) { } 1 611 612
95 1 confess "Attempt to set $param to $value failed: $param is not a valid " . "parameter\n";
96 }
97 else {
98 611 ▶ return $self->{$param} unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 610 611
99 610 $self->{$param} = $value;
100 }
101 610 undef;
102 }
103  
104  
105 65879 ▶sub get_param {
Subroutine get_param: called CC=3 SCAR=11.0
Pod undocumented
106 65879 my ($self, $param, $code_args) = @_;
107 65879 my $value = $self->{$param};
108 65879 ▶ if (ref $value eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "CODE") 1 65878 65879
109 1 ▶ $code_args ||= [];
Condition50%1/2
$code_args ||= []
l!l
01
MC/DC0%0/1
$code_args ||= []
$code_args
Truth table50%1/2
A
A= $code_args
Aresult
11
00
110 1 $value = $value->(@$code_args);
111 }
112 65879 return $value;
113 }
114  
115 ##
116 8132 ▶sub file_has_been_modified {
Subroutine file_has_been_modified: called CC=2 SCAR=6.9
Pod undocumented
117 8132 my $self = shift;
118 8132 my $file = shift;
119 8132 ▶ return undef unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 320 7812 8132
120 7812 my $mtime = (stat $file)[9];
121 7812 return ($mtime > $self->{profile}{attribute}{basetime});
122 }
123  
124 ##
125 1 ▶sub _output_additional {
Subroutine _output_additional: called CC=2 SCAR=7.0
126 1 my ($self, $fname, $content) = @_;
127 1 ▶ open(OUT, '>', "$self->{output_dir}/$fname")
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">", qq[$self->{"output_dir"}/$fname] or confess(qq[Unable to open $self->{"output_dir"}/$fname for writing; $!\n]) 1 0 1
128 or confess "Unable to open $self->{output_dir}/$fname for writing; $!\n";
129 1 print OUT $content;
130 1 close OUT;
131 }
132  
133 ##
134 306 ▶sub output_dir {
Subroutine output_dir: called CC=4 SCAR=14.1
Pod undocumented
135 306 my ($self, $dir) = @_;
136 306 ▶ return $self->{output_dir} unless defined($dir);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 1 305 306
137 305 ▶ if (!mkdir $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $dir) 305 0 305
138 305 ▶ confess "Unable to create directory $dir: $!\n" if $! !~ /exists/;
Branch: 1 branch
BranchTrueFalseTotal
unless $! =~ /exists/ 0 305 305
139 }
140 305 $self->{output_dir} = $dir;
141 }
142  
143 ##
144 305 ▶sub report {
Subroutine report: called CC=5 SCAR=16.2
Pod undocumented
145 305 my $self = shift;
146 305 my ($opts) = @_;
147  
148 305 my $level_additional_sub = $opts->{level_additional};
149 305 my $profile = $self->{profile};
150 305 my $modes = $profile->get_profile_levels;
151 305 my @levels = grep { {reverse %$modes}->{$_} } qw(sub block line);
152 305 for my $level (@levels) {
153 print "Writing $level reports to $self->{output_dir} directory\n"
154 915 ▶ unless $opts->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"quiet"} 912 3 915
155 $self->_generate_report($profile, $level,
156 915 ▶ show_progress => (not $opts->{quiet} and -t STDOUT)
Condition66%2/3
!$opts->{'quiet'} && -t STDOUT
!ll&&!rl&&r
39120
MC/DC0%0/2
!$opts->{'quiet'} && -t STDOUT
!$opts->{'quiet'} -t STDOUT
Truth table66%2/3
not(A) && B
A= !$opts->{'quiet'}B= -t STDOUT
ABresult
0X0
100
111
157 );
158 915 ▶ $level_additional_sub->($profile, $level)
Branch: 1 branch
BranchTrueFalseTotal
if $level_additional_sub 0 915 915
159 if $level_additional_sub;
160 }
161 }
162  
163 17324 ▶sub current_level {
Subroutine current_level: called CC=3 SCAR=11.0
Pod undocumented
164 17324 my $self = shift;
165 17324 ▶ $self->{current_level} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 917 16407 17324
166 17324 ▶ return $self->{current_level} || 'line';
Condition100%2/2
$self->{'current_level'} || 'line'
l!l
173231
MC/DC100%1/1
$self->{'current_level'} || 'line'
$self->{'current_level'}
Truth table100%2/2
A
A= $self->{'current_level'}
Aresult
11
00
167 }
168  
169 16410 ▶sub fname_for_fileinfo {
Subroutine fname_for_fileinfo: called CC=7 SCAR=19.6
Pod undocumented
170 16410 my ($self, $fi, $level) = @_;
171 16410 ▶ confess "No fileinfo" unless $fi;
Branch: 1 branch
BranchTrueFalseTotal
unless $fi 1 16409 16410
172 16409 ▶ $level ||= $self->current_level;
Condition66%2/3
$level ||= $self->current_level
l!l&&r!l&&!r
4164050
MC/DC0%0/2
$level ||= $self->current_level
$level $self->current_level
Truth table66%2/3
A ||= B
A= $levelB= $self->current_level
ABresult
1X1
011
000
173  
174 16409 my $fname = $fi->filename_without_inc;
175  
176 # We want to have descriptive and unambiguous filename
177 # but we don't want to risk failure due to filenames being longer
178 # than MAXPATH (including the length of whatever dir we're writing
179 # the report files into). So we truncate to the last component if
180 # the filenames seems 'dangerously long'. XXX be smarter about this.
181 # This is safe from ambiguity because we add the fid to the filename below.
182 16409 ▶ my $max_len = $ENV{NYTPROF_FNAME_TRIM} || 50;
Condition50%1/2
$ENV{'NYTPROF_FNAME_TRIM'} || 50
l!l
016409
MC/DC0%0/1
$ENV{'NYTPROF_FNAME_TRIM'} || 50
$ENV{'NYTPROF_FNAME_TRIM'}
Truth table50%1/2
A
A= $ENV{'NYTPROF_FNAME_TRIM'}
Aresult
11
00
183 16409 ▶ $fname =~ s!/.*/!/.../! if length($fname) > $max_len; # remove dir path
Branch: 1 branch
BranchTrueFalseTotal
if length $fname > $max_len 41 16368 16409
184 16409 ▶ $fname = "TOOLONG" if length($fname) > $max_len; # just in case
Branch: 1 branch
BranchTrueFalseTotal
if length $fname > $max_len 32 16377 16409
185  
186 16409 $fname = html_safe_filename($fname);
187 16409 $fname .= "-".$fi->fid; # to ensure uniqueness and for info
188 16409 ▶ $fname .= "-$level" if $level;
Branch: 1 branch
BranchTrueFalseTotal
if $level 16409 0 16409
189  
190 16409 return $fname;
191 }
192  
193  
194 ##
195 915 ▶sub _generate_report {
Subroutine _generate_report: called CC=85 SCAR=51.8
196 915 my $self = shift;
197 915 my ($profile, $LEVEL, %opts) = @_;
198  
199 915 $self->current_level($LEVEL);
200  
201 915 ▶ my @all_fileinfos = $profile->all_fileinfos
Branch: 1 branch
BranchTrueFalseTotal
my(@all_fileinfos) = $profile->all_fileinfos or carp("Profile report data contains no files") 915 0 915
202 or carp "Profile report data contains no files";
203  
204 915 my @fis = @all_fileinfos;
205 915 ▶ if ($LEVEL ne 'line') {
Branch: 1 branch
BranchTrueFalseTotal
if ($LEVEL ne "line") 610 305 915
206 # we only generate line-level reports for evals
207 # for efficiency and because some data model editing only
208 # is only implemented for line-level data
209 610 @fis = grep { not $_->is_eval } @fis;
210 }
211  
212 915 my $progress;
213 915 foreach my $fi (@fis) {
214  
215 16403 ▶ if ($opts{show_progress}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"show_progress"}) 0 16403 16403
216 0 local $| = 1;
217 0 ++$progress;
218 0 printf "\r %3d%% ... ", $progress/@fis*100;
219 }
220  
221 16403 my $meta = $fi->meta;
222 16403 my $filestr = $fi->filename;
223  
224 # { linenumber => { subname => [ count, time ] } }
225 16403 my $subcalls_at_line = { %{ $fi->sub_call_lines } };
226 16403 ▶ my $subcalls_max_line = max( keys %$subcalls_at_line ) || 0;
Condition100%2/2
&max(keys %$subcalls_at_line) || 0
l!l
145951808
MC/DC100%1/1
&max(keys %$subcalls_at_line) || 0
&max(keys %$subcalls_at_line)
Truth table100%2/2
A
A= &max(keys %$subcalls_at_line)
Aresult
11
00
227  
228 # { linenumber => [ $subinfo, ... ] }
229 16403 my $subdefs_at_line = { %{ $profile->subs_defined_in_file_by_line($filestr) } };
230 16403 ▶ my $subdefs_max_line = max( keys %$subdefs_at_line ) || 0;
Condition100%2/2
&max(keys %$subdefs_at_line) || 0
l!l
33916064
MC/DC100%1/1
&max(keys %$subdefs_at_line) || 0
&max(keys %$subdefs_at_line)
Truth table100%2/2
A
A= &max(keys %$subdefs_at_line)
Aresult
11
00
231 16403 delete $subdefs_at_line->{0}; # xsubs handled separately
232  
233 # { linenumber => { fid => $fileinfo } }
234 16403 my $evals_at_line = { %{ $fi->evals_by_line } };
235 16403 ▶ my $evals_max_line = max( keys %$evals_at_line ) || 0;
Condition100%2/2
&max(keys %$evals_at_line) || 0
l!l
170414699
MC/DC100%1/1
&max(keys %$evals_at_line) || 0
&max(keys %$evals_at_line)
Truth table100%2/2
A
A= &max(keys %$evals_at_line)
Aresult
11
00
236  
237 # note that a file may have no source lines executed, so no keys here
238 # (but is included because some xsubs in the package were executed)
239 16403 ▶ my $lines_array = $fi->line_time_data([$LEVEL]) || [];
Condition100%2/2
$fi->line_time_data([$LEVEL]) || []
l!l
144591944
MC/DC100%1/1
$fi->line_time_data([$LEVEL]) || []
$fi->line_time_data([$LEVEL])
Truth table100%2/2
A
A= $fi->line_time_data([$LEVEL])
Aresult
11
00
240 16403 my $src_max_line = scalar @$lines_array;
241  
242 16403 for ($src_max_line, $subcalls_max_line, $subdefs_max_line, $evals_max_line) {
243 65612 ▶ next if $_ < 2**16;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 65536 65612 0 65612
244 0 warn "Ignoring indication that $filestr has $_ lines! (Possibly corrupt data)\n";
245 0 $_ = 0;
246 }
247  
248 16403 my $max_linenum = max(
249 $src_max_line,
250 $subcalls_max_line,
251 $subdefs_max_line,
252 $evals_max_line,
253 );
254  
255 16403 ▶ warn sprintf "%s max lines: %s (stmts %s, subcalls %s, subdefs %s, evals %s)\n",
Branch: 1 branch
BranchTrueFalseTotal
if trace_level() >= 4 or $max_linenum > 32768 0 16403 16403
Condition33%1/3
trace_level() >= 4 or $max_linenum > 32768
l!l&&r!l&&!r
0016403
MC/DC0%0/2
trace_level() >= 4 or $max_linenum > 32768
trace_level() >= 4 $max_linenum > 32768
Truth table33%1/3
A or B
A= trace_level() >= 4B= $max_linenum > 32768
ABresult
1X1
011
000
256 $filestr, $max_linenum, scalar @$lines_array,
257 $subcalls_max_line, $subdefs_max_line, $evals_max_line
258 if trace_level() >= 4 or $max_linenum > 2**15;
259  
260 16403 my %stats_accum; # holds all line times. used to find median
261 my %stats_by_line; # holds individual line stats
262 16403 my $runningTotalTime = 0; # holds the running total
263 # (should equal sum of $stats_accum)
264 16403 my $runningTotalCalls = 0; # holds the running total number of calls.
265  
266 16403 ▶ for (my $linenum = 0; $linenum <= $max_linenum; ++$linenum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linenum <= $max_linenum) 6637028 16403 6653431
267  
268 6637028 ▶ if (my $subdefs = delete $subdefs_at_line->{$linenum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subdefs = delete $subdefs_at_line->{$linenum}) 5673 6631355 6637028
269 5673 $stats_by_line{$linenum}->{'subdef_info'} = $subdefs;
270 }
271  
272 6637028 ▶ if (my $subcalls = delete $subcalls_at_line->{$linenum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subcalls = delete $subcalls_at_line->{$linenum}) 306706 6330322 6637028
273 306706 ▶ my $line_stats = $stats_by_line{$linenum} ||= {};
Condition50%1/2
$stats_by_line{$linenum} ||= {}
l!l
0306706
MC/DC0%0/1
$stats_by_line{$linenum} ||= {}
$stats_by_line{$linenum}
Truth table50%1/2
A
A= $stats_by_line{$linenum}
Aresult
11
00
274  
275 306706 $line_stats->{'subcall_info'} = $subcalls;
276 306706 $line_stats->{'subcall_count'} = sum(map { $_->[0] } values %$subcalls);
277 306706 $line_stats->{'subcall_time'} = sum(map { $_->[1] } values %$subcalls);
278  
279 613412 push @{$stats_accum{$_}}, $line_stats->{$_}
280 306706 for (qw(subcall_count subcall_time));
281 }
282  
283 6637028 ▶ if (my $evalcalls = delete $evals_at_line->{$linenum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $evalcalls = delete $evals_at_line->{$linenum}) 1848 6635180 6637028
284 1848 ▶ my $line_stats = $stats_by_line{$linenum} ||= {};
Condition100%2/2
$stats_by_line{$linenum} ||= {}
l!l
481800
MC/DC100%1/1
$stats_by_line{$linenum} ||= {}
$stats_by_line{$linenum}
Truth table100%2/2
A
A= $stats_by_line{$linenum}
Aresult
11
00
285  
286 # %$evals => { fid => $fileinfo }
287 1848 $line_stats->{'evalcall_info'} = $evalcalls;
288 1848 $line_stats->{'evalcall_count'} = values %$evalcalls;
289  
290 # get list of evals, including nested evals
291 1848 my @eval_fis = map { ($_, $_->has_evals(1)) } values %$evalcalls;
292 1848 $line_stats->{'evalcall_count_nested'} = @eval_fis;
293 $line_stats->{'evalcall_stmts_time_nested'} = sum(
294 1848 map { $_->sum_of_stmts_time } @eval_fis);
295 }
296  
297 6637028 ▶ if (my $stmts = $lines_array->[$linenum]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stmts = $lines_array->[$linenum]) 321215 6315813 6637028
298 321215 ▶ next if !@$stmts; # XXX happens for evals, investigate
Branch: 1 branch
BranchTrueFalseTotal
unless @$stmts 812 320403 321215
299  
300 320403 my ($stmt_time, $stmt_count) = @$stmts;
301 320403 ▶ my $line_stats = $stats_by_line{$linenum} ||= {};
Condition100%2/2
$stats_by_line{$linenum} ||= {}
l!l
104064216339
MC/DC100%1/1
$stats_by_line{$linenum} ||= {}
$stats_by_line{$linenum}
Truth table100%2/2
A
A= $stats_by_line{$linenum}
Aresult
11
00
302  
303 # The debugger cannot stop on BEGIN{...} lines. A line in a begin
304 # may set a scalar reference to something that needs to be eval'd later.
305 # as a result, if the variable is expanded outside of the BEGIN, we'll
306 # see the original BEGIN line, but it won't have any calls or times
307 # associated. This will cause a divide by zero error.
308 320403 ▶ $stmt_count ||= 1;
Condition100%2/2
$stmt_count ||= 1
l!l
319871532
MC/DC100%1/1
$stmt_count ||= 1
$stmt_count
Truth table100%2/2
A
A= $stmt_count
Aresult
11
00
309  
310 320403 $line_stats->{'time'} = $stmt_time;
311 320403 $line_stats->{'calls'} = $stmt_count;
312 320403 $line_stats->{'time/call'} = $stmt_time/$stmt_count;
313  
314 961209 push @{$stats_accum{$_}}, $line_stats->{$_}
315 320403 for (qw(time calls time/call));
316  
317 320403 $runningTotalTime += $stmt_time;
318 320403 $runningTotalCalls += $stmt_count;
319 }
320  
321 0 warn "$linenum: @{[ %{ $stats_by_line{$linenum} } ]}\n"
322 6636216 ▶ if trace_level() >= 3 && $stats_by_line{$linenum};
Branch: 1 branch
BranchTrueFalseTotal
if trace_level() >= 3 and $stats_by_line{$linenum} 0 6636216 6636216
Condition33%1/3
trace_level() >= 3 and $stats_by_line{$linenum}
!ll&&!rl&&r
663621600
MC/DC0%0/2
trace_level() >= 3 and $stats_by_line{$linenum}
trace_level() >= 3 $stats_by_line{$linenum}
Truth table33%1/3
A and B
A= trace_level() >= 3B= $stats_by_line{$linenum}
ABresult
0X0
100
111
323 }
324  
325 0 ▶ warn "unprocessed keys in subdefs_at_line: @{[ keys %$subdefs_at_line ]}\n"
Branch: 1 branch
BranchTrueFalseTotal
if %$subdefs_at_line 0 16403 16403
326 if %$subdefs_at_line;
327 0 ▶ warn "unprocessed keys in subcalls_at_line: @{[ keys %$subcalls_at_line ]}\n"
Branch: 1 branch
BranchTrueFalseTotal
if %$subcalls_at_line 0 16403 16403
328 if %$subcalls_at_line;
329 0 ▶ warn "unprocessed keys in evals_at_line: @{[ keys %$evals_at_line ]}\n"
Branch: 1 branch
BranchTrueFalseTotal
if %$evals_at_line 0 16403 16403
330 if %$evals_at_line;
331  
332 16403 $meta->{'time'} = $runningTotalTime;
333 16403 $meta->{'calls'} = $runningTotalCalls;
334 16403 ▶ $meta->{'time/call'} =
Branch: 1 branch
BranchTrueFalseTotal
$runningTotalCalls ? : 14459 1944 16403
335 ($runningTotalCalls) ? $runningTotalTime / $runningTotalCalls: 0;
336  
337 # Use Median Absolute Deviation Formula to get file deviations for each of
338 # calls, time and time/call values
339 my %stats_for_file = (
340 'calls' => calculate_median_absolute_deviation($stats_accum{'calls'}||[]),
341 'time' => calculate_median_absolute_deviation($stats_accum{'time'}||[]),
342 'time/call' => calculate_median_absolute_deviation($stats_accum{'time/call'}||[]),
343 subcall_count => calculate_median_absolute_deviation($stats_accum{subcall_count}||[]),
344 16403 ▶ subcall_time => calculate_median_absolute_deviation($stats_accum{subcall_time}||[]),
Condition100%10/10
$stats_accum{'calls'} || []
l!l
144591944
$stats_accum{'time'} || []
l!l
144591944
$stats_accum{'time/call'} || []
l!l
144591944
$stats_accum{'subcall_count'} || []
l!l
148991504
$stats_accum{'subcall_time'} || []
l!l
148991504
MC/DC100%1/1
$stats_accum{'calls'} || []
$stats_accum{'calls'}
MC/DC100%1/1
$stats_accum{'time'} || []
$stats_accum{'time'}
MC/DC100%1/1
$stats_accum{'time/call'} || []
$stats_accum{'time/call'}
MC/DC100%1/1
$stats_accum{'subcall_count'} || []
$stats_accum{'subcall_count'}
MC/DC100%1/1
$stats_accum{'subcall_time'} || []
$stats_accum{'subcall_time'}
Truth table100%2/2
A
A= $stats_accum{'calls'}
Aresult
11
00
Truth table100%2/2
A
A= $stats_accum{'time'}
Aresult
11
00
Truth table100%2/2
A
A= $stats_accum{'time/call'}
Aresult
11
00
Truth table100%2/2
A
A= $stats_accum{'subcall_count'}
Aresult
11
00
Truth table100%2/2
A
A= $stats_accum{'subcall_time'}
Aresult
11
00
345 );
346  
347 # the output file name that will be open later. Not including directory at this time.
348 # keep here so that the variable replacement subs can get at it.
349 16403 my $fname = $self->fname_for_fileinfo($fi) . $self->{suffix};
350  
351 # localize header and footer for variable replacement
352 16403 my $header = $self->get_param('header', [$profile, $fi, $fname, $LEVEL]);
353 16403 my $datastart = $self->get_param('datastart', [$profile, $fi]);
354 16403 my $dataend = $self->get_param('dataend', [$profile, $fi]);
355 16403 my $FILE = $filestr;
356 #warn Dumper(\%stats_by_line);
357 # open output file
358 #warn "$self->{output_dir}/$fname";
359 16403 ▶ open(OUT, ">", "$self->{output_dir}/$fname")
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">", qq[$self->{"output_dir"}/$fname] or confess(qq[Unable to open $self->{"output_dir"}/$fname ] . "for writing: $!\n") 16403 0 16403
360 or confess "Unable to open $self->{output_dir}/$fname " . "for writing: $!\n";
361  
362 # begin output
363 16403 print OUT $header;
364  
365 # If we don't have savesrc for the file then we'll be reading the current
366 # file contents which may have changed since the profile was run.
367 # In this case we need to warn the user as the report would be garbled.
368 16403 ▶ print OUT $self->get_param('taintmsg', [$profile, $fi])
Branch: 1 branch
BranchTrueFalseTotal
if not $fi->has_savesrc and $self->file_has_been_modified($filestr) 0 16403 16403
Condition66%2/3
not $fi->has_savesrc and $self->file_has_been_modified($filestr)
!ll&&!rl&&r
827281310
MC/DC0%0/2
not $fi->has_savesrc and $self->file_has_been_modified($filestr)
not $fi->has_savesrc $self->file_has_been_modified($filestr)
Truth table66%2/3
not(A) and B
A= not $fi->has_savesrcB= $self->file_has_been_modified($filestr)
ABresult
0X0
100
111
369 if !$fi->has_savesrc and $self->file_has_been_modified($filestr);
370  
371 print OUT $self->get_param('sawampersand', [$profile, $fi])
372 if $profile->{attribute}{sawampersand_fid}
373 16403 ▶ && $fi->fid == $profile->{attribute}{sawampersand_fid};
Branch: 1 branch
BranchTrueFalseTotal
if $profile->{"attribute"}{"sawampersand_fid"} and $fi->fid == $profile->{"attribute"}{"sawampersand_fid"} 0 16403 16403
Condition33%1/3
$profile->{"attribute"}{"sawampersand_fid"} and $fi->fid == $profile->{"attribute"}{"sawampersand_fid"}
!ll&&!rl&&r
1640300
MC/DC0%0/2
$profile->{"attribute"}{"sawampersand_fid"} and $fi->fid == $profile->{"attribute"}{"sawampersand_fid"}
$profile->{"attribute"}{"sawampersand_fid"} $fi->fid == $profile->{"attribute"}{"sawampersand_fid"}
Truth table33%1/3
A and B
A= $profile->{"attribute"}{"sawampersand_fid"}B= $fi->fid == $profile->{"attribute"}{"sawampersand_fid"}
ABresult
0X0
100
111
374  
375 print OUT $self->get_param('merged_fids', [$profile, $fi])
376 16403 ▶ if $fi->meta->{merged_fids};
Branch: 1 branch
BranchTrueFalseTotal
if $fi->meta->{"merged_fids"} 264 16139 16403
377  
378 16403 print OUT $datastart;
379  
380 16403 my $LINE = 1; # line number in source code
381 16403 my $src_lines = $fi->srclines_array;
382 16403 ▶ if (!$src_lines) { # no savesrc, and no file available
Branch: 1 branch
BranchTrueFalseTotal
unless ($src_lines) 319 16084 16403
383  
384 319 my $msg = '';
385 319 ▶ if ($fi->is_fake) {
Branch: 4 branches
BranchTrueFalseTotal
if ($fi->is_fake) { } 0 319 319
elsif ($fi->is_eval) { } 316 3 319
elsif ($filestr =~ m[^/loader/0x[0-9a-zA-Z]+/]) { } 0 3 3
elsif (not $fi->is_file) { } 0 3 3
386 # eg the "/unknown-eval-invoker"
387 0 $msg = "No source code available for synthetic (fake) file $filestr.",
388 }
389 elsif ($fi->is_eval) {
390 316 $msg = "No source code available for string eval $filestr.\nYou probably need to use a more recent version of perl. See savesrc option in documentation.",
391 }
392 elsif ($filestr =~ m{^/loader/0x[0-9a-zA-Z]+/}) {
393 # a synthetic file name that perl assigns when reading
394 # code returned by a CODE ref in @INC
395 0 $msg = "No source code available for 'file' loaded via CODE reference in \@INC.\nSee savesrc option in documentation.",
396 }
397 elsif (not $fi->is_file) {
398 0 $msg = "No source code available for non-file '$filestr'.\nYou probably need to use a more recent version of perl. See savesrc option in documentation.",
399 }
400 else {
401 3 ▶ $msg = "Unable to open '$filestr' for reading: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless $filestr =~ m[t/test01\.p$] 0 3 3
402 unless $filestr =~ m{t/test01\.p$};
403  
404 # clarify some current Moose limitations XXX
405 3 ▶ if ($filestr =~ m!/(accessor .*) defined at /!) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filestr =~ m[/(accessor .*) defined at /]) { } 0 3 3
elsif ($filestr =~ m[/(generated method \(unknown origin\))]) { } 0 3 3
406 0 $msg = "Source for generated Moose $1 isn't available ($filestr: $!)";
407 }
408 elsif ($filestr =~ m!/(generated method \(unknown origin\))!) {
409 0 $msg = "Source for Moose $1 isn't available ($filestr: $!)";
410 }
411  
412 # the report will not be complete, but this doesn't need to be fatal
413 3 my $hint = '';
414 3 ▶ $hint .= " Try running $0 in the same directory as you ran Devel::NYTProf, "
Branch: 1 branch
BranchTrueFalseTotal
if $filestr ne "-e" and not $filestr =~ m[^/] and not our $_generate_report_inc_hint++ 0 3 3
Condition33%2/6
$filestr ne "-e" and not $filestr =~ m[^/] and not our $_generate_report_inc_hint++
!ll&&!rl&&r
300
$filestr ne "-e" and not $filestr =~ m[^/]
!ll&&!rl&&r
030
MC/DC0%0/3
$filestr ne "-e" and not $filestr =~ m[^/] and not our $_generate_report_inc_hint++
$filestr ne "-e" not $filestr =~ m[^/] not our $_generate_report_inc_hint++
Truth table25%1/4
A and not(B) and not(C)
A= $filestr ne "-e"B= not $filestr =~ m[^/]C= not our $_generate_report_inc_hint++
ABCresult
0XX0
10X0
1100
1111
415 . "or ensure \@INC is correct."
416 if $filestr ne '-e'
417 and $filestr !~ m:^/:
418 and not our $_generate_report_inc_hint++; # only once
419  
420 # If neither $msg nor $hint has been populated, no need to
421 # warn, thereby avoiding superfluous new line in test output
422 3 ▶ if ($msg or $hint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg or $hint) 0 3 3
Condition33%1/3
$msg or $hint
l!l&&r!l&&!r
003
MC/DC0%0/2
$msg or $hint
$msg $hint
Truth table33%1/3
A or B
A= $msgB= $hint
ABresult
1X1
011
000
423 warn "$msg$hint\n"
424 # only once per filestr
425 0 ▶ unless our $_generate_report_filestr_warn->{$filestr}++;
Branch: 1 branch
BranchTrueFalseTotal
unless our $_generate_report_filestr_warn->{$filestr}++ 0 0 0
426 }
427  
428 }
429  
430 319 $src_lines = [ $msg ];
431 319 $LINE = 0; # start numbering from 0 to flag fake contents
432 }
433  
434 # ensure we don't have any undef source lines
435 # (to avoid warnings from the code below)
436 16403 my $src_undefs;
437 16403 ▶ defined $_ or $_ = '' && ++$src_undefs for @$src_lines;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or $_ = "" 10292439 0 10292439
438 # XXX shouldn't be need but don't have a test case so grumble
439 # about it in the hope of getting a test case
440 16403 ▶ warn sprintf "Saw %d missing (undef) lines in the %d lines of source code for %s\n",
Branch: 1 branch
BranchTrueFalseTotal
if $src_undefs 0 16403 16403
441 $src_undefs, scalar @$src_lines, $filestr
442 if $src_undefs;
443  
444 # Since we use @$src_lines to drive the report generation, pad the array to
445 # ensure it has enough lines to include all the available profile info.
446 # Then the report is still useful even if we have no source code.
447 16403 ▶ push @$src_lines, '' while @$src_lines < $max_linenum-1;
Branch: 1 branch
BranchTrueFalseTotal
@$src_lines < $max_linenum - 1 and do { push @$src_lines, "" } 232855 16403 249258
448  
449 16403 ▶ if (my $z = $stats_by_line{0}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $z = $stats_by_line{0}) 1216 15187 16403
450 # typically indicates cases where we could do better
451 1216 ▶ if (trace_level()) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level()) 0 1216 1216
452 0 warn "$filestr has unexpected info for line 0: @{[ %$z ]}\n";
453 # sub defs: used to be xsubs but they're handled separately now
454 # so there are no known causes of this any more
455 0 ▶ if (my $i = $z->{subdef_info}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = $z->{"subdef_info"}) 0 0 0
456 0 warn "0: @{[ map { $_->subname } @$i ]}\n"
457 }
458 # sub calls: they're typically END blocks that appear to be
459 # invoked from the main .pl script perl ran.
460 # Also some BEGINs and things like main::CORE:ftfile
461 # (see CPANDB's cpangraph script for some examples)
462 0 ▶ if (my $i = $z->{subcall_info}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = $z->{"subcall_info"}) 0 0 0
463 0 warn sprintf "0: called %20s %s\n", $_, join " ", @{ $i->{$_} }
464 0 for sort keys %$i;
465 }
466 }
467  
468 1216 $LINE = 0;
469 1216 unshift @$src_lines, "Profile data that couldn't be associated with a specific line:";
470 }
471  
472 my $line_sub = $self->{mk_report_source_line}
473 16403 ▶ or die "mk_report_source_line not set";
Branch: 1 branch
BranchTrueFalseTotal
my $line_sub = $self->{"mk_report_source_line"} or die "mk_report_source_line not set" 16403 0 16403
474  
475 16403 my $prev_line = '-';
476 16403 ▶ while ( @$src_lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$src_lines) 10302437 16403 10318840
477 10302437 my $line = shift @$src_lines;
478 10302437 chomp $line;
479  
480 # detect a series of blank lines, e.g. a chunk of pod savesrc didn't store
481 my $skip_blanks = (
482 $prev_line eq '' && $line eq '' && # blank behind and here
483 @$src_lines && $src_lines->[0] =~ /^\s*$/ && # blank ahead
484 10302437 ▶ !$stats_by_line{$LINE} # nothing to report
Condition100%12/12
$prev_line eq '' && $line eq '' && @$src_lines && $src_lines->[0] =~ /^\s*$/ && !$stats_by_line{$LINE}
!ll&&!rl&&r
1029388656502901
$prev_line eq "" and $line eq "" and @$src_lines and $src_lines->[0] =~ /^\s*$/
!ll&&!rl&&r
10279606142808551
$prev_line eq "" and $line eq "" and @$src_lines
!ll&&!rl&&r
1027934326322831
$prev_line eq "" and $line eq ""
!ll&&!rl&&r
8167959211138423094
MC/DC100%5/5
$prev_line eq '' && $line eq '' && @$src_lines && $src_lines->[0] =~ /^\s*$/ && !$stats_by_line{$LINE}
$prev_line eq "" $line eq "" @$src_lines $src_lines->[0] =~ /^\s*$/ !$stats_by_line{$LINE}
Truth table100%6/6
A and B and C and D && not(E)
A= $prev_line eq ""B= $line eq ""C= @$src_linesD= $src_lines->[0] =~ /^\s*$/E= !$stats_by_line{$LINE}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
485 );
486  
487 10302437 ▶ if ($line =~ m/^\# \s* line \s+ (\d+) \b/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\# \s* line \s+ (\d+) \b/x) 0 10302437 10302437
488 # XXX we should be smarter about this - patches welcome!
489 # We should at least ignore the common AutoSplit case
490 # which we detect and workaround elsewhere.
491 warn "Ignoring '$line' directive at line $LINE - profile data for $filestr will be out of sync with source\n"
492 0 ▶ unless our $line_directive_warn->{$filestr}++; # once per file
Branch: 1 branch
BranchTrueFalseTotal
unless our $line_directive_warn->{$filestr}++ 0 0 0
493 }
494  
495 print OUT $line_sub->(
496 ($skip_blanks) ? "- -" : $LINE, $line,
497 10302437 ▶ $stats_by_line{$LINE} || {},
Branch: 1 branch
BranchTrueFalseTotal
$skip_blanks ? : 2901 10299536 10302437
Condition100%2/2
$stats_by_line{$LINE} || {}
l!l
5300479772390
MC/DC100%1/1
$stats_by_line{$LINE} || {}
$stats_by_line{$LINE}
Truth table100%2/2
A
A= $stats_by_line{$LINE}
Aresult
11
00
498 \%stats_for_file,
499 $profile,
500 $fi,
501 );
502  
503 10302437 ▶ if ($skip_blanks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_blanks) 2901 10299536 10302437
504 2901 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (@$src_lines and $src_lines->[0] =~ /^\s*$/ and not $stats_by_line{$LINE + 1}) 224073 2901 226974
Condition100%6/6
@$src_lines and $src_lines->[0] =~ /^\s*$/ and not $stats_by_line{$LINE + 1}
!ll&&!rl&&r
3482553224073
@$src_lines and $src_lines->[0] =~ /^\s*$/
!ll&&!rl&&r
25296226626
MC/DC100%3/3
@$src_lines and $src_lines->[0] =~ /^\s*$/ and not $stats_by_line{$LINE + 1}
@$src_lines $src_lines->[0] =~ /^\s*$/ not $stats_by_line{$LINE + 1}
Truth table100%4/4
A and B and not(C)
A= @$src_linesB= $src_lines->[0] =~ /^\s*$/C= not $stats_by_line{$LINE + 1}
ABCresult
0XX0
10X0
1100
1111
505 @$src_lines && $src_lines->[0] =~ /^\s*$/ &&
506 !$stats_by_line{$LINE+1}
507 ) {
508 224073 shift @$src_lines;
509 224073 $LINE++;
510 }
511 }
512 10302437 $prev_line = $line;
513 }
514 continue {
515 10302437 $LINE++;
516 }
517  
518 16403 my $separator_sub = $self->{mk_report_separator_line};
519  
520 # iterate over xsubs
521 $line_sub = $self->{mk_report_xsub_line}
522 16403 ▶ or die "mk_report_xsub_line not set";
Branch: 1 branch
BranchTrueFalseTotal
$line_sub = $self->{"mk_report_xsub_line"} or die "mk_report_xsub_line not set" 16403 0 16403
523 16403 my $subs_defined_in_file = $profile->subs_defined_in_file($filestr);
524 16403 foreach my $subname (sort keys %$subs_defined_in_file) {
525 244929 my $subinfo = $subs_defined_in_file->{$subname};
526 244929 my $kind = $subinfo->kind;
527  
528 244929 ▶ next if $kind eq 'perl';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "perl" 133758 111171 244929
529 111171 ▶ next if $subinfo->calls == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $subinfo->calls == 0 51 111120 111171
530  
531 111120 ▶ if ($separator_sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($separator_sub) 0 111120 111120
532 0 print OUT $separator_sub->($profile, $fi);
533 0 undef $separator_sub; # do mk_report_separator_line just once
534 }
535  
536 111120 print OUT $line_sub->(
537 $subname,
538 "sub $subname; # $kind\n\t",
539 { subdef_info => [ $subinfo ], }, #stats_for_line
540 undef, # stats_for_file
541 $profile, $fi
542 );
543 }
544  
545 16403 print OUT $dataend;
546 16403 print OUT $self->get_param('footer', [$profile, $filestr]);
547 16403 close OUT;
548 }
549 915 ▶ print "\n" if $opts{show_progress};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"show_progress"} 0 915 915
550 }
551  
552  
553 11 ▶sub url_for_file {
Subroutine url_for_file: called CC=6 SCAR=18.1
Pod undocumented
554 11 my ($self, $file, $anchor, $level) = @_;
555 11 ▶ confess "No file specified" unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 11 11
556 11 ▶ $level ||= '';
Condition100%2/2
$level ||= ""
l!l
38
MC/DC100%1/1
$level ||= ""
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
557  
558 11 ▶ my $url = $self->{_cache}{"url_for_file,$file,$level"} ||= do {
Condition66%2/3
$self->{"_cache"}{"url_for_file,$file,$level"} ||= do { my $fi = $self->{"profile"}->fileinfo_of($file); $level = "line" if $fi->is_eval; $self->fname_for_fileinfo($fi, $level) . '.html' }
l!l&&r!l&&!r
740
MC/DC0%0/2
$self->{"_cache"}{"url_for_file,$file,$level"} ||= do { my $fi = $self->{"profile"}->fileinfo_of($file); $level = "line" if $fi->is_eval; $self->fname_for_fileinfo($fi, $level) . '.html' }
$self->{"_cache"}{"url_for_file,$file,$level"} do { my $fi = $self->{"profile"}->fileinfo_of($file); $level = "line" if $fi->is_eval; $self->fname_for_fileinfo($fi, $level) . '.html' }
Truth table66%2/3
A ||= B
A= $self->{"_cache"}{"url_for_file,$file,$level"}B= do { my $fi = $self->{"profile"}->fileinfo_of($file); $level = "line" if $fi->is_eval; $self->fname_for_fileinfo($fi, $level) . '.html' }
ABresult
1X1
011
000
559 4 my $fi = $self->{profile}->fileinfo_of($file);
560 4 ▶ $level = 'line' if $fi->is_eval;
Branch: 1 branch
BranchTrueFalseTotal
if $fi->is_eval 0 4 4
561 4 $self->fname_for_fileinfo($fi, $level) . ".html";
562 };
563  
564 11 ▶ $url .= "#$anchor" if defined $anchor;
Branch: 1 branch
BranchTrueFalseTotal
if defined $anchor 8 3 11
565 11 return $url;
566 }
567  
568 3 ▶sub href_for_file {
Subroutine href_for_file: called CC=2 SCAR=8.1
Pod undocumented
569 3 my $url = shift->url_for_file(@_);
570 3 ▶ return qq{href="$url"} if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 3 0 3
571 0 return $url;
572 }
573  
574  
575 8 ▶sub url_for_sub {
Subroutine url_for_sub: called CC=3 SCAR=11.0
Pod undocumented
576 8 my ($self, $sub, %opts) = @_;
577 8 my $profile = $self->{profile};
578  
579 8 my ($file, $fid, $first, $last, $fi) = $profile->file_line_range_of_sub($sub);
580 8 ▶ return "" unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 8 8
581 8 ▶ if (!$first) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($first) 2 6 8
582 # use sanitized subname as label for xsubs
583 # XXX must match what nytprofhtml does for xsubs
584 2 ($first = $sub) =~ s/\W/_/g;
585 }
586 8 return $self->url_for_file($fi, $first);
587 }
588  
589 4 ▶sub href_for_sub {
Subroutine href_for_sub: called CC=2 SCAR=8.1
Pod undocumented
590 4 my $url = shift->url_for_sub(@_);
591 4 ▶ return qq{href="$url"} if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 4 0 4
592 0 return $url;
593 }
594  
595  
596 351 1;
« blib/script/flamegraph.pl ↑ Summary blib/lib/Devel/NYTProf/FileInfo.pm »