Skip to content

Commit 01d6e39

Browse files
committed
Apply review suggestions.
1 parent 2f42cd5 commit 01d6e39

File tree

3 files changed

+26
-56
lines changed

3 files changed

+26
-56
lines changed

src/gc-debug.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,19 +1229,14 @@ void _report_gc_finished(uint64_t pause, uint64_t freed, int full, int recollect
12291229
recollect ? "recollect" : ""
12301230
);
12311231

1232-
jl_safe_printf("Heap stats: bytes_mapped %.2f MB, bytes_allocd %.2f MB\nbytes_freed %.2f MB, bytes_mallocd %.1f, malloc_bytes_freed %.2f MB\npages_perm_allocd %zu, heap_size %.2f MB, heap_target %.2f MB, live_bytes %.2f MB\n",
1232+
jl_safe_printf("Heap stats: bytes_mapped %.2f MB, bytes_resident %.2f MB, heap_size %.2f MB, heap_target %.2f MB, live_bytes %.2f MB\n, Fragmentation %.3f",
12331233
jl_atomic_load_relaxed(&gc_heap_stats.bytes_mapped)/(double)(1<<20),
1234-
jl_atomic_load_relaxed(&gc_heap_stats.bytes_allocd)/(double)(1<<20),
1235-
jl_atomic_load_relaxed(&gc_heap_stats.bytes_freed)/(double)(1<<20),
1236-
jl_atomic_load_relaxed(&gc_heap_stats.bytes_mallocd)/(double)(1<<20),
1237-
jl_atomic_load_relaxed(&gc_heap_stats.malloc_bytes_freed)/(double)(1<<20),
1238-
jl_atomic_load_relaxed(&gc_heap_stats.pages_perm_allocd),
1234+
jl_atomic_load_relaxed(&gc_heap_stats.bytes_resident)/(double)(1<<20),
12391235
jl_atomic_load_relaxed(&gc_heap_stats.heap_size)/(double)(1<<20),
12401236
jl_atomic_load_relaxed(&gc_heap_stats.heap_target)/(double)(1<<20),
1241-
live_bytes/(double)(1<<20)
1237+
live_bytes/(double)(1<<20),
1238+
(double)live_bytes/(double)jl_atomic_load_relaxed(&gc_heap_stats.heap_size)
12421239
);
1243-
double bytes_mapped = (jl_atomic_load_relaxed(&gc_heap_stats.bytes_resident) + jl_atomic_load_relaxed(&gc_heap_stats.bytes_mallocd) - jl_atomic_load_relaxed(&gc_heap_stats.malloc_bytes_freed))/(double)(1<<20);
1244-
jl_safe_printf("Fragmentation %f, mapped_bytes %.2f MB\n", (double)live_bytes/(double)jl_atomic_load_relaxed(&gc_heap_stats.heap_size), bytes_mapped);
12451240
// Should fragmentation use bytes_resident instead of heap_size?
12461241
}
12471242

src/gc.c

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,6 @@ STATIC_INLINE jl_value_t *jl_gc_big_alloc_inner(jl_ptls_t ptls, size_t sz)
10101010
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + allocsz);
10111011
jl_atomic_store_relaxed(&ptls->gc_num.bigalloc,
10121012
jl_atomic_load_relaxed(&ptls->gc_num.bigalloc) + 1);
1013-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_mallocd, allocsz);
10141013
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, allocsz);
10151014
#ifdef MEMDEBUG
10161015
memset(v, 0xee, allocsz);
@@ -1057,8 +1056,6 @@ static bigval_t **sweep_big_list(int sweep_full, bigval_t **pv) JL_NOTSAFEPOINT
10571056
if (nxt)
10581057
nxt->prev = pv;
10591058
gc_num.freed += v->sz&~3;
1060-
jl_atomic_store_relaxed(&gc_heap_stats.malloc_bytes_freed,
1061-
jl_atomic_load_relaxed(&gc_heap_stats.malloc_bytes_freed) + (v->sz&~3));
10621059
jl_atomic_store_relaxed(&gc_heap_stats.heap_size,
10631060
jl_atomic_load_relaxed(&gc_heap_stats.heap_size) - (v->sz&~3));
10641061
#ifdef MEMDEBUG
@@ -1120,7 +1117,6 @@ void jl_gc_count_allocd(size_t sz) JL_NOTSAFEPOINT
11201117
jl_ptls_t ptls = jl_current_task->ptls;
11211118
jl_atomic_store_relaxed(&ptls->gc_num.allocd,
11221119
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + sz);
1123-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_mallocd, sz);
11241120
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, sz);
11251121
}
11261122

