Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Lib/test/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,34 @@ def __del__(self):
rc, out, err = assert_python_ok('-c', code)
self.assertEqual(out.strip(), b'__del__ called')

@unittest.skipIf(Py_GIL_DISABLED, "requires GC generations or increments")
def test_gc_debug_stats(self):
# Checks that debug information is printed to stderr
# when DEBUG_STATS is set.
code = """if 1:
import gc
gc.set_debug(%s)
d = {}
d[(1,2)] = 1
gc.collect()
"""
_, _, err = assert_python_ok("-c", code % "gc.DEBUG_STATS")
self.assertRegex(err, b"gc: collecting generation [0-9]+")
self.assertRegex(
err,
b"gc: objects in each generation: [0-9]+ [0-9]+ [0-9]+",
)
self.assertRegex(
err, b"gc: objects in permanent generation: [0-9]+"
)
self.assertRegex(
err,
b"gc: done, .* unreachable, .* uncollectable, .* elapsed",
)

_, _, err = assert_python_ok("-c", code % "0")
self.assertNotIn(b"elapsed", err)

def test_global_del_SystemExit(self):
code = """if 1:
class ClassWithDel:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Restore elapsed time and unreachable object count in GC debug output. These
were inadvertently removed during a refactor of ``gc.c``. The debug log now
again reports elapsed collection time and the number of unreachable objects.
14 changes: 14 additions & 0 deletions Python/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "pycore_interpframe.h" // _PyFrame_GetLocalsArray()
#include "pycore_object_alloc.h" // _PyObject_MallocWithType()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_time.h" // _PyTime_PerfCounterUnchecked()
#include "pycore_tuple.h" // _PyTuple_MaybeUntrack()
#include "pycore_weakref.h" // _PyWeakref_ClearRef()

Expand Down Expand Up @@ -2077,8 +2078,10 @@ _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
if (reason != _Py_GC_REASON_SHUTDOWN) {
invoke_gc_callback(gcstate, "start", generation, &stats);
}
PyTime_t t1 = 0; /* initialize to prevent a compiler warning */
if (gcstate->debug & _PyGC_DEBUG_STATS) {
PySys_WriteStderr("gc: collecting generation %d...\n", generation);
(void)PyTime_PerfCounterRaw(&t1);
show_stats_each_generations(gcstate);
}
if (PyDTrace_GC_START_ENABLED()) {
Expand Down Expand Up @@ -2115,6 +2118,17 @@ _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
#endif
validate_spaces(gcstate);
_Py_atomic_store_int(&gcstate->collecting, 0);

if (gcstate->debug & _PyGC_DEBUG_STATS) {
PyTime_t t2 = 0; /* initialize to prevent a compiler warning */
(void)PyTime_PerfCounterRaw(&t2);
double d = PyTime_AsSecondsDouble(t2 - t1);
PySys_WriteStderr(
"gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
stats.collected + stats.uncollectable, stats.uncollectable, d
);
}

return stats.uncollectable + stats.collected;
}

Expand Down
Loading