Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "hashtable.h" |
| 3 | #include "frameobject.h" |
| 4 | #include "pythread.h" |
| 5 | #include "osdefs.h" |
| 6 | |
| 7 | /* Trace memory blocks allocated by PyMem_RawMalloc() */ |
| 8 | #define TRACE_RAW_MALLOC |
| 9 | |
| 10 | /* Forward declaration */ |
| 11 | static void tracemalloc_stop(void); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 12 | static void* raw_malloc(size_t size); |
| 13 | static void raw_free(void *ptr); |
| 14 | |
| 15 | #ifdef Py_DEBUG |
| 16 | # define TRACE_DEBUG |
| 17 | #endif |
| 18 | |
| 19 | #define _STR(VAL) #VAL |
| 20 | #define STR(VAL) _STR(VAL) |
| 21 | |
| 22 | /* Protected by the GIL */ |
| 23 | static struct { |
| 24 | PyMemAllocator mem; |
| 25 | PyMemAllocator raw; |
| 26 | PyMemAllocator obj; |
| 27 | } allocators; |
| 28 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 29 | static struct { |
| 30 | /* Module initialized? |
| 31 | Variable protected by the GIL */ |
| 32 | enum { |
| 33 | TRACEMALLOC_NOT_INITIALIZED, |
| 34 | TRACEMALLOC_INITIALIZED, |
| 35 | TRACEMALLOC_FINALIZED |
| 36 | } initialized; |
| 37 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 38 | /* Is tracemalloc tracing memory allocations? |
| 39 | Variable protected by the GIL */ |
| 40 | int tracing; |
| 41 | |
| 42 | /* limit of the number of frames in a traceback, 1 by default. |
| 43 | Variable protected by the GIL. */ |
| 44 | int max_nframe; |
Victor Stinner | be0708f | 2013-12-01 10:03:26 +0100 | [diff] [blame] | 45 | } tracemalloc_config = {TRACEMALLOC_NOT_INITIALIZED, 0, 1}; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 46 | |
| 47 | #if defined(TRACE_RAW_MALLOC) && defined(WITH_THREAD) |
| 48 | /* This lock is needed because tracemalloc_free() is called without |
| 49 | the GIL held from PyMem_RawFree(). It cannot acquire the lock because it |
| 50 | would introduce a deadlock in PyThreadState_DeleteCurrent(). */ |
| 51 | static PyThread_type_lock tables_lock; |
| 52 | # define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1) |
| 53 | # define TABLES_UNLOCK() PyThread_release_lock(tables_lock) |
| 54 | #else |
| 55 | /* variables are protected by the GIL */ |
| 56 | # define TABLES_LOCK() |
| 57 | # define TABLES_UNLOCK() |
| 58 | #endif |
| 59 | |
| 60 | /* Pack the frame_t structure to reduce the memory footprint on 64-bit |
| 61 | architectures: 12 bytes instead of 16. This optimization might produce |
| 62 | SIGBUS on architectures not supporting unaligned memory accesses (64-bit |
| 63 | IPS CPU?): on such architecture, the structure must not be packed. */ |
| 64 | #pragma pack(4) |
| 65 | typedef struct |
| 66 | #ifdef __GNUC__ |
| 67 | __attribute__((packed)) |
| 68 | #endif |
| 69 | { |
| 70 | PyObject *filename; |
| 71 | int lineno; |
| 72 | } frame_t; |
| 73 | |
| 74 | typedef struct { |
| 75 | Py_uhash_t hash; |
| 76 | int nframe; |
| 77 | frame_t frames[1]; |
| 78 | } traceback_t; |
| 79 | |
| 80 | #define TRACEBACK_SIZE(NFRAME) \ |
| 81 | (sizeof(traceback_t) + sizeof(frame_t) * (NFRAME - 1)) |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 82 | |
| 83 | #define MAX_NFRAME \ |
| 84 | ((INT_MAX - sizeof(traceback_t)) / sizeof(frame_t) + 1) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 85 | |
| 86 | static PyObject *unknown_filename = NULL; |
| 87 | static traceback_t tracemalloc_empty_traceback; |
| 88 | |
Victor Stinner | 7a5be14 | 2013-11-26 01:06:02 +0100 | [diff] [blame] | 89 | /* Trace of a memory block */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 90 | typedef struct { |
Victor Stinner | 7a5be14 | 2013-11-26 01:06:02 +0100 | [diff] [blame] | 91 | /* Size of the memory block in bytes */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 92 | size_t size; |
Victor Stinner | 7a5be14 | 2013-11-26 01:06:02 +0100 | [diff] [blame] | 93 | |
| 94 | /* Traceback where the memory block was allocated */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 95 | traceback_t *traceback; |
| 96 | } trace_t; |
| 97 | |
| 98 | /* Size in bytes of currently traced memory. |
| 99 | Protected by TABLES_LOCK(). */ |
| 100 | static size_t tracemalloc_traced_memory = 0; |
| 101 | |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 102 | /* Peak size in bytes of traced memory. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 103 | Protected by TABLES_LOCK(). */ |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 104 | static size_t tracemalloc_peak_traced_memory = 0; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 105 | |
| 106 | /* Hash table used as a set to to intern filenames: |
| 107 | PyObject* => PyObject*. |
| 108 | Protected by the GIL */ |
| 109 | static _Py_hashtable_t *tracemalloc_filenames = NULL; |
| 110 | |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 111 | /* Buffer to store a new traceback in traceback_new(). |
| 112 | Protected by the GIL. */ |
| 113 | static traceback_t *tracemalloc_traceback = NULL; |
| 114 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 115 | /* Hash table used as a set to intern tracebacks: |
| 116 | traceback_t* => traceback_t* |
| 117 | Protected by the GIL */ |
| 118 | static _Py_hashtable_t *tracemalloc_tracebacks = NULL; |
| 119 | |
| 120 | /* pointer (void*) => trace (trace_t). |
| 121 | Protected by TABLES_LOCK(). */ |
| 122 | static _Py_hashtable_t *tracemalloc_traces = NULL; |
| 123 | |
| 124 | #ifdef TRACE_DEBUG |
| 125 | static void |
| 126 | tracemalloc_error(const char *format, ...) |
| 127 | { |
| 128 | va_list ap; |
| 129 | fprintf(stderr, "tracemalloc: "); |
| 130 | va_start(ap, format); |
| 131 | vfprintf(stderr, format, ap); |
| 132 | va_end(ap); |
| 133 | fprintf(stderr, "\n"); |
| 134 | fflush(stderr); |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | #if defined(WITH_THREAD) && defined(TRACE_RAW_MALLOC) |
| 139 | #define REENTRANT_THREADLOCAL |
| 140 | |
| 141 | /* If your OS does not provide native thread local storage, you can implement |
| 142 | it manually using a lock. Functions of thread.c cannot be used because |
| 143 | they use PyMem_RawMalloc() which leads to a reentrant call. */ |
| 144 | #if !(defined(_POSIX_THREADS) || defined(NT_THREADS)) |
| 145 | # error "need native thread local storage (TLS)" |
| 146 | #endif |
| 147 | |
| 148 | static int tracemalloc_reentrant_key; |
| 149 | |
| 150 | /* Any non-NULL pointer can be used */ |
| 151 | #define REENTRANT Py_True |
| 152 | |
| 153 | static int |
| 154 | get_reentrant(void) |
| 155 | { |
| 156 | void *ptr = PyThread_get_key_value(tracemalloc_reentrant_key); |
| 157 | if (ptr != NULL) { |
| 158 | assert(ptr == REENTRANT); |
| 159 | return 1; |
| 160 | } |
| 161 | else |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | set_reentrant(int reentrant) |
| 167 | { |
Victor Stinner | 2ead3d2 | 2013-11-26 01:08:53 +0100 | [diff] [blame] | 168 | assert(reentrant == 0 || reentrant == 1); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 169 | if (reentrant) { |
| 170 | assert(PyThread_get_key_value(tracemalloc_reentrant_key) == NULL); |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 171 | PyThread_set_key_value(tracemalloc_reentrant_key, REENTRANT); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 172 | } |
| 173 | else { |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 174 | assert(PyThread_get_key_value(tracemalloc_reentrant_key) == REENTRANT); |
| 175 | PyThread_set_key_value(tracemalloc_reentrant_key, NULL); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | #else |
| 180 | |
| 181 | /* WITH_THREAD not defined: Python compiled without threads, |
| 182 | or TRACE_RAW_MALLOC not defined: variable protected by the GIL */ |
| 183 | static int tracemalloc_reentrant = 0; |
| 184 | |
| 185 | static int |
| 186 | get_reentrant(void) |
| 187 | { |
| 188 | return tracemalloc_reentrant; |
| 189 | } |
| 190 | |
| 191 | static void |
| 192 | set_reentrant(int reentrant) |
| 193 | { |
| 194 | assert(!reentrant || !get_reentrant()); |
| 195 | tracemalloc_reentrant = reentrant; |
| 196 | } |
| 197 | #endif |
| 198 | |
| 199 | static int |
| 200 | hashtable_compare_unicode(const void *key, const _Py_hashtable_entry_t *entry) |
| 201 | { |
| 202 | if (key != NULL && entry->key != NULL) |
| 203 | return (PyUnicode_Compare((PyObject *)key, (PyObject *)entry->key) == 0); |
| 204 | else |
| 205 | return key == entry->key; |
| 206 | } |
| 207 | |
| 208 | static _Py_hashtable_allocator_t hashtable_alloc = {malloc, free}; |
| 209 | |
| 210 | static _Py_hashtable_t * |
| 211 | hashtable_new(size_t data_size, |
| 212 | _Py_hashtable_hash_func hash_func, |
| 213 | _Py_hashtable_compare_func compare_func) |
| 214 | { |
| 215 | return _Py_hashtable_new_full(data_size, 0, |
| 216 | hash_func, compare_func, |
| 217 | NULL, NULL, NULL, &hashtable_alloc); |
| 218 | } |
| 219 | |
| 220 | static void* |
| 221 | raw_malloc(size_t size) |
| 222 | { |
| 223 | return allocators.raw.malloc(allocators.raw.ctx, size); |
| 224 | } |
| 225 | |
| 226 | static void |
| 227 | raw_free(void *ptr) |
| 228 | { |
| 229 | allocators.raw.free(allocators.raw.ctx, ptr); |
| 230 | } |
| 231 | |
| 232 | static Py_uhash_t |
| 233 | hashtable_hash_traceback(const void *key) |
| 234 | { |
| 235 | const traceback_t *traceback = key; |
| 236 | return traceback->hash; |
| 237 | } |
| 238 | |
| 239 | static int |
| 240 | hashtable_compare_traceback(const traceback_t *traceback1, |
| 241 | const _Py_hashtable_entry_t *he) |
| 242 | { |
| 243 | const traceback_t *traceback2 = he->key; |
| 244 | const frame_t *frame1, *frame2; |
| 245 | int i; |
| 246 | |
| 247 | if (traceback1->nframe != traceback2->nframe) |
| 248 | return 0; |
| 249 | |
| 250 | for (i=0; i < traceback1->nframe; i++) { |
| 251 | frame1 = &traceback1->frames[i]; |
| 252 | frame2 = &traceback2->frames[i]; |
| 253 | |
| 254 | if (frame1->lineno != frame2->lineno) |
| 255 | return 0; |
| 256 | |
| 257 | if (frame1->filename != frame2->filename) { |
| 258 | assert(PyUnicode_Compare(frame1->filename, frame2->filename) != 0); |
| 259 | return 0; |
| 260 | } |
| 261 | } |
| 262 | return 1; |
| 263 | } |
| 264 | |
| 265 | static void |
| 266 | tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame) |
| 267 | { |
| 268 | PyCodeObject *code; |
| 269 | PyObject *filename; |
| 270 | _Py_hashtable_entry_t *entry; |
| 271 | |
| 272 | frame->filename = unknown_filename; |
| 273 | frame->lineno = PyFrame_GetLineNumber(pyframe); |
| 274 | assert(frame->lineno >= 0); |
| 275 | if (frame->lineno < 0) |
| 276 | frame->lineno = 0; |
| 277 | |
| 278 | code = pyframe->f_code; |
| 279 | if (code == NULL) { |
| 280 | #ifdef TRACE_DEBUG |
Victor Stinner | 4dc7420 | 2013-11-26 01:18:52 +0100 | [diff] [blame] | 281 | tracemalloc_error("failed to get the code object of the frame"); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 282 | #endif |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | if (code->co_filename == NULL) { |
| 287 | #ifdef TRACE_DEBUG |
| 288 | tracemalloc_error("failed to get the filename of the code object"); |
| 289 | #endif |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | filename = code->co_filename; |
| 294 | assert(filename != NULL); |
| 295 | if (filename == NULL) |
| 296 | return; |
| 297 | |
| 298 | if (!PyUnicode_Check(filename)) { |
| 299 | #ifdef TRACE_DEBUG |
| 300 | tracemalloc_error("filename is not an unicode string"); |
| 301 | #endif |
| 302 | return; |
| 303 | } |
| 304 | if (!PyUnicode_IS_READY(filename)) { |
| 305 | /* Don't make a Unicode string ready to avoid reentrant calls |
| 306 | to tracemalloc_malloc() or tracemalloc_realloc() */ |
| 307 | #ifdef TRACE_DEBUG |
| 308 | tracemalloc_error("filename is not a ready unicode string"); |
| 309 | #endif |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | /* intern the filename */ |
| 314 | entry = _Py_hashtable_get_entry(tracemalloc_filenames, filename); |
| 315 | if (entry != NULL) { |
| 316 | filename = (PyObject *)entry->key; |
| 317 | } |
| 318 | else { |
| 319 | /* tracemalloc_filenames is responsible to keep a reference |
| 320 | to the filename */ |
| 321 | Py_INCREF(filename); |
| 322 | if (_Py_hashtable_set(tracemalloc_filenames, filename, NULL, 0) < 0) { |
| 323 | Py_DECREF(filename); |
| 324 | #ifdef TRACE_DEBUG |
| 325 | tracemalloc_error("failed to intern the filename"); |
| 326 | #endif |
| 327 | return; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* the tracemalloc_filenames table keeps a reference to the filename */ |
| 332 | frame->filename = filename; |
| 333 | } |
| 334 | |
| 335 | static Py_uhash_t |
| 336 | traceback_hash(traceback_t *traceback) |
| 337 | { |
| 338 | /* code based on tuplehash() of Objects/tupleobject.c */ |
Victor Stinner | 4d8c29c | 2013-12-16 23:05:13 +0100 | [diff] [blame^] | 339 | Py_uhash_t x, y; /* Unsigned for defined overflow behavior. */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 340 | int len = traceback->nframe; |
| 341 | Py_uhash_t mult = _PyHASH_MULTIPLIER; |
| 342 | frame_t *frame; |
| 343 | |
| 344 | x = 0x345678UL; |
| 345 | frame = traceback->frames; |
| 346 | while (--len >= 0) { |
Victor Stinner | 4d8c29c | 2013-12-16 23:05:13 +0100 | [diff] [blame^] | 347 | y = (Py_uhash_t)PyObject_Hash(frame->filename); |
| 348 | y ^= (Py_uhash_t)frame->lineno; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 349 | frame++; |
| 350 | |
| 351 | x = (x ^ y) * mult; |
| 352 | /* the cast might truncate len; that doesn't change hash stability */ |
Victor Stinner | 4d8c29c | 2013-12-16 23:05:13 +0100 | [diff] [blame^] | 353 | mult += (Py_uhash_t)(82520UL + len + len); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 354 | } |
| 355 | x += 97531UL; |
| 356 | return x; |
| 357 | } |
| 358 | |
| 359 | static void |
| 360 | traceback_get_frames(traceback_t *traceback) |
| 361 | { |
| 362 | PyThreadState *tstate; |
| 363 | PyFrameObject *pyframe; |
| 364 | |
| 365 | #ifdef WITH_THREAD |
| 366 | tstate = PyGILState_GetThisThreadState(); |
| 367 | #else |
| 368 | tstate = PyThreadState_Get(); |
| 369 | #endif |
| 370 | if (tstate == NULL) { |
| 371 | #ifdef TRACE_DEBUG |
| 372 | tracemalloc_error("failed to get the current thread state"); |
| 373 | #endif |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | for (pyframe = tstate->frame; pyframe != NULL; pyframe = pyframe->f_back) { |
| 378 | tracemalloc_get_frame(pyframe, &traceback->frames[traceback->nframe]); |
| 379 | assert(traceback->frames[traceback->nframe].filename != NULL); |
| 380 | assert(traceback->frames[traceback->nframe].lineno >= 0); |
| 381 | traceback->nframe++; |
| 382 | if (traceback->nframe == tracemalloc_config.max_nframe) |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | static traceback_t * |
| 388 | traceback_new(void) |
| 389 | { |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 390 | traceback_t *traceback; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 391 | _Py_hashtable_entry_t *entry; |
| 392 | |
| 393 | #ifdef WITH_THREAD |
| 394 | assert(PyGILState_Check()); |
| 395 | #endif |
| 396 | |
| 397 | /* get frames */ |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 398 | traceback = tracemalloc_traceback; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 399 | traceback->nframe = 0; |
| 400 | traceback_get_frames(traceback); |
| 401 | if (traceback->nframe == 0) |
| 402 | return &tracemalloc_empty_traceback; |
| 403 | traceback->hash = traceback_hash(traceback); |
| 404 | |
| 405 | /* intern the traceback */ |
| 406 | entry = _Py_hashtable_get_entry(tracemalloc_tracebacks, traceback); |
| 407 | if (entry != NULL) { |
| 408 | traceback = (traceback_t *)entry->key; |
| 409 | } |
| 410 | else { |
| 411 | traceback_t *copy; |
| 412 | size_t traceback_size; |
| 413 | |
| 414 | traceback_size = TRACEBACK_SIZE(traceback->nframe); |
| 415 | |
| 416 | copy = raw_malloc(traceback_size); |
| 417 | if (copy == NULL) { |
| 418 | #ifdef TRACE_DEBUG |
| 419 | tracemalloc_error("failed to intern the traceback: malloc failed"); |
| 420 | #endif |
| 421 | return NULL; |
| 422 | } |
| 423 | memcpy(copy, traceback, traceback_size); |
| 424 | |
| 425 | if (_Py_hashtable_set(tracemalloc_tracebacks, copy, NULL, 0) < 0) { |
| 426 | raw_free(copy); |
| 427 | #ifdef TRACE_DEBUG |
| 428 | tracemalloc_error("failed to intern the traceback: putdata failed"); |
| 429 | #endif |
| 430 | return NULL; |
| 431 | } |
| 432 | traceback = copy; |
| 433 | } |
| 434 | return traceback; |
| 435 | } |
| 436 | |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 437 | static int |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 438 | tracemalloc_add_trace(void *ptr, size_t size) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 439 | { |
| 440 | traceback_t *traceback; |
| 441 | trace_t trace; |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 442 | int res; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 443 | |
| 444 | #ifdef WITH_THREAD |
| 445 | assert(PyGILState_Check()); |
| 446 | #endif |
| 447 | |
| 448 | traceback = traceback_new(); |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 449 | if (traceback == NULL) |
| 450 | return -1; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 451 | |
| 452 | trace.size = size; |
| 453 | trace.traceback = traceback; |
| 454 | |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 455 | res = _Py_HASHTABLE_SET(tracemalloc_traces, ptr, trace); |
| 456 | if (res == 0) { |
Victor Stinner | d606ba7 | 2013-11-24 11:28:20 +0100 | [diff] [blame] | 457 | assert(tracemalloc_traced_memory <= PY_SIZE_MAX - size); |
| 458 | tracemalloc_traced_memory += size; |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 459 | if (tracemalloc_traced_memory > tracemalloc_peak_traced_memory) |
| 460 | tracemalloc_peak_traced_memory = tracemalloc_traced_memory; |
Victor Stinner | d606ba7 | 2013-11-24 11:28:20 +0100 | [diff] [blame] | 461 | } |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 462 | |
| 463 | return res; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static void |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 467 | tracemalloc_remove_trace(void *ptr) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 468 | { |
| 469 | trace_t trace; |
| 470 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 471 | if (_Py_hashtable_pop(tracemalloc_traces, ptr, &trace, sizeof(trace))) { |
| 472 | assert(tracemalloc_traced_memory >= trace.size); |
| 473 | tracemalloc_traced_memory -= trace.size; |
| 474 | } |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static void* |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 478 | tracemalloc_malloc(void *ctx, size_t size) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 479 | { |
| 480 | PyMemAllocator *alloc = (PyMemAllocator *)ctx; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 481 | void *ptr; |
| 482 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 483 | ptr = alloc->malloc(alloc->ctx, size); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 484 | if (ptr == NULL) |
| 485 | return NULL; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 486 | |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 487 | TABLES_LOCK(); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 488 | if (tracemalloc_add_trace(ptr, size) < 0) { |
| 489 | /* Failed to allocate a trace for the new memory block */ |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 490 | TABLES_UNLOCK(); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 491 | alloc->free(alloc->ctx, ptr); |
| 492 | return NULL; |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 493 | } |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 494 | TABLES_UNLOCK(); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 495 | return ptr; |
| 496 | } |
| 497 | |
| 498 | static void* |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 499 | tracemalloc_realloc(void *ctx, void *ptr, size_t new_size) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 500 | { |
| 501 | PyMemAllocator *alloc = (PyMemAllocator *)ctx; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 502 | void *ptr2; |
| 503 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 504 | ptr2 = alloc->realloc(alloc->ctx, ptr, new_size); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 505 | if (ptr2 == NULL) |
| 506 | return NULL; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 507 | |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 508 | if (ptr != NULL) { |
| 509 | /* an existing memory block has been resized */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 510 | |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 511 | TABLES_LOCK(); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 512 | tracemalloc_remove_trace(ptr); |
Victor Stinner | 08facd2 | 2013-11-24 12:27:59 +0100 | [diff] [blame] | 513 | |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 514 | if (tracemalloc_add_trace(ptr2, new_size) < 0) { |
| 515 | /* Memory allocation failed. The error cannot be reported to |
| 516 | the caller, because realloc() may already have shrinked the |
| 517 | memory block and so removed bytes. |
| 518 | |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 519 | This case is very unlikely: an hash entry has just been |
| 520 | released, so the hash table should have at least one free entry. |
| 521 | |
| 522 | The GIL and the table lock ensures that only one thread is |
| 523 | allocating memory. */ |
| 524 | assert(0 && "should never happen"); |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 525 | } |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 526 | TABLES_UNLOCK(); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 527 | } |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 528 | else { |
| 529 | /* new allocation */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 530 | |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 531 | TABLES_LOCK(); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 532 | if (tracemalloc_add_trace(ptr2, new_size) < 0) { |
| 533 | /* Failed to allocate a trace for the new memory block */ |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 534 | TABLES_UNLOCK(); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 535 | alloc->free(alloc->ctx, ptr2); |
| 536 | return NULL; |
| 537 | } |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 538 | TABLES_UNLOCK(); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 539 | } |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 540 | return ptr2; |
| 541 | } |
| 542 | |
| 543 | static void |
| 544 | tracemalloc_free(void *ctx, void *ptr) |
| 545 | { |
| 546 | PyMemAllocator *alloc = (PyMemAllocator *)ctx; |
| 547 | |
| 548 | if (ptr == NULL) |
| 549 | return; |
| 550 | |
| 551 | /* GIL cannot be locked in PyMem_RawFree() because it would introduce |
| 552 | a deadlock in PyThreadState_DeleteCurrent(). */ |
| 553 | |
| 554 | alloc->free(alloc->ctx, ptr); |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 555 | |
| 556 | TABLES_LOCK(); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 557 | tracemalloc_remove_trace(ptr); |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 558 | TABLES_UNLOCK(); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static void* |
| 562 | tracemalloc_malloc_gil(void *ctx, size_t size) |
| 563 | { |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 564 | void *ptr; |
| 565 | |
| 566 | if (get_reentrant()) { |
| 567 | PyMemAllocator *alloc = (PyMemAllocator *)ctx; |
| 568 | return alloc->malloc(alloc->ctx, size); |
| 569 | } |
| 570 | |
| 571 | /* Ignore reentrant call. PyObjet_Malloc() calls PyMem_Malloc() for |
| 572 | allocations larger than 512 bytes, don't trace the same memory |
| 573 | allocation twice. */ |
| 574 | set_reentrant(1); |
| 575 | |
| 576 | ptr = tracemalloc_malloc(ctx, size); |
| 577 | |
| 578 | set_reentrant(0); |
| 579 | return ptr; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static void* |
| 583 | tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size) |
| 584 | { |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 585 | void *ptr2; |
| 586 | |
| 587 | if (get_reentrant()) { |
| 588 | /* Reentrant call to PyMem_Realloc() and PyMem_RawRealloc(). |
| 589 | Example: PyMem_RawRealloc() is called internally by pymalloc |
| 590 | (_PyObject_Malloc() and _PyObject_Realloc()) to allocate a new |
| 591 | arena (new_arena()). */ |
| 592 | PyMemAllocator *alloc = (PyMemAllocator *)ctx; |
| 593 | |
| 594 | ptr2 = alloc->realloc(alloc->ctx, ptr, new_size); |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 595 | if (ptr2 != NULL && ptr != NULL) { |
| 596 | TABLES_LOCK(); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 597 | tracemalloc_remove_trace(ptr); |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 598 | TABLES_UNLOCK(); |
| 599 | } |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 600 | return ptr2; |
| 601 | } |
| 602 | |
| 603 | /* Ignore reentrant call. PyObjet_Realloc() calls PyMem_Realloc() for |
| 604 | allocations larger than 512 bytes. Don't trace the same memory |
| 605 | allocation twice. */ |
| 606 | set_reentrant(1); |
| 607 | |
| 608 | ptr2 = tracemalloc_realloc(ctx, ptr, new_size); |
| 609 | |
| 610 | set_reentrant(0); |
| 611 | return ptr2; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | #ifdef TRACE_RAW_MALLOC |
| 615 | static void* |
| 616 | tracemalloc_raw_malloc(void *ctx, size_t size) |
| 617 | { |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 618 | #ifdef WITH_THREAD |
| 619 | PyGILState_STATE gil_state; |
| 620 | #endif |
| 621 | void *ptr; |
| 622 | |
| 623 | if (get_reentrant()) { |
| 624 | PyMemAllocator *alloc = (PyMemAllocator *)ctx; |
| 625 | return alloc->malloc(alloc->ctx, size); |
| 626 | } |
| 627 | |
| 628 | /* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc() |
| 629 | indirectly which would call PyGILState_Ensure() if reentrant are not |
| 630 | disabled. */ |
| 631 | set_reentrant(1); |
| 632 | |
| 633 | #ifdef WITH_THREAD |
| 634 | gil_state = PyGILState_Ensure(); |
| 635 | ptr = tracemalloc_malloc(ctx, size); |
| 636 | PyGILState_Release(gil_state); |
| 637 | #else |
| 638 | ptr = tracemalloc_malloc(ctx, size); |
| 639 | #endif |
| 640 | |
| 641 | set_reentrant(0); |
| 642 | return ptr; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | static void* |
| 646 | tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size) |
| 647 | { |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 648 | #ifdef WITH_THREAD |
| 649 | PyGILState_STATE gil_state; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 650 | #endif |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 651 | void *ptr2; |
| 652 | |
| 653 | if (get_reentrant()) { |
| 654 | /* Reentrant call to PyMem_RawRealloc(). */ |
| 655 | PyMemAllocator *alloc = (PyMemAllocator *)ctx; |
| 656 | |
| 657 | ptr2 = alloc->realloc(alloc->ctx, ptr, new_size); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 658 | |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 659 | if (ptr2 != NULL && ptr != NULL) { |
| 660 | TABLES_LOCK(); |
| 661 | tracemalloc_remove_trace(ptr); |
| 662 | TABLES_UNLOCK(); |
| 663 | } |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 664 | return ptr2; |
| 665 | } |
| 666 | |
| 667 | /* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc() |
| 668 | indirectly which would call PyGILState_Ensure() if reentrant calls are |
| 669 | not disabled. */ |
| 670 | set_reentrant(1); |
| 671 | |
| 672 | #ifdef WITH_THREAD |
| 673 | gil_state = PyGILState_Ensure(); |
| 674 | ptr2 = tracemalloc_realloc(ctx, ptr, new_size); |
| 675 | PyGILState_Release(gil_state); |
| 676 | #else |
| 677 | ptr2 = tracemalloc_realloc(ctx, ptr, new_size); |
| 678 | #endif |
| 679 | |
| 680 | set_reentrant(0); |
| 681 | return ptr2; |
| 682 | } |
| 683 | #endif /* TRACE_RAW_MALLOC */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 684 | |
| 685 | static int |
| 686 | tracemalloc_clear_filename(_Py_hashtable_entry_t *entry, void *user_data) |
| 687 | { |
| 688 | PyObject *filename = (PyObject *)entry->key; |
| 689 | Py_DECREF(filename); |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | static int |
| 694 | traceback_free_traceback(_Py_hashtable_entry_t *entry, void *user_data) |
| 695 | { |
| 696 | traceback_t *traceback = (traceback_t *)entry->key; |
| 697 | raw_free(traceback); |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | /* reentrant flag must be set to call this function and GIL must be held */ |
| 702 | static void |
| 703 | tracemalloc_clear_traces(void) |
| 704 | { |
| 705 | #ifdef WITH_THREAD |
| 706 | /* The GIL protects variables againt concurrent access */ |
| 707 | assert(PyGILState_Check()); |
| 708 | #endif |
| 709 | |
| 710 | /* Disable also reentrant calls to tracemalloc_malloc() to not add a new |
| 711 | trace while we are clearing traces */ |
| 712 | assert(get_reentrant()); |
| 713 | |
| 714 | TABLES_LOCK(); |
| 715 | _Py_hashtable_clear(tracemalloc_traces); |
| 716 | tracemalloc_traced_memory = 0; |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 717 | tracemalloc_peak_traced_memory = 0; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 718 | TABLES_UNLOCK(); |
| 719 | |
| 720 | _Py_hashtable_foreach(tracemalloc_tracebacks, traceback_free_traceback, NULL); |
| 721 | _Py_hashtable_clear(tracemalloc_tracebacks); |
| 722 | |
| 723 | _Py_hashtable_foreach(tracemalloc_filenames, tracemalloc_clear_filename, NULL); |
| 724 | _Py_hashtable_clear(tracemalloc_filenames); |
| 725 | } |
| 726 | |
| 727 | static int |
| 728 | tracemalloc_init(void) |
| 729 | { |
| 730 | if (tracemalloc_config.initialized == TRACEMALLOC_FINALIZED) { |
| 731 | PyErr_SetString(PyExc_RuntimeError, |
| 732 | "the tracemalloc module has been unloaded"); |
| 733 | return -1; |
| 734 | } |
| 735 | |
| 736 | if (tracemalloc_config.initialized == TRACEMALLOC_INITIALIZED) |
| 737 | return 0; |
| 738 | |
| 739 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw); |
| 740 | |
| 741 | #ifdef REENTRANT_THREADLOCAL |
| 742 | tracemalloc_reentrant_key = PyThread_create_key(); |
| 743 | if (tracemalloc_reentrant_key == -1) { |
| 744 | #ifdef MS_WINDOWS |
| 745 | PyErr_SetFromWindowsErr(0); |
| 746 | #else |
| 747 | PyErr_SetFromErrno(PyExc_OSError); |
| 748 | #endif |
| 749 | return -1; |
| 750 | } |
| 751 | #endif |
| 752 | |
| 753 | #if defined(WITH_THREAD) && defined(TRACE_RAW_MALLOC) |
| 754 | if (tables_lock == NULL) { |
| 755 | tables_lock = PyThread_allocate_lock(); |
| 756 | if (tables_lock == NULL) { |
| 757 | PyErr_SetString(PyExc_RuntimeError, "cannot allocate lock"); |
| 758 | return -1; |
| 759 | } |
| 760 | } |
| 761 | #endif |
| 762 | |
| 763 | tracemalloc_filenames = hashtable_new(0, |
| 764 | (_Py_hashtable_hash_func)PyObject_Hash, |
| 765 | hashtable_compare_unicode); |
| 766 | |
| 767 | tracemalloc_tracebacks = hashtable_new(0, |
| 768 | (_Py_hashtable_hash_func)hashtable_hash_traceback, |
| 769 | (_Py_hashtable_compare_func)hashtable_compare_traceback); |
| 770 | |
| 771 | tracemalloc_traces = hashtable_new(sizeof(trace_t), |
| 772 | _Py_hashtable_hash_ptr, |
| 773 | _Py_hashtable_compare_direct); |
| 774 | |
| 775 | if (tracemalloc_filenames == NULL || tracemalloc_tracebacks == NULL |
| 776 | || tracemalloc_traces == NULL) |
| 777 | { |
| 778 | PyErr_NoMemory(); |
| 779 | return -1; |
| 780 | } |
| 781 | |
| 782 | unknown_filename = PyUnicode_FromString("<unknown>"); |
| 783 | if (unknown_filename == NULL) |
| 784 | return -1; |
| 785 | PyUnicode_InternInPlace(&unknown_filename); |
| 786 | |
| 787 | tracemalloc_empty_traceback.nframe = 1; |
| 788 | /* borrowed reference */ |
| 789 | tracemalloc_empty_traceback.frames[0].filename = unknown_filename; |
| 790 | tracemalloc_empty_traceback.frames[0].lineno = 0; |
| 791 | tracemalloc_empty_traceback.hash = traceback_hash(&tracemalloc_empty_traceback); |
| 792 | |
| 793 | /* Disable tracing allocations until hooks are installed. Set |
| 794 | also the reentrant flag to detect bugs: fail with an assertion error |
| 795 | if set_reentrant(1) is called while tracing is disabled. */ |
| 796 | set_reentrant(1); |
| 797 | |
| 798 | tracemalloc_config.initialized = TRACEMALLOC_INITIALIZED; |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | static void |
| 803 | tracemalloc_deinit(void) |
| 804 | { |
| 805 | if (tracemalloc_config.initialized != TRACEMALLOC_INITIALIZED) |
| 806 | return; |
| 807 | tracemalloc_config.initialized = TRACEMALLOC_FINALIZED; |
| 808 | |
| 809 | tracemalloc_stop(); |
| 810 | |
| 811 | /* destroy hash tables */ |
| 812 | _Py_hashtable_destroy(tracemalloc_traces); |
| 813 | _Py_hashtable_destroy(tracemalloc_tracebacks); |
| 814 | _Py_hashtable_destroy(tracemalloc_filenames); |
| 815 | |
| 816 | #if defined(WITH_THREAD) && defined(TRACE_RAW_MALLOC) |
| 817 | if (tables_lock != NULL) { |
| 818 | PyThread_free_lock(tables_lock); |
| 819 | tables_lock = NULL; |
| 820 | } |
| 821 | #endif |
| 822 | |
| 823 | #ifdef REENTRANT_THREADLOCAL |
| 824 | PyThread_delete_key(tracemalloc_reentrant_key); |
| 825 | #endif |
| 826 | |
| 827 | Py_XDECREF(unknown_filename); |
| 828 | } |
| 829 | |
| 830 | static int |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 831 | tracemalloc_start(int max_nframe) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 832 | { |
| 833 | PyMemAllocator alloc; |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 834 | size_t size; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 835 | |
| 836 | if (tracemalloc_init() < 0) |
| 837 | return -1; |
| 838 | |
| 839 | if (tracemalloc_config.tracing) { |
| 840 | /* hook already installed: do nothing */ |
| 841 | return 0; |
| 842 | } |
| 843 | |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 844 | assert(1 <= max_nframe && max_nframe <= MAX_NFRAME); |
| 845 | tracemalloc_config.max_nframe = max_nframe; |
| 846 | |
| 847 | /* allocate a buffer to store a new traceback */ |
| 848 | size = TRACEBACK_SIZE(max_nframe); |
| 849 | assert(tracemalloc_traceback == NULL); |
| 850 | tracemalloc_traceback = raw_malloc(size); |
| 851 | if (tracemalloc_traceback == NULL) { |
| 852 | PyErr_NoMemory(); |
| 853 | return -1; |
| 854 | } |
| 855 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 856 | #ifdef TRACE_RAW_MALLOC |
| 857 | alloc.malloc = tracemalloc_raw_malloc; |
| 858 | alloc.realloc = tracemalloc_raw_realloc; |
| 859 | alloc.free = tracemalloc_free; |
| 860 | |
| 861 | alloc.ctx = &allocators.raw; |
| 862 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw); |
| 863 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc); |
| 864 | #endif |
| 865 | |
| 866 | alloc.malloc = tracemalloc_malloc_gil; |
| 867 | alloc.realloc = tracemalloc_realloc_gil; |
| 868 | alloc.free = tracemalloc_free; |
| 869 | |
| 870 | alloc.ctx = &allocators.mem; |
| 871 | PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem); |
| 872 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc); |
| 873 | |
| 874 | alloc.ctx = &allocators.obj; |
| 875 | PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj); |
| 876 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc); |
| 877 | |
| 878 | /* everything is ready: start tracing Python memory allocations */ |
| 879 | tracemalloc_config.tracing = 1; |
| 880 | set_reentrant(0); |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | static void |
| 886 | tracemalloc_stop(void) |
| 887 | { |
| 888 | if (!tracemalloc_config.tracing) |
| 889 | return; |
| 890 | |
| 891 | /* stop tracing Python memory allocations */ |
| 892 | tracemalloc_config.tracing = 0; |
| 893 | |
| 894 | /* set the reentrant flag to detect bugs: fail with an assertion error if |
| 895 | set_reentrant(1) is called while tracing is disabled. */ |
| 896 | set_reentrant(1); |
| 897 | |
| 898 | /* unregister the hook on memory allocators */ |
| 899 | #ifdef TRACE_RAW_MALLOC |
| 900 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw); |
| 901 | #endif |
| 902 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem); |
| 903 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj); |
| 904 | |
| 905 | /* release memory */ |
| 906 | tracemalloc_clear_traces(); |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 907 | raw_free(tracemalloc_traceback); |
| 908 | tracemalloc_traceback = NULL; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 909 | } |
| 910 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 911 | static PyObject* |
| 912 | lineno_as_obj(int lineno) |
| 913 | { |
| 914 | if (lineno >= 0) |
| 915 | return PyLong_FromLong(lineno); |
| 916 | else |
| 917 | Py_RETURN_NONE; |
| 918 | } |
| 919 | |
| 920 | PyDoc_STRVAR(tracemalloc_is_tracing_doc, |
| 921 | "is_tracing()->bool\n" |
| 922 | "\n" |
| 923 | "True if the tracemalloc module is tracing Python memory allocations,\n" |
| 924 | "False otherwise."); |
| 925 | |
| 926 | static PyObject* |
| 927 | py_tracemalloc_is_tracing(PyObject *self) |
| 928 | { |
| 929 | return PyBool_FromLong(tracemalloc_config.tracing); |
| 930 | } |
| 931 | |
| 932 | PyDoc_STRVAR(tracemalloc_clear_traces_doc, |
| 933 | "clear_traces()\n" |
| 934 | "\n" |
| 935 | "Clear traces of memory blocks allocated by Python."); |
| 936 | |
| 937 | static PyObject* |
| 938 | py_tracemalloc_clear_traces(PyObject *self) |
| 939 | { |
| 940 | if (!tracemalloc_config.tracing) |
| 941 | Py_RETURN_NONE; |
| 942 | |
| 943 | set_reentrant(1); |
| 944 | tracemalloc_clear_traces(); |
| 945 | set_reentrant(0); |
| 946 | |
| 947 | Py_RETURN_NONE; |
| 948 | } |
| 949 | |
| 950 | static PyObject* |
| 951 | frame_to_pyobject(frame_t *frame) |
| 952 | { |
| 953 | PyObject *frame_obj, *lineno_obj; |
| 954 | |
| 955 | frame_obj = PyTuple_New(2); |
| 956 | if (frame_obj == NULL) |
| 957 | return NULL; |
| 958 | |
| 959 | if (frame->filename == NULL) |
| 960 | frame->filename = Py_None; |
| 961 | Py_INCREF(frame->filename); |
| 962 | PyTuple_SET_ITEM(frame_obj, 0, frame->filename); |
| 963 | |
| 964 | assert(frame->lineno >= 0); |
| 965 | lineno_obj = lineno_as_obj(frame->lineno); |
| 966 | if (lineno_obj == NULL) { |
| 967 | Py_DECREF(frame_obj); |
| 968 | return NULL; |
| 969 | } |
| 970 | PyTuple_SET_ITEM(frame_obj, 1, lineno_obj); |
| 971 | |
| 972 | return frame_obj; |
| 973 | } |
| 974 | |
| 975 | static PyObject* |
| 976 | traceback_to_pyobject(traceback_t *traceback, _Py_hashtable_t *intern_table) |
| 977 | { |
| 978 | int i; |
| 979 | PyObject *frames, *frame; |
| 980 | |
| 981 | if (intern_table != NULL) { |
| 982 | if (_Py_HASHTABLE_GET(intern_table, traceback, frames)) { |
| 983 | Py_INCREF(frames); |
| 984 | return frames; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | frames = PyTuple_New(traceback->nframe); |
| 989 | if (frames == NULL) |
| 990 | return NULL; |
| 991 | |
| 992 | for (i=0; i < traceback->nframe; i++) { |
| 993 | frame = frame_to_pyobject(&traceback->frames[i]); |
| 994 | if (frame == NULL) { |
| 995 | Py_DECREF(frames); |
| 996 | return NULL; |
| 997 | } |
| 998 | PyTuple_SET_ITEM(frames, i, frame); |
| 999 | } |
| 1000 | |
| 1001 | if (intern_table != NULL) { |
| 1002 | if (_Py_HASHTABLE_SET(intern_table, traceback, frames) < 0) { |
| 1003 | Py_DECREF(frames); |
| 1004 | PyErr_NoMemory(); |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | /* intern_table keeps a new reference to frames */ |
| 1008 | Py_INCREF(frames); |
| 1009 | } |
| 1010 | return frames; |
| 1011 | } |
| 1012 | |
| 1013 | static PyObject* |
| 1014 | trace_to_pyobject(trace_t *trace, _Py_hashtable_t *intern_tracebacks) |
| 1015 | { |
| 1016 | PyObject *trace_obj = NULL; |
| 1017 | PyObject *size, *traceback; |
| 1018 | |
| 1019 | trace_obj = PyTuple_New(2); |
| 1020 | if (trace_obj == NULL) |
| 1021 | return NULL; |
| 1022 | |
| 1023 | size = PyLong_FromSize_t(trace->size); |
| 1024 | if (size == NULL) { |
| 1025 | Py_DECREF(trace_obj); |
| 1026 | return NULL; |
| 1027 | } |
| 1028 | PyTuple_SET_ITEM(trace_obj, 0, size); |
| 1029 | |
| 1030 | traceback = traceback_to_pyobject(trace->traceback, intern_tracebacks); |
| 1031 | if (traceback == NULL) { |
| 1032 | Py_DECREF(trace_obj); |
| 1033 | return NULL; |
| 1034 | } |
| 1035 | PyTuple_SET_ITEM(trace_obj, 1, traceback); |
| 1036 | |
| 1037 | return trace_obj; |
| 1038 | } |
| 1039 | |
| 1040 | typedef struct { |
| 1041 | _Py_hashtable_t *traces; |
| 1042 | _Py_hashtable_t *tracebacks; |
| 1043 | PyObject *list; |
| 1044 | } get_traces_t; |
| 1045 | |
| 1046 | static int |
| 1047 | tracemalloc_get_traces_fill(_Py_hashtable_entry_t *entry, void *user_data) |
| 1048 | { |
| 1049 | get_traces_t *get_traces = user_data; |
| 1050 | trace_t *trace; |
| 1051 | PyObject *tracemalloc_obj; |
| 1052 | int res; |
| 1053 | |
| 1054 | trace = (trace_t *)_PY_HASHTABLE_ENTRY_DATA(entry); |
| 1055 | |
| 1056 | tracemalloc_obj = trace_to_pyobject(trace, get_traces->tracebacks); |
| 1057 | if (tracemalloc_obj == NULL) |
| 1058 | return 1; |
| 1059 | |
| 1060 | res = PyList_Append(get_traces->list, tracemalloc_obj); |
| 1061 | Py_DECREF(tracemalloc_obj); |
| 1062 | if (res < 0) |
| 1063 | return 1; |
| 1064 | |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | static int |
| 1069 | tracemalloc_pyobject_decref_cb(_Py_hashtable_entry_t *entry, void *user_data) |
| 1070 | { |
| 1071 | PyObject *obj = (PyObject *)_Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry); |
| 1072 | Py_DECREF(obj); |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | PyDoc_STRVAR(tracemalloc_get_traces_doc, |
Victor Stinner | 4fbefdb | 2013-11-25 09:33:18 +0100 | [diff] [blame] | 1077 | "_get_traces() -> list\n" |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1078 | "\n" |
| 1079 | "Get traces of all memory blocks allocated by Python.\n" |
| 1080 | "Return a list of (size: int, traceback: tuple) tuples.\n" |
| 1081 | "traceback is a tuple of (filename: str, lineno: int) tuples.\n" |
| 1082 | "\n" |
| 1083 | "Return an empty list if the tracemalloc module is disabled."); |
| 1084 | |
| 1085 | static PyObject* |
| 1086 | py_tracemalloc_get_traces(PyObject *self, PyObject *obj) |
| 1087 | { |
| 1088 | get_traces_t get_traces; |
| 1089 | int err; |
| 1090 | |
| 1091 | get_traces.traces = NULL; |
| 1092 | get_traces.tracebacks = NULL; |
| 1093 | get_traces.list = PyList_New(0); |
| 1094 | if (get_traces.list == NULL) |
| 1095 | goto error; |
| 1096 | |
| 1097 | if (!tracemalloc_config.tracing) |
| 1098 | return get_traces.list; |
| 1099 | |
Victor Stinner | de2f132 | 2013-11-26 00:26:23 +0100 | [diff] [blame] | 1100 | /* the traceback hash table is used temporarily to intern traceback tuple |
| 1101 | of (filename, lineno) tuples */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1102 | get_traces.tracebacks = hashtable_new(sizeof(PyObject *), |
| 1103 | _Py_hashtable_hash_ptr, |
| 1104 | _Py_hashtable_compare_direct); |
| 1105 | if (get_traces.tracebacks == NULL) { |
| 1106 | PyErr_NoMemory(); |
| 1107 | goto error; |
| 1108 | } |
| 1109 | |
| 1110 | TABLES_LOCK(); |
| 1111 | get_traces.traces = _Py_hashtable_copy(tracemalloc_traces); |
| 1112 | TABLES_UNLOCK(); |
| 1113 | |
| 1114 | if (get_traces.traces == NULL) { |
| 1115 | PyErr_NoMemory(); |
| 1116 | goto error; |
| 1117 | } |
| 1118 | |
| 1119 | set_reentrant(1); |
| 1120 | err = _Py_hashtable_foreach(get_traces.traces, |
| 1121 | tracemalloc_get_traces_fill, &get_traces); |
| 1122 | set_reentrant(0); |
| 1123 | if (err) |
| 1124 | goto error; |
| 1125 | |
| 1126 | goto finally; |
| 1127 | |
| 1128 | error: |
| 1129 | Py_CLEAR(get_traces.list); |
| 1130 | |
| 1131 | finally: |
| 1132 | if (get_traces.tracebacks != NULL) { |
| 1133 | _Py_hashtable_foreach(get_traces.tracebacks, |
| 1134 | tracemalloc_pyobject_decref_cb, NULL); |
| 1135 | _Py_hashtable_destroy(get_traces.tracebacks); |
| 1136 | } |
| 1137 | if (get_traces.traces != NULL) |
| 1138 | _Py_hashtable_destroy(get_traces.traces); |
| 1139 | |
| 1140 | return get_traces.list; |
| 1141 | } |
| 1142 | |
| 1143 | PyDoc_STRVAR(tracemalloc_get_object_traceback_doc, |
Victor Stinner | 4fbefdb | 2013-11-25 09:33:18 +0100 | [diff] [blame] | 1144 | "_get_object_traceback(obj)\n" |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1145 | "\n" |
| 1146 | "Get the traceback where the Python object obj was allocated.\n" |
| 1147 | "Return a tuple of (filename: str, lineno: int) tuples.\n" |
| 1148 | "\n" |
| 1149 | "Return None if the tracemalloc module is disabled or did not\n" |
| 1150 | "trace the allocation of the object."); |
| 1151 | |
| 1152 | static PyObject* |
| 1153 | py_tracemalloc_get_object_traceback(PyObject *self, PyObject *obj) |
| 1154 | { |
| 1155 | PyTypeObject *type; |
| 1156 | void *ptr; |
| 1157 | trace_t trace; |
| 1158 | int found; |
| 1159 | |
| 1160 | if (!tracemalloc_config.tracing) |
| 1161 | Py_RETURN_NONE; |
| 1162 | |
| 1163 | type = Py_TYPE(obj); |
| 1164 | if (PyType_IS_GC(type)) |
| 1165 | ptr = (void *)((char *)obj - sizeof(PyGC_Head)); |
| 1166 | else |
| 1167 | ptr = (void *)obj; |
| 1168 | |
| 1169 | TABLES_LOCK(); |
| 1170 | found = _Py_HASHTABLE_GET(tracemalloc_traces, ptr, trace); |
| 1171 | TABLES_UNLOCK(); |
| 1172 | |
| 1173 | if (!found) |
| 1174 | Py_RETURN_NONE; |
| 1175 | |
| 1176 | return traceback_to_pyobject(trace.traceback, NULL); |
| 1177 | } |
| 1178 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1179 | PyDoc_STRVAR(tracemalloc_start_doc, |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1180 | "start(nframe: int=1)\n" |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1181 | "\n" |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1182 | "Start tracing Python memory allocations. Set also the maximum number \n" |
| 1183 | "of frames stored in the traceback of a trace to nframe."); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1184 | |
| 1185 | static PyObject* |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1186 | py_tracemalloc_start(PyObject *self, PyObject *args) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1187 | { |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1188 | Py_ssize_t nframe = 1; |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1189 | int nframe_int; |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1190 | |
| 1191 | if (!PyArg_ParseTuple(args, "|n:start", &nframe)) |
| 1192 | return NULL; |
| 1193 | |
| 1194 | if (nframe < 1 || nframe > MAX_NFRAME) { |
| 1195 | PyErr_Format(PyExc_ValueError, |
| 1196 | "the number of frames must be in range [1; %i]", |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1197 | (int)MAX_NFRAME); |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1198 | return NULL; |
| 1199 | } |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1200 | nframe_int = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int); |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1201 | |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1202 | if (tracemalloc_start(nframe_int) < 0) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1203 | return NULL; |
| 1204 | |
| 1205 | Py_RETURN_NONE; |
| 1206 | } |
| 1207 | |
| 1208 | PyDoc_STRVAR(tracemalloc_stop_doc, |
| 1209 | "stop()\n" |
| 1210 | "\n" |
| 1211 | "Stop tracing Python memory allocations and clear traces\n" |
| 1212 | "of memory blocks allocated by Python."); |
| 1213 | |
| 1214 | static PyObject* |
| 1215 | py_tracemalloc_stop(PyObject *self) |
| 1216 | { |
| 1217 | tracemalloc_stop(); |
| 1218 | Py_RETURN_NONE; |
| 1219 | } |
| 1220 | |
| 1221 | PyDoc_STRVAR(tracemalloc_get_traceback_limit_doc, |
| 1222 | "get_traceback_limit() -> int\n" |
| 1223 | "\n" |
| 1224 | "Get the maximum number of frames stored in the traceback\n" |
| 1225 | "of a trace.\n" |
| 1226 | "\n" |
| 1227 | "By default, a trace of an allocated memory block only stores\n" |
| 1228 | "the most recent frame: the limit is 1."); |
| 1229 | |
| 1230 | static PyObject* |
| 1231 | py_tracemalloc_get_traceback_limit(PyObject *self) |
| 1232 | { |
| 1233 | return PyLong_FromLong(tracemalloc_config.max_nframe); |
| 1234 | } |
| 1235 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1236 | PyDoc_STRVAR(tracemalloc_get_tracemalloc_memory_doc, |
| 1237 | "get_tracemalloc_memory() -> int\n" |
| 1238 | "\n" |
| 1239 | "Get the memory usage in bytes of the tracemalloc module\n" |
| 1240 | "used internally to trace memory allocations."); |
| 1241 | |
| 1242 | static PyObject* |
| 1243 | tracemalloc_get_tracemalloc_memory(PyObject *self) |
| 1244 | { |
| 1245 | size_t size; |
| 1246 | PyObject *size_obj; |
| 1247 | |
| 1248 | size = _Py_hashtable_size(tracemalloc_tracebacks); |
| 1249 | size += _Py_hashtable_size(tracemalloc_filenames); |
| 1250 | |
| 1251 | TABLES_LOCK(); |
| 1252 | size += _Py_hashtable_size(tracemalloc_traces); |
| 1253 | TABLES_UNLOCK(); |
| 1254 | |
| 1255 | size_obj = PyLong_FromSize_t(size); |
| 1256 | return Py_BuildValue("N", size_obj); |
| 1257 | } |
| 1258 | |
| 1259 | PyDoc_STRVAR(tracemalloc_get_traced_memory_doc, |
Victor Stinner | 59463d8 | 2013-11-26 10:46:06 +0100 | [diff] [blame] | 1260 | "get_traced_memory() -> (int, int)\n" |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1261 | "\n" |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 1262 | "Get the current size and peak size of memory blocks traced\n" |
| 1263 | "by the tracemalloc module as a tuple: (current: int, peak: int)."); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1264 | |
| 1265 | static PyObject* |
| 1266 | tracemalloc_get_traced_memory(PyObject *self) |
| 1267 | { |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 1268 | Py_ssize_t size, peak_size; |
| 1269 | PyObject *size_obj, *peak_size_obj; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1270 | |
| 1271 | if (!tracemalloc_config.tracing) |
| 1272 | return Py_BuildValue("ii", 0, 0); |
| 1273 | |
| 1274 | TABLES_LOCK(); |
| 1275 | size = tracemalloc_traced_memory; |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 1276 | peak_size = tracemalloc_peak_traced_memory; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1277 | TABLES_UNLOCK(); |
| 1278 | |
| 1279 | size_obj = PyLong_FromSize_t(size); |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 1280 | peak_size_obj = PyLong_FromSize_t(peak_size); |
| 1281 | return Py_BuildValue("NN", size_obj, peak_size_obj); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | static PyMethodDef module_methods[] = { |
| 1285 | {"is_tracing", (PyCFunction)py_tracemalloc_is_tracing, |
| 1286 | METH_NOARGS, tracemalloc_is_tracing_doc}, |
| 1287 | {"clear_traces", (PyCFunction)py_tracemalloc_clear_traces, |
| 1288 | METH_NOARGS, tracemalloc_clear_traces_doc}, |
| 1289 | {"_get_traces", (PyCFunction)py_tracemalloc_get_traces, |
| 1290 | METH_NOARGS, tracemalloc_get_traces_doc}, |
| 1291 | {"_get_object_traceback", (PyCFunction)py_tracemalloc_get_object_traceback, |
| 1292 | METH_O, tracemalloc_get_object_traceback_doc}, |
| 1293 | {"start", (PyCFunction)py_tracemalloc_start, |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1294 | METH_VARARGS, tracemalloc_start_doc}, |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1295 | {"stop", (PyCFunction)py_tracemalloc_stop, |
| 1296 | METH_NOARGS, tracemalloc_stop_doc}, |
| 1297 | {"get_traceback_limit", (PyCFunction)py_tracemalloc_get_traceback_limit, |
| 1298 | METH_NOARGS, tracemalloc_get_traceback_limit_doc}, |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1299 | {"get_tracemalloc_memory", (PyCFunction)tracemalloc_get_tracemalloc_memory, |
| 1300 | METH_NOARGS, tracemalloc_get_tracemalloc_memory_doc}, |
| 1301 | {"get_traced_memory", (PyCFunction)tracemalloc_get_traced_memory, |
| 1302 | METH_NOARGS, tracemalloc_get_traced_memory_doc}, |
| 1303 | |
| 1304 | /* sentinel */ |
| 1305 | {NULL, NULL} |
| 1306 | }; |
| 1307 | |
| 1308 | PyDoc_STRVAR(module_doc, |
| 1309 | "Debug module to trace memory blocks allocated by Python."); |
| 1310 | |
| 1311 | static struct PyModuleDef module_def = { |
| 1312 | PyModuleDef_HEAD_INIT, |
| 1313 | "_tracemalloc", |
| 1314 | module_doc, |
| 1315 | 0, /* non-negative size to be able to unload the module */ |
| 1316 | module_methods, |
| 1317 | NULL, |
| 1318 | }; |
| 1319 | |
| 1320 | PyMODINIT_FUNC |
| 1321 | PyInit__tracemalloc(void) |
| 1322 | { |
| 1323 | PyObject *m; |
| 1324 | m = PyModule_Create(&module_def); |
| 1325 | if (m == NULL) |
| 1326 | return NULL; |
| 1327 | |
| 1328 | if (tracemalloc_init() < 0) |
| 1329 | return NULL; |
| 1330 | |
| 1331 | return m; |
| 1332 | } |
| 1333 | |
| 1334 | static int |
| 1335 | parse_sys_xoptions(PyObject *value) |
| 1336 | { |
| 1337 | PyObject *valuelong; |
| 1338 | long nframe; |
| 1339 | |
| 1340 | if (value == Py_True) |
| 1341 | return 1; |
| 1342 | |
| 1343 | assert(PyUnicode_Check(value)); |
| 1344 | if (PyUnicode_GetLength(value) == 0) |
| 1345 | return -1; |
| 1346 | |
| 1347 | valuelong = PyLong_FromUnicodeObject(value, 10); |
| 1348 | if (valuelong == NULL) |
| 1349 | return -1; |
| 1350 | |
| 1351 | nframe = PyLong_AsLong(valuelong); |
| 1352 | Py_DECREF(valuelong); |
| 1353 | if (nframe == -1 && PyErr_Occurred()) |
| 1354 | return -1; |
| 1355 | |
| 1356 | if (nframe < 1 || nframe > MAX_NFRAME) |
| 1357 | return -1; |
| 1358 | |
| 1359 | return Py_SAFE_DOWNCAST(nframe, long, int); |
| 1360 | } |
| 1361 | |
| 1362 | int |
| 1363 | _PyTraceMalloc_Init(void) |
| 1364 | { |
| 1365 | char *p; |
| 1366 | int nframe; |
| 1367 | |
| 1368 | #ifdef WITH_THREAD |
| 1369 | assert(PyGILState_Check()); |
| 1370 | #endif |
| 1371 | |
| 1372 | if ((p = Py_GETENV("PYTHONTRACEMALLOC")) && *p != '\0') { |
| 1373 | char *endptr = p; |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1374 | long value; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1375 | |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1376 | value = strtol(p, &endptr, 10); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1377 | if (*endptr != '\0' |
| 1378 | || value < 1 |
| 1379 | || value > MAX_NFRAME |
| 1380 | || (errno == ERANGE && value == ULONG_MAX)) |
| 1381 | { |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1382 | Py_FatalError("PYTHONTRACEMALLOC: invalid number of frames"); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1383 | return -1; |
| 1384 | } |
| 1385 | |
| 1386 | nframe = (int)value; |
| 1387 | } |
| 1388 | else { |
| 1389 | PyObject *xoptions, *key, *value; |
| 1390 | |
| 1391 | xoptions = PySys_GetXOptions(); |
| 1392 | if (xoptions == NULL) |
| 1393 | return -1; |
| 1394 | |
| 1395 | key = PyUnicode_FromString("tracemalloc"); |
| 1396 | if (key == NULL) |
| 1397 | return -1; |
| 1398 | |
| 1399 | value = PyDict_GetItemWithError(xoptions, key); |
| 1400 | Py_DECREF(key); |
| 1401 | if (value == NULL) { |
| 1402 | if (PyErr_Occurred()) |
| 1403 | return -1; |
| 1404 | |
| 1405 | /* -X tracemalloc is not used */ |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
| 1409 | nframe = parse_sys_xoptions(value); |
| 1410 | Py_DECREF(value); |
| 1411 | if (nframe < 0) { |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1412 | Py_FatalError("-X tracemalloc=NFRAME: invalid number of frames"); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1416 | return tracemalloc_start(nframe); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1417 | } |
| 1418 | |
Victor Stinner | be0708f | 2013-12-01 10:03:26 +0100 | [diff] [blame] | 1419 | void |
| 1420 | _PyTraceMalloc_Fini(void) |
| 1421 | { |
| 1422 | #ifdef WITH_THREAD |
| 1423 | assert(PyGILState_Check()); |
| 1424 | #endif |
| 1425 | tracemalloc_deinit(); |
| 1426 | } |
| 1427 | |