8000 GH-126491: GC: Mark objects reachable from roots before doing cycle collection by markshannon · Pull Request #126502 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-126491: GC: Mark objects reachable from roots before doing cycle collection #126502

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

Merged
merged 35 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
2ec8d8a
GC experiment: mark almost all reachable objects before doing collect…
markshannon Nov 4, 2024
1fdf00e
Add stats for objects marked
markshannon Nov 4, 2024
5e813c5
Start with mark phase
markshannon Nov 4, 2024
8bd7606
Add stats for visits during marking
markshannon Nov 5, 2024
3513da2
Visit new frames before each increment
markshannon Nov 5, 2024
ab1faec
Redo stats
markshannon Nov 6, 2024
9e2d93c
Fix freezing and GC untracking
markshannon Nov 6, 2024
3c18fc8
Don't untrack dicts
markshannon Nov 6, 2024
94da963
Remove lazy dict tracking from no-gil build
markshannon Nov 6, 2024
659fd1e
Remove unused variable
markshannon Nov 6, 2024
4cfbc4f
Add news
markshannon Nov 6, 2024
8c92ca6
Fix use after free
markshannon Nov 6, 2024
12d7f7c
Attempt more careful fix of use-after-free
markshannon Nov 7, 2024
1f619d7
Typo
markshannon Nov 7, 2024
b55fe37
Fix use of uninitialized variable
markshannon Nov 7, 2024
73b7f52
Fix compiler warnings
markshannon Nov 7, 2024
33f6386
Tweak test
markshannon Nov 7, 2024
8574d00
Add section to internal docs
markshannon Nov 11, 2024
70007b0
Rephrase new docs
markshannon Nov 11, 2024
f043080
Use symbolic constant
markshannon Nov 13, 2024
db2e173
Update section on untracking
markshannon Nov 13, 2024
6a50c2f
Merge branch 'main' into mark-first-gc
markshannon Nov 14, 2024
b9467ec
Update docs
markshannon Nov 14, 2024
14ae8d7
A few more edits
markshannon Nov 14, 2024
3337512
Update comment
markshannon Nov 14, 2024
3ae87fa
Address doc review comments
markshannon Nov 14, 2024
a2d9e3e
Merge branch 'main' into mark-first-gc
markshannon Nov 15, 2024
1452378
Avoid repeated collection of the young gen
markshannon Nov 15, 2024
595b14c
Clearer calculation of work to do.
markshannon Nov 15, 2024
278059b
Make sure tuples are untracked and avoid quadratic time validation
markshannon Nov 15, 2024
f186b4a
Update InternalDocs/garbage_collector.md
markshannon Nov 18, 2024
5f6d04e
Remove unused variable
markshannon Nov 18, 2024
9cfb5f0
Tweak work to do calculation
markshannon Nov 18, 2024
c7683a4
Explain work to do calculation
markshannon Nov 18, 2024
170ea6d
Initialize field to prevent code analyzer warning.
markshannon Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Redo stats
  • Loading branch information
