-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
gh-131253: free-threaded build support for pystats #137189
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1c194e2
gh-131253: free-threaded build support for pystats
nascheme 7dc9191
Make pystats global state be per-interpreter.
nascheme 60454a2
Add NEWS.
nascheme df1c5d4
Merge 'origin/main' into gh-131253-pystats-ft
nascheme c94e5c4
Fix some data race issues.
nascheme e77af2c
Remove _Py_tss_stats thread local.
nascheme 488f0be
Merge 'origin/main' into gh-131253-pystats-ft
nascheme a48c7f6
Improve _Py_STATS_COND_EXPR macro.
nascheme 612ea96
Improve comment for ts->pystats member.
nascheme 0e09267
Cleanup init of interp->pystats_struct.
nascheme a3cfb7e
Split stats zeroing into separate functions.
nascheme 724b086
Merge 'origin/main' into gh-131253-pystats-ft
nascheme bef1bcb
Fix a couple bugs related to stats allocation.
nascheme 5034375
Add unit test for pystats.
nascheme af9d886
Merge 'origin/main' into gh-131253-pystats-ft
nascheme 09011fe
Typo fix for Lib/test/test_pystats.py
nascheme 1e5db7a
Typo fix for comment in Include/cpython/pystate.h
nascheme 873d915
Code style tweaks.
nascheme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| // | ||
| // - _Py_INCREF_STAT_INC() and _Py_DECREF_STAT_INC() used by Py_INCREF() | ||
| // and Py_DECREF(). | ||
| // - _Py_stats variable | ||
| // - _PyStats_GET() | ||
| // | ||
| // Functions of the sys module: | ||
| // | ||
|
|
@@ -14,7 +14,7 @@ | |
| // - sys._stats_dump() | ||
| // | ||
| // Python must be built with ./configure --enable-pystats to define the | ||
| // Py_STATS macro. | ||
| // _PyStats_GET() function. | ||
| // | ||
| // Define _PY_INTERPRETER macro to increment interpreter_increfs and | ||
| // interpreter_decrefs. Otherwise, increment increfs and decrefs. | ||
|
|
@@ -109,6 +109,15 @@ typedef struct _gc_stats { | |
| uint64_t objects_not_transitively_reachable; | ||
| } GCStats; | ||
|
|
||
| #ifdef Py_GIL_DISABLED | ||
| // stats specific to free-threaded build | ||
| typedef struct _ft_stats { | ||
| uint64_t mutex_sleeps; | ||
| uint64_t qsbr_polls; | ||
| uint64_t world_stops; | ||
| } FTStats; | ||
| #endif | ||
|
|
||
| typedef struct _uop_stats { | ||
| uint64_t execution_count; | ||
| uint64_t miss; | ||
|
|
@@ -173,22 +182,57 @@ typedef struct _stats { | |
| CallStats call_stats; | ||
| ObjectStats object_stats; | ||
| OptimizationStats optimization_stats; | ||
| #ifdef Py_GIL_DISABLED | ||
| FTStats ft_stats; | ||
| #endif | ||
| RareEventStats rare_event_stats; | ||
| GCStats *gc_stats; | ||
| GCStats gc_stats[3]; // must match NUM_GENERATIONS | ||
| } PyStats; | ||
|
|
||
|
|
||
| // Export for shared extensions like 'math' | ||
| PyAPI_DATA(PyStats*) _Py_stats; | ||
| #if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE) | ||
| extern _Py_thread_local PyStats *_Py_tss_stats; | ||
| #endif | ||
|
|
||
| // Export for most shared extensions, used via _PyStats_GET() static | ||
| // inline function. | ||
| PyAPI_FUNC(PyStats *) _PyStats_GetLocal(void); | ||
|
|
||
| // Return pointer to the PyStats structure, NULL if recording is off. | ||
| static inline PyStats* | ||
| _PyStats_GET(void) | ||
| { | ||
| #if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
| return _Py_tss_stats; | ||
| #else | ||
| return _PyStats_GetLocal(); | ||
| #endif | ||
| } | ||
|
|
||
| #define _Py_STATS_EXPR(expr) \ | ||
| do { \ | ||
| PyStats *s = _PyStats_GET(); \ | ||
| if (s != NULL) { \ | ||
| s->expr; \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| #define _Py_STATS_COND_EXPR(cond, expr) \ | ||
| do { \ | ||
| PyStats *s = _PyStats_GET(); \ | ||
| if (s != NULL && cond) { \ | ||
nascheme marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| s->expr; \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| #ifdef _PY_INTERPRETER | ||
| # define _Py_INCREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.interpreter_increfs++; } while (0) | ||
| # define _Py_DECREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.interpreter_decrefs++; } while (0) | ||
| # define _Py_INCREF_IMMORTAL_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.interpreter_immortal_increfs++; } while (0) | ||
| # define _Py_DECREF_IMMORTAL_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.interpreter_immortal_decrefs++; } while (0) | ||
| # define _Py_INCREF_STAT_INC() _Py_STATS_EXPR(object_stats.interpreter_increfs++) | ||
| # define _Py_DECREF_STAT_INC() _Py_STATS_EXPR(object_stats.interpreter_decrefs++) | ||
| # define _Py_INCREF_IMMORTAL_STAT_INC() _Py_STATS_EXPR(object_stats.interpreter_immortal_increfs++) | ||
| # define _Py_DECREF_IMMORTAL_STAT_INC() _Py_STATS_EXPR(object_stats.interpreter_immortal_decrefs++) | ||
| #else | ||
| # define _Py_INCREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.increfs++; } while (0) | ||
| # define _Py_DECREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.decrefs++; } while (0) | ||
| # define _Py_INCREF_IMMORTAL_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.immortal_increfs++; } while (0) | ||
| # define _Py_DECREF_IMMORTAL_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.immortal_decrefs++; } while (0) | ||
| # define _Py_INCREF_STAT_INC() _Py_STATS_EXPR(object_stats.increfs++) | ||
| # define _Py_DECREF_STAT_INC() _Py_STATS_EXPR(object_stats.decrefs++) | ||
| # define _Py_INCREF_IMMORTAL_STAT_INC() _Py_STATS_EXPR(object_stats.immortal_increfs++) | ||
| # define _Py_DECREF_IMMORTAL_STAT_INC() _Py_STATS_EXPR(object_stats.immortal_decrefs++) | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core_and_Builtins/2025-07-29-17-51-14.gh-issue-131253.GpRjWy.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Support the ``--enable-pystats`` build option for the free-threaded build. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.