@@ -1196,12 +1192,8 @@ static void jl_gc_free_array(jl_array_t *a) JL_NOTSAFEPOINT
11961192
jl_free_aligned(d);
11971193
else
11981194
free(d);
1199-
jl_atomic_store_relaxed(&gc_heap_stats.malloc_bytes_freed,
1200-
jl_atomic_load_relaxed(&gc_heap_stats.malloc_bytes_freed) + jl_array_nbytes(a));
12011195
jl_atomic_store_relaxed(&gc_heap_stats.heap_size,
12021196
jl_atomic_load_relaxed(&gc_heap_stats.heap_size) - jl_array_nbytes(a));
1203-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.malloc_bytes_freed, jl_array_nbytes(a));
1204-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -jl_array_nbytes(a));
12051197
gc_num.freed += jl_array_nbytes(a);
12061198
gc_num.freecall++;
12071199
}
@@ -1273,7 +1265,6 @@ static NOINLINE jl_taggedvalue_t *gc_add_page(jl_gc_pool_t *p) JL_NOTSAFEPOINT
12731265
set_page_metadata(pg);
12741266
push_page_metadata_back(&ptls->page_metadata_allocd, pg);
12751267
jl_taggedvalue_t *fl = gc_reset_page(ptls, p, pg);
1276-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_allocd, GC_PAGE_SZ);
12771268
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, GC_PAGE_SZ);
12781269
p->newpages = fl;
12791270
return fl;
@@ -1468,11 +1459,9 @@ static jl_taggedvalue_t **gc_sweep_page(jl_gc_pool_t *p, jl_gc_pagemeta_t **allo
14681459
}
14691460
else if (freed_lazily) {
14701461
push_page_metadata_back(lazily_freed, pg);
1471-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_freed, GC_PAGE_SZ);
14721462
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -GC_PAGE_SZ);
14731463
}
14741464
else {
1475-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_freed, GC_PAGE_SZ);
14761465
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -GC_PAGE_SZ);
14771466
#ifdef _P64 // only enable concurrent sweeping on 64bit
14781467
if (jl_n_sweepthreads == 0) {
@@ -3279,29 +3268,30 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
32793268
}
32803269

