Skip to content

Commit 5f36833

Browse files
committed
Implement new GC heuristics.
1 parent 8a4ab11 commit 5f36833

File tree

6 files changed

+156
-91
lines changed

6 files changed

+156
-91
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Language changes
99

1010
Compiler/Runtime improvements
1111
-----------------------------
12+
* Updated GC heuristics to count allocated pages instead of individual objects ([#50144]).
1213

1314
Command-line option changes
1415
---------------------------

doc/src/devdocs/gc.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ This scheme eliminates the need of explicitly keeping a flag to indicate a full
6767
## Heuristics
6868

6969
GC heuristics tune the GC by changing the size of the allocation interval between garbage collections.
70-
If a GC was unproductive, then we increase the size of the allocation interval to allow objects more time to die.
71-
If a GC returns a lot of space we can shrink the interval. The goal is to find a steady state where we are
72-
allocating just about the same amount as we are collecting.
70+
71+
The GC heuristics measure how big the heap size is after a collection and set the next
72+
collection according to the algorithm described by https://dl.acm.org/doi/10.1145/3563323,
73+
in summary, it argues that the heap target should have a square root relationship with the live heap, and that it should also be scaled by how fast the GC is freeing objects and how fast the mutators are allocating.
74+
The heuristics measure the heap size by counting the number of pages that are in use and the objects that use malloc. Previously we measured the heap size by counting
75+
the alive objects, but that doesn't take into account fragmentation which could lead to bad decisions, that also meant that we used thread local information (allocations) to make
76+
decisions about a process wide (when to GC), measuring pages means the decision is global.
77+
78+
The GC will do full collections when the heap size reaches 80% of the maximum allowed size.

src/gc-debug.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// This file is a part of Julia. License is MIT: https://julialang.org/license
22

33
#include "gc.h"
4+
#include "julia.h"
45
#include <inttypes.h>
6+
#include <stddef.h>
7+
#include <stdint.h>
58
#include <stdio.h>
69

710
// re-include assert.h without NDEBUG,
@@ -1216,15 +1219,30 @@ JL_DLLEXPORT void jl_enable_gc_logging(int enable) {
12161219
gc_logging_enabled = enable;
12171220
}
12181221

1219-
void _report_gc_finished(uint64_t pause, uint64_t freed, int full, int recollect) JL_NOTSAFEPOINT {
1222+
void _report_gc_finished(uint64_t pause, uint64_t freed, int full, int recollect, int64_t live_bytes) JL_NOTSAFEPOINT {
12201223
if (!gc_logging_enabled) {
12211224
return;
12221225
}
12231226
jl_safe_printf("GC: pause %.2fms. collected %fMB. %s %s\n",
1224-
pause/1e6, freed/1e6,
1227+
pause/1e6, freed/(double)(1<<20),
12251228
full ? "full" : "incr",
12261229
recollect ? "recollect" : ""
12271230
);
1231+
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",
1233+
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),
1239+
jl_atomic_load_relaxed(&gc_heap_stats.heap_size)/(double)(1<<20),
1240+
jl_atomic_load_relaxed(&gc_heap_stats.heap_target)/(double)(1<<20),
1241+
live_bytes/(double)(1<<20)
1242+
);
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);
1245+
// Should fragmentation use bytes_resident instead of heap_size?
12281246
}
12291247

12301248
#ifdef __cplusplus

src/gc-pages.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ char *jl_gc_try_alloc_pages_(int pg_cnt) JL_NOTSAFEPOINT
5252
// round data pointer up to the nearest gc_page_data-aligned
5353
// boundary if mmap didn't already do so.
5454
mem = (char*)gc_page_data(mem + GC_PAGE_SZ - 1);
55+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_mapped, pages_sz);
56+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_resident, pages_sz);
5557
return mem;
5658
}
5759

@@ -115,6 +117,7 @@ NOINLINE jl_gc_pagemeta_t *jl_gc_alloc_page(void) JL_NOTSAFEPOINT
115117
// try to get page from `pool_freed`
116118
meta = pop_lf_page_metadata_back(&global_page_pool_freed);
117119
if (meta != NULL) {
120+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_resident, GC_PAGE_SZ);
118121
gc_alloc_map_set(meta->data, GC_PAGE_ALLOCATED);
119122
goto exit;
120123
}
@@ -188,6 +191,7 @@ void jl_gc_free_page(jl_gc_pagemeta_t *pg) JL_NOTSAFEPOINT
188191
madvise(p, decommit_size, MADV_DONTNEED);
189192
#endif
190193
msan_unpoison(p, decommit_size);
194+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.bytes_resident, -decommit_size);
191195
}
192196

193197
#ifdef __cplusplus

0 commit comments

Comments
 (0)