| 1 |
#!/usr/bin/perl |
| 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 |
|
| 11 |
use warnings; |
| 12 |
use strict; |
| 13 |
|
| 14 |
use Devel::NYTProf::Core; |
| 15 |
require Devel::NYTProf::Data; |
| 16 |
|
| 17 |
our $VERSION = '6.15'; |
| 18 |
|
| 19 |
use Data::Dumper; |
| 20 |
use Getopt::Long; |
| 21 |
use Carp; |
| 22 |
|
| 23 |
GetOptions( |
| 24 |
'help|h' => \&usage, |
| 25 |
'verbose|v' => \my $opt_verbose, |
| 26 |
'calls!' => \my $opt_calls, # sum calls instead of time |
| 27 |
'debug|d' => \my $opt_debug, |
| 28 |
'stable' => \my $opt_stable, # used for testing (stability) |
| 29 |
) or usage(); |
| 30 |
|
| 31 |
$opt_verbose++ if $opt_debug; |
| 32 |
$|++ if $opt_verbose; |
| 33 |
|
| 34 |
usage() unless @ARGV; |
| 35 |
|
| 36 |
|
| 37 |
sub usage { |
| 38 |
print <<END; |
| 39 |
usage: [perl] nytprofcalls [opts] nytprof-file [...] |
| 40 |
|
| 41 |
--help, -h Print this message |
| 42 |
--verbose, -v Be more verbose |
| 43 |
|
| 44 |
This script is part of the Devel::NYTProf distribution. |
| 45 |
See https://metacpan.org/release/Devel-NYTProf for details and copyright. |
| 46 |
END |
| 47 |
exit 0; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
my $last_subid = 0; |
| 52 |
my %subname2id; |
| 53 |
|
| 54 |
my %option; |
| 55 |
my %attribute; |
| 56 |
|
| 57 |
|
| 58 |
# We're building a tree structure from a stream of "subroutine returned" events. |
| 59 |
# (We use these because the subroutine entry events don't have reliable |
| 60 |
# value for the subroutine name, and obviously don't have timings.) |
| 61 |
# |
| 62 |
# Building a call tree from return events is a little tricky because they don't |
| 63 |
# appear in natural order. The code can return from a call at any depth |
| 64 |
# deeper than the last seen depth. |
| 65 |
# |
| 66 |
my $root = {}; |
| 67 |
my @stack = ($root); |
| 68 |
my $total_in = 0; |
| 69 |
|
| 70 |
|
| 71 |
my $callbacks = { |
| 72 |
OPTION => sub { my (undef, $k, $v) = @_; $option{$k} = $v }, |
| 73 |
ATTRIBUTE => sub { my (undef, $k, $v) = @_; $attribute{$k} = $v }, |
| 74 |
}; |
| 75 |
$callbacks->{SUB_ENTRY} = \&on_sub_entry_log if $opt_verbose; |
| 76 |
$callbacks->{SUB_RETURN} = \&on_sub_return_build_call_stack; |
| 77 |
$callbacks->{all_loaded} = sub { |
| 78 |
output_call_path_hash( extract_call_path_hash($root) ); |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
foreach my $input (@ARGV) { |
| 83 |
warn "Reading $input...\n" if $opt_verbose; |
| 84 |
Devel::NYTProf::Data->new({ |
| 85 |
filename => $input, |
| 86 |
quiet => 1, |
| 87 |
callback => $callbacks |
| 88 |
}); |
| 89 |
} |
| 90 |
$callbacks->{all_loaded}->(); |
| 91 |
|
| 92 |
|
| 93 |
exit 0; |
| 94 |
|
| 95 |
|
| 96 |
sub on_sub_entry_log { |
| 97 |
my (undef, $fid, $line) = @_; |
| 98 |
warn "> at $fid:$line\n"; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
sub on_sub_return_build_call_stack { |
| 103 |
# $retn_depth is the call stack depth of the sub call we're returning from |
| 104 |
my (undef, $retn_depth, undef, $excl_time, $subname) = @_; |
| 105 |
|
| 106 |
warn sprintf "< %2d %-10s %s (stack %d)\n", $retn_depth, $subname, $excl_time, scalar @stack |
| 107 |
if $opt_verbose; |
| 108 |
|
| 109 |
my $v = ($opt_calls) ? 1 : $excl_time; |
| 110 |
$total_in += $v; |
| 111 |
|
| 112 |
# normalize and merge sibling string evals by setting eval seqn to 0 |
| 113 |
$subname =~ s/\( (\w*eval)\s\d+ \) (?= \[ .+? :\d+ \] )/($1 0)/gx; |
| 114 |
# assign an id to the subname for memory efficiency |
| 115 |
my $subid = $subname2id{$subname} ||= ++$last_subid; |
| 116 |
|
| 117 |
# Either... |
| 118 |
# a) we're returning from some sub deeper than the current stack |
| 119 |
# in which case we push unnamed sub calls ("0") onto the stack |
| 120 |
# till we get to the right depth, then fall through to: |
| 121 |
# b) we're returning from the sub on top of the stack. |
| 122 |
|
| 123 |
while (@stack <= $retn_depth) { # build out the tree if needed |
| 124 |
my $crnt_node = $stack[-1]; |
| 125 |
die "panic" if $crnt_node->{0}; |
| 126 |
push @stack, ($crnt_node->{0} = {}); |
| 127 |
} |
| 128 |
|
| 129 |
# top of stack: sub we're returning from |
| 130 |
# next on stack: sub that was the caller |
| 131 |
my $sub_return = pop @stack; |
| 132 |
my $sub_caller = $stack[-1] || die "panic"; |
| 133 |
|
| 134 |
die "panic" unless $sub_return == $sub_caller->{0}; |
| 135 |
delete $sub_caller->{0} or die "panic"; # == $sub_return |
| 136 |
|
| 137 |
# { |
| 138 |
# 0 - as-yet un-returned subs |
| 139 |
# 'v' - cumulative excl_time in this sub |
| 140 |
# $subid1 => {...} # calls to $subid1 made by this sub |
| 141 |
# $subid2 => {...} |
| 142 |
# } |
| 143 |
|
| 144 |
$sub_return->{v} += $v; |
| 145 |
_merge_sub_return_into_caller($sub_caller->{$subid} ||= {}, $sub_return); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
# build hash of call paths ("subid;subid;subid" => value) from the call tree |
| 150 |
sub extract_call_path_hash { |
| 151 |
my ($root) = @_; |
| 152 |
|
| 153 |
my %subid_call_path_hash; |
| 154 |
visit_nodes_depth_first($root, [], sub { |
| 155 |
my ($node, $path) = @_; |
| 156 |
$subid_call_path_hash{ join(";", @$path) } += $node->{v} |
| 157 |
if @$path; |
| 158 |
%$node = (); # reclaim memory as we go |
| 159 |
}); |
| 160 |
return \%subid_call_path_hash; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
sub output_call_path_hash { |
| 165 |
my ($subid_call_path_hash) = @_; |
| 166 |
|
| 167 |
# ensure subnames don't contain ";" or " " |
| 168 |
tr/; /??/ for values %subname2id; |
| 169 |
my %subid2name = reverse %subname2id; |
| 170 |
|
| 171 |
# output the totals without scaling, so they're in ticks_per_sec units |
| 172 |
my $val_scale_factor = 1; # ($opt_calls) ? 1 : 1_000_000 / $attribute{ticks_per_sec}; |
| 173 |
my $val_format = ($opt_calls || $val_scale_factor==1) ? "%s %d\n" : "%s %.1f\n"; |
| 174 |
my $total_out = 0; |
| 175 |
|
| 176 |
# output the subid_call_path_hash hash using subroutine names |
| 177 |
my @keys = keys %$subid_call_path_hash; |
| 178 |
@keys = sort @keys if $opt_stable; |
| 179 |
for my $subidpath (@keys) { |
| 180 |
my @path = map { $subid2name{$_} } split ";", $subidpath; |
| 181 |
my $path = join(";", @path); |
| 182 |
my $v = $subid_call_path_hash->{$subidpath}; |
| 183 |
printf $val_format, join(";", @path), $v * $val_scale_factor; |
| 184 |
$total_out += $v; |
| 185 |
} |
| 186 |
|
| 187 |
warn "nytprofcalls inconsistency: total in $total_in doesn't match total out $total_out\n" |
| 188 |
if $total_in != $total_out; |
| 189 |
|
| 190 |
warn sprintf "Done. Total $total_in\n" |
| 191 |
if $opt_verbose; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
sub _merge_sub_return_into_caller { |
| 196 |
my ($dest, $new, $recurse) = @_; |
| 197 |
$dest->{v} += delete $new->{v}; |
| 198 |
while ( my ($new_called_subid, $new_called_node) = each %$new ) { |
| 199 |
if ($dest->{$new_called_subid}) { |
| 200 |
_merge_sub_return_into_caller($dest->{$new_called_subid}, $new_called_node); |
| 201 |
} |
| 202 |
else { |
| 203 |
$dest->{$new_called_subid} = $new_called_node; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
sub visit_nodes_depth_first { # depth first |
| 210 |
my $node = shift; |
| 211 |
my $path = shift; |
| 212 |
my $sub = shift; |
| 213 |
|
| 214 |
warn "visit_node: @{[ %$node ]}\n" if $opt_debug; |
| 215 |
|
| 216 |
push @$path, undef; |
| 217 |
while ( my ($subid, $childnode) = each %$node) { |
| 218 |
next if $subid eq 'v'; |
| 219 |
die "panic" if $subid eq '0'; |
| 220 |
|
| 221 |
$path->[-1] = $subid; |
| 222 |
warn "node @$path: @{[ %$childnode ]}\n" if $opt_debug; |
| 223 |
visit_nodes_depth_first($childnode, $path, $sub); |
| 224 |
} |
| 225 |
pop @$path; |
| 226 |
|
| 227 |
$sub->($node, $path); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
__END__ |
| 232 |
|
| 233 |
=head1 NAME |
| 234 |
|
| 235 |
nytprofcalls - experimental |
| 236 |
|
| 237 |
=cut |
| 238 |
# vim:ts=8:sw=4:et |