@@ -19,6 +19,7 @@ Data members:
1919#include "pycore_call.h" // _PyObject_CallNoArgs()
2020#include "pycore_ceval.h" // _PyEval_SetAsyncGenFinalizer()
2121#include "pycore_frame.h" // _PyInterpreterFrame
22+ #include "pycore_gc.h" // _PyGC_GetMimallocAllocatedBytes()
2223#include "pycore_import.h" // _PyImport_SetDLOpenFlags()
2324#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
2425#include "pycore_interpframe.h" // _PyFrame_GetFirstComplete()
@@ -2060,6 +2061,32 @@ sys_getallocatedblocks_impl(PyObject *module)
20602061 return _Py_GetGlobalAllocatedBlocks ();
20612062}
20622063
2064+ PyDoc_STRVAR (sys__get_mimalloc_allocated_bytes__doc__ ,
2065+ "_get_mimalloc_allocated_bytes($module, /)\n"
2066+ "--\n"
2067+ "\n"
2068+ "Return total bytes allocated across all mimalloc heaps in this interpreter.\n"
2069+ "\n"
2070+ "Free-threaded build only. Stops the world while reading per-thread heap\n"
2071+ "structures. Intended for benchmarking: the OS RSS does not reliably reflect\n"
2072+ "Python's live memory because mimalloc retains freed pages.\n"
2073+ "Raises NotImplementedError on the GIL-enabled build." );
2074+
2075+ static PyObject *
2076+ sys__get_mimalloc_allocated_bytes (PyObject * module , PyObject * Py_UNUSED (ignored ))
2077+ {
2078+ #ifdef Py_GIL_DISABLED
2079+ PyInterpreterState * interp = _PyInterpreterState_GET ();
2080+ Py_ssize_t total = _PyGC_GetMimallocAllocatedBytes (interp );
2081+ return PyLong_FromSsize_t (total );
2082+ #else
2083+ PyErr_SetString (PyExc_NotImplementedError ,
2084+ "sys._get_mimalloc_allocated_bytes() is only available "
2085+ "on the free-threaded build" );
2086+ return NULL ;
2087+ #endif
2088+ }
2089+
20632090/*[clinic input]
20642091sys.getunicodeinternedsize -> Py_ssize_t
20652092
@@ -2927,6 +2954,8 @@ static PyMethodDef sys_methods[] = {
29272954 SYS_GETDEFAULTENCODING_METHODDEF
29282955 SYS_GETDLOPENFLAGS_METHODDEF
29292956 SYS_GETALLOCATEDBLOCKS_METHODDEF
2957+ {"_get_mimalloc_allocated_bytes" , sys__get_mimalloc_allocated_bytes ,
2958+ METH_NOARGS , sys__get_mimalloc_allocated_bytes__doc__ },
29302959 SYS_GETUNICODEINTERNEDSIZE_METHODDEF
29312960 SYS_GETFILESYSTEMENCODING_METHODDEF
29322961 SYS_GETFILESYSTEMENCODEERRORS_METHODDEF
0 commit comments