Source

hotpy_2 / Python / trace_logger.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* Trace Recorder */
/* This is the final stage of the pipeline.
 * It just records the instructions it receives */

#include "Python.h"
#include "opcode.h"
#include "optimiser.h"
#define WITH_INSTRUCTION_NAMES
#include "trace_opcode.h"

typedef struct _trace_logger {
    HotPyOptimiser opt_base;
    FILE *out;
    char *indent;
} HotPyTraceLogger;

#define OUT (((HotPyTraceLogger *)x)->out)
#define INDENT (((HotPyTraceLogger *)x)->indent)

static char *indent_str = ".........................................";

static void
print_str(FILE *out, PyObject *value)
{
    if (value == NULL)  {
        fprintf(out, "null");
        return;
    }
    if (Py_TYPE(value) != &PyTuple_Type && Py_TYPE(value) != &PyUnicode_Type &&
        (PySequence_Check(value) || PyMapping_Check(value)))
        fprintf(out, "<%s>", Py_TYPE(value)->tp_name);
    else
        PyObject_Print(value, out, 0);
}

static void
push_constant(HotPyOptimiser *x, PyObject *value)
{
    fprintf(OUT, "%spush constant ", INDENT);
    print_str(OUT, value);
    fprintf(OUT, "\n");
    x->next->push_constant(x->next, value);
}

static void comment(HotPyOptimiser *x, const char *text)
{
   fprintf(OUT, "# %s\n", text);
}

static HotPyContext *
require_type(HotPyOptimiser *x, PyTypeObject* type)
{
    fprintf(OUT, "%sRequire type: %s\n", INDENT, type->tp_name);
    return x->next->require_type(x->next, type);
}

static HotPyContext *
require_value(HotPyOptimiser *x, PyObject* v)
{
    fprintf(OUT, "%sRequire value: ", INDENT);
    print_str(OUT, v);
    fprintf(OUT, "\n");
    return x->next->require_value(x->next, v);
}

static HotPyContext *
require_code(HotPyOptimiser *x, PyObject* v)
{
    PyObject *code;
    char *kind;
    if (PyFunction_Check(v)) {
        kind = "function";
        code = PyFunction_GET_CODE(v);
    }
    else {
        kind = "generator";
        code = ((PyGenObject *)v)->gi_code;
    }
    fprintf(OUT, "%sRequire (%s) code: ", INDENT, kind);
    print_str(OUT, code);
    fprintf(OUT, "\n");
    return x->next->require_code(x->next, v);
}

static HotPyContext *
require_lasti(HotPyOptimiser *x, int lasti)
{
    fprintf(OUT, "%sRequire (generator) lasti: %d\n", INDENT, lasti);
    return x->next->require_lasti(x->next, lasti);
}

static HotPyContext *
require_boolean_or_int(HotPyOptimiser *x)
{
    fprintf(OUT, "%sRequire boolean or int\n", INDENT);
    return x->next->require_boolean_or_int(x->next);
}

static HotPyContext *
opcode_with_exit(HotPyOptimiser *x, int op)
{
    fprintf(OUT, "%s%s\n", INDENT, _HotPy_Instruction_Names[op]);
    return x->next->opcode_with_exit(x->next, op);
}

static HotPyContext *
prepare_call1(HotPyOptimiser *x)
{
    fprintf(OUT, "%sprepare_call1\n", INDENT);
    return x->next->prepare_call1(x->next);
}

static HotPyContext *
prepare_call2(HotPyOptimiser *x)
{
    fprintf(OUT, "%sprepare_call2\n", INDENT);
    return x->next->prepare_call2(x->next);
}

static HotPyContext *
call_object(HotPyOptimiser *x)
{
    fprintf(OUT, "%scall_object\n", INDENT);
    return x->next->call_object(x->next);
}

static HotPyContext *
call_cfunction(HotPyOptimiser *x, PyObject *f, int args)
{
    fprintf(OUT, "%scall_cfunction %d\n", INDENT, args);
    return x->next->call_cfunction(x->next, f, args);
}

static HotPyContext *
call_cmethod(HotPyOptimiser *x, PyObject *f, int args)
{
    fprintf(OUT, "%scall_cmethod %d\n", INDENT, args);
    return x->next->call_cmethod(x->next, f, args);
}

