Skip to content

Commit df266ef

Browse files
d-nettoRAI CI (GitHub Action Automation)
authored andcommitted
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 ae82390 commit df266ef

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
@@ -753,7 +753,6 @@ int current_sweep_full = 0;
753753
int under_pressure = 0;
754754

755755
// Full collection heuristics
756-
static int64_t pool_live_bytes = 0;
757756
static int64_t live_bytes = 0;
758757
static int64_t promoted_bytes = 0;
759758
static int64_t last_full_live = 0; // live_bytes after last full collection
@@ -1306,6 +1305,8 @@ STATIC_INLINE jl_value_t *jl_gc_pool_alloc_inner(jl_ptls_t ptls, int pool_offset
13061305
maybe_collect(ptls);
13071306
jl_atomic_store_relaxed(&ptls->gc_num.allocd,
13081307
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + osize);
1308+
jl_atomic_store_relaxed(&ptls->gc_num.pool_live_bytes,
1309+
jl_atomic_load_relaxed(&ptls->gc_num.pool_live_bytes) + osize);
13091310
jl_atomic_store_relaxed(&ptls->gc_num.poolalloc,
13101311
jl_atomic_load_relaxed(&ptls->gc_num.poolalloc) + 1);
13111312
// first try to use the freelist
@@ -1492,7 +1493,8 @@ static void gc_sweep_page(jl_gc_pool_t *p, jl_gc_page_stack_t *allocd, jl_gc_pag
14921493
}
14931494
}
14941495
gc_time_count_page(freedall, pg_skpd);
1495-
jl_atomic_fetch_add((_Atomic(int64_t) *)&pool_live_bytes, GC_PAGE_SZ - GC_PAGE_OFFSET - nfree * osize);
1496+
jl_ptls_t ptls = gc_all_tls_states[pg->thread_n];
1497+
jl_atomic_fetch_add(&ptls->gc_num.pool_live_bytes, GC_PAGE_SZ - GC_PAGE_OFFSET - nfree * osize);
14961498
jl_atomic_fetch_add((_Atomic(int64_t) *)&gc_num.freed, (nfree - old_nfree) * osize);
14971499
}
14981500

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

32663269
JL_DLLEXPORT int64_t jl_gc_pool_live_bytes(void)
32673270
{
3271+
int64_t pool_live_bytes = 0;
3272+
for (int i = 0; i < gc_n_threads; i++) {
3273+
jl_ptls_t ptls2 = gc_all_tls_states[i];
3274+
if (ptls2 != NULL) {
3275+
pool_live_bytes += jl_atomic_load_relaxed(&ptls2->gc_num.pool_live_bytes);
3276+
}
3277+
}
32683278
return pool_live_bytes;
32693279
}
32703280

@@ -3470,7 +3480,6 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
34703480
promoted_bytes = 0;
34713481
}
34723482
scanned_bytes = 0;
3473-
pool_live_bytes = 0;
34743483
// 6. start sweeping
34753484
uint64_t start_sweep_time = jl_hrtime();
34763485
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)