Skip to content

Commit

Permalink
make pool_live_bytes metric more accurate (JuliaLang#52015)
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
d-netto authored and RAI CI (GitHub Action Automation) committed Jan 31, 2024
1 parent 4236b0b commit 51a3d47
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,6 @@ int current_sweep_full = 0;
int under_pressure = 0;

// Full collection heuristics
static int64_t pool_live_bytes = 0;
static int64_t live_bytes = 0;
static int64_t promoted_bytes = 0;
static int64_t last_full_live = 0; // live_bytes after last full collection
Expand Down Expand Up @@ -1306,6 +1305,8 @@ STATIC_INLINE jl_value_t *jl_gc_pool_alloc_inner(jl_ptls_t ptls, int pool_offset
maybe_collect(ptls);
jl_atomic_store_relaxed(&ptls->gc_num.allocd,
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + osize);
jl_atomic_store_relaxed(&ptls->gc_num.pool_live_bytes,
jl_atomic_load_relaxed(&ptls->gc_num.pool_live_bytes) + osize);
jl_atomic_store_relaxed(&ptls->gc_num.poolalloc,
jl_atomic_load_relaxed(&ptls->gc_num.poolalloc) + 1);
// first try to use the freelist
Expand Down Expand Up @@ -1492,7 +1493,8 @@ static void gc_sweep_page(jl_gc_pool_t *p, jl_gc_page_stack_t *allocd, jl_gc_pag
}
}
gc_time_count_page(freedall, pg_skpd);
jl_atomic_fetch_add((_Atomic(int64_t) *)&pool_live_bytes, GC_PAGE_SZ - GC_PAGE_OFFSET - nfree * osize);
jl_ptls_t ptls = gc_all_tls_states[pg->thread_n];
jl_atomic_fetch_add(&ptls->gc_num.pool_live_bytes, GC_PAGE_SZ - GC_PAGE_OFFSET - nfree * osize);
jl_atomic_fetch_add((_Atomic(int64_t) *)&gc_num.freed, (nfree - old_nfree) * osize);
}

Expand Down Expand Up @@ -1614,6 +1616,7 @@ static void gc_sweep_pool(void)
}
continue;
}
jl_atomic_store_relaxed(&ptls2->gc_num.pool_live_bytes, 0);
for (int i = 0; i < JL_GC_N_POOLS; i++) {
jl_gc_pool_t *p = &ptls2->heap.norm_pools[i];
jl_taggedvalue_t *last = p->freelist;
Expand Down Expand Up @@ -3265,6 +3268,13 @@ JL_DLLEXPORT int64_t jl_gc_sync_total_bytes(int64_t offset) JL_NOTSAFEPOINT

JL_DLLEXPORT int64_t jl_gc_pool_live_bytes(void)
{
int64_t pool_live_bytes = 0;
for (int i = 0; i < gc_n_threads; i++) {
jl_ptls_t ptls2 = gc_all_tls_states[i];
if (ptls2 != NULL) {
pool_live_bytes += jl_atomic_load_relaxed(&ptls2->gc_num.pool_live_bytes);
}
}
return pool_live_bytes;
}

Expand Down Expand Up @@ -3470,7 +3480,6 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
promoted_bytes = 0;
}
scanned_bytes = 0;
pool_live_bytes = 0;
// 6. start sweeping
uint64_t start_sweep_time = jl_hrtime();
JL_PROBE_GC_SWEEP_BEGIN(sweep_full);
Expand Down
1 change: 1 addition & 0 deletions src/julia_threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ typedef struct {

typedef struct {
_Atomic(int64_t) allocd;
_Atomic(int64_t) pool_live_bytes;
_Atomic(int64_t) freed;
_Atomic(uint64_t) malloc;
_Atomic(uint64_t) realloc;
Expand Down

0 comments on commit 51a3d47

Please sign in to comment.