Skip to content

Commit 70851d8

Browse files
committed
Fixed spin count problem
1 parent ad9a689 commit 70851d8

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/coreclr/gc/gc.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ BOOL bgc_heap_walk_for_etw_p = FALSE;
9191

9292
#define UOH_ALLOCATION_RETRY_MAX_COUNT 2
9393

94+
#define MAX_YP_SPIN_COUNT_UNIT 32768
95+
9496
uint32_t yp_spin_count_unit = 0;
9597
uint32_t original_spin_count_unit = 0;
9698
size_t loh_size_threshold = LARGE_OBJECT_SIZE;
@@ -2288,6 +2290,7 @@ double gc_heap::short_plugs_pad_ratio = 0;
22882290

22892291
int gc_heap::generation_skip_ratio_threshold = 0;
22902292
int gc_heap::conserve_mem_setting = 0;
2293+
bool gc_heap::spin_count_unit_config_p = false;
22912294

22922295
uint64_t gc_heap::suspended_start_time = 0;
22932296
uint64_t gc_heap::end_gc_time = 0;
@@ -13839,6 +13842,15 @@ HRESULT gc_heap::initialize_gc (size_t soh_segment_size,
1383913842
yp_spin_count_unit = 32 * g_num_processors;
1384013843
#endif //MULTIPLE_HEAPS
1384113844

13845+
// Check if the values are valid for the spin count if provided by the user
13846+
// and if they are, set them as the yp_spin_count_unit and then ignore any updates made in SetYieldProcessorScalingFactor.
13847+
int64_t spin_count_unit_from_config = GCConfig::GetGCSpinCountUnit();
13848+
gc_heap::spin_count_unit_config_p = (spin_count_unit_from_config > 0) && (spin_count_unit_from_config <= MAX_YP_SPIN_COUNT_UNIT);
13849+
if (gc_heap::spin_count_unit_config_p)
13850+
{
13851+
yp_spin_count_unit = static_cast<int32_t>(spin_count_unit_from_config);
13852+
}
13853+
1384213854
original_spin_count_unit = yp_spin_count_unit;
1384313855

1384413856
#if defined(__linux__)
@@ -45860,14 +45872,17 @@ size_t GCHeap::GetPromotedBytes(int heap_index)
4586045872

4586145873
void GCHeap::SetYieldProcessorScalingFactor (float scalingFactor)
4586245874
{
45863-
assert (yp_spin_count_unit != 0);
45864-
uint32_t saved_yp_spin_count_unit = yp_spin_count_unit;
45865-
yp_spin_count_unit = (uint32_t)((float)original_spin_count_unit * scalingFactor / (float)9);
45866-
45867-
// It's very suspicious if it becomes 0 and also, we don't want to spin too much.
45868-
if ((yp_spin_count_unit == 0) || (yp_spin_count_unit > 32768))
45875+
if (!gc_heap::spin_count_unit_config_p)
4586945876
{
45870-
yp_spin_count_unit = saved_yp_spin_count_unit;
45877+
assert (yp_spin_count_unit != 0);
45878+
uint32_t saved_yp_spin_count_unit = yp_spin_count_unit;
45879+
yp_spin_count_unit = (uint32_t)((float)original_spin_count_unit * scalingFactor / (float)9);
45880+
45881+
// It's very suspicious if it becomes 0 and also, we don't want to spin too much.
45882+
if ((yp_spin_count_unit == 0) || (yp_spin_count_unit > MAX_YP_SPIN_COUNT_UNIT))
45883+
{
45884+
yp_spin_count_unit = saved_yp_spin_count_unit;
45885+
}
4587145886
}
4587245887
}
4587345888

src/coreclr/gc/gcconfig.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ class GCConfigStringHolder
136136
INT_CONFIG (GCEnabledInstructionSets, "GCEnabledInstructionSets", NULL, -1, "Specifies whether GC can use AVX2 or AVX512F - 0 for neither, 1 for AVX2, 3 for AVX512F")\
137137
INT_CONFIG (GCConserveMem, "GCConserveMemory", "System.GC.ConserveMemory", 0, "Specifies how hard GC should try to conserve memory - values 0-9") \
138138
INT_CONFIG (GCWriteBarrier, "GCWriteBarrier", NULL, 0, "Specifies whether GC should use more precise but slower write barrier") \
139-
STRING_CONFIG(GCName, "GCName", "System.GC.Name", "Specifies the path of the standalone GC implementation.")
139+
STRING_CONFIG(GCName, "GCName", "System.GC.Name", "Specifies the path of the standalone GC implementation.") \
140+
INT_CONFIG (GCSpinCountUnit, "GCSpinCountUnit", 0, 0, "Specifies the spin count unit used by the GC.")
140141
// This class is responsible for retreiving configuration information
141142
// for how the GC should operate.
142143
class GCConfig

src/coreclr/gc/gcpriv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4097,6 +4097,8 @@ class gc_heap
40974097
PER_HEAP_ISOLATED_FIELD_INIT_ONLY int generation_skip_ratio_threshold;
40984098
PER_HEAP_ISOLATED_FIELD_INIT_ONLY int conserve_mem_setting;
40994099

4100+
PER_HEAP_ISOLATED_FIELD_INIT_ONLY bool spin_count_unit_config_p;
4101+
41004102
// For SOH we always allocate segments of the same
41014103
// size unless no_gc_region requires larger ones.
41024104
PER_HEAP_ISOLATED_FIELD_INIT_ONLY size_t soh_segment_size;

0 commit comments

Comments
 (0)