Skip to content

Commit c2bf816

Browse files
committed
Bring back elapsed time and unreachable count to gc debug
1 parent baf4515 commit c2bf816

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Python/gc.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "pycore_interpframe.h" // _PyFrame_GetLocalsArray()
1111
#include "pycore_object_alloc.h" // _PyObject_MallocWithType()
1212
#include "pycore_pystate.h" // _PyThreadState_GET()
13+
#include "pycore_time.h" // _PyTime_MonotonicRaw()
1314
#include "pycore_tuple.h" // _PyTuple_MaybeUntrack()
1415
#include "pycore_weakref.h" // _PyWeakref_ClearRef()
1516

@@ -1816,6 +1817,7 @@ gc_collect_region(PyThreadState *tstate,
18161817
/* Collect statistics on uncollectable objects found and print
18171818
* debugging information. */
18181819
Py_ssize_t n = 0;
1820+
18191821
for (gc = GC_NEXT(&finalizers); gc != &finalizers; gc = GC_NEXT(gc)) {
18201822
n++;
18211823
if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE)
@@ -2066,6 +2068,8 @@ _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
20662068
{
20672069
GCState *gcstate = &tstate->interp->gc;
20682070
assert(tstate->current_frame == NULL || tstate->current_frame->stackpointer != NULL);
2071+
PyTime_t now;
2072+
(void)PyTime_MonotonicRaw(&now);
20692073

20702074
int expected = 0;
20712075
if (!_Py_atomic_compare_exchange_int(&gcstate->collecting, &expected, 1)) {
@@ -2115,6 +2119,17 @@ _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
21152119
#endif
21162120
validate_spaces(gcstate);
21172121
_Py_atomic_store_int(&gcstate->collecting, 0);
2122+
2123+
if (gcstate->debug & _PyGC_DEBUG_STATS) {
2124+
PyTime_t endtime;
2125+
(void)PyTime_MonotonicRaw(&endtime);
2126+
double d = PyTime_AsSecondsDouble(endtime - now);
2127+
PySys_WriteStderr(
2128+
"gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
2129+
stats.collected, stats.uncollectable, d
2130+
);
2131+
}
2132+
21182133
return stats.uncollectable + stats.collected;
21192134
}
21202135

0 commit comments

Comments
 (0)