From 726ef0e9142236d2c04f941d35178ab183e0d713 Mon Sep 17 00:00:00 2001 From: Diogo Netto <61364108+d-netto@users.noreply.github.com> Date: Wed, 6 Dec 2023 09:57:44 -0300 Subject: [PATCH] functionality to expose page utilization at the julia level (#113) --- base/timing.jl | 7 +++++++ src/gc.c | 8 ++------ src/gc.h | 3 --- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/base/timing.jl b/base/timing.jl index aeca790bce7e9..4b14161aa89d2 100644 --- a/base/timing.jl +++ b/base/timing.jl @@ -98,6 +98,13 @@ function gc_live_bytes() Int(ccall(:jl_gc_live_bytes, Int64, ())) + num.allocd + num.deferred_alloc end +# must be kept in sync with the value from `src/julia_threads.h`` +const JL_GC_N_MAX_POOLS = 51 +function gc_page_utilization_data() + page_utilization_raw = cglobal(:jl_gc_page_utilization_stats, Float64) + return Base.unsafe_wrap(Array, page_utilization_raw, JL_GC_N_MAX_POOLS, own=false) +end + """ Base.jit_total_bytes() diff --git a/src/gc.c b/src/gc.c index 4003e61accf6a..832e06adfded1 100644 --- a/src/gc.c +++ b/src/gc.c @@ -1391,21 +1391,19 @@ int jl_gc_classify_pools(size_t sz, int *osize) // sweep phase gc_fragmentation_stat_t gc_page_fragmentation_stats[JL_GC_N_POOLS]; +JL_DLLEXPORT double jl_gc_page_utilization_stats[JL_GC_N_MAX_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; @@ -1414,12 +1412,10 @@ STATIC_INLINE void gc_dump_page_utilization_data(void) JL_NOTSAFEPOINT 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_gc_page_utilization_stats[i] = utilization; 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 buffered_pages = 0; diff --git a/src/gc.h b/src/gc.h index e22cfe4c13e90..a6ad9f41dabf0 100644 --- a/src/gc.h +++ b/src/gc.h @@ -223,9 +223,6 @@ STATIC_INLINE jl_gc_pagemeta_t *pop_lf_back(jl_gc_page_stack_t *pool) JL_NOTSAFE } } -// 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;