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