GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-97922: Run the GC only on eval breaker by pablogsal · Pull Request #97920 · python/cpython

gh-97922: Run the GC only on eval breaker #97920

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.

Sign up for GitHub

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

Merged
pablogsal merged 7 commits into python:main from pablogsal:gc_scheduled
Oct 8, 2022

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-97922: Run the GC only on eval breaker #97920

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
e6b7b2c
Run the GC only on eval breaker
pablogsal Oct 4, 2022
52f3855
Add news entry
pablogsal Oct 5, 2022
0bbc488
fixup! Add news entry
pablogsal Oct 7, 2022
28426c2
Update Python/ceval_gil.c
pablogsal Oct 7, 2022
9a1592a
fixup! fixup! Add news entry
pablogsal Oct 7, 2022
0d6c8c5
Merge remote-tracking branch 'origin/gc_scheduled' into gc_scheduled
pablogsal Oct 7, 2022
0411480
Update Python/ceval_gil.c
pablogsal Oct 8, 2022
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

7 changes: 7 additions & 0 deletions 7 Doc/whatsnew/3.12.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ Other Language Changes
when parsing source code containing null bytes. (Contributed by Pablo Galindo
in :gh:`96670`.)

* The Garbage Collector now runs only on the eval breaker mechanism of the
Python bytecode evaluation loop instead on object allocations. The GC can
also run when :c:func:`PyErr_CheckSignals` is called so C extensions that
need to run for a long time without executing any Python code also have a
chance to execute the GC periodically. (Contributed by Pablo Galindo in
:gh:`97922`.)

New Modules
===========

Expand Down
2 changes: 2 additions & 0 deletions 2 Include/internal/pycore_gc.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ extern void _PyList_ClearFreeList(PyInterpreterState *interp);
extern void _PyDict_ClearFreeList(PyInterpreterState *interp);
extern void _PyAsyncGen_ClearFreeLists(PyInterpreterState *interp);
extern void _PyContext_ClearFreeList(PyInterpreterState *interp);
extern void _Py_ScheduleGC(PyInterpreterState *interp);
extern void _Py_RunGC(PyThreadState *tstate);

#ifdef __cplusplus
}
Expand Down
2 changes: 2 additions & 0 deletions 2 Include/internal/pycore_interp.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct _ceval_state {
_Py_atomic_int eval_breaker;
/* Request for dropping the GIL */
_Py_atomic_int gil_drop_request;
/* The GC is ready to be executed */
_Py_atomic_int gc_scheduled;
struct _pending_calls pending;
};

