Skip to content

Commit 0f84737

Browse files
Move most of the tracemalloc_* globals to _PyRuntimeState.
1 parent 43e66af commit 0f84737

File tree

3 files changed

+62
-63
lines changed

3 files changed

+62
-63
lines changed

Include/internal/pycore_tracemalloc.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_hashtable.h" // _Py_hashtable_t
12+
1113

1214
/* Trace memory blocks allocated by PyMem_RawMalloc() */
1315
#define TRACE_RAW_MALLOC
@@ -32,6 +34,34 @@ struct _PyTraceMalloc_Config {
3234
};
3335

3436

37+
/* Pack the frame_t structure to reduce the memory footprint on 64-bit
38+
architectures: 12 bytes instead of 16. */
39+
struct
40+
#ifdef __GNUC__
41+
__attribute__((packed))
42+
#elif defined(_MSC_VER)
43+
#pragma pack(push, 4)
44+
#endif
45+
tracemalloc_frame {
46+
/* filename cannot be NULL: "<unknown>" is used if the Python frame
47+
filename is NULL */
48+
PyObject *filename;
49+
unsigned int lineno;
50+
};
51+
#ifdef _MSC_VER
52+
#pragma pack(pop)
53+
#endif
54+
55+
struct tracemalloc_traceback {
56+
Py_uhash_t hash;
57+
/* Number of frames stored */
58+
uint16_t nframe;
59+
/* Total number of frames the traceback had */
60+
uint16_t total_nframe;
61+
struct tracemalloc_frame frames[1];
62+
};
63+
64+
3565
struct _tracemalloc_runtime_state {
3666
struct _PyTraceMalloc_Config config;
3767
/* Protected by the GIL */
@@ -43,6 +73,29 @@ struct _tracemalloc_runtime_state {
4373
#if defined(TRACE_RAW_MALLOC)
4474
PyThread_type_lock tables_lock;
4575
#endif
76+
/* Size in bytes of currently traced memory.
77+
Protected by TABLES_LOCK(). */
78+
size_t traced_memory;
79+
/* Peak size in bytes of traced memory.
80+
Protected by TABLES_LOCK(). */
81+
size_t peak_traced_memory;
82+
/* Hash table used as a set to intern filenames:
83+
PyObject* => PyObject*.
84+
Protected by the GIL */
85+
_Py_hashtable_t *filenames;
86+
/* Buffer to store a new traceback in traceback_new().
87+
Protected by the GIL. */
88+
struct tracemalloc_traceback *traceback;
89+
/* Hash table used as a set to intern tracebacks:
90+
traceback_t* => traceback_t*
91+
Protected by the GIL */
92+
_Py_hashtable_t *tracebacks;
93+
/* pointer (void*) => trace (trace_t*).
94+
Protected by TABLES_LOCK(). */
95+
_Py_hashtable_t *traces;
96+
/* domain (unsigned int) => traces (_Py_hashtable_t).
97+
Protected by TABLES_LOCK(). */
98+
_Py_hashtable_t *domains;
4699
};
47100

48101
#define _tracemalloc_runtime_state_INIT \

Modules/_tracemalloc.c

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,8 @@ static void raw_free(void *ptr);
5151

5252
#define DEFAULT_DOMAIN 0
5353

54-
/* Pack the frame_t structure to reduce the memory footprint on 64-bit
55-
architectures: 12 bytes instead of 16. */
56-
typedef struct
57-
#ifdef __GNUC__
58-
__attribute__((packed))
59-
#elif defined(_MSC_VER)
60-
#pragma pack(push, 4)
61-
#endif
62-
{
63-
/* filename cannot be NULL: "<unknown>" is used if the Python frame
64-
filename is NULL */
65-
PyObject *filename;
66-
unsigned int lineno;
67-
} frame_t;
68-
#ifdef _MSC_VER
69-
#pragma pack(pop)
70-
#endif
71-
72-
73-
typedef struct {
74-
Py_uhash_t hash;
75-
/* Number of frames stored */
76-
uint16_t nframe;
77-
/* Total number of frames the traceback had */
78-
uint16_t total_nframe;
79-
frame_t frames[1];
80-
} traceback_t;
54+
typedef struct tracemalloc_frame frame_t;
55+
typedef struct tracemalloc_traceback traceback_t;
8156

8257
#define TRACEBACK_SIZE(NFRAME) \
8358
(sizeof(traceback_t) + sizeof(frame_t) * (NFRAME - 1))
@@ -100,35 +75,13 @@ typedef struct {
10075
} trace_t;
10176

10277

103-
/* Size in bytes of currently traced memory.
104-
Protected by TABLES_LOCK(). */
105-
static size_t tracemalloc_traced_memory = 0;
106-
107-
/* Peak size in bytes of traced memory.
108-
Protected by TABLES_LOCK(). */
109-
static size_t tracemalloc_peak_traced_memory = 0;
110-
111-
/* Hash table used as a set to intern filenames:
112-
PyObject* => PyObject*.
113-
Protected by the GIL */
114-
static _Py_hashtable_t *tracemalloc_filenames = NULL;
115-
116-
/* Buffer to store a new traceback in traceback_new().
117-
Protected by the GIL. */
118-
static traceback_t *tracemalloc_traceback = NULL;
119-
120-
/* Hash table used as a set to intern tracebacks:
121-
traceback_t* => traceback_t*
122-
Protected by the GIL */
123-
static _Py_hashtable_t *tracemalloc_tracebacks = NULL;
124-
125-
/* pointer (void*) => trace (trace_t*).
126-
Protected by TABLES_LOCK(). */
127-
static _Py_hashtable_t *tracemalloc_traces = NULL;
128-
129-
/* domain (unsigned int) => traces (_Py_hashtable_t).
130-
Protected by TABLES_LOCK(). */
131-
static _Py_hashtable_t *tracemalloc_domains = NULL;
78+
#define tracemalloc_traced_memory _PyRuntime.tracemalloc.traced_memory
79+
#define tracemalloc_peak_traced_memory _PyRuntime.tracemalloc.peak_traced_memory
80+
#define tracemalloc_filenames _PyRuntime.tracemalloc.filenames
81+
#define tracemalloc_traceback _PyRuntime.tracemalloc.traceback
82+
#define tracemalloc_tracebacks _PyRuntime.tracemalloc.tracebacks
83+
#define tracemalloc_traces _PyRuntime.tracemalloc.traces
84+
#define tracemalloc_domains _PyRuntime.tracemalloc.domains
13285

13386

13487
#ifdef TRACE_DEBUG

Tools/c-analyzer/cpython/globals-to-fix.tsv

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,6 @@ Modules/faulthandler.c - old_stack -
378378
## state
379379

380380
Modules/_tracemalloc.c - tracemalloc_empty_traceback -
381-
Modules/_tracemalloc.c - tracemalloc_traced_memory -
382-
Modules/_tracemalloc.c - tracemalloc_peak_traced_memory -
383-
Modules/_tracemalloc.c - tracemalloc_filenames -
384-
Modules/_tracemalloc.c - tracemalloc_traceback -
385-
Modules/_tracemalloc.c - tracemalloc_tracebacks -
386-
Modules/_tracemalloc.c - tracemalloc_traces -
387-
Modules/_tracemalloc.c - tracemalloc_domains -
388381
Modules/_tracemalloc.c - tracemalloc_reentrant_key -
389382
Modules/faulthandler.c faulthandler_dump_traceback reentrant -
390383
Modules/signalmodule.c - is_tripped -

0 commit comments

Comments
 (0)