Repository navigation
gh-158972: Drain each thread's page list during GC #160150
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mpage
wants to merge
2
commits into
python:main
Choose a base branch
from
mpage:gh-158972-drain-page-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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 |
|---|---|---|
|
|
@@ -226,16 +226,26 @@ _PyMem_mi_page_reclaimed(mi_page_t *page) | |
| #endif | ||
| } | ||
|
|
||
| #ifdef Py_GIL_DISABLED | ||
| // Free the empty pages on `target`'s QSBR page list whose QSBR goal has been | ||
| // reached. | ||
| // | ||
| // `cur` must be the calling thread's state: _Py_qsbr_poll() must be called | ||
| // with the caller's own QSBR state. The goals themselves are global, so `cur` | ||
| // can check pages parked by any thread. | ||
| // | ||
| // `target` is the thread whose page list is drained. It must be `cur`, or a | ||
| // thread that is suspended (e.g., during a stop-the-world pause), because | ||
| // _mi_page_free() modifies the heap and segment state of the thread that owns | ||
| // the page. | ||
| static void | ||
| _PyMem_mi_heap_collect_qsbr(mi_heap_t *heap) | ||
| _PyMem_mi_collect_tstate_page_list(_PyThreadStateImpl *cur, _PyThreadStateImpl *target) | ||
| { | ||
| #ifdef Py_GIL_DISABLED | ||
| if (!heap->page_use_qsbr) { | ||
| if (!_Py_atomic_load_int(&target->mimalloc.initialized)) { | ||
| // The thread may not have called tstate_mimalloc_bind() yet. | ||
| return; | ||
| } | ||
|
|
||
| _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); | ||
| struct llist_node *head = &tstate->mimalloc.page_list; | ||
| struct llist_node *head = &target->mimalloc.page_list; | ||
| if (llist_empty(head)) { | ||
| return; | ||
| } | ||
|
|
@@ -249,13 +259,26 @@ _PyMem_mi_heap_collect_qsbr(mi_heap_t *heap) | |
| continue; | ||
| } | ||
|
|
||
| if (!_Py_qsbr_poll(tstate->qsbr, page->qsbr_goal)) { | ||
| if (!_Py_qsbr_poll(cur->qsbr, page->qsbr_goal)) { | ||
| return; | ||
| } | ||
|
|
||
| _PyMem_mi_page_clear_qsbr(page); | ||
| _mi_page_free(page, mi_page_queue_of(page), false); | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| static void | ||
| _PyMem_mi_heap_collect_qsbr(mi_heap_t *heap) | ||
| { | ||
| #ifdef Py_GIL_DISABLED | ||
| if (!heap->page_use_qsbr) { | ||
| return; | ||
| } | ||
|
|
||
| _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); | ||
| _PyMem_mi_collect_tstate_page_list(tstate, tstate); | ||
| #endif | ||
| } | ||
|
|
||
|
|
@@ -1702,6 +1725,19 @@ _PyMem_FiniDelayed(PyInterpreterState *interp) | |
| } | ||
| } | ||
|
|
||
| #ifdef Py_GIL_DISABLED | ||
| void | ||
| _PyMem_FreeParkedQSBRPages(PyInterpreterState *interp) | ||
| { | ||
| _PyThreadStateImpl *tstate = (_PyThreadStateImpl *) _PyThreadState_GET(); | ||
| _Py_FOR_EACH_TSTATE_BEGIN(interp, p) { | ||
| _PyMem_mi_collect_tstate_page_list(tstate, (_PyThreadStateImpl *) p); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| } | ||
| _Py_FOR_EACH_TSTATE_END(interp); | ||
| } | ||
| #endif | ||
|
|
||
|
|
||
| /**************************/ | ||
| /* the "object" allocator */ | ||
| /**************************/ | ||
|
|
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
(_PyThreadStateImpl *)_PyThreadState_GET()