Expand Down
2 changes: 1 addition & 1 deletion 2 Lib/test/test_frame.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def callback(phase, info):
frame!
"""
nonlocal sneaky_frame_object
sneaky_frame_object = sys._getframe().f_back
sneaky_frame_object = sys._getframe().f_back.f_back
# We're done here:
gc.callbacks.remove(callback)

Expand Down
5 changes: 5 additions & 0 deletions 5 Misc/NEWS.d/next/Core and Builtins/2022-10-05-11-37-15.gh-issue-97922.Zu9Bge.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The Garbage Collector now runs only on the eval breaker mechanism of the
Python bytecode evaluation loop instead on object allocations. The GC can
also run when :c:func:`PyErr_CheckSignals` is called so C extensions that
need to run for a long time without executing any Python code also have a
chance to execute the GC periodically.
27 changes: 24 additions & 3 deletions 27 Modules/gcmodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,20 @@ PyObject_IS_GC(PyObject *obj)
return _PyObject_IS_GC(obj);
}

void
_Py_ScheduleGC(PyInterpreterState *interp)
{
GCState *gcstate = &interp->gc;
if (gcstate->collecting == 1) {
return;
}
struct _ceval_state *ceval = &interp->ceval;
if (!_Py_atomic_load_relaxed(&ceval->gc_scheduled)) {
_Py_atomic_store_relaxed(&ceval->gc_scheduled, 1);
_Py_atomic_store_relaxed(&ceval->eval_breaker, 1);
}
}

void
_PyObject_GC_Link(PyObject *op)
{
Expand All @@ -2269,12 +2283,19 @@ _PyObject_GC_Link(PyObject *op)
!gcstate->collecting &&
!_PyErr_Occurred(tstate))
{
gcstate->collecting = 1;
gc_collect_generations(tstate);
gcstate->collecting = 0;
_Py_ScheduleGC(tstate->interp);
}
}

void
_Py_RunGC(PyThreadState *tstate)
{
GCState *gcstate = &tstate->interp->gc;
gcstate->collecting = 1;
gc_collect_generations(tstate);
gcstate->collecting = 0;
}

static PyObject *
gc_alloc(size_t basicsize, size_t presize)
{
Expand Down
13 changes: 13 additions & 0 deletions 13 Modules/signalmodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,19 @@ int
PyErr_CheckSignals(void)
{
PyThreadState *tstate = _PyThreadState_GET();

/* Opportunistically check if the GC is scheduled to run and run it
if we have a request. This is done here because native code needs
to call this API if is going to run for some time without executing
Python code to ensure signals are handled. Checking for the GC here
allows long running native code to clean cycles created using the C-API
even if it doesn't run the evaluation loop */
struct _ceval_state *interp_ceval_state = &tstate->interp->ceval;
if (_Py_atomic_load_relaxed(&interp_ceval_state->gc_scheduled)) {
_Py_atomic_store_relaxed(&interp_ceval_state->gc_scheduled, 0);
Comment thread
brandtbucher marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

_Py_RunGC(tstate);
}

if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
return 0;
}
Expand Down
30 changes: 20 additions & 10 deletions 30 Python/ceval_gil.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pycore_pyerrors.h" // _PyErr_Fetch()
#include "pycore_pylifecycle.h" // _PyErr_Print()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_interp.h" // _Py_RunGC()
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()

/*
Expand Down Expand Up @@ -69,7 +70,8 @@ COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
&& _Py_ThreadCanHandleSignals(interp))
| (_Py_atomic_load_relaxed_int32(&ceval2->pending.calls_to_do)
&& _Py_ThreadCanHandlePendingCalls())
| ceval2->pending.async_exc);
| ceval2->pending.async_exc
| _Py_atomic_load_relaxed_int32(&ceval2->gc_scheduled));
}


Expand Down Expand Up @@ -938,6 +940,7 @@ _Py_HandlePending(PyThreadState *tstate)
{
_PyRuntimeState * const runtime = &_PyRuntime;
struct _ceval_runtime_state *ceval = &runtime->ceval;
struct _ceval_state *interp_ceval_state = &tstate->interp->ceval;

/* Pending signals */
if (_Py_atomic_load_relaxed_int32(&ceval->signals_pending)) {
Expand All @@ -947,20 +950,26 @@ _Py_HandlePending(PyThreadState *tstate)
}

/* Pending calls */
struct _ceval_state *ceval2 = &tstate->interp->ceval;
if (_Py_atomic_load_relaxed_int32(&ceval2->pending.calls_to_do)) {
if (_Py_atomic_load_relaxed_int32(&interp_ceval_state->pending.calls_to_do)) {
if (make_pending_calls(tstate->interp) != 0) {
return -1;
}
}

/* GC scheduled to run */
if (_Py_atomic_load_relaxed_int32(&interp_ceval_state->gc_scheduled)) {
_Py_atomic_store_relaxed(&interp_ceval_state->gc_scheduled, 0);
COMPUTE_EVAL_BREAKER(tstate->interp, ceval, interp_ceval_state);
_Py_RunGC(tstate);
}

/* GIL drop request */
if (_Py_atomic_load_relaxed_int32(&ceval2->gil_drop_request)) {
if (_Py_atomic_load_relaxed_int32(&interp_ceval_state->gil_drop_request)) {
/* Give another thread a chance */
if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Py_FatalError("tstate mix-up");
}
drop_gil(ceval, ceval2, tstate);
drop_gil(ceval, interp_ceval_state, tstate);

/* Other threads may run now */

Expand All @@ -981,16 +990,17 @@ _Py_HandlePending(PyThreadState *tstate)
return -1;
}

#ifdef MS_WINDOWS
// bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
// different thread than the Python thread, in which case

// It is possible that some of the conditions that trigger the eval breaker
// are called in a different thread than the Python thread. An example of
// this is bpo-42296: On Windows, _PyEval_SignalReceived() can be called in
// a different thread than the Python thread, in which case
// _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
// current Python thread with the correct _Py_ThreadCanHandleSignals()
// value. It prevents to interrupt the eval loop at every instruction if
// the current Python thread cannot handle signals (if
// _Py_ThreadCanHandleSignals() is false).
COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
#endif
COMPUTE_EVAL_BREAKER(tstate->interp, ceval, interp_ceval_state);

return 0;
}
Expand Down
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.