Skip to content

Commit c558563

Browse files
committed
make pool_live_bytes metric more accurate (JuliaLang#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 74cbf65 commit c558563

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
@@ -751,7 +751,6 @@ int current_sweep_full = 0;
751751
int under_pressure = 0;
752752

753753
// Full collection heuristics
754-
static int64_t pool_live_bytes = 0;
755754
static int64_t live_bytes = 0;
756755
static int64_t promoted_bytes = 0;
757756
static int64_t last_full_live = 0; // live_bytes after last full collection
@@ -1304,6 +1303,8 @@ STATIC_INLINE jl_value_t *jl_gc_pool_alloc_inner(jl_ptls_t ptls, int pool_offset
13041303
maybe_collect(ptls);
13051304
jl_atomic_store_relaxed(&ptls->gc_num.allocd,
13061305
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + osize);
1306+
jl_atomic_store_relaxed(&ptls->gc_num.pool_live_bytes,
1307+
jl_atomic_load_relaxed(&ptls->gc_num.pool_live_bytes) + osize);
13071308
jl_atomic_store_relaxed(&ptls->gc_num.poolalloc,
13081309
jl_atomic_load_relaxed(&ptls->gc_num.poolalloc) + 1);
13091310
// first try to use the freelist
@@ -1490,7 +1491,8 @@ static void gc_sweep_page(jl_gc_pool_t *p, jl_gc_page_stack_t *allocd, jl_gc_pag
14901491
}
14911492
}
14921493
gc_time_count_page(freedall, pg_skpd);
1493-
jl_atomic_fetch_add((_Atomic(int64_t) *)&pool_live_bytes, GC_PAGE_SZ - GC_PAGE_OFFSET - nfree * osize);
1494+
jl_ptls_t ptls = gc_all_tls_states[pg->thread_n];
1495+
jl_atomic_fetch_add(&ptls->gc_num.pool_live_bytes, GC_PAGE_SZ - GC_PAGE_OFFSET - nfree * osize);
14941496
jl_atomic_fetch_add((_Atomic(int64_t) *)&gc_num.freed, (nfree - old_nfree) * osize);
14951497
}
14961498

@@ -1612,6 +1614,7 @@ static void gc_sweep_pool(void)
16121614
}
16131615
continue;
16141616
}
1617+
jl_atomic_store_relaxed(&ptls2->gc_num.pool_live_bytes, 0);
16151618
for (int i = 0; i < JL_GC_N_POOLS; i++) {
16161619
jl_gc_pool_t *p = &ptls2->heap.norm_pools[i];
16171620
jl_taggedvalue_t *last = p->freelist;
@@ -3161,6 +3164,13 @@ JL_DLLEXPORT int64_t jl_gc_sync_total_bytes(int64_t offset) JL_NOTSAFEPOINT
31613164

31623165
JL_DLLEXPORT int64_t jl_gc_pool_live_bytes(void)
31633166
{
3167+
int64_t pool_live_bytes = 0;
3168+
for (int i = 0; i < gc_n_threads; i++) {
3169+
jl_ptls_t ptls2 = gc_all_tls_states[i];
3170+
if (ptls2 != NULL) {
3171+
pool_live_bytes += jl_atomic_load_relaxed(&ptls2->gc_num.pool_live_bytes);
3172+
}
3173+
}
31643174
return pool_live_bytes;
31653175
}
31663176

@@ -3366,7 +3376,6 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
33663376
promoted_bytes = 0;
33673377
}
33683378
scanned_bytes = 0;
3369-
pool_live_bytes = 0;
33703379
// 6. start sweeping
33713380
uint64_t start_sweep_time = jl_hrtime();
33723381
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(int64_t) freed;
134135
_Atomic(uint64_t) malloc;
135136
_Atomic(uint64_t) realloc;

0 commit comments

Comments
 (0)