Skip to content

Commit bd23e4e

Browse files
committed
Improve accounting a bit more
1 parent 6470135 commit bd23e4e

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/gc-debug.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,10 +1230,10 @@ void _report_gc_finished(uint64_t pause, uint64_t freed, int full, int recollect
12301230
recollect ? "recollect" : ""
12311231
);
12321232

1233-
jl_safe_printf("Heap stats: bytes_mapped %.2f MB, bytes_resident %.2f MB, live_bytes %.2f MB,\nheap_size %.2f MB, heap_target %.2f MB, Fragmentation %.3f\n",
1233+
jl_safe_printf("Heap stats: bytes_mapped %.2f MB, bytes_resident %.2f MB,\nheap_size %.2f MB, heap_target %.2f MB, Fragmentation %.3f\n",
12341234
jl_atomic_load_relaxed(&gc_heap_stats.bytes_mapped)/(double)(1<<20),
12351235
jl_atomic_load_relaxed(&gc_heap_stats.bytes_resident)/(double)(1<<20),
1236-
live_bytes/(double)(1<<20),
1236+
// live_bytes/(double)(1<<20), live byes tracking is not accurate.
12371237
jl_atomic_load_relaxed(&gc_heap_stats.heap_size)/(double)(1<<20),
12381238
jl_atomic_load_relaxed(&gc_heap_stats.heap_target)/(double)(1<<20),
12391239
(double)live_bytes/(double)jl_atomic_load_relaxed(&gc_heap_stats.heap_size)

src/gc.c

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ static uint64_t old_heap_size = 0;
697697
static uint64_t old_alloc_diff = 0;
698698
static uint64_t old_freed_diff = 0;
699699
static uint64_t gc_end_time = 0;
700-
700+
static int thrash_counter = 0;
701701

702702
// global variables for GC stats
703703

@@ -3324,7 +3324,7 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
33243324
gc_num.last_incremental_sweep = gc_end_time;
33253325
}
33263326

3327-
int thrashing = 0; // maybe we should report this to the user or error out?
3327+
int thrashing = thrash_counter > 4; // maybe we should report this to the user or error out?
33283328
size_t heap_size = jl_atomic_load_relaxed(&gc_heap_stats.heap_size);
33293329
double target_allocs = 0.0;
33303330
double min_interval = default_collect_interval;
@@ -3343,7 +3343,10 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
33433343
old_freed_diff = freed_diff;
33443344
old_pause_time = pause;
33453345
old_heap_size = heap_size; // TODO: Update these values dynamically instead of just during the GC
3346-
thrashing = gc_time > mutator_time * 98 ? 1 : 0;
3346+
if (gc_time > alloc_time * 95)
3347+
thrash_counter += 1;
3348+
else
3349+
thrash_counter = 0;
33473350
if (alloc_mem != 0 && alloc_time != 0 && gc_mem != 0 && gc_time != 0 ) {
33483351
double alloc_rate = alloc_mem/alloc_time;
33493352
double gc_rate = gc_mem/gc_time;
@@ -3723,6 +3726,26 @@ JL_DLLEXPORT void *jl_gc_counted_realloc_with_old_size(void *p, size_t old, size
37233726
jl_atomic_store_relaxed(&ptls->gc_num.realloc,
37243727
jl_atomic_load_relaxed(&ptls->gc_num.realloc) + 1);
37253728
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, sz-old);
3729+
3730+
int64_t diff = sz - old;
3731+
if (diff < 0) {
3732+
uint64_t free_acc = jl_atomic_load_relaxed(&ptls->gc_num.free_acc);
3733+
if (free_acc + diff < 16*1024)
3734+
jl_atomic_store_relaxed(&ptls->gc_num.free_acc, free_acc + sz);
3735+
else {
3736+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -(free_acc + sz));
3737+
jl_atomic_store_relaxed(&ptls->gc_num.free_acc, 0);
3738+
}
3739+
}
3740+
else {
3741+
uint64_t alloc_acc = jl_atomic_load_relaxed(&ptls->gc_num.alloc_acc);
3742+
if (alloc_acc + diff < 16*1024)
3743+
jl_atomic_store_relaxed(&ptls->gc_num.alloc_acc, alloc_acc + diff);
3744+
else {
3745+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, alloc_acc + diff);
3746+
jl_atomic_store_relaxed(&ptls->gc_num.alloc_acc, 0);
3747+
}
3748+
}
37263749
}
37273750
return realloc(p, sz);
37283751
}
@@ -3839,6 +3862,27 @@ static void *gc_managed_realloc_(jl_ptls_t ptls, void *d, size_t sz, size_t olds
38393862
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + (allocsz - oldsz));
38403863
jl_atomic_store_relaxed(&ptls->gc_num.realloc,
38413864
jl_atomic_load_relaxed(&ptls->gc_num.realloc) + 1);
3865+
3866+
int64_t diff = allocsz - oldsz;
3867+
if (diff < 0) {
3868+
uint64_t free_acc = jl_atomic_load_relaxed(&ptls->gc_num.free_acc);
3869+
if (free_acc + diff < 16*1024)
3870+
jl_atomic_store_relaxed(&ptls->gc_num.free_acc, free_acc + sz);
3871+
else {
3872+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -(free_acc + sz));
3873+
jl_atomic_store_relaxed(&ptls->gc_num.free_acc, 0);
3874+
}
3875+
}
3876+
else {
3877+
uint64_t alloc_acc = jl_atomic_load_relaxed(&ptls->gc_num.alloc_acc);
3878+
if (alloc_acc + diff < 16*1024)
3879+
jl_atomic_store_relaxed(&ptls->gc_num.alloc_acc, alloc_acc + diff);
3880+
else {
3881+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, alloc_acc + diff);
3882+
jl_atomic_store_relaxed(&ptls->gc_num.alloc_acc, 0);
3883+
}
3884+
}
3885+
38423886
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, allocsz-oldsz);
38433887
int last_errno = errno;
38443888
#ifdef _OS_WINDOWS_

0 commit comments

Comments
 (0)