-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
GH-140638: Add a GC "duration" stat
#141720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Expose a ``"duration"`` stat in :func:`gc.get_stats` and | ||
| :data:`gc.callbacks`. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1911,7 +1911,7 @@ | |||||
| static void | ||||||
| invoke_gc_callback(PyThreadState *tstate, const char *phase, | ||||||
| int generation, Py_ssize_t collected, | ||||||
| Py_ssize_t uncollectable) | ||||||
| Py_ssize_t uncollectable, double duration) | ||||||
| { | ||||||
| assert(!_PyErr_Occurred(tstate)); | ||||||
|
|
||||||
|
|
@@ -1928,7 +1928,8 @@ | |||||
| info = Py_BuildValue("{sisnsn}", | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to change this line to
Suggested change
no?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch, thanks. Annoying that it just ignores trailing args like that. |
||||||
| "generation", generation, | ||||||
| "collected", collected, | ||||||
| "uncollectable", uncollectable); | ||||||
| "uncollectable", uncollectable, | ||||||
| "duration", duration); | ||||||
| if (info == NULL) { | ||||||
| PyErr_FormatUnraisable("Exception ignored while " | ||||||
| "invoking gc callbacks"); | ||||||
|
|
@@ -2340,7 +2341,6 @@ | |||||
| { | ||||||
| Py_ssize_t m = 0; /* # objects collected */ | ||||||
| Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */ | ||||||
| PyTime_t t1 = 0; /* initialize to prevent a compiler warning */ | ||||||
| GCState *gcstate = &tstate->interp->gc; | ||||||
|
|
||||||
| // gc_collect_main() must not be called before _PyGC_Init | ||||||
|
|
@@ -2372,19 +2372,21 @@ | |||||
| GC_STAT_ADD(generation, collections, 1); | ||||||
|
|
||||||
| if (reason != _Py_GC_REASON_SHUTDOWN) { | ||||||
| invoke_gc_callback(tstate, "start", generation, 0, 0); | ||||||
| invoke_gc_callback(tstate, "start", generation, 0, 0, 0); | ||||||
| } | ||||||
|
|
||||||
| if (gcstate->debug & _PyGC_DEBUG_STATS) { | ||||||
| PySys_WriteStderr("gc: collecting generation %d...\n", generation); | ||||||
| show_stats_each_generations(gcstate); | ||||||
| // ignore error: don't interrupt the GC if reading the clock fails | ||||||
| (void)PyTime_PerfCounterRaw(&t1); | ||||||
|
Check warning on line 2382 in Python/gc_free_threading.c
|
||||||
| } | ||||||
|
|
||||||
| if (PyDTrace_GC_START_ENABLED()) { | ||||||
| PyDTrace_GC_START(generation); | ||||||
| } | ||||||
| PyTime_t start, stop; | ||||||
| (void)PyTime_PerfCounterRaw(&start); | ||||||
|
|
||||||
| PyInterpreterState *interp = tstate->interp; | ||||||
|
|
||||||
|
|
@@ -2399,13 +2401,13 @@ | |||||
| m = state.collected; | ||||||
| n = state.uncollectable; | ||||||
|
|
||||||
| (void)PyTime_PerfCounterRaw(&stop); | ||||||
| double duration = PyTime_AsSecondsDouble(stop - start); | ||||||
|
|
||||||
| if (gcstate->debug & _PyGC_DEBUG_STATS) { | ||||||
| PyTime_t t2; | ||||||
| (void)PyTime_PerfCounterRaw(&t2); | ||||||
| double d = PyTime_AsSecondsDouble(t2 - t1); | ||||||
| PySys_WriteStderr( | ||||||
| "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n", | ||||||
| n+m, n, d); | ||||||
| n+m, n, duration); | ||||||
| } | ||||||
|
|
||||||
| // Clear the current thread's free-list again. | ||||||
|
|
@@ -2426,6 +2428,7 @@ | |||||
| stats->collections++; | ||||||
| stats->collected += m; | ||||||
| stats->uncollectable += n; | ||||||
| stats->duration += duration; | ||||||
|
|
||||||
| GC_STAT_ADD(generation, objects_collected, m); | ||||||
| #ifdef Py_STATS | ||||||
|
|
@@ -2444,7 +2447,7 @@ | |||||
| } | ||||||
|
|
||||||
| if (reason != _Py_GC_REASON_SHUTDOWN) { | ||||||
| invoke_gc_callback(tstate, "stop", generation, m, n); | ||||||
| invoke_gc_callback(tstate, "stop", generation, m, n, duration); | ||||||
| } | ||||||
|
|
||||||
| assert(!_PyErr_Occurred(tstate)); | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just did this for consistency with the above points, haha.