From 84da2ff509e2c48d5f76089060e7ed1980be2176 Mon Sep 17 00:00:00 2001 From: Peter Sollich Date: Wed, 28 Sep 2022 16:33:11 +0200 Subject: [PATCH] Gradual decommit in wks (#73620) * Initial version of gradual decommit for WKS. This is the regions version modeled after the behavior of the segments version. Idea is simply to limit the amount of decommitted memory based on the time since the last GC. * Change decommit_step to take a step_milliseconds parameter - this makes the logic for the WKS decommit more straightforward. * Only do decommits at most every 100 milliseconds to limit the number of decommitted regions. * Address code review feedback: disable the logic in decommit_ephemeral_segment_pages for WKS, some changes in distribute_free_regions as a consequence. * Remove unused static field ephemeral_elapsed. * Fix typo in comment. --- src/coreclr/gc/gc.cpp | 35 ++++++++++++++++++++++++++++------- src/coreclr/gc/gcpriv.h | 2 +- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/coreclr/gc/gc.cpp b/src/coreclr/gc/gc.cpp index 93c6715b565..07cb6db7690 100644 --- a/src/coreclr/gc/gc.cpp +++ b/src/coreclr/gc/gc.cpp @@ -6757,7 +6757,7 @@ void gc_heap::gc_thread_function () uint32_t wait_result = gc_heap::ee_suspend_event.Wait(gradual_decommit_in_progress_p ? DECOMMIT_TIME_STEP_MILLISECONDS : INFINITE, FALSE); if (wait_result == WAIT_TIMEOUT) { - gradual_decommit_in_progress_p = decommit_step (); + gradual_decommit_in_progress_p = decommit_step (DECOMMIT_TIME_STEP_MILLISECONDS); continue; } @@ -6850,7 +6850,7 @@ void gc_heap::gc_thread_function () // check if we should do some decommitting if (gradual_decommit_in_progress_p) { - gradual_decommit_in_progress_p = decommit_step (); + gradual_decommit_in_progress_p = decommit_step (DECOMMIT_TIME_STEP_MILLISECONDS); } } else @@ -12598,7 +12598,7 @@ void gc_heap::distribute_free_regions() global_regions_to_decommit[kind].transfer_regions (&hp->free_regions[kind]); } } - while (decommit_step()) + while (decommit_step(DECOMMIT_TIME_STEP_MILLISECONDS)) { } #ifdef MULTIPLE_HEAPS @@ -12850,8 +12850,29 @@ void gc_heap::distribute_free_regions() } } #else //MULTIPLE_HEAPS - while (decommit_step()) + // we want to limit the amount of decommit we do per time to indirectly + // limit the amount of time spent in recommit and page faults + // we use the elapsed time since the last GC to arrive at the desired + // decommit size + // we limit the elapsed time to 10 seconds to avoid spending too much time decommitting + // if less than DECOMMIT_TIME_STEP_MILLISECONDS elapsed, we don't decommit - + // we don't want to decommit fractions of regions here + dynamic_data* dd0 = dynamic_data_of (0); + size_t ephemeral_elapsed = (size_t)((dd_time_clock (dd0) - gc_last_ephemeral_decommit_time) / 1000); + if (ephemeral_elapsed >= DECOMMIT_TIME_STEP_MILLISECONDS) + { + gc_last_ephemeral_decommit_time = dd_time_clock (dd0); + size_t decommit_step_milliseconds = min (ephemeral_elapsed, (10*1000)); + + decommit_step (decommit_step_milliseconds); + } + // transfer any remaining regions on the decommit list back to the free list + for (int kind = basic_free_region; kind < count_free_region_kinds; kind++) { + if (global_regions_to_decommit[kind].get_num_free_regions() != 0) + { + free_regions[kind].transfer_regions (&global_regions_to_decommit[kind]); + } } #endif //MULTIPLE_HEAPS #endif //USE_REGIONS @@ -40573,7 +40594,7 @@ void gc_heap::decommit_ephemeral_segment_pages() (heap_segment_committed (tail_region) - heap_segment_mem (tail_region))/1024, (decommit_target - heap_segment_mem (tail_region))/1024)); } -#else //MULTIPLE_HEAPS && USE_REGIONS +#elif !defined(USE_REGIONS) dynamic_data* dd0 = dynamic_data_of (0); @@ -40640,12 +40661,12 @@ void gc_heap::decommit_ephemeral_segment_pages() } // return true if we actually decommitted anything -bool gc_heap::decommit_step () +bool gc_heap::decommit_step (uint64_t step_milliseconds) { size_t decommit_size = 0; #ifdef USE_REGIONS - const size_t max_decommit_step_size = DECOMMIT_SIZE_PER_MILLISECOND * DECOMMIT_TIME_STEP_MILLISECONDS; + const size_t max_decommit_step_size = DECOMMIT_SIZE_PER_MILLISECOND * step_milliseconds; for (int kind = basic_free_region; kind < count_free_region_kinds; kind++) { dprintf (REGIONS_LOG, ("decommit_step %d, regions_to_decommit = %Id", diff --git a/src/coreclr/gc/gcpriv.h b/src/coreclr/gc/gcpriv.h index b6c12ccd0ce..df4ce12ec52 100644 --- a/src/coreclr/gc/gcpriv.h +++ b/src/coreclr/gc/gcpriv.h @@ -2063,7 +2063,7 @@ protected: PER_HEAP size_t decommit_heap_segment_pages_worker (heap_segment* seg, uint8_t *new_committed); PER_HEAP_ISOLATED - bool decommit_step (); + bool decommit_step (uint64_t step_milliseconds); PER_HEAP void decommit_heap_segment (heap_segment* seg); PER_HEAP_ISOLATED -- GitLab