Debian Codegraph @

Include/internal/pycore_tracemalloc.h

raw
1 #ifndef Py_INTERNAL_TRACEMALLOC_H
2 #define Py_INTERNAL_TRACEMALLOC_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7 #ifndef Py_BUILD_CORE
8 # error "this header requires Py_BUILD_CORE define"
9 #endif
10
11 #include "pycore_hashtable.h" // _Py_hashtable_t
12
13
14 struct _PyTraceMalloc_Config {
15 /* Module initialized?
16 Variable protected by the GIL */
17 enum {
18 TRACEMALLOC_NOT_INITIALIZED,
19 TRACEMALLOC_INITIALIZED,
20 TRACEMALLOC_FINALIZED
21 } initialized;
22
23 /* Is tracemalloc tracing memory allocations?
24 Variable protected by the TABLES_LOCK() and stored atomically.
25 Atomic store is used so that it can read without locking for the
26 general case of checking if tracemalloc is enabled.
27 */
28 int tracing;
29
30 /* limit of the number of frames in a traceback, 1 by default.
31 Variable protected by the GIL. */
32 int max_nframe;
33 };
34
35
36 /* Pack the frame_t structure to reduce the memory footprint on 64-bit
37 architectures: 12 bytes instead of 16. */
38 #if defined(_MSC_VER)
39 #pragma pack(push, 4)
40 #endif
41
42 struct
43 #ifdef __GNUC__
44 __attribute__((packed))
45 #endif
46 tracemalloc_frame {
47 /* filename cannot be NULL: "<unknown>" is used if the Python frame
48 filename is NULL */
49 PyObject *filename;
50 unsigned int lineno;
51 };
52 #ifdef _MSC_VER
53 #pragma pack(pop)
54 #endif
55
56 struct tracemalloc_traceback {
57 Py_uhash_t hash;
58 /* Number of frames stored */
59 uint16_t nframe;
60 /* Total number of frames the traceback had */
61 uint16_t total_nframe;
62 struct tracemalloc_frame frames[1];
63 };
64
65
66 struct _tracemalloc_runtime_state {
67 struct _PyTraceMalloc_Config config;
68
69 /* Protected by the GIL */
70 struct {
71 PyMemAllocatorEx mem;
72 PyMemAllocatorEx raw;
73 PyMemAllocatorEx obj;
74 } allocators;
75
76 PyMutex tables_lock;
77 /* Size in bytes of currently traced memory.
78 Protected by TABLES_LOCK(). */
79 size_t traced_memory;
80 /* Peak size in bytes of traced memory.
81 Protected by TABLES_LOCK(). */
82 size_t peak_traced_memory;
83 /* Hash table used as a set to intern filenames:
84 PyObject* => PyObject*.
85 Protected by the TABLES_LOCK(). */
86 _Py_hashtable_t *filenames;
87 /* Buffer to store a new traceback in traceback_new().
88 Protected by the TABLES_LOCK(). */
89 struct tracemalloc_traceback *traceback;
90 /* Hash table used as a set to intern tracebacks:
91 traceback_t* => traceback_t*
92 Protected by the TABLES_LOCK(). */
93 _Py_hashtable_t *tracebacks;
94 /* pointer (void*) => trace (trace_t*).
95 Protected by TABLES_LOCK(). */
96 _Py_hashtable_t *traces;
97 /* domain (unsigned int) => traces (_Py_hashtable_t).
98 Protected by TABLES_LOCK(). */
99 _Py_hashtable_t *domains;
100
101 struct tracemalloc_traceback empty_traceback;
102
103 Py_tss_t reentrant_key;
104 };
105
106 #define _tracemalloc_runtime_state_INIT \
107 { \
108 .config = { \
109 .initialized = TRACEMALLOC_NOT_INITIALIZED, \
110 .tracing = 0, \
111 .max_nframe = 1, \
112 }, \
113 .reentrant_key = Py_tss_NEEDS_INIT, \
114 }
115
116
117 // Get the traceback where a memory block was allocated.
118 //
119 // Return a tuple of (filename: str, lineno: int) tuples.
120 //
121 // Return None if the tracemalloc module is disabled or if the memory block
122 // is not tracked by tracemalloc.
123 //
124 // Raise an exception and return NULL on error.
125 //
126 // Export for '_testinternalcapi' shared extension.
127 PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback(
128 unsigned int domain,
129 uintptr_t ptr);
130
131 /* Return non-zero if tracemalloc is tracing */
132 extern int _PyTraceMalloc_IsTracing(void);
133
134 /* Clear the tracemalloc traces */
135 extern void _PyTraceMalloc_ClearTraces(void);
136
137 /* Clear the tracemalloc traces */
138 extern PyObject* _PyTraceMalloc_GetTraces(void);
139
140 /* Clear tracemalloc traceback for an object */
141 extern PyObject* _PyTraceMalloc_GetObjectTraceback(PyObject *obj);
142
143 /* Initialize tracemalloc */
144 extern PyStatus _PyTraceMalloc_Init(void);
145
146 /* Start tracemalloc */
147 extern int _PyTraceMalloc_Start(int max_nframe);
148
149 /* Stop tracemalloc */
150 extern void _PyTraceMalloc_Stop(void);
151
152 /* Get the tracemalloc traceback limit */
153 extern int _PyTraceMalloc_GetTracebackLimit(void);
154
155 /* Get the memory usage of tracemalloc in bytes */
156 extern size_t _PyTraceMalloc_GetMemory(void);
157
158 /* Get the current size and peak size of traced memory blocks as a 2-tuple */
159 extern PyObject* _PyTraceMalloc_GetTracedMemory(void);
160
161 /* Set the peak size of traced memory blocks to the current size */
162 extern void _PyTraceMalloc_ResetPeak(void);
163
164 #ifdef __cplusplus
165 }
166 #endif
167 #endif // !Py_INTERNAL_TRACEMALLOC_H