mirror of
https://github.com/python/cpython.git
synced 2025-10-17 04:08:28 +00:00
gh-129185: Remove internal TRACE_RAW_MALLOC macro (#129218)
Always build tracemalloc with PyMem_RawMalloc() hooks.
This commit is contained in:
parent
46c7e13c05
commit
e579cdb21e
2 changed files with 3 additions and 51 deletions
|
@ -11,10 +11,6 @@ extern "C" {
|
||||||
#include "pycore_hashtable.h" // _Py_hashtable_t
|
#include "pycore_hashtable.h" // _Py_hashtable_t
|
||||||
|
|
||||||
|
|
||||||
/* Trace memory blocks allocated by PyMem_RawMalloc() */
|
|
||||||
#define TRACE_RAW_MALLOC
|
|
||||||
|
|
||||||
|
|
||||||
struct _PyTraceMalloc_Config {
|
struct _PyTraceMalloc_Config {
|
||||||
/* Module initialized?
|
/* Module initialized?
|
||||||
Variable protected by the GIL */
|
Variable protected by the GIL */
|
||||||
|
@ -74,9 +70,7 @@ struct _tracemalloc_runtime_state {
|
||||||
PyMemAllocatorEx obj;
|
PyMemAllocatorEx obj;
|
||||||
} allocators;
|
} allocators;
|
||||||
|
|
||||||
#if defined(TRACE_RAW_MALLOC)
|
|
||||||
PyThread_type_lock tables_lock;
|
PyThread_type_lock tables_lock;
|
||||||
#endif
|
|
||||||
/* Size in bytes of currently traced memory.
|
/* Size in bytes of currently traced memory.
|
||||||
Protected by TABLES_LOCK(). */
|
Protected by TABLES_LOCK(). */
|
||||||
size_t traced_memory;
|
size_t traced_memory;
|
||||||
|
|
|
@ -33,18 +33,12 @@ static int _PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event,
|
||||||
#define allocators _PyRuntime.tracemalloc.allocators
|
#define allocators _PyRuntime.tracemalloc.allocators
|
||||||
|
|
||||||
|
|
||||||
#if defined(TRACE_RAW_MALLOC)
|
|
||||||
/* This lock is needed because tracemalloc_free() is called without
|
/* This lock is needed because tracemalloc_free() is called without
|
||||||
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
|
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
|
||||||
would introduce a deadlock in _PyThreadState_DeleteCurrent(). */
|
would introduce a deadlock in _PyThreadState_DeleteCurrent(). */
|
||||||
# define tables_lock _PyRuntime.tracemalloc.tables_lock
|
#define tables_lock _PyRuntime.tracemalloc.tables_lock
|
||||||
# define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
|
#define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
|
||||||
# define TABLES_UNLOCK() PyThread_release_lock(tables_lock)
|
#define TABLES_UNLOCK() PyThread_release_lock(tables_lock)
|
||||||
#else
|
|
||||||
/* variables are protected by the GIL */
|
|
||||||
# define TABLES_LOCK()
|
|
||||||
# define TABLES_UNLOCK()
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#define DEFAULT_DOMAIN 0
|
#define DEFAULT_DOMAIN 0
|
||||||
|
@ -98,9 +92,6 @@ tracemalloc_error(const char *format, ...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if defined(TRACE_RAW_MALLOC)
|
|
||||||
#define REENTRANT_THREADLOCAL
|
|
||||||
|
|
||||||
#define tracemalloc_reentrant_key _PyRuntime.tracemalloc.reentrant_key
|
#define tracemalloc_reentrant_key _PyRuntime.tracemalloc.reentrant_key
|
||||||
|
|
||||||
/* Any non-NULL pointer can be used */
|
/* Any non-NULL pointer can be used */
|
||||||
|
@ -137,25 +128,6 @@ set_reentrant(int reentrant)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
/* TRACE_RAW_MALLOC not defined: variable protected by the GIL */
|
|
||||||
static int tracemalloc_reentrant = 0;
|
|
||||||
|
|
||||||
static int
|
|
||||||
get_reentrant(void)
|
|
||||||
{
|
|
||||||
return tracemalloc_reentrant;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
set_reentrant(int reentrant)
|
|
||||||
{
|
|
||||||
assert(reentrant != tracemalloc_reentrant);
|
|
||||||
tracemalloc_reentrant = reentrant;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
static Py_uhash_t
|
static Py_uhash_t
|
||||||
hashtable_hash_pyobject(const void *key)
|
hashtable_hash_pyobject(const void *key)
|
||||||
|
@ -709,7 +681,6 @@ tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef TRACE_RAW_MALLOC
|
|
||||||
static void*
|
static void*
|
||||||
tracemalloc_raw_malloc(void *ctx, size_t size)
|
tracemalloc_raw_malloc(void *ctx, size_t size)
|
||||||
{
|
{
|
||||||
|
@ -729,7 +700,6 @@ tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
|
||||||
{
|
{
|
||||||
return tracemalloc_realloc(1, ctx, ptr, new_size);
|
return tracemalloc_realloc(1, ctx, ptr, new_size);
|
||||||
}
|
}
|
||||||
#endif /* TRACE_RAW_MALLOC */
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -767,20 +737,16 @@ _PyTraceMalloc_Init(void)
|
||||||
|
|
||||||
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
|
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
|
||||||
|
|
||||||
#ifdef REENTRANT_THREADLOCAL
|
|
||||||
if (PyThread_tss_create(&tracemalloc_reentrant_key) != 0) {
|
if (PyThread_tss_create(&tracemalloc_reentrant_key) != 0) {
|
||||||
return _PyStatus_NO_MEMORY();
|
return _PyStatus_NO_MEMORY();
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TRACE_RAW_MALLOC)
|
|
||||||
if (tables_lock == NULL) {
|
if (tables_lock == NULL) {
|
||||||
tables_lock = PyThread_allocate_lock();
|
tables_lock = PyThread_allocate_lock();
|
||||||
if (tables_lock == NULL) {
|
if (tables_lock == NULL) {
|
||||||
return _PyStatus_NO_MEMORY();
|
return _PyStatus_NO_MEMORY();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
tracemalloc_filenames = hashtable_new(hashtable_hash_pyobject,
|
tracemalloc_filenames = hashtable_new(hashtable_hash_pyobject,
|
||||||
hashtable_compare_unicode,
|
hashtable_compare_unicode,
|
||||||
|
@ -826,16 +792,12 @@ tracemalloc_deinit(void)
|
||||||
_Py_hashtable_destroy(tracemalloc_tracebacks);
|
_Py_hashtable_destroy(tracemalloc_tracebacks);
|
||||||
_Py_hashtable_destroy(tracemalloc_filenames);
|
_Py_hashtable_destroy(tracemalloc_filenames);
|
||||||
|
|
||||||
#if defined(TRACE_RAW_MALLOC)
|
|
||||||
if (tables_lock != NULL) {
|
if (tables_lock != NULL) {
|
||||||
PyThread_free_lock(tables_lock);
|
PyThread_free_lock(tables_lock);
|
||||||
tables_lock = NULL;
|
tables_lock = NULL;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef REENTRANT_THREADLOCAL
|
|
||||||
PyThread_tss_delete(&tracemalloc_reentrant_key);
|
PyThread_tss_delete(&tracemalloc_reentrant_key);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -866,7 +828,6 @@ _PyTraceMalloc_Start(int max_nframe)
|
||||||
}
|
}
|
||||||
|
|
||||||
PyMemAllocatorEx alloc;
|
PyMemAllocatorEx alloc;
|
||||||
#ifdef TRACE_RAW_MALLOC
|
|
||||||
alloc.malloc = tracemalloc_raw_malloc;
|
alloc.malloc = tracemalloc_raw_malloc;
|
||||||
alloc.calloc = tracemalloc_raw_calloc;
|
alloc.calloc = tracemalloc_raw_calloc;
|
||||||
alloc.realloc = tracemalloc_raw_realloc;
|
alloc.realloc = tracemalloc_raw_realloc;
|
||||||
|
@ -875,7 +836,6 @@ _PyTraceMalloc_Start(int max_nframe)
|
||||||
alloc.ctx = &allocators.raw;
|
alloc.ctx = &allocators.raw;
|
||||||
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
|
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
|
||||||
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
|
||||||
#endif
|
|
||||||
|
|
||||||
alloc.malloc = tracemalloc_malloc_gil;
|
alloc.malloc = tracemalloc_malloc_gil;
|
||||||
alloc.calloc = tracemalloc_calloc_gil;
|
alloc.calloc = tracemalloc_calloc_gil;
|
||||||
|
@ -916,9 +876,7 @@ _PyTraceMalloc_Stop(void)
|
||||||
tracemalloc_config.tracing = 0;
|
tracemalloc_config.tracing = 0;
|
||||||
|
|
||||||
/* unregister the hook on memory allocators */
|
/* unregister the hook on memory allocators */
|
||||||
#ifdef TRACE_RAW_MALLOC
|
|
||||||
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
|
||||||
#endif
|
|
||||||
PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem);
|
PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem);
|
||||||
PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj);
|
PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue