From ea11f258b719d79e33a074c4a8ec77a0aa9f334b Mon Sep 17 00:00:00 2001 From: Diogo Netto <61364108+d-netto@users.noreply.github.com> Date: Wed, 13 Dec 2023 13:38:41 -0300 Subject: [PATCH] backport memory pressure callback to 1.9 (#114) --- src/gc.c | 23 +++++++++++++---------- src/julia_gcext.h | 4 ++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/gc.c b/src/gc.c index 3a7246383bf2f..1776c6beacade 100644 --- a/src/gc.c +++ b/src/gc.c @@ -50,7 +50,6 @@ static jl_gc_callback_list_t *gc_cblist_post_gc; static jl_gc_callback_list_t *gc_cblist_notify_external_alloc; static jl_gc_callback_list_t *gc_cblist_notify_external_free; static jl_gc_callback_list_t *gc_cblist_notify_gc_pressure; -typedef void (*jl_gc_cb_notify_gc_pressure_t)(void); #define gc_invoke_callbacks(ty, list, args) \ do { \ @@ -138,12 +137,12 @@ JL_DLLEXPORT void jl_gc_set_cb_notify_external_free(jl_gc_cb_notify_external_fre } JL_DLLEXPORT void jl_gc_set_cb_notify_gc_pressure(jl_gc_cb_notify_gc_pressure_t cb, int enable) -{ - if (enable) - jl_gc_register_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb); - else - jl_gc_deregister_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb); -} + { + if (enable) + jl_gc_register_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb); + else + jl_gc_deregister_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb); + } // Protect all access to `finalizer_list_marked` and `to_finalize`. // For accessing `ptls->finalizers`, the lock is needed if a thread @@ -676,6 +675,7 @@ static void gc_sweep_foreign_objs(void) } // GC knobs and self-measurement variables +static int under_memory_pressure; static int64_t last_gc_total_bytes = 0; // 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 * have tried to steal from the queue which still has a work item left, but failed to do so, * which violates the semantics of Chase-Lev's work-stealing queue. * - * - Let E1 be the event "master thread writes -1 to gc_master_tid" and E2 be the even + * - Let E1 be the event "master thread writes -1 to gc_master_tid" and E2 be the event * "master thread observes that `gc_n_threads_marking` is zero". Since we're using * sequentially consistent atomics, E1 => E2. Now suppose one thread which is spinning in * `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) // If the live data outgrows the suggested max_total_memory // we keep going with full gcs until we either free some space or get an OOM error. if (live_bytes > max_total_memory) { + under_memory_pressure = 1; sweep_full = 1; } if (gc_sweep_always_full) { @@ -3709,10 +3710,12 @@ JL_DLLEXPORT void jl_gc_collect(jl_gc_collection_t collection) gc_invoke_callbacks(jl_gc_cb_post_gc_t, gc_cblist_post_gc, (collection)); - if (under_pressure) + + if (under_memory_pressure) { gc_invoke_callbacks(jl_gc_cb_notify_gc_pressure_t, gc_cblist_notify_gc_pressure, ()); - under_pressure = 0; + } + under_memory_pressure = 0; #ifdef _OS_WINDOWS_ SetLastError(last_error); #endif diff --git a/src/julia_gcext.h b/src/julia_gcext.h index 27f0a6b5ec11c..e7cb57d622a78 100644 --- a/src/julia_gcext.h +++ b/src/julia_gcext.h @@ -34,6 +34,10 @@ JL_DLLEXPORT void jl_gc_set_cb_notify_external_alloc(jl_gc_cb_notify_external_al JL_DLLEXPORT void jl_gc_set_cb_notify_external_free(jl_gc_cb_notify_external_free_t cb, int enable); +// Memory pressure callback +typedef void (*jl_gc_cb_notify_gc_pressure_t)(void); +JL_DLLEXPORT void jl_gc_set_cb_notify_gc_pressure(jl_gc_cb_notify_gc_pressure_t cb, int enable); + // Types for custom mark and sweep functions. typedef uintptr_t (*jl_markfunc_t)(jl_ptls_t, jl_value_t *obj); typedef void (*jl_sweepfunc_t)(jl_value_t *obj);