-
Notifications
You must be signed in to change notification settings - Fork 171
/
profile.c
403 lines (350 loc) · 15.9 KB
/
profile.c
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
402
403
#include "moar.h"
/* Simple allocation functions. */
static MVMObject * new_array(MVMThreadContext *tc) {
return MVM_repr_alloc_init(tc, MVM_hll_current(tc)->slurpy_array_type);
}
static MVMObject * new_hash(MVMThreadContext *tc) {
return MVM_repr_alloc_init(tc, MVM_hll_current(tc)->slurpy_hash_type);
}
static MVMObject * box_i(MVMThreadContext *tc, MVMint64 i) {
return MVM_repr_box_int(tc, MVM_hll_current(tc)->int_box_type, i);
}
static MVMObject * box_s(MVMThreadContext *tc, MVMString *s) {
return MVM_repr_box_str(tc, MVM_hll_current(tc)->str_box_type, s);
}
static MVMString * str(MVMThreadContext *tc, const char *buf) {
return MVM_string_ascii_decode_nt(tc, tc->instance->VMString, buf);
}
/* String constants we'll reuse. */
typedef struct {
MVMString *total_time;
MVMString *call_graph;
MVMString *name;
MVMString *id;
MVMString *file;
MVMString *line;
MVMString *entries;
MVMString *spesh_entries;
MVMString *jit_entries;
MVMString *inlined_entries;
MVMString *inclusive_time;
MVMString *exclusive_time;
MVMString *callees;
MVMString *allocations;
MVMString *type;
MVMString *count;
MVMString *gcs;
MVMString *time;
MVMString *full;
MVMString *cleared_bytes;
MVMString *retained_bytes;
MVMString *promoted_bytes;
MVMString *osr;
MVMString *deopt_one;
MVMString *deopt_all;
MVMString *spesh_time;
MVMString *is_collection;
} ProfDumpStrs;
static void call_node_to_hash(MVMThreadContext *tc, ProfDumpStrs *pds,
MVMProfileCallNode *pcn, MVMObject *node_hash) {
MVMObject *alloc_list = new_array(tc);
MVMuint32 i;
/* Try to resolve the code filename and line number. */
MVMBytecodeAnnotation *annot = MVM_bytecode_resolve_annotation(tc,
&(pcn->sf->body), 0);
MVMint32 fshi = annot ? (MVMint32)annot->filename_string_heap_index : -1;
/* Add name of code object. */
MVM_repr_bind_key_o(tc, node_hash,
pds->name, box_s(tc, pcn->sf->body.name));
/* Add line number and file name. */
if (fshi >= 0 && fshi < pcn->sf->body.cu->body.num_strings)
MVM_repr_bind_key_o(tc, node_hash, pds->file,
box_s(tc, pcn->sf->body.cu->body.strings[fshi]));
else if (pcn->sf->body.cu->body.filename)
MVM_repr_bind_key_o(tc, node_hash, pds->file,
box_s(tc, pcn->sf->body.cu->body.filename));
else
MVM_repr_bind_key_o(tc, node_hash, pds->file,
box_s(tc, tc->instance->str_consts.empty));
MVM_repr_bind_key_o(tc, node_hash, pds->line,
box_i(tc, annot ? (MVMint32)annot->line_number : -1));
/* Use static frame memory address to get a unique ID. */
MVM_repr_bind_key_o(tc, node_hash, pds->id,
box_i(tc, (MVMint64)pcn->sf));
/* Entry counts. */
MVM_repr_bind_key_o(tc, node_hash, pds->entries,
box_i(tc, pcn->total_entries));
MVM_repr_bind_key_o(tc, node_hash, pds->spesh_entries,
box_i(tc, pcn->specialized_entries));
MVM_repr_bind_key_o(tc, node_hash, pds->jit_entries,
box_i(tc, pcn->jit_entries));
MVM_repr_bind_key_o(tc, node_hash, pds->inlined_entries,
box_i(tc, pcn->inlined_entries));
/* Total (inclusive) time. */
MVM_repr_bind_key_o(tc, node_hash, pds->inclusive_time,
box_i(tc, pcn->total_time / 1000));
/* OSR and deopt counts. */
MVM_repr_bind_key_o(tc, node_hash, pds->osr,
box_i(tc, pcn->osr_count));
MVM_repr_bind_key_o(tc, node_hash, pds->deopt_one,
box_i(tc, pcn->deopt_one_count));
MVM_repr_bind_key_o(tc, node_hash, pds->deopt_all,
box_i(tc, pcn->deopt_all_count));
/* Emit allocations. */
MVM_repr_bind_key_o(tc, node_hash, pds->allocations, alloc_list);
for (i = 0; i < pcn->num_alloc; i++) {
MVMObject *alloc_info = new_hash(tc);
MVMObject *type = pcn->alloc[i].type;
MVM_repr_bind_key_o(tc, alloc_info, pds->id, box_i(tc, (MVMint64)type));
MVM_repr_bind_key_o(tc, alloc_info, pds->type, type);
MVM_repr_bind_key_o(tc, alloc_info, pds->count,
box_i(tc, pcn->alloc[i].allocations));
MVM_repr_push_o(tc, alloc_list, alloc_info);
}
}
static void aggregate_nodes(MVMThreadContext *tc, ProfDumpStrs *pds, MVMProfileCallNode *pcn,
MVMObject *existing_hash, MVMObject *existing_callees) {
/* First, let's see if there's already an entry in the existing callees
* that has the same ID as our call data object */
MVMuint64 existing_elems = MVM_repr_elems(tc, existing_callees);
MVMuint64 target_id = (MVMuint64)pcn->sf;
MVMObject *node_hash;
MVMint64 index;
/* XXX this function currently never touches existing_hash, but it really should. */
for (index = 0; index < existing_elems; index++) {
MVMObject *callee = MVM_repr_at_pos_o(tc, existing_callees, index);
MVMuint64 other_id = MVM_repr_get_int(tc, MVM_repr_at_key_o(tc, callee, pds->id));
if (other_id == target_id) {
MVMint64 alloc_i;
MVMint64 alloc_k;
MVMint64 existing_allocations;
MVMObject *alloc_list;
node_hash = callee;
/* add up entry counts. */
MVM_repr_bind_key_o(tc, node_hash, pds->entries,
box_i(tc, pcn->total_entries
+ MVM_repr_get_int(tc, MVM_repr_at_key_o(tc, node_hash, pds->entries))));
MVM_repr_bind_key_o(tc, node_hash, pds->spesh_entries,
box_i(tc, pcn->specialized_entries
+ MVM_repr_get_int(tc, MVM_repr_at_key_o(tc, node_hash, pds->spesh_entries))));
MVM_repr_bind_key_o(tc, node_hash, pds->jit_entries,
box_i(tc, pcn->jit_entries
+ MVM_repr_get_int(tc, MVM_repr_at_key_o(tc, node_hash, pds->jit_entries))));
MVM_repr_bind_key_o(tc, node_hash, pds->inlined_entries,
box_i(tc, pcn->inlined_entries
+ MVM_repr_get_int(tc, MVM_repr_at_key_o(tc, node_hash, pds->inlined_entries))));
/* add more total (inclusive) time. */
MVM_repr_bind_key_o(tc, node_hash, pds->inclusive_time,
box_i(tc, pcn->total_time / 1000
+ MVM_repr_get_int(tc, MVM_repr_at_key_o(tc, node_hash, pds->inclusive_time))));
/* OSR and deopt counts. */
MVM_repr_bind_key_o(tc, node_hash, pds->osr,
box_i(tc, pcn->osr_count
+ MVM_repr_get_int(tc, MVM_repr_at_key_o(tc, node_hash, pds->osr))));
MVM_repr_bind_key_o(tc, node_hash, pds->deopt_one,
box_i(tc, pcn->deopt_one_count
+ MVM_repr_get_int(tc, MVM_repr_at_key_o(tc, node_hash, pds->deopt_one))));
MVM_repr_bind_key_o(tc, node_hash, pds->deopt_all,
box_i(tc, pcn->deopt_all_count
+ MVM_repr_get_int(tc, MVM_repr_at_key_o(tc, node_hash, pds->deopt_all))));
/* Merging allocations is a tiny bit more difficult,
* as we have to go through another list to find a matching entry
* to merge with. */
/* Emit allocations. */
alloc_list = MVM_repr_at_key_o(tc, node_hash, pds->allocations);
existing_allocations = MVM_repr_elems(tc, alloc_list);
for (alloc_i = 0; alloc_i < pcn->num_alloc; alloc_i++) {
MVMObject *alloc_info = NULL;
for (alloc_k = 0; alloc_k < existing_allocations && alloc_info == NULL; alloc_k++) {
alloc_info = MVM_repr_at_pos_o(tc, alloc_list, alloc_k);
if (MVM_repr_get_int(tc, MVM_repr_at_key_o(tc, alloc_info, pds->id)) != (MVMint64)pcn->alloc[alloc_i].type) {
alloc_info = NULL;
}
}
if (alloc_info != NULL) {
MVM_repr_bind_key_o(tc, alloc_info, pds->count,
box_i(tc, pcn->alloc[alloc_i].allocations
+ MVM_repr_get_int(tc, MVM_repr_at_key_o(tc, alloc_info, pds->count))));
} else {
MVMObject *type = pcn->alloc[alloc_i].type;
MVMObject *alloc_info = new_hash(tc);
MVM_repr_bind_key_o(tc, alloc_info, pds->id, box_i(tc, (MVMint64)type));
MVM_repr_bind_key_o(tc, alloc_info, pds->type, type);
MVM_repr_bind_key_o(tc, alloc_info, pds->count,
box_i(tc, pcn->alloc[alloc_i].allocations));
MVM_repr_push_o(tc, alloc_list, alloc_info);
}
}
goto do_successors;
}
}
{
node_hash = new_hash(tc);
/* We got here, that means we have to generate a new node */
call_node_to_hash(tc, pds, pcn, node_hash);
MVM_repr_push_o(tc, existing_callees, node_hash);
}
do_successors:
if (pcn->num_succ) {
MVMuint32 i;
MVMuint64 exclusive_time = pcn->total_time;
for (i = 0; i < pcn->num_succ; i++) {
MVM_repr_bind_key_o(tc, node_hash, pds->is_collection, box_i(tc, 1));
/* XXX In the next call, we should really pass existing_hash instead of node_hash */
aggregate_nodes(tc, pds, pcn->succ[i], node_hash, existing_callees);
exclusive_time -= pcn->succ[i]->total_time;
}
MVM_repr_bind_key_o(tc, node_hash, pds->exclusive_time,
box_i(tc, exclusive_time / 1000));
}
else {
MVM_repr_bind_key_o(tc, node_hash, pds->exclusive_time,
box_i(tc, pcn->total_time / 1000));
}
}
/* Dumps a call graph node. */
static MVMObject * dump_call_graph_node(MVMThreadContext *tc, ProfDumpStrs *pds,
MVMProfileCallNode *pcn, MVMuint8 depth) {
MVMObject *node_hash = new_hash(tc);
MVMuint32 i;
call_node_to_hash(tc, pds, pcn, node_hash);
/* Visit successors in the call graph, dumping them and working out the
* exclusive time. */
if (pcn->num_succ) {
MVMObject *callees = new_array(tc);
MVMuint64 exclusive_time = pcn->total_time;
for (i = 0; i < pcn->num_succ; i++) {
if (depth < 32) {
MVM_repr_push_o(tc, callees,
dump_call_graph_node(tc, pds, pcn->succ[i], depth + 1));
exclusive_time -= pcn->succ[i]->total_time;
}
else {
MVM_repr_bind_key_o(tc, node_hash, pds->is_collection, box_i(tc, 1));
aggregate_nodes(tc, pds, pcn->succ[i], node_hash, callees);
exclusive_time -= pcn->succ[i]->total_time;
}
}
MVM_repr_bind_key_o(tc, node_hash, pds->exclusive_time,
box_i(tc, exclusive_time / 1000));
MVM_repr_bind_key_o(tc, node_hash, pds->callees, callees);
}
else {
MVM_repr_bind_key_o(tc, node_hash, pds->exclusive_time,
box_i(tc, pcn->total_time / 1000));
}
return node_hash;
}
/* Dumps data from a single thread. */
static MVMObject * dump_thread_data(MVMThreadContext *tc, ProfDumpStrs *pds,
MVMProfileThreadData *ptd) {
MVMObject *thread_hash = new_hash(tc);
MVMObject *thread_gcs = new_array(tc);
MVMuint32 i;
/* Add time. */
MVM_repr_bind_key_o(tc, thread_hash, pds->total_time,
box_i(tc, (ptd->end_time - ptd->start_time) / 1000));
/* Add call graph. */
if (ptd->call_graph)
MVM_repr_bind_key_o(tc, thread_hash, pds->call_graph,
dump_call_graph_node(tc, pds, ptd->call_graph, 0));
/* Add GCs. */
for (i = 0; i < ptd->num_gcs; i++) {
MVMObject *gc_hash = new_hash(tc);
MVM_repr_bind_key_o(tc, gc_hash, pds->time,
box_i(tc, ptd->gcs[i].time / 1000));
MVM_repr_bind_key_o(tc, gc_hash, pds->full,
box_i(tc, ptd->gcs[i].full));
MVM_repr_bind_key_o(tc, gc_hash, pds->cleared_bytes,
box_i(tc, ptd->gcs[i].cleared_bytes));
MVM_repr_bind_key_o(tc, gc_hash, pds->retained_bytes,
box_i(tc, ptd->gcs[i].retained_bytes));
MVM_repr_bind_key_o(tc, gc_hash, pds->promoted_bytes,
box_i(tc, ptd->gcs[i].promoted_bytes));
MVM_repr_push_o(tc, thread_gcs, gc_hash);
}
MVM_repr_bind_key_o(tc, thread_hash, pds->gcs, thread_gcs);
/* Add spesh time. */
MVM_repr_bind_key_o(tc, thread_hash, pds->spesh_time,
box_i(tc, ptd->spesh_time / 1000));
return thread_hash;
}
/* Dumps data from all threads into an array of per-thread data. */
static MVMObject * dump_data(MVMThreadContext *tc) {
MVMObject *threads_array;
ProfDumpStrs pds;
/* We'll allocate the data in gen2, but as we want to keep it, but to be
* sure we don't trigger a GC run. */
MVM_gc_allocate_gen2_default_set(tc);
/* Some string constants to re-use. */
pds.total_time = str(tc, "total_time");
pds.call_graph = str(tc, "call_graph");
pds.name = str(tc, "name");
pds.id = str(tc, "id");
pds.file = str(tc, "file");
pds.line = str(tc, "line");
pds.entries = str(tc, "entries");
pds.spesh_entries = str(tc, "spesh_entries");
pds.jit_entries = str(tc, "jit_entries");
pds.inlined_entries = str(tc, "inlined_entries");
pds.inclusive_time = str(tc, "inclusive_time");
pds.exclusive_time = str(tc, "exclusive_time");
pds.callees = str(tc, "callees");
pds.allocations = str(tc, "allocations");
pds.type = str(tc, "type");
pds.count = str(tc, "count");
pds.gcs = str(tc, "gcs");
pds.time = str(tc, "time");
pds.full = str(tc, "full");
pds.cleared_bytes = str(tc, "cleared_bytes");
pds.retained_bytes = str(tc, "retained_bytes");
pds.promoted_bytes = str(tc, "promoted_bytes");
pds.osr = str(tc, "osr");
pds.deopt_one = str(tc, "deopt_one");
pds.deopt_all = str(tc, "deopt_all");
pds.spesh_time = str(tc, "spesh_time");
pds.is_collection = str(tc, "is_collection");
/* Build up threads array. */
/* XXX Only main thread for now. */
threads_array = new_array(tc);
if (tc->prof_data)
MVM_repr_push_o(tc, threads_array, dump_thread_data(tc, &pds, tc->prof_data));
/* Switch back to default allocation and return result; */
MVM_gc_allocate_gen2_default_clear(tc);
return threads_array;
}
/* Starts profiling with the specified configuration. */
void MVM_profile_start(MVMThreadContext *tc, MVMObject *config) {
/* Enable profiling. */
if (tc->instance->profiling)
MVM_exception_throw_adhoc(tc, "Profiling is already started");
tc->instance->profiling = 1;
tc->instance->instrumentation_level++;
}
/* Ends profiling, builds the result data structure, and returns it. */
MVMObject * MVM_profile_end(MVMThreadContext *tc) {
/* Disable profiling. */
/* XXX Needs to account for multiple threads. */
if (!tc->instance->profiling)
MVM_exception_throw_adhoc(tc, "Cannot end profiling if not profiling");
tc->instance->profiling = 0;
tc->instance->instrumentation_level++;
/* Record end time. */
if (tc->prof_data)
tc->prof_data->end_time = uv_hrtime();
/* Build and return result data structure. */
return dump_data(tc);
}
/* Marks objects held in the profiling graph. */
static void mark_call_graph_node(MVMThreadContext *tc, MVMProfileCallNode *node, MVMGCWorklist *worklist) {
MVMuint32 i;
MVM_gc_worklist_add(tc, worklist, &(node->sf));
for (i = 0; i < node->num_alloc; i++)
MVM_gc_worklist_add(tc, worklist, &(node->alloc[i].type));
for (i = 0; i < node->num_succ; i++)
mark_call_graph_node(tc, node->succ[i], worklist);
}
void MVM_profile_mark_data(MVMThreadContext *tc, MVMGCWorklist *worklist) {
if (tc->prof_data)
mark_call_graph_node(tc, tc->prof_data->call_graph, worklist);
}