/usr/lib/perl5/Devel/NYTProf/FileInfo.pm is in libdevel-nytprof-perl 5.06-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | package Devel::NYTProf::FileInfo; # fid_fileinfo
use strict;
use Carp;
use Config;
use List::Util qw(sum max);
use Devel::NYTProf::Util qw(strip_prefix_from_paths trace_level);
use Devel::NYTProf::Constants qw(
NYTP_FIDf_HAS_SRC NYTP_FIDf_SAVE_SRC NYTP_FIDf_IS_FAKE NYTP_FIDf_IS_PMC
NYTP_FIDf_IS_EVAL
NYTP_FIDi_FILENAME NYTP_FIDi_EVAL_FID NYTP_FIDi_EVAL_LINE NYTP_FIDi_FID
NYTP_FIDi_FLAGS NYTP_FIDi_FILESIZE NYTP_FIDi_FILEMTIME NYTP_FIDi_PROFILE
NYTP_FIDi_EVAL_FI NYTP_FIDi_HAS_EVALS NYTP_FIDi_SUBS_DEFINED NYTP_FIDi_SUBS_CALLED
NYTP_FIDi_elements
NYTP_SCi_CALL_COUNT NYTP_SCi_INCL_RTIME NYTP_SCi_EXCL_RTIME NYTP_SCi_RECI_RTIME
NYTP_SCi_REC_DEPTH NYTP_SCi_CALLING_SUB
NYTP_SCi_elements
);
# extra constants for private elements
use constant {
NYTP_FIDi_meta => NYTP_FIDi_elements + 1,
NYTP_FIDi_cache => NYTP_FIDi_elements + 2,
};
sub filename { shift->[NYTP_FIDi_FILENAME()] }
sub eval_fid { shift->[NYTP_FIDi_EVAL_FID()] }
sub eval_line { shift->[NYTP_FIDi_EVAL_LINE()] }
sub fid { shift->[NYTP_FIDi_FID()] }
sub size { shift->[NYTP_FIDi_FILESIZE()] }
sub mtime { shift->[NYTP_FIDi_FILEMTIME()] }
sub profile { shift->[NYTP_FIDi_PROFILE()] }
sub flags { shift->[NYTP_FIDi_FLAGS()] }
# if an eval then return fileinfo obj for the fid that executed the eval
sub eval_fi { shift->[NYTP_FIDi_EVAL_FI()] }
# is_eval is true only for simple string evals (doesn't consider NYTP_FIDf_IS_EVAL)
sub is_eval { shift->[NYTP_FIDi_EVAL_FI()] ? 1 : 0 }
sub is_fake { shift->flags & NYTP_FIDf_IS_FAKE }
sub is_file {
my $self = shift;
return not ($self->is_fake or $self->is_eval or $self->flags & NYTP_FIDf_IS_EVAL());
}
# general purpose hash - mainly a hack to help kill off Reader.pm
sub meta { shift->[NYTP_FIDi_meta()] ||= {} }
# general purpose cache
sub cache { shift->[NYTP_FIDi_cache()] ||= {} }
# array of fileinfo's for each string eval in the file
sub has_evals {
my ($self, $include_nested) = @_;
my $eval_fis = $self->[NYTP_FIDi_HAS_EVALS()]
or return;
return @$eval_fis if !$include_nested;
my @eval_fis = @$eval_fis;
# walk down tree of nested evals, adding them to @fi
for (my $i=0; my $fi = $eval_fis[$i]; ++$i) {
push @eval_fis, $fi->has_evals(0);
}
return @eval_fis;
}
sub sibling_evals {
my ($self) = @_;
my $parent_fi = $self->eval_fi
or return; # not an eval
my $eval_line = $self->eval_line;
return grep { $_->eval_line == $eval_line } $parent_fi->has_evals;
}
sub _nullify {
my $self = shift;
@$self = (); # Zap!
}
# return subs defined as list of SubInfo objects
sub subs_defined {
my ($self, $incl_nested_evals) = @_;
return map { $_->subs_defined(0) } $self, $self->has_evals(1)
if $incl_nested_evals;
return values %{ $self->[NYTP_FIDi_SUBS_DEFINED()] };
}
sub subs_defined_sorted {
my ($self, $incl_nested_evals) = @_;
return sort { $a->subname cmp $b->subname } $self->subs_defined($incl_nested_evals);
}
sub _remove_sub_defined {
my ($self, $si) = @_;
my $subname = $si->subname;
delete $self->[NYTP_FIDi_SUBS_DEFINED()]->{$subname}
or carp sprintf "_remove_sub_defined: sub %s wasn't defined in %d %s",
$subname, $self->fid, $self->filename;
}
sub _add_new_sub_defined {
my ($self, $subinfo) = @_;
my $subname = $subinfo->subname;
my $subs_defined = $self->[NYTP_FIDi_SUBS_DEFINED()] ||= {};
my $existing_si = $subs_defined->{$subname};
croak sprintf "sub %s already defined in fid %d %s",
$subname, $self->fid, $self->filename
if $existing_si;
$subs_defined->{$subname} = $subinfo;
}
=head2 sub_call_lines
$hash = $fi->sub_call_lines;
Returns a reference to a hash containing information about subroutine calls
made at individual lines within the source file.
Returns undef if no subroutine calling information is available.
The keys of the returned hash are line numbers. The values are references to
hashes with fully qualified subroutine names as keys. Each hash value is an
reference to an array containing an integer call count (how many times the sub
was called from that line of that file) and an inclusive time (how much time
was spent inside the sub when it was called from that line of that file).
For example, if the following was line 42 of a file C<foo.pl>:
++$wiggle if foo(24) == bar(42);
that line was executed once, and foo and bar were imported from pkg1, then
sub_call_lines() would return something like:
{
42 => {
'pkg1::foo' => [ 1, 0.02093 ],
'pkg1::bar' => [ 1, 0.00154 ],
},
}
=cut
sub sub_call_lines { shift->[NYTP_FIDi_SUBS_CALLED()] }
=head2 evals_by_line
# { line => { fid_of_eval_at_line => $fi, ... }, ... }
$hash = $fi->evals_by_line;
Returns a reference to a hash containing information about string evals
executed at individual lines within a source file.
The keys of the returned hash are line numbers. The values are references to
hashes with file id integers as keys and FileInfo objects as values.
=cut
sub evals_by_line {
my ($self) = @_;
# find all fids that have this fid as an eval_fid
# { line => { fid_of_eval_at_line => $fi, ... } }
my %evals_by_line;
for my $fi ($self->has_evals) {
$evals_by_line{ $fi->eval_line }->{ $fi->fid } = $fi;
}
return \%evals_by_line;
}
sub line_time_data {
my ($self, $levels) = @_;
$levels ||= [ 'line' ];
# XXX this can be optimized once the fidinfo contains directs refs to the data
my $profile = $self->profile;
my $fid = $self->fid;
for my $level (@$levels) {
my $fid_ary = $profile->get_fid_line_data($level);
return $fid_ary->[$fid] if $fid_ary && $fid_ary->[$fid];
}
return undef;
}
sub excl_time { # total exclusive time for fid
my $self = shift;
my $line_data = $self->line_time_data([qw(sub block line)])
|| return undef;
my $excl_time = 0;
for (@$line_data) {
next unless $_;
$excl_time += $_->[0];
}
return $excl_time;
}
sub sum_of_stmts_count {
my ($self, $incl_nested_evals) = @_;
return sum(map { $_->sum_of_stmts_count(0) } $self, $self->has_evals(1))
if $incl_nested_evals;
my $ref = \$self->cache->{NYTP_FIDi_sum_stmts_count};
$$ref = $self->_sum_of_line_time_data(1)
if not defined $$ref;
return $$ref;
}
sub sum_of_stmts_time {
my ($self, $incl_nested_evals) = @_;
return sum(map { $_->sum_of_stmts_time(0) } $self, $self->has_evals(1))
if $incl_nested_evals;
my $ref = \$self->cache->{NYTP_FIDi_sum_stmts_times};
$$ref = $self->_sum_of_line_time_data(0)
if not defined $$ref;
return $$ref;
}
sub _sum_of_line_time_data {
my ($self, $idx) = @_;
my $line_time_data = $self->line_time_data;
my $sum = 0;
$sum += $_->[$idx]||0 for @$line_time_data;
return $sum;
}
sub outer {
my ($self, $recurse) = @_;
my $fi = $self->eval_fi
or return;
my $prev = $self;
while ($recurse and my $eval_fi = $fi->eval_fi) {
$prev = $fi;
$fi = $eval_fi;
}
return $fi unless wantarray;
return ($fi, $prev->eval_line);
}
sub is_pmc {
return (shift->flags & NYTP_FIDf_IS_PMC());
}
sub collapse_sibling_evals {
my ($self, $survivor_fi, @donors) = @_;
my $profile = $self->profile;
die "Can't collapse_sibling_evals of non-sibling evals"
if grep { $_->eval_fid != $survivor_fi->eval_fid or
$_->eval_line != $survivor_fi->eval_line
} @donors;
my $s_ltd = $survivor_fi->line_time_data; # XXX line only
my $s_scl = $survivor_fi->sub_call_lines;
my %donor_fids;
for my $donor_fi (@donors) {
# copy data from donor to survivor_fi then delete donor
my $donor_fid = $donor_fi->fid;
$donor_fids{$donor_fid} = $donor_fi;
warn sprintf "collapse_sibling_evals: processing donor fid %d: %s\n",
$donor_fid, $donor_fi->filename
if trace_level() >= 3;
# XXX nested evals not handled yet
warn sprintf "collapse_sibling_evals: nested evals in %s not handled",
$donor_fi->filename
if $donor_fi->has_evals;
# for each sub defined in the donor,
# move the sub definition to the survivor
if (my @subs_defined = $donor_fi->subs_defined) {
for my $si (@subs_defined) {
warn sprintf " - moving from fid %d: sub %s\n",
$donor_fid, $si->subname
if trace_level() >= 4;
$si->_alter_fileinfo($donor_fi, $survivor_fi);
warn sprintf " - moving done\n"
if trace_level() >= 4;
}
}
# for each sub call made by the donor,
# move the sub calls to the survivor
# 1 => { 'main::foo' => [ 1, '1.38e-05', '1.24e-05', ..., { 'main::RUNTIME' => undef } ] }
if (my $sub_call_lines = $donor_fi->sub_call_lines) {
my %subnames_called_by_donor;
# merge details of subs called from $donor_fi
while ( my ($line, $sc_hash) = each %$sub_call_lines ) {
my $s_sc_hash = $s_scl->{$line} ||= {};
for my $subname (keys %$sc_hash ) {
my $s_sc_info = $s_sc_hash->{$subname} ||= [];
my $sc_info = delete $sc_hash->{$subname};
Devel::NYTProf::SubInfo::_merge_in_caller_info($s_sc_info, $sc_info,
tag => "line $line calls to $subname",
);
$subnames_called_by_donor{$subname}++;
}
}
%$sub_call_lines = (); # zap
# update subinfo (NYTP_SIi_CALLED_BY)
$profile->subinfo_of($_)->_alter_called_by_fileinfo($donor_fi, $survivor_fi)
for keys %subnames_called_by_donor;
}
# copy line time data
my $d_ltd = $donor_fi->line_time_data || []; # XXX line only
for my $line (0..@$d_ltd-1) {
my $d_tld_l = $d_ltd->[$line] or next;
my $s_tld_l = $s_ltd->[$line] ||= [];
$s_tld_l->[$_] += $d_tld_l->[$_] for (0..@$d_tld_l-1);
warn sprintf "%d:%d: @$s_tld_l from @$d_tld_l fid:%d\n",
$survivor_fi->fid, $line, $donor_fid if 0;
}
push @{ $survivor_fi->meta->{merged_fids} }, $donor_fid;
++$survivor_fi->meta->{merged_fids_src_varied}
if $donor_fi->src_digest ne $survivor_fi->src_digest;
$donor_fi->_nullify;
}
# remove donors from parent NYTP_FIDi_HAS_EVALS
if (my $eval_fis = $self->[NYTP_FIDi_HAS_EVALS()]) {
my %donors = map { +"$_" => 1 } @donors;
my $count = @$eval_fis;
@$eval_fis = grep { !$donors{$_} } @$eval_fis;
warn "_delete_eval mismatch"
if @$eval_fis != $count - @donors;
}
# update sawampersand_fid if it's one of the now-dead donors
if ($donor_fids{ $profile->attributes->{sawampersand_fid} || 0 }) {
$profile->attributes->{sawampersand_fid} = $survivor_fi->fid;
}
# now the fid merging is complete...
# look for any anon subs that are effectively duplicates
# (ie have the same name except for eval seqn)
# if more than one for any given name we merge them
if (my @subs_defined = $survivor_fi->subs_defined_sorted) {
# bucket anon subs by normalized name
my %newname;
for my $si (@subs_defined) {
next unless $si->is_anon;
(my $newname = $si->subname) =~ s/ \( ((?:re_)?) eval \s \d+ \) /(${1}eval 0)/xg;
push @{ $newname{$newname} }, $si;
}
while ( my ($newname, $to_merge) = each %newname ) {
my $survivor_si = shift @$to_merge;
next unless @$to_merge; # nothing to do
my $survivor_subname = $survivor_si->subname;
warn sprintf "collapse_sibling_evals: merging %d subs into %s: %s\n",
scalar @$to_merge, $survivor_subname,
join ", ", map { $_->subname } @$to_merge
if trace_level() >= 3;
for my $delete_si (@$to_merge) {
my $delete_subname = $delete_si->subname;
# for every file that called this sub, find the lines that made the calls
# and change the name to the new sub
for my $caller_fid ($delete_si->caller_fids) {
my $caller_fi = $profile->fileinfo_of($caller_fid);
# sub_call_lines ==> { line => { sub => ... } }
for my $subs_called_on_line (values %{ $caller_fi->sub_call_lines }) {
my $sc_info = delete $subs_called_on_line->{$delete_subname}
or next;
my $s_sc_info = $subs_called_on_line->{$survivor_subname} ||= [];
Devel::NYTProf::SubInfo::_merge_in_caller_info($s_sc_info, $sc_info,
tag => "collapse eval $delete_subname",
);
}
}
$survivor_si->merge_in($delete_si);
$survivor_fi->_remove_sub_defined($delete_si);
$profile->_disconnect_subinfo($delete_si);
}
}
}
warn sprintf "collapse_sibling_evals done for ".$survivor_fi->filename."\n"
if trace_level() >= 2;
return $survivor_fi;
}
# Should return the filename that the application used when loading the file
# For evals should remove the @INC portion from within the "(eval N)[$path]"
# and similarly for Class::MOP #line evals "... defined at $path".
# This is a bit of a fudge. Filename handling should be improved in the profiler.
sub filename_without_inc {
my $self = shift;
my $f = [$self->filename];
strip_prefix_from_paths([$self->profile->inc], $f,
qr/(?: ^ | \[ | \sdefined\sat\s )/x
);
return $f->[0];
}
sub abs_filename {
my $self = shift;
my $filename = $self->filename;
# strip of autosplit annotation, if any
$filename =~ s/ \(autosplit into .*//;
# if it's a .pmc then assume that's the file we want to look at
# (because the main use for .pmc's are related to perl6)
$filename .= "c" if $self->is_pmc;
# search profile @INC if filename is not absolute
my @files = ($filename);
if ($filename !~ m/^\//) {
my @inc = $self->profile->inc;
@files = map { "$_/$filename" } @inc;
}
for my $file (@files) {
return $file if -f $file;
}
# returning the still-relative filename is better than returning an undef
return $filename;
}
# has source code stored within the profile data file
sub has_savesrc {
my $self = shift;
return $self->profile->{fid_srclines}[ $self->fid ];
}
sub srclines_array {
my $self = shift;
if (my $srclines = $self->has_savesrc) {
my $copy = [ @$srclines ]; # shallow clone
shift @$copy; # line 0 not used
return $copy;
}
my $filename = $self->abs_filename;
if (open my $fh, "<", $filename) {
return [ <$fh> ];
}
return undef;
}
sub src_digest {
my $self = shift;
return $self->cache->{src_digest} ||= do {
my $srclines_array = $self->srclines_array || [];
my $src = join "\n", @$srclines_array;
# return empty string for digest if there's no src
($src) ? join ",", (
scalar @$srclines_array, # number of lines
length $src, # total length
unpack("%32C*",$src) ) # 32-bit checksum
: '';
};
}
sub normalize_for_test {
my $self = shift;
# normalize eval sequence numbers in 'file' names to 0
$self->[NYTP_FIDi_FILENAME] =~ s/ \( ((?:re_)?) eval \s \d+ \) /(${1}eval 0)/xg
if not $ENV{NYTPROF_TEST_SKIP_EVAL_NORM};
# normalize flags to avoid failures due to savesrc and perl version
$self->[NYTP_FIDi_FLAGS] &= ~(NYTP_FIDf_HAS_SRC|NYTP_FIDf_SAVE_SRC);
# '1' => { 'main::foo' => [ 1, '1.38e-05', '1.24e-05', ..., { 'main::RUNTIME' => undef } ] }
for my $subscalled (values %{ $self->sub_call_lines }) {
for my $subname (keys %$subscalled) {
my $sc = $subscalled->{$subname};
$sc->[NYTP_SCi_INCL_RTIME] =
$sc->[NYTP_SCi_EXCL_RTIME] =
$sc->[NYTP_SCi_RECI_RTIME] = 0;
if (not $ENV{NYTPROF_TEST_SKIP_EVAL_NORM}) {
# normalize eval sequence numbers in anon sub names to 0
(my $newname = $subname) =~ s/ \( ((?:re_)?) eval \s \d+ \) /(${1}eval 0)/xg;
if ($newname ne $subname) {
warn "Normalizing $subname to $newname overwrote other called-by data\n"
if $subscalled->{$newname};
$subscalled->{$newname} = delete $subscalled->{$subname};
}
}
}
}
}
sub summary {
my ($fi) = @_;
return sprintf "fid%d: %s",
$fi->fid, $fi->filename_without_inc;
}
sub dump {
my ($self, $separator, $fh, $path, $prefix, $opts) = @_;
my @values = @{$self}[
NYTP_FIDi_FILENAME, NYTP_FIDi_EVAL_FID, NYTP_FIDi_EVAL_LINE, NYTP_FIDi_FID,
NYTP_FIDi_FLAGS, NYTP_FIDi_FILESIZE, NYTP_FIDi_FILEMTIME
];
$values[0] = $self->filename_without_inc;
# also remove possible remaining perl version seen in some cpantesters
# http://www.cpantesters.org/cpan/report/bf913910-bfdd-11df-a657-c9f38a00995b
$values[0] =~ s!^$Config{version}/!!o;
printf $fh "%s[ %s ]\n", $prefix, join(" ", map { defined($_) ? $_ : 'undef' } @values);
if (not $opts->{skip_internal_details}) {
for my $si ($self->subs_defined_sorted) {
my ($fl, $ll) = ($si->first_line, $si->last_line);
defined $_ or $_ = 'undef' for ($fl, $ll);
printf $fh "%s%s%s%s%s%s-%s\n",
$prefix, 'sub', $separator,
$si->subname(' and '), $separator,
$fl, $ll;
}
# { line => { subname => [...] }, ... }
my $sub_call_lines = $self->sub_call_lines;
for my $line (sort { $a <=> $b } keys %$sub_call_lines) {
my $subs_called = $sub_call_lines->{$line};
for my $subname (sort keys %$subs_called) {
my @sc = @{$subs_called->{$subname}};
$sc[NYTP_SCi_CALLING_SUB] = join "|", sort keys %{ $sc[NYTP_SCi_CALLING_SUB] };
printf $fh "%s%s%s%s%s%s%s[ %s ]\n",
$prefix, 'call', $separator,
$line, $separator, $subname, $separator,
join(" ", map { defined($_) ? $_ : 'undef' } @sc)
}
}
# string evals, group by the line the eval is on
my %eval_lines;
for my $eval_fi ($self->has_evals(0)) {
push @{ $eval_lines{ $eval_fi->eval_line } }, $eval_fi;
}
for my $line (sort { $a <=> $b } keys %eval_lines) {
my $eval_fis = $eval_lines{$line};
my @has_evals = map { $_->has_evals(1) } @$eval_fis;
my @merged_fids = map { @{ $_->meta->{merged_fids}||[]} } @$eval_fis;
printf $fh "%s%s%s%d%s[ count %d nested %d merged %d ]\n",
$prefix, 'eval', $separator,
$eval_fis->[0]->eval_line, $separator,
scalar @$eval_fis, # count of evals executed on this line
scalar @has_evals, # count of nested evals they executed
scalar @merged_fids, # count of evals merged (collapsed) away
}
}
}
# vim: ts=8:sw=4:et
1;
|