-
Notifications
You must be signed in to change notification settings - Fork 138
/
pprof2cg.pl
executable file
·401 lines (309 loc) · 13.5 KB
/
pprof2cg.pl
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
#! perl
# Copyright (C) 2009, Parrot Foundation.
use strict;
use warnings;
=head1 NAME
tools/dev/pprof2cg.pl
=head1 DESCRIPTION
Convert the output of Parrot's profiling runcore to a Callgrind-compatible
format.
=head1 SYNOPSIS
perl tools/dev/pprof2cg.pl parrot.pprof.1234
=head1 USAGE
Generate a profile by passing C<-Rprofiling> to parrot, for example C<./parrot
-Rprofiling perl6.pbc hello.p6>. Once execution completes, C<parrot> will
print a message specifying the location of the parrot profile (pprof). The
profile will be named parrot.pprof.XXXX, where XXXX is the PID of the parrot
process unless another name is specified by the B<PARROT_PROFILING_OUTPUT>
environment variable.
To generate a Callgrind-compatible profile, run this script with the pprof
filename as the first argument. The output file usable by kcachegrind will be
in parrot.out.XXXX, where XXXX again is the PID of the original parrot process.
=head1 ENVIRONMENT VARIABLES
=head2 PARROT_PROFILING_OUTPUT
If the environment variable PARROT_PROFILING_OUTPUT is set, the profiling
runcore will attempt to use its value as the profile filename. Note that it
does not check whether the file already exists and will happily overwrite
existing files.
=cut
main(@ARGV);
=head1 INTERNAL DATA STRUCTURES
=over 4
=item notes
Parrot's execution model is built on continuation-passing style and does not
precisely fit the straightforward function-based format that
Callgrind-compatible tools expect. For this reason, the profiling runcore
captures information about context switches (CS lines in the pprof file) and
pprof2cg.pl maintains a context stack that functions similarly to a typical
call stack. pprof2cg.pl then maps these context switches as if they were
function calls and returns. See C<$call_stack> for more information.
=item C<$call_stack>
Variables which are named C<$call_stack> hold a reference to an array of hashes
which contain information about the currently active contexts. When collecting
timing information about an op, it is necessary to add that information to all
function calls on the stack because Callgrind-compatible tools expect the cost
of a function call to include the cost of all calls made by that function, etc.
When a context switch is detected, C<process_line> looks at the context stack
to determine if the context switch looks like a function call (if the context
hasn't been seen before) or a return (if the context is somewhere on the
stack). There are some other cases that the code handles, but these can be
ignored for now in the interest of simplicity. If the context has been seen,
C<process_line> shifts contexts off the stack until it finds the context that
has been switched to. When C<process_line> detects a new context, it adds a
fake op representing a function call to C<$stats> and unshifts a new context
onto the stack.
Each element of C<@$call_stack> contains the information needed to uniquely
identify the site of the original context switch.
=item C<$stats>
Variables which are named C<$stats> contain a reference to a deeply nested
HoHoH.. which contains all information gathered about a profiled PIR program.
The nested hashes and arrays refer to the file, namespace, line of source code
and op number, respectively. The op number is used to allow multiple
instructions per line because PIR instructions often represent multiple
low-level instructions. This also makes it easy to inject pseudo-ops to
represent function calls.
Each op always has a time value representing the total amount of time spent in
that op. Ops may also have an op_name value that gives the name of the op.
When control flow similar to a function call is detected, a pseudo-op
representing a function call is injected. These pseudo-ops have zero cost when
initialized and are used to determine the total time spent between when the
context becomes active and when control flow returns to or past the context.
Although they're not exactly like functions calls, they're close enough that it
may help to think of them as such.
Uncomment the print_stats line in main to see a representation of the data
contained in C<$stats>.
=back
=head1 FUNCTIONS
=over 4
=item C<main>
This function is minimal driver for the other functions in this file, taking
the name of a Parrot profile and writing a Callgrind-compatible profile to a
similarly-named file.
=cut
sub main {
my $filename = shift;
my $stats = {};
die "Usage: $0 filename\n"
unless defined $filename;
$stats->{global_stats}{total_time} = 0;
open(my $in_fh, '<', $filename) or die "couldn't open $filename for reading: $!";
process_input($in_fh, $stats);
close($in_fh) or die "couldn't close $filename: $!";
unless ($filename =~ s/pprof/out/) {
$filename = "$filename.out";
}
open(my $out_fh, '>', $filename) or die "couldn't open $filename for writing: $!";
my $cg_profile = get_cg_profile($stats);
print $out_fh $cg_profile;
close($out_fh) or die "couldn't close $filename: $!";
print "$filename can now be used with kcachegrind or other callgrind-compatible tools.\n";
}
=item C<process_input>
This function takes a file handle open to a Parrot profile and a reference
to a hash of fine-grained statistics about the current PIR program. It
modifies the statistics according to the information from the Parrot profile.
=cut
sub process_input {
my ($input, $stats) = @_;
my $call_stack = [];
while(my $line = <$input>) {
if ($line =~ /^OP:(.*)$/) {
# Decode string in the format C<{x{key1:value1}x}{x{key2:value2}x}>
my %op_hash = $1 =~ /{x{([^:]+):(.*?)}x}/g
or die "invalidly formed line '$line'";
my $cur_ctx = $call_stack->[0]
or die "input file did not specify an initial context";
# If we've already seen this line, bump the op number. Otherwise reset it.
if (exists $cur_ctx->{line} && $op_hash{line} == $cur_ctx->{line}) {
$cur_ctx->{op_num}++;
}
else {
$cur_ctx->{op_num} = 0;
}
$cur_ctx->{line} = $op_hash{line};
my $extra = { op_name => $op_hash{op} };
my $time = $op_hash{time};
$stats->{global_stats}{total_time} += $time;
store_stats($stats, $cur_ctx, $time, $extra);
# Add the time spent by this op to each op on the call "stack".
$stats->{ $_->{file} }{ $_->{ns} }{ $_->{line} }[ $_->{op_num} ]{time} += $time
for @$call_stack[1 .. $#$call_stack];
}
#context switch
elsif ($line =~ /^CS:(.*)$/) {
# Decode string in the format C<{x{key1:value1}x}{x{key2:value2}x}>
my %cs_hash = $1 =~ /{x{([^:]+):(.*?)}x}/g
or die "invalidly formed line '$line'";
if (!@$call_stack) {
$call_stack->[0] = \%cs_hash;
}
else {
my $cur_ctx = $call_stack->[0];
my $hash_ctx = $cs_hash{ctx};
my $is_redundant = $cur_ctx->{ctx} eq $hash_ctx;
my $reused_ctx = $is_redundant && $cur_ctx->{sub} ne $cs_hash{sub};
# If we're calling a new sub with the same context, modify the
# current context to have the name and address of the new sub.
if ($reused_ctx) {
$cur_ctx->{ns} = $cs_hash{ns};
$cur_ctx->{sub} = $cs_hash{sub};
}
# The new context is the same as the old one, so don't modify the call stack.
elsif ($is_redundant) {
# This space intentionally left blank.
}
# If the new context isn't in the current call stack, unshift
# it onto the start of the stack.
elsif (!grep {$_->{ctx} eq $hash_ctx} @$call_stack) {
$cur_ctx->{op_num}++;
my $extra = {
op_name => "CALL",
target => $cs_hash{ns}
};
store_stats($stats, $call_stack->[0], 0, $extra);
unshift @$call_stack, \%cs_hash;
}
else {
#shift contexts off the stack until one matches the current ctx
shift @$call_stack while $call_stack->[0]{ctx} ne $hash_ctx;
}
}
#print Dumper($call_stack);
}
elsif ($line =~ /^VERSION:(\d+)$/) {
my $version = $1;
if ($version != 2) {
die "profile was generated by an incompatible version of the profiling runcore.";
}
}
elsif ($line =~ /^CLI:(.*)$/) {
$stats->{'global_stats'}{'cli'} = $1;
}
elsif ($line =~ /^END_OF_RUNLOOP:(.*)$/) {
# This is the end of an outermost runloop. Several of these can
# occur during the execution of a script, e.g. for :init subs.
@$call_stack = ();
}
elsif ($line =~ /^AN:/) {
#ignore annotations for now
}
elsif ($line =~ /^#/) {
#comments are always ignored
}
else {
die "Unrecognized line format: '$line'";
}
}
}
=item C<print_stats>
This function prints a complete, human-readable representation of the
statistical data that have been collected into the C<$stats> argument to
stdout. It is primarily intended to ease debugging and is not necessary to
create a Callgrind-compatible profile.
=cut
sub print_stats {
my $stats = shift;
for my $file (grep {$_ ne 'global_stats'} sort keys %$stats) {
for my $ns (sort keys %{ $stats->{$file} }) {
for my $line_num (sort {$a<=>$b} keys %{ $stats->{$file}{$ns} }) {
for my $op_num (0 .. $#{$stats->{$file}{$ns}{$line_num}}) {
print "$file $ns line/op:$line_num/$op_num ";
for my $attr (sort keys %{ $stats->{$file}{$ns}{$line_num}[$op_num] }) {
print "{ $attr => $stats->{$file}{$ns}{$line_num}[$op_num]{$attr} } ";
}
print "\n";
}
}
print "\n";
}
}
}
=item C<store_stats>
This function adds statistical data to the C<$stats> hash reference. The
C<$loc> argument specifies information such as the namespace, file, line
and op number where the data should go. C<$time> is an integer representing
the amount of time spent at the specified location. C<$extra> contains any
ancillary data that should be stored in the hash. This includes data on
(faked) subroutine calls and op names.
=cut
sub store_stats {
my ($stats, $loc, $time, $extra) = @_;
my $by_op = ( $stats->{ $loc->{file} }{ $loc->{ns} }{ $loc->{line} }[ $loc->{op_num} ] ||= {} );
if ($by_op->{hits}) {
$by_op->{hits} ++;
$by_op->{time} += $time;
}
else {
$by_op->{hits} = 1;
$by_op->{time} = $time;
$by_op->{$_} = $extra->{$_} for keys %$extra;
}
}
=item C<get_cg_profile>
This function takes a reference to a hash of statistical information about a
PIR program and returns a string containing a Callgrind-compatible profile.
Although some information in the profile may not be accurate (namely PID and
creator), tools such as kcachegrind are able to consume files generated by this
function.
=cut
sub get_cg_profile {
my $stats = shift;
my @output = ();
push @output, <<"HEADER";
version: 1
creator: 3.4.1-Debian
pid: 5751
cmd: $stats->{'global_stats'}{'cli'}
part: 1
desc: I1 cache:
desc: D1 cache:
desc: L2 cache:
desc: Timerange: Basic block 0 - $stats->{'global_stats'}{'total_time'}
desc: Trigger: Program termination
positions: line
events: Ir
summary: $stats->{'global_stats'}{'total_time'}
HEADER
for my $file (grep {$_ ne 'global_stats'} keys %$stats) {
push @output, "fl=$file";
for my $ns (keys %{ $stats->{$file} }) {
push @output, "\nfn=$ns";
for my $line (sort keys %{ $stats->{$file}{$ns} }) {
my $curr_op = 0;
my $line_stats = $stats->{$file}{$ns}{$line};
my $op_count = scalar(@$line_stats);
my $op_time = 0;
while ($curr_op < $op_count && $line_stats->[$curr_op]{'op_name'} ne 'CALL') {
$op_time += $line_stats->[$curr_op]{'time'};
$curr_op++;
}
push @output, "$line $op_time";
if ($curr_op < $op_count && $line_stats->[$curr_op]{'op_name'} eq 'CALL') {
my $call_target = $line_stats->[$curr_op]{'target'};
my $call_count = $line_stats->[$curr_op]{'hits'};
my $call_cost = $line_stats->[$curr_op]{'time'};
push @output, "cfn=$call_target";
push @output, "calls=$call_count $call_cost";
}
if ($curr_op < $op_count) {
$op_time = 0;
while ($curr_op < $op_count) {
$op_time += $line_stats->[$curr_op]{'time'};
$curr_op++;
}
push @output, "$line $op_time";
}
}
}
}
push @output, "totals: $stats->{'global_stats'}{'total_time'}";
return join("\n", @output);
}
=back
=cut
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: