Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport memory pressure callback to 1.9 #114

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static jl_gc_callback_list_t *gc_cblist_pre_gc;
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;

#define gc_invoke_callbacks(ty, list, args) \
do { \
Expand Down Expand Up @@ -126,6 +127,14 @@ JL_DLLEXPORT void jl_gc_set_cb_notify_external_free(jl_gc_cb_notify_external_fre
jl_gc_deregister_callback(&gc_cblist_notify_external_free, (jl_gc_cb_func_t)cb);
}

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);
}

// Protect all access to `finalizer_list_marked` and `to_finalize`.
// For accessing `ptls->finalizers`, the lock is needed if a thread
// is going to realloc the buffer (of its own list) or accessing the
Expand Down Expand Up @@ -767,6 +776,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
Expand Down Expand Up @@ -3544,6 +3554,7 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
else {
// We can't stay under our goal so let's go back to
// the minimum interval and hope things get better
under_memory_pressure = 1;
gc_num.interval = default_collect_interval;
}
}
Expand Down Expand Up @@ -3647,6 +3658,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_memory_pressure) {
gc_invoke_callbacks(jl_gc_cb_notify_gc_pressure_t,
gc_cblist_notify_gc_pressure, ());
}
under_memory_pressure = 0;
#ifdef _OS_WINDOWS_
SetLastError(last_error);
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/julia_gcext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down