Skip to content

Commit 8bd7606

Browse files
committed
Add stats for visits during marking
1 parent 5e813c5 commit 8bd7606

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

Include/cpython/pystats.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ typedef struct _object_stats {
9898
typedef struct _gc_stats {
9999
uint64_t collections;
100100
uint64_t object_visits;
101+
uint64_t mark_visits;
101102
uint64_t objects_collected;
102103
uint64_t objects_marked;
103104
} GCStats;

Python/gc.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,15 +1360,23 @@ IS_IN_VISITED(PyGC_Head *gc, int visited_space)
13601360
struct container_and_flag {
13611361
PyGC_Head *container;
13621362
int visited_space;
1363+
int mark;
13631364
uintptr_t size;
13641365
};
13651366

13661367
/* A traversal callback for adding to container) */
13671368
static int
13681369
visit_add_to_container(PyObject *op, void *arg)
13691370
{
1370-
OBJECT_STAT_INC(object_visits);
13711371
struct container_and_flag *cf = (struct container_and_flag *)arg;
1372+
#ifdef Py_STATS
1373+
if (cf->mark) {
1374+
GC_STAT_ADD(1, mark_visits, 1);
1375+
}
1376+
else {
1377+
OBJECT_STAT_INC(object_visits);
1378+
}
1379+
#endif
13721380
int visited = cf->visited_space;
13731381
assert(visited == get_gc_state()->visited_space);
13741382
if (!_Py_IsImmortal(op) && _PyObject_IS_GC(op)) {
@@ -1390,6 +1398,7 @@ expand_region_transitively_reachable(PyGC_Head *container, PyGC_Head *gc, GCStat
13901398
struct container_and_flag arg = {
13911399
.container = container,
13921400
.visited_space = gcstate->visited_space,
1401+
.mark = 0,
13931402
.size = 0
13941403
};
13951404
assert(GC_NEXT(gc) == container);
@@ -1493,6 +1502,7 @@ gc_mark(PyThreadState *tstate, struct gc_collection_stats *stats)
14931502
struct container_and_flag arg = {
14941503
.container = &reachable,
14951504
.visited_space = gcstate->visited_space,
1505+
.mark = 1,
14961506
.size = 0
14971507
};
14981508
while (!gc_list_is_empty(&reachable)) {

Python/specialize.c

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

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["marking visits"]),
11211122
Count(gen["objects marked"]),
11221123
)
11231124
for (i, gen) in enumerate(gc_stats)
@@ -1128,7 +1129,7 @@ def calc_gc_stats(stats: Stats) -> Rows:
11281129
"GC collections and effectiveness",
11291130
[
11301131
Table(
1131-
("Generation:", "Collections:", "Objects collected:", "Object visits:", "Objects marked:"),
1132+
("Generation:", "Collections:", "Objects collected:", "Object visits:", "Marking visits:", "Objects marked:"),
11321133
calc_gc_stats,
11331134
)
11341135
],

0 commit comments

Comments
 (0)