From 4169667e5cf80952c20dff2a6123e18b3df89fc1 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Thu, 21 Mar 2024 09:45:24 -0400 Subject: [PATCH 1/2] gh-117122: Fix pystats after incremental GC changes --- Include/cpython/pystats.h | 1 + Python/gc.c | 9 ++++----- Python/specialize.c | 1 + Tools/scripts/summarize_stats.py | 9 ++++++++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Include/cpython/pystats.h b/Include/cpython/pystats.h index 887fbbedf88502..c575b9106b7dbe 100644 --- a/Include/cpython/pystats.h +++ b/Include/cpython/pystats.h @@ -94,6 +94,7 @@ typedef struct _gc_stats { uint64_t collections; uint64_t object_visits; uint64_t objects_collected; + uint64_t objects_queued; } GCStats; typedef struct _uop_stats { diff --git a/Python/gc.c b/Python/gc.c index d0f4ce38bbe567..0f168ed66c1fcb 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -1384,7 +1384,6 @@ expand_region_transitively_reachable(PyGC_Head *container, PyGC_Head *gc, GCStat static void completed_cycle(GCState *gcstate) { - PyGC_Head *not_visited = &gcstate->old[gcstate->visited_space^1].head; assert(gc_list_is_empty(not_visited)); gcstate->visited_space = flip_old_space(gcstate->visited_space); if (gcstate->work_to_do > 0) { @@ -1421,7 +1420,7 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats) gc_set_old_space(gc, gcstate->visited_space); increment_size += expand_region_transitively_reachable(&increment, gc, gcstate); } - GC_STAT_ADD(1, objects_queued, region_size); + GC_STAT_ADD(1, objects_queued, increment_size); PyGC_Head survivors; gc_list_init(&survivors); gc_collect_region(tstate, &increment, &survivors, UNTRACK_TUPLES, stats); @@ -1807,10 +1806,10 @@ _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason) _PyErr_SetRaisedException(tstate, exc); GC_STAT_ADD(generation, objects_collected, stats.collected); #ifdef Py_STATS - if (_py_stats) { + if (_Py_stats) { GC_STAT_ADD(generation, object_visits, - _py_stats->object_stats.object_visits); - _py_stats->object_stats.object_visits = 0; + _Py_stats->object_stats.object_visits); + _Py_stats->object_stats.object_visits = 0; } #endif validate_old(gcstate); diff --git a/Python/specialize.c b/Python/specialize.c index b1f9eb756c3665..09bfbede04eb68 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -212,6 +212,7 @@ print_gc_stats(FILE *out, GCStats *stats) fprintf(out, "GC[%d] collections: %" PRIu64 "\n", i, stats[i].collections); fprintf(out, "GC[%d] object visits: %" PRIu64 "\n", i, stats[i].object_visits); fprintf(out, "GC[%d] objects collected: %" PRIu64 "\n", i, stats[i].objects_collected); + fprintf(out, "GC[%d] objects queued: %" PRIu64 "\n", i, stats[i].objects_queued); } } diff --git a/Tools/scripts/summarize_stats.py b/Tools/scripts/summarize_stats.py index 6af14e1b769b80..ba1fc3e9ddcc3e 100644 --- a/Tools/scripts/summarize_stats.py +++ b/Tools/scripts/summarize_stats.py @@ -1103,6 +1103,7 @@ def calc_gc_stats(stats: Stats) -> Rows: Count(gen["collections"]), Count(gen["objects collected"]), Count(gen["object visits"]), + Count(gen["objects queued"]), ) for (i, gen) in enumerate(gc_stats) ] @@ -1112,7 +1113,13 @@ def calc_gc_stats(stats: Stats) -> Rows: "GC collections and effectiveness", [ Table( - ("Generation:", "Collections:", "Objects collected:", "Object visits:"), + ( + "Generation:", + "Collections:", + "Objects collected:", + "Object visits:", + "Objects queued:", + ), calc_gc_stats, ) ], From 881c4e15328fbb1c92846802bb21263f39fecbb6 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Thu, 21 Mar 2024 09:55:18 -0400 Subject: [PATCH 2/2] Fix Py_DEBUG builds --- Python/gc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Python/gc.c b/Python/gc.c index 0f168ed66c1fcb..82adc6f904fa71 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -1384,7 +1384,11 @@ expand_region_transitively_reachable(PyGC_Head *container, PyGC_Head *gc, GCStat static void completed_cycle(GCState *gcstate) { + #ifdef Py_DEBUG + PyGC_Head *not_visited = &gcstate->old[gcstate->visited_space^1].head; assert(gc_list_is_empty(not_visited)); + #endif + gcstate->visited_space = flip_old_space(gcstate->visited_space); if (gcstate->work_to_do > 0) { gcstate->work_to_do = 0;