FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Modules/_lsprof.c at 3.15 · python/cpython · GitHub
python
cpython
Repository navigation
Code
Issues
5k+
(5k+)
Pull requests
2.5k
(2.5k)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Modules
/
_lsprof.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
1164 lines (1015 loc) · 33.4 KB
Breadcrumbs
cpython
/
Modules
/
_lsprof.c
Copy path
File metadata and controls
1164 lines (1015 loc) · 33.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#ifndef
Py_BUILD_CORE_BUILTIN
# define
Py_BUILD_CORE_MODULE
1
#endif
#include
"Python.h"
#include
"pycore_call.h"
// _PyObject_CallNoArgs()
#include
"pycore_ceval.h"
// _PyEval_SetProfile()
#include
"pycore_pystate.h"
// _PyThreadState_GET()
#include
"pycore_time.h"
// _PyTime_FromSecondsObject()
#include
"pycore_typeobject.h"
// _PyType_GetModuleState()
#include
"pycore_unicodeobject.h"
// _PyUnicode_EqualToASCIIString()
#include
"rotatingtree.h"
/************************************************************/
/* Written by Brett Rosen and Ted Czotter */
struct
_ProfilerEntry
;
/* represents a function called from another function */
typedef
struct
_ProfilerSubEntry
{
rotating_node_t
header
;
PyTime_t
tt
;
PyTime_t
it
;
long
callcount
;
long
recursivecallcount
;
long
recursionLevel
;
}
ProfilerSubEntry
;
/* represents a function or user defined block */
typedef
struct
_ProfilerEntry
{
rotating_node_t
header
;
PyObject
*
userObj
;
/* PyCodeObject, or a descriptive str for builtins */
PyTime_t
tt
;
/* total time in this entry */
PyTime_t
it
;
/* inline time in this entry (not in subcalls) */
long
callcount
;
/* how many times this was called */
long
recursivecallcount
;
/* how many times called recursively */
long
recursionLevel
;
rotating_node_t
*
calls
;
}
ProfilerEntry
;
typedef
struct
_ProfilerContext
{
PyTime_t
t0
;
PyTime_t
subt
;
struct
_ProfilerContext
*
previous
;
ProfilerEntry
*
ctxEntry
;
}
ProfilerContext
;
typedef
struct
{
PyObject_HEAD
rotating_node_t
*
profilerEntries
;
ProfilerContext
*
currentProfilerContext
;
ProfilerContext
*
freelistProfilerContext
;
int
flags
;
PyObject
*
externalTimer
;
double
externalTimerUnit
;
int
tool_id
;
PyObject
*
missing
;
}
ProfilerObject
;
#define
ProfilerObject_CAST
(
op
) ((ProfilerObject *)(op))
#define
POF_ENABLED
0x001
#define
POF_SUBCALLS
0x002
#define
POF_BUILTINS
0x004
#define
POF_EXT_TIMER
0x008
#define
POF_NOMEMORY
0x100
/*[clinic input]
module _lsprof
class _lsprof.Profiler "ProfilerObject *" "&ProfilerType"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e349ac952152f336]*/
#include
"clinic/_lsprof.c.h"
typedef
struct
{
PyTypeObject
*
profiler_type
;
PyTypeObject
*
stats_entry_type
;
PyTypeObject
*
stats_subentry_type
;
}
_lsprof_state
;
static
inline
_lsprof_state
*
_lsprof_get_state
(
PyObject
*
module
)
{
void
*
state
=
PyModule_GetState
(
module
);
assert
(
state
!=
NULL
);
return
(
_lsprof_state
*
)
state
;
}
/*** External Timers ***/
static
PyTime_t
CallExternalTimer
(
ProfilerObject
*
pObj
)
{
PyObject
*
o
=
NULL
;
// External timer can do arbitrary things so we need a flag to prevent
// horrible things to happen
pObj
->
flags
|=
POF_EXT_TIMER
;
o
=
_PyObject_CallNoArgs
(
pObj
->
externalTimer
);
pObj
->
flags
&= ~
POF_EXT_TIMER
;
if
(
o
==
NULL
) {
PyErr_FormatUnraisable
(
"Exception ignored while calling "
"_lsprof timer %R"
,
pObj
->
externalTimer
);
return
0
;
}
PyTime_t
result
;
int
err
;
if
(
pObj
->
externalTimerUnit
>
0.0
) {
/* interpret the result as an integer that will be scaled
in profiler_getstats() */
err
=
PyLong_AsInt64
(
o
,
&
result
);
}
else
{
/* interpret the result as a double measured in seconds.
As the profiler works with PyTime_t internally
we convert it to a large integer */
err
=
_PyTime_FromSecondsObject
(
&
result
,
o
,
_PyTime_ROUND_FLOOR
);
}
Py_DECREF
(
o
);
if
(
err
<
0
) {
PyErr_FormatUnraisable
(
"Exception ignored while calling "
"_lsprof timer %R"
,
pObj
->
externalTimer
);
return
0
;
}
return
result
;
}
static
inline
PyTime_t
call_timer
(
ProfilerObject
*
pObj
)
{
if
(
pObj
->
externalTimer
!=
NULL
) {
return
CallExternalTimer
(
pObj
);
}
else
{
PyTime_t
t
;
(
void
)
PyTime_PerfCounterRaw
(
&
t
);
return
t
;
}
}
/*** ProfilerObject ***/
static
PyObject
*
normalizeUserObj
(
PyObject
*
obj
)
{
PyCFunctionObject
*
fn
;
if
(!
PyCFunction_Check
(
obj
)) {
return
Py_NewRef
(
obj
);
}
/* Replace built-in function objects with a descriptive string
because of built-in methods -- keeping a reference to
__self__ is probably not a good idea. */
fn
=
(
PyCFunctionObject
*
)
obj
;
if
(
fn
->
m_self
==
NULL
) {
/* built-in function: look up the module name */
PyObject
*
mod
=
fn
->
m_module
;
PyObject
*
modname
=
NULL
;
if
(
mod
!=
NULL
) {
if
(
PyUnicode_Check
(
mod
)) {
modname
=
Py_NewRef
(
mod
);
}
else
if
(
PyModule_Check
(
mod
)) {
modname
=
PyModule_GetNameObject
(
mod
);
if
(
modname
==
NULL
)
PyErr_Clear
();
}
}
if
(
modname
!=
NULL
) {
if
(!
_PyUnicode_EqualToASCIIString
(
modname
,
"builtins"
)) {
PyObject
*
result
;
result
=
PyUnicode_FromFormat
(
"<%U.%s>"
,
modname
,
fn
->
m_ml
->
ml_name
);
Py_DECREF
(
modname
);
return
result
;
}
Py_DECREF
(
modname
);
}
return
PyUnicode_FromFormat
(
"<%s>"
,
fn
->
m_ml
->
ml_name
);
}
else
{
/* built-in method: try to return
repr(getattr(type(__self__), __name__))
*/
PyObject
*
self
=
fn
->
m_self
;
PyObject
*
name
=
PyUnicode_FromString
(
fn
->
m_ml
->
ml_name
);
PyObject
*
modname
=
fn
->
m_module
;
if
(
name
!=
NULL
) {
PyObject
*
mo
=
_PyType_LookupRef
(
Py_TYPE
(
self
),
name
);
Py_DECREF
(
name
);
if
(
mo
!=
NULL
) {
PyObject
*
res
=
PyObject_Repr
(
mo
);
Py_DECREF
(
mo
);
if
(
res
!=
NULL
)
return
res
;
}
}
/* Otherwise, use __module__ */
PyErr_Clear
();
if
(
modname
!=
NULL
&&
PyUnicode_Check
(
modname
))
return
PyUnicode_FromFormat
(
"<built-in method %S.%s>"
,
modname
,
fn
->
m_ml
->
ml_name
);
else
return
PyUnicode_FromFormat
(
"<built-in method %s>"
,
fn
->
m_ml
->
ml_name
);
}
}
static
ProfilerEntry
*
newProfilerEntry
(
ProfilerObject
*
pObj
,
void
*
key
,
PyObject
*
userObj
)
{
ProfilerEntry
*
self
;
self
=
(
ProfilerEntry
*
)
PyMem_Malloc
(
sizeof
(
ProfilerEntry
));
if
(
self
==
NULL
) {
pObj
->
flags
|=
POF_NOMEMORY
;
return
NULL
;
}
userObj
=
normalizeUserObj
(
userObj
);
if
(
userObj
==
NULL
) {
PyErr_Clear
();
PyMem_Free
(
self
);
pObj
->
flags
|=
POF_NOMEMORY
;
return
NULL
;
}
self
->
header
.
key
=
key
;
self
->
userObj
=
userObj
;
self
->
tt
=
0
;
self
->
it
=
0
;
self
->
callcount
=
0
;
self
->
recursivecallcount
=
0
;
self
->
recursionLevel
=
0
;
self
->
calls
=
EMPTY_ROTATING_TREE
;
RotatingTree_Add
(
&
pObj
->
profilerEntries
,
&
self
->
header
);
return
self
;
}
static
ProfilerEntry
*
getEntry
(
ProfilerObject
*
pObj
,
void
*
key
)
{
return
(
ProfilerEntry
*
)
RotatingTree_Get
(
&
pObj
->
profilerEntries
,
key
);
}
static
ProfilerSubEntry
*
getSubEntry
(
ProfilerObject
*
pObj
,
ProfilerEntry
*
caller
,
ProfilerEntry
*
entry
)
{
return
(
ProfilerSubEntry
*
)
RotatingTree_Get
(
&
caller
->
calls
,
(
void
*
)
entry
);
}
static
ProfilerSubEntry
*
newSubEntry
(
ProfilerObject
*
pObj
,
ProfilerEntry
*
caller
,
ProfilerEntry
*
entry
)
{
ProfilerSubEntry
*
self
;
self
=
(
ProfilerSubEntry
*
)
PyMem_Malloc
(
sizeof
(
ProfilerSubEntry
));
if
(
self
==
NULL
) {
pObj
->
flags
|=
POF_NOMEMORY
;
return
NULL
;
}
self
->
header
.
key
=
(
void
*
)
entry
;
self
->
tt
=
0
;
self
->
it
=
0
;
self
->
callcount
=
0
;
self
->
recursivecallcount
=
0
;
self
->
recursionLevel
=
0
;
RotatingTree_Add
(
&
caller
->
calls
,
&
self
->
header
);
return
self
;
}
static
int
freeSubEntry
(
rotating_node_t
*
header
,
void
*
arg
)
{
ProfilerSubEntry
*
subentry
=
(
ProfilerSubEntry
*
)
header
;
PyMem_Free
(
subentry
);
return
0
;
}
static
int
freeEntry
(
rotating_node_t
*
header
,
void
*
arg
)
{
ProfilerEntry
*
entry
=
(
ProfilerEntry
*
)
header
;
RotatingTree_Enum
(
entry
->
calls
,
freeSubEntry
,
NULL
);
Py_DECREF
(
entry
->
userObj
);
PyMem_Free
(
entry
);
return
0
;
}
static
void
clearEntries
(
ProfilerObject
*
pObj
)
{
RotatingTree_Enum
(
pObj
->
profilerEntries
,
freeEntry
,
NULL
);
pObj
->
profilerEntries
=
EMPTY_ROTATING_TREE
;
/* release the memory hold by the ProfilerContexts */
while
(
pObj
->
currentProfilerContext
) {
ProfilerContext
*
pContext
=
pObj
->
currentProfilerContext
;
pObj
->
currentProfilerContext
=
pContext
->
previous
;
PyMem_Free
(
pContext
);
}
while
(
pObj
->
freelistProfilerContext
) {
ProfilerContext
*
c
=
pObj
->
freelistProfilerContext
;
pObj
->
freelistProfilerContext
=
c
->
previous
;
PyMem_Free
(
c
);
}
pObj
->
freelistProfilerContext
=
NULL
;
}
static
void
initContext
(
ProfilerObject
*
pObj
,
ProfilerContext
*
self
,
ProfilerEntry
*
entry
)
{
self
->
ctxEntry
=
entry
;
self
->
subt
=
0
;
self
->
previous
=
pObj
->
currentProfilerContext
;
pObj
->
currentProfilerContext
=
self
;
++
entry
->
recursionLevel
;
if
((
pObj
->
flags
&
POF_SUBCALLS
)
&&
self
->
previous
) {
/* find or create an entry for me in my caller's entry */
ProfilerEntry
*
caller
=
self
->
previous
->
ctxEntry
;
ProfilerSubEntry
*
subentry
=
getSubEntry
(
pObj
,
caller
,
entry
);
if
(
subentry
==
NULL
)
subentry
=
newSubEntry
(
pObj
,
caller
,
entry
);
if
(
subentry
)
++
subentry
->
recursionLevel
;
}
self
->
t0
=
call_timer
(
pObj
);
}
static
void
Stop
(
ProfilerObject
*
pObj
,
ProfilerContext
*
self
,
ProfilerEntry
*
entry
)
{
PyTime_t
tt
=
call_timer
(
pObj
)
-
self
->
t0
;
PyTime_t
it
=
tt
-
self
->
subt
;
if
(
self
->
previous
)
self
->
previous
->
subt
+=
tt
;
pObj
->
currentProfilerContext
=
self
->
previous
;
if
(
--
entry
->
recursionLevel
==
0
)
entry
->
tt
+=
tt
;
else
++
entry
->
recursivecallcount
;
entry
->
it
+=
it
;
entry
->
callcount
++
;
if
((
pObj
->
flags
&
POF_SUBCALLS
)
&&
self
->
previous
) {
/* find or create an entry for me in my caller's entry */
ProfilerEntry
*
caller
=
self
->
previous
->
ctxEntry
;
ProfilerSubEntry
*
subentry
=
getSubEntry
(
pObj
,
caller
,
entry
);
if
(
subentry
) {
if
(
--
subentry
->
recursionLevel
==
0
)
subentry
->
tt
+=
tt
;
else
++
subentry
->
recursivecallcount
;
subentry
->
it
+=
it
;
++
subentry
->
callcount
;
}
}
}
static
void
ptrace_enter_call
(
PyObject
*
self
,
void
*
key
,
PyObject
*
userObj
)
{
/* entering a call to the function identified by 'key'
(which can be a PyCodeObject or a PyMethodDef pointer) */
ProfilerObject
*
pObj
=
(
ProfilerObject
*
)
self
;
ProfilerEntry
*
profEntry
;
ProfilerContext
*
pContext
;
/* Events raised by the external timer must be ignored: it can run
arbitrary code while a context is still being unwound. */
if
(
pObj
->
flags
&
POF_EXT_TIMER
) {
return
;
}
/* In the case of entering a generator expression frame via a
* throw (gen_send_ex(.., 1)), we may already have an
* Exception set here. We must not mess around with this
* exception, and some of the code under here assumes that
* PyErr_* is its own to mess around with, so we have to
* save and restore any current exception. */
PyObject
*
exc
=
PyErr_GetRaisedException
();
profEntry
=
getEntry
(
pObj
,
key
);
if
(
profEntry
==
NULL
) {
profEntry
=
newProfilerEntry
(
pObj
,
key
,
userObj
);
if
(
profEntry
==
NULL
)
goto
restorePyerr
;
}
/* grab a ProfilerContext out of the free list */
pContext
=
pObj
->
freelistProfilerContext
;
if
(
pContext
) {
pObj
->
freelistProfilerContext
=
pContext
->
previous
;
}
else
{
/* free list exhausted, allocate a new one */
pContext
=
(
ProfilerContext
*
)
PyMem_Malloc
(
sizeof
(
ProfilerContext
));
if
(
pContext
==
NULL
) {
pObj
->
flags
|=
POF_NOMEMORY
;
goto
restorePyerr
;
}
}
initContext
(
pObj
,
pContext
,
profEntry
);
restorePyerr
:
PyErr_SetRaisedException
(
exc
);
}
static
void
ptrace_leave_call
(
PyObject
*
self
,
void
*
key
)
{
/* leaving a call to the function identified by 'key' */
ProfilerObject
*
pObj
=
(
ProfilerObject
*
)
self
;
ProfilerEntry
*
profEntry
;
ProfilerContext
*
pContext
;
if
(
pObj
->
flags
&
POF_EXT_TIMER
) {
return
;
}
pContext
=
pObj
->
currentProfilerContext
;
if
(
pContext
==
NULL
)
return
;
profEntry
=
getEntry
(
pObj
,
key
);
if
(
profEntry
) {
Stop
(
pObj
,
pContext
,
profEntry
);
}
else
{
pObj
->
currentProfilerContext
=
pContext
->
previous
;
}
/* put pContext into the free list */
pContext
->
previous
=
pObj
->
freelistProfilerContext
;
pObj
->
freelistProfilerContext
=
pContext
;
}
static
int
pending_exception
(
ProfilerObject
*
pObj
)
{
if
(
pObj
->
flags
&
POF_NOMEMORY
) {
pObj
->
flags
-=
POF_NOMEMORY
;
PyErr_SetString
(
PyExc_MemoryError
,
"memory was exhausted while profiling"
);
return
-1
;
}
return
0
;
}
/************************************************************/
static
PyStructSequence_Field
profiler_entry_fields
[]
=
{
{
"code"
,
"code object or built-in function name"
},
{
"callcount"
,
"how many times this was called"
},
{
"reccallcount"
,
"how many times called recursively"
},
{
"totaltime"
,
"total time in this entry"
},
{
"inlinetime"
,
"inline time in this entry (not in subcalls)"
},
{
"calls"
,
"details of the calls"
},
{
0
}
};
static
PyStructSequence_Field
profiler_subentry_fields
[]
=
{
{
"code"
,
"called code object or built-in function name"
},
{
"callcount"
,
"how many times this is called"
},
{
"reccallcount"
,
"how many times this is called recursively"
},
{
"totaltime"
,
"total time spent in this call"
},
{
"inlinetime"
,
"inline time (not in further subcalls)"
},
{
0
}
};
static
PyStructSequence_Desc
profiler_entry_desc
=
{
.
name
=
"_lsprof.profiler_entry"
,
.
fields
=
profiler_entry_fields
,
.
doc
=
NULL
,
.
n_in_sequence
=
6
};
static
PyStructSequence_Desc
profiler_subentry_desc
=
{
.
name
=
"_lsprof.profiler_subentry"
,
.
fields
=
profiler_subentry_fields
,
.
doc
=
NULL
,
.
n_in_sequence
=
5
};
typedef
struct
{
PyObject
*
list
;
PyObject
*
sublist
;
double
factor
;
_lsprof_state
*
state
;
}
statscollector_t
;
static
int
statsForSubEntry
(
rotating_node_t
*
node
,
void
*
arg
)
{
ProfilerSubEntry
*
sentry
=
(
ProfilerSubEntry
*
)
node
;
statscollector_t
*
collect
=
(
statscollector_t
*
)
arg
;
ProfilerEntry
*
entry
=
(
ProfilerEntry
*
)
sentry
->
header
.
key
;
int
err
;
PyObject
*
sinfo
;
sinfo
=
PyObject_CallFunction
((
PyObject
*
)
collect
->
state
->
stats_subentry_type
,
"((Olldd))"
,
entry
->
userObj
,
sentry
->
callcount
,
sentry
->
recursivecallcount
,
collect
->
factor
*
sentry
->
tt
,
collect
->
factor
*
sentry
->
it
);
if
(
sinfo
==
NULL
)
return
-1
;
err
=
PyList_Append
(
collect
->
sublist
,
sinfo
);
Py_DECREF
(
sinfo
);
return
err
;
}
static
int
statsForEntry
(
rotating_node_t
*
node
,
void
*
arg
)
{
ProfilerEntry
*
entry
=
(
ProfilerEntry
*
)
node
;
statscollector_t
*
collect
=
(
statscollector_t
*
)
arg
;
PyObject
*
info
;
int
err
;
if
(
entry
->
callcount
==
0
)
return
0
;
/* skip */
if
(
entry
->
calls
!=
EMPTY_ROTATING_TREE
) {
collect
->
sublist
=
PyList_New
(
0
);
if
(
collect
->
sublist
==
NULL
)
return
-1
;
if
(
RotatingTree_Enum
(
entry
->
calls
,
statsForSubEntry
,
collect
)
!=
0
) {
Py_DECREF
(
collect
->
sublist
);
return
-1
;
}
}
else
{
collect
->
sublist
=
Py_NewRef
(
Py_None
);
}
info
=
PyObject_CallFunction
((
PyObject
*
)
collect
->
state
->
stats_entry_type
,
"((OllddO))"
,
entry
->
userObj
,
entry
->
callcount
,
entry
->
recursivecallcount
,
collect
->
factor
*
entry
->
tt
,
collect
->
factor
*
entry
->
it
,
collect
->
sublist
);
Py_DECREF
(
collect
->
sublist
);
if
(
info
==
NULL
)
return
-1
;
err
=
PyList_Append
(
collect
->
list
,
info
);
Py_DECREF
(
info
);
return
err
;
}
/*[clinic input]
@critical_section
_lsprof.Profiler.getstats
cls: defining_class
list of profiler_entry objects.
getstats() -> list of profiler_entry objects
Return all information collected by the profiler.
Each profiler_entry is a tuple-like object with the
following attributes:
code code object
callcount how many times this was called
reccallcount how many times called recursively
totaltime total time in this entry
inlinetime inline time in this entry (not in subcalls)
calls details of the calls
The calls attribute is either None or a list of
profiler_subentry objects:
code called code object
callcount how many times this is called
reccallcount how many times this is called recursively
totaltime total time spent in this call
inlinetime inline time (not in further subcalls)
[clinic start generated code]*/
static
PyObject
*
_lsprof_Profiler_getstats_impl
(
ProfilerObject
*
self
,
PyTypeObject
*
cls
)
/*[clinic end generated code: output=1806ef720019ee03 input=3dc69eb85ed73d91]*/
{
statscollector_t
collect
;
collect
.
state
=
_PyType_GetModuleState
(
cls
);
if
(
pending_exception
(
self
)) {
return
NULL
;
}
if
(!
self
->
externalTimer
||
self
->
externalTimerUnit
==
0.0
) {
PyTime_t
onesec
=
_PyTime_FromSeconds
(
1
);
collect
.
factor
=
(
double
)
1
/
onesec
;
}
else
{
collect
.
factor
=
self
->
externalTimerUnit
;
}
collect
.
list
=
PyList_New
(
0
);
if
(
collect
.
list
==
NULL
)
return
NULL
;
if
(
RotatingTree_Enum
(
self
->
profilerEntries
,
statsForEntry
,
&
collect
)
!=
0
) {
Py_DECREF
(
collect
.
list
);
return
NULL
;
}
return
collect
.
list
;
}
static
int
setSubcalls
(
ProfilerObject
*
pObj
,
int
nvalue
)
{
if
(
nvalue
==
0
)
pObj
->
flags
&= ~
POF_SUBCALLS
;
else
if
(
nvalue
>
0
)
pObj
->
flags
|=
POF_SUBCALLS
;
return
0
;
}
static
int
setBuiltins
(
ProfilerObject
*
pObj
,
int
nvalue
)
{
if
(
nvalue
==
0
)
pObj
->
flags
&= ~
POF_BUILTINS
;
else
if
(
nvalue
>
0
) {
pObj
->
flags
|=
POF_BUILTINS
;
}
return
0
;
}
/*[clinic input]
@critical_section
_lsprof.Profiler._pystart_callback
code: object
instruction_offset: object
/
[clinic start generated code]*/
static
PyObject
*
_lsprof_Profiler__pystart_callback_impl
(
ProfilerObject
*
self
,
PyObject
*
code
,
PyObject
*
instruction_offset
)
/*[clinic end generated code: output=5fec8b7ad5ed25e8 input=b61a0e79cf1f8499]*/
{
ptrace_enter_call
((
PyObject
*
)
self
, (
void
*
)
code
,
code
);
Py_RETURN_NONE
;
}
/*[clinic input]
@critical_section
_lsprof.Profiler._pythrow_callback
code: object
instruction_offset: object
exception: object
/
[clinic start generated code]*/
static
PyObject
*
_lsprof_Profiler__pythrow_callback_impl
(
ProfilerObject
*
self
,
PyObject
*
code
,
PyObject
*
instruction_offset
,
PyObject
*
exception
)
/*[clinic end generated code: output=0a32988919dfb94c input=60c7f272206d3758]*/
{
ptrace_enter_call
((
PyObject
*
)
self
, (
void
*
)
code
,
code
);
Py_RETURN_NONE
;
}
/*[clinic input]
@critical_section
_lsprof.Profiler._pyreturn_callback
code: object
instruction_offset: object
retval: object
/
[clinic start generated code]*/
static
PyObject
*
_lsprof_Profiler__pyreturn_callback_impl
(
ProfilerObject
*
self
,
PyObject
*
code
,
PyObject
*
instruction_offset
,
PyObject
*
retval
)
/*[clinic end generated code: output=9e2f6fc1b882c51e input=0ddcc1ec53faa928]*/
{
ptrace_leave_call
((
PyObject
*
)
self
, (
void
*
)
code
);
Py_RETURN_NONE
;
}
PyObject
*
get_cfunc_from_callable
(
PyObject
*
callable
,
PyObject
*
self_arg
,
PyObject
*
missing
)
{
// return a new reference
if
(
PyCFunction_Check
(
callable
)) {
Py_INCREF
(
callable
);
return
(
PyObject
*
)((
PyCFunctionObject
*
)
callable
);
}
if
(
Py_TYPE
(
callable
)
==
&
PyMethodDescr_Type
) {
/* For backwards compatibility need to
* convert to builtin method */
/* If no arg, skip */
if
(
self_arg
==
missing
) {
return
NULL
;
}
PyObject
*
meth
=
Py_TYPE
(
callable
)
->
tp_descr_get
(
callable
,
self_arg
, (
PyObject
*
)
Py_TYPE
(
self_arg
));
if
(
meth
==
NULL
) {
PyErr_Clear
();
return
NULL
;
}
if
(
PyCFunction_Check
(
meth
)) {
return
(
PyObject
*
)((
PyCFunctionObject
*
)
meth
);
}
Py_DECREF
(
meth
);
}
return
NULL
;
}
/*[clinic input]
@critical_section
_lsprof.Profiler._ccall_callback
code: object
instruction_offset: object
callable: object
self_arg: object
/
[clinic start generated code]*/
static
PyObject
*
_lsprof_Profiler__ccall_callback_impl
(
ProfilerObject
*
self
,
PyObject
*
code
,
PyObject
*
instruction_offset
,
PyObject
*
callable
,
PyObject
*
self_arg
)
/*[clinic end generated code: output=152db83cabd18cad input=2fc1e0630ee5e32b]*/
{
if
(
self
->
flags
&
POF_BUILTINS
) {
PyObject
*
cfunc
=
get_cfunc_from_callable
(
callable
,
self_arg
,
self
->
missing
);
if
(
cfunc
) {
ptrace_enter_call
((
PyObject
*
)
self
,
((
PyCFunctionObject
*
)
cfunc
)
->
m_ml
,
cfunc
);
Py_DECREF
(
cfunc
);
}
}
Py_RETURN_NONE
;
}
/*[clinic input]
@critical_section
_lsprof.Profiler._creturn_callback
code: object
instruction_offset: object
callable: object
self_arg: object
/
[clinic start generated code]*/
static
PyObject
*
_lsprof_Profiler__creturn_callback_impl
(
ProfilerObject
*
self
,
PyObject
*
code
,
PyObject
*
instruction_offset
,
PyObject
*
callable
,
PyObject
*
self_arg
)
/*[clinic end generated code: output=1e886dde8fed8fb0 input=bdc246d6b5b8714a]*/
{
if
(
self
->
flags
&
POF_BUILTINS
) {
PyObject
*
cfunc
=
get_cfunc_from_callable
(
callable
,
self_arg
,
self
->
missing
);
if
(
cfunc
) {
ptrace_leave_call
((
PyObject
*
)
self
,
((
PyCFunctionObject
*
)
cfunc
)
->
m_ml
);
Py_DECREF
(
cfunc
);
}
}
Py_RETURN_NONE
;
}
static
const
struct
{
int
event
;
const
char
*
callback_method
;
}
callback_table
[]
=
{
{
PY_MONITORING_EVENT_PY_START
,
"_pystart_callback"
},
{
PY_MONITORING_EVENT_PY_RESUME
,
"_pystart_callback"
},
{
PY_MONITORING_EVENT_PY_THROW
,
"_pythrow_callback"
},
{
PY_MONITORING_EVENT_PY_RETURN
,
"_pyreturn_callback"
},
{
PY_MONITORING_EVENT_PY_YIELD
,
"_pyreturn_callback"
},
{
PY_MONITORING_EVENT_PY_UNWIND
,
"_pyreturn_callback"
},
{
PY_MONITORING_EVENT_CALL
,
"_ccall_callback"
},
{
PY_MONITORING_EVENT_C_RETURN
,
"_creturn_callback"
},
{
PY_MONITORING_EVENT_C_RAISE
,
"_creturn_callback"
},
{
0
,
NULL
}
};
/*[clinic input]
@critical_section
_lsprof.Profiler.enable
subcalls: bool = True
If True, also records for each function
statistics separated according to its current caller.
builtins: bool = True
If True, records the time spent in
built-in functions separately from their caller.
Start collecting profiling information.
[clinic start generated code]*/
static
PyObject
*
_lsprof_Profiler_enable_impl
(
ProfilerObject
*
self
,
int
subcalls
,
int
builtins
)
/*[clinic end generated code: output=1e747f9dc1edd571 input=0b88115b1c796173]*/
{
int
all_events
=
0
;
if
(
setSubcalls
(
self
,
subcalls
)
<
0
||
setBuiltins
(
self
,
builtins
)
<
0
) {
return
NULL
;
}
PyObject
*
monitoring
=
PySys_GetAttrString
(
"monitoring"
);
if
(!
monitoring
) {
return
NULL
;
}
PyObject
*
check
=
PyObject_CallMethod
(
monitoring
,
"use_tool_id"
,
"is"
,
self
->
tool_id
,
"cProfile"
);
if
(
check
==
NULL
) {
goto
error
;
}
Py_DECREF
(
check
);
for
(
int
i
=
0
;
callback_table
[
i
].
callback_method
;
i
++
) {
int
event
=
(
1
<<
callback_table
[
i
].
event
);
PyObject
*
callback
=
PyObject_GetAttrString
((
PyObject
*
)
self
,
callback_table
[
i
].
callback_method
);
if
(!
callback
) {
goto
error
;
}
PyObject
*
register_result
=
PyObject_CallMethod
(
monitoring
,
"register_callback"
,
"iiO"
,
self
->
tool_id
,
event
,
callback
);
Py_DECREF
(
callback
);
if
(
register_result
==
NULL
) {
goto
error
;
}
Py_DECREF
(
register_result
);
all_events
|=
event
;
}
PyObject
*
event_result
=
PyObject_CallMethod
(
monitoring
,
"set_events"
,
"ii"
,
self
->
tool_id
,
all_events
);
if
(
event_result
==
NULL
) {
goto
error
;
}
Py_DECREF
(
event_result
);
Py_DECREF
(
monitoring
);
self
->
flags
|=
POF_ENABLED
;
Py_RETURN_NONE
;
error
:
Py_DECREF
(
monitoring
);
return
NULL
;
}
static
void
flush_unmatched
(
ProfilerObject
*
pObj
)
{
while
(
pObj
->
currentProfilerContext
) {
ProfilerContext
*
pContext
=
pObj
->
currentProfilerContext
;
ProfilerEntry
*
profEntry
=
pContext
->
ctxEntry
;
if
(
profEntry
)
Stop
(
pObj
,
pContext
,
profEntry
);
else
pObj
->
currentProfilerContext
=
pContext
->
previous
;
if
(
pContext
)
PyMem_Free
(
pContext
);
}
}
/*[clinic input]
@critical_section
_lsprof.Profiler.disable
Stop collecting profiling information.
[clinic start generated code]*/
static
PyObject
*
_lsprof_Profiler_disable_impl
(
ProfilerObject
*
self
)
/*[clinic end generated code: output=838cffef7f651870 input=f7e4787cae20f7f6]*/
{
if
(
self
->
flags
&
POF_EXT_TIMER
) {
PyErr_SetString
(
PyExc_RuntimeError
,
"cannot disable profiler in external timer"
);
return
NULL
;
}
if
(
self
->
flags
&
POF_ENABLED
) {
PyObject
*
result
=
NULL
;
PyObject
*
monitoring
=
PySys_GetAttrString
(
"monitoring"
);
if
(!
monitoring
) {
return
NULL
;
}
for
(
int
i
=
0
;
callback_table
[
i
].
callback_method
;
i
++
) {
result
=
PyObject_CallMethod
(
monitoring
,
"register_callback"
,
"iiO"
,
self
->
tool_id
,
(
1
<<
callback_table
[
i
].
event
),
Py_None
);
if
(!
result
) {
Py_DECREF
(
monitoring
);
return
NULL
;
}
Py_DECREF
(
result
);
}
result
=
PyObject_CallMethod
(
monitoring
,
"set_events"
,
"ii"
,
self
->
tool_id
,
0
);
if
(!
result
) {
Py_DECREF
(
monitoring
);
return
NULL
;
}
Py_DECREF
(
result
);
result
=
PyObject_CallMethod
(
monitoring
,
"free_tool_id"
,
"i"
,
self
->
tool_id
);
if
(!
result
) {
Py_DECREF
(
monitoring
);
return
NULL
;
}
Py_DECREF
(
result
);
Py_DECREF
(
monitoring
);
self
->
flags
&= ~
POF_ENABLED
;
flush_unmatched
(
self
);
}
if
(
pending_exception
(
self
)) {
return
NULL
;
}
Py_RETURN_NONE
;
}
/*[clinic input]
@critical_section
_lsprof.Profiler.clear
Clear all profiling information collected so far.
[clinic start generated code]*/
static
PyObject
*
_lsprof_Profiler_clear_impl
(
ProfilerObject
*
self
)
/*[clinic end generated code: output=dd1c668fb84b1335 input=4aab219d5d7a9bec]*/
{
if
(
self
->
flags
&
POF_EXT_TIMER
) {
PyErr_SetString
(
PyExc_RuntimeError
,
"cannot clear profiler in external timer"
);
return
NULL
;
}
clearEntries
(
self
);
Py_RETURN_NONE
;
}
static
int
profiler_traverse
(
PyObject
*
op
,
visitproc
visit
,
void
*
arg
)
{
ProfilerObject
*
self
=
ProfilerObject_CAST
(
op
);
Py_VISIT
(
Py_TYPE
(
op
));
Py_VISIT
(
self
->
externalTimer
);
Py_VISIT
(
self
->
missing
);
return
0
;
}
static
void
profiler_dealloc
(
PyObject
*
op
)
{
ProfilerObject
*
self
=
ProfilerObject_CAST
(
op
);
PyObject_GC_UnTrack
(
self
);
if
(
self
->
flags
&
POF_ENABLED
) {
PyThreadState
*
tstate
=
_PyThreadState_GET
();
if
(
_PyEval_SetProfile
(
tstate
,
NULL
,
NULL
)
<
0
) {
PyErr_FormatUnraisable
(
"Exception ignored while "
"destroying _lsprof profiler"
);
}
}
/* Drop the external timer before flushing: it is Python code, and the
profiler can be deallocated by the garbage collector. */
Py_CLEAR
(
self
->
externalTimer
);
flush_unmatched
(
self
);
clearEntries
(
self
);
Py_XDECREF
(
self
->
missing
);
PyTypeObject
*
tp
=
Py_TYPE
(
self
);
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL