]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] GH-122298: Restore printing of GC stats (GH-123261) (#123268)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 2 Sep 2024 11:10:27 +0000 (13:10 +0200)
committerGitHub <noreply@github.com>
Mon, 2 Sep 2024 11:10:27 +0000 (13:10 +0200)
GH-122298: Restore printing of GC stats (GH-123261)
(cherry picked from commit 7cd3aa42f0cf72bf9a214e2630850879fe078377)

Co-authored-by: Mark Shannon <mark@hotpy.org>
Misc/NEWS.d/next/Core_and_Builtins/2024-08-23-11-26-54.gh-issue-122298.ZMyln4.rst [new file with mode: 0644]
Python/gc.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-08-23-11-26-54.gh-issue-122298.ZMyln4.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-08-23-11-26-54.gh-issue-122298.ZMyln4.rst
new file mode 100644 (file)
index 0000000..e7645bf
--- /dev/null
@@ -0,0 +1,3 @@
+Restore printout of GC stats when ``gc.set_debug(gc.DEBUG_STATS)`` is
+called. This featue was accidentally removed when implementing incremental
+GC.
index aa8b216124c36a84abcb6760f69f2ea7e638d594..de24e486541f4633222181b4b1ea9636fb0130f1 100644 (file)
@@ -1289,6 +1289,7 @@ gc_collect_young(PyThreadState *tstate,
     GCState *gcstate = &tstate->interp->gc;
     PyGC_Head *young = &gcstate->young.head;
     PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
+    GC_STAT_ADD(0, collections, 1);
 #ifdef Py_STATS
     {
         Py_ssize_t count = 0;
@@ -1417,6 +1418,7 @@ completed_cycle(GCState *gcstate)
 static void
 gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
 {
+    GC_STAT_ADD(1, collections, 1);
     GCState *gcstate = &tstate->interp->gc;
     PyGC_Head *not_visited = &gcstate->old[gcstate->visited_space^1].head;
     PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
@@ -1462,6 +1464,7 @@ static void
 gc_collect_full(PyThreadState *tstate,
                 struct gc_collection_stats *stats)
 {
+    GC_STAT_ADD(2, collections, 1);
     GCState *gcstate = &tstate->interp->gc;
     validate_old(gcstate);
     PyGC_Head *young = &gcstate->young.head;
@@ -1784,6 +1787,24 @@ PyGC_IsEnabled(void)
     return gcstate->enabled;
 }
 
+// Show stats for objects in each generations
+static void
+show_stats_each_generations(GCState *gcstate)
+{
+    char buf[100];
+    size_t pos = 0;
+
+    for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) {
+        pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos,
+                             " %zd",
+                             gc_list_size(GEN_HEAD(gcstate, i)));
+    }
+    PySys_FormatStderr(
+        "gc: objects in each generation:%s\n"
+        "gc: objects in permanent generation: %zd\n",
+        buf, gc_list_size(&gcstate->permanent_generation.head));
+}
+
 Py_ssize_t
 _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
 {
@@ -1799,6 +1820,10 @@ _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
     if (reason != _Py_GC_REASON_SHUTDOWN) {
         invoke_gc_callback(gcstate, "start", generation, &stats);
     }
+    if (gcstate->debug & _PyGC_DEBUG_STATS) {
+        PySys_WriteStderr("gc: collecting generation %d...\n", generation);
+        show_stats_each_generations(gcstate);
+    }
     if (PyDTrace_GC_START_ENABLED()) {
         PyDTrace_GC_START(generation);
     }