Skip to content

Commit fe8f02e

Browse files
committed
Fix duration in gc.get_stats()
1 parent cce1e16 commit fe8f02e

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

Lib/test/test_gc.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,10 @@ def test_get_stats(self):
862862
self.assertEqual(new[0]["collections"], old[0]["collections"] + 1)
863863
self.assertEqual(new[1]["collections"], old[1]["collections"])
864864
self.assertEqual(new[2]["collections"], old[2]["collections"])
865-
for stat in ["collected", "uncollectable", "duration"]:
865+
self.assertGreater(new[0]["duration"], old[0]["duration"])
866+
self.assertEqual(new[1]["duration"], old[1]["duration"])
867+
self.assertEqual(new[2]["duration"], old[2]["duration"])
868+
for stat in ["collected", "uncollectable"]:
866869
self.assertGreaterEqual(new[0][stat], old[0][stat])
867870
self.assertEqual(new[1][stat], old[1][stat])
868871
self.assertEqual(new[2][stat], old[2][stat])
@@ -871,7 +874,10 @@ def test_get_stats(self):
871874
self.assertEqual(new[0]["collections"], old[0]["collections"])
872875
self.assertEqual(new[1]["collections"], old[1]["collections"])
873876
self.assertEqual(new[2]["collections"], old[2]["collections"] + 1)
874-
for stat in ["collected", "uncollectable", "duration"]:
877+
self.assertEqual(new[0]["duration"], old[0]["duration"])
878+
self.assertEqual(new[1]["duration"], old[1]["duration"])
879+
self.assertGreater(new[2]["duration"], old[2]["duration"])
880+
for stat in ["collected", "uncollectable"]:
875881
self.assertEqual(new[0][stat], old[0][stat])
876882
self.assertEqual(new[1][stat], old[1][stat])
877883
self.assertGreaterEqual(new[2][stat], old[2][stat])

Python/gc.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,6 @@ gc_collect_young(PyThreadState *tstate,
13881388
validate_spaces(gcstate);
13891389
gcstate->young.count = 0;
13901390
gcstate->old[gcstate->visited_space].count++;
1391-
add_stats(gcstate, 0, stats);
13921391
validate_spaces(gcstate);
13931392
}
13941393

@@ -1702,7 +1701,6 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
17021701
assert(gc_list_is_empty(&increment));
17031702
gcstate->work_to_do -= increment_size;
17041703

1705-
add_stats(gcstate, 1, stats);
17061704
if (gc_list_is_empty(not_visited)) {
17071705
completed_scavenge(gcstate);
17081706
}
@@ -1737,7 +1735,6 @@ gc_collect_full(PyThreadState *tstate,
17371735
completed_scavenge(gcstate);
17381736
_PyGC_ClearAllFreeLists(tstate->interp);
17391737
validate_spaces(gcstate);
1740-
add_stats(gcstate, 2, stats);
17411738
}
17421739

17431740
/* This is the main function. Read this to understand how the
@@ -2107,6 +2104,7 @@ _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
21072104
}
21082105
(void)PyTime_PerfCounterRaw(&stop);
21092106
stats.duration = PyTime_AsSecondsDouble(stop - start);
2107+
add_stats(gcstate, generation, &stats);
21102108
if (PyDTrace_GC_DONE_ENABLED()) {
21112109
PyDTrace_GC_DONE(stats.uncollectable + stats.collected);
21122110
}

0 commit comments

Comments
 (0)