| 1 | #!/usr/local/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 | 348 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 348 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 348 | ▶ | use Devel::NYTProf::Core; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 348 | require Devel::NYTProf::Data; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 348 | our $VERSION = '6.16'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 348 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 348 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 348 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 348 | ▶ | GetOptions( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 348 | ▶ | $opt_verbose++ if $opt_debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 348 | ▶ | $|++ if $opt_verbose; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 348 | ▶ | usage() unless @ARGV; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub usage { | ||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | 0 | 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 | 0 | exit 0; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 348 | my $last_subid = 0; | |||||||||||||||||||||||||||
| 52 | 348 | my %subname2id; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | my %option; | ||||||||||||||||||||||||||||
| 55 | 348 | 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 | 348 | my $root = {}; | |||||||||||||||||||||||||||
| 67 | 348 | my @stack = ($root); | |||||||||||||||||||||||||||
| 68 | 348 | my $total_in = 0; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | my $callbacks = { | ||||||||||||||||||||||||||||
| 72 | 6264 | ▶ | OPTION => sub { my (undef, $k, $v) = @_; $option{$k} = $v }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 3132 | ▶ | ATTRIBUTE => sub { my (undef, $k, $v) = @_; $attribute{$k} = $v }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 348 | }; | |||||||||||||||||||||||||||
| 75 | 348 | ▶ | $callbacks->{SUB_ENTRY} = \&on_sub_entry_log if $opt_verbose; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 348 | $callbacks->{SUB_RETURN} = \&on_sub_return_build_call_stack; | |||||||||||||||||||||||||||
| 77 | 348 | ▶ | $callbacks->{all_loaded} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 348 | output_call_path_hash( extract_call_path_hash($root) ); | |||||||||||||||||||||||||||
| 79 | 348 | }; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 348 | foreach my $input (@ARGV) { | |||||||||||||||||||||||||||
| 83 | 348 | ▶ | warn "Reading $input...\n" if $opt_verbose; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 348 | Devel::NYTProf::Data->new({ | |||||||||||||||||||||||||||
| 85 | filename => $input, | ||||||||||||||||||||||||||||
| 86 | quiet => 1, | ||||||||||||||||||||||||||||
| 87 | callback => $callbacks | ||||||||||||||||||||||||||||
| 88 | }); | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 348 | $callbacks->{all_loaded}->(); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 348 | exit 0; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub on_sub_entry_log { | ||||||||||||||||||||||||||
|
Subroutine
on_sub_entry_log: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 97 | 0 | my (undef, $fid, $line) = @_; | |||||||||||||||||||||||||||
| 98 | 0 | warn "> at $fid:$line\n"; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 70080551 | ▶ | sub on_sub_return_build_call_stack { | ||||||||||||||||||||||||||
|
Subroutine
on_sub_return_build_call_stack: called CC=10 SCAR=23.8
| |||||||||||||||||||||||||||||
| 103 | # $retn_depth is the call stack depth of the sub call we're returning from | ||||||||||||||||||||||||||||
| 104 | 70080551 | my (undef, $retn_depth, undef, $excl_time, $subname) = @_; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 70080551 | ▶ | warn sprintf "< %2d %-10s %s (stack %d)\n", $retn_depth, $subname, $excl_time, scalar @stack | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | if $opt_verbose; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 70080551 | ▶ | my $v = ($opt_calls) ? 1 : $excl_time; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 70080551 | $total_in += $v; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # normalize and merge sibling string evals by setting eval seqn to 0 | ||||||||||||||||||||||||||||
| 113 | 70080551 | $subname =~ s/\( (\w*eval)\s\d+ \) (?= \[ .+? :\d+ \] )/($1 0)/gx; | |||||||||||||||||||||||||||
| 114 | # assign an id to the subname for memory efficiency | ||||||||||||||||||||||||||||
| 115 | 70080551 | ▶ | my $subid = $subname2id{$subname} ||= ++$last_subid; | ||||||||||||||||||||||||||
|
Condition66%2/3
$subname2id{$subname} ||= ++$last_subid
MC/DC0%0/2
$subname2id{$subname} ||= ++$last_subid
$subname2id{$subname}
++$last_subid
Truth table66%2/3
A ||= B
A= $subname2id{$subname}B= ++$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 | 70080551 | ▶ | while (@stack <= $retn_depth) { # build out the tree if needed | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 70080551 | my $crnt_node = $stack[-1]; | |||||||||||||||||||||||||||
| 125 | 70080551 | ▶ | die "panic" if $crnt_node->{0}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 70080551 | 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 | 70080551 | my $sub_return = pop @stack; | |||||||||||||||||||||||||||
| 132 | 70080551 | ▶ | my $sub_caller = $stack[-1] || die "panic"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$stack[-1] || die('panic')
MC/DC0%0/1
$stack[-1] || die('panic')
$stack[-1]
Truth table50%1/2
A
A= $stack[-1]
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 70080551 | ▶ | die "panic" unless $sub_return == $sub_caller->{0}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 70080551 | ▶ | delete $sub_caller->{0} or die "panic"; # == $sub_return | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 70080551 | $sub_return->{v} += $v; | |||||||||||||||||||||||||||
| 145 | 70080551 | ▶ | _merge_sub_return_into_caller($sub_caller->{$subid} ||= {}, $sub_return); | ||||||||||||||||||||||||||
|
Condition100%2/2
$sub_caller->{$subid} ||= {}
MC/DC100%1/1
$sub_caller->{$subid} ||= {}
$sub_caller->{$subid}
Truth table100%2/2
A
A= $sub_caller->{$subid}
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # build hash of call paths ("subid;subid;subid" => value) from the call tree | ||||||||||||||||||||||||||||
| 150 | 348 | ▶ | sub extract_call_path_hash { | ||||||||||||||||||||||||||
|
Subroutine
extract_call_path_hash: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 151 | 348 | my ($root) = @_; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 348 | my %subid_call_path_hash; | |||||||||||||||||||||||||||
| 154 | 346768 | ▶ | visit_nodes_depth_first($root, [], sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 155 | 346768 | my ($node, $path) = @_; | |||||||||||||||||||||||||||
| 156 | $subid_call_path_hash{ join(";", @$path) } += $node->{v} | ||||||||||||||||||||||||||||
| 157 | 346768 | ▶ | if @$path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 346768 | %$node = (); # reclaim memory as we go | |||||||||||||||||||||||||||
| 159 | 348 | }); | |||||||||||||||||||||||||||
| 160 | 348 | return \%subid_call_path_hash; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 348 | ▶ | sub output_call_path_hash { | ||||||||||||||||||||||||||
|
Subroutine
output_call_path_hash: called CC=8 SCAR=21.6
| |||||||||||||||||||||||||||||
| 165 | 348 | my ($subid_call_path_hash) = @_; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # ensure subnames don't contain ";" or " " | ||||||||||||||||||||||||||||
| 168 | 348 | tr/; /??/ for values %subname2id; | |||||||||||||||||||||||||||
| 169 | 348 | my %subid2name = reverse %subname2id; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # output the totals without scaling, so they're in ticks_per_sec units | ||||||||||||||||||||||||||||
| 172 | 348 | my $val_scale_factor = 1; # ($opt_calls) ? 1 : 1_000_000 / $attribute{ticks_per_sec}; | |||||||||||||||||||||||||||
| 173 | 348 | ▶ | my $val_format = ($opt_calls || $val_scale_factor==1) ? "%s %d\n" : "%s %.1f\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opt_calls or $val_scale_factor == 1
MC/DC0%0/2
$opt_calls or $val_scale_factor == 1
$opt_calls
$val_scale_factor == 1
Truth table33%1/3
A or B
A= $opt_callsB= $val_scale_factor == 1
| |||||||||||||||||||||||||||||
| 174 | 348 | my $total_out = 0; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # output the subid_call_path_hash hash using subroutine names | ||||||||||||||||||||||||||||
| 177 | 348 | my @keys = keys %$subid_call_path_hash; | |||||||||||||||||||||||||||
| 178 | 348 | ▶ | @keys = sort @keys if $opt_stable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 348 | for my $subidpath (@keys) { | |||||||||||||||||||||||||||
| 180 | 346420 | my @path = map { $subid2name{$_} } split ";", $subidpath; | |||||||||||||||||||||||||||
| 181 | 346420 | my $path = join(";", @path); | |||||||||||||||||||||||||||
| 182 | 346420 | my $v = $subid_call_path_hash->{$subidpath}; | |||||||||||||||||||||||||||
| 183 | 346420 | printf $val_format, join(";", @path), $v * $val_scale_factor; | |||||||||||||||||||||||||||
| 184 | 346420 | $total_out += $v; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 348 | ▶ | warn "nytprofcalls inconsistency: total in $total_in doesn't match total out $total_out\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | if $total_in != $total_out; | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 348 | ▶ | warn sprintf "Done. Total $total_in\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | if $opt_verbose; | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 99521610 | ▶ | sub _merge_sub_return_into_caller { | ||||||||||||||||||||||||||
|
Subroutine
_merge_sub_return_into_caller: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 196 | 99521610 | my ($dest, $new, $recurse) = @_; | |||||||||||||||||||||||||||
| 197 | 99521610 | $dest->{v} += delete $new->{v}; | |||||||||||||||||||||||||||
| 198 | 99521610 | ▶ | while ( my ($new_called_subid, $new_called_node) = each %$new ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 44890627 | ▶ | if ($dest->{$new_called_subid}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 29441059 | _merge_sub_return_into_caller($dest->{$new_called_subid}, $new_called_node); | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | else { | ||||||||||||||||||||||||||||
| 203 | 15449568 | $dest->{$new_called_subid} = $new_called_node; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 346768 | ▶ | sub visit_nodes_depth_first { # depth first | ||||||||||||||||||||||||||
|
Subroutine
visit_nodes_depth_first: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||
| 210 | 346768 | my $node = shift; | |||||||||||||||||||||||||||
| 211 | 346768 | my $path = shift; | |||||||||||||||||||||||||||
| 212 | 346768 | my $sub = shift; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | ▶ | warn "visit_node: @{[ %$node ]}\n" if $opt_debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 346768 | push @$path, undef; | |||||||||||||||||||||||||||
| 217 | 346768 | ▶ | while ( my ($subid, $childnode) = each %$node) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 692840 | ▶ | next if $subid eq 'v'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 346420 | ▶ | die "panic" if $subid eq '0'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 346420 | $path->[-1] = $subid; | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | warn "node @$path: @{[ %$childnode ]}\n" if $opt_debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 346420 | visit_nodes_depth_first($childnode, $path, $sub); | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | 346768 | pop @$path; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 346768 | $sub->($node, $path); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | __END__ | ||||||||||||||||||||||||||||