Skip to content

Commit 6a8a3ff

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

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
@@ -2958,7 +2958,7 @@ size_t gc_count_work_in_queue(jl_ptls_t ptls) JL_NOTSAFEPOINT
29582958
* have tried to steal from the queue which still has a work item left, but failed to do so,
29592959
* which violates the semantics of Chase-Lev's work-stealing queue.
29602960
*
2961-
* - Let E1 be the event "master thread writes -1 to gc_master_tid" and E2 be the even
2961+
* - Let E1 be the event "master thread writes -1 to gc_master_tid" and E2 be the event
29622962
* "master thread observes that `gc_n_threads_marking` is zero". Since we're using
29632963
* sequentially consistent atomics, E1 => E2. Now suppose one thread which is spinning in
29642964
* `gc_should_mark` tries to enter the mark-loop after E2. In order to do so, it must
@@ -3502,6 +3502,7 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
35023502
// If the live data outgrows the suggested max_total_memory
35033503
// we keep going with full gcs until we either free some space or get an OOM error.
35043504
if (live_bytes > max_total_memory) {
3505+
under_memory_pressure = 1;
35053506
sweep_full = 1;
35063507
}
35073508
if (gc_sweep_always_full) {
@@ -3709,10 +3710,12 @@ JL_DLLEXPORT void jl_gc_collect(jl_gc_collection_t collection)
37093710

37103711
gc_invoke_callbacks(jl_gc_cb_post_gc_t,
37113712
gc_cblist_post_gc, (collection));
3712-
if (under_pressure)
3713+
3714+
if (under_memory_pressure) {
37133715
gc_invoke_callbacks(jl_gc_cb_notify_gc_pressure_t,
37143716
gc_cblist_notify_gc_pressure, ());
3715-
under_pressure = 0;
3717+
}
3718+
under_memory_pressure = 0;
37163719
#ifdef _OS_WINDOWS_
37173720
SetLastError(last_error);
37183721
#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)