FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-143057: Use atomics for _PyRuntime.tracemalloc.config.tracing by Yhg1s · Pull Request #143066 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .c  (1) .h  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 1 addition & 2 deletions Include/internal/pycore_tracemalloc.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Back | FazBrowse Home | New Git URL