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