static HotPyContext *
call_wrapper(HotPyOptimiser *x, int op, PyObject *w)
{
    fprintf(OUT, "%s%s\n", INDENT, _HotPy_Instruction_Names[op]);
    return x->next->call_wrapper(x->next, op, w);
}

static void clear(HotPyOptimiser *x)
{
    INDENT = indent_str + strlen(indent_str);
    fflush(OUT);
    x->next->clear(x->next);
}

static void
finish(HotPyOptimiser *x)
{
    INDENT = indent_str + strlen(indent_str);
    fprintf(OUT, "--------------\n\n");
    fflush(OUT);
    x->next->finish(x->next);
}

static int
start(HotPyOptimiser *x, HotPyTraceObject *trace, PyFrameObject *f, HotPyContext *c, char *reason)
{
    char buf[2000];
    int i;
    fprintf(OUT, "Starting Trace %lx at line %d", (long)trace,
            PyFrame_GetLineNumber(f));
    fprintf(OUT, " of ");
    print_str(OUT, f->f_code->co_name);
    fprintf(OUT, "\nReason: %s\n--------------\n", reason);
    HotPyContext_Dump(&buf[0], c);
    fprintf(OUT, "%s", buf);
    INDENT = indent_str + strlen(indent_str);
    assert(strlen(indent_str) > HOTPY_CONTEXT_DEPTH);
    for (i = 0; i < HOTPY_CONTEXT_DEPTH; i++) {
        if (c->cf_instrs[i].code == NULL)
            break;
        INDENT -= 1;
    }
    return x->next->start(x->next, trace, f, c, reason);
}

static void
opcode(HotPyOptimiser *x, int op)
{
    fprintf(OUT, "%s%s\n", INDENT, _HotPy_Instruction_Names[op]);
    x->next->opcode(x->next, op);
}

static void
tuple_concat(HotPyOptimiser *x)
{
    fprintf(OUT, "%stuple concat\n", INDENT);
    x->next->tuple_concat(x->next);
}

static void
setup_block(HotPyOptimiser *x, int op, int oparg0, int oparg1)
{
    fprintf(OUT, "%s%s %d %d\n", INDENT, _HotPy_Instruction_Names[op], oparg0, oparg1);
    x->next->setup_block(x->next, op, oparg0, oparg1);
}

static void
opcode_with_arg(HotPyOptimiser *x, int op, int oparg)
{
    fprintf(OUT, "%s%s %d\n", INDENT, _HotPy_Instruction_Names[op], oparg);
    x->next->opcode_with_arg(x->next, op, oparg);
}

static void
set_lasti(HotPyOptimiser *x, int index)
{
    int line = -1;
    /* Find code object then use PyCode_Addr2Line(PyCodeObject *, index); to get line */
    fprintf(OUT, "%sset lasti %d (line %d)\n", INDENT, index, line);
    return x->next->set_lasti(x->next, index);
}

static void
pick(HotPyOptimiser *x, int index)
{
    fprintf(OUT, "%spick %d\n", INDENT, index);
    return x->next->pick(x->next, index);
}

static void
get_globals(HotPyOptimiser *x)
{
    fprintf(OUT, "%sget_globals\n", INDENT);
    x->next->get_globals(x->next);
}

static HotPyContext *
make_frame(HotPyOptimiser *x, PyObject *function)
{
    fprintf(OUT, "%smake_frame\n", INDENT);
    return x->next->make_frame(x->next, function);
}

static void
make_frame_const(HotPyOptimiser *x, PyObject *function)
{
    fprintf(OUT, "%smake_frame_const\n", INDENT);
    x->next->make_frame_const(x->next, function);
}

static void
gen_enter(HotPyOptimiser *x, PyCodeObject *code)
{
    fprintf(OUT, "%sgen enter\n", INDENT);
    x->next->gen_enter(x->next, code);
    assert(INDENT > indent_str);
    --INDENT;
}

static void
enter_frame(HotPyOptimiser *x, PyCodeObject *code)
{
    fprintf(OUT, "%senter frame\n", INDENT);
    x->next->enter_frame(x->next, code);
    assert(INDENT > indent_str);
    --INDENT;
}

