Skip to content

Commit 32aa29f

Browse files
authored
Page based heap size heuristics (#50144)
This PR implements GC heuristics based on the amount of pages allocated instead of live objects like was done before. The heuristic for new heap target is based on https://dl.acm.org/doi/10.1145/3563323 (in summary it argues that the heap target should have square root behaviour). From my testing this fixes #49545 and #49761
2 parents d1be33d + 9f3ca7c commit 32aa29f

File tree

9 files changed

+203
-112
lines changed

9 files changed

+203
-112
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: 15 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,
@@ -1217,15 +1220,25 @@ JL_DLLEXPORT void jl_enable_gc_logging(int enable) {
12171220
gc_logging_enabled = enable;
12181221
}
12191222

1220-
void _report_gc_finished(uint64_t pause, uint64_t freed, int full, int recollect) JL_NOTSAFEPOINT {
1223+
void _report_gc_finished(uint64_t pause, uint64_t freed, int full, int recollect, int64_t live_bytes) JL_NOTSAFEPOINT {
12211224
if (!gc_logging_enabled) {
12221225
return;
12231226
}
12241227
jl_safe_printf("GC: pause %.2fms. collected %fMB. %s %s\n",
1225-
pause/1e6, freed/1e6,
1228+
pause/1e6, freed/(double)(1<<20),
12261229
full ? "full" : "incr",
12271230
recollect ? "recollect" : ""
12281231
);
1232+
1233+
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",
1234+
jl_atomic_load_relaxed(&gc_heap_stats.bytes_mapped)/(double)(1<<20),
1235+
jl_atomic_load_relaxed(&gc_heap_stats.bytes_resident)/(double)(1<<20),
1236+
jl_atomic_load_relaxed(&gc_heap_stats.heap_size)/(double)(1<<20),
1237+
jl_atomic_load_relaxed(&gc_heap_stats.heap_target)/(double)(1<<20),
1238+
live_bytes/(double)(1<<20),
1239+
(double)live_bytes/(double)jl_atomic_load_relaxed(&gc_heap_stats.heap_size)
1240+
);
1241+
// Should fragmentation use bytes_resident instead of heap_size?
12291242
}
12301243

12311244
#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)