Skip to content

Commit

Permalink
add some instrumentation to measure page utilization per size class (#…
Browse files Browse the repository at this point in the history
…52164)

One of the limitations is that it's only accurate right after the GC.
Still might be helpful for observability purposes.
  • Loading branch information
d-netto committed Mar 16, 2024
1 parent 132dd10 commit 6466777
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,38 @@ int jl_gc_classify_pools(size_t sz, int *osize)

// sweep phase

gc_fragmentation_stat_t gc_page_fragmentation_stats[JL_GC_N_POOLS];

extern gc_fragmentation_stat_t gc_page_fragmentation_stats[JL_GC_N_POOLS];

STATIC_INLINE void gc_update_page_fragmentation_data(jl_gc_pagemeta_t *pg) JL_NOTSAFEPOINT
{
#ifdef GC_MEASURE_PAGE_FRAGMENTATION
gc_fragmentation_stat_t *stats = &gc_page_fragmentation_stats[pg->pool_n];
jl_atomic_fetch_add(&stats->n_freed_objs, pg->nfree);
jl_atomic_fetch_add(&stats->n_pages_allocd, 1);
#endif
}

STATIC_INLINE void gc_dump_page_utilization_data(void) JL_NOTSAFEPOINT
{
#ifdef GC_MEASURE_PAGE_FRAGMENTATION
for (int i = 0; i < JL_GC_N_POOLS; i++) {
gc_fragmentation_stat_t *stats = &gc_page_fragmentation_stats[i];
double utilization = 1.0;
size_t n_freed_objs = jl_atomic_load_relaxed(&stats->n_freed_objs);
size_t n_pages_allocd = jl_atomic_load_relaxed(&stats->n_pages_allocd);
if (n_pages_allocd != 0) {
utilization -= ((double)n_freed_objs * (double)jl_gc_sizeclasses[i]) / (double)n_pages_allocd / (double)GC_PAGE_SZ;
}
jl_safe_printf("Size class %d: %.2f%% utilization\n", jl_gc_sizeclasses[i], utilization * 100.0);
jl_atomic_store_relaxed(&stats->n_freed_objs, 0);
jl_atomic_store_relaxed(&stats->n_pages_allocd, 0);
}
jl_safe_printf("-----------------------------------------\n");
#endif
}

int64_t lazy_freed_pages = 0;

// Returns pointer to terminal pointer of list rooted at *pfl.
Expand Down Expand Up @@ -1697,6 +1729,7 @@ static jl_taggedvalue_t **sweep_page(jl_gc_pool_t *p, jl_gc_pagemeta_t *pg, jl_t
nfree = pg->nfree;

done:
gc_update_page_fragmentation_data(pg);
gc_time_count_page(freedall, pg_skpd);
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);
Expand Down Expand Up @@ -1876,6 +1909,7 @@ static void gc_sweep_pool(int sweep_full)
}
}

gc_dump_page_utilization_data();
gc_time_pool_end(sweep_full);
}

Expand Down
11 changes: 8 additions & 3 deletions src/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,14 @@ typedef struct {
// structure of this representation allows us to partially unroll and optimize
// various conditions at each level.

// The following constants define the branching factors at each level.
// The constants and GC_PAGE_LG2 must therefore sum to sizeof(void*).
// They should all be multiples of 32 (sizeof(uint32_t)) except that REGION2_PG_COUNT may also be 1.
// data structures for tracking fragmentation in the pool allocator
// #define GC_MEASURE_PAGE_FRAGMENTATION

typedef struct {
_Atomic(size_t) n_freed_objs;
_Atomic(size_t) n_pages_allocd;
} gc_fragmentation_stat_t;

#ifdef _P64
#define REGION0_PG_COUNT (1 << 16)
#define REGION1_PG_COUNT (1 << 16)
Expand Down

0 comments on commit 6466777

Please sign in to comment.