@@ -45,12 +45,13 @@ static inline PyObject* _Py_FROM_GC(PyGC_Head *gc) {
4545 * the per-object lock.
4646 */
4747#ifdef Py_GIL_DISABLED
48- # define _PyGC_BITS_TRACKED (1) // Tracked by the GC
49- # define _PyGC_BITS_FINALIZED (2) // tp_finalize was called
50- # define _PyGC_BITS_UNREACHABLE (4)
51- # define _PyGC_BITS_FROZEN (8)
52- # define _PyGC_BITS_SHARED (16)
53- # define _PyGC_BITS_DEFERRED (64) // Use deferred reference counting
48+ # define _PyGC_BITS_TRACKED (1<<0) // Tracked by the GC
49+ # define _PyGC_BITS_FINALIZED (1<<1) // tp_finalize was called
50+ # define _PyGC_BITS_UNREACHABLE (1<<2)
51+ # define _PyGC_BITS_FROZEN (1<<3)
52+ # define _PyGC_BITS_SHARED (1<<4)
53+ # define _PyGC_BITS_ALIVE (1<<5) // Reachable from a known root.
54+ # define _PyGC_BITS_DEFERRED (1<<7) // Use deferred reference counting
5455#endif
5556
5657#ifdef Py_GIL_DISABLED
@@ -289,6 +290,38 @@ enum _GCPhase {
289290 GC_PHASE_COLLECT = 1
290291};
291292
293+ // if true, enable GC timing statistics
294+ #define WITH_GC_TIMING_STATS 0
295+
296+ #if WITH_GC_TIMING_STATS
297+
298+ #define QUANTILE_COUNT 5
299+ #define MARKER_COUNT (QUANTILE_COUNT * 3 + 2)
300+
301+ typedef struct {
302+ double q [MARKER_COUNT ];
303+ double dn [MARKER_COUNT ];
304+ double np [MARKER_COUNT ];
305+ int n [MARKER_COUNT ];
306+ int count ;
307+ double max ;
308+ } p2_engine ;
309+
310+ struct gc_timing_state {
311+ /* timing statistics computed by P^2 algorithm */
312+ p2_engine auto_all ; // timing for all automatic collections
313+ p2_engine auto_full ; // timing for full (gen2) automatic collections
314+ /* Total time spent inside cyclic GC */
315+ PyTime_t gc_total_time ;
316+ /* Time spent inside incremental mark part of cyclic GC */
317+ PyTime_t gc_mark_time ;
318+ /* Maximum GC pause time */
319+ PyTime_t gc_max_pause ;
320+ /* Total number of times GC was run */
321+ PyTime_t gc_runs ;
322+ };
323+ #endif // WITH_GC_TIMING_STATS
324+
292325struct _gc_runtime_state {
293326 /* List of objects that still need to be cleaned up, singly linked
294327 * via their gc headers' gc_prev pointers. */
@@ -318,6 +351,11 @@ struct _gc_runtime_state {
318351 int visited_space ;
319352 int phase ;
320353
354+ #if WITH_GC_TIMING_STATS
355+ /* state for GC timing statistics */
356+ struct gc_timing_state timing_state ;
357+ #endif
358+
321359#ifdef Py_GIL_DISABLED
322360 /* This is the number of objects that survived the last full
323361 collection. It approximates the number of long lived objects
0 commit comments