-
Notifications
You must be signed in to change notification settings - Fork 22
/
profile.c
454 lines (363 loc) · 12.7 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* profile.c: Glulxe profiling functions.
Designed by Andrew Plotkin <[email protected]>
http://eblong.com/zarf/glulx/index.html
*/
/*
If compiled in, these functions maintain a collection of profiling
information as the Glulx program executes.
The profiling code is not smart about VM operations that rearrange the
call stack. In fact, it's downright stupid. @restart, @restore,
@restoreundo, or @throw will kill the interpreter.
On a normal VM exit (end of top-level routine or @quit), the profiler
writes out a data file, using the filename you provided with the
"--profile" option. Note that if the VM exits via glk_exit(), or is
interrupted, the data file will be created (empty) but never filled in.
The data file is an XML file of the form
<profile>
<function addr=HEX ... />
<calls fromaddr=HEX toaddr=HEX count=INT />
<function addr=HEX ... />
<calls fromaddr=HEX toaddr=HEX count=INT />
...
</profile>
The calls lines are only included if you use the "--profcalls" option.
These simply describe the number of times any function (identified by
VM address) called any other function. There will be at most one such
line for each (fromaddr, toaddr) pair.
The function list includes every function which was called during the
program's run. Each function tag includes the following attributes:
addr=HEX: The VM address of the function (in hex).
call_count=INT: The number of times the function was called.
accel_count=INT: The number of times the function was called with
acceleration.
total_time=FLOAT: The amount of time spent during all calls to the
function (in seconds, as a floating-point value).
total_ops=INT: The number of opcodes executed during all calls to
the function.
self_time=FLOAT: The amount of time spent during all calls to the
function, excluding time spent in subcalls (functions called *by* the
function).
self_ops=INT: The number of opcodes executed during all calls to
the function, excluding time spent in subcalls.
max_depth=INT: The deepest this function has been nested on the
stack, during any call.
max_stack_use=INT: The greatest number of words on the stack, during
any call. (This is measured when the function returns, so it may
not capture the peak stack usage. If a function never returns, e.g.
Main__(), then this value is approximate.)
Note that if a function does not make any function calls, total_time
will be the same as self_time (and total_ops the same as self_ops).
Some of the function entries refer to special interpreter operations.
(These have high addresses, outside the range of normal game files.)
Functions with addresses in the 0xE0000000 range are the interpreter's
output opcodes: @streamchar, @streamunichar, @streamnum, @streamstr.
Functions with addresses in the 0xF0000000 range are @glk opcode calls.
The number in the lower bits specifies which Glk function was called.
You will always see a large self_time for function 0xF00000C0; this
represents all the time spent waiting for input in glk_select().
(Both the 0xE0000000 and 0xF0000000 entries represent time spent in the
Glk library, but they get there by different code paths.)
The function with the lowest address is the top-level Main__()
function generated by the compiler. Its total_time is the running time
of the entire program; its total_ops is the number of opcodes executed
by the entire program; its max_depth is zero.
*/
#include "glk.h"
#include "glulxe.h"
#if VM_PROFILING
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
/* Set if the --profile switch is used. */
static int profiling_active = FALSE;
static char *profiling_filename = NULL;
static strid_t profiling_stream = NULL;
static int profiling_call_counts = FALSE;
typedef struct function_struct {
glui32 addr;
glui32 call_count;
glui32 accel_count;
glui32 entry_depth;
struct timeval entry_start_time;
glui32 entry_start_op;
glui32 max_depth;
glui32 max_stack_use;
struct timeval total_time;
glui32 total_ops;
struct timeval self_time;
glui32 self_ops;
struct callcount_struct *outcalls;
struct function_struct *hash_next;
} function_t;
typedef struct callcount_struct {
glui32 toaddr;
glui32 count;
struct callcount_struct *next;
} callcount_t;
typedef struct frame_struct {
struct frame_struct *parent;
function_t *func;
glui32 depth;
struct timeval entry_time;
glui32 entry_op;
struct timeval children_time;
glui32 children_ops;
} frame_t;
#define FUNC_HASH_SIZE (511)
static function_t **functions = NULL;
static frame_t *current_frame = NULL;
/* This counter is globally visible, because the profile_tick() macro
increments it. */
glui32 profile_opcount = 0;
/* This is called from the setup code -- glkunix_startup_code(), for the
Unix version. If called, the interpreter will keep profiling information,
and write it out at shutdown time. If this is not called, the interpreter
will skip all the profiling code. (Although it won't be quite as fast
as if the VM_PROFILING symbol were compiled out entirely.)
The arguments are a little tricky, because I developed this on Unix,
but I want it to remain accessible on all platforms. Pass a writable
stream object as the first argument; at game-shutdown time, the terp
will write the profiling data to this object and then close it.
However, if it's not convenient to open a stream in the startup code,
you can simply pass a filename as the second argument. This filename
will be opened according to the usual Glk data file rules, which means
it may wind up in a sandboxed data directory. The filename should not
contain slashes or other pathname separators.
If you pass NULL for both arguments, a file called "profile-raw" will
be written.
*/
void setup_profile(strid_t stream, char *filename)
{
profiling_active = TRUE;
if (stream)
profiling_stream = stream;
else if (filename)
profiling_filename = filename;
else
profiling_filename = "profile-raw";
}
int init_profile()
{
int bucknum;
if (!profiling_active)
return TRUE;
functions = (function_t **)glulx_malloc(FUNC_HASH_SIZE
* sizeof(function_t *));
if (!functions)
return FALSE;
for (bucknum=0; bucknum<FUNC_HASH_SIZE; bucknum++)
functions[bucknum] = NULL;
return TRUE;
}
void profile_set_call_counts(int flag)
{
profiling_call_counts = flag;
}
int profile_profiling_active()
{
return profiling_active;
}
static function_t *get_function(glui32 addr)
{
int bucknum = (addr % FUNC_HASH_SIZE);
function_t *func;
for (func = (functions[bucknum]);
func && func->addr != addr;
func = func->hash_next) { }
if (!func) {
func = (function_t *)glulx_malloc(sizeof(function_t));
if (!func)
fatal_error("Profiler: cannot malloc function.");
memset(func, 0, sizeof(function_t));
func->hash_next = functions[bucknum];
functions[bucknum] = func;
func->addr = addr;
func->call_count = 0;
func->accel_count = 0;
timerclear(&func->entry_start_time);
func->entry_start_op = 0;
timerclear(&func->total_time);
func->total_ops = 0;
timerclear(&func->self_time);
func->self_ops = 0;
func->max_depth = 0;
func->max_stack_use = 0;
func->outcalls = NULL;
}
return func;
}
static char *timeprint(struct timeval *tv, char *buf)
{
sprintf(buf, "%ld.%.6ld", (long)tv->tv_sec, (long)tv->tv_usec);
return buf;
}
void profile_in(glui32 addr, glui32 stackuse, int accel)
{
frame_t *fra;
function_t *func;
struct timeval now;
if (!profiling_active)
return;
/* printf("### IN: %lx%s\n", addr, (accel?" accel":"")); */
if (profiling_call_counts && current_frame) {
function_t *parfunc = current_frame->func;
callcount_t **ccref;
for (ccref = &parfunc->outcalls; *ccref; ccref = &((*ccref)->next)) {
if ((*ccref)->toaddr == addr)
break;
}
if (*ccref) {
(*ccref)->count += 1;
}
else {
*ccref = glulx_malloc(sizeof(callcount_t));
(*ccref)->toaddr = addr;
(*ccref)->count = 1;
(*ccref)->next = NULL;
}
}
gettimeofday(&now, NULL);
func = get_function(addr);
func->call_count += 1;
if (accel)
func->accel_count += 1;
if (!func->entry_depth) {
func->entry_start_time = now;
func->entry_start_op = profile_opcount;
}
func->entry_depth += 1;
if (func->max_stack_use < stackuse)
func->max_stack_use = stackuse;
fra = (frame_t *)glulx_malloc(sizeof(frame_t));
if (!fra)
fatal_error("Profiler: cannot malloc frame.");
memset(fra, 0, sizeof(frame_t));
fra->parent = current_frame;
current_frame = fra;
if (fra->parent)
fra->depth = fra->parent->depth + 1;
fra->func = func;
fra->entry_time = now;
fra->entry_op = profile_opcount;
timerclear(&fra->children_time);
fra->children_ops = 0;
}
void profile_out(glui32 stackuse)
{
frame_t *fra;
function_t *func;
struct timeval now, runtime;
glui32 runops;
if (!profiling_active)
return;
/* printf("### OUT\n"); */
if (!current_frame)
fatal_error("Profiler: stack underflow.");
gettimeofday(&now, NULL);
fra = current_frame;
func = fra->func;
timersub(&now, &fra->entry_time, &runtime);
runops = profile_opcount - fra->entry_op;
timeradd(&runtime, &func->self_time, &func->self_time);
timersub(&func->self_time, &fra->children_time, &func->self_time);
func->self_ops += runops;
func->self_ops -= fra->children_ops;
if (func->max_depth < fra->depth)
func->max_depth = fra->depth;
if (func->max_stack_use < stackuse)
func->max_stack_use = stackuse;
if (fra->parent) {
timeradd(&runtime, &fra->parent->children_time, &fra->parent->children_time);
fra->parent->children_ops += runops;
}
if (!func->entry_depth)
fatal_error("Profiler: function entry underflow.");
func->entry_depth -= 1;
if (!func->entry_depth) {
timersub(&now, &func->entry_start_time, &runtime);
timerclear(&func->entry_start_time);
runops = profile_opcount - func->entry_start_op;
func->entry_start_op = 0;
timeradd(&runtime, &func->total_time, &func->total_time);
func->total_ops += runops;
}
current_frame = fra->parent;
fra->parent = NULL;
glulx_free(fra);
}
/* ### throw/catch */
/* ### restore/restore_undo/restart */
void profile_fail(char *reason)
{
if (!profiling_active)
return;
fatal_error_2("Profiler: unable to handle operation", reason);
}
void profile_quit()
{
int bucknum;
function_t *func;
char linebuf[512];
strid_t profstr;
if (!profiling_active)
return;
while (current_frame) {
profile_out(0);
}
if (profiling_stream) {
profstr = profiling_stream;
}
else if (profiling_filename) {
frefid_t profref = glk_fileref_create_by_name(fileusage_BinaryMode|fileusage_Data, profiling_filename, 0);
if (!profref)
fatal_error_2("Profiler: unable to create profile output fileref", profiling_filename);
profstr = glk_stream_open_file(profref, filemode_Write, 0);
}
else {
fatal_error("Profiler: no profile output handle!");
}
glk_put_string_stream(profstr, "<profile>\n");
for (bucknum=0; bucknum<FUNC_HASH_SIZE; bucknum++) {
char total_buf[20], self_buf[20];
callcount_t *cc;
for (func = functions[bucknum]; func; func=func->hash_next) {
/* ###
sprintf(linebuf, "function %lx called %ld times, total ops %ld, total time %s, self ops %ld, self time %s\n",
func->addr, func->call_count,
func->total_ops,
timeprint(&func->total_time, total_buf),
func->self_ops,
timeprint(&func->self_time, self_buf));
### */
sprintf(linebuf, " <function addr=\"%lx\" call_count=\"%ld\" accel_count=\"%ld\" total_ops=\"%ld\" total_time=\"%s\" self_ops=\"%ld\" self_time=\"%s\" max_depth=\"%ld\" max_stack_use=\"%ld\" />\n",
(unsigned long)func->addr, (long)func->call_count, (long)func->accel_count,
(long)func->total_ops,
timeprint(&func->total_time, total_buf),
(long)func->self_ops,
timeprint(&func->self_time, self_buf),
(long)func->max_depth, (long)func->max_stack_use);
glk_put_string_stream(profstr, linebuf);
for (cc = func->outcalls; cc; cc = cc->next) {
sprintf(linebuf, " <calls fromaddr=\"%lx\" toaddr=\"%lx\" count=\"%ld\" />\n",
(unsigned long)func->addr, (unsigned long)cc->toaddr, (long)cc->count);
glk_put_string_stream(profstr, linebuf);
}
}
}
glk_put_string_stream(profstr, "</profile>\n");
glk_stream_close(profstr, NULL);
/* ### Ought to free the function structures, not just the hash array. */
glulx_free(functions);
functions = NULL;
}
#else /* VM_PROFILING */
void setup_profile(strid_t stream, char *filename)
{
/* Profiling is not compiled in. Do nothing. */
}
int init_profile()
{
/* Profiling is not compiled in. Do nothing. */
return TRUE;
}
#endif /* VM_PROFILING */