static void
leave_gen_frame(HotPyOptimiser *x)
{
    fprintf(OUT, "%sleave gen frame\n", INDENT);
    x->next->leave_gen_frame(x->next);
    assert(INDENT < indent_str + strlen(indent_str));
    ++INDENT;
}

static void
leave_frame(HotPyOptimiser *x)
{
    fprintf(OUT, "%sleave frame\n", INDENT);
    x->next->leave_frame(x->next);
    assert(INDENT < indent_str + strlen(indent_str));
    ++INDENT;
}

static void
binary_op(HotPyOptimiser *x, int func, PyObject *res)
{
    fprintf(OUT, "%sbinary op %d->", INDENT, func);
    print_str(OUT, (PyObject *)Py_TYPE(res));
    fprintf(OUT, "\n");
    x->next->binary_op(x->next, func, res);
}

static void
binary_op_vk(HotPyOptimiser *x, int func, PyObject *k, PyObject *res)
{
    fprintf(OUT, "%sbinary op vk %d (right=", INDENT, func);
    print_str(OUT, k);
    fprintf(OUT, ")->");
    print_str(OUT, (PyObject *)Py_TYPE(res));
    fprintf(OUT, "\n");
    x->next->binary_op_vk(x->next, func, k, res);
}

static void
binary_op_kv(HotPyOptimiser *x, int func, PyObject *k, PyObject *res)
{
    fprintf(OUT, "%sbinary op kv %d (left=", INDENT, func);
    print_str(OUT, k);
    fprintf(OUT, ")->");
    print_str(OUT, (PyObject *)Py_TYPE(res));
    fprintf(OUT, "\n");
    x->next->binary_op_kv(x->next, func, k, res);
}

static HotPyContext*
from_object_dict_fast(HotPyOptimiser *x, int dict_offset,
                      PyDictKeysObject *keys, PyObject *key)
{
    fprintf(OUT, "%sfrom_object_dict_fast\n", INDENT);
    return x->next->from_object_dict_fast(x->next, dict_offset, keys, key);
}

static void
set_in_object_dict_fast(HotPyOptimiser *x, int dict_offset, PyDictKeysObject *keys, PyObject *key)
{
    fprintf(OUT, "%sset_in_object_dict_fast\n", INDENT);
    return x->next->set_in_object_dict_fast(x->next, dict_offset, keys, key);
}

