Skip to content

Commit 2287687

Browse files
committed
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 cddef7e commit 2287687

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/gc.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,6 @@ int under_pressure = 0;
872872
#define inc_sat(v,s) v = (v) >= s ? s : (v)+1
873873

874874
// Full collection heuristics
875-
static int64_t pool_live_bytes = 0;
876875
static int64_t live_bytes = 0;
877876
static int64_t promoted_bytes = 0;
878877
static int64_t last_live_bytes = 0; // live_bytes at last collection
@@ -1503,6 +1502,8 @@ static inline jl_value_t *jl_gc_pool_alloc_inner(jl_ptls_t ptls, int pool_offset
15031502
maybe_collect(ptls);
15041503
jl_atomic_store_relaxed(&ptls->gc_num.allocd,
15051504
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + osize);
1505+
jl_atomic_store_relaxed(&ptls->gc_num.pool_live_bytes,
1506+
jl_atomic_load_relaxed(&ptls->gc_num.pool_live_bytes) + osize);
15061507
jl_atomic_store_relaxed(&ptls->gc_num.poolalloc,
15071508
jl_atomic_load_relaxed(&ptls->gc_num.poolalloc) + 1);
15081509
// first try to use the freelist
@@ -1691,8 +1692,9 @@ static jl_taggedvalue_t **sweep_page(jl_gc_pool_t *p, jl_gc_pagemeta_t *pg, jl_t
16911692

16921693
done:
16931694
gc_time_count_page(freedall, pg_skpd);
1694-
gc_num.freed += (nfree - old_nfree) * osize;
1695-
pool_live_bytes += GC_PAGE_SZ - GC_PAGE_OFFSET - nfree * osize;
1695+
jl_ptls_t ptls = gc_all_tls_states[pg->thread_n];
1696+
jl_atomic_fetch_add(&ptls->gc_num.pool_live_bytes, GC_PAGE_SZ - GC_PAGE_OFFSET - nfree * osize);
1697+
jl_atomic_fetch_add((_Atomic(int64_t) *)&gc_num.freed, (nfree - old_nfree) * osize);
16961698
return pfl;
16971699
}
16981700

@@ -1831,6 +1833,7 @@ static void gc_sweep_pool(int sweep_full)
18311833
}
18321834
continue;
18331835
}
1836+
jl_atomic_store_relaxed(&ptls2->gc_num.pool_live_bytes, 0);
18341837
for (int i = 0; i < JL_GC_N_POOLS; i++) {
18351838
jl_gc_pool_t *p = &ptls2->heap.norm_pools[i];
18361839
jl_taggedvalue_t *last = p->freelist;
@@ -3339,6 +3342,13 @@ JL_DLLEXPORT int64_t jl_gc_sync_total_bytes(int64_t offset) JL_NOTSAFEPOINT
33393342

33403343
JL_DLLEXPORT int64_t jl_gc_pool_live_bytes(void)
33413344
{
3345+
int64_t pool_live_bytes = 0;
3346+
for (int i = 0; i < gc_n_threads; i++) {
3347+
jl_ptls_t ptls2 = gc_all_tls_states[i];
3348+
if (ptls2 != NULL) {
3349+
pool_live_bytes += jl_atomic_load_relaxed(&ptls2->gc_num.pool_live_bytes);
3350+
}
3351+
}
33423352
return pool_live_bytes;
33433353
}
33443354

@@ -3594,7 +3604,6 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
35943604
promoted_bytes = 0;
35953605
}
35963606
scanned_bytes = 0;
3597-
pool_live_bytes = 0;
35983607
// 6. start sweeping
35993608
uint64_t start_sweep_time = jl_hrtime();
36003609
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
@@ -127,6 +127,7 @@ typedef struct {
127127

128128
typedef struct {
129129
_Atomic(int64_t) allocd;
130+
_Atomic(int64_t) pool_live_bytes;
130131
_Atomic(uint64_t) malloc;
131132
_Atomic(uint64_t) realloc;
132133
_Atomic(uint64_t) poolalloc;

0 commit comments

Comments
 (0)