Skip to content

Commit 51214a7

Browse files
authored
Make pool allocator stats from gc_page_fragmentation_stats more visible for Julia user code (#58659)
- Mark `gc_page_fragmentation_stats` DLL_EXPORT so that we can load it through `cglobal`. - Avoid resetting data from `gc_page_fragmentation_stats` after sweeping to ensure it reflects the page stats from the last GC. - Some minor/cosmetic function renaming.
1 parent dece143 commit 51214a7

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/gc-stock.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -818,29 +818,35 @@ int jl_gc_classify_pools(size_t sz, int *osize)
818818

819819
// sweep phase
820820

821-
gc_fragmentation_stat_t gc_page_fragmentation_stats[JL_GC_N_POOLS];
821+
JL_DLLEXPORT gc_fragmentation_stat_t jl_gc_page_fragmentation_stats[JL_GC_N_POOLS];
822822
JL_DLLEXPORT double jl_gc_page_utilization_stats[JL_GC_N_MAX_POOLS];
823823

824-
STATIC_INLINE void gc_update_page_fragmentation_data(jl_gc_pagemeta_t *pg) JL_NOTSAFEPOINT
824+
STATIC_INLINE void gc_update_fragmentation_data_for_size_class(jl_gc_pagemeta_t *pg) JL_NOTSAFEPOINT
825825
{
826-
gc_fragmentation_stat_t *stats = &gc_page_fragmentation_stats[pg->pool_n];
826+
gc_fragmentation_stat_t *stats = &jl_gc_page_fragmentation_stats[pg->pool_n];
827827
jl_atomic_fetch_add_relaxed(&stats->n_freed_objs, pg->nfree);
828828
jl_atomic_fetch_add_relaxed(&stats->n_pages_allocd, 1);
829829
}
830830

831-
STATIC_INLINE void gc_dump_page_utilization_data(void) JL_NOTSAFEPOINT
831+
STATIC_INLINE void gc_reset_fragmentation_data_for_size_classes(void) JL_NOTSAFEPOINT
832832
{
833833
for (int i = 0; i < JL_GC_N_POOLS; i++) {
834-
gc_fragmentation_stat_t *stats = &gc_page_fragmentation_stats[i];
834+
jl_atomic_store_relaxed(&jl_gc_page_fragmentation_stats[i].n_freed_objs, 0);
835+
jl_atomic_store_relaxed(&jl_gc_page_fragmentation_stats[i].n_pages_allocd, 0);
836+
}
837+
}
838+
839+
STATIC_INLINE void gc_compute_utilization_data_for_size_classes(void) JL_NOTSAFEPOINT
840+
{
841+
for (int i = 0; i < JL_GC_N_POOLS; i++) {
842+
gc_fragmentation_stat_t *stats = &jl_gc_page_fragmentation_stats[i];
835843
double utilization = 1.0;
836844
size_t n_freed_objs = jl_atomic_load_relaxed(&stats->n_freed_objs);
837845
size_t n_pages_allocd = jl_atomic_load_relaxed(&stats->n_pages_allocd);
838846
if (n_pages_allocd != 0) {
839847
utilization -= ((double)n_freed_objs * (double)jl_gc_sizeclasses[i]) / (double)n_pages_allocd / (double)GC_PAGE_SZ;
840848
}
841849
jl_gc_page_utilization_stats[i] = utilization;
842-
jl_atomic_store_relaxed(&stats->n_freed_objs, 0);
843-
jl_atomic_store_relaxed(&stats->n_pages_allocd, 0);
844850
}
845851
}
846852

@@ -948,7 +954,7 @@ static void gc_sweep_page(gc_page_profiler_serializer_t *s, jl_gc_pool_t *p, jl_
948954

949955
done:
950956
if (re_use_page) {
951-
gc_update_page_fragmentation_data(pg);
957+
gc_update_fragmentation_data_for_size_class(pg);
952958
push_lf_back(allocd, pg);
953959
}
954960
else {
@@ -1388,6 +1394,7 @@ static void gc_sweep_pool(void)
13881394
// the actual sweeping
13891395
jl_gc_padded_page_stack_t *new_gc_allocd_scratch = (jl_gc_padded_page_stack_t *) calloc_s(n_threads * sizeof(jl_gc_padded_page_stack_t));
13901396
jl_ptls_t ptls = jl_current_task->ptls;
1397+
gc_reset_fragmentation_data_for_size_classes();
13911398
gc_sweep_wake_all_pages(ptls, new_gc_allocd_scratch);
13921399
gc_sweep_pool_parallel(ptls);
13931400
gc_sweep_wait_for_all_pages();
@@ -1455,7 +1462,7 @@ static void gc_sweep_pool(void)
14551462
#else
14561463
gc_free_pages();
14571464
#endif
1458-
gc_dump_page_utilization_data();
1465+
gc_compute_utilization_data_for_size_classes();
14591466
gc_time_pool_end(current_sweep_full);
14601467
}
14611468

0 commit comments

Comments
 (0)