Skip to content

Commit a9f4c08

Browse files
gbaraldid-netto
authored andcommitted
Add under pressure callback
1 parent 7597f15 commit a9f4c08

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
@@ -26,6 +26,8 @@ static jl_gc_callback_list_t *gc_cblist_pre_gc;
2626
static jl_gc_callback_list_t *gc_cblist_post_gc;
2727
static jl_gc_callback_list_t *gc_cblist_notify_external_alloc;
2828
static jl_gc_callback_list_t *gc_cblist_notify_external_free;
29+
static jl_gc_callback_list_t *gc_cblist_notify_gc_pressure;
30+
typedef void (*jl_gc_cb_notify_gc_pressure_t)(void);
2931

3032
#define gc_invoke_callbacks(ty, list, args) \
3133
do { \
@@ -112,6 +114,14 @@ JL_DLLEXPORT void jl_gc_set_cb_notify_external_free(jl_gc_cb_notify_external_fre
112114
jl_gc_deregister_callback(&gc_cblist_notify_external_free, (jl_gc_cb_func_t)cb);
113115
}
114116

117+
JL_DLLEXPORT void jl_gc_set_cb_notify_gc_pressure(jl_gc_cb_notify_gc_pressure_t cb, int enable)
118+
{
119+
if (enable)
120+
jl_gc_register_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb);
121+
else
122+
jl_gc_deregister_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb);
123+
}
124+
115125
// Save/restore local mark stack to/from thread-local storage.
116126

117127
STATIC_INLINE void export_gc_state(jl_ptls_t ptls, jl_gc_mark_sp_t *sp) {
@@ -854,6 +864,7 @@ static int mark_reset_age = 0;
854864
static int64_t scanned_bytes; // young bytes scanned while marking
855865
static int64_t perm_scanned_bytes; // old bytes scanned while marking
856866
int prev_sweep_full = 1;
867+
int under_pressure = 0;
857868

858869
#define inc_sat(v,s) v = (v) >= s ? s : (v)+1
859870

@@ -3600,6 +3611,8 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
36003611
next_sweep_full = 1;
36013612
else
36023613
next_sweep_full = 0;
3614+
if (heap_size > max_total_memory * 0.8 || thrashing)
3615+
under_pressure = 1;
36033616
// sweeping is over
36043617
// 6. if it is a quick sweep, put back the remembered objects in queued state
36053618
// so that we don't trigger the barrier again on them.
@@ -3745,6 +3758,10 @@ JL_DLLEXPORT void jl_gc_collect(jl_gc_collection_t collection)
37453758

37463759
gc_invoke_callbacks(jl_gc_cb_post_gc_t,
37473760
gc_cblist_post_gc, (collection));
3761+
if (under_pressure)
3762+
gc_invoke_callbacks(jl_gc_cb_notify_gc_pressure_t,
3763+
gc_cblist_notify_gc_pressure, ());
3764+
under_pressure = 0;
37483765
#ifdef _OS_WINDOWS_
37493766
SetLastError(last_error);
37503767
#endif

src/jl_exported_funcs.inc

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

0 commit comments

Comments
 (0)