Skip to content

Commit 7cd3aa4

Browse files
authored
GH-122298: Restore printing of GC stats (GH-123261)
1 parent adc5190 commit 7cd3aa4

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Restore printout of GC stats when ``gc.set_debug(gc.DEBUG_STATS)`` is
2+
called. This featue was accidentally removed when implementing incremental
3+
GC.

Python/gc.c

+25
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,7 @@ gc_collect_young(PyThreadState *tstate,
13001300
GCState *gcstate = &tstate->interp->gc;
13011301
PyGC_Head *young = &gcstate->young.head;
13021302
PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
1303+
GC_STAT_ADD(0, collections, 1);
13031304
#ifdef Py_STATS
13041305
{
13051306
Py_ssize_t count = 0;
@@ -1428,6 +1429,7 @@ completed_cycle(GCState *gcstate)
14281429
static void
14291430
gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
14301431
{
1432+
GC_STAT_ADD(1, collections, 1);
14311433
GCState *gcstate = &tstate->interp->gc;
14321434
PyGC_Head *not_visited = &gcstate->old[gcstate->visited_space^1].head;
14331435
PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
@@ -1473,6 +1475,7 @@ static void
14731475
gc_collect_full(PyThreadState *tstate,
14741476
struct gc_collection_stats *stats)
14751477
{
1478+
GC_STAT_ADD(2, collections, 1);
14761479
GCState *gcstate = &tstate->interp->gc;
14771480
validate_old(gcstate);
14781481
PyGC_Head *young = &gcstate->young.head;
@@ -1795,6 +1798,24 @@ PyGC_IsEnabled(void)
17951798
return gcstate->enabled;
17961799
}
17971800

1801+
// Show stats for objects in each generations
1802+
static void
1803+
show_stats_each_generations(GCState *gcstate)
1804+
{
1805+
char buf[100];
1806+
size_t pos = 0;
1807+
1808+
for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) {
1809+
pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos,
1810+
" %zd",
1811+
gc_list_size(GEN_HEAD(gcstate, i)));
1812+
}
1813+
PySys_FormatStderr(
1814+
"gc: objects in each generation:%s\n"
1815+
"gc: objects in permanent generation: %zd\n",
1816+
buf, gc_list_size(&gcstate->permanent_generation.head));
1817+
}
1818+
17981819
Py_ssize_t
17991820
_PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
18001821
{
@@ -1810,6 +1831,10 @@ _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
18101831
if (reason != _Py_GC_REASON_SHUTDOWN) {
18111832
invoke_gc_callback(gcstate, "start", generation, &stats);
18121833
}
1834+
if (gcstate->debug & _PyGC_DEBUG_STATS) {
1835+
PySys_WriteStderr("gc: collecting generation %d...\n", generation);
1836+
show_stats_each_generations(gcstate);
1837+
}
18131838
if (PyDTrace_GC_START_ENABLED()) {
18141839
PyDTrace_GC_START(generation);
18151840
}

0 commit comments

Comments
 (0)