Skip to content

bpo-37146: Allow to configure opcode cache and fix huntleaks by presetting it to 1 #13789

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct _ceval_runtime_state {
/* Request for checking signals. */
_Py_atomic_int signals_pending;
struct _gil_runtime_state gil;
struct int opcode_cache_minruns;
};

/* interpreter state */
Expand Down Expand Up @@ -312,6 +313,9 @@ PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);

PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime);

PyAPI_FUNC(void) _PyEval_SetOpcodeCacheMinRuns(int minruns);
PyAPI_FUNC(int) _PyEval_GetOpcodeCacheMinRuns(void);

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/libregrtest/refleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def dash_R(ns, test_name, test_func):
if not hasattr(sys, 'gettotalrefcount'):
raise Exception("Tracking reference leaks requires a debug build "
"of Python")
minruns = sys._getopcacheminruns()
sys._setopcacheminruns(1)

# Avoid false positives due to various caches
# filling slowly with random data:
Expand Down Expand Up @@ -104,6 +106,7 @@ def get_pooled_int(value):
rc_before = rc_after
fd_before = fd_after

sys._setopcacheminruns(minruns)
if not ns.quiet:
print(file=sys.stderr)

Expand Down
1 change: 0 additions & 1 deletion Lib/test/libregrtest/runtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ def _runtest_inner2(ns, test_name):
gc.garbage.clear()

support.reap_children()

return refleak


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added ``sys._setopcacheminruns`` and ``sys._getopcacheminruns`` to allow
configuring the minimum number of function executions to activate the global
opcode cache.
18 changes: 15 additions & 3 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ static long dxp[256];
#endif

/* per opcode cache */
#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
#define OPCACHE_STATS 0 /* Enable stats */

#if OPCACHE_STATS
Expand Down Expand Up @@ -1152,9 +1151,10 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
f->f_executing = 1;

if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
int opcacheminruns = _PyEval_GetOpcodeCacheMinRuns();
if (co->co_opcache_flag < opcacheminruns) {
co->co_opcache_flag++;
if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
if (co->co_opcache_flag == opcacheminruns) {
if (_PyCode_InitOpcache(co) < 0) {
return NULL;
}
Expand Down Expand Up @@ -5632,3 +5632,15 @@ maybe_dtrace_line(PyFrameObject *frame,
}
*instr_prev = frame->f_lasti;
}

void
_PyEval_SetOpcodeCacheMinRuns(int minruns)
{
_PyRuntime.ceval.opcode_cache_minruns = minruns;
}

int
_PyEval_GetOpcodeCacheMinRuns()
{
return _PyRuntime.ceval.opcode_cache_minruns;
}
63 changes: 62 additions & 1 deletion Python/clinic/sysmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,8 @@ _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
return status;
}

runtime->ceval.opcodeconfig.minruns = 1024;

runtime->pre_initialized = 1;
return _PyStatus_OK();
}
Expand Down
40 changes: 40 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,44 @@ sys_getcheckinterval_impl(PyObject *module)
return PyLong_FromLong(interp->check_interval);
}

/*[clinic input]
sys._setopcacheminruns

minruns: int
/

Set the minimum value of runs to activate opcode cache.
[clinic start generated code]*/

static PyObject *
sys__setopcacheminruns_impl(PyObject *module, int minruns)
/*[clinic end generated code: output=e29d082b62b2aa8a input=169a982eee6595d5]*/
{
if (minruns <= 0) {
PyErr_SetString(PyExc_ValueError,
"opcache min runs must be strictly positive");
return NULL;
}
_PyEval_SetOpcodeCacheMinRuns(minruns);
Py_RETURN_NONE;
}


/*[clinic input]
sys._getopcacheminruns -> int

Return the current minimum value of runs to activate opcode cache.
[clinic start generated code]*/

static int
sys__getopcacheminruns_impl(PyObject *module)
/*[clinic end generated code: output=f25d23b7afd8512c input=93784822a3e883c9]*/
{
return _PyEval_GetOpcodeCacheMinRuns();
}



/*[clinic input]
sys.setswitchinterval

Expand Down Expand Up @@ -1936,6 +1974,8 @@ static PyMethodDef sys_methods[] = {
SYS_MDEBUG_METHODDEF
SYS_SETCHECKINTERVAL_METHODDEF
SYS_GETCHECKINTERVAL_METHODDEF
SYS__SETOPCACHEMINRUNS_METHODDEF
SYS__GETOPCACHEMINRUNS_METHODDEF
SYS_SETSWITCHINTERVAL_METHODDEF
SYS_GETSWITCHINTERVAL_METHODDEF
SYS_SETDLOPENFLAGS_METHODDEF
Expand Down