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