Skip to content

Commit 8fc11f7

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

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/gc.c

Lines changed: 13 additions & 10 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
@@ -2947,7 +2947,7 @@ size_t gc_count_work_in_queue(jl_ptls_t ptls) JL_NOTSAFEPOINT
29472947
* have tried to steal from the queue which still has a work item left, but failed to do so,
29482948
* which violates the semantics of Chase-Lev's work-stealing queue.
29492949
*
2950-
* - Let E1 be the event "master thread writes -1 to gc_master_tid" and E2 be the even
2950+
* - Let E1 be the event "master thread writes -1 to gc_master_tid" and E2 be the event
29512951
* "master thread observes that `gc_n_threads_marking` is zero". Since we're using
29522952
* sequentially consistent atomics, E1 => E2. Now suppose one thread which is spinning in
29532953
* `gc_should_mark` tries to enter the mark-loop after E2. In order to do so, it must
@@ -3482,6 +3482,7 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
34823482
// If the live data outgrows the suggested max_total_memory
34833483
// we keep going with full gcs until we either free some space or get an OOM error.
34843484
if (live_bytes > max_total_memory) {
3485+
under_memory_pressure = 1;
34853486
sweep_full = 1;
34863487
}
34873488
if (gc_sweep_always_full) {
@@ -3689,10 +3690,12 @@ JL_DLLEXPORT void jl_gc_collect(jl_gc_collection_t collection)
36893690

36903691
gc_invoke_callbacks(jl_gc_cb_post_gc_t,
36913692
gc_cblist_post_gc, (collection));
3692-
if (under_pressure)
3693+
3694+
if (under_memory_pressure) {
36933695
gc_invoke_callbacks(jl_gc_cb_notify_gc_pressure_t,
36943696
gc_cblist_notify_gc_pressure, ());
3695-
under_pressure = 0;
3697+
}
3698+
under_memory_pressure = 0;
36963699
#ifdef _OS_WINDOWS_
36973700
SetLastError(last_error);
36983701
#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)