Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Include/internal/pycore_tracemalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ struct _PyTraceMalloc_Config {
TRACEMALLOC_FINALIZED
} initialized;

/* Is tracemalloc tracing memory allocations?
Variable protected by the TABLES_LOCK(). */
/* Is tracemalloc tracing memory allocations? (Atomic.) */
int tracing;

/* limit of the number of frames in a traceback, 1 by default.
Expand Down
19 changes: 15 additions & 4 deletions Python/tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ _PyTraceMalloc_Start(int max_nframe)

/* everything is ready: start tracing Python memory allocations */
TABLES_LOCK();
tracemalloc_config.tracing = 1;
_Py_atomic_store_int_release(&tracemalloc_config.tracing, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think release is stronger than necessary because there are no data dependencies. The actual functions are called with the lock held anyways so only atomicity of tracing matters.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a hidden data dependency: we don't want tracing to be enabled, an allocation to be tracked, and then another thread deallocating that tracked data without untracking.

TABLES_UNLOCK();

return 0;
Expand All @@ -867,7 +867,7 @@ _PyTraceMalloc_Stop(void)
}

/* stop tracing Python memory allocations */
tracemalloc_config.tracing = 0;
_Py_atomic_store_int_release(&tracemalloc_config.tracing, 0);

/* unregister the hook on memory allocators */
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
Expand Down Expand Up @@ -1207,6 +1207,12 @@ int
PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
size_t size)
{
if (!_Py_atomic_load_int_relaxed(&tracemalloc_config.tracing)) {
// Exit early without attempting to lock if it doesn't look like
// we're tracing.
return -2;
}

PyGILState_STATE gil_state = PyGILState_Ensure();
TABLES_LOCK();

Expand All @@ -1215,7 +1221,7 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
result = tracemalloc_add_trace_unlocked(domain, ptr, size);
}
else {
/* tracemalloc is not tracing: do nothing */
/* tracemalloc was disabled before we locked tables; do nothing. */
result = -2;
}

Expand All @@ -1228,6 +1234,11 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
int
PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
{
if (!_Py_atomic_load_int_relaxed(&tracemalloc_config.tracing)) {
// Exit early without attempting to lock if it doesn't look like
// we're tracing.
return -2;
}
TABLES_LOCK();

int result;
Expand All @@ -1236,7 +1247,7 @@ PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
result = 0;
}
else {
/* tracemalloc is not tracing: do nothing */
/* tracemalloc was disabled before we locked tables; do nothing. */
result = -2;
}

Expand Down
Loading