@@ -1300,6 +1300,7 @@ gc_collect_young(PyThreadState *tstate,
1300
1300
GCState * gcstate = & tstate -> interp -> gc ;
1301
1301
PyGC_Head * young = & gcstate -> young .head ;
1302
1302
PyGC_Head * visited = & gcstate -> old [gcstate -> visited_space ].head ;
1303
+ GC_STAT_ADD (0 , collections , 1 );
1303
1304
#ifdef Py_STATS
1304
1305
{
1305
1306
Py_ssize_t count = 0 ;
@@ -1428,6 +1429,7 @@ completed_cycle(GCState *gcstate)
1428
1429
static void
1429
1430
gc_collect_increment (PyThreadState * tstate , struct gc_collection_stats * stats )
1430
1431
{
1432
+ GC_STAT_ADD (1 , collections , 1 );
1431
1433
GCState * gcstate = & tstate -> interp -> gc ;
1432
1434
PyGC_Head * not_visited = & gcstate -> old [gcstate -> visited_space ^1 ].head ;
1433
1435
PyGC_Head * visited = & gcstate -> old [gcstate -> visited_space ].head ;
@@ -1473,6 +1475,7 @@ static void
1473
1475
gc_collect_full (PyThreadState * tstate ,
1474
1476
struct gc_collection_stats * stats )
1475
1477
{
1478
+ GC_STAT_ADD (2 , collections , 1 );
1476
1479
GCState * gcstate = & tstate -> interp -> gc ;
1477
1480
validate_old (gcstate );
1478
1481
PyGC_Head * young = & gcstate -> young .head ;
@@ -1795,6 +1798,24 @@ PyGC_IsEnabled(void)
1795
1798
return gcstate -> enabled ;
1796
1799
}
1797
1800
1801
+ // Show stats for objects in each generations
1802
+ static void
1803
+ show_stats_each_generations (GCState * gcstate )
1804
+ {
1805
+ char buf [100 ];
1806
+ size_t pos = 0 ;
1807
+
1808
+ for (int i = 0 ; i < NUM_GENERATIONS && pos < sizeof (buf ); i ++ ) {
1809
+ pos += PyOS_snprintf (buf + pos , sizeof (buf )- pos ,
1810
+ " %zd" ,
1811
+ gc_list_size (GEN_HEAD (gcstate , i )));
1812
+ }
1813
+ PySys_FormatStderr (
1814
+ "gc: objects in each generation:%s\n"
1815
+ "gc: objects in permanent generation: %zd\n" ,
1816
+ buf , gc_list_size (& gcstate -> permanent_generation .head ));
1817
+ }
1818
+
1798
1819
Py_ssize_t
1799
1820
_PyGC_Collect (PyThreadState * tstate , int generation , _PyGC_Reason reason )
1800
1821
{
@@ -1810,6 +1831,10 @@ _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
1810
1831
if (reason != _Py_GC_REASON_SHUTDOWN ) {
1811
1832
invoke_gc_callback (gcstate , "start" , generation , & stats );
1812
1833
}
1834
+ if (gcstate -> debug & _PyGC_DEBUG_STATS ) {
1835
+ PySys_WriteStderr ("gc: collecting generation %d...\n" , generation );
1836
+ show_stats_each_generations (gcstate );
1837
+ }
1813
1838
if (PyDTrace_GC_START_ENABLED ()) {
1814
1839
PyDTrace_GC_START (generation );
1815
1840
}
0 commit comments