Skip to content

Commit 15b34a5

Browse files
committed
Add under pressure callback
1 parent e6a18f3 commit 15b34a5

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/gc.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ static jl_gc_callback_list_t *gc_cblist_pre_gc;
4242
static jl_gc_callback_list_t *gc_cblist_post_gc;
4343
static jl_gc_callback_list_t *gc_cblist_notify_external_alloc;
4444
static jl_gc_callback_list_t *gc_cblist_notify_external_free;
45+
static jl_gc_callback_list_t *gc_cblist_notify_gc_pressure;
46+
typedef void (*jl_gc_cb_notify_gc_pressure_t)(void);
4547

4648
#define gc_invoke_callbacks(ty, list, args) \
4749
do { \
@@ -128,6 +130,14 @@ JL_DLLEXPORT void jl_gc_set_cb_notify_external_free(jl_gc_cb_notify_external_fre
128130
jl_gc_deregister_callback(&gc_cblist_notify_external_free, (jl_gc_cb_func_t)cb);
129131
}
130132

133+
JL_DLLEXPORT void jl_gc_set_cb_notify_gc_pressure(jl_gc_cb_notify_gc_pressure_t cb, int enable)
134+
{
135+
if (enable)
136+
jl_gc_register_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb);
137+
else
138+
jl_gc_deregister_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb);
139+
}
140+
131141
// Protect all access to `finalizer_list_marked` and `to_finalize`.
132142
// For accessing `ptls->finalizers`, the lock is needed if a thread
133143
// is going to realloc the buffer (of its own list) or accessing the
@@ -739,6 +749,7 @@ static int mark_reset_age = 0;
739749
static int64_t scanned_bytes; // young bytes scanned while marking
740750
static int64_t perm_scanned_bytes; // old bytes scanned while marking
741751
int prev_sweep_full = 1;
752+
int under_pressure = 0;
742753

743754
// Full collection heuristics
744755
static int64_t live_bytes = 0;
@@ -3338,6 +3349,8 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
33383349
next_sweep_full = 1;
33393350
else
33403351
next_sweep_full = 0;
3352+
if (heap_size > max_total_memory * 0.8 || thrashing)
3353+
under_pressure = 1;
33413354
// sweeping is over
33423355
// 7. if it is a quick sweep, put back the remembered objects in queued state
33433356
// so that we don't trigger the barrier again on them.
@@ -3487,6 +3500,10 @@ JL_DLLEXPORT void jl_gc_collect(jl_gc_collection_t collection)
34873500

34883501
gc_invoke_callbacks(jl_gc_cb_post_gc_t,
34893502
gc_cblist_post_gc, (collection));
3503+
if (under_pressure)
3504+
gc_invoke_callbacks(jl_gc_cb_notify_gc_pressure_t,
3505+
gc_cblist_notify_gc_pressure, ());
3506+
under_pressure = 0;
34903507
#ifdef _OS_WINDOWS_
34913508
SetLastError(last_error);
34923509
#endif

src/jl_exported_funcs.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
XX(jl_gc_schedule_foreign_sweepfunc) \
192192
XX(jl_gc_set_cb_notify_external_alloc) \
193193
XX(jl_gc_set_cb_notify_external_free) \
194+
XX(jl_gc_set_cb_notify_gc_pressure) \
194195
XX(jl_gc_set_cb_post_gc) \
195196
XX(jl_gc_set_cb_pre_gc) \
196197
XX(jl_gc_set_cb_root_scanner) \

0 commit comments

Comments
 (0)