Skip to content

Commit

Permalink
gh-129185: Remove internal TRACE_RAW_MALLOC macro (#129218)
Browse files Browse the repository at this point in the history
Always build tracemalloc with PyMem_RawMalloc() hooks.
  • Loading branch information
vstinner authored Jan 23, 2025
1 parent 46c7e13 commit e579cdb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 51 deletions.
6 changes: 0 additions & 6 deletions Include/internal/pycore_tracemalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ extern "C" {
#include "pycore_hashtable.h" // _Py_hashtable_t


/* Trace memory blocks allocated by PyMem_RawMalloc() */
#define TRACE_RAW_MALLOC


struct _PyTraceMalloc_Config {
/* Module initialized?
Variable protected by the GIL */
Expand Down Expand Up @@ -74,9 +70,7 @@ struct _tracemalloc_runtime_state {
PyMemAllocatorEx obj;
} allocators;

#if defined(TRACE_RAW_MALLOC)
PyThread_type_lock tables_lock;
#endif
/* Size in bytes of currently traced memory.
Protected by TABLES_LOCK(). */
size_t traced_memory;
Expand Down
48 changes: 3 additions & 45 deletions Python/tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@ static int _PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event,
#define allocators _PyRuntime.tracemalloc.allocators


#if defined(TRACE_RAW_MALLOC)
/* This lock is needed because tracemalloc_free() is called without
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
would introduce a deadlock in _PyThreadState_DeleteCurrent(). */
# define tables_lock _PyRuntime.tracemalloc.tables_lock
# define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
# define TABLES_UNLOCK() PyThread_release_lock(tables_lock)
#else
/* variables are protected by the GIL */
# define TABLES_LOCK()
# define TABLES_UNLOCK()
#endif
#define tables_lock _PyRuntime.tracemalloc.tables_lock
#define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
#define TABLES_UNLOCK() PyThread_release_lock(tables_lock)


#define DEFAULT_DOMAIN 0
Expand Down Expand Up @@ -98,9 +92,6 @@ tracemalloc_error(const char *format, ...)
#endif


#if defined(TRACE_RAW_MALLOC)
#define REENTRANT_THREADLOCAL

#define tracemalloc_reentrant_key _PyRuntime.tracemalloc.reentrant_key

/* Any non-NULL pointer can be used */
Expand Down Expand Up @@ -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
hashtable_hash_pyobject(const void *key)
Expand Down Expand Up @@ -709,7 +681,6 @@ tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
}


#ifdef TRACE_RAW_MALLOC
static void*
tracemalloc_raw_malloc(void *ctx, size_t size)
{
Expand All @@ -729,7 +700,6 @@ tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
{
return tracemalloc_realloc(1, ctx, ptr, new_size);
}
#endif /* TRACE_RAW_MALLOC */


static void
Expand Down Expand Up @@ -767,20 +737,16 @@ _PyTraceMalloc_Init(void)

PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);

#ifdef REENTRANT_THREADLOCAL
if (PyThread_tss_create(&tracemalloc_reentrant_key) != 0) {
return _PyStatus_NO_MEMORY();
}
#endif

#if defined(TRACE_RAW_MALLOC)
if (tables_lock == NULL) {
tables_lock = PyThread_allocate_lock();
if (tables_lock == NULL) {
return _PyStatus_NO_MEMORY();
}
}
#endif

tracemalloc_filenames = hashtable_new(hashtable_hash_pyobject,
hashtable_compare_unicode,
Expand Down Expand Up @@ -826,16 +792,12 @@ tracemalloc_deinit(void)
_Py_hashtable_destroy(tracemalloc_tracebacks);
_Py_hashtable_destroy(tracemalloc_filenames);

#if defined(TRACE_RAW_MALLOC)
if (tables_lock != NULL) {
PyThread_free_lock(tables_lock);
tables_lock = NULL;
}
#endif

#ifdef REENTRANT_THREADLOCAL
PyThread_tss_delete(&tracemalloc_reentrant_key);
#endif
}


Expand Down Expand Up @@ -866,7 +828,6 @@ _PyTraceMalloc_Start(int max_nframe)
}

PyMemAllocatorEx alloc;
#ifdef TRACE_RAW_MALLOC
alloc.malloc = tracemalloc_raw_malloc;
alloc.calloc = tracemalloc_raw_calloc;
alloc.realloc = tracemalloc_raw_realloc;
Expand All @@ -875,7 +836,6 @@ _PyTraceMalloc_Start(int max_nframe)
alloc.ctx = &allocators.raw;
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
#endif

alloc.malloc = tracemalloc_malloc_gil;
alloc.calloc = tracemalloc_calloc_gil;
Expand Down Expand Up @@ -916,9 +876,7 @@ _PyTraceMalloc_Stop(void)
tracemalloc_config.tracing = 0;

/* unregister the hook on memory allocators */
#ifdef TRACE_RAW_MALLOC
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
#endif
PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem);
PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj);

Expand Down

0 comments on commit e579cdb

Please sign in to comment.