annotate Modules/_tracemalloc.c @ 87690:66c1c9f32567 default tip

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