Debian Codegraph @

bin/nytprofmerge

raw
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::FileHandle;
16 require Devel::NYTProf::Data;
17 use List::Util qw(min sum);
18
19 our $VERSION = '6.15';
20
21 if ($VERSION != $Devel::NYTProf::Core::VERSION) {
22 die "$0 version '$VERSION' doesn't match version '$Devel::NYTProf::Core::VERSION' of $INC{'Devel/NYTProf/Core.pm'}\n";
23 }
24
25 use Getopt::Long;
26 use Carp;
27
28 my $opt_out = 'nytprof-merged.out';
29
30 GetOptions(
31 'out|o=s' => \$opt_out,
32 'help|h' => \&usage,
33 'verbose|v' => \my $opt_verbose,
34 ) or usage();
35
36 usage() unless @ARGV;
37
38 print "Opening output $opt_out\n" if $opt_verbose;
39 my $out = Devel::NYTProf::FileHandle::open($opt_out, "wb")
40 or die "Error opening $opt_out: $!\n";
41
42 my $sub_is_anon_in_eval = qr/__ANON__\[\(eval/;
43
44 my $next_fid = 1;
45 my %fid_to_file;
46 my %file_to_fid;
47 my %fids = (0 => 0);
48 # Similar, but with all evals folded too
49 my %fids_folded = (0 => 0);
50
51 my %eval_to_fid;
52
53 my $version;
54 my %seen_subs;
55 my %callers;
56 my %map_range;
57
58 my @pending_fids;
59 my %pending_subs;
60
61 sub _write_time_block_or_line {
62 my ($tag, $ticks, $fid, $line, $block_line, $sub_line) = @_;
63
64 confess("No mapping for $fid") unless defined $fids{$fid};
65 $fid = $fids{$fid};
66 # Is this a subroutine (re)defined in an eval?
67 my $mapped_fid = $map_range{$fid}[$line];
68 $fid = $mapped_fid if defined $mapped_fid;
69
70 # XXX overflow isn't passed in or through
71 if ($tag eq 'TIME_LINE') {
72 $out->write_time_line($ticks, 0, $fid, $line);
73 } else {
74 $out->write_time_block($ticks, 0, $fid, $line, $block_line, $sub_line);
75 }
76 }
77
78 # Complain loudly if any of these attributes differ between profiles
79 my %attr_should_be_identical = map {$_, 1} qw(
80 PL_perldb clock_id nv_size perl_version ticks_per_sec xs_version
81 );
82
83
84 # Effectively, these are global variables. Sorry.
85 our $input;
86 our %attributes;
87 our %options;
88 our $deflating;
89
90 my %dispatcher =
91 (
92 '' => sub {
93 die "Unknown tag '$_[0]' in $input\n";
94 },
95 VERSION => sub {
96 my (undef, $major, $minor) = @_;
97 my $this_version = "$major $minor";
98 if($version) {
99 die "Incompatible version '$this_version' in $input, expected '$version'"
100 unless $this_version eq $version;
101 } else {
102 $version = $this_version;
103 $out->write_header($major, $minor);
104 }
105 },
106 COMMENT => sub {
107 my (undef, $text) = @_;
108 chomp $text; # Arguably this is a bug in the callback interface.
109 # This isn't true unless we enable compression ourselves, and if we
110 # do that, the low level code will write out a correct comment
111 # automatically.
112 return if $text =~ /\ACompressed at level \d with zlib [0-9.]+\z/;
113 $out->write_comment($text)
114 },
115
116 ATTRIBUTE => sub {
117 my (undef, $key, $value) = @_;
118 if ($attr_should_be_identical{$key}) {
119 if (exists $attributes{$key}) {
120 if ($attributes{$key} ne $value) {
121 warn("Attribute '$key' has value '$value' in $input which differs from the previous value '$attributes{$key}'; this implies inconsistent profiles and thus garbage results\n");
122 }
123 } else {
124 $attributes{$key} = $value;
125 $out->write_attribute($key, $value);
126 }
127 } else {
128 push @{$attributes{$key}}, $value;
129 }
130 },
131
132 OPTION => sub {
133 my (undef, $key, $value) = @_;
134
135 if (exists $options{$key}) {
136 if ($options{$key} ne $value) {
137 warn("Option '$key' has value '$value' in $input which differs from the previous value '$options{$key}'; this implies inconsistent profiles and thus garbage results\n");
138 }
139 } else {
140 $options{$key} = $value;
141 $out->write_option($key, $value);
142 }
143 },
144
145 START_DEFLATE => sub {
146 if (!$deflating && $out->can('start_deflate_write_tag_comment')) {
147 $out->start_deflate_write_tag_comment;
148 ++$deflating;
149 }
150 },
151
152 PID_START => sub {
153 my (undef, $pid, $parent, $time) = @_;
154 $out->write_process_start($pid, $parent, $time);
155 },
156 PID_END => sub {
157 my (undef, $pid, $time) = @_;
158 $out->write_process_end($pid, $time);
159 },
160
161 NEW_FID => sub {
162 my (undef, $fid, $eval_fid, $eval_line, $flags, $size, $mtime, $name) = @_;
163
164 return unless $pending_fids[$fid];
165 my ($new_fid, $new_eval_fid) = @{$pending_fids[$fid]};
166
167 $out->write_new_fid($new_fid, $new_eval_fid, $eval_line, $flags,
168 $size, $mtime, $name);
169 },
170 TIME_BLOCK => \&_write_time_block_or_line,
171 TIME_LINE => \&_write_time_block_or_line,
172
173 DISCOUNT => sub {
174 $out->write_discount();
175 },
176 SUB_INFO => sub {
177 my (undef, $fid, $first_line, $last_line, $name) = @_;
178
179 my $output_fid = $pending_subs{"$fid,$first_line,$last_line,$name"};
180 return unless defined $output_fid;
181
182 $out->write_sub_info($output_fid, $name, $first_line, $last_line);
183 },
184 SUB_CALLERS => sub {
185 my (undef, $fid, $line, $count, $incl_time, $excl_time, $reci_time, $rec_depth, $called, $caller) = @_;
186 confess("No mapping for $fid") unless defined $fids{$fid};
187 $fid = $fids{$fid};
188 my $mapped_fid = $map_range{$fid}[$line];
189 $fid = $mapped_fid if defined $mapped_fid;
190
191 if ($callers{"$fid,$line"}{$called}{$caller}) {
192 my $sum = $callers{"$fid,$line"}{$called}{$caller};
193 $sum->{count} += $count;
194 $sum->{incl} += $incl_time;
195 $sum->{excl} += $excl_time;
196 $sum->{reci} += $reci_time;
197 $sum->{depth} = $rec_depth if $rec_depth > $sum->{depth};
198 } else {
199 # New;
200 $callers{"$fid,$line"}{$called}{$caller} =
201 {
202 depth => $rec_depth,
203 count => $count,
204 incl => $incl_time,
205 excl => $excl_time,
206 reci => $reci_time,
207 };
208 }
209 },
210
211 SUB_ENTRY => sub {
212 my (undef, $fid, $line) = @_;
213 confess("No mapping for $fid") unless defined $fids{$fid};
214 $fid = $fids{$fid};
215 $out->write_call_entry($fid, $line);
216 },
217 SUB_RETURN => sub {
218 my (undef, $retn_depth, $incl_time, $excl_time, $subname) = @_;
219 $out->write_call_return($retn_depth, $subname, $incl_time, $excl_time);
220 },
221
222 SRC_LINE => sub {
223 my (undef, $fid, $line, $text) = @_;
224 confess("No mapping for $fid") unless defined $fids{$fid};
225 $fid = $fids{$fid};
226 # Is this a subroutine (re)defined in an eval?
227 my $mapped_fid = $map_range{$fid}[$line];
228 $fid = $mapped_fid if defined $mapped_fid;
229 $out->write_src_line($fid, $line, $text);
230 },
231 );
232
233 foreach $input (@ARGV) {
234 print "Reading $input...\n" if $opt_verbose;
235 @pending_fids = ();
236 %pending_subs = ();
237
238 # first pass
239 Devel::NYTProf::Data->new({filename => $input, callback => {
240 NEW_FID => sub {
241 my (undef, $fid, $eval_fid, $eval_line, $flags, $size, $mtime, $name) = @_;
242 my ($new_fid, $new_eval_fid);
243 if($eval_fid) {
244 # Generally, treat every eval as distinct, even at the same location
245 $new_eval_fid = $fids{$eval_fid};
246
247 # Sanity check. Should never happen except that
248 # if $eval_fid < $fid then this is a known problem
249 # with evals fids getting profiled/output before the parent fid
250 warn("unknown eval_fid $eval_fid in $input fid $fid\n")
251 unless defined $new_eval_fid;
252
253 $new_fid = $next_fid++;
254 $fids{$fid} = $new_fid;
255
256 # But also track the first fid to be allocated at that line of the eval
257 my $folded_fid = $fids_folded{$eval_fid};
258 Carp::cluck("unknown folded eval_fid $eval_fid in $input fid $fid")
259 unless defined $folded_fid;
260
261 my $corresponding_eval = $eval_to_fid{"$folded_fid,$eval_line"};
262 if (!defined $corresponding_eval) {
263 # Not seen a fid generated in an eval at this location before
264 $eval_to_fid{"$folded_fid,$eval_line"} = $new_fid;
265 $fids_folded{$fid} = $new_fid;
266 } else {
267 $fids_folded{$fid} = $corresponding_eval;
268 }
269 } else {
270 $new_eval_fid = $eval_fid;
271 $new_fid = $file_to_fid{$name};
272
273 if(defined $new_fid) {
274 $fids_folded{$fid} = $fids{$fid} = $new_fid;
275 return;
276 }
277
278 $new_fid = $next_fid++;
279 $fids_folded{$fid} = $fids{$fid} = $new_fid;
280 $file_to_fid{$name} = $new_fid;
281 }
282 $fid_to_file{$new_fid} = $name;
283 $pending_fids[$fid] = [$new_fid, $new_eval_fid];
284 },
285 SUB_INFO => sub {
286 my (undef, $fid, $first_line, $last_line, $name) = @_;
287 my $output_fid;
288 if ($name =~ $sub_is_anon_in_eval) {
289 confess("No mapping for $fid") unless defined $fids{$fid};
290 $output_fid = $fids{$fid};
291 $seen_subs{"$output_fid,$name"} ||= "$first_line,$last_line";
292 } else {
293 confess("No mapping for $fid") unless defined $fids_folded{$fid};
294 my $folded = $fids_folded{$fid};
295 my $seen = $seen_subs{"$folded,$name"};
296 if (defined $seen && $seen ne "$first_line,$last_line") {
297 # Warn that we are not folding
298
299 # Carry on, and output a SUB_INFO block for this fid
300 $output_fid = $fid;
301 } else {
302 # This subroutine has be (re)defined in two distinct
303 # evals, but appears to be identical. So for this lines
304 # range in the second eval, treat profiling data as if it
305 # came from the fid of the first eval, so that all calls
306 # to the sub are collated.
307
308 # Have to use the mapped fid as the key to this hash, as
309 # only the mapped fids are unique
310 my $mapped_fid = $fids{$fid};
311 $map_range{$mapped_fid}[$_] = $folded
312 for $first_line .. $last_line;
313
314 return if defined $seen;
315
316 $seen_subs{"$folded,$name"} = "$first_line,$last_line";
317 $output_fid = $folded;
318 }
319 }
320 $pending_subs{"$fid,$first_line,$last_line,$name"} = $output_fid;
321 }
322 }});
323
324 # second pass
325 print "Re-reading $input...\n" if $opt_verbose;
326 Devel::NYTProf::Data->new({filename => $input, callback => \%dispatcher});
327 }
328
329 print "Finalizing...\n" if $opt_verbose;
330 # Deterministic order is useful for testing.
331 foreach my $fid_line (sort keys %callers) {
332 my ($fid, $line) = split ',', $fid_line;
333 foreach my $called (sort keys %{$callers{$fid_line}}) {
334 foreach my $caller (sort keys %{$callers{$fid_line}{$called}}) {
335 my $sum = $callers{$fid_line}{$called}{$caller};
336 $out->write_sub_callers($fid, $line, $caller, $sum->{count},
337 @{$sum}{qw(incl excl reci)},
338 $sum->{depth}, $called);
339 }
340 }
341 }
342
343 foreach my $key (sort grep {!$attr_should_be_identical{$_}} keys %attributes) {
344 my @values = @{$attributes{$key}};
345
346 if ($key eq 'basetime') {
347 my $value = min(@values);
348 $out->write_attribute($key, $value);
349 }
350 elsif ($key eq 'application') { # "merge" the application names
351 my %counts;
352 $counts{$_}++ foreach @values;
353 my @grouped;
354 foreach my $prog (sort keys %counts) {
355 my $count = $counts{$prog};
356 push @grouped, $prog;
357 $grouped[-1] .= " ($count runs)" if $count > 1;
358 }
359 my $last = pop @grouped;
360 my $value = @grouped ? join (', ', @grouped) . " and $last" : $last;
361 $out->write_attribute($key, $value);
362 }
363 elsif ($key eq 'cumulative_overhead_ticks') { # sum cumulative_overhead_ticks
364 $out->write_attribute($key, sum(@values));
365 }
366 elsif ($key =~ /^sawampersand_\w+$/) { # sawampersand_fid/_line
367 # we just pass through the first value seen
368 $out->write_attribute($key, $values[0]);
369 }
370 else {
371 warn sprintf "Attribute %s has %d distinct values passed through unmerged\n",
372 $key, scalar @values
373 if @values > 1;
374 $out->write_attribute($key, $_) foreach @values;
375 }
376 }
377
378
379 print "Done.\n" if $opt_verbose;
380 exit 0;
381
382 sub usage {
383 print <<END;
384 usage: [perl] nytprofmerge [opts] nytprof-file [...]
385
386 --out <file>, -o <file> Name of output file [default: $opt_out]
387 --help, -h Print this message
388 --verbose, -v Be more verbose
389
390 This script is part of the Devel::NYTProf distribution.
391 See https://metacpan.org/release/Devel-NYTProf for details and copyright.
392 END
393 exit 0;
394 }
395
396 __END__
397
398 =head1 NAME
399
400 nytprofmerge - Reads multiple NYTProf profiles and outputs a merged one
401
402 =head1 SYNOPSIS
403
404 $ nytprofmerge --out=nytprof-merged.out nytprof.out.*
405
406 $ nytprofmerge nytprof.out.*
407
408 =head1 DESCRIPTION
409
410 Reads multiple profile data files generated by Devel::NYTProf and writes out
411 a new profile data file containing data merged from the original files.
412
413 C<nytprofmerge> is likely to produce garbage if the input profiles
414 aren't all profiles of I<exactly> the same software.
415
416 C<nytprofmerge> is new and somewhat experimental. If it produces unexpected
417 results please produce a I<small> test case that demonstrates the problem and
418 let us know at L<http://groups.google.com/group/develnytprof-dev> - thanks!
419
420 =cut
421 # vim:ts=8:sw=4:et