Skip to content

Commit 2b95cea

Browse files
d-nettoDrvi
authored andcommitted
backport memory pressure callback to 1.9 (#114)
1 parent 0e198fe commit 2b95cea

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/gc.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ static jl_gc_callback_list_t *gc_cblist_post_gc;
5050
static jl_gc_callback_list_t *gc_cblist_notify_external_alloc;
5151
static jl_gc_callback_list_t *gc_cblist_notify_external_free;
5252
static jl_gc_callback_list_t *gc_cblist_notify_gc_pressure;
53-
typedef void (*jl_gc_cb_notify_gc_pressure_t)(void);
5453

5554
#define gc_invoke_callbacks(ty, list, args) \
5655
do { \
@@ -138,12 +137,12 @@ JL_DLLEXPORT void jl_gc_set_cb_notify_external_free(jl_gc_cb_notify_external_fre
138137
}
139138

140139
JL_DLLEXPORT void jl_gc_set_cb_notify_gc_pressure(jl_gc_cb_notify_gc_pressure_t cb, int enable)
141-
{
142-
if (enable)
143-
jl_gc_register_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb);
144-
else
145-
jl_gc_deregister_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb);
146-
}
140+
{
141+
if (enable)
142+
jl_gc_register_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb);
143+
else
144+
jl_gc_deregister_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb);
145+
}
147146

148147
// Protect all access to `finalizer_list_marked` and `to_finalize`.
149148
// For accessing `ptls->finalizers`, the lock is needed if a thread
@@ -676,6 +675,7 @@ static void gc_sweep_foreign_objs(void)
676675
}
677676

678677
// GC knobs and self-measurement variables
678+
static int under_memory_pressure;
679679
static int64_t last_gc_total_bytes = 0;
680680

681681
// max_total_memory is a suggestion. We try very hard to stay
@@ -3488,6 +3488,7 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
34883488
// If the live data outgrows the suggested max_total_memory
34893489
// we keep going with full gcs until we either free some space or get an OOM error.
34903490
if (live_bytes > max_total_memory) {
3491+
under_memory_pressure = 1;
34913492
sweep_full = 1;
34923493
}
34933494
if (gc_sweep_always_full) {
@@ -3695,10 +3696,12 @@ JL_DLLEXPORT void jl_gc_collect(jl_gc_collection_t collection)
36953696

36963697
gc_invoke_callbacks(jl_gc_cb_post_gc_t,
36973698
gc_cblist_post_gc, (collection));
3698-
if (under_pressure)
3699+
3700+
if (under_memory_pressure) {
36993701
gc_invoke_callbacks(jl_gc_cb_notify_gc_pressure_t,
37003702
gc_cblist_notify_gc_pressure, ());
3701-
under_pressure = 0;
3703+
}
3704+
under_memory_pressure = 0;
37023705
#ifdef _OS_WINDOWS_
37033706
SetLastError(last_error);
37043707
#endif

src/julia_gcext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ JL_DLLEXPORT void jl_gc_set_cb_notify_external_alloc(jl_gc_cb_notify_external_al
3434
JL_DLLEXPORT void jl_gc_set_cb_notify_external_free(jl_gc_cb_notify_external_free_t cb,
3535
int enable);
3636

37+
// Memory pressure callback
38+
typedef void (*jl_gc_cb_notify_gc_pressure_t)(void);
39+
JL_DLLEXPORT void jl_gc_set_cb_notify_gc_pressure(jl_gc_cb_notify_gc_pressure_t cb, int enable);
40+
3741
// Types for custom mark and sweep functions.
3842
typedef uintptr_t (*jl_markfunc_t)(jl_ptls_t, jl_value_t *obj);
3943
typedef void (*jl_sweepfunc_t)(jl_value_t *obj);

0 commit comments

Comments
 (0)