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

Page based heap size heuristics #50144

Merged
merged 13 commits into from
Jul 23, 2023
Prev Previous commit
Next Next commit
Add under pressure callback
  • Loading branch information
gbaraldi committed Jul 19, 2023
commit 15b34a5768f330d581472c461be2d663b794f5fa
17 changes: 17 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ 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;
typedef void (*jl_gc_cb_notify_gc_pressure_t)(void);

#define gc_invoke_callbacks(ty, list, args) \
do { \
Expand Down Expand Up @@ -128,6 +130,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 @@ -739,6 +749,7 @@ static int mark_reset_age = 0;
static int64_t scanned_bytes; // young bytes scanned while marking
static int64_t perm_scanned_bytes; // old bytes scanned while marking
int prev_sweep_full = 1;
int under_pressure = 0;

// Full collection heuristics
static int64_t live_bytes = 0;
Expand Down Expand Up @@ -3338,6 +3349,8 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
next_sweep_full = 1;
else
next_sweep_full = 0;
if (heap_size > max_total_memory * 0.8 || thrashing)
under_pressure = 1;
// sweeping is over
// 7. if it is a quick sweep, put back the remembered objects in queued state
// so that we don't trigger the barrier again on them.
Expand Down Expand Up @@ -3487,6 +3500,10 @@ 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)
gc_invoke_callbacks(jl_gc_cb_notify_gc_pressure_t,
gc_cblist_notify_gc_pressure, ());
under_pressure = 0;
#ifdef _OS_WINDOWS_
SetLastError(last_error);
#endif
Expand Down
1 change: 1 addition & 0 deletions src/jl_exported_funcs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
XX(jl_gc_schedule_foreign_sweepfunc) \
XX(jl_gc_set_cb_notify_external_alloc) \
XX(jl_gc_set_cb_notify_external_free) \
XX(jl_gc_set_cb_notify_gc_pressure) \
XX(jl_gc_set_cb_post_gc) \
XX(jl_gc_set_cb_pre_gc) \
XX(jl_gc_set_cb_root_scanner) \
Expand Down