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