#define OPCODE_WITH_ARG(xxx) \
static void \
xxx(HotPyOptimiser *x, int oparg) \
{ \
    fprintf(OUT, "%s%s %d\n", INDENT, #xxx, oparg); \
    x->next->xxx(x->next, oparg); \
}

#define OPCODE_WITH_NAME(xxx) \
static void \
xxx(HotPyOptimiser *x, PyObject *name) \
{ \
    fprintf(OUT, "%s%s ", INDENT, #xxx); \
    print_str(OUT, name); \
    fprintf(OUT, "\n"); \
    x->next->xxx(x->next, name); \
}

OPCODE_WITH_ARG(load_fast)
OPCODE_WITH_ARG(store_fast)
OPCODE_WITH_ARG(delete_fast)
OPCODE_WITH_NAME(load_from_globals)
OPCODE_WITH_NAME(store_to_globals)
OPCODE_WITH_NAME(has_special)
OPCODE_WITH_ARG(load_register)
OPCODE_WITH_ARG(load_clear_register)
OPCODE_WITH_ARG(store_register)

static void
fast_load_from_globals(HotPyOptimiser *x, PyObject *globals, PyObject *name)
{
    fprintf(OUT, "%sfast_load_from_globals ", INDENT);
    print_str(OUT, name);
    fprintf(OUT, "\n");
    x->next->fast_load_from_globals(x->next, globals, name);
}

static void
fast_frame(HotPyOptimiser *x, int n, PyCodeObject *code)
{
    fprintf(OUT, "%sfast_frame %d\n", INDENT, n);
    x->next->fast_frame(x->next, n, code);
}

static void
fail(HotPyOptimiser *x, int op, char *msg)
{
    fprintf(OUT, "FAIL: '%s' in instruction %s\n", msg, _HotPy_Instruction_Names[op]);
    return x->next->fail(x->next, op, msg);
}

static HotPyContext *
current_context(HotPyOptimiser *x)
{
    return x->next->current_context(x->next);
}

static void
dead_locals(HotPyOptimiser *x)
{
    fprintf(OUT, "%s#Dead locals\n", INDENT);
    x->next->dead_locals(x->next);
}

static void
unrolling(HotPyOptimiser *x)
{
    fprintf(OUT, "%sLoop Header <--\n", INDENT);
    x->next->unrolling(x->next);
}

static void
bind_callable(HotPyOptimiser *x, PyTypeObject *bound_type)
{
    fprintf(OUT, "%sbind_callable \n", INDENT);
    x->next->bind_callable(x->next, bound_type);
}

static HotPyContext *
load_member(HotPyOptimiser *x, PyMemberDescrObject *desc)
{
    fprintf(OUT, "%sload_member \n", INDENT);
    return x->next->load_member(x->next, desc);
}

static HotPyContext *
store_member(HotPyOptimiser *x, PyMemberDef *def)
{
    fprintf(OUT, "%sstore_member \n", INDENT);
    return x->next->store_member(x->next, def);
}

HotPyOptimiser *HotPyTraceLogger_New(HotPyOptimiser * next, FILE *out) {
    HotPyTraceLogger *x = PyMem_Malloc(sizeof(HotPyTraceLogger));
    x->opt_base.opt_name = "trace logger";
    x->opt_base.next = next;
    x->opt_base.push_constant = push_constant;
    x->opt_base.require_type = require_type;
    x->opt_base.require_lasti = require_lasti;
    x->opt_base.require_value = require_value;
    x->opt_base.require_code = require_code;
    x->opt_base.require_boolean_or_int = require_boolean_or_int;
    x->opt_base.opcode_with_exit = opcode_with_exit;
    x->opt_base.clear = clear;
    x->opt_base.comment = comment;
    x->opt_base.start = start;
    x->opt_base.finish = finish;
    x->opt_base.set_lasti = set_lasti;
    x->opt_base.unrolling = unrolling;
    x->opt_base.fail = fail;
    x->opt_base.call_object = call_object;
    x->opt_base.call_cfunction = call_cfunction;
    x->opt_base.call_cmethod = call_cmethod;
    x->opt_base.call_wrapper = call_wrapper;
    x->opt_base.prepare_call1 = prepare_call1;
    x->opt_base.prepare_call2 = prepare_call2;
    x->opt_base.load_fast = load_fast;
    x->opt_base.store_fast = store_fast;
    x->opt_base.delete_fast = delete_fast;
    x->opt_base.get_globals = get_globals;
    x->opt_base.load_from_globals = load_from_globals;
    x->opt_base.fast_load_from_globals = fast_load_from_globals;
    x->opt_base.store_to_globals = store_to_globals;
    x->opt_base.has_special = has_special;
    x->opt_base.load_register = load_register;
    x->opt_base.load_clear_register = load_clear_register;
    x->opt_base.store_register = store_register;
    x->opt_base.set_lasti = set_lasti;
    x->opt_base.fast_frame = fast_frame;
    x->opt_base.make_frame = make_frame;
    x->opt_base.make_frame_const = make_frame_const;
    x->opt_base.enter_frame = enter_frame;
    x->opt_base.gen_enter = gen_enter;
    x->opt_base.leave_frame = leave_frame;
    x->opt_base.leave_gen_frame = leave_gen_frame;
    x->opt_base.binary_op = binary_op;
    x->opt_base.binary_op_kv = binary_op_kv;
    x->opt_base.binary_op_vk = binary_op_vk;
    x->opt_base.tuple_concat = tuple_concat;
    x->opt_base.setup_block = setup_block;
    x->opt_base.pick = pick;
    x->opt_base.bind_callable = bind_callable;
    x->opt_base.load_member = load_member;
    x->opt_base.store_member = store_member;
    x->opt_base.opcode = opcode;
    x->opt_base.from_object_dict_fast = from_object_dict_fast;
    x->opt_base.set_in_object_dict_fast = set_in_object_dict_fast;
    x->opt_base.opcode_with_arg = opcode_with_arg;
    x->opt_base.current_context = current_context;
    x->opt_base.dead_locals = dead_locals;
    x->out = out;
    return (HotPyOptimiser *)x;
}