◐ Shell
clean mode source ↗

gh-131253: free-threaded build support for pystats by nascheme · Pull Request #137189 · python/cpython

Allow the --enable-pystats build option to be used with
free-threading.  For the free-threaded builds, the stats structure is
allocated per-thread and then periodically merged into a global stats
structure (on thread exit or when the reporting function is called).

Summary of changes:

* introduce _Py_tss_stats thread-local variable.  This is set
  when stats are on, replacing the _Py_stats global that's used
  in the non-free-threaded build.

* replace _Py_stats references with _PyStats_GET()

* move pystats logic from Python/specialize.c into Python/pystats.c

* add some free-threaded specific stat counters

@bedevere-app Bot mentioned this pull request

Jul 28, 2025

colesbury

Need to do a merge before reporting (I lost that bit of code on a
re-factor).

Fix various issues with data races.  When merging from all threads, we
need to stop-the-world to avoid races.  When toggling on or off, also
need to stop-the-world.  Remove the need for locking for
_PyStats_Attach().
Add pystats pointer to PyThreadState and use from there instead.  This
is slightly slower but shouldn't matter in practice.  This simplifies
the attach/detach logic as well.

@nascheme nascheme marked this pull request as ready for review

August 8, 2025 00:03

kumaraditya303

markshannon

markshannon

sergey-miryanov

sergey-miryanov

sergey-miryanov

Add the _PyStats_InterpInit() function which will set pystats_enabled
flag and allocate pystats_struct as required.  We shouldn't be putting
logic in _PyObject_InitState().  This also fixes a bug where
interp->pystats_struct was allocated repeated rather than once per
interpreter.
This is a bit more efficient, with the downside of a bit extra code.
For _PyStats_Clear(), we don't need to do the merging operation.  I
think the code is a bit more clear this way.

@nascheme

sergey-miryanov

sergey-miryanov

kumaraditya303

kumaraditya303

Need to allocate in stats_toggle_on_off() if the thread never had
stats enabled yet.  For zeroing, handle case that struct is not
allocated.  Remove bogus ts->_status.active check.

kumaraditya303

Co-authored-by: Kumar Aditya <kumaraditya@python.org>

kumaraditya303

kumaraditya303

kumaraditya303

kumaraditya303

kumaraditya303

Co-authored-by: Kumar Aditya <kumaraditya@python.org>

StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request

Dec 6, 2025
…7189)

Allow the --enable-pystats build option to be used with free-threading.  The
stats are now stored on a per-interpreter basis, rather than process global.
For free-threaded builds, the stats structure is allocated per-thread and
then periodically merged into the per-interpreter stats structure (on thread
exit or when the reporting function is called). Most of the pystats related
code has be moved into the file Python/pystats.c.