Skip to content

Commit bc0d888

Browse files
authored
make pool_live_bytes metric more accurate (#52015)
`pool_live_bytes` was previously lazily updated during the GC, meaning it was only accurate right after a GC. Make this metric accurate if gathered after a GC has happened.
1 parent c377e02 commit bc0d888

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/gc.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,6 @@ int current_sweep_full = 0;
758758
int under_pressure = 0;
759759

760760
// Full collection heuristics
761-
static int64_t pool_live_bytes = 0;
762761
static int64_t live_bytes = 0;
763762
static int64_t promoted_bytes = 0;
764763
static int64_t last_live_bytes = 0; // live_bytes at last collection
@@ -1333,6 +1332,8 @@ STATIC_INLINE jl_value_t *jl_gc_pool_alloc_inner(jl_ptls_t ptls, int pool_offset
13331332
maybe_collect(ptls);
13341333
jl_atomic_store_relaxed(&ptls->gc_num.allocd,
13351334
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + osize);
1335+
jl_atomic_store_relaxed(&ptls->gc_num.pool_live_bytes,
1336+
jl_atomic_load_relaxed(&ptls->gc_num.pool_live_bytes) + osize);
13361337
jl_atomic_store_relaxed(&ptls->gc_num.poolalloc,
13371338
jl_atomic_load_relaxed(&ptls->gc_num.poolalloc) + 1);
13381339
// first try to use the freelist
@@ -1520,7 +1521,8 @@ static void gc_sweep_page(jl_gc_pool_t *p, jl_gc_page_stack_t *allocd, jl_gc_pag
15201521
}
15211522
}
15221523
gc_time_count_page(freedall, pg_skpd);
1523-
jl_atomic_fetch_add((_Atomic(int64_t) *)&pool_live_bytes, GC_PAGE_SZ - GC_PAGE_OFFSET - nfree * osize);
1524+
jl_ptls_t ptls = gc_all_tls_states[pg->thread_n];
1525+
jl_atomic_fetch_add(&ptls->gc_num.pool_live_bytes, GC_PAGE_SZ - GC_PAGE_OFFSET - nfree * osize);
15241526
jl_atomic_fetch_add((_Atomic(int64_t) *)&gc_num.freed, (nfree - old_nfree) * osize);
15251527
}
15261528

@@ -1642,6 +1644,7 @@ static void gc_sweep_pool(void)
16421644
}
16431645
continue;
16441646
}
1647+
jl_atomic_store_relaxed(&ptls2->gc_num.pool_live_bytes, 0);
16451648
for (int i = 0; i < JL_GC_N_POOLS; i++) {
16461649
jl_gc_pool_t *p = &ptls2->heap.norm_pools[i];
16471650
jl_taggedvalue_t *last = p->freelist;
@@ -3182,6 +3185,13 @@ JL_DLLEXPORT int64_t jl_gc_sync_total_bytes(int64_t offset) JL_NOTSAFEPOINT
31823185

31833186
JL_DLLEXPORT int64_t jl_gc_pool_live_bytes(void)
31843187
{
3188+
int64_t pool_live_bytes = 0;
3189+
for (int i = 0; i < gc_n_threads; i++) {
3190+
jl_ptls_t ptls2 = gc_all_tls_states[i];
3191+
if (ptls2 != NULL) {
3192+
pool_live_bytes += jl_atomic_load_relaxed(&ptls2->gc_num.pool_live_bytes);
3193+
}
3194+
}
31853195
return pool_live_bytes;
31863196
}
31873197

@@ -3346,7 +3356,6 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
33463356
promoted_bytes = 0;
33473357
}
33483358
scanned_bytes = 0;
3349-
pool_live_bytes = 0;
33503359
// 6. start sweeping
33513360
uint64_t start_sweep_time = jl_hrtime();
33523361
JL_PROBE_GC_SWEEP_BEGIN(sweep_full);

src/julia_threads.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ typedef struct {
130130

131131
typedef struct {
132132
_Atomic(int64_t) allocd;
133+
_Atomic(int64_t) pool_live_bytes;
133134
_Atomic(uint64_t) malloc;
134135
_Atomic(uint64_t) realloc;
135136
_Atomic(uint64_t) poolalloc;

0 commit comments

Comments
 (0)