Skip to content

Commit

Permalink
Move get_tlbc_blocks into the sys module
Browse files Browse the repository at this point in the history
  • Loading branch information
mpage committed Sep 30, 2024
1 parent ad180d1 commit 95d2264
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 32 deletions.
5 changes: 2 additions & 3 deletions Lib/test/libregrtest/refleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,11 @@ def get_pooled_int(value):
# Use an internal-only keyword argument that mypy doesn't know yet
_only_immortal=True) # type: ignore[call-arg]
alloc_after = getallocatedblocks() - interned_immortal_after
if support.Py_GIL_DISABLED:
if _get_tlbc_blocks := getattr(sys, "_get_tlbc_blocks", None):
# Ignore any thread-local bytecode that was allocated. These will be
# released when the code object is destroyed, typically at runtime
# shutdown
import _testinternalcapi
alloc_after -= _testinternalcapi.get_tlbc_blocks()
alloc_after -= _get_tlbc_blocks()
rc_after = gettotalrefcount()
fd_after = fd_count()

Expand Down
28 changes: 0 additions & 28 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2006,33 +2006,6 @@ get_tlbc_id(PyObject *Py_UNUSED(module), PyObject *obj)
}
return PyLong_FromVoidPtr(bc);
}

static int
count_tlbc_blocks(PyObject *obj, Py_ssize_t *count)
{
if (PyCode_Check(obj)) {
_PyCodeArray *tlbc = ((PyCodeObject *)obj)->co_tlbc;
// First entry always points to the bytecode at the end of the code
// object. Exclude it from the count as it is allocated as part of
// creating the code object.
for (Py_ssize_t i = 1; i < tlbc->size; i++) {
if (tlbc->entries[i] != NULL) {
(*count)++;
}
}
}
return 1;
}

// Return the total number of thread-local bytecode copies, excluding the
// copies that are embedded in the code object.
static PyObject *
get_tlbc_blocks(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(ignored))
{
Py_ssize_t count = 0;
PyUnstable_GC_VisitObjects((gcvisitobjects_t) count_tlbc_blocks, &count);
return PyLong_FromSsize_t(count);
}
#endif

static PyObject *
Expand Down Expand Up @@ -2207,7 +2180,6 @@ static PyMethodDef module_functions[] = {
{"py_thread_id", get_py_thread_id, METH_NOARGS},
{"get_tlbc", get_tlbc, METH_O, NULL},
{"get_tlbc_id", get_tlbc_id, METH_O, NULL},
{"get_tlbc_blocks", get_tlbc_blocks, METH_NOARGS},
#endif
{"suppress_immortalization", suppress_immortalization, METH_O},
{"get_immortalize_deferred", get_immortalize_deferred, METH_NOARGS},
Expand Down
38 changes: 37 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.

36 changes: 36 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,41 @@ sys__is_gil_enabled_impl(PyObject *module)
#endif
}

#ifdef Py_GIL_DISABLED
static int
count_tlbc_blocks(PyObject *obj, Py_ssize_t *count)
{
if (PyCode_Check(obj)) {
_PyCodeArray *tlbc = ((PyCodeObject *)obj)->co_tlbc;
// First entry always points to the bytecode at the end of the code
// object. Exclude it from the count as it is allocated as part of
// creating the code object.
for (Py_ssize_t i = 1; i < tlbc->size; i++) {
if (tlbc->entries[i] != NULL) {
(*count)++;
}
}
}
return 1;
}

/*[clinic input]
sys._get_tlbc_blocks -> Py_ssize_t
Return the total number of thread-local bytecode copies, excluding the copies that are embedded in the code object.
[clinic start generated code]*/

static Py_ssize_t
sys__get_tlbc_blocks_impl(PyObject *module)
/*[clinic end generated code: output=4b4e350583cbd643 input=37c14e47d8905a95]*/
{
Py_ssize_t count = 0;
PyUnstable_GC_VisitObjects((gcvisitobjects_t) count_tlbc_blocks, &count);
return count;
}
#endif /* Py_GIL_DISABLED */



static PerfMapState perf_map_state;

Expand Down Expand Up @@ -2617,6 +2652,7 @@ static PyMethodDef sys_methods[] = {
#endif
SYS__GET_CPU_COUNT_CONFIG_METHODDEF
SYS__IS_GIL_ENABLED_METHODDEF
SYS__GET_TLBC_BLOCKS_METHODDEF
{NULL, NULL} // sentinel
};

Expand Down

0 comments on commit 95d2264

Please sign in to comment.