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 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 7 | #include "clinic/_tracemalloc.c.h" |
| 8 | /*[clinic input] |
| 9 | module _tracemalloc |
| 10 | [clinic start generated code]*/ |
| 11 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=708a98302fc46e5f]*/ |
| 12 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 13 | /* Trace memory blocks allocated by PyMem_RawMalloc() */ |
| 14 | #define TRACE_RAW_MALLOC |
| 15 | |
| 16 | /* Forward declaration */ |
| 17 | static void tracemalloc_stop(void); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 18 | static void* raw_malloc(size_t size); |
| 19 | static void raw_free(void *ptr); |
| 20 | |
| 21 | #ifdef Py_DEBUG |
| 22 | # define TRACE_DEBUG |
| 23 | #endif |
| 24 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 25 | /* Protected by the GIL */ |
| 26 | static struct { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 27 | PyMemAllocatorEx mem; |
| 28 | PyMemAllocatorEx raw; |
| 29 | PyMemAllocatorEx obj; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 30 | } allocators; |
| 31 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 32 | static struct { |
| 33 | /* Module initialized? |
| 34 | Variable protected by the GIL */ |
| 35 | enum { |
| 36 | TRACEMALLOC_NOT_INITIALIZED, |
| 37 | TRACEMALLOC_INITIALIZED, |
| 38 | TRACEMALLOC_FINALIZED |
| 39 | } initialized; |
| 40 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 41 | /* Is tracemalloc tracing memory allocations? |
| 42 | Variable protected by the GIL */ |
| 43 | int tracing; |
| 44 | |
| 45 | /* limit of the number of frames in a traceback, 1 by default. |
| 46 | Variable protected by the GIL. */ |
| 47 | int max_nframe; |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 48 | |
| 49 | /* use domain in trace key? |
| 50 | Variable protected by the GIL. */ |
| 51 | int use_domain; |
Victor Stinner | 5e14a38 | 2016-03-23 22:03:55 +0100 | [diff] [blame] | 52 | } tracemalloc_config = {TRACEMALLOC_NOT_INITIALIZED, 0, 1, 0}; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 53 | |
| 54 | #if defined(TRACE_RAW_MALLOC) && defined(WITH_THREAD) |
| 55 | /* This lock is needed because tracemalloc_free() is called without |
| 56 | the GIL held from PyMem_RawFree(). It cannot acquire the lock because it |
| 57 | would introduce a deadlock in PyThreadState_DeleteCurrent(). */ |
| 58 | static PyThread_type_lock tables_lock; |
| 59 | # define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1) |
| 60 | # define TABLES_UNLOCK() PyThread_release_lock(tables_lock) |
| 61 | #else |
| 62 | /* variables are protected by the GIL */ |
| 63 | # define TABLES_LOCK() |
| 64 | # define TABLES_UNLOCK() |
| 65 | #endif |
| 66 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 67 | |
| 68 | #define DEFAULT_DOMAIN 0 |
| 69 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 70 | /* Pack the frame_t structure to reduce the memory footprint. */ |
| 71 | typedef struct |
| 72 | #ifdef __GNUC__ |
| 73 | __attribute__((packed)) |
| 74 | #endif |
| 75 | { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 76 | uintptr_t ptr; |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 77 | unsigned int domain; |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 78 | } pointer_t; |
| 79 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 80 | /* Pack the frame_t structure to reduce the memory footprint on 64-bit |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 81 | architectures: 12 bytes instead of 16. */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 82 | typedef struct |
| 83 | #ifdef __GNUC__ |
| 84 | __attribute__((packed)) |
Victor Stinner | dd382ef | 2014-02-01 03:43:58 +0100 | [diff] [blame] | 85 | #elif defined(_MSC_VER) |
Segev Finer | 3924377 | 2017-07-25 11:47:43 +0300 | [diff] [blame] | 86 | #pragma pack(push, 4) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 87 | #endif |
| 88 | { |
Victor Stinner | 7105e9f | 2016-03-15 14:28:04 +0100 | [diff] [blame] | 89 | /* filename cannot be NULL: "<unknown>" is used if the Python frame |
| 90 | filename is NULL */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 91 | PyObject *filename; |
Victor Stinner | 9528334 | 2016-03-15 21:57:02 +0100 | [diff] [blame] | 92 | unsigned int lineno; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 93 | } frame_t; |
Segev Finer | 3924377 | 2017-07-25 11:47:43 +0300 | [diff] [blame] | 94 | #ifdef _MSC_VER |
| 95 | #pragma pack(pop) |
| 96 | #endif |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 97 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 98 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 99 | typedef struct { |
| 100 | Py_uhash_t hash; |
| 101 | int nframe; |
| 102 | frame_t frames[1]; |
| 103 | } traceback_t; |
| 104 | |
| 105 | #define TRACEBACK_SIZE(NFRAME) \ |
| 106 | (sizeof(traceback_t) + sizeof(frame_t) * (NFRAME - 1)) |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 107 | |
| 108 | #define MAX_NFRAME \ |
Victor Stinner | 52aee85 | 2014-08-16 15:44:02 +0200 | [diff] [blame] | 109 | ((INT_MAX - (int)sizeof(traceback_t)) / (int)sizeof(frame_t) + 1) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 110 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 111 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 112 | static PyObject *unknown_filename = NULL; |
| 113 | static traceback_t tracemalloc_empty_traceback; |
| 114 | |
Victor Stinner | 7a5be14 | 2013-11-26 01:06:02 +0100 | [diff] [blame] | 115 | /* Trace of a memory block */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 116 | typedef struct { |
Victor Stinner | 7a5be14 | 2013-11-26 01:06:02 +0100 | [diff] [blame] | 117 | /* Size of the memory block in bytes */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 118 | size_t size; |
Victor Stinner | 7a5be14 | 2013-11-26 01:06:02 +0100 | [diff] [blame] | 119 | |
| 120 | /* Traceback where the memory block was allocated */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 121 | traceback_t *traceback; |
| 122 | } trace_t; |
| 123 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 124 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 125 | /* Size in bytes of currently traced memory. |
| 126 | Protected by TABLES_LOCK(). */ |
| 127 | static size_t tracemalloc_traced_memory = 0; |
| 128 | |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 129 | /* Peak size in bytes of traced memory. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 130 | Protected by TABLES_LOCK(). */ |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 131 | static size_t tracemalloc_peak_traced_memory = 0; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 132 | |
Serhiy Storchaka | 56a6d85 | 2014-12-01 18:28:43 +0200 | [diff] [blame] | 133 | /* Hash table used as a set to intern filenames: |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 134 | PyObject* => PyObject*. |
| 135 | Protected by the GIL */ |
| 136 | static _Py_hashtable_t *tracemalloc_filenames = NULL; |
| 137 | |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 138 | /* Buffer to store a new traceback in traceback_new(). |
| 139 | Protected by the GIL. */ |
| 140 | static traceback_t *tracemalloc_traceback = NULL; |
| 141 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 142 | /* Hash table used as a set to intern tracebacks: |
| 143 | traceback_t* => traceback_t* |
| 144 | Protected by the GIL */ |
| 145 | static _Py_hashtable_t *tracemalloc_tracebacks = NULL; |
| 146 | |
| 147 | /* pointer (void*) => trace (trace_t). |
| 148 | Protected by TABLES_LOCK(). */ |
| 149 | static _Py_hashtable_t *tracemalloc_traces = NULL; |
| 150 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 151 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 152 | #ifdef TRACE_DEBUG |
| 153 | static void |
| 154 | tracemalloc_error(const char *format, ...) |
| 155 | { |
| 156 | va_list ap; |
| 157 | fprintf(stderr, "tracemalloc: "); |
| 158 | va_start(ap, format); |
| 159 | vfprintf(stderr, format, ap); |
| 160 | va_end(ap); |
| 161 | fprintf(stderr, "\n"); |
| 162 | fflush(stderr); |
| 163 | } |
| 164 | #endif |
| 165 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 166 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 167 | #if defined(WITH_THREAD) && defined(TRACE_RAW_MALLOC) |
| 168 | #define REENTRANT_THREADLOCAL |
| 169 | |
| 170 | /* If your OS does not provide native thread local storage, you can implement |
| 171 | it manually using a lock. Functions of thread.c cannot be used because |
| 172 | they use PyMem_RawMalloc() which leads to a reentrant call. */ |
| 173 | #if !(defined(_POSIX_THREADS) || defined(NT_THREADS)) |
| 174 | # error "need native thread local storage (TLS)" |
| 175 | #endif |
| 176 | |
Victor Stinner | 4a06647 | 2016-03-22 17:45:09 +0100 | [diff] [blame] | 177 | static int tracemalloc_reentrant_key = -1; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 178 | |
| 179 | /* Any non-NULL pointer can be used */ |
| 180 | #define REENTRANT Py_True |
| 181 | |
| 182 | static int |
| 183 | get_reentrant(void) |
| 184 | { |
Victor Stinner | 4a06647 | 2016-03-22 17:45:09 +0100 | [diff] [blame] | 185 | void *ptr; |
| 186 | |
| 187 | assert(tracemalloc_reentrant_key != -1); |
| 188 | ptr = PyThread_get_key_value(tracemalloc_reentrant_key); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 189 | if (ptr != NULL) { |
| 190 | assert(ptr == REENTRANT); |
| 191 | return 1; |
| 192 | } |
| 193 | else |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static void |
| 198 | set_reentrant(int reentrant) |
| 199 | { |
Victor Stinner | 2ead3d2 | 2013-11-26 01:08:53 +0100 | [diff] [blame] | 200 | assert(reentrant == 0 || reentrant == 1); |
Victor Stinner | 4a06647 | 2016-03-22 17:45:09 +0100 | [diff] [blame] | 201 | assert(tracemalloc_reentrant_key != -1); |
| 202 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 203 | if (reentrant) { |
Victor Stinner | 0cfc058 | 2016-03-22 17:40:07 +0100 | [diff] [blame] | 204 | assert(!get_reentrant()); |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 205 | PyThread_set_key_value(tracemalloc_reentrant_key, REENTRANT); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 206 | } |
| 207 | else { |
Victor Stinner | 0cfc058 | 2016-03-22 17:40:07 +0100 | [diff] [blame] | 208 | assert(get_reentrant()); |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 209 | PyThread_set_key_value(tracemalloc_reentrant_key, NULL); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | #else |
| 214 | |
| 215 | /* WITH_THREAD not defined: Python compiled without threads, |
| 216 | or TRACE_RAW_MALLOC not defined: variable protected by the GIL */ |
| 217 | static int tracemalloc_reentrant = 0; |
| 218 | |
| 219 | static int |
| 220 | get_reentrant(void) |
| 221 | { |
| 222 | return tracemalloc_reentrant; |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | set_reentrant(int reentrant) |
| 227 | { |
Victor Stinner | d5871e6 | 2016-03-23 00:17:04 +0100 | [diff] [blame] | 228 | assert(reentrant != tracemalloc_reentrant); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 229 | tracemalloc_reentrant = reentrant; |
| 230 | } |
| 231 | #endif |
| 232 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 233 | |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 234 | static Py_uhash_t |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 235 | hashtable_hash_pyobject(_Py_hashtable_t *ht, const void *pkey) |
Victor Stinner | 51b846c | 2016-03-18 21:52:22 +0100 | [diff] [blame] | 236 | { |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 237 | PyObject *obj; |
| 238 | |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 239 | _Py_HASHTABLE_READ_KEY(ht, pkey, obj); |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 240 | return PyObject_Hash(obj); |
| 241 | } |
| 242 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 243 | |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 244 | static int |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 245 | hashtable_compare_unicode(_Py_hashtable_t *ht, const void *pkey, |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 246 | const _Py_hashtable_entry_t *entry) |
| 247 | { |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 248 | PyObject *key1, *key2; |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 249 | |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 250 | _Py_HASHTABLE_READ_KEY(ht, pkey, key1); |
| 251 | _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, key2); |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 252 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 253 | if (key1 != NULL && key2 != NULL) |
| 254 | return (PyUnicode_Compare(key1, key2) == 0); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 255 | else |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 256 | return key1 == key2; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 259 | |
| 260 | static Py_uhash_t |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 261 | hashtable_hash_pointer_t(_Py_hashtable_t *ht, const void *pkey) |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 262 | { |
| 263 | pointer_t ptr; |
| 264 | Py_uhash_t hash; |
| 265 | |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 266 | _Py_HASHTABLE_READ_KEY(ht, pkey, ptr); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 267 | |
| 268 | hash = (Py_uhash_t)_Py_HashPointer((void*)ptr.ptr); |
| 269 | hash ^= ptr.domain; |
| 270 | return hash; |
| 271 | } |
| 272 | |
| 273 | |
doko@ubuntu.com | bc73150 | 2016-05-18 01:06:01 +0200 | [diff] [blame] | 274 | static int |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 275 | hashtable_compare_pointer_t(_Py_hashtable_t *ht, const void *pkey, |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 276 | const _Py_hashtable_entry_t *entry) |
| 277 | { |
| 278 | pointer_t ptr1, ptr2; |
| 279 | |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 280 | _Py_HASHTABLE_READ_KEY(ht, pkey, ptr1); |
| 281 | _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, ptr2); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 282 | |
| 283 | /* compare pointer before domain, because pointer is more likely to be |
| 284 | different */ |
| 285 | return (ptr1.ptr == ptr2.ptr && ptr1.domain == ptr2.domain); |
| 286 | |
| 287 | } |
| 288 | |
| 289 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 290 | static _Py_hashtable_t * |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 291 | hashtable_new(size_t key_size, size_t data_size, |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 292 | _Py_hashtable_hash_func hash_func, |
| 293 | _Py_hashtable_compare_func compare_func) |
| 294 | { |
Victor Stinner | c955387 | 2016-03-22 12:13:01 +0100 | [diff] [blame] | 295 | _Py_hashtable_allocator_t hashtable_alloc = {malloc, free}; |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 296 | return _Py_hashtable_new_full(key_size, data_size, 0, |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 297 | hash_func, compare_func, |
Victor Stinner | c955387 | 2016-03-22 12:13:01 +0100 | [diff] [blame] | 298 | &hashtable_alloc); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 299 | } |
| 300 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 301 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 302 | static void* |
| 303 | raw_malloc(size_t size) |
| 304 | { |
| 305 | return allocators.raw.malloc(allocators.raw.ctx, size); |
| 306 | } |
| 307 | |
| 308 | static void |
| 309 | raw_free(void *ptr) |
| 310 | { |
| 311 | allocators.raw.free(allocators.raw.ctx, ptr); |
| 312 | } |
| 313 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 314 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 315 | static Py_uhash_t |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 316 | hashtable_hash_traceback(_Py_hashtable_t *ht, const void *pkey) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 317 | { |
Victor Stinner | b32a7ed | 2016-03-21 23:05:08 +0100 | [diff] [blame] | 318 | traceback_t *traceback; |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 319 | |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 320 | _Py_HASHTABLE_READ_KEY(ht, pkey, traceback); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 321 | return traceback->hash; |
| 322 | } |
| 323 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 324 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 325 | static int |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 326 | hashtable_compare_traceback(_Py_hashtable_t *ht, const void *pkey, |
| 327 | const _Py_hashtable_entry_t *entry) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 328 | { |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 329 | traceback_t *traceback1, *traceback2; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 330 | const frame_t *frame1, *frame2; |
| 331 | int i; |
| 332 | |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 333 | _Py_HASHTABLE_READ_KEY(ht, pkey, traceback1); |
| 334 | _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, traceback2); |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 335 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 336 | if (traceback1->nframe != traceback2->nframe) |
| 337 | return 0; |
| 338 | |
| 339 | for (i=0; i < traceback1->nframe; i++) { |
| 340 | frame1 = &traceback1->frames[i]; |
| 341 | frame2 = &traceback2->frames[i]; |
| 342 | |
| 343 | if (frame1->lineno != frame2->lineno) |
| 344 | return 0; |
| 345 | |
| 346 | if (frame1->filename != frame2->filename) { |
| 347 | assert(PyUnicode_Compare(frame1->filename, frame2->filename) != 0); |
| 348 | return 0; |
| 349 | } |
| 350 | } |
| 351 | return 1; |
| 352 | } |
| 353 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 354 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 355 | static void |
| 356 | tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame) |
| 357 | { |
| 358 | PyCodeObject *code; |
| 359 | PyObject *filename; |
| 360 | _Py_hashtable_entry_t *entry; |
Victor Stinner | 9528334 | 2016-03-15 21:57:02 +0100 | [diff] [blame] | 361 | int lineno; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 362 | |
| 363 | frame->filename = unknown_filename; |
Victor Stinner | 9528334 | 2016-03-15 21:57:02 +0100 | [diff] [blame] | 364 | lineno = PyFrame_GetLineNumber(pyframe); |
| 365 | if (lineno < 0) |
| 366 | lineno = 0; |
| 367 | frame->lineno = (unsigned int)lineno; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 368 | |
| 369 | code = pyframe->f_code; |
| 370 | if (code == NULL) { |
| 371 | #ifdef TRACE_DEBUG |
Victor Stinner | 4dc7420 | 2013-11-26 01:18:52 +0100 | [diff] [blame] | 372 | tracemalloc_error("failed to get the code object of the frame"); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 373 | #endif |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | if (code->co_filename == NULL) { |
| 378 | #ifdef TRACE_DEBUG |
| 379 | tracemalloc_error("failed to get the filename of the code object"); |
| 380 | #endif |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | filename = code->co_filename; |
| 385 | assert(filename != NULL); |
| 386 | if (filename == NULL) |
| 387 | return; |
| 388 | |
| 389 | if (!PyUnicode_Check(filename)) { |
| 390 | #ifdef TRACE_DEBUG |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 391 | tracemalloc_error("filename is not a unicode string"); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 392 | #endif |
| 393 | return; |
| 394 | } |
| 395 | if (!PyUnicode_IS_READY(filename)) { |
| 396 | /* Don't make a Unicode string ready to avoid reentrant calls |
| 397 | to tracemalloc_malloc() or tracemalloc_realloc() */ |
| 398 | #ifdef TRACE_DEBUG |
| 399 | tracemalloc_error("filename is not a ready unicode string"); |
| 400 | #endif |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | /* intern the filename */ |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 405 | entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_filenames, filename); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 406 | if (entry != NULL) { |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 407 | _Py_HASHTABLE_ENTRY_READ_KEY(tracemalloc_filenames, entry, filename); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 408 | } |
| 409 | else { |
| 410 | /* tracemalloc_filenames is responsible to keep a reference |
| 411 | to the filename */ |
| 412 | Py_INCREF(filename); |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 413 | if (_Py_HASHTABLE_SET_NODATA(tracemalloc_filenames, filename) < 0) { |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 414 | Py_DECREF(filename); |
| 415 | #ifdef TRACE_DEBUG |
| 416 | tracemalloc_error("failed to intern the filename"); |
| 417 | #endif |
| 418 | return; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /* the tracemalloc_filenames table keeps a reference to the filename */ |
| 423 | frame->filename = filename; |
| 424 | } |
| 425 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 426 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 427 | static Py_uhash_t |
| 428 | traceback_hash(traceback_t *traceback) |
| 429 | { |
| 430 | /* code based on tuplehash() of Objects/tupleobject.c */ |
Victor Stinner | 4d8c29c | 2013-12-16 23:05:13 +0100 | [diff] [blame] | 431 | Py_uhash_t x, y; /* Unsigned for defined overflow behavior. */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 432 | int len = traceback->nframe; |
| 433 | Py_uhash_t mult = _PyHASH_MULTIPLIER; |
| 434 | frame_t *frame; |
| 435 | |
| 436 | x = 0x345678UL; |
| 437 | frame = traceback->frames; |
| 438 | while (--len >= 0) { |
Victor Stinner | 4d8c29c | 2013-12-16 23:05:13 +0100 | [diff] [blame] | 439 | y = (Py_uhash_t)PyObject_Hash(frame->filename); |
| 440 | y ^= (Py_uhash_t)frame->lineno; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 441 | frame++; |
| 442 | |
| 443 | x = (x ^ y) * mult; |
| 444 | /* the cast might truncate len; that doesn't change hash stability */ |
Victor Stinner | 4d8c29c | 2013-12-16 23:05:13 +0100 | [diff] [blame] | 445 | mult += (Py_uhash_t)(82520UL + len + len); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 446 | } |
| 447 | x += 97531UL; |
| 448 | return x; |
| 449 | } |
| 450 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 451 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 452 | static void |
| 453 | traceback_get_frames(traceback_t *traceback) |
| 454 | { |
| 455 | PyThreadState *tstate; |
| 456 | PyFrameObject *pyframe; |
| 457 | |
| 458 | #ifdef WITH_THREAD |
| 459 | tstate = PyGILState_GetThisThreadState(); |
| 460 | #else |
| 461 | tstate = PyThreadState_Get(); |
| 462 | #endif |
| 463 | if (tstate == NULL) { |
| 464 | #ifdef TRACE_DEBUG |
| 465 | tracemalloc_error("failed to get the current thread state"); |
| 466 | #endif |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | for (pyframe = tstate->frame; pyframe != NULL; pyframe = pyframe->f_back) { |
| 471 | tracemalloc_get_frame(pyframe, &traceback->frames[traceback->nframe]); |
| 472 | assert(traceback->frames[traceback->nframe].filename != NULL); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 473 | traceback->nframe++; |
| 474 | if (traceback->nframe == tracemalloc_config.max_nframe) |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 479 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 480 | static traceback_t * |
| 481 | traceback_new(void) |
| 482 | { |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 483 | traceback_t *traceback; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 484 | _Py_hashtable_entry_t *entry; |
| 485 | |
| 486 | #ifdef WITH_THREAD |
| 487 | assert(PyGILState_Check()); |
| 488 | #endif |
| 489 | |
| 490 | /* get frames */ |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 491 | traceback = tracemalloc_traceback; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 492 | traceback->nframe = 0; |
| 493 | traceback_get_frames(traceback); |
| 494 | if (traceback->nframe == 0) |
| 495 | return &tracemalloc_empty_traceback; |
| 496 | traceback->hash = traceback_hash(traceback); |
| 497 | |
| 498 | /* intern the traceback */ |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 499 | entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_tracebacks, traceback); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 500 | if (entry != NULL) { |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 501 | _Py_HASHTABLE_ENTRY_READ_KEY(tracemalloc_tracebacks, entry, traceback); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 502 | } |
| 503 | else { |
| 504 | traceback_t *copy; |
| 505 | size_t traceback_size; |
| 506 | |
| 507 | traceback_size = TRACEBACK_SIZE(traceback->nframe); |
| 508 | |
| 509 | copy = raw_malloc(traceback_size); |
| 510 | if (copy == NULL) { |
| 511 | #ifdef TRACE_DEBUG |
| 512 | tracemalloc_error("failed to intern the traceback: malloc failed"); |
| 513 | #endif |
| 514 | return NULL; |
| 515 | } |
| 516 | memcpy(copy, traceback, traceback_size); |
| 517 | |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 518 | if (_Py_HASHTABLE_SET_NODATA(tracemalloc_tracebacks, copy) < 0) { |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 519 | raw_free(copy); |
| 520 | #ifdef TRACE_DEBUG |
| 521 | tracemalloc_error("failed to intern the traceback: putdata failed"); |
| 522 | #endif |
| 523 | return NULL; |
| 524 | } |
| 525 | traceback = copy; |
| 526 | } |
| 527 | return traceback; |
| 528 | } |
| 529 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 530 | |
Victor Stinner | 5e14a38 | 2016-03-23 22:03:55 +0100 | [diff] [blame] | 531 | static int |
| 532 | tracemalloc_use_domain_cb(_Py_hashtable_t *old_traces, |
| 533 | _Py_hashtable_entry_t *entry, void *user_data) |
| 534 | { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 535 | uintptr_t ptr; |
Victor Stinner | 5e14a38 | 2016-03-23 22:03:55 +0100 | [diff] [blame] | 536 | pointer_t key; |
| 537 | _Py_hashtable_t *new_traces = (_Py_hashtable_t *)user_data; |
| 538 | const void *pdata = _Py_HASHTABLE_ENTRY_PDATA(old_traces, entry); |
| 539 | |
| 540 | _Py_HASHTABLE_ENTRY_READ_KEY(old_traces, entry, ptr); |
| 541 | key.ptr = ptr; |
| 542 | key.domain = DEFAULT_DOMAIN; |
| 543 | |
| 544 | return _Py_hashtable_set(new_traces, |
| 545 | sizeof(key), &key, |
| 546 | old_traces->data_size, pdata); |
| 547 | } |
| 548 | |
| 549 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 550 | /* Convert tracemalloc_traces from compact key (uintptr_t) to pointer_t key. |
Victor Stinner | 5e14a38 | 2016-03-23 22:03:55 +0100 | [diff] [blame] | 551 | * Return 0 on success, -1 on error. */ |
| 552 | static int |
| 553 | tracemalloc_use_domain(void) |
| 554 | { |
| 555 | _Py_hashtable_t *new_traces = NULL; |
| 556 | |
| 557 | assert(!tracemalloc_config.use_domain); |
| 558 | |
| 559 | new_traces = hashtable_new(sizeof(pointer_t), |
| 560 | sizeof(trace_t), |
| 561 | hashtable_hash_pointer_t, |
| 562 | hashtable_compare_pointer_t); |
| 563 | if (new_traces == NULL) { |
| 564 | return -1; |
| 565 | } |
| 566 | |
| 567 | if (_Py_hashtable_foreach(tracemalloc_traces, tracemalloc_use_domain_cb, |
| 568 | new_traces) < 0) |
| 569 | { |
| 570 | _Py_hashtable_destroy(new_traces); |
| 571 | return -1; |
| 572 | } |
| 573 | |
| 574 | _Py_hashtable_destroy(tracemalloc_traces); |
| 575 | tracemalloc_traces = new_traces; |
| 576 | |
| 577 | tracemalloc_config.use_domain = 1; |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 583 | static void |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 584 | tracemalloc_remove_trace(unsigned int domain, uintptr_t ptr) |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 585 | { |
| 586 | trace_t trace; |
| 587 | int removed; |
| 588 | |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 589 | assert(tracemalloc_config.tracing); |
| 590 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 591 | if (tracemalloc_config.use_domain) { |
| 592 | pointer_t key = {ptr, domain}; |
| 593 | removed = _Py_HASHTABLE_POP(tracemalloc_traces, key, trace); |
| 594 | } |
| 595 | else { |
| 596 | removed = _Py_HASHTABLE_POP(tracemalloc_traces, ptr, trace); |
| 597 | } |
| 598 | if (!removed) { |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | assert(tracemalloc_traced_memory >= trace.size); |
| 603 | tracemalloc_traced_memory -= trace.size; |
| 604 | } |
| 605 | |
| 606 | #define REMOVE_TRACE(ptr) \ |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 607 | tracemalloc_remove_trace(DEFAULT_DOMAIN, (uintptr_t)(ptr)) |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 608 | |
| 609 | |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 610 | static int |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 611 | tracemalloc_add_trace(unsigned int domain, uintptr_t ptr, |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 612 | size_t size) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 613 | { |
Victor Stinner | ca79ccd | 2016-03-23 09:38:54 +0100 | [diff] [blame] | 614 | pointer_t key = {ptr, domain}; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 615 | traceback_t *traceback; |
| 616 | trace_t trace; |
Victor Stinner | ca79ccd | 2016-03-23 09:38:54 +0100 | [diff] [blame] | 617 | _Py_hashtable_entry_t* entry; |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 618 | int res; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 619 | |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 620 | assert(tracemalloc_config.tracing); |
| 621 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 622 | traceback = traceback_new(); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 623 | if (traceback == NULL) { |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 624 | return -1; |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 625 | } |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 626 | |
Victor Stinner | 5e14a38 | 2016-03-23 22:03:55 +0100 | [diff] [blame] | 627 | if (!tracemalloc_config.use_domain && domain != DEFAULT_DOMAIN) { |
| 628 | /* first trace using a non-zero domain whereas traces use compact |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 629 | (uintptr_t) keys: switch to pointer_t keys. */ |
Victor Stinner | 5e14a38 | 2016-03-23 22:03:55 +0100 | [diff] [blame] | 630 | if (tracemalloc_use_domain() < 0) { |
| 631 | return -1; |
| 632 | } |
| 633 | } |
| 634 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 635 | if (tracemalloc_config.use_domain) { |
Victor Stinner | ca79ccd | 2016-03-23 09:38:54 +0100 | [diff] [blame] | 636 | entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_traces, key); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 637 | } |
| 638 | else { |
Victor Stinner | ca79ccd | 2016-03-23 09:38:54 +0100 | [diff] [blame] | 639 | entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_traces, ptr); |
Victor Stinner | d606ba7 | 2013-11-24 11:28:20 +0100 | [diff] [blame] | 640 | } |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 641 | |
Victor Stinner | ca79ccd | 2016-03-23 09:38:54 +0100 | [diff] [blame] | 642 | if (entry != NULL) { |
| 643 | /* the memory block is already tracked */ |
| 644 | _Py_HASHTABLE_ENTRY_READ_DATA(tracemalloc_traces, entry, trace); |
| 645 | assert(tracemalloc_traced_memory >= trace.size); |
| 646 | tracemalloc_traced_memory -= trace.size; |
| 647 | |
| 648 | trace.size = size; |
| 649 | trace.traceback = traceback; |
| 650 | _Py_HASHTABLE_ENTRY_WRITE_DATA(tracemalloc_traces, entry, trace); |
| 651 | } |
| 652 | else { |
| 653 | trace.size = size; |
| 654 | trace.traceback = traceback; |
| 655 | |
| 656 | if (tracemalloc_config.use_domain) { |
| 657 | res = _Py_HASHTABLE_SET(tracemalloc_traces, key, trace); |
| 658 | } |
| 659 | else { |
| 660 | res = _Py_HASHTABLE_SET(tracemalloc_traces, ptr, trace); |
| 661 | } |
| 662 | if (res != 0) { |
| 663 | return res; |
| 664 | } |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 665 | } |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 666 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 667 | assert(tracemalloc_traced_memory <= SIZE_MAX - size); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 668 | tracemalloc_traced_memory += size; |
| 669 | if (tracemalloc_traced_memory > tracemalloc_peak_traced_memory) |
| 670 | tracemalloc_peak_traced_memory = tracemalloc_traced_memory; |
| 671 | return 0; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 672 | } |
| 673 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 674 | #define ADD_TRACE(ptr, size) \ |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 675 | tracemalloc_add_trace(DEFAULT_DOMAIN, (uintptr_t)(ptr), size) |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 676 | |
| 677 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 678 | static void* |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 679 | 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] | 680 | { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 681 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 682 | void *ptr; |
| 683 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 684 | assert(elsize == 0 || nelem <= SIZE_MAX / elsize); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 685 | |
| 686 | if (use_calloc) |
| 687 | ptr = alloc->calloc(alloc->ctx, nelem, elsize); |
| 688 | else |
| 689 | ptr = alloc->malloc(alloc->ctx, nelem * elsize); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 690 | if (ptr == NULL) |
| 691 | return NULL; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 692 | |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 693 | TABLES_LOCK(); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 694 | if (ADD_TRACE(ptr, nelem * elsize) < 0) { |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 695 | /* Failed to allocate a trace for the new memory block */ |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 696 | TABLES_UNLOCK(); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 697 | alloc->free(alloc->ctx, ptr); |
| 698 | return NULL; |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 699 | } |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 700 | TABLES_UNLOCK(); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 701 | return ptr; |
| 702 | } |
| 703 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 704 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 705 | static void* |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 706 | tracemalloc_realloc(void *ctx, void *ptr, size_t new_size) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 707 | { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 708 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 709 | void *ptr2; |
| 710 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 711 | ptr2 = alloc->realloc(alloc->ctx, ptr, new_size); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 712 | if (ptr2 == NULL) |
| 713 | return NULL; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 714 | |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 715 | if (ptr != NULL) { |
| 716 | /* an existing memory block has been resized */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 717 | |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 718 | TABLES_LOCK(); |
Victor Stinner | 42bcf37 | 2016-03-23 09:08:08 +0100 | [diff] [blame] | 719 | |
| 720 | /* tracemalloc_add_trace() updates the trace if there is already |
| 721 | a trace at address (domain, ptr2) */ |
| 722 | if (ptr2 != ptr) { |
| 723 | REMOVE_TRACE(ptr); |
| 724 | } |
Victor Stinner | 08facd2 | 2013-11-24 12:27:59 +0100 | [diff] [blame] | 725 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 726 | if (ADD_TRACE(ptr2, new_size) < 0) { |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 727 | /* Memory allocation failed. The error cannot be reported to |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 728 | the caller, because realloc() may already have shrunk the |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 729 | memory block and so removed bytes. |
| 730 | |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 731 | This case is very unlikely: a hash entry has just been |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 732 | released, so the hash table should have at least one free entry. |
| 733 | |
| 734 | The GIL and the table lock ensures that only one thread is |
| 735 | allocating memory. */ |
| 736 | assert(0 && "should never happen"); |
Victor Stinner | 5296867 | 2013-11-24 11:37:15 +0100 | [diff] [blame] | 737 | } |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 738 | TABLES_UNLOCK(); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 739 | } |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 740 | else { |
| 741 | /* new allocation */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 742 | |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 743 | TABLES_LOCK(); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 744 | if (ADD_TRACE(ptr2, new_size) < 0) { |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 745 | /* Failed to allocate a trace for the new memory block */ |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 746 | TABLES_UNLOCK(); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 747 | alloc->free(alloc->ctx, ptr2); |
| 748 | return NULL; |
| 749 | } |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 750 | TABLES_UNLOCK(); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 751 | } |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 752 | return ptr2; |
| 753 | } |
| 754 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 755 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 756 | static void |
| 757 | tracemalloc_free(void *ctx, void *ptr) |
| 758 | { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 759 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 760 | |
| 761 | if (ptr == NULL) |
| 762 | return; |
| 763 | |
| 764 | /* GIL cannot be locked in PyMem_RawFree() because it would introduce |
| 765 | a deadlock in PyThreadState_DeleteCurrent(). */ |
| 766 | |
| 767 | alloc->free(alloc->ctx, ptr); |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 768 | |
| 769 | TABLES_LOCK(); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 770 | REMOVE_TRACE(ptr); |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 771 | TABLES_UNLOCK(); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 772 | } |
| 773 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 774 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 775 | static void* |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 776 | 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] | 777 | { |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 778 | void *ptr; |
| 779 | |
| 780 | if (get_reentrant()) { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 781 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 782 | if (use_calloc) |
| 783 | return alloc->calloc(alloc->ctx, nelem, elsize); |
| 784 | else |
| 785 | return alloc->malloc(alloc->ctx, nelem * elsize); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* Ignore reentrant call. PyObjet_Malloc() calls PyMem_Malloc() for |
| 789 | allocations larger than 512 bytes, don't trace the same memory |
| 790 | allocation twice. */ |
| 791 | set_reentrant(1); |
| 792 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 793 | ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 794 | |
| 795 | set_reentrant(0); |
| 796 | return ptr; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 799 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 800 | static void* |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 801 | tracemalloc_malloc_gil(void *ctx, size_t size) |
| 802 | { |
| 803 | return tracemalloc_alloc_gil(0, ctx, 1, size); |
| 804 | } |
| 805 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 806 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 807 | static void* |
| 808 | tracemalloc_calloc_gil(void *ctx, size_t nelem, size_t elsize) |
| 809 | { |
| 810 | return tracemalloc_alloc_gil(1, ctx, nelem, elsize); |
| 811 | } |
| 812 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 813 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 814 | static void* |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 815 | tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size) |
| 816 | { |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 817 | void *ptr2; |
| 818 | |
| 819 | if (get_reentrant()) { |
| 820 | /* Reentrant call to PyMem_Realloc() and PyMem_RawRealloc(). |
| 821 | Example: PyMem_RawRealloc() is called internally by pymalloc |
| 822 | (_PyObject_Malloc() and _PyObject_Realloc()) to allocate a new |
| 823 | arena (new_arena()). */ |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 824 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 825 | |
| 826 | ptr2 = alloc->realloc(alloc->ctx, ptr, new_size); |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 827 | if (ptr2 != NULL && ptr != NULL) { |
| 828 | TABLES_LOCK(); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 829 | REMOVE_TRACE(ptr); |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 830 | TABLES_UNLOCK(); |
| 831 | } |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 832 | return ptr2; |
| 833 | } |
| 834 | |
| 835 | /* Ignore reentrant call. PyObjet_Realloc() calls PyMem_Realloc() for |
| 836 | allocations larger than 512 bytes. Don't trace the same memory |
| 837 | allocation twice. */ |
| 838 | set_reentrant(1); |
| 839 | |
| 840 | ptr2 = tracemalloc_realloc(ctx, ptr, new_size); |
| 841 | |
| 842 | set_reentrant(0); |
| 843 | return ptr2; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 844 | } |
| 845 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 846 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 847 | #ifdef TRACE_RAW_MALLOC |
| 848 | static void* |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 849 | 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] | 850 | { |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 851 | #ifdef WITH_THREAD |
| 852 | PyGILState_STATE gil_state; |
| 853 | #endif |
| 854 | void *ptr; |
| 855 | |
| 856 | if (get_reentrant()) { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 857 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 858 | if (use_calloc) |
| 859 | return alloc->calloc(alloc->ctx, nelem, elsize); |
| 860 | else |
| 861 | return alloc->malloc(alloc->ctx, nelem * elsize); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc() |
| 865 | indirectly which would call PyGILState_Ensure() if reentrant are not |
| 866 | disabled. */ |
| 867 | set_reentrant(1); |
| 868 | |
| 869 | #ifdef WITH_THREAD |
| 870 | gil_state = PyGILState_Ensure(); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 871 | ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 872 | PyGILState_Release(gil_state); |
| 873 | #else |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 874 | ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 875 | #endif |
| 876 | |
| 877 | set_reentrant(0); |
| 878 | return ptr; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 881 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 882 | static void* |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 883 | tracemalloc_raw_malloc(void *ctx, size_t size) |
| 884 | { |
| 885 | return tracemalloc_raw_alloc(0, ctx, 1, size); |
| 886 | } |
| 887 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 888 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 889 | static void* |
| 890 | tracemalloc_raw_calloc(void *ctx, size_t nelem, size_t elsize) |
| 891 | { |
| 892 | return tracemalloc_raw_alloc(1, ctx, nelem, elsize); |
| 893 | } |
| 894 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 895 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 896 | static void* |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 897 | tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size) |
| 898 | { |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 899 | #ifdef WITH_THREAD |
| 900 | PyGILState_STATE gil_state; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 901 | #endif |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 902 | void *ptr2; |
| 903 | |
| 904 | if (get_reentrant()) { |
| 905 | /* Reentrant call to PyMem_RawRealloc(). */ |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 906 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 907 | |
| 908 | ptr2 = alloc->realloc(alloc->ctx, ptr, new_size); |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 909 | |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 910 | if (ptr2 != NULL && ptr != NULL) { |
| 911 | TABLES_LOCK(); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 912 | REMOVE_TRACE(ptr); |
Victor Stinner | 88c2987 | 2013-12-04 01:47:46 +0100 | [diff] [blame] | 913 | TABLES_UNLOCK(); |
| 914 | } |
Victor Stinner | 1511680 | 2013-12-04 01:29:35 +0100 | [diff] [blame] | 915 | return ptr2; |
| 916 | } |
| 917 | |
| 918 | /* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc() |
| 919 | indirectly which would call PyGILState_Ensure() if reentrant calls are |
| 920 | not disabled. */ |
| 921 | set_reentrant(1); |
| 922 | |
| 923 | #ifdef WITH_THREAD |
| 924 | gil_state = PyGILState_Ensure(); |
| 925 | ptr2 = tracemalloc_realloc(ctx, ptr, new_size); |
| 926 | PyGILState_Release(gil_state); |
| 927 | #else |
| 928 | ptr2 = tracemalloc_realloc(ctx, ptr, new_size); |
| 929 | #endif |
| 930 | |
| 931 | set_reentrant(0); |
| 932 | return ptr2; |
| 933 | } |
| 934 | #endif /* TRACE_RAW_MALLOC */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 935 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 936 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 937 | static int |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 938 | tracemalloc_clear_filename(_Py_hashtable_t *ht, _Py_hashtable_entry_t *entry, |
| 939 | void *user_data) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 940 | { |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 941 | PyObject *filename; |
| 942 | |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 943 | _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, filename); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 944 | Py_DECREF(filename); |
| 945 | return 0; |
| 946 | } |
| 947 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 948 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 949 | static int |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 950 | traceback_free_traceback(_Py_hashtable_t *ht, _Py_hashtable_entry_t *entry, |
| 951 | void *user_data) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 952 | { |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 953 | traceback_t *traceback; |
| 954 | |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 955 | _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, traceback); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 956 | raw_free(traceback); |
| 957 | return 0; |
| 958 | } |
| 959 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 960 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 961 | /* reentrant flag must be set to call this function and GIL must be held */ |
| 962 | static void |
| 963 | tracemalloc_clear_traces(void) |
| 964 | { |
| 965 | #ifdef WITH_THREAD |
| 966 | /* The GIL protects variables againt concurrent access */ |
| 967 | assert(PyGILState_Check()); |
| 968 | #endif |
| 969 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 970 | TABLES_LOCK(); |
| 971 | _Py_hashtable_clear(tracemalloc_traces); |
| 972 | tracemalloc_traced_memory = 0; |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 973 | tracemalloc_peak_traced_memory = 0; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 974 | TABLES_UNLOCK(); |
| 975 | |
| 976 | _Py_hashtable_foreach(tracemalloc_tracebacks, traceback_free_traceback, NULL); |
| 977 | _Py_hashtable_clear(tracemalloc_tracebacks); |
| 978 | |
| 979 | _Py_hashtable_foreach(tracemalloc_filenames, tracemalloc_clear_filename, NULL); |
| 980 | _Py_hashtable_clear(tracemalloc_filenames); |
| 981 | } |
| 982 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 983 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 984 | static int |
| 985 | tracemalloc_init(void) |
| 986 | { |
| 987 | if (tracemalloc_config.initialized == TRACEMALLOC_FINALIZED) { |
| 988 | PyErr_SetString(PyExc_RuntimeError, |
| 989 | "the tracemalloc module has been unloaded"); |
| 990 | return -1; |
| 991 | } |
| 992 | |
Victor Stinner | f9a7115 | 2016-03-22 23:54:42 +0100 | [diff] [blame] | 993 | if (tracemalloc_config.initialized == TRACEMALLOC_INITIALIZED) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 994 | return 0; |
| 995 | |
| 996 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw); |
| 997 | |
| 998 | #ifdef REENTRANT_THREADLOCAL |
| 999 | tracemalloc_reentrant_key = PyThread_create_key(); |
| 1000 | if (tracemalloc_reentrant_key == -1) { |
| 1001 | #ifdef MS_WINDOWS |
| 1002 | PyErr_SetFromWindowsErr(0); |
| 1003 | #else |
| 1004 | PyErr_SetFromErrno(PyExc_OSError); |
| 1005 | #endif |
| 1006 | return -1; |
| 1007 | } |
| 1008 | #endif |
| 1009 | |
| 1010 | #if defined(WITH_THREAD) && defined(TRACE_RAW_MALLOC) |
| 1011 | if (tables_lock == NULL) { |
| 1012 | tables_lock = PyThread_allocate_lock(); |
| 1013 | if (tables_lock == NULL) { |
| 1014 | PyErr_SetString(PyExc_RuntimeError, "cannot allocate lock"); |
| 1015 | return -1; |
| 1016 | } |
| 1017 | } |
| 1018 | #endif |
| 1019 | |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 1020 | tracemalloc_filenames = hashtable_new(sizeof(PyObject *), 0, |
| 1021 | hashtable_hash_pyobject, |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1022 | hashtable_compare_unicode); |
| 1023 | |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 1024 | tracemalloc_tracebacks = hashtable_new(sizeof(traceback_t *), 0, |
| 1025 | hashtable_hash_traceback, |
| 1026 | hashtable_compare_traceback); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1027 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1028 | if (tracemalloc_config.use_domain) { |
| 1029 | tracemalloc_traces = hashtable_new(sizeof(pointer_t), |
| 1030 | sizeof(trace_t), |
| 1031 | hashtable_hash_pointer_t, |
| 1032 | hashtable_compare_pointer_t); |
| 1033 | } |
| 1034 | else { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 1035 | tracemalloc_traces = hashtable_new(sizeof(uintptr_t), |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1036 | sizeof(trace_t), |
| 1037 | _Py_hashtable_hash_ptr, |
| 1038 | _Py_hashtable_compare_direct); |
| 1039 | } |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1040 | |
| 1041 | if (tracemalloc_filenames == NULL || tracemalloc_tracebacks == NULL |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 1042 | || tracemalloc_traces == NULL) { |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1043 | PyErr_NoMemory(); |
| 1044 | return -1; |
| 1045 | } |
| 1046 | |
| 1047 | unknown_filename = PyUnicode_FromString("<unknown>"); |
| 1048 | if (unknown_filename == NULL) |
| 1049 | return -1; |
| 1050 | PyUnicode_InternInPlace(&unknown_filename); |
| 1051 | |
| 1052 | tracemalloc_empty_traceback.nframe = 1; |
| 1053 | /* borrowed reference */ |
| 1054 | tracemalloc_empty_traceback.frames[0].filename = unknown_filename; |
| 1055 | tracemalloc_empty_traceback.frames[0].lineno = 0; |
| 1056 | tracemalloc_empty_traceback.hash = traceback_hash(&tracemalloc_empty_traceback); |
| 1057 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1058 | tracemalloc_config.initialized = TRACEMALLOC_INITIALIZED; |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1062 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1063 | static void |
| 1064 | tracemalloc_deinit(void) |
| 1065 | { |
Victor Stinner | f9a7115 | 2016-03-22 23:54:42 +0100 | [diff] [blame] | 1066 | if (tracemalloc_config.initialized != TRACEMALLOC_INITIALIZED) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1067 | return; |
| 1068 | tracemalloc_config.initialized = TRACEMALLOC_FINALIZED; |
| 1069 | |
| 1070 | tracemalloc_stop(); |
| 1071 | |
| 1072 | /* destroy hash tables */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1073 | _Py_hashtable_destroy(tracemalloc_tracebacks); |
| 1074 | _Py_hashtable_destroy(tracemalloc_filenames); |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 1075 | _Py_hashtable_destroy(tracemalloc_traces); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1076 | |
| 1077 | #if defined(WITH_THREAD) && defined(TRACE_RAW_MALLOC) |
| 1078 | if (tables_lock != NULL) { |
| 1079 | PyThread_free_lock(tables_lock); |
| 1080 | tables_lock = NULL; |
| 1081 | } |
| 1082 | #endif |
| 1083 | |
| 1084 | #ifdef REENTRANT_THREADLOCAL |
| 1085 | PyThread_delete_key(tracemalloc_reentrant_key); |
Victor Stinner | 4a06647 | 2016-03-22 17:45:09 +0100 | [diff] [blame] | 1086 | tracemalloc_reentrant_key = -1; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1087 | #endif |
| 1088 | |
| 1089 | Py_XDECREF(unknown_filename); |
| 1090 | } |
| 1091 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1092 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1093 | static int |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1094 | tracemalloc_start(int max_nframe) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1095 | { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 1096 | PyMemAllocatorEx alloc; |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1097 | size_t size; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1098 | |
Victor Stinner | f9a7115 | 2016-03-22 23:54:42 +0100 | [diff] [blame] | 1099 | if (tracemalloc_init() < 0) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1100 | return -1; |
| 1101 | |
| 1102 | if (tracemalloc_config.tracing) { |
| 1103 | /* hook already installed: do nothing */ |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
Victor Stinner | 52aee85 | 2014-08-16 15:44:02 +0200 | [diff] [blame] | 1107 | assert(1 <= max_nframe && max_nframe <= MAX_NFRAME); |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1108 | tracemalloc_config.max_nframe = max_nframe; |
| 1109 | |
| 1110 | /* allocate a buffer to store a new traceback */ |
| 1111 | size = TRACEBACK_SIZE(max_nframe); |
| 1112 | assert(tracemalloc_traceback == NULL); |
| 1113 | tracemalloc_traceback = raw_malloc(size); |
| 1114 | if (tracemalloc_traceback == NULL) { |
| 1115 | PyErr_NoMemory(); |
| 1116 | return -1; |
| 1117 | } |
| 1118 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1119 | #ifdef TRACE_RAW_MALLOC |
| 1120 | alloc.malloc = tracemalloc_raw_malloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1121 | alloc.calloc = tracemalloc_raw_calloc; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1122 | alloc.realloc = tracemalloc_raw_realloc; |
| 1123 | alloc.free = tracemalloc_free; |
| 1124 | |
| 1125 | alloc.ctx = &allocators.raw; |
| 1126 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw); |
| 1127 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc); |
| 1128 | #endif |
| 1129 | |
| 1130 | alloc.malloc = tracemalloc_malloc_gil; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1131 | alloc.calloc = tracemalloc_calloc_gil; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1132 | alloc.realloc = tracemalloc_realloc_gil; |
| 1133 | alloc.free = tracemalloc_free; |
| 1134 | |
| 1135 | alloc.ctx = &allocators.mem; |
| 1136 | PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem); |
| 1137 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc); |
| 1138 | |
| 1139 | alloc.ctx = &allocators.obj; |
| 1140 | PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj); |
| 1141 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc); |
| 1142 | |
| 1143 | /* everything is ready: start tracing Python memory allocations */ |
| 1144 | tracemalloc_config.tracing = 1; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1145 | |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1149 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1150 | static void |
| 1151 | tracemalloc_stop(void) |
| 1152 | { |
Victor Stinner | f9a7115 | 2016-03-22 23:54:42 +0100 | [diff] [blame] | 1153 | if (!tracemalloc_config.tracing) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1154 | return; |
| 1155 | |
| 1156 | /* stop tracing Python memory allocations */ |
| 1157 | tracemalloc_config.tracing = 0; |
| 1158 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1159 | /* unregister the hook on memory allocators */ |
| 1160 | #ifdef TRACE_RAW_MALLOC |
| 1161 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw); |
| 1162 | #endif |
| 1163 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem); |
| 1164 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj); |
| 1165 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1166 | tracemalloc_clear_traces(); |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 1167 | |
| 1168 | /* release memory */ |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1169 | raw_free(tracemalloc_traceback); |
| 1170 | tracemalloc_traceback = NULL; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1173 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1174 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1175 | /*[clinic input] |
| 1176 | _tracemalloc.is_tracing |
| 1177 | |
| 1178 | Return True if the tracemalloc module is tracing Python memory allocations. |
| 1179 | [clinic start generated code]*/ |
| 1180 | |
| 1181 | static PyObject * |
| 1182 | _tracemalloc_is_tracing_impl(PyObject *module) |
Serhiy Storchaka | 9735384 | 2017-02-05 22:58:46 +0200 | [diff] [blame] | 1183 | /*[clinic end generated code: output=2d763b42601cd3ef input=af104b0a00192f63]*/ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1184 | { |
| 1185 | return PyBool_FromLong(tracemalloc_config.tracing); |
| 1186 | } |
| 1187 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1188 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1189 | /*[clinic input] |
| 1190 | _tracemalloc.clear_traces |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1191 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1192 | Clear traces of memory blocks allocated by Python. |
| 1193 | [clinic start generated code]*/ |
| 1194 | |
| 1195 | static PyObject * |
| 1196 | _tracemalloc_clear_traces_impl(PyObject *module) |
| 1197 | /*[clinic end generated code: output=a86080ee41b84197 input=0dab5b6c785183a5]*/ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1198 | { |
| 1199 | if (!tracemalloc_config.tracing) |
| 1200 | Py_RETURN_NONE; |
| 1201 | |
| 1202 | set_reentrant(1); |
| 1203 | tracemalloc_clear_traces(); |
| 1204 | set_reentrant(0); |
| 1205 | |
| 1206 | Py_RETURN_NONE; |
| 1207 | } |
| 1208 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1209 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1210 | static PyObject* |
| 1211 | frame_to_pyobject(frame_t *frame) |
| 1212 | { |
| 1213 | PyObject *frame_obj, *lineno_obj; |
| 1214 | |
| 1215 | frame_obj = PyTuple_New(2); |
| 1216 | if (frame_obj == NULL) |
| 1217 | return NULL; |
| 1218 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1219 | Py_INCREF(frame->filename); |
| 1220 | PyTuple_SET_ITEM(frame_obj, 0, frame->filename); |
| 1221 | |
Victor Stinner | 9528334 | 2016-03-15 21:57:02 +0100 | [diff] [blame] | 1222 | lineno_obj = PyLong_FromUnsignedLong(frame->lineno); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1223 | if (lineno_obj == NULL) { |
| 1224 | Py_DECREF(frame_obj); |
| 1225 | return NULL; |
| 1226 | } |
| 1227 | PyTuple_SET_ITEM(frame_obj, 1, lineno_obj); |
| 1228 | |
| 1229 | return frame_obj; |
| 1230 | } |
| 1231 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1232 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1233 | static PyObject* |
| 1234 | traceback_to_pyobject(traceback_t *traceback, _Py_hashtable_t *intern_table) |
| 1235 | { |
| 1236 | int i; |
| 1237 | PyObject *frames, *frame; |
| 1238 | |
| 1239 | if (intern_table != NULL) { |
| 1240 | if (_Py_HASHTABLE_GET(intern_table, traceback, frames)) { |
| 1241 | Py_INCREF(frames); |
| 1242 | return frames; |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | frames = PyTuple_New(traceback->nframe); |
| 1247 | if (frames == NULL) |
| 1248 | return NULL; |
| 1249 | |
| 1250 | for (i=0; i < traceback->nframe; i++) { |
| 1251 | frame = frame_to_pyobject(&traceback->frames[i]); |
| 1252 | if (frame == NULL) { |
| 1253 | Py_DECREF(frames); |
| 1254 | return NULL; |
| 1255 | } |
| 1256 | PyTuple_SET_ITEM(frames, i, frame); |
| 1257 | } |
| 1258 | |
| 1259 | if (intern_table != NULL) { |
| 1260 | if (_Py_HASHTABLE_SET(intern_table, traceback, frames) < 0) { |
| 1261 | Py_DECREF(frames); |
| 1262 | PyErr_NoMemory(); |
| 1263 | return NULL; |
| 1264 | } |
| 1265 | /* intern_table keeps a new reference to frames */ |
| 1266 | Py_INCREF(frames); |
| 1267 | } |
| 1268 | return frames; |
| 1269 | } |
| 1270 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1271 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1272 | static PyObject* |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 1273 | trace_to_pyobject(unsigned int domain, trace_t *trace, |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1274 | _Py_hashtable_t *intern_tracebacks) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1275 | { |
| 1276 | PyObject *trace_obj = NULL; |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1277 | PyObject *obj; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1278 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1279 | trace_obj = PyTuple_New(3); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1280 | if (trace_obj == NULL) |
| 1281 | return NULL; |
| 1282 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1283 | obj = PyLong_FromSize_t(domain); |
| 1284 | if (obj == NULL) { |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1285 | Py_DECREF(trace_obj); |
| 1286 | return NULL; |
| 1287 | } |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1288 | PyTuple_SET_ITEM(trace_obj, 0, obj); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1289 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1290 | obj = PyLong_FromSize_t(trace->size); |
| 1291 | if (obj == NULL) { |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1292 | Py_DECREF(trace_obj); |
| 1293 | return NULL; |
| 1294 | } |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1295 | PyTuple_SET_ITEM(trace_obj, 1, obj); |
| 1296 | |
| 1297 | obj = traceback_to_pyobject(trace->traceback, intern_tracebacks); |
| 1298 | if (obj == NULL) { |
| 1299 | Py_DECREF(trace_obj); |
| 1300 | return NULL; |
| 1301 | } |
| 1302 | PyTuple_SET_ITEM(trace_obj, 2, obj); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1303 | |
| 1304 | return trace_obj; |
| 1305 | } |
| 1306 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1307 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1308 | typedef struct { |
| 1309 | _Py_hashtable_t *traces; |
| 1310 | _Py_hashtable_t *tracebacks; |
| 1311 | PyObject *list; |
| 1312 | } get_traces_t; |
| 1313 | |
| 1314 | static int |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 1315 | tracemalloc_get_traces_fill(_Py_hashtable_t *traces, _Py_hashtable_entry_t *entry, |
| 1316 | void *user_data) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1317 | { |
| 1318 | get_traces_t *get_traces = user_data; |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 1319 | unsigned int domain; |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 1320 | trace_t trace; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1321 | PyObject *tracemalloc_obj; |
| 1322 | int res; |
| 1323 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1324 | if (tracemalloc_config.use_domain) { |
| 1325 | pointer_t key; |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 1326 | _Py_HASHTABLE_ENTRY_READ_KEY(traces, entry, key); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1327 | domain = key.domain; |
| 1328 | } |
| 1329 | else { |
| 1330 | domain = DEFAULT_DOMAIN; |
| 1331 | } |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 1332 | _Py_HASHTABLE_ENTRY_READ_DATA(traces, entry, trace); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1333 | |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 1334 | tracemalloc_obj = trace_to_pyobject(domain, &trace, get_traces->tracebacks); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1335 | if (tracemalloc_obj == NULL) |
| 1336 | return 1; |
| 1337 | |
| 1338 | res = PyList_Append(get_traces->list, tracemalloc_obj); |
| 1339 | Py_DECREF(tracemalloc_obj); |
| 1340 | if (res < 0) |
| 1341 | return 1; |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1346 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1347 | static int |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 1348 | tracemalloc_pyobject_decref_cb(_Py_hashtable_t *tracebacks, |
| 1349 | _Py_hashtable_entry_t *entry, |
| 1350 | void *user_data) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1351 | { |
Victor Stinner | c955387 | 2016-03-22 12:13:01 +0100 | [diff] [blame] | 1352 | PyObject *obj; |
Victor Stinner | e8c6b2f | 2016-03-23 09:25:01 +0100 | [diff] [blame] | 1353 | _Py_HASHTABLE_ENTRY_READ_DATA(tracebacks, entry, obj); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1354 | Py_DECREF(obj); |
| 1355 | return 0; |
| 1356 | } |
| 1357 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1358 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1359 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1360 | /*[clinic input] |
| 1361 | _tracemalloc._get_traces |
| 1362 | |
| 1363 | Get traces of all memory blocks allocated by Python. |
| 1364 | |
| 1365 | Return a list of (size: int, traceback: tuple) tuples. |
| 1366 | traceback is a tuple of (filename: str, lineno: int) tuples. |
| 1367 | |
| 1368 | Return an empty list if the tracemalloc module is disabled. |
| 1369 | [clinic start generated code]*/ |
| 1370 | |
| 1371 | static PyObject * |
| 1372 | _tracemalloc__get_traces_impl(PyObject *module) |
| 1373 | /*[clinic end generated code: output=e9929876ced4b5cc input=6c7d2230b24255aa]*/ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1374 | { |
| 1375 | get_traces_t get_traces; |
| 1376 | int err; |
| 1377 | |
| 1378 | get_traces.traces = NULL; |
| 1379 | get_traces.tracebacks = NULL; |
| 1380 | get_traces.list = PyList_New(0); |
| 1381 | if (get_traces.list == NULL) |
| 1382 | goto error; |
| 1383 | |
| 1384 | if (!tracemalloc_config.tracing) |
| 1385 | return get_traces.list; |
| 1386 | |
Victor Stinner | de2f132 | 2013-11-26 00:26:23 +0100 | [diff] [blame] | 1387 | /* the traceback hash table is used temporarily to intern traceback tuple |
| 1388 | of (filename, lineno) tuples */ |
Victor Stinner | c955387 | 2016-03-22 12:13:01 +0100 | [diff] [blame] | 1389 | get_traces.tracebacks = hashtable_new(sizeof(traceback_t *), |
| 1390 | sizeof(PyObject *), |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1391 | _Py_hashtable_hash_ptr, |
| 1392 | _Py_hashtable_compare_direct); |
| 1393 | if (get_traces.tracebacks == NULL) { |
| 1394 | PyErr_NoMemory(); |
| 1395 | goto error; |
| 1396 | } |
| 1397 | |
| 1398 | TABLES_LOCK(); |
| 1399 | get_traces.traces = _Py_hashtable_copy(tracemalloc_traces); |
| 1400 | TABLES_UNLOCK(); |
| 1401 | |
| 1402 | if (get_traces.traces == NULL) { |
| 1403 | PyErr_NoMemory(); |
| 1404 | goto error; |
| 1405 | } |
| 1406 | |
| 1407 | set_reentrant(1); |
| 1408 | err = _Py_hashtable_foreach(get_traces.traces, |
| 1409 | tracemalloc_get_traces_fill, &get_traces); |
| 1410 | set_reentrant(0); |
| 1411 | if (err) |
| 1412 | goto error; |
| 1413 | |
| 1414 | goto finally; |
| 1415 | |
| 1416 | error: |
| 1417 | Py_CLEAR(get_traces.list); |
| 1418 | |
| 1419 | finally: |
| 1420 | if (get_traces.tracebacks != NULL) { |
| 1421 | _Py_hashtable_foreach(get_traces.tracebacks, |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 1422 | tracemalloc_pyobject_decref_cb, NULL); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1423 | _Py_hashtable_destroy(get_traces.tracebacks); |
| 1424 | } |
Victor Stinner | c955387 | 2016-03-22 12:13:01 +0100 | [diff] [blame] | 1425 | if (get_traces.traces != NULL) { |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1426 | _Py_hashtable_destroy(get_traces.traces); |
Victor Stinner | c955387 | 2016-03-22 12:13:01 +0100 | [diff] [blame] | 1427 | } |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1428 | |
| 1429 | return get_traces.list; |
| 1430 | } |
| 1431 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1432 | |
Victor Stinner | 0611c26 | 2016-03-15 22:22:13 +0100 | [diff] [blame] | 1433 | static traceback_t* |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 1434 | tracemalloc_get_traceback(unsigned int domain, uintptr_t ptr) |
Victor Stinner | 0611c26 | 2016-03-15 22:22:13 +0100 | [diff] [blame] | 1435 | { |
| 1436 | trace_t trace; |
| 1437 | int found; |
| 1438 | |
| 1439 | if (!tracemalloc_config.tracing) |
| 1440 | return NULL; |
| 1441 | |
| 1442 | TABLES_LOCK(); |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1443 | if (tracemalloc_config.use_domain) { |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 1444 | pointer_t key = {ptr, domain}; |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1445 | found = _Py_HASHTABLE_GET(tracemalloc_traces, key, trace); |
| 1446 | } |
| 1447 | else { |
| 1448 | found = _Py_HASHTABLE_GET(tracemalloc_traces, ptr, trace); |
| 1449 | } |
Victor Stinner | 0611c26 | 2016-03-15 22:22:13 +0100 | [diff] [blame] | 1450 | TABLES_UNLOCK(); |
| 1451 | |
| 1452 | if (!found) |
| 1453 | return NULL; |
| 1454 | |
| 1455 | return trace.traceback; |
| 1456 | } |
| 1457 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1458 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1459 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1460 | /*[clinic input] |
| 1461 | _tracemalloc._get_object_traceback |
| 1462 | |
| 1463 | obj: object |
| 1464 | / |
| 1465 | |
| 1466 | Get the traceback where the Python object obj was allocated. |
| 1467 | |
| 1468 | Return a tuple of (filename: str, lineno: int) tuples. |
| 1469 | Return None if the tracemalloc module is disabled or did not |
| 1470 | trace the allocation of the object. |
| 1471 | [clinic start generated code]*/ |
| 1472 | |
| 1473 | static PyObject * |
| 1474 | _tracemalloc__get_object_traceback(PyObject *module, PyObject *obj) |
| 1475 | /*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1476 | { |
| 1477 | PyTypeObject *type; |
| 1478 | void *ptr; |
Victor Stinner | 0611c26 | 2016-03-15 22:22:13 +0100 | [diff] [blame] | 1479 | traceback_t *traceback; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1480 | |
| 1481 | type = Py_TYPE(obj); |
| 1482 | if (PyType_IS_GC(type)) |
| 1483 | ptr = (void *)((char *)obj - sizeof(PyGC_Head)); |
| 1484 | else |
| 1485 | ptr = (void *)obj; |
| 1486 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 1487 | traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (uintptr_t)ptr); |
Victor Stinner | 0611c26 | 2016-03-15 22:22:13 +0100 | [diff] [blame] | 1488 | if (traceback == NULL) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1489 | Py_RETURN_NONE; |
| 1490 | |
Victor Stinner | 0611c26 | 2016-03-15 22:22:13 +0100 | [diff] [blame] | 1491 | return traceback_to_pyobject(traceback, NULL); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1492 | } |
| 1493 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1494 | |
Victor Stinner | 0611c26 | 2016-03-15 22:22:13 +0100 | [diff] [blame] | 1495 | #define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str)) |
| 1496 | |
| 1497 | static void |
| 1498 | _PyMem_DumpFrame(int fd, frame_t * frame) |
| 1499 | { |
| 1500 | PUTS(fd, " File \""); |
| 1501 | _Py_DumpASCII(fd, frame->filename); |
| 1502 | PUTS(fd, "\", line "); |
| 1503 | _Py_DumpDecimal(fd, frame->lineno); |
| 1504 | PUTS(fd, "\n"); |
| 1505 | } |
| 1506 | |
| 1507 | /* Dump the traceback where a memory block was allocated into file descriptor |
| 1508 | fd. The function may block on TABLES_LOCK() but it is unlikely. */ |
| 1509 | void |
| 1510 | _PyMem_DumpTraceback(int fd, const void *ptr) |
| 1511 | { |
| 1512 | traceback_t *traceback; |
| 1513 | int i; |
| 1514 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 1515 | traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (uintptr_t)ptr); |
Victor Stinner | 0611c26 | 2016-03-15 22:22:13 +0100 | [diff] [blame] | 1516 | if (traceback == NULL) |
| 1517 | return; |
| 1518 | |
| 1519 | PUTS(fd, "Memory block allocated at (most recent call first):\n"); |
| 1520 | for (i=0; i < traceback->nframe; i++) { |
| 1521 | _PyMem_DumpFrame(fd, &traceback->frames[i]); |
| 1522 | } |
| 1523 | PUTS(fd, "\n"); |
| 1524 | } |
| 1525 | |
| 1526 | #undef PUTS |
| 1527 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1528 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1529 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1530 | /*[clinic input] |
| 1531 | _tracemalloc.start |
| 1532 | |
| 1533 | nframe: Py_ssize_t = 1 |
| 1534 | / |
| 1535 | |
| 1536 | Start tracing Python memory allocations. |
| 1537 | |
| 1538 | Also set the maximum number of frames stored in the traceback of a |
| 1539 | trace to nframe. |
| 1540 | [clinic start generated code]*/ |
| 1541 | |
| 1542 | static PyObject * |
| 1543 | _tracemalloc_start_impl(PyObject *module, Py_ssize_t nframe) |
| 1544 | /*[clinic end generated code: output=0f558d2079511553 input=997841629cc441cb]*/ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1545 | { |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1546 | int nframe_int; |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1547 | |
Victor Stinner | 52aee85 | 2014-08-16 15:44:02 +0200 | [diff] [blame] | 1548 | if (nframe < 1 || nframe > MAX_NFRAME) { |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1549 | PyErr_Format(PyExc_ValueError, |
| 1550 | "the number of frames must be in range [1; %i]", |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1551 | (int)MAX_NFRAME); |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1552 | return NULL; |
| 1553 | } |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1554 | nframe_int = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int); |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 1555 | |
Victor Stinner | f9a7115 | 2016-03-22 23:54:42 +0100 | [diff] [blame] | 1556 | if (tracemalloc_start(nframe_int) < 0) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1557 | return NULL; |
| 1558 | |
| 1559 | Py_RETURN_NONE; |
| 1560 | } |
| 1561 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1562 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1563 | /*[clinic input] |
| 1564 | _tracemalloc.stop |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1565 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1566 | Stop tracing Python memory allocations. |
| 1567 | |
| 1568 | Also clear traces of memory blocks allocated by Python. |
| 1569 | [clinic start generated code]*/ |
| 1570 | |
| 1571 | static PyObject * |
| 1572 | _tracemalloc_stop_impl(PyObject *module) |
| 1573 | /*[clinic end generated code: output=c3c42ae03e3955cd input=7478f075e51dae18]*/ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1574 | { |
| 1575 | tracemalloc_stop(); |
| 1576 | Py_RETURN_NONE; |
| 1577 | } |
| 1578 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1579 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1580 | /*[clinic input] |
| 1581 | _tracemalloc.get_traceback_limit |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1582 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1583 | Get the maximum number of frames stored in the traceback of a trace. |
| 1584 | |
| 1585 | By default, a trace of an allocated memory block only stores |
| 1586 | the most recent frame: the limit is 1. |
| 1587 | [clinic start generated code]*/ |
| 1588 | |
| 1589 | static PyObject * |
| 1590 | _tracemalloc_get_traceback_limit_impl(PyObject *module) |
| 1591 | /*[clinic end generated code: output=d556d9306ba95567 input=da3cd977fc68ae3b]*/ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1592 | { |
| 1593 | return PyLong_FromLong(tracemalloc_config.max_nframe); |
| 1594 | } |
| 1595 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1596 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1597 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1598 | /*[clinic input] |
| 1599 | _tracemalloc.get_tracemalloc_memory |
| 1600 | |
| 1601 | Get the memory usage in bytes of the tracemalloc module. |
| 1602 | |
| 1603 | This memory is used internally to trace memory allocations. |
| 1604 | [clinic start generated code]*/ |
| 1605 | |
| 1606 | static PyObject * |
| 1607 | _tracemalloc_get_tracemalloc_memory_impl(PyObject *module) |
| 1608 | /*[clinic end generated code: output=e3f14e280a55f5aa input=5d919c0f4d5132ad]*/ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1609 | { |
| 1610 | size_t size; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1611 | |
| 1612 | size = _Py_hashtable_size(tracemalloc_tracebacks); |
| 1613 | size += _Py_hashtable_size(tracemalloc_filenames); |
| 1614 | |
| 1615 | TABLES_LOCK(); |
| 1616 | size += _Py_hashtable_size(tracemalloc_traces); |
| 1617 | TABLES_UNLOCK(); |
| 1618 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1619 | return PyLong_FromSize_t(size); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1620 | } |
| 1621 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1622 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1623 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1624 | /*[clinic input] |
| 1625 | _tracemalloc.get_traced_memory |
| 1626 | |
| 1627 | Get the current size and peak size of memory blocks traced by tracemalloc. |
| 1628 | |
| 1629 | Returns a tuple: (current: int, peak: int). |
| 1630 | [clinic start generated code]*/ |
| 1631 | |
| 1632 | static PyObject * |
| 1633 | _tracemalloc_get_traced_memory_impl(PyObject *module) |
| 1634 | /*[clinic end generated code: output=5b167189adb9e782 input=61ddb5478400ff66]*/ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1635 | { |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 1636 | Py_ssize_t size, peak_size; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1637 | |
| 1638 | if (!tracemalloc_config.tracing) |
| 1639 | return Py_BuildValue("ii", 0, 0); |
| 1640 | |
| 1641 | TABLES_LOCK(); |
| 1642 | size = tracemalloc_traced_memory; |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 1643 | peak_size = tracemalloc_peak_traced_memory; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1644 | TABLES_UNLOCK(); |
| 1645 | |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1646 | return Py_BuildValue("nn", size, peak_size); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1647 | } |
| 1648 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1649 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1650 | static PyMethodDef module_methods[] = { |
Serhiy Storchaka | b451f91 | 2017-02-04 12:18:38 +0200 | [diff] [blame] | 1651 | _TRACEMALLOC_IS_TRACING_METHODDEF |
| 1652 | _TRACEMALLOC_CLEAR_TRACES_METHODDEF |
| 1653 | _TRACEMALLOC__GET_TRACES_METHODDEF |
| 1654 | _TRACEMALLOC__GET_OBJECT_TRACEBACK_METHODDEF |
| 1655 | _TRACEMALLOC_START_METHODDEF |
| 1656 | _TRACEMALLOC_STOP_METHODDEF |
| 1657 | _TRACEMALLOC_GET_TRACEBACK_LIMIT_METHODDEF |
| 1658 | _TRACEMALLOC_GET_TRACEMALLOC_MEMORY_METHODDEF |
| 1659 | _TRACEMALLOC_GET_TRACED_MEMORY_METHODDEF |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1660 | /* sentinel */ |
| 1661 | {NULL, NULL} |
| 1662 | }; |
| 1663 | |
| 1664 | PyDoc_STRVAR(module_doc, |
| 1665 | "Debug module to trace memory blocks allocated by Python."); |
| 1666 | |
| 1667 | static struct PyModuleDef module_def = { |
| 1668 | PyModuleDef_HEAD_INIT, |
| 1669 | "_tracemalloc", |
| 1670 | module_doc, |
| 1671 | 0, /* non-negative size to be able to unload the module */ |
| 1672 | module_methods, |
| 1673 | NULL, |
| 1674 | }; |
| 1675 | |
| 1676 | PyMODINIT_FUNC |
| 1677 | PyInit__tracemalloc(void) |
| 1678 | { |
| 1679 | PyObject *m; |
| 1680 | m = PyModule_Create(&module_def); |
| 1681 | if (m == NULL) |
| 1682 | return NULL; |
| 1683 | |
| 1684 | if (tracemalloc_init() < 0) |
| 1685 | return NULL; |
| 1686 | |
| 1687 | return m; |
| 1688 | } |
| 1689 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1690 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1691 | static int |
| 1692 | parse_sys_xoptions(PyObject *value) |
| 1693 | { |
| 1694 | PyObject *valuelong; |
| 1695 | long nframe; |
| 1696 | |
| 1697 | if (value == Py_True) |
| 1698 | return 1; |
| 1699 | |
| 1700 | assert(PyUnicode_Check(value)); |
| 1701 | if (PyUnicode_GetLength(value) == 0) |
| 1702 | return -1; |
| 1703 | |
| 1704 | valuelong = PyLong_FromUnicodeObject(value, 10); |
| 1705 | if (valuelong == NULL) |
| 1706 | return -1; |
| 1707 | |
| 1708 | nframe = PyLong_AsLong(valuelong); |
| 1709 | Py_DECREF(valuelong); |
| 1710 | if (nframe == -1 && PyErr_Occurred()) |
| 1711 | return -1; |
| 1712 | |
Victor Stinner | 52aee85 | 2014-08-16 15:44:02 +0200 | [diff] [blame] | 1713 | if (nframe < 1 || nframe > MAX_NFRAME) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1714 | return -1; |
| 1715 | |
| 1716 | return Py_SAFE_DOWNCAST(nframe, long, int); |
| 1717 | } |
| 1718 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1719 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1720 | int |
| 1721 | _PyTraceMalloc_Init(void) |
| 1722 | { |
| 1723 | char *p; |
| 1724 | int nframe; |
| 1725 | |
| 1726 | #ifdef WITH_THREAD |
| 1727 | assert(PyGILState_Check()); |
| 1728 | #endif |
| 1729 | |
| 1730 | if ((p = Py_GETENV("PYTHONTRACEMALLOC")) && *p != '\0') { |
| 1731 | char *endptr = p; |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1732 | long value; |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1733 | |
Victor Stinner | 711e27c | 2013-12-16 23:06:19 +0100 | [diff] [blame] | 1734 | errno = 0; |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1735 | value = strtol(p, &endptr, 10); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1736 | if (*endptr != '\0' |
| 1737 | || value < 1 |
Victor Stinner | 52aee85 | 2014-08-16 15:44:02 +0200 | [diff] [blame] | 1738 | || value > MAX_NFRAME |
Victor Stinner | 711e27c | 2013-12-16 23:06:19 +0100 | [diff] [blame] | 1739 | || errno == ERANGE) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1740 | { |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1741 | Py_FatalError("PYTHONTRACEMALLOC: invalid number of frames"); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1742 | return -1; |
| 1743 | } |
| 1744 | |
| 1745 | nframe = (int)value; |
| 1746 | } |
| 1747 | else { |
| 1748 | PyObject *xoptions, *key, *value; |
| 1749 | |
| 1750 | xoptions = PySys_GetXOptions(); |
| 1751 | if (xoptions == NULL) |
| 1752 | return -1; |
| 1753 | |
| 1754 | key = PyUnicode_FromString("tracemalloc"); |
| 1755 | if (key == NULL) |
| 1756 | return -1; |
| 1757 | |
Xiang Zhang | 4ed5ad7 | 2017-07-19 00:33:29 +0800 | [diff] [blame] | 1758 | value = PyDict_GetItemWithError(xoptions, key); /* borrowed */ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1759 | Py_DECREF(key); |
| 1760 | if (value == NULL) { |
| 1761 | if (PyErr_Occurred()) |
| 1762 | return -1; |
| 1763 | |
| 1764 | /* -X tracemalloc is not used */ |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
| 1768 | nframe = parse_sys_xoptions(value); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1769 | if (nframe < 0) { |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1770 | Py_FatalError("-X tracemalloc=NFRAME: invalid number of frames"); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | |
Victor Stinner | f28ce60 | 2013-11-27 22:27:13 +0100 | [diff] [blame] | 1774 | return tracemalloc_start(nframe); |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1775 | } |
| 1776 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 1777 | |
Victor Stinner | be0708f | 2013-12-01 10:03:26 +0100 | [diff] [blame] | 1778 | void |
| 1779 | _PyTraceMalloc_Fini(void) |
| 1780 | { |
| 1781 | #ifdef WITH_THREAD |
| 1782 | assert(PyGILState_Check()); |
| 1783 | #endif |
| 1784 | tracemalloc_deinit(); |
| 1785 | } |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 1786 | |
| 1787 | int |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 1788 | PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr, |
| 1789 | size_t size) |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 1790 | { |
| 1791 | int res; |
| 1792 | #ifdef WITH_THREAD |
| 1793 | PyGILState_STATE gil_state; |
| 1794 | #endif |
| 1795 | |
| 1796 | if (!tracemalloc_config.tracing) { |
| 1797 | /* tracemalloc is not tracing: do nothing */ |
| 1798 | return -2; |
| 1799 | } |
| 1800 | |
| 1801 | #ifdef WITH_THREAD |
| 1802 | gil_state = PyGILState_Ensure(); |
| 1803 | #endif |
| 1804 | |
| 1805 | TABLES_LOCK(); |
| 1806 | res = tracemalloc_add_trace(domain, ptr, size); |
| 1807 | TABLES_UNLOCK(); |
| 1808 | |
| 1809 | #ifdef WITH_THREAD |
| 1810 | PyGILState_Release(gil_state); |
| 1811 | #endif |
| 1812 | return res; |
| 1813 | } |
| 1814 | |
| 1815 | |
| 1816 | int |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 1817 | PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr) |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 1818 | { |
| 1819 | if (!tracemalloc_config.tracing) { |
| 1820 | /* tracemalloc is not tracing: do nothing */ |
| 1821 | return -2; |
| 1822 | } |
| 1823 | |
| 1824 | TABLES_LOCK(); |
| 1825 | tracemalloc_remove_trace(domain, ptr); |
| 1826 | TABLES_UNLOCK(); |
| 1827 | |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
| 1831 | |
| 1832 | PyObject* |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 1833 | _PyTraceMalloc_GetTraceback(unsigned int domain, uintptr_t ptr) |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 1834 | { |
| 1835 | traceback_t *traceback; |
| 1836 | |
| 1837 | traceback = tracemalloc_get_traceback(domain, ptr); |
| 1838 | if (traceback == NULL) |
| 1839 | Py_RETURN_NONE; |
| 1840 | |
| 1841 | return traceback_to_pyobject(traceback, NULL); |
| 1842 | } |