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