annotate Modules/_lsprof.c @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 7f1f9e18cdfa
children d5fbda03c603
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
1 #include "Python.h"
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
2 #include "compile.h"
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
3 #include "frameobject.h"
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
4 #include "structseq.h"
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
5 #include "rotatingtree.h"
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
6
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
7 #if !defined(HAVE_LONG_LONG)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
8 #error "This module requires long longs!"
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
9 #endif
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
10
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
11 /*** Selection of a high-precision timer ***/
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
12
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
13 #ifdef MS_WINDOWS
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
14
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
15 #include <windows.h>
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
16
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
17 static PY_LONG_LONG
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
18 hpTimer(void)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
19 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
20 LARGE_INTEGER li;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
21 QueryPerformanceCounter(&li);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
22 return li.QuadPart;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
23 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
24
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
25 static double
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
26 hpTimerUnit(void)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
27 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
28 LARGE_INTEGER li;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
29 if (QueryPerformanceFrequency(&li))
36471
165389b14d6b The default timer unit was incorrectly measured in milliseconds instead
Armin Rigo <arigo@tunes.org>
parents: 36459
diff changeset
30 return 1.0 / li.QuadPart;
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
31 else
36471
165389b14d6b The default timer unit was incorrectly measured in milliseconds instead
Armin Rigo <arigo@tunes.org>
parents: 36459
diff changeset
32 return 0.000001; /* unlikely */
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
33 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
34
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
35 #else /* !MS_WINDOWS */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
36
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
37 #ifndef HAVE_GETTIMEOFDAY
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
38 #error "This module requires gettimeofday() on non-Windows platforms!"
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
39 #endif
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
40
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
41 #if (defined(PYOS_OS2) && defined(PYCC_GCC))
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
42 #include <sys/time.h>
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
43 #else
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
44 #include <sys/resource.h>
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
45 #include <sys/times.h>
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
46 #endif
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
47
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
48 static PY_LONG_LONG
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
49 hpTimer(void)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
50 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
51 struct timeval tv;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
52 PY_LONG_LONG ret;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
53 #ifdef GETTIMEOFDAY_NO_TZ
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
54 gettimeofday(&tv);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
55 #else
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
56 gettimeofday(&tv, (struct timezone *)NULL);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
57 #endif
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
58 ret = tv.tv_sec;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
59 ret = ret * 1000000 + tv.tv_usec;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
60 return ret;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
61 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
62
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
63 static double
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
64 hpTimerUnit(void)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
65 {
36471
165389b14d6b The default timer unit was incorrectly measured in milliseconds instead
Armin Rigo <arigo@tunes.org>
parents: 36459
diff changeset
66 return 0.000001;
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
67 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
68
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
69 #endif /* MS_WINDOWS */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
70
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
71 /************************************************************/
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
72 /* Written by Brett Rosen and Ted Czotter */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
73
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
74 struct _ProfilerEntry;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
75
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
76 /* represents a function called from another function */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
77 typedef struct _ProfilerSubEntry {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
78 rotating_node_t header;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
79 PY_LONG_LONG tt;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
80 PY_LONG_LONG it;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
81 long callcount;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
82 long recursivecallcount;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
83 long recursionLevel;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
84 } ProfilerSubEntry;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
85
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
86 /* represents a function or user defined block */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
87 typedef struct _ProfilerEntry {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
88 rotating_node_t header;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
89 PyObject *userObj; /* PyCodeObject, or a descriptive str for builtins */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
90 PY_LONG_LONG tt; /* total time in this entry */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
91 PY_LONG_LONG it; /* inline time in this entry (not in subcalls) */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
92 long callcount; /* how many times this was called */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
93 long recursivecallcount; /* how many times called recursively */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
94 long recursionLevel;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
95 rotating_node_t *calls;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
96 } ProfilerEntry;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
97
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
98 typedef struct _ProfilerContext {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
99 PY_LONG_LONG t0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
100 PY_LONG_LONG subt;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
101 struct _ProfilerContext *previous;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
102 ProfilerEntry *ctxEntry;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
103 } ProfilerContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
104
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
105 typedef struct {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
106 PyObject_HEAD
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
107 rotating_node_t *profilerEntries;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
108 ProfilerContext *currentProfilerContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
109 ProfilerContext *freelistProfilerContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
110 int flags;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
111 PyObject *externalTimer;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
112 double externalTimerUnit;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
113 } ProfilerObject;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
114
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
115 #define POF_ENABLED 0x001
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
116 #define POF_SUBCALLS 0x002
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
117 #define POF_BUILTINS 0x004
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
118 #define POF_NOMEMORY 0x100
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
119
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
120 staticforward PyTypeObject PyProfiler_Type;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
121
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
122 #define PyProfiler_Check(op) PyObject_TypeCheck(op, &PyProfiler_Type)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43258
diff changeset
123 #define PyProfiler_CheckExact(op) (Py_TYPE(op) == &PyProfiler_Type)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
124
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
125 /*** External Timers ***/
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
126
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
127 #define DOUBLE_TIMER_PRECISION 4294967296.0
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
128 static PyObject *empty_tuple;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
129
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
130 static PY_LONG_LONG CallExternalTimer(ProfilerObject *pObj)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
131 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
132 PY_LONG_LONG result;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
133 PyObject *o = PyObject_Call(pObj->externalTimer, empty_tuple, NULL);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
134 if (o == NULL) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
135 PyErr_WriteUnraisable(pObj->externalTimer);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
136 return 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
137 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
138 if (pObj->externalTimerUnit > 0.0) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
139 /* interpret the result as an integer that will be scaled
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
140 in profiler_getstats() */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
141 result = PyLong_AsLongLong(o);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
142 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
143 else {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
144 /* interpret the result as a double measured in seconds.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
145 As the profiler works with PY_LONG_LONG internally
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
146 we convert it to a large integer */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
147 double val = PyFloat_AsDouble(o);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
148 /* error handling delayed to the code below */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
149 result = (PY_LONG_LONG) (val * DOUBLE_TIMER_PRECISION);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
150 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
151 Py_DECREF(o);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
152 if (PyErr_Occurred()) {
49358
7f1f9e18cdfa Fix a refleak introduced by r66677.
Brett Cannon <bcannon@gmail.com>
parents: 49335
diff changeset
153 PyErr_WriteUnraisable(pObj->externalTimer);
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
154 return 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
155 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
156 return result;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
157 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
158
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
159 #define CALL_TIMER(pObj) ((pObj)->externalTimer ? \
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
160 CallExternalTimer(pObj) : \
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
161 hpTimer())
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
162
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
163 /*** ProfilerObject ***/
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
164
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
165 static PyObject *
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
166 normalizeUserObj(PyObject *obj)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
167 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
168 PyCFunctionObject *fn;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
169 if (!PyCFunction_Check(obj)) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
170 Py_INCREF(obj);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
171 return obj;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
172 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
173 /* Replace built-in function objects with a descriptive string
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
174 because of built-in methods -- keeping a reference to
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
175 __self__ is probably not a good idea. */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
176 fn = (PyCFunctionObject *)obj;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
177
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
178 if (fn->m_self == NULL) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
179 /* built-in function: look up the module name */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
180 PyObject *mod = fn->m_module;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
181 char *modname;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
182 if (mod && PyString_Check(mod)) {
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
183 modname = PyString_AS_STRING(mod);
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
184 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
185 else if (mod && PyModule_Check(mod)) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
186 modname = PyModule_GetName(mod);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
187 if (modname == NULL) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
188 PyErr_Clear();
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
189 modname = "__builtin__";
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
190 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
191 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
192 else {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
193 modname = "__builtin__";
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
194 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
195 if (strcmp(modname, "__builtin__") != 0)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
196 return PyString_FromFormat("<%s.%s>",
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
197 modname,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
198 fn->m_ml->ml_name);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
199 else
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
200 return PyString_FromFormat("<%s>",
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
201 fn->m_ml->ml_name);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
202 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
203 else {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
204 /* built-in method: try to return
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
205 repr(getattr(type(__self__), __name__))
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
206 */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
207 PyObject *self = fn->m_self;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
208 PyObject *name = PyString_FromString(fn->m_ml->ml_name);
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
209 if (name != NULL) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43258
diff changeset
210 PyObject *mo = _PyType_Lookup(Py_TYPE(self), name);
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
211 Py_XINCREF(mo);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
212 Py_DECREF(name);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
213 if (mo != NULL) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
214 PyObject *res = PyObject_Repr(mo);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
215 Py_DECREF(mo);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
216 if (res != NULL)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
217 return res;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
218 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
219 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
220 PyErr_Clear();
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
221 return PyString_FromFormat("<built-in method %s>",
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
222 fn->m_ml->ml_name);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
223 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
224 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
225
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
226 static ProfilerEntry*
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
227 newProfilerEntry(ProfilerObject *pObj, void *key, PyObject *userObj)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
228 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
229 ProfilerEntry *self;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
230 self = (ProfilerEntry*) malloc(sizeof(ProfilerEntry));
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
231 if (self == NULL) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
232 pObj->flags |= POF_NOMEMORY;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
233 return NULL;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
234 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
235 userObj = normalizeUserObj(userObj);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
236 if (userObj == NULL) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
237 PyErr_Clear();
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
238 free(self);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
239 pObj->flags |= POF_NOMEMORY;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
240 return NULL;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
241 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
242 self->header.key = key;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
243 self->userObj = userObj;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
244 self->tt = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
245 self->it = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
246 self->callcount = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
247 self->recursivecallcount = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
248 self->recursionLevel = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
249 self->calls = EMPTY_ROTATING_TREE;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
250 RotatingTree_Add(&pObj->profilerEntries, &self->header);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
251 return self;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
252 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
253
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
254 static ProfilerEntry*
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
255 getEntry(ProfilerObject *pObj, void *key)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
256 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
257 return (ProfilerEntry*) RotatingTree_Get(&pObj->profilerEntries, key);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
258 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
259
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
260 static ProfilerSubEntry *
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
261 getSubEntry(ProfilerObject *pObj, ProfilerEntry *caller, ProfilerEntry* entry)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
262 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
263 return (ProfilerSubEntry*) RotatingTree_Get(&caller->calls,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
264 (void *)entry);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
265 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
266
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
267 static ProfilerSubEntry *
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
268 newSubEntry(ProfilerObject *pObj, ProfilerEntry *caller, ProfilerEntry* entry)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
269 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
270 ProfilerSubEntry *self;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
271 self = (ProfilerSubEntry*) malloc(sizeof(ProfilerSubEntry));
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
272 if (self == NULL) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
273 pObj->flags |= POF_NOMEMORY;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
274 return NULL;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
275 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
276 self->header.key = (void *)entry;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
277 self->tt = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
278 self->it = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
279 self->callcount = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
280 self->recursivecallcount = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
281 self->recursionLevel = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
282 RotatingTree_Add(&caller->calls, &self->header);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
283 return self;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
284 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
285
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
286 static int freeSubEntry(rotating_node_t *header, void *arg)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
287 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
288 ProfilerSubEntry *subentry = (ProfilerSubEntry*) header;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
289 free(subentry);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
290 return 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
291 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
292
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
293 static int freeEntry(rotating_node_t *header, void *arg)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
294 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
295 ProfilerEntry *entry = (ProfilerEntry*) header;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
296 RotatingTree_Enum(entry->calls, freeSubEntry, NULL);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
297 Py_DECREF(entry->userObj);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
298 free(entry);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
299 return 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
300 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
301
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
302 static void clearEntries(ProfilerObject *pObj)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
303 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
304 RotatingTree_Enum(pObj->profilerEntries, freeEntry, NULL);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
305 pObj->profilerEntries = EMPTY_ROTATING_TREE;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
306 /* release the memory hold by the free list of ProfilerContexts */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
307 while (pObj->freelistProfilerContext) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
308 ProfilerContext *c = pObj->freelistProfilerContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
309 pObj->freelistProfilerContext = c->previous;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
310 free(c);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
311 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
312 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
313
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
314 static void
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
315 initContext(ProfilerObject *pObj, ProfilerContext *self, ProfilerEntry *entry)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
316 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
317 self->ctxEntry = entry;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
318 self->subt = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
319 self->previous = pObj->currentProfilerContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
320 pObj->currentProfilerContext = self;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
321 ++entry->recursionLevel;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
322 if ((pObj->flags & POF_SUBCALLS) && self->previous) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
323 /* find or create an entry for me in my caller's entry */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
324 ProfilerEntry *caller = self->previous->ctxEntry;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
325 ProfilerSubEntry *subentry = getSubEntry(pObj, caller, entry);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
326 if (subentry == NULL)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
327 subentry = newSubEntry(pObj, caller, entry);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
328 if (subentry)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
329 ++subentry->recursionLevel;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
330 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
331 self->t0 = CALL_TIMER(pObj);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
332 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
333
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
334 static void
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
335 Stop(ProfilerObject *pObj, ProfilerContext *self, ProfilerEntry *entry)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
336 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
337 PY_LONG_LONG tt = CALL_TIMER(pObj) - self->t0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
338 PY_LONG_LONG it = tt - self->subt;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
339 if (self->previous)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
340 self->previous->subt += tt;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
341 pObj->currentProfilerContext = self->previous;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
342 if (--entry->recursionLevel == 0)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
343 entry->tt += tt;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
344 else
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
345 ++entry->recursivecallcount;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
346 entry->it += it;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
347 entry->callcount++;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
348 if ((pObj->flags & POF_SUBCALLS) && self->previous) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
349 /* find or create an entry for me in my caller's entry */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
350 ProfilerEntry *caller = self->previous->ctxEntry;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
351 ProfilerSubEntry *subentry = getSubEntry(pObj, caller, entry);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
352 if (subentry) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
353 if (--subentry->recursionLevel == 0)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
354 subentry->tt += tt;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
355 else
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
356 ++subentry->recursivecallcount;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
357 subentry->it += it;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
358 ++subentry->callcount;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
359 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
360 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
361 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
362
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
363 static void
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
364 ptrace_enter_call(PyObject *self, void *key, PyObject *userObj)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
365 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
366 /* entering a call to the function identified by 'key'
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
367 (which can be a PyCodeObject or a PyMethodDef pointer) */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
368 ProfilerObject *pObj = (ProfilerObject*)self;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
369 ProfilerEntry *profEntry;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
370 ProfilerContext *pContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
371
43258
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
372 /* In the case of entering a generator expression frame via a
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
373 * throw (gen_send_ex(.., 1)), we may already have an
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
374 * Exception set here. We must not mess around with this
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
375 * exception, and some of the code under here assumes that
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
376 * PyErr_* is its own to mess around with, so we have to
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
377 * save and restore any current exception. */
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
378 PyObject *last_type, *last_value, *last_tb;
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
379 PyErr_Fetch(&last_type, &last_value, &last_tb);
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
380
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
381 profEntry = getEntry(pObj, key);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
382 if (profEntry == NULL) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
383 profEntry = newProfilerEntry(pObj, key, userObj);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
384 if (profEntry == NULL)
43258
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
385 goto restorePyerr;
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
386 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
387 /* grab a ProfilerContext out of the free list */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
388 pContext = pObj->freelistProfilerContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
389 if (pContext) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
390 pObj->freelistProfilerContext = pContext->previous;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
391 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
392 else {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
393 /* free list exhausted, allocate a new one */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
394 pContext = (ProfilerContext*)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
395 malloc(sizeof(ProfilerContext));
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
396 if (pContext == NULL) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
397 pObj->flags |= POF_NOMEMORY;
43258
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
398 goto restorePyerr;
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
399 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
400 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
401 initContext(pObj, pContext, profEntry);
43258
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
402
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
403 restorePyerr:
0a31d1c45a89 Patch #1733973 by peaker:
Armin Rigo <arigo@tunes.org>
parents: 42333
diff changeset
404 PyErr_Restore(last_type, last_value, last_tb);
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
405 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
406
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
407 static void
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
408 ptrace_leave_call(PyObject *self, void *key)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
409 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
410 /* leaving a call to the function identified by 'key' */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
411 ProfilerObject *pObj = (ProfilerObject*)self;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
412 ProfilerEntry *profEntry;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
413 ProfilerContext *pContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
414
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
415 pContext = pObj->currentProfilerContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
416 if (pContext == NULL)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
417 return;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
418 profEntry = getEntry(pObj, key);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
419 if (profEntry) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
420 Stop(pObj, pContext, profEntry);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
421 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
422 else {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
423 pObj->currentProfilerContext = pContext->previous;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
424 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
425 /* put pContext into the free list */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
426 pContext->previous = pObj->freelistProfilerContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
427 pObj->freelistProfilerContext = pContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
428 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
429
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
430 static int
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
431 profiler_callback(PyObject *self, PyFrameObject *frame, int what,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
432 PyObject *arg)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
433 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
434 switch (what) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
435
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
436 /* the 'frame' of a called function is about to start its execution */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
437 case PyTrace_CALL:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
438 ptrace_enter_call(self, (void *)frame->f_code,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
439 (PyObject *)frame->f_code);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
440 break;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
441
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
442 /* the 'frame' of a called function is about to finish
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
443 (either normally or with an exception) */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
444 case PyTrace_RETURN:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
445 ptrace_leave_call(self, (void *)frame->f_code);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
446 break;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
447
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
448 /* case PyTrace_EXCEPTION:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
449 If the exception results in the function exiting, a
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
450 PyTrace_RETURN event will be generated, so we don't need to
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
451 handle it. */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
452
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
453 #ifdef PyTrace_C_CALL /* not defined in Python <= 2.3 */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
454 /* the Python function 'frame' is issuing a call to the built-in
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
455 function 'arg' */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
456 case PyTrace_C_CALL:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
457 if ((((ProfilerObject *)self)->flags & POF_BUILTINS)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
458 && PyCFunction_Check(arg)) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
459 ptrace_enter_call(self,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
460 ((PyCFunctionObject *)arg)->m_ml,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
461 arg);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
462 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
463 break;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
464
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
465 /* the call to the built-in function 'arg' is returning into its
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
466 caller 'frame' */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
467 case PyTrace_C_RETURN: /* ...normally */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
468 case PyTrace_C_EXCEPTION: /* ...with an exception set */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
469 if ((((ProfilerObject *)self)->flags & POF_BUILTINS)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
470 && PyCFunction_Check(arg)) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
471 ptrace_leave_call(self,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
472 ((PyCFunctionObject *)arg)->m_ml);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
473 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
474 break;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
475 #endif
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
476
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
477 default:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
478 break;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
479 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
480 return 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
481 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
482
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
483 static int
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
484 pending_exception(ProfilerObject *pObj)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
485 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
486 if (pObj->flags & POF_NOMEMORY) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
487 pObj->flags -= POF_NOMEMORY;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
488 PyErr_SetString(PyExc_MemoryError,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
489 "memory was exhausted while profiling");
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
490 return -1;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
491 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
492 return 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
493 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
494
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
495 /************************************************************/
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
496
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
497 static PyStructSequence_Field profiler_entry_fields[] = {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
498 {"code", "code object or built-in function name"},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
499 {"callcount", "how many times this was called"},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
500 {"reccallcount", "how many times called recursively"},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
501 {"totaltime", "total time in this entry"},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
502 {"inlinetime", "inline time in this entry (not in subcalls)"},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
503 {"calls", "details of the calls"},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
504 {0}
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
505 };
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
506
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
507 static PyStructSequence_Field profiler_subentry_fields[] = {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
508 {"code", "called code object or built-in function name"},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
509 {"callcount", "how many times this is called"},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
510 {"reccallcount", "how many times this is called recursively"},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
511 {"totaltime", "total time spent in this call"},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
512 {"inlinetime", "inline time (not in further subcalls)"},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
513 {0}
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
514 };
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
515
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
516 static PyStructSequence_Desc profiler_entry_desc = {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
517 "_lsprof.profiler_entry", /* name */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
518 NULL, /* doc */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
519 profiler_entry_fields,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
520 6
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
521 };
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
522
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
523 static PyStructSequence_Desc profiler_subentry_desc = {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
524 "_lsprof.profiler_subentry", /* name */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
525 NULL, /* doc */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
526 profiler_subentry_fields,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
527 5
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
528 };
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
529
37772
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36861
diff changeset
530 static int initialized;
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
531 static PyTypeObject StatsEntryType;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
532 static PyTypeObject StatsSubEntryType;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
533
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
534
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
535 typedef struct {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
536 PyObject *list;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
537 PyObject *sublist;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
538 double factor;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
539 } statscollector_t;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
540
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
541 static int statsForSubEntry(rotating_node_t *node, void *arg)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
542 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
543 ProfilerSubEntry *sentry = (ProfilerSubEntry*) node;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
544 statscollector_t *collect = (statscollector_t*) arg;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
545 ProfilerEntry *entry = (ProfilerEntry*) sentry->header.key;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
546 int err;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
547 PyObject *sinfo;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
548 sinfo = PyObject_CallFunction((PyObject*) &StatsSubEntryType,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
549 "((Olldd))",
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
550 entry->userObj,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
551 sentry->callcount,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
552 sentry->recursivecallcount,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
553 collect->factor * sentry->tt,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
554 collect->factor * sentry->it);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
555 if (sinfo == NULL)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
556 return -1;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
557 err = PyList_Append(collect->sublist, sinfo);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
558 Py_DECREF(sinfo);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
559 return err;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
560 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
561
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
562 static int statsForEntry(rotating_node_t *node, void *arg)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
563 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
564 ProfilerEntry *entry = (ProfilerEntry*) node;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
565 statscollector_t *collect = (statscollector_t*) arg;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
566 PyObject *info;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
567 int err;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
568 if (entry->callcount == 0)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
569 return 0; /* skip */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
570
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
571 if (entry->calls != EMPTY_ROTATING_TREE) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
572 collect->sublist = PyList_New(0);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
573 if (collect->sublist == NULL)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
574 return -1;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
575 if (RotatingTree_Enum(entry->calls,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
576 statsForSubEntry, collect) != 0) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
577 Py_DECREF(collect->sublist);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
578 return -1;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
579 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
580 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
581 else {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
582 Py_INCREF(Py_None);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
583 collect->sublist = Py_None;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
584 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
585
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
586 info = PyObject_CallFunction((PyObject*) &StatsEntryType,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
587 "((OllddO))",
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
588 entry->userObj,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
589 entry->callcount,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
590 entry->recursivecallcount,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
591 collect->factor * entry->tt,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
592 collect->factor * entry->it,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
593 collect->sublist);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
594 Py_DECREF(collect->sublist);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
595 if (info == NULL)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
596 return -1;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
597 err = PyList_Append(collect->list, info);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
598 Py_DECREF(info);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
599 return err;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
600 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
601
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
602 PyDoc_STRVAR(getstats_doc, "\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
603 getstats() -> list of profiler_entry objects\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
604 \n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
605 Return all information collected by the profiler.\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
606 Each profiler_entry is a tuple-like object with the\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
607 following attributes:\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
608 \n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
609 code code object\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
610 callcount how many times this was called\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
611 reccallcount how many times called recursively\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
612 totaltime total time in this entry\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
613 inlinetime inline time in this entry (not in subcalls)\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
614 calls details of the calls\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
615 \n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
616 The calls attribute is either None or a list of\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
617 profiler_subentry objects:\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
618 \n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
619 code called code object\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
620 callcount how many times this is called\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
621 reccallcount how many times this is called recursively\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
622 totaltime total time spent in this call\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
623 inlinetime inline time (not in further subcalls)\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
624 ");
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
625
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
626 static PyObject*
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
627 profiler_getstats(ProfilerObject *pObj, PyObject* noarg)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
628 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
629 statscollector_t collect;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
630 if (pending_exception(pObj))
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
631 return NULL;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
632 if (!pObj->externalTimer)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
633 collect.factor = hpTimerUnit();
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
634 else if (pObj->externalTimerUnit > 0.0)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
635 collect.factor = pObj->externalTimerUnit;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
636 else
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
637 collect.factor = 1.0 / DOUBLE_TIMER_PRECISION;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
638 collect.list = PyList_New(0);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
639 if (collect.list == NULL)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
640 return NULL;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
641 if (RotatingTree_Enum(pObj->profilerEntries, statsForEntry, &collect)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
642 != 0) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
643 Py_DECREF(collect.list);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
644 return NULL;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
645 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
646 return collect.list;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
647 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
648
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
649 static int
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
650 setSubcalls(ProfilerObject *pObj, int nvalue)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
651 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
652 if (nvalue == 0)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
653 pObj->flags &= ~POF_SUBCALLS;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
654 else if (nvalue > 0)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
655 pObj->flags |= POF_SUBCALLS;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
656 return 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
657 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
658
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
659 static int
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
660 setBuiltins(ProfilerObject *pObj, int nvalue)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
661 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
662 if (nvalue == 0)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
663 pObj->flags &= ~POF_BUILTINS;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
664 else if (nvalue > 0) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
665 #ifndef PyTrace_C_CALL
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
666 PyErr_SetString(PyExc_ValueError,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
667 "builtins=True requires Python >= 2.4");
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
668 return -1;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
669 #else
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
670 pObj->flags |= POF_BUILTINS;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
671 #endif
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
672 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
673 return 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
674 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
675
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
676 PyDoc_STRVAR(enable_doc, "\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
677 enable(subcalls=True, builtins=True)\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
678 \n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
679 Start collecting profiling information.\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
680 If 'subcalls' is True, also records for each function\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
681 statistics separated according to its current caller.\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
682 If 'builtins' is True, records the time spent in\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
683 built-in functions separately from their caller.\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
684 ");
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
685
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
686 static PyObject*
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
687 profiler_enable(ProfilerObject *self, PyObject *args, PyObject *kwds)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
688 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
689 int subcalls = -1;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
690 int builtins = -1;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36471
diff changeset
691 static char *kwlist[] = {"subcalls", "builtins", 0};
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
692 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:enable",
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
693 kwlist, &subcalls, &builtins))
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
694 return NULL;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
695 if (setSubcalls(self, subcalls) < 0 || setBuiltins(self, builtins) < 0)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
696 return NULL;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
697 PyEval_SetProfile(profiler_callback, (PyObject*)self);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
698 self->flags |= POF_ENABLED;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
699 Py_INCREF(Py_None);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
700 return Py_None;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
701 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
702
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
703 static void
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
704 flush_unmatched(ProfilerObject *pObj)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
705 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
706 while (pObj->currentProfilerContext) {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
707 ProfilerContext *pContext = pObj->currentProfilerContext;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
708 ProfilerEntry *profEntry= pContext->ctxEntry;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
709 if (profEntry)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
710 Stop(pObj, pContext, profEntry);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
711 else
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
712 pObj->currentProfilerContext = pContext->previous;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
713 if (pContext)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
714 free(pContext);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
715 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
716
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
717 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
718
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
719 PyDoc_STRVAR(disable_doc, "\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
720 disable()\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
721 \n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
722 Stop collecting profiling information.\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
723 ");
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
724
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
725 static PyObject*
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
726 profiler_disable(ProfilerObject *self, PyObject* noarg)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
727 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
728 self->flags &= ~POF_ENABLED;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
729 PyEval_SetProfile(NULL, NULL);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
730 flush_unmatched(self);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
731 if (pending_exception(self))
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
732 return NULL;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
733 Py_INCREF(Py_None);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
734 return Py_None;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
735 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
736
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
737 PyDoc_STRVAR(clear_doc, "\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
738 clear()\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
739 \n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
740 Clear all profiling information collected so far.\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
741 ");
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
742
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
743 static PyObject*
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
744 profiler_clear(ProfilerObject *pObj, PyObject* noarg)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
745 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
746 clearEntries(pObj);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
747 Py_INCREF(Py_None);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
748 return Py_None;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
749 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
750
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
751 static void
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
752 profiler_dealloc(ProfilerObject *op)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
753 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
754 if (op->flags & POF_ENABLED)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
755 PyEval_SetProfile(NULL, NULL);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
756 flush_unmatched(op);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
757 clearEntries(op);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
758 Py_XDECREF(op->externalTimer);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43258
diff changeset
759 Py_TYPE(op)->tp_free(op);
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
760 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
761
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
762 static int
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
763 profiler_init(ProfilerObject *pObj, PyObject *args, PyObject *kw)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
764 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
765 PyObject *o;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
766 PyObject *timer = NULL;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
767 double timeunit = 0.0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
768 int subcalls = 1;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
769 #ifdef PyTrace_C_CALL
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
770 int builtins = 1;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
771 #else
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
772 int builtins = 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
773 #endif
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36471
diff changeset
774 static char *kwlist[] = {"timer", "timeunit",
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
775 "subcalls", "builtins", 0};
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
776
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
777 if (!PyArg_ParseTupleAndKeywords(args, kw, "|Odii:Profiler", kwlist,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
778 &timer, &timeunit,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
779 &subcalls, &builtins))
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
780 return -1;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
781
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
782 if (setSubcalls(pObj, subcalls) < 0 || setBuiltins(pObj, builtins) < 0)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
783 return -1;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
784 o = pObj->externalTimer;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
785 pObj->externalTimer = timer;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
786 Py_XINCREF(timer);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
787 Py_XDECREF(o);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
788 pObj->externalTimerUnit = timeunit;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
789 return 0;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
790 }
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
791
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
792 static PyMethodDef profiler_methods[] = {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
793 {"getstats", (PyCFunction)profiler_getstats,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
794 METH_NOARGS, getstats_doc},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
795 {"enable", (PyCFunction)profiler_enable,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
796 METH_VARARGS | METH_KEYWORDS, enable_doc},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
797 {"disable", (PyCFunction)profiler_disable,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
798 METH_NOARGS, disable_doc},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
799 {"clear", (PyCFunction)profiler_clear,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
800 METH_NOARGS, clear_doc},
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
801 {NULL, NULL}
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
802 };
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
803
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
804 PyDoc_STRVAR(profiler_doc, "\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
805 Profiler(custom_timer=None, time_unit=None, subcalls=True, builtins=True)\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
806 \n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
807 Builds a profiler object using the specified timer function.\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
808 The default timer is a fast built-in one based on real time.\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
809 For custom timer functions returning integers, time_unit can\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
810 be a float specifying a scale (i.e. how long each integer unit\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
811 is, in seconds).\n\
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
812 ");
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
813
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
814 statichere PyTypeObject PyProfiler_Type = {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
815 PyObject_HEAD_INIT(NULL)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
816 0, /* ob_size */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
817 "_lsprof.Profiler", /* tp_name */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
818 sizeof(ProfilerObject), /* tp_basicsize */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
819 0, /* tp_itemsize */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
820 (destructor)profiler_dealloc, /* tp_dealloc */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
821 0, /* tp_print */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
822 0, /* tp_getattr */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
823 0, /* tp_setattr */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
824 0, /* tp_compare */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
825 0, /* tp_repr */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
826 0, /* tp_as_number */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
827 0, /* tp_as_sequence */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
828 0, /* tp_as_mapping */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
829 0, /* tp_hash */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
830 0, /* tp_call */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
831 0, /* tp_str */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
832 0, /* tp_getattro */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
833 0, /* tp_setattro */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
834 0, /* tp_as_buffer */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
835 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
836 profiler_doc, /* tp_doc */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
837 0, /* tp_traverse */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
838 0, /* tp_clear */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
839 0, /* tp_richcompare */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
840 0, /* tp_weaklistoffset */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
841 0, /* tp_iter */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
842 0, /* tp_iternext */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
843 profiler_methods, /* tp_methods */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
844 0, /* tp_members */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
845 0, /* tp_getset */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
846 0, /* tp_base */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
847 0, /* tp_dict */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
848 0, /* tp_descr_get */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
849 0, /* tp_descr_set */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
850 0, /* tp_dictoffset */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
851 (initproc)profiler_init, /* tp_init */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
852 PyType_GenericAlloc, /* tp_alloc */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
853 PyType_GenericNew, /* tp_new */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
854 PyObject_Del, /* tp_free */
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
855 };
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
856
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
857 static PyMethodDef moduleMethods[] = {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
858 {NULL, NULL}
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
859 };
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
860
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
861 PyMODINIT_FUNC
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
862 init_lsprof(void)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
863 {
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
864 PyObject *module, *d;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
865 module = Py_InitModule3("_lsprof", moduleMethods, "Fast profiler");
36861
895074e5ed3a Thanks to Coverity, these were all reported by their Prevent tool.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
866 if (module == NULL)
895074e5ed3a Thanks to Coverity, these were all reported by their Prevent tool.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
867 return;
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
868 d = PyModule_GetDict(module);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
869 if (PyType_Ready(&PyProfiler_Type) < 0)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
870 return;
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
871 PyDict_SetItemString(d, "Profiler", (PyObject *)&PyProfiler_Type);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
872
37772
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36861
diff changeset
873 if (!initialized) {
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36861
diff changeset
874 PyStructSequence_InitType(&StatsEntryType,
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36861
diff changeset
875 &profiler_entry_desc);
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36861
diff changeset
876 PyStructSequence_InitType(&StatsSubEntryType,
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36861
diff changeset
877 &profiler_subentry_desc);
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36861
diff changeset
878 }
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
879 Py_INCREF((PyObject*) &StatsEntryType);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
880 Py_INCREF((PyObject*) &StatsSubEntryType);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
881 PyModule_AddObject(module, "profiler_entry",
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
882 (PyObject*) &StatsEntryType);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
883 PyModule_AddObject(module, "profiler_subentry",
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
884 (PyObject*) &StatsSubEntryType);
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
885 empty_tuple = PyTuple_New(0);
37772
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36861
diff changeset
886 initialized = 1;
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
887 }