Debian Codegraph @

terps/glulxe/profile.c

raw symbols SCIP
1 /* profile.c: Glulxe profiling functions.
2 Designed by Andrew Plotkin <[email protected]>
3 http://eblong.com/zarf/glulx/index.html
4 */
5
6 /*
7 If compiled in, these functions maintain a collection of profiling
8 information as the Glulx program executes.
9
10 The profiling code is not smart about VM operations that rearrange the
11 call stack. In fact, it's downright stupid. @restart, @restore,
12 @restoreundo, or @throw will kill the interpreter.
13
14 On a normal VM exit (end of top-level routine or @quit), the profiler
15 writes out a data file, using the filename you provided with the
16 "--profile" option. Note that if the VM exits via glk_exit(), or is
17 interrupted, the data file will be created (empty) but never filled in.
18
19 The data file is an XML file of the form
20
21 <profile>
22 <function addr=HEX ... />
23 <calls fromaddr=HEX toaddr=HEX count=INT />
24 <function addr=HEX ... />
25 <calls fromaddr=HEX toaddr=HEX count=INT />
26 ...
27 </profile>
28
29 The calls lines are only included if you use the "--profcalls" option.
30 These simply describe the number of times any function (identified by
31 VM address) called any other function. There will be at most one such
32 line for each (fromaddr, toaddr) pair.
33
34 The function list includes every function which was called during the
35 program's run. Each function tag includes the following attributes:
36
37 addr=HEX: The VM address of the function (in hex).
38 call_count=INT: The number of times the function was called.
39 accel_count=INT: The number of times the function was called with
40 acceleration.
41 total_time=FLOAT: The amount of time spent during all calls to the
42 function (in seconds, as a floating-point value).
43 total_ops=INT: The number of opcodes executed during all calls to
44 the function.
45 self_time=FLOAT: The amount of time spent during all calls to the
46 function, excluding time spent in subcalls (functions called *by* the
47 function).
48 self_ops=INT: The number of opcodes executed during all calls to
49 the function, excluding time spent in subcalls.
50 max_depth=INT: The deepest this function has been nested on the
51 stack, during any call.
52 max_stack_use=INT: The greatest number of words on the stack, during
53 any call. (This is measured when the function returns, so it may
54 not capture the peak stack usage. If a function never returns, e.g.
55 Main__(), then this value is approximate.)
56
57 Note that if a function does not make any function calls, total_time
58 will be the same as self_time (and total_ops the same as self_ops).
59
60 Some of the function entries refer to special interpreter operations.
61 (These have high addresses, outside the range of normal game files.)
62 Functions with addresses in the 0xE0000000 range are the interpreter's
63 output opcodes: @streamchar, @streamunichar, @streamnum, @streamstr.
64
65 Functions with addresses in the 0xF0000000 range are @glk opcode calls.
66 The number in the lower bits specifies which Glk function was called.
67 You will always see a large self_time for function 0xF00000C0; this
68 represents all the time spent waiting for input in glk_select().
69
70 (Both the 0xE0000000 and 0xF0000000 entries represent time spent in the
71 Glk library, but they get there by different code paths.)
72
73 The function with the lowest address is the top-level Main__()
74 function generated by the compiler. Its total_time is the running time
75 of the entire program; its total_ops is the number of opcodes executed
76 by the entire program; its max_depth is zero.
77
78 */
79
80 #include "glk.h"
81 #include "glulxe.h"
82
83 #if VM_PROFILING
84
85 #include <stdio.h>
86 #include <string.h>
87 #include <sys/time.h>
88
89 /* Set if the --profile switch is used. */
90 static int profiling_active = FALSE;
91 static char *profiling_filename = NULL;
92 static strid_t profiling_stream = NULL;
93 static int profiling_call_counts = FALSE;
94
95 typedef struct function_struct {
96 glui32 addr;
97
98 glui32 call_count;
99 glui32 accel_count;
100 glui32 entry_depth;
101 struct timeval entry_start_time;
102 glui32 entry_start_op;
103 glui32 max_depth;
104 glui32 max_stack_use;
105 struct timeval total_time;
106 glui32 total_ops;
107 struct timeval self_time;
108 glui32 self_ops;
109
110 struct callcount_struct *outcalls;
111
112 struct function_struct *hash_next;
113 } function_t;
114
115 typedef struct callcount_struct {
116 glui32 toaddr;
117 glui32 count;
118 struct callcount_struct *next;
119 } callcount_t;
120
121 typedef struct frame_struct {
122 struct frame_struct *parent;
123 function_t *func;
124 glui32 depth;
125
126 struct timeval entry_time;
127 glui32 entry_op;
128
129 struct timeval children_time;
130 glui32 children_ops;
131 } frame_t;
132
133 #define FUNC_HASH_SIZE (511)
134
135 static function_t **functions = NULL;
136 static frame_t *current_frame = NULL;
137
138 /* This counter is globally visible, because the profile_tick() macro
139 increments it. */
140 glui32 profile_opcount = 0;
141
142 /* This is called from the setup code -- glkunix_startup_code(), for the
143 Unix version. If called, the interpreter will keep profiling information,
144 and write it out at shutdown time. If this is not called, the interpreter
145 will skip all the profiling code. (Although it won't be quite as fast
146 as if the VM_PROFILING symbol were compiled out entirely.)
147
148 The arguments are a little tricky, because I developed this on Unix,
149 but I want it to remain accessible on all platforms. Pass a writable
150 stream object as the first argument; at game-shutdown time, the terp
151 will write the profiling data to this object and then close it.
152
153 However, if it's not convenient to open a stream in the startup code,
154 you can simply pass a filename as the second argument. This filename
155 will be opened according to the usual Glk data file rules, which means
156 it may wind up in a sandboxed data directory. The filename should not
157 contain slashes or other pathname separators.
158
159 If you pass NULL for both arguments, a file called "profile-raw" will
160 be written.
161 */
162 void setup_profile(strid_t stream, char *filename)
163 {
164 profiling_active = TRUE;
165
166 if (stream)
167 profiling_stream = stream;
168 else if (filename)
169 profiling_filename = filename;
170 else
171 profiling_filename = "profile-raw";
172 }
173
174 int init_profile()
175 {
176 int bucknum;
177
178 if (!profiling_active)
179 return TRUE;
180
181 functions = (function_t **)glulx_malloc(FUNC_HASH_SIZE
182 * sizeof(function_t *));
183 if (!functions)
184 return FALSE;
185
186 for (bucknum=0; bucknum<FUNC_HASH_SIZE; bucknum++)
187 functions[bucknum] = NULL;
188
189 return TRUE;
190 }
191
192 void profile_set_call_counts(int flag)
193 {
194 profiling_call_counts = flag;
195 }
196
197 int profile_profiling_active()
198 {
199 return profiling_active;
200 }
201
202 static function_t *get_function(glui32 addr)
203 {
204 int bucknum = (addr % FUNC_HASH_SIZE);
205 function_t *func;
206
207 for (func = (functions[bucknum]);
208 func && func->addr != addr;
209 func = func->hash_next) { }
210
211 if (!func) {
212 func = (function_t *)glulx_malloc(sizeof(function_t));
213 if (!func)
214 fatal_error("Profiler: cannot malloc function.");
215 memset(func, 0, sizeof(function_t));
216 func->hash_next = functions[bucknum];
217 functions[bucknum] = func;
218
219 func->addr = addr;
220 func->call_count = 0;
221 func->accel_count = 0;
222 timerclear(&func->entry_start_time);
223 func->entry_start_op = 0;
224 timerclear(&func->total_time);
225 func->total_ops = 0;
226 timerclear(&func->self_time);
227 func->self_ops = 0;
228 func->max_depth = 0;
229 func->max_stack_use = 0;
230 func->outcalls = NULL;
231 }
232
233 return func;
234 }
235
236 static char *timeprint(struct timeval *tv, char *buf)
237 {
238 sprintf(buf, "%ld.%.6ld", (long)tv->tv_sec, (long)tv->tv_usec);
239 return buf;
240 }
241
242 void profile_in(glui32 addr, glui32 stackuse, int accel)
243 {
244 frame_t *fra;
245 function_t *func;
246 struct timeval now;
247
248 if (!profiling_active)
249 return;
250
251 /* printf("### IN: %lx%s\n", addr, (accel?" accel":"")); */
252
253 if (profiling_call_counts && current_frame) {
254 function_t *parfunc = current_frame->func;
255 callcount_t **ccref;
256 for (ccref = &parfunc->outcalls; *ccref; ccref = &((*ccref)->next)) {
257 if ((*ccref)->toaddr == addr)
258 break;
259 }
260 if (*ccref) {
261 (*ccref)->count += 1;
262 }
263 else {
264 *ccref = glulx_malloc(sizeof(callcount_t));
265 (*ccref)->toaddr = addr;
266 (*ccref)->count = 1;
267 (*ccref)->next = NULL;
268 }
269 }
270
271 gettimeofday(&now, NULL);
272
273 func = get_function(addr);
274 func->call_count += 1;
275 if (accel)
276 func->accel_count += 1;
277 if (!func->entry_depth) {
278 func->entry_start_time = now;
279 func->entry_start_op = profile_opcount;
280 }
281 func->entry_depth += 1;
282
283 if (func->max_stack_use < stackuse)
284 func->max_stack_use = stackuse;
285
286 fra = (frame_t *)glulx_malloc(sizeof(frame_t));
287 if (!fra)
288 fatal_error("Profiler: cannot malloc frame.");
289 memset(fra, 0, sizeof(frame_t));
290
291 fra->parent = current_frame;
292 current_frame = fra;
293
294 if (fra->parent)
295 fra->depth = fra->parent->depth + 1;
296 fra->func = func;
297 fra->entry_time = now;
298 fra->entry_op = profile_opcount;
299 timerclear(&fra->children_time);
300 fra->children_ops = 0;
301 }
302
303 void profile_out(glui32 stackuse)
304 {
305 frame_t *fra;
306 function_t *func;
307 struct timeval now, runtime;
308 glui32 runops;
309
310 if (!profiling_active)
311 return;
312
313 /* printf("### OUT\n"); */
314
315 if (!current_frame)
316 fatal_error("Profiler: stack underflow.");
317
318 gettimeofday(&now, NULL);
319
320 fra = current_frame;
321 func = fra->func;
322
323 timersub(&now, &fra->entry_time, &runtime);
324 runops = profile_opcount - fra->entry_op;
325
326 timeradd(&runtime, &func->self_time, &func->self_time);
327 timersub(&func->self_time, &fra->children_time, &func->self_time);
328 func->self_ops += runops;
329 func->self_ops -= fra->children_ops;
330
331 if (func->max_depth < fra->depth)
332 func->max_depth = fra->depth;
333 if (func->max_stack_use < stackuse)
334 func->max_stack_use = stackuse;
335
336 if (fra->parent) {
337 timeradd(&runtime, &fra->parent->children_time, &fra->parent->children_time);
338 fra->parent->children_ops += runops;
339 }
340
341 if (!func->entry_depth)
342 fatal_error("Profiler: function entry underflow.");
343
344 func->entry_depth -= 1;
345 if (!func->entry_depth) {
346 timersub(&now, &func->entry_start_time, &runtime);
347 timerclear(&func->entry_start_time);
348
349 runops = profile_opcount - func->entry_start_op;
350 func->entry_start_op = 0;
351
352 timeradd(&runtime, &func->total_time, &func->total_time);
353 func->total_ops += runops;
354 }
355
356 current_frame = fra->parent;
357 fra->parent = NULL;
358
359 glulx_free(fra);
360 }
361
362 /* ### throw/catch */
363 /* ### restore/restore_undo/restart */
364
365 void profile_fail(char *reason)
366 {
367 if (!profiling_active)
368 return;
369
370 fatal_error_2("Profiler: unable to handle operation", reason);
371 }
372
373 void profile_quit()
374 {
375 int bucknum;
376 function_t *func;
377 char linebuf[512];
378 strid_t profstr;
379
380 if (!profiling_active)
381 return;
382
383 while (current_frame) {
384 profile_out(0);
385 }
386
387 if (profiling_stream) {
388 profstr = profiling_stream;
389 }
390 else if (profiling_filename) {
391 frefid_t profref = glk_fileref_create_by_name(fileusage_BinaryMode|fileusage_Data, profiling_filename, 0);
392 if (!profref)
393 fatal_error_2("Profiler: unable to create profile output fileref", profiling_filename);
394
395 profstr = glk_stream_open_file(profref, filemode_Write, 0);
396 }
397 else {
398 fatal_error("Profiler: no profile output handle!");
399 }
400
401 glk_put_string_stream(profstr, "<profile>\n");
402
403 for (bucknum=0; bucknum<FUNC_HASH_SIZE; bucknum++) {
404 char total_buf[20], self_buf[20];
405 callcount_t *cc;
406
407 for (func = functions[bucknum]; func; func=func->hash_next) {
408 /* ###
409 sprintf(linebuf, "function %lx called %ld times, total ops %ld, total time %s, self ops %ld, self time %s\n",
410 func->addr, func->call_count,
411 func->total_ops,
412 timeprint(&func->total_time, total_buf),
413 func->self_ops,
414 timeprint(&func->self_time, self_buf));
415 ### */
416 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",
417 (unsigned long)func->addr, (long)func->call_count, (long)func->accel_count,
418 (long)func->total_ops,
419 timeprint(&func->total_time, total_buf),
420 (long)func->self_ops,
421 timeprint(&func->self_time, self_buf),
422 (long)func->max_depth, (long)func->max_stack_use);
423 glk_put_string_stream(profstr, linebuf);
424 for (cc = func->outcalls; cc; cc = cc->next) {
425 sprintf(linebuf, " <calls fromaddr=\"%lx\" toaddr=\"%lx\" count=\"%ld\" />\n",
426 (unsigned long)func->addr, (unsigned long)cc->toaddr, (long)cc->count);
427 glk_put_string_stream(profstr, linebuf);
428 }
429 }
430 }
431
432 glk_put_string_stream(profstr, "</profile>\n");
433
434 glk_stream_close(profstr, NULL);
435
436 /* ### Ought to free the function structures, not just the hash array. */
437 glulx_free(functions);
438 functions = NULL;
439 }
440
441 #else /* VM_PROFILING */
442
443 void setup_profile(strid_t stream, char *filename)
444 {
445 /* Profiling is not compiled in. Do nothing. */
446 }
447
448 int init_profile()
449 {
450 /* Profiling is not compiled in. Do nothing. */
451 return TRUE;
452 }
453
454 #endif /* VM_PROFILING */