markshannon committed Nov 6, 2024
commit ab1faeca1f198487ca5fb5c8376340a33141611c
4 changes: 2 additions & 2 deletions Include/cpython/pystats.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ typedef struct _object_stats {
typedef struct _gc_stats {
uint64_t collections;
uint64_t object_visits;
uint64_t mark_visits;
uint64_t objects_collected;
uint64_t objects_marked;
uint64_t objects_transitively_reachable;
uint64_t objects_not_transitively_reachable;
} GCStats;

typedef struct _uop_stats {
Expand Down
48 changes: 26 additions & 22 deletions Python/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1370,14 +1370,7 @@ static int
visit_add_to_container(PyObject *op, void *arg)
{
struct container_and_flag *cf = (struct container_and_flag *)arg;
#ifdef Py_STATS
if (cf->mark) {
GC_STAT_ADD(1, mark_visits, 1);
}
else {
OBJECT_STAT_INC(object_visits);
}
#endif
OBJECT_STAT_INC(object_visits);
int visited = cf->visited_space;
assert(visited == get_gc_state()->visited_space);
if (!_Py_IsImmortal(op) && _PyObject_IS_GC(op)) {
Expand Down Expand Up @@ -1482,7 +1475,7 @@ mark_all_reachable(PyGC_Head *reachable, PyGC_Head *visited, int visited_space)
return arg.size;
}

static int
static Py_ssize_t
mark_global_roots(PyInterpreterState *interp, PyGC_Head *visited, int visited_space)
{
PyGC_Head reachable;
Expand All @@ -1491,12 +1484,21 @@ mark_global_roots(PyInterpreterState *interp, PyGC_Head *visited, int visited_sp
objects_marked += move_to_reachable(interp->sysdict, &reachable, visited_space);
objects_marked += move_to_reachable(interp->builtins, &reachable, visited_space);
objects_marked += move_to_reachable(interp->dict, &reachable, visited_space);
struct types_state *types = &interp->types;
for (int i = 0; i < _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES; i++) {
objects_marked += move_to_reachable(types->builtins.initialized[i].tp_dict, &reachable, visited_space);
objects_marked += move_to_reachable(types->builtins.initialized[i].tp_subclasses, &reachable, visited_space);
}
for (int i = 0; i < _Py_MAX_MANAGED_STATIC_EXT_TYPES; i++) {
objects_marked += move_to_reachable(types->for_extensions.initialized[i].tp_dict, &reachable, visited_space);
objects_marked += move_to_reachable(types->for_extensions.initialized[i].tp_subclasses, &reachable, visited_space);
}
objects_marked += mark_all_reachable(&reachable, visited, visited_space);
assert(gc_list_is_empty(&reachable));
return objects_marked;
}

static int
static Py_ssize_t
mark_stacks(PyInterpreterState *interp, PyGC_Head *visited, int visited_space, bool start)
{
PyGC_Head reachable;
Expand Down Expand Up @@ -1550,7 +1552,7 @@ mark_stacks(PyInterpreterState *interp, PyGC_Head *visited, int visited_space, b
return objects_marked;
}

static void
static Py_ssize_t
mark_at_start(PyThreadState *tstate)
{
// TO DO -- Make this incremental
Expand All @@ -1560,12 +1562,8 @@ mark_at_start(PyThreadState *tstate)
Py_ssize_t objects_marked = mark_global_roots(tstate->interp, visited, gcstate->visited_space);
objects_marked += mark_stacks(tstate->interp, visited, gcstate->visited_space, true);
gcstate->work_to_do -= objects_marked;
#ifdef Py_STATS
if (_Py_stats) {
GC_STAT_ADD(1, objects_marked, objects_marked);
}
#endif
gcstate->phase = GC_PHASE_COLLECT;
return objects_marked;
}

static void
Expand All @@ -1575,7 +1573,9 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
GCState *gcstate = &tstate->interp->gc;
gcstate->work_to_do += gcstate->young.count;
if (gcstate->phase == GC_PHASE_MARK) {
mark_at_start(tstate);
Py_ssize_t objects_marked = mark_at_start(tstate);
GC_STAT_ADD(1, objects_transitively_reachable, objects_marked);
gcstate->work_to_do -= objects_marked;
return;
}
PyGC_Head *not_visited = &gcstate->old[gcstate->visited_space^1].head;
Expand All @@ -1586,7 +1586,9 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
if (scale_factor < 1) {
scale_factor = 1;
}
gcstate->work_to_do -= mark_stacks(tstate->interp, visited, gcstate->visited_space, false);
Py_ssize_t objects_marked = mark_stacks(tstate->interp, visited, gcstate->visited_space, false);
GC_STAT_ADD(1, objects_transitively_reachable, objects_marked);
gcstate->work_to_do -= objects_marked;
gc_list_set_space(&gcstate->young.head, gcstate->visited_space);
gc_list_merge(&gcstate->young.head, &increment);
gcstate->young.count = 0;
Expand All @@ -1603,6 +1605,7 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
gc_set_old_space(gc, gcstate->visited_space);
increment_size += expand_region_transitively_reachable(&increment, gc, gcstate);
}
GC_STAT_ADD(1, objects_not_transitively_reachable, increment_size);
gc_list_validate_space(&increment, gcstate->visited_space);
PyGC_Head survivors;
gc_list_init(&survivors);
Expand Down Expand Up @@ -1631,16 +1634,17 @@ gc_collect_full(PyThreadState *tstate,
PyGC_Head *young = &gcstate->young.head;
PyGC_Head *pending = &gcstate->old[gcstate->visited_space^1].head;
PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
mark_global_roots(tstate->interp, visited, gcstate->visited_space);
mark_stacks(tstate->interp, visited, gcstate->visited_space, true);
/* merge all generations into pending */
gc_list_validate_space(young, 1-gcstate->visited_space);
gc_list_merge(young, pending);
gc_list_set_space(visited, 1-gcstate->visited_space);
gc_list_merge(visited, pending);
/* Mark reachable */
mark_global_roots(tstate->interp, visited, gcstate->visited_space);
mark_stacks(tstate->interp, visited, gcstate->visited_space, true);
Py_ssize_t reachable = mark_global_roots(tstate->interp, visited, gcstate->visited_space);
reachable += mark_stacks(tstate->interp, visited, gcstate->visited_space, true);
(void)reachable;
GC_STAT_ADD(2, objects_transitively_reachable, reachable);
GC_STAT_ADD(2, objects_not_transitively_reachable, gc_list_size(pending));
gcstate->young.count = 0;
gc_list_set_space(pending, gcstate->visited_space);
gc_collect_region(tstate, pending, visited,
Expand Down
4 changes: 2 additions & 2 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ print_gc_stats(FILE *out, GCStats *stats)
{
for (int i = 0; i < NUM_GENERATIONS; i++) {
fprintf(out, "GC[%d] collections: %" PRIu64 "\n", i, stats[i].collections);
fprintf(out, "GC[%d] marking visits: %" PRIu64 "\n", i, stats[i].mark_visits);
fprintf(out, "GC[%d] object visits: %" PRIu64 "\n", i, stats[i].object_visits);
fprintf(out, "GC[%d] objects marked: %" PRIu64 "\n", i, stats[i].objects_marked);
fprintf(out, "GC[%d] objects reachable from roots: %" PRIu64 "\n", i, stats[i].objects_transitively_reachable);
fprintf(out, "GC[%d] objects not reachable from roots: %" PRIu64 "\n", i, stats[i].objects 6D40 _not_transitively_reachable);
fprintf(out, "GC[%d] objects collected: %" PRIu64 "\n", i, stats[i].objects_collected);
}
}
Expand Down
7 changes: 4 additions & 3 deletions Tools/scripts/summarize_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,8 +1118,8 @@ def calc_gc_stats(stats: Stats) -> Rows:
Count(gen["collections"]),
Count(gen["objects collected"]),
Count(gen["object visits"]),
Count(gen["marking visits"]),
Count(gen["objects marked"]),
Count(gen["objects reachable from roots"]),
Count(gen["objects not reachable from roots"]),
)
for (i, gen) in enumerate(gc_stats)
]
Expand All @@ -1129,7 +1129,8 @@ def calc_gc_stats(stats: Stats) -> Rows:
"GC collections and effectiveness",
[
Table(
("Generation:", "Collections:", "Objects collected:", "Object visits:", "Marking visits:", "Objects marked:"),
("Generation:", "Collections:", "Objects collected:", "Object visits:",
"Reachable from roots:", "Not reachable from roots:"),
calc_gc_stats,
)
],
Expand Down
0