Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-143057: Use atomics for _PyRuntime.tracemalloc.config.tracing#143066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
Yhg1s wants to merge1 commit intopython:mainfromYhg1s:tracemalloc_atomic_check
Closed
Show file tree
Hide file tree
Changes fromall 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 deletionsInclude/internal/pycore_tracemalloc.h
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsPython/tracemalloc.c
View file
Open in desktop
Original file line numberDiff line numberDiff 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 oftracing matters.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I think thereis 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 DownExpand 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 {
/* tracemallocis not tracing:do nothing */
/* tracemallocwas 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 {
/* tracemallocis not tracing:do nothing */
/* tracemallocwas disabled before we locked tables;do nothing. */
result = -2;
}

Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp