-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy path_yappi.c
1971 lines (1648 loc) · 48.4 KB
/
_yappi.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
yappi
Yet Another Python Profiler
Sumer Cip 2019
*/
#include "config.h"
#if !defined(HAVE_LONG_LONG)
#error "Yappi requires long longs!"
#endif
#ifdef IS_PY3K
#include "bytesobject.h"
#endif
#include "frameobject.h"
#include "callstack.h"
#include "hashtab.h"
#include "debug.h"
#include "timing.h"
#include "freelist.h"
#include "mem.h"
#ifdef IS_PY3K
PyDoc_STRVAR(_yappi__doc__, "Yet Another Python Profiler");
#endif
// linked list for holding callee/caller info in the pit
// we need to record the timing data on the pairs (parent, child)
typedef struct {
unsigned int index;
unsigned long callcount;
unsigned long nonrecursive_callcount; // how many times the child function is called non-recursively?
long long tsubtotal; // the time that the child function spent excluding its children (include recursive parent-child calls)
long long ttotal; // the total time that the child function spent
struct _pit_children_info *next;
} _pit_children_info;
typedef struct {
// we currently use frame pointer for identifying the running coroutine on
// a specific _pit.
PyFrameObject *frame;
// the first time this coroutine is seen on stack
long long t0;
struct _coro *next;
} _coro;
// module definitions
typedef struct {
PyObject *name;
PyObject *modname;
unsigned long lineno;
unsigned long callcount;
// the number of actual calls when the function is recursive.
unsigned long nonrecursive_callcount;
// time function spent excluding its children (include recursive calls)
long long tsubtotal;
// the total time that a function spent
long long ttotal;
unsigned int builtin;
// a number that uniquely identifies the _pit during the lifetime of a profile
// session (for multiple start/stop pairs) We could use PyCodeObject for this
// too, but then there are builtin calls, it is better to have a custom id
// per-pit instead of dealing with the keys and their references.
unsigned int index;
// concurrent running coroutines on this _pit
_coro *coroutines;
_pit_children_info *children;
} _pit; // profile_item
typedef struct {
_cstack *cs;
_htab *rec_levels;
// mapping tag:htab of pits
_htab *tags;
long id; // internal tid given by user callback or yappi. Will be unique per profile session.
long tid; // the real OS thread id.
PyObject *name;
long long t0; // profiling start CPU time
unsigned long sched_cnt; // how many times this thread is scheduled
PyThreadState *ts_ptr;
} _ctx; // context
typedef struct {
PyObject *ctx_id;
PyObject *tag;
PyObject *name;
PyObject *modname;
} _func_stat_filter;
typedef struct
{
_func_stat_filter func_filter;
PyObject *enumfn;
} _ctxenumarg;
typedef struct
{
_ctxenumarg *enum_args;
_ctx *ctx;
} _ctxfuncenumarg;
typedef struct {
int builtins;
int multithreaded;
} _flag; // flags passed from yappi.start()
// globals
static PyObject *YappiProfileError;
static _htab *contexts;
static _flag flags;
static _freelist *flpit;
static _freelist *flctx;
static int yappinitialized;
static unsigned int ycurfuncindex; // used for providing unique index for functions
static long ycurthreadindex = 0;
static int yapphavestats; // start() called at least once or stats cleared?
static int yapprunning;
static int paused;
static time_t yappstarttime;
static long long yappstarttick;
static long long yappstoptick;
static _ctx *prev_ctx = NULL;
static _ctx *current_ctx = NULL;
static _ctx *initial_ctx = NULL; // used for holding the context that called start()
static PyObject *context_id_callback = NULL;
static PyObject *tag_callback = NULL;
static PyObject *context_name_callback = NULL;
static PyObject *test_timings; // used for testing
static const long DEFAULT_TAG = 0;
// defines
#define UNINITIALIZED_STRING_VAL "N/A"
#ifdef IS_PY3K // string formatting helper functions compatible with with both 2.x and 3.x
#define PyStr_AS_CSTRING(s) PyUnicode_AsUTF8(s)
#define PyStr_Check(s) PyUnicode_Check(s)
#define PyStr_FromString(s) PyUnicode_FromString(s)
#define PyStr_FromFormatV(fmt, vargs) PyUnicode_FromFormatV(fmt, vargs)
#else // < Py3x
#define PyStr_AS_CSTRING(s) PyString_AS_STRING(s)
#define PyStr_Check(s) PyString_Check(s)
#define PyStr_FromString(s) PyString_FromString(s)
#define PyStr_FromFormatV(fmt, vargs) PyString_FromFormatV(fmt, vargs)
#endif
// forwards
static _ctx * _profile_thread(PyThreadState *ts);
static int _pitenumdel(_hitem *item, void *arg);
// funcs
/*
static void _DebugPrintObjects(unsigned int arg_count, ...)
{
unsigned int i;
va_list vargs;
va_start(vargs, arg_count);
for (i=0; i<arg_count; i++) {
PyObject_Print(va_arg(vargs, PyObject *), stdout, Py_PRINT_RAW);
}
printf("\n");
va_end(vargs);
}
*/
int IS_ASYNC(PyFrameObject *frame)
{
int result = 0;
#if defined(IS_PY3K)
#if PY_MINOR_VERSION >= 4
result = frame->f_code->co_flags & CO_COROUTINE ||
frame->f_code->co_flags & CO_ITERABLE_COROUTINE;
#endif
#if PY_MINOR_VERSION >= 6
result = result || frame->f_code->co_flags & CO_ASYNC_GENERATOR;
#endif
#endif
return result;
}
static PyObject *
PyStr_FromFormat(const char *fmt, ...)
{
PyObject* ret;
va_list vargs;
va_start(vargs, fmt);
ret = PyStr_FromFormatV(fmt, vargs);
va_end(vargs);
return ret;
}
// module functions
static void
_log_err(unsigned int code)
{
yerr("Internal Error. [%u]", code);
}
static _pit *
_create_pit()
{
_pit *pit;
pit = flget(flpit);
if (!pit)
return NULL;
pit->callcount = 0;
pit->nonrecursive_callcount = 0;
pit->ttotal = 0;
pit->tsubtotal = 0;
pit->name = NULL;
pit->modname = NULL;
pit->lineno = 0;
pit->builtin = 0;
pit->index = ycurfuncindex++;
pit->children = NULL;
pit->coroutines = NULL;
return pit;
}
static _ctx *
_create_ctx(void)
{
_ctx *ctx;
ctx = flget(flctx);
if (!ctx)
return NULL;
ctx->cs = screate(100);
if (!ctx->cs)
return NULL;
ctx->tags = htcreate(HT_TAG_SIZE);
if (!ctx->tags)
return NULL;
ctx->sched_cnt = 0;
ctx->id = 0;
ctx->tid = 0;
ctx->name = NULL;
ctx->t0 = tickcount();
ctx->rec_levels = htcreate(HT_RLEVEL_SIZE);
if (!ctx->rec_levels)
return NULL;
return ctx;
}
PyObject *
_call_funcobjargs(PyObject *func, PyObject *args)
{
// restore to correct ctx after a func call on Python side.
// Python side might have context switched to another thread which might
// have changed to current_ctx, after a while when the func returns
// we will be in a correct ThreadState * but current_ctx might not
// be correct.
PyObject *result;
_ctx *_local_current_ctx, *_local_prev_ctx;
_local_current_ctx = current_ctx;
_local_prev_ctx = prev_ctx;
result = PyObject_CallFunctionObjArgs(func, args);
current_ctx = _local_current_ctx;
prev_ctx = _local_prev_ctx;
return result;
}
static PyObject *
_current_context_name(void)
{
PyObject *name;
if (!context_name_callback) {
return NULL;
}
name = _call_funcobjargs(context_name_callback, NULL);
if (!name) {
PyErr_Print();
goto err;
}
if (name == Py_None) {
// Name not available yet - will try again on the next call
goto later;
}
if (!PyStr_Check(name)) {
yerr("context name callback returned non-string");
goto err;
}
return name;
err:
PyErr_Clear();
Py_CLEAR(context_name_callback); /* Don't use the callback again. */
Py_XDECREF(name);
return NULL;
later:
Py_XDECREF(name);
return NULL;
}
static long
_current_tag(void)
{
PyObject *r;
uintptr_t result;
if (!tag_callback) {
return DEFAULT_TAG;
}
r = _call_funcobjargs(tag_callback, NULL);
if (!r) {
PyErr_Print();
goto error;
}
result = (uintptr_t)PyLong_AsLong(r);
Py_DECREF(r);
if (PyErr_Occurred()) {
yerr("tag_callback returned non-integer");
goto error;
}
return result;
error:
PyErr_Clear();
Py_CLEAR(tag_callback); // don't use callback again
return 0;
}
static uintptr_t
_current_context_id(PyThreadState *ts)
{
uintptr_t rc;
PyObject *callback_rc, *d, *ytid;
PyThreadState *curr_ts;
if (context_id_callback) {
callback_rc = _call_funcobjargs(context_id_callback, NULL);
if (!callback_rc) {
PyErr_Print();
goto error;
}
rc = (uintptr_t)PyLong_AsLong(callback_rc);
Py_DECREF(callback_rc);
if (PyErr_Occurred()) {
yerr("context id callback returned non-integer");
goto error;
}
return rc;
} else {
// Use thread_id instead of ts pointer, because when we create/delete many threads, some
// of them do not show up in the thread_stats, because ts pointers are recycled in the VM.
// Also, OS tids are recycled, too. The only valid way is to give ctx's custom tids which
// are hold in a per-thread structure. Again: we use an integer instead of directly mapping the ctx
// pointer to some per-thread structure because other threading libraries do not necessarily
// have direct ThreadState->Thread mapping. Greenlets, for example, will only have a single
// thread. Therefore, we need to identify the "context" concept independent from ThreadState
// objects.
if (!flags.multithreaded) {
return 0;
}
curr_ts = PyThreadState_GET();
if (curr_ts != ts) {
PyThreadState_Swap(ts);
}
// TODO: Any more optimization? This has increased the runtime factor from 7x to 11x.
// and also we may have a memory leak below. We maybe can optimize the common case.
d = PyThreadState_GetDict();
ytid = PyDict_GetItemString(d, "_yappi_tid");
if (!ytid) {
ytid = PyLong_FromLong(ycurthreadindex++);
PyDict_SetItemString(d, "_yappi_tid", ytid);
}
rc = PyLong_AsLong(ytid);
if (curr_ts != ts) {
PyThreadState_Swap(curr_ts);
}
return rc;
}
error:
PyErr_Clear();
Py_CLEAR(context_id_callback); // don't use callback again
return 0;
}
static _ctx *
_thread2ctx(PyThreadState *ts)
{
_hitem *it;
it = hfind(contexts, _current_context_id(ts));
if (!it) {
// callback functions in some circumtances, can be called before the context entry is not
// created. (See issue 21). To prevent this problem we need to ensure the context entry for
// the thread is always available here.
return _profile_thread(ts);
}
return (_ctx *)it->val;
}
// the pit will be cleared by the relevant freelist. we do not free it here.
// we only DECREF the CodeObject or the MethodDescriptive string.
static void
_del_pit(_pit *pit)
{
// the pit will be freed by fldestrot() in clear_stats, otherwise it stays
// for later enumeration
_pit_children_info *it,*next;
// free children
it = pit->children;
while(it) {
next = (_pit_children_info *)it->next;
yfree(it);
it = next;
}
pit->children = NULL;
}
static PyObject *
_pycfunction_module_name(PyCFunctionObject *cfn)
{
PyObject *obj;
PyObject *name;
// The __module__ attribute, can be anything
obj = cfn->m_module;
if (!obj) {
// TODO: Is this always correct?
name = PyStr_FromString("__builtin__");
} else if (PyStr_Check(obj)) {
Py_INCREF(obj);
name = obj;
} else if (PyModule_Check(obj)) {
const char *s = PyModule_GetName(obj);
if (!s) {
goto error;
}
name = PyStr_FromString(s);
} else {
// Something else - str(obj)
name = PyObject_Str(obj);
}
return name;
error:
PyErr_Clear();
return PyStr_FromString("<unknown>");
}
_htab *
_get_pits_tbl(long current_tag)
{
_hitem *it;
_htab *pits;
it = hfind(current_ctx->tags, (uintptr_t)current_tag);
if (!it) {
pits = htcreate(HT_TAGGED_PIT_SIZE);
if (!pits) {
return NULL;
}
if (!hadd(current_ctx->tags, (uintptr_t)current_tag, (uintptr_t)pits)) {
return NULL;
}
return pits;
}
return (_htab *)it->val;
}
static _pit *
_ccode2pit(void *cco, long current_tag)
{
PyCFunctionObject *cfn;
_hitem *it;
PyObject *name;
_htab *pits;
pits = _get_pits_tbl(current_tag);
if (!pits) {
return NULL;
}
cfn = cco;
// Issue #15:
// Hashing cfn to the pits table causes different object methods
// to be hashed into the same slot. Use cfn->m_ml for hashing the
// Python C functions.
it = hfind(pits, (uintptr_t)cfn->m_ml);
if (!it) {
_pit *pit = _create_pit();
if (!pit)
return NULL;
if (!hadd(pits, (uintptr_t)cfn->m_ml, (uintptr_t)pit))
return NULL;
pit->builtin = 1;
pit->modname = _pycfunction_module_name(cfn);
pit->lineno = 0;
// built-in method?
if (cfn->m_self != NULL) {
name = PyStr_FromString(cfn->m_ml->ml_name);
if (name != NULL) {
PyObject *obj_type = PyObject_Type(cfn->m_self);
PyObject *mo = _PyType_Lookup((PyTypeObject *)obj_type, name);
Py_XINCREF(mo);
Py_XDECREF(obj_type);
Py_DECREF(name);
if (mo != NULL) {
pit->name = PyObject_Repr(mo);
Py_DECREF(mo);
return pit;
}
}
PyErr_Clear();
}
pit->name = PyStr_FromString(cfn->m_ml->ml_name);
return pit;
}
return ((_pit *)it->val);
}
// maps the PyCodeObject to our internal pit item via hash table.
static _pit *
_code2pit(PyFrameObject *fobj, long current_tag)
{
_hitem *it;
PyCodeObject *cobj;
_pit *pit;
_htab *pits;
pits = _get_pits_tbl(current_tag);
if (!pits) {
return NULL;
}
cobj = fobj->f_code;
it = hfind(pits, (uintptr_t)cobj);
if (it) {
return ((_pit *)it->val);
}
pit = _create_pit();
if (!pit)
return NULL;
if (!hadd(pits, (uintptr_t)cobj, (uintptr_t)pit))
return NULL;
pit->name = NULL;
Py_INCREF(cobj->co_filename);
pit->modname = cobj->co_filename;
pit->lineno = cobj->co_firstlineno;
PyFrame_FastToLocals(fobj);
if (cobj->co_argcount) {
const char *firstarg = PyStr_AS_CSTRING(PyTuple_GET_ITEM(cobj->co_varnames, 0));
if (!strcmp(firstarg, "self")) {
PyObject* locals = fobj->f_locals;
if (locals) {
PyObject* self = PyDict_GetItemString(locals, "self");
if (self) {
PyObject *class_obj = PyObject_GetAttrString(self, "__class__");
if (class_obj) {
PyObject *class_name = PyObject_GetAttrString(class_obj, "__name__");
if (class_name) {
pit->name = PyStr_FromFormat("%s.%s", PyStr_AS_CSTRING(class_name), PyStr_AS_CSTRING(cobj->co_name));
Py_DECREF(class_name);
}
Py_DECREF(class_obj);
}
}
}
}
}
if (!pit->name) {
Py_INCREF(cobj->co_name);
pit->name = cobj->co_name;
}
PyFrame_LocalsToFast(fobj, 0);
return pit;
}
static _pit *
_get_frame(void)
{
_cstackitem *ci;
ci = shead(current_ctx->cs);
if (!ci) {
return NULL;
}
return ci->ckey;
}
static _cstackitem *
_push_frame(_pit *cp)
{
return spush(current_ctx->cs, cp);
}
static _pit *
_pop_frame(void)
{
_cstackitem *ci;
ci = spop(current_ctx->cs);
if (!ci) {
return NULL;
}
return ci->ckey;
}
static _pit_children_info *
_add_child_info(_pit *parent, _pit *current)
{
_pit_children_info *newci;
// TODO: Optimize by moving to a freelist?
newci = ymalloc(sizeof(_pit_children_info));
if (!newci) {
return NULL;
}
newci->index = current->index;
newci->callcount = 0;
newci->nonrecursive_callcount = 0;
newci->ttotal = 0;
newci->tsubtotal = 0;
newci->next = (struct _pit_children_info *)parent->children;
parent->children = (_pit_children_info *)newci;
return newci;
}
static _pit_children_info *
_get_child_info(_pit *parent, _pit *current, int add_if_not_exists)
{
_pit_children_info *citem;
if (!parent || !current) {
return NULL;
}
citem = parent->children;
while(citem) {
if (citem->index == current->index) {
break;
}
citem = (_pit_children_info *)citem->next;
}
if (add_if_not_exists && !citem) {
citem = _add_child_info(parent, current);
}
return citem;
}
static long
get_rec_level(uintptr_t key)
{
_hitem *it;
it = hfind(current_ctx->rec_levels, key);
if (!it) {
_log_err(1);
return -1; // should not happen
}
return it->val;
}
static int
incr_rec_level(uintptr_t key)
{
_hitem *it;
it = hfind(current_ctx->rec_levels, key);
if (it) {
it->val++;
} else {
if (!hadd(current_ctx->rec_levels, key, 1))
{
_log_err(2);
return 0; // should not happen
}
}
return 1;
}
static int
decr_rec_level(uintptr_t key)
{
_hitem *it;
uintptr_t v;
it = hfind(current_ctx->rec_levels, key);
if (it) {
v = it->val--; /*supress warning -- it is safe to cast long vs pointers*/
if (v == 0)
{
hfree(current_ctx->rec_levels, it);
}
} else {
_log_err(3);
return 0; // should not happen
}
return 1;
}
static long long
_get_frame_elapsed(void)
{
_cstackitem *ci;
_pit *cp;
long long result;
ci = shead(current_ctx->cs);
if (!ci) {
return 0LL;
}
cp = ci->ckey;
if (test_timings) {
uintptr_t rlevel = get_rec_level((uintptr_t)cp);
PyObject *formatted_string = PyStr_FromFormat(
"%s_%d", PyStr_AS_CSTRING(cp->name), rlevel);
PyObject *tval = PyDict_GetItem(test_timings, formatted_string);
Py_DECREF(formatted_string);
if (tval) {
result = PyLong_AsLong(tval);
} else {
result = DEFAULT_TEST_ELAPSED_TIME;
}
} else {
result = tickcount() - ci->t0;
}
return result;
}
static int
_coro_enter(_pit *cp, PyFrameObject *frame)
{
_coro *coro;
if (!(get_timing_clock_type() == WALL_CLOCK) ||
(get_rec_level((uintptr_t)cp) != 1)) {
return 0;
}
// if we already have this coro then it was yielded before
coro = cp->coroutines;
while(coro) {
if (coro->frame == frame) {
return 0;
}
coro = (_coro *)coro->next;
}
//printf("CORO ENTER %s %p\n", PyStr_AS_CSTRING(cp->name), frame);
coro = ymalloc(sizeof(_coro));
if (!coro) {
return -1;
}
coro->frame = frame;
coro->t0 = tickcount();
coro->next = NULL;
if (cp->coroutines) {
coro->next = (struct _coro *)cp->coroutines;
}
cp->coroutines = coro;
return 1;
}
static long long
_coro_exit(_pit *cp, PyFrameObject *frame)
{
_coro *coro, *prev;
long long _t0;
if (!(get_timing_clock_type() == WALL_CLOCK) ||
(get_rec_level((uintptr_t)cp) != 1)) {
return 0;
}
//printf("CORO EXIT %s %p\n", PyStr_AS_CSTRING(cp->name), frame);
prev = NULL;
coro = cp->coroutines;
while(coro) {
if (coro->frame == frame) {
_t0 = coro->t0;
if (prev) {
prev->next = coro->next;
} else {
cp->coroutines = NULL;
}
yfree(coro);
return tickcount() - _t0;
}
prev = coro;
coro = (_coro *)coro->next;
}
// expected: a func leaves without enter
return 0;
}
static void
_call_enter(PyObject *self, PyFrameObject *frame, PyObject *arg, int ccall)
{
_pit *cp,*pp;
_cstackitem *ci;
_pit_children_info *pci;
long current_tag;
//printf("call ENTER:%s %s\n", PyStr_AS_CSTRING(frame->f_code->co_filename),
// PyStr_AS_CSTRING(frame->f_code->co_name));
current_tag = _current_tag();
if (ccall) {
cp = _ccode2pit((PyCFunctionObject *)arg, current_tag);
} else {
cp = _code2pit(frame, current_tag);
}
// something went wrong. No mem, or another error. we cannot find
// a corresponding pit. just run away:)
if (!cp) {
_log_err(4);
return;
}
// create/update children info if we have a valid parent
pp = _get_frame();
if (pp) {
pci = _get_child_info(pp, cp, 1);
if (!pci) {
_log_err(12); // defensive runaway
return;
}
incr_rec_level((uintptr_t)pci);
}
ci = _push_frame(cp);
if (!ci) { // runaway! (defensive)
_log_err(5);
return;
}
ci->t0 = tickcount();
incr_rec_level((uintptr_t)cp);
// TODO: Comment
if (IS_ASYNC(frame)) {
_coro_enter(cp, frame);
}
}
static void
_call_leave(PyObject *self, PyFrameObject *frame, PyObject *arg, int ccall)
{
long long elapsed;
_pit *cp, *pp, *ppp;
_pit_children_info *pci,*ppci;
int yielded = 0;
pci = ppci = NULL;
//printf("call LEAVE:%s %s\n", PyStr_AS_CSTRING(frame->f_code->co_filename),
// PyStr_AS_CSTRING(frame->f_code->co_name));
elapsed = _get_frame_elapsed();
// leaving a frame while callstack is empty?
cp = _pop_frame();
if (!cp) {
return;
}
//if (frame->f_code->co_flags & CO_GENERATOR) {
//printf("is a generator func.\n");
//}
// TODO: Comment
if (IS_ASYNC(frame)) {
if (frame->f_stacktop) {
yielded = 1;
if (get_timing_clock_type() == WALL_CLOCK) {
// In fact setting this zero means following:
// for this specific pit, we say that if WALL_CLOCK is on,
// we only aggregate time between first non-recursive enter and
// last non-recursive exit
elapsed = 0;
}
} else {
long long coro_elapsed = _coro_exit(cp, frame);
if (coro_elapsed > 0) {
elapsed = coro_elapsed;
}
}
}
if (!yielded) {
cp->callcount++;
}
// is this the last function in the callstack?
pp = _pop_frame();
if (!pp) {
// update actual pit
cp->ttotal += elapsed;
cp->tsubtotal += elapsed;
if (!yielded) {
cp->nonrecursive_callcount++;
}
decr_rec_level((uintptr_t)cp);
return;
}
// get children info
pci = _get_child_info(pp, cp, 0);
if(!pci)
{
_log_err(6);
return; // defensive
}
// a calls b. b's elapsed time is subtracted from a's tsub and
// a adds its own elapsed it is leaving.
pp->tsubtotal -= elapsed;
cp->tsubtotal += elapsed;
if (!yielded) {
pci->callcount++;
}
// a->b->c. b->c is substracted from a->b.
ppp = _get_frame();
if (ppp) {
ppci = _get_child_info(ppp, pp, 0);
if(!ppci) {
_log_err(7);
return;
}
ppci->tsubtotal -= elapsed;
}
pci->tsubtotal += elapsed;
// wait for the top-level function/parent/child to update timing values accordingly.
if (get_rec_level((uintptr_t)cp) == 1) {
cp->ttotal += elapsed;
if (!yielded) {