Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Run the GC only on eval breaker
  • Loading branch information
pablogsal committed Oct 7, 2022
commit e6b7b2cc1799bb0ea4fd170dae51daace94f0124
2 changes: 2 additions & 0 deletions Include/internal/pycore_gc.h
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 Include/internal/pycore_interp.h
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
21 changes: 18 additions & 3 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,14 @@ PyObject_IS_GC(PyObject *obj)
return _PyObject_IS_GC(obj);
}

void
_Py_ScheduleGC(PyInterpreterState *interp)
{
struct _ceval_state *ceval = &interp->ceval;
_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 +2277,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 Modules/signalmodule.c
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);
_Py_RunGC(tstate);
}

if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
return 0;
}
Expand Down
17 changes: 12 additions & 5 deletions Python/ceval_gil.c
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 @@ -938,6 +939,13 @@ _Py_HandlePending(PyThreadState *tstate)
{
_PyRuntimeState * const runtime = &_PyRuntime;
struct _ceval_runtime_state *ceval = &runtime->ceval;
struct _ceval_state *interp_ceval_state = &tstate->interp->ceval;

/* 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);
_Py_RunGC(tstate);
}

/* Pending signals */
if (_Py_atomic_load_relaxed_int32(&ceval->signals_pending)) {
Expand All @@ -947,20 +955,19 @@ _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;
}
}

/* 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 Down Expand Up @@ -989,7 +996,7 @@ _Py_HandlePending(PyThreadState *tstate)
// 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);
COMPUTE_EVAL_BREAKER(tstate->interp, ceval, interp_ceval_state);
#endif

return 0;
Expand Down