From 9c0397a172b6a07e9feac32d8da330634fce3681 Mon Sep 17 00:00:00 2001 From: Maoni Stephens Date: Sat, 20 Nov 2021 17:35:25 -0800 Subject: [PATCH 1/2] fix for excessive gen0 in high memory load situation --- src/coreclr/gc/gc.cpp | 31 ++++++++++++++++--------------- src/coreclr/gc/gcpriv.h | 3 +-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/coreclr/gc/gc.cpp b/src/coreclr/gc/gc.cpp index 221d2dc2be6316..45c4cad75c2dbe 100644 --- a/src/coreclr/gc/gc.cpp +++ b/src/coreclr/gc/gc.cpp @@ -7151,7 +7151,6 @@ void gc_mechanisms::init_mechanisms() found_finalizers = FALSE; #ifdef BACKGROUND_GC background_p = gc_heap::background_running_p() != FALSE; - allocations_allowed = TRUE; #endif //BACKGROUND_GC entry_memory_load = 0; @@ -7444,15 +7443,6 @@ void gc_heap::reset_allocation_pointers (generation* gen, uint8_t* start) bool gc_heap::new_allocation_allowed (int gen_number) { -#ifdef BACKGROUND_GC - //TODO BACKGROUND_GC this is for test only - if (!settings.allocations_allowed) - { - dprintf (2, ("new allocation not allowed")); - return FALSE; - } -#endif //BACKGROUND_GC - if (dd_new_allocation (dynamic_data_of (gen_number)) < 0) { if (gen_number != 0) @@ -16071,18 +16061,22 @@ void gc_heap::wait_for_background (alloc_wait_reason awr, bool loh_p) add_saved_spinlock_info (loh_p, me_acquire, mt_wait_bgc); } -void gc_heap::wait_for_bgc_high_memory (alloc_wait_reason awr, bool loh_p) +bool gc_heap::wait_for_bgc_high_memory (alloc_wait_reason awr, bool loh_p) { + bool wait_p = false; if (gc_heap::background_running_p()) { uint32_t memory_load; get_memory_info (&memory_load); if (memory_load >= m_high_memory_load_th) { + wait_p = true; dprintf (GTC_LOG, ("high mem - wait for BGC to finish, wait reason: %d", awr)); wait_for_background (awr, loh_p); } } + + return wait_p; } #endif //BACKGROUND_GC @@ -17189,8 +17183,10 @@ allocation_state gc_heap::try_allocate_more_space (alloc_context* acontext, size check_for_full_gc (gen_number, size); } + bool recheck_p = false; + #ifdef BACKGROUND_GC - wait_for_bgc_high_memory (awr_gen0_alloc, loh_p); + recheck_p = wait_for_bgc_high_memory (awr_gen0_alloc, loh_p); #endif //BACKGROUND_GC #ifdef SYNCHRONIZATION_STATS @@ -17198,10 +17194,15 @@ allocation_state gc_heap::try_allocate_more_space (alloc_context* acontext, size #endif //SYNCHRONIZATION_STATS dprintf (2, ("h%d running out of budget on gen%d, gc", heap_number, gen_number)); - if (!settings.concurrent || (gen_number == 0)) +#ifdef BACKGROUND_GC + if (!recheck_p || !(new_allocation_allowed (gen_number))) +#endif //BACKGROUND_GC { - trigger_gc_for_alloc (0, ((gen_number == 0) ? reason_alloc_soh : reason_alloc_loh), - msl, loh_p, mt_try_budget); + if (!settings.concurrent || (gen_number == 0)) + { + trigger_gc_for_alloc (0, ((gen_number == 0) ? reason_alloc_soh : reason_alloc_loh), + msl, loh_p, mt_try_budget); + } } } } diff --git a/src/coreclr/gc/gcpriv.h b/src/coreclr/gc/gcpriv.h index 051dd993014906..ee13911990a016 100644 --- a/src/coreclr/gc/gcpriv.h +++ b/src/coreclr/gc/gcpriv.h @@ -504,7 +504,6 @@ class gc_mechanisms #ifdef BACKGROUND_GC BOOL background_p; bgc_state b_state; - BOOL allocations_allowed; #endif //BACKGROUND_GC #ifdef STRESS_HEAP @@ -1791,7 +1790,7 @@ class gc_heap void wait_for_background (alloc_wait_reason awr, bool loh_p); PER_HEAP - void wait_for_bgc_high_memory (alloc_wait_reason awr, bool loh_p); + bool wait_for_bgc_high_memory (alloc_wait_reason awr, bool loh_p); PER_HEAP void bgc_uoh_alloc_clr (uint8_t* alloc_start, From d982a32c5b8987c28b6321b3db50bbee238646e5 Mon Sep 17 00:00:00 2001 From: Maoni Stephens Date: Wed, 24 Nov 2021 17:27:34 -0800 Subject: [PATCH 2/2] cr feedback --- src/coreclr/gc/gc.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/coreclr/gc/gc.cpp b/src/coreclr/gc/gc.cpp index 45c4cad75c2dbe..391ff85a6ce2b9 100644 --- a/src/coreclr/gc/gc.cpp +++ b/src/coreclr/gc/gc.cpp @@ -17183,10 +17183,8 @@ allocation_state gc_heap::try_allocate_more_space (alloc_context* acontext, size check_for_full_gc (gen_number, size); } - bool recheck_p = false; - #ifdef BACKGROUND_GC - recheck_p = wait_for_bgc_high_memory (awr_gen0_alloc, loh_p); + bool recheck_p = wait_for_bgc_high_memory (awr_gen0_alloc, loh_p); #endif //BACKGROUND_GC #ifdef SYNCHRONIZATION_STATS @@ -17195,7 +17193,11 @@ allocation_state gc_heap::try_allocate_more_space (alloc_context* acontext, size dprintf (2, ("h%d running out of budget on gen%d, gc", heap_number, gen_number)); #ifdef BACKGROUND_GC - if (!recheck_p || !(new_allocation_allowed (gen_number))) + bool trigger_gc_p = true; + if (recheck_p) + trigger_gc_p = !(new_allocation_allowed (gen_number)); + + if (trigger_gc_p) #endif //BACKGROUND_GC { if (!settings.concurrent || (gen_number == 0))