Skip to content

Commit f61f5e3

Browse files
d-nettoRAI CI (GitHub Action Automation)
authored andcommitted
backport memory pressure callback to 1.9 (#114)
1 parent 54db887 commit f61f5e3

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
@@ -3495,6 +3495,7 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
34953495
// If the live data outgrows the suggested max_total_memory
34963496
// we keep going with full gcs until we either free some space or get an OOM error.
34973497
if (live_bytes > max_total_memory) {
3498+
under_memory_pressure = 1;
34983499
sweep_full = 1;
34993500
}
35003501
if (gc_sweep_always_full) {
@@ -3702,10 +3703,12 @@ JL_DLLEXPORT void jl_gc_collect(jl_gc_collection_t collection)
37023703

37033704
gc_invoke_callbacks(jl_gc_cb_post_gc_t,
37043705
gc_cblist_post_gc, (collection));
3705-
if (under_pressure)
3706+
3707+
if (under_memory_pressure) {
37063708
gc_invoke_callbacks(jl_gc_cb_notify_gc_pressure_t,
37073709
gc_cblist_notify_gc_pressure, ());
3708-
under_pressure = 0;
3710+
}
3711+
under_memory_pressure = 0;
37093712
#ifdef _OS_WINDOWS_
37103713
SetLastError(last_error);
37113714
#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)