32813270
size_t heap_size = jl_atomic_load_relaxed(&gc_heap_stats.heap_size);
3282-
uint64_t alloc_diff = before_free_heap_size - old_heap_size;
3283-
uint64_t freed_diff = before_free_heap_size - heap_size;
3284-
3285-
double alloc_smooth_factor = 0.95;
3286-
double collec_smooth_factor = 0.5;
3287-
double tuning_factor = 0.03;
3288-
double alloc_mem = jl_gc_smooth(old_alloc_diff, alloc_diff, alloc_smooth_factor);
3289-
double alloc_time = jl_gc_smooth(old_mut_time, mutator_time, alloc_smooth_factor);
3290-
double gc_mem = jl_gc_smooth(old_freed_diff, freed_diff, collec_smooth_factor);
3291-
double gc_time = jl_gc_smooth(old_pause_time, pause, collec_smooth_factor);
3292-
old_alloc_diff = alloc_diff;
3293-
old_mut_time = mutator_time;
3294-
old_freed_diff = freed_diff;
3295-
old_pause_time = pause;
3296-
old_heap_size = heap_size;
3271+
double target_allocs = 0.0;
32973272
double min_interval = default_collect_interval;
3298-
double target_allocs;
3299-
if (alloc_mem != 0 && alloc_time != 0 && gc_mem != 0 && gc_time != 0) {
3300-
double alloc_rate = alloc_mem/alloc_time;
3301-
double gc_rate = gc_mem/gc_time;
3302-
target_allocs = sqrt(((double)heap_size/min_interval * alloc_rate)/(gc_rate * tuning_factor)); // work on multiples of min interval
3273+
if (collection == JL_GC_AUTO) {
3274+
uint64_t alloc_diff = before_free_heap_size - old_heap_size;
3275+
uint64_t freed_diff = before_free_heap_size - heap_size;
3276+
double alloc_smooth_factor = 0.95;
3277+
double collect_smooth_factor = 0.5;
3278+
double tuning_factor = 0.03;
3279+
double alloc_mem = jl_gc_smooth(old_alloc_diff, alloc_diff, alloc_smooth_factor);
3280+
double alloc_time = jl_gc_smooth(old_mut_time, mutator_time, alloc_smooth_factor);
3281+
double gc_mem = jl_gc_smooth(old_freed_diff, freed_diff, collect_smooth_factor);
3282+
double gc_time = jl_gc_smooth(old_pause_time, pause, collect_smooth_factor);
3283+
old_alloc_diff = alloc_diff;
3284+
old_mut_time = mutator_time;
3285+
old_freed_diff = freed_diff;
3286+
old_pause_time = pause;
3287+
old_heap_size = heap_size;
3288+
if (alloc_mem != 0 && alloc_time != 0 && gc_mem != 0 && gc_time != 0 ) {
3289+
double alloc_rate = alloc_mem/alloc_time;
3290+
double gc_rate = gc_mem/gc_time;
3291+
target_allocs = sqrt(((double)heap_size/min_interval * alloc_rate)/(gc_rate * tuning_factor)); // work on multiples of min interval
3292+
}
33033293
}
3304-
else
3294+
if (target_allocs == 0.0)
33053295
target_allocs = 2*sqrt((double)heap_size/min_interval);
33063296

33073297
uint64_t target_heap = (uint64_t)target_allocs*min_interval + heap_size;
@@ -3611,7 +3601,6 @@ JL_DLLEXPORT void *jl_gc_counted_malloc(size_t sz)
36113601
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + sz);
36123602
jl_atomic_store_relaxed(&ptls->gc_num.malloc,
36133603
jl_atomic_load_relaxed(&ptls->gc_num.malloc) + 1);
3614-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_mallocd, sz);
36153604
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, sz);
36163605
}
36173606
return malloc(sz);
@@ -3628,7 +3617,6 @@ JL_DLLEXPORT void *jl_gc_counted_calloc(size_t nm, size_t sz)
36283617
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + nm*sz);
36293618
jl_atomic_store_relaxed(&ptls->gc_num.malloc,
36303619
jl_atomic_load_relaxed(&ptls->gc_num.malloc) + 1);
3631-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_mallocd, nm*sz);
36323620
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, nm*sz);
36333621
}
36343622
return calloc(nm, sz);
@@ -3645,7 +3633,6 @@ JL_DLLEXPORT void jl_gc_counted_free_with_size(void *p, size_t sz)
36453633
jl_atomic_load_relaxed(&ptls->gc_num.freed) + sz);
36463634
jl_atomic_store_relaxed(&ptls->gc_num.freecall,
36473635
jl_atomic_load_relaxed(&ptls->gc_num.freecall) + 1);
3648-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.malloc_bytes_freed, sz);
36493636
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -sz);
36503637
}
36513638
}
@@ -3665,8 +3652,6 @@ JL_DLLEXPORT void *jl_gc_counted_realloc_with_old_size(void *p, size_t old, size
36653652
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + (sz - old));
36663653
jl_atomic_store_relaxed(&ptls->gc_num.realloc,
36673654
jl_atomic_load_relaxed(&ptls->gc_num.realloc) + 1);
3668-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_mallocd, sz);
3669-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.malloc_bytes_freed, old);
36703655
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, sz-old);
36713656
}
36723657
return realloc(p, sz);
@@ -3742,7 +3727,6 @@ JL_DLLEXPORT void *jl_gc_managed_malloc(size_t sz)
37423727
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + allocsz);
37433728
jl_atomic_store_relaxed(&ptls->gc_num.malloc,
37443729
jl_atomic_load_relaxed(&ptls->gc_num.malloc) + 1);
3745-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_mallocd, allocsz);
37463730
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, allocsz);
37473731
int last_errno = errno;
37483732
#ifdef _OS_WINDOWS_
@@ -3782,8 +3766,6 @@ static void *gc_managed_realloc_(jl_ptls_t ptls, void *d, size_t sz, size_t olds
37823766
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + (allocsz - oldsz));
37833767
jl_atomic_store_relaxed(&ptls->gc_num.realloc,
37843768
jl_atomic_load_relaxed(&ptls->gc_num.realloc) + 1);
3785-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_mallocd, allocsz);
3786-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.malloc_bytes_freed, oldsz);
37873769
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, allocsz-oldsz);
37883770
int last_errno = errno;
37893771
#ifdef _OS_WINDOWS_
@@ -3873,7 +3855,6 @@ static void *gc_perm_alloc_large(size_t sz, int zero, unsigned align, unsigned o
38733855
#ifdef _OS_WINDOWS_
38743856
SetLastError(last_error);
38753857
#endif
3876-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_allocd,sz);
38773858
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size,sz);
38783859
errno = last_errno;
38793860
jl_may_leak(base);
@@ -3918,7 +3899,6 @@ void *jl_gc_perm_alloc_nolock(size_t sz, int zero, unsigned align, unsigned offs
39183899
errno = last_errno;
39193900
if (__unlikely(pool == MAP_FAILED))
39203901
return NULL;
3921-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.pages_perm_allocd, 1);
39223902
#endif
39233903
gc_perm_pool = (uintptr_t)pool;
39243904
gc_perm_end = gc_perm_pool + GC_PERM_POOL_SIZE;

src/gc.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,6 @@ typedef struct {
261261
typedef struct {
262262
_Atomic(size_t) bytes_mapped;
263263
_Atomic(size_t) bytes_resident;
264-
_Atomic(size_t) bytes_freed;
265-
_Atomic(size_t) bytes_allocd;
266-
_Atomic(size_t) bytes_mallocd;
267-
_Atomic(size_t) malloc_bytes_freed;
268-
_Atomic(size_t) pages_perm_allocd;
269264
_Atomic(size_t) heap_size;
270265
_Atomic(size_t) heap_target;
271266
} gc_heapstatus_t;

0 commit comments

Comments
 (0)