Elapsed time debugging in gc.c

In GH-108362: Incremental GC implementation #116206 (@markshannon ), it seems to me that the elapsed time debug information disappeared.

This change took effect in Python 3.14.

Is it possible to get back that piece of debug information?

In Python 3.13 with gc.DEBUG_STATS you would get information like this:

gc: collecting generation 2...
gc: objects in each generation: 1473 0 5043
gc: objects in permanent generation: 0
gc: done, 0 unreachable, 0 uncollectable, 0.0236s elapsed

whereas in Python 3.14:

gc: collecting generation 2...
gc: objects in each generation: 1394 5072 248
gc: objects in permanent generation: 0

Ps., I believe that it is commit 1530932 that removes the elapsed time functionality, but I might be wrong.

1 Like

Hi @pdgr, do you want to implement this?

I can give it a shot.

4 Likes

Hi, Sergey. I have prepared a patch. Do you want to look at it first, or should I immediately create a PR and/or issue?

The gist of it is that we now

#include "pycore_time.h"          // _PyTime_MonotonicRaw()

in gc.c, and in gc_collect_region, create a PyTime_t nowand at the end of that function we write it

if (gcstate->debug & _PyGC_DEBUG_STATS) {
    PyTime_t endtime;
    (void)PyTime_MonotonicRaw(&endtime);
    double d = PyTime_AsSecondsDouble(endtime - now);
    PySys_WriteStderr(
        "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
        n+m, n, d
        );
}

Much of this is recovered from before the refactoring.

I’ve also added Py_ssize_t m = 0;that counts unreachable and final_unreachable.

Hi, Pål!

It is fine if you just open PR (technically you have to create an issue for this and the PR that linked the issue, may be you find interesting to check Python Developer’s Guide).

I’m not a codeowner for GC or core-dev, so I just can give you some suggestions, but last decision will be to core-devs.

First, you don’t need to introduce m variable you can use stats→collected and stats→uncollectable.

Second, I propose to add time measurements to _PyGC_Collect this will not be 100% equal to 3.13 version, but IMHO more appropriate here.

So, create the PR and let other devs review your solution. Feel free to ping me you have some questions.

Thanks!

Issue Bring back elapsed time debugging in gc.c · Issue #140358 · python/cpython · GitHub

PR https://github.com/python/cpython/pull/140359

PR has been merged and issue closed.

Thanks for the assistance, Sergey!

3 Likes

Thanks for implementing this!

1 Like