-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-100403: Collect GC statistics via -X option #100958
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
Open
pablogsal
wants to merge
10
commits into
python:main
Choose a base branch
from
pablogsal:gc_stats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+396
−3
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bfbf366
gh-100403: Collect GC statistics when --enable-pystats is provided
pablogsal adc9eb7
fixup! gh-100403: Collect GC statistics when --enable-pystats is prov…
pablogsal e298f62
fixup! fixup! gh-100403: Collect GC statistics when --enable-pystats …
pablogsal 346a444
fixup! fixup! fixup! gh-100403: Collect GC statistics when --enable-p…
pablogsal 1d251c2
fixup! fixup! fixup! fixup! gh-100403: Collect GC statistics when --e…
pablogsal c58b2ff
Make easier to port to Python 3.11.
nascheme 3457f52
Add PYTHONGCTHRESHOLD env var.
nascheme 1024041
Reformat with 'black'.
nascheme 653a781
Add 'Collected per run time' bar chart.
nascheme fbe3ea1
Implement -X option
pablogsal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
#include "pycore_pyerrors.h" | ||
#include "pycore_pystate.h" // _PyThreadState_GET() | ||
#include "pydtrace.h" | ||
#include "pystats.h" | ||
|
||
typedef struct _gc_runtime_state GCState; | ||
|
||
|
@@ -39,6 +40,17 @@ module gc | |
[clinic start generated code]*/ | ||
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5c9690ecc842d79]*/ | ||
|
||
typedef struct _gc_stats { | ||
size_t n_collections; | ||
PyObject* generation_number; | ||
PyObject* total_objects; | ||
PyObject* uncollectable; | ||
PyObject* collected_cycles; | ||
PyObject* collection_time; | ||
} PyGCStats; | ||
|
||
PyGCStats _pygc_stats_struct = { 0 }; | ||
|
||
|
||
#ifdef Py_DEBUG | ||
# define GC_DEBUG | ||
|
@@ -135,6 +147,32 @@ get_gc_state(void) | |
return &interp->gc; | ||
} | ||
|
||
static inline int | ||
is_main_interpreter(void) | ||
{ | ||
return (PyInterpreterState_Get() == PyInterpreterState_Main()); | ||
} | ||
|
||
static int | ||
_PyInitGCStats() { | ||
if (!is_main_interpreter()) { | ||
return 0; | ||
} | ||
#define INIT_FIELD(field) {\ | ||
_pygc_stats_struct.field = PyList_New(0);\ | ||
if (_pygc_stats_struct.field== NULL) {\ | ||
return -1; \ | ||
}\ | ||
_PyObject_GC_UNTRACK(_pygc_stats_struct.field); } | ||
|
||
INIT_FIELD(generation_number); | ||
pablogsal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
INIT_FIELD(total_objects); | ||
INIT_FIELD(uncollectable); | ||
INIT_FIELD(collected_cycles); | ||
INIT_FIELD(collection_time); | ||
#undef INIT_FIELD | ||
return 0; | ||
} | ||
|
||
void | ||
_PyGC_InitState(GCState *gcstate) | ||
|
@@ -151,8 +189,15 @@ _PyGC_InitState(GCState *gcstate) | |
}; | ||
gcstate->generation0 = GEN_HEAD(gcstate, 0); | ||
INIT_HEAD(gcstate->permanent_generation); | ||
|
||
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. Spurious edit. |
||
#undef INIT_HEAD | ||
|
||
char *s = Py_GETENV("PYTHONGCTHRESHOLD"); | ||
if (s) { | ||
long n = atol(s); | ||
if (n > 0) { | ||
gcstate->generations[0].threshold = n; | ||
} | ||
} | ||
} | ||
|
||
|
||
|
@@ -171,6 +216,13 @@ _PyGC_Init(PyInterpreterState *interp) | |
return _PyStatus_NO_MEMORY(); | ||
} | ||
|
||
if (_Py_GetConfig()->gc_stats_file) { | ||
if(_PyInitGCStats()) { | ||
Py_FatalError("Could not initialize GC stats"); | ||
} | ||
gcstate->advanced_stats = 1; | ||
} | ||
|
||
return _PyStatus_OK(); | ||
} | ||
|
||
|
@@ -1192,6 +1244,13 @@ gc_collect_main(PyThreadState *tstate, int generation, | |
PyGC_Head *gc; | ||
_PyTime_t t1 = 0; /* initialize to prevent a compiler warning */ | ||
GCState *gcstate = &tstate->interp->gc; | ||
_PyTime_t gc_t1 = 0; | ||
_PyTime_t gc_t2 = 0; | ||
Py_ssize_t t = 0; | ||
|
||
if (gcstate->advanced_stats) { | ||
gc_t1 = _PyTime_GetPerfCounter(); | ||
} | ||
|
||
// gc_collect_main() must not be called before _PyGC_Init | ||
// or after _PyGC_Fini() | ||
|
@@ -1226,6 +1285,10 @@ gc_collect_main(PyThreadState *tstate, int generation, | |
old = young; | ||
validate_list(old, collecting_clear_unreachable_clear); | ||
|
||
if (gcstate->advanced_stats) { | ||
t = gc_list_size(young); | ||
} | ||
|
||
deduce_unreachable(young, &unreachable); | ||
|
||
untrack_tuples(young); | ||
|
@@ -1324,6 +1387,31 @@ gc_collect_main(PyThreadState *tstate, int generation, | |
_PyErr_WriteUnraisableMsg("in garbage collection", NULL); | ||
} | ||
} | ||
if (gcstate->advanced_stats) { | ||
gc_t2 = _PyTime_GetPerfCounter(); | ||
#define ADD_ELEMENT(field, elem, converter_fn) \ | ||
{ \ | ||
PyObject* item = converter_fn(elem); \ | ||
if (!item) { \ | ||
_PyErr_WriteUnraisableMsg("in garbage collection", NULL); \ | ||
} \ | ||
if (item && PyList_Append(_pygc_stats_struct.field, item) >= 0) { \ | ||
_PyErr_WriteUnraisableMsg("in garbage collection", NULL); \ | ||
} \ | ||
Py_XDECREF(item); \ | ||
} \ | ||
|
||
if (_pygc_stats_struct.generation_number) { | ||
_pygc_stats_struct.n_collections++; | ||
ADD_ELEMENT(generation_number, generation, PyLong_FromLong); | ||
ADD_ELEMENT(total_objects, t, PyLong_FromSsize_t); | ||
ADD_ELEMENT(uncollectable, n, PyLong_FromSsize_t); | ||
ADD_ELEMENT(collected_cycles, m, PyLong_FromSsize_t); | ||
double d = _PyTime_AsSecondsDouble(gc_t2 - gc_t1); | ||
ADD_ELEMENT(collection_time, d, PyFloat_FromDouble); | ||
} | ||
#undef ADD_ELEMENT | ||
} | ||
|
||
/* Update stats */ | ||
if (n_collected) { | ||
|
@@ -2168,11 +2256,66 @@ gc_fini_untrack(PyGC_Head *list) | |
} | ||
} | ||
|
||
static void | ||
print_stats(FILE *out) { | ||
#define WRITE_ITEM(collection, index, type, converter_fn, str_format) { \ | ||
PyObject* item = PyList_GET_ITEM(_pygc_stats_struct.collection, index); \ | ||
if (!item) { \ | ||
_PyErr_WriteUnraisableMsg(" when writing gc stats", NULL); \ | ||
} \ | ||
type num = converter_fn(item); \ | ||
if (!num && PyErr_Occurred()) { \ | ||
_PyErr_WriteUnraisableMsg(" when writing gc stats", NULL); \ | ||
} \ | ||
fprintf(out, str_format, num); } \ | ||
|
||
fprintf(out, "collection,generation_number,total_objects,uncollectable,collected_cycles,collection_time\n"); | ||
for (size_t i = 0; i < _pygc_stats_struct.n_collections; i++) { | ||
fprintf(out, "%zd", i); | ||
WRITE_ITEM(generation_number, i, long, PyLong_AsLong, "%zd,"); | ||
WRITE_ITEM(total_objects, i, long, PyLong_AsLong, "%zd,"); | ||
WRITE_ITEM(uncollectable, i, long, PyLong_AsLong, "%zd,"); | ||
WRITE_ITEM(collected_cycles, i, long, PyLong_AsLong, "%zd,"); | ||
WRITE_ITEM(collection_time, i, double, PyFloat_AsDouble, "%f"); | ||
fprintf(out, "\n"); | ||
} | ||
} | ||
|
||
void | ||
_Py_PrintGCStats(int to_file) | ||
{ | ||
FILE *out = stderr; | ||
wchar_t *filename = _Py_GetConfig()->gc_stats_file; | ||
if (filename && wcslen(filename) != 0) { | ||
// Open a file from the gc_stats_file (wchar) | ||
out = _Py_wfopen(filename, L"wb"); | ||
if (!out) { | ||
_PyErr_WriteUnraisableMsg(" when writing gc stats", NULL); | ||
return; | ||
} | ||
} | ||
else { | ||
fprintf(out, "GC stats:\n"); | ||
} | ||
print_stats(out); | ||
if (out != stderr) { | ||
fclose(out); | ||
} | ||
|
||
Py_CLEAR(_pygc_stats_struct.generation_number); | ||
Py_CLEAR(_pygc_stats_struct.total_objects); | ||
Py_CLEAR(_pygc_stats_struct.uncollectable); | ||
Py_CLEAR(_pygc_stats_struct.collected_cycles); | ||
Py_CLEAR(_pygc_stats_struct.collection_time); | ||
} | ||
|
||
void | ||
_PyGC_Fini(PyInterpreterState *interp) | ||
{ | ||
GCState *gcstate = &interp->gc; | ||
if (is_main_interpreter() && gcstate->advanced_stats) { | ||
_Py_PrintGCStats(1); | ||
} | ||
Py_CLEAR(gcstate->garbage); | ||
Py_CLEAR(gcstate->callbacks); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Spurious edit.
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.
Sir Bedevere obtained new powers?