Skip to content

Commit 1fdf00e

Browse files
committed
Add stats for objects marked
1 parent 2ec8d8a commit 1fdf00e

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

Include/cpython/pystats.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ typedef struct _gc_stats {
9999
uint64_t collections;
100100
uint64_t object_visits;
101101
uint64_t objects_collected;
102+
uint64_t objects_marked;
102103
} GCStats;
103104

104105
typedef struct _uop_stats {

Python/gc.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,21 +1445,23 @@ gc_mark(PyThreadState *tstate, struct gc_collection_stats *stats)
14451445
gc_list_init(&reachable);
14461446
// Move all reachable objects into visited space.
14471447
PyGC_Head *gc = AS_GC(tstate->interp->sysdict);
1448+
Py_ssize_t objects_marked = 0;
14481449
if (gc_old_space(gc) != gcstate->visited_space) {
14491450
gc_flip_old_space(gc);
14501451
gc_list_move(gc, &reachable);
1452+
objects_marked++;
14511453
}
14521454
gc = AS_GC(tstate->interp->builtins);
14531455
if (gc_old_space(gc) != gcstate->visited_space) {
14541456
gc_flip_old_space(gc);
14551457
gc_list_move(gc, &reachable);
1458+
objects_marked++;
14561459
}
14571460
// Move all objects on stacks to reachable
14581461
_PyRuntimeState *runtime = &_PyRuntime;
14591462
HEAD_LOCK(runtime);
14601463
PyThreadState* ts = PyInterpreterState_ThreadHead(tstate->interp);
14611464
HEAD_UNLOCK(runtime);
1462-
Py_ssize_t objects_marked = 0;
14631465
while (ts) {
14641466
_PyInterpreterFrame *frame = ts->current_frame;
14651467
while (frame) {
@@ -1503,8 +1505,14 @@ gc_mark(PyThreadState *tstate, struct gc_collection_stats *stats)
15031505
visit_add_to_container,
15041506
&arg);
15051507
}
1508+
objects_marked += arg.size;
15061509
validate_old(gcstate);
15071510
gcstate->work_to_do -= objects_marked;
1511+
#ifdef Py_STATS
1512+
if (_Py_stats) {
1513+
GC_STAT_ADD(1, objects_marked, objects_marked);
1514+
}
1515+
#endif
15081516
gcstate->phase = GC_PHASE_COLLECT;
15091517
}
15101518

Python/specialize.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ print_gc_stats(FILE *out, GCStats *stats)
230230
for (int i = 0; i < NUM_GENERATIONS; i++) {
231231
fprintf(out, "GC[%d] collections: %" PRIu64 "\n", i, stats[i].collections);
232232
fprintf(out, "GC[%d] object visits: %" PRIu64 "\n", i, stats[i].object_visits);
233+
fprintf(out, "GC[%d] objects marked: %" PRIu64 "\n", i, stats[i].objects_marked);
233234
fprintf(out, "GC[%d] objects collected: %" PRIu64 "\n", i, stats[i].objects_collected);
234235
}
235236
}

Tools/scripts/summarize_stats.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ def calc_gc_stats(stats: Stats) -> Rows:
11181118
Count(gen["collections"]),
11191119
Count(gen["objects collected"]),
11201120
Count(gen["object visits"]),
1121+
Count(gen["objects marked"]),
11211122
)
11221123
for (i, gen) in enumerate(gc_stats)
11231124
]
@@ -1127,7 +1128,7 @@ def calc_gc_stats(stats: Stats) -> Rows:
11271128
"GC collections and effectiveness",
11281129
[
11291130
Table(
1130-
("Generation:", "Collections:", "Objects collected:", "Object visits:"),
1131+
("Generation:", "Collections:", "Objects collected:", "Object visits:", "Objects marked:"),
11311132
calc_gc_stats,
11321133
)
11331134
],

0 commit comments

Comments
 (0)