/
Spesh.rakumod
379 lines (336 loc) Β· 11 KB
/
Spesh.rakumod
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
# An attempt at providing the framework for an introspection interface to
# the files generated with MVM_SPESH_LOG=filename setting.
#
# Such a file consistes of 1 or more sections that consist of a log section,
# an update info section with statistics received, a planning section, and
# zero or more action sections.
#
# Method "bails" provides a Bag of the names of the ops that caused a bail.
#
# When used as a script, it will accept the name of a spesh file and output
# a list of all of the opcodes that caused a bail, ordered by number of bails.
#
# $ MVM_SPESH_LOG=spesh perl6 yourcode
# $ perl6 lib/Spesh.pm6 spesh
class Spesh {
has @.parts;
has $!bails;
has $!time;
has @!cuids;
method bails() {
$!bails //= [(+)] @.parts>>.bails;
}
method time() {
$!time //= @.parts>>.time.sum;
}
method cuids() {
@!cuids ?? @!cuids !! do {
for @.parts -> $part {
@!cuids[.cuid].push( $_ ) for $part.statistics;
@!cuids[.cuid].push( $_ ) for $part.actions;
}
@!cuids
}
}
role Bails {
has $!bails;
method bails(--> Bag:D) {
$!bails //= $.text
.comb( /:r bail ':' <-[ \n ]>* / )
.map( *.words.tail )
.map( { .starts-with( "(ins=" ) ?? .substr(5,*-1) !! $_ } )
.Bag
}
}
role Time {
method time(--> Int:D) {
$.text.match( / <after " "> \d+ <before us " "> /, :g ).sum // 0
}
}
role Cuid-File-Line {
method cuid(--> Int:D) {
$.text.match(
/:r <after 'cuid: ' > \d+ /
).Int
}
method file-line(--> Str:D) {
$.text.match(
/:r <after 'file: ' > <-[ ) ]>+ <before ')' > /
).Str
}
}
my class Received does Time {
has $.text;
}
my class Updated does Time {
has $.text;
method frames(--> Int:D) {
$.text.match(
/:r \d+ <before " frames" > /
).Int
}
}
my class Statistics does Time does Cuid-File-Line {
has $.text;
method name(--> Str:D) {
$.text.match(
/:r <after "Latest statistics for '" > <-[']>* <before "'" > /
).Str
}
method total-hits(--> Int:D) {
$.text.match(
/:r <after "Total hits: " > \d+ /
).Int
}
}
my class Planning does Time {
has $.text;
method specializations(--> Int:D) {
$.text.match(
/:r \d+ <before " specialization(s)" > /
).Int
}
method time(--> Int:D) {
$.text.match( /:r <after "planned in "> \d+ <before "us)"> /).Int
}
}
class Gathering { } # just used for status
class Observation does Bails does Time does Cuid-File-Line {
has $.text;
method name(--> Str:D) {
$.text.match(
/:r <after "Observed type specialization of '" > <-[']>* <before "'" > /
).Str
}
}
class GuardTree does Bails does Time does Cuid-File-Line {
has $.text;
method name(--> Str:D) {
$.text.match(
/:r <after "Latest guard tree for '" > <-[']>* <before "'" > /
).Str
}
}
class Certainty does Bails does Time does Cuid-File-Line {
has $.text;
method name(--> Str:D) {
$.text.match(
/:r <after "Certain specialization of '" > <-[']>* <before "'" > /
).Str
}
}
class Specialization does Bails does Time does Cuid-File-Line {
has $.text;
method time(--> Int:D) {
$.text.match(
/:r <after 'Specialization took ' > \d+ <before 'us' > /
).Int
}
method jitted(--> Bool:D) {
$.text.contains('JIT was successful')
}
method compilation-time(--> Int:D) {
$.text.match(
/:r <after 'compilation took ' > \d+ <before 'us' > /
).Int
}
method frame-size(--> Int:D) {
$.text.match(
/:r <after 'Frame size: ' > \d+ <before ' byte' > /
).Int
}
method bytecode-size(--> Int:D) {
$.text.match(
/:r <after 'Bytecode size: ' > \d+ <before ' byte' > /
).Int
}
method name(--> Str:D) {
$.text.match(
/:r <after "Spesh of '" > <-[']>* <before "' " > /
).Str
}
}
class Part {
has $.received;
has $.updated;
has @.statistics;
has $.planning;
has @.actions;
has $.bails;
has $.time;
method bails() {
$!bails //= [(+)] @.actions>>.bails;
}
method time(--> Int:D) {
$!time //=
$.received.time
+ $.updated.time
+ @.statistics>>.time.sum
+ $.planning.time
+ @.actions>>.time.sum
}
method observations() { @.actions.grep: Observation }
method guardtrees() { @.actions.grep: GuardTree }
method certainties() { @.actions.grep: Certainty }
method specializations() { @.actions.grep: Specialization }
}
my constant start-Received = "Received Logs";
my constant start-Updated = "Statistics Updated";
my constant start-Statistics = "Latest statistics";
my constant start-Planning = "Specialization Plan";
my constant start-Observation = "Observed type specialization of";
my constant start-GuardTree = "Latest guard tree for";
my constant start-Certainty = "Certain specialization of";
my constant start-Specialization = "Specialization of";
method new($filename --> Spesh:D) {
my $handle = $filename.IO.open or die "Could not read $filename: $!";
my $status = Received;
my @parts;
my $received;
my $updated;
my @statistics;
my $planning;
my @actions;
my $type;
my @text;
sub finish() {
@actions.push(
$type.new(text => @text.join("\n"))
);
}
sub next($next, $line) {
@text = $line;
$status = Gathering;
$type = $next;
}
for $handle.lines {
if $status === Gathering {
if .starts-with(start-Received) {
finish;
@parts.push(Part.new(
received => Received.new(text => $received),
updated => Updated.new(text => $updated),
statistics => @statistics,
planning => Planning.new(text => $planning),
actions => @actions,
));
@text = $_;
$status = Received;
}
elsif .starts-with(start-Observation) {
finish;
next(Observation, $_);
}
elsif .starts-with(start-GuardTree) {
finish;
next(GuardTree, $_);
}
elsif .starts-with(start-Certainty) {
finish;
next(Certainty, $_);
}
elsif .starts-with(start-Specialization) {
finish;
next(Specialization, $_);
}
else {
@text.push($_);
}
}
elsif $status === Planning {
if .starts-with(start-Observation) {
$planning = @text.join("\n");
next(Observation, $_);
}
elsif .starts-with(start-GuardTree) {
$planning = @text.join("\n");
next(GuardTree, $_);
}
elsif .starts-with(start-Certainty) {
$planning = @text.join("\n");
next(Certainty, $_);
}
elsif .starts-with(start-Specialization) {
$planning = @text.join("\n");
next(Specialization, $_);
}
elsif .starts-with(start-Received) {
$planning = @text.join("\n");
@parts.push(Part.new(
received => Received.new(text => $received),
updated => Updated.new(text => $updated),
statistics => @statistics,
planning => Planning.new(text => $planning),
));
@text = $_;
$status = Received;
}
else {
@text.push($_);
}
}
elsif $status === Statistics {
if .starts-with(start-Planning) {
@statistics.push(
Statistics.new(text => @text.join("\n"))
);
@actions = ();
@text = $_;
$status = Planning;
}
elsif .starts-with(start-Statistics) {
@statistics.push(
Statistics.new(text => @text.join("\n"))
);
@text = $_;
}
else {
@text.push($_);
}
}
elsif $status === Updated {
if .starts-with(start-Statistics) {
$updated = @text.join("\n");
@statistics = ();
@text = $_;
$status = Statistics;
}
else {
@text.push($_);
}
}
elsif $status === Received {
if .starts-with(start-Updated) {
$received = @text.join("\n");
@text = $_;
$status = Updated;
}
else {
@text.push($_);
}
}
else {
die;
}
}
$handle.close;
@parts.push(Part.new(
received => Received.new(text => $received),
updated => Updated.new(text => $updated),
statistics => @statistics,
planning => Planning.new(text => $planning),
actions => @actions,
));
self.bless(:@parts)
}
}
sub MAIN($filename where *.IO.e) {
note "Parsing '$filename'";
my $spesh = Spesh.new($filename);
note "Finding bailed ops";
my $bails = $spesh.bails;
note "Done";
say "Found $bails.elems() different ops getting bailed:";
printf("%4d: %s\n", .value, .key) for $bails.sort( *.value );
say $spesh.cuids[1]>>.file-line.Bag;
}