From a589e3926a1780256fdb52376f8681fe047daf54 Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Wed, 18 Oct 2017 20:51:30 -0700 Subject: Fix hill climbing float overflow (#14505) Fix hill climbing float overflow - When hill climbing finds that it wants to decrease the thread count but can't because the thread count is already the minimum, it instead tries to increase the sampling interval by a factor of up to 10 depending on how much it wanted to decrease the thread count - The ratio was being used incorrectly (used max instead of min), and sometimes the ratio can be so large that the conversion to int after the float math overflows - If something in the process enabled floating point exceptions, it may also crash - There doesn't appear to be a clean way to disable hill climbing, added a config variable that disables it in case a workaround is necessary for some other reason in the future - Fixed to avoid overflow in the math to what was probably intended - There may be another bug in GetWaveComponent() that causes values of such high magnitude to be generated, I'll leave that investigation for when that in particular becomes a real issue --- src/inc/clrconfigvalues.h | 1 + src/vm/hillclimbing.cpp | 2 +- src/vm/win32threadpool.cpp | 2 ++ src/vm/win32threadpool.h | 3 ++- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/inc/clrconfigvalues.h b/src/inc/clrconfigvalues.h index 6b935239e4..96cd11014a 100644 --- a/src/inc/clrconfigvalues.h +++ b/src/inc/clrconfigvalues.h @@ -874,6 +874,7 @@ RETAIL_CONFIG_DWORD_INFO(EXTERNAL_Thread_UseAllCpuGroups, W("Thread_UseAllCpuGro CONFIG_DWORD_INFO(INTERNAL_ThreadpoolTickCountAdjustment, W("ThreadpoolTickCountAdjustment"), 0, "") +RETAIL_CONFIG_DWORD_INFO(INTERNAL_HillClimbing_Disable, W("HillClimbing_Disable"), 0, "Disables hill climbing for thread adjustments in the thread pool"); RETAIL_CONFIG_DWORD_INFO(INTERNAL_HillClimbing_WavePeriod, W("HillClimbing_WavePeriod"), 4, ""); RETAIL_CONFIG_DWORD_INFO(INTERNAL_HillClimbing_TargetSignalToNoiseRatio, W("HillClimbing_TargetSignalToNoiseRatio"), 300, ""); RETAIL_CONFIG_DWORD_INFO(INTERNAL_HillClimbing_ErrorSmoothingFactor, W("HillClimbing_ErrorSmoothingFactor"), 1, ""); diff --git a/src/vm/hillclimbing.cpp b/src/vm/hillclimbing.cpp index 598ef3e8cd..196708a5e2 100644 --- a/src/vm/hillclimbing.cpp +++ b/src/vm/hillclimbing.cpp @@ -335,7 +335,7 @@ int HillClimbing::Update(int currentThreadCount, double sampleDuration, int numC // we'll simply stay at minThreads much longer, and only occasionally try a higher value. // if (ratio.r < 0.0 && newThreadCount == ThreadpoolMgr::MinLimitTotalWorkerThreads) - *pNewSampleInterval = (int)(0.5 + m_currentSampleInterval * (10.0 * max(-ratio.r, 1.0))); + *pNewSampleInterval = (int)(0.5 + m_currentSampleInterval * (10.0 * min(-ratio.r, 1.0))); else *pNewSampleInterval = m_currentSampleInterval; diff --git a/src/vm/win32threadpool.cpp b/src/vm/win32threadpool.cpp index a73328ba35..4210060755 100644 --- a/src/vm/win32threadpool.cpp +++ b/src/vm/win32threadpool.cpp @@ -104,6 +104,7 @@ DWORD ThreadpoolMgr::NextCompletedWorkRequestsTime; LARGE_INTEGER ThreadpoolMgr::CurrentSampleStartTime; unsigned int ThreadpoolMgr::WorkerThreadSpinLimit; +bool ThreadpoolMgr::IsHillClimbingDisabled; int ThreadpoolMgr::ThreadAdjustmentInterval; #define INVALID_HANDLE ((HANDLE) -1) @@ -355,6 +356,7 @@ BOOL ThreadpoolMgr::Initialize() EX_TRY { WorkerThreadSpinLimit = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_ThreadPool_UnfairSemaphoreSpinLimit); + IsHillClimbingDisabled = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_HillClimbing_Disable) != 0; ThreadAdjustmentInterval = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_HillClimbing_SampleIntervalLow); pADTPCount->InitResources(); diff --git a/src/vm/win32threadpool.h b/src/vm/win32threadpool.h index fd5b98f369..2a3a3bbb24 100644 --- a/src/vm/win32threadpool.h +++ b/src/vm/win32threadpool.h @@ -861,7 +861,7 @@ public: { ThreadCounter::Counts counts = WorkerCounter.GetCleanCounts(); if (counts.NumActive <= counts.MaxWorking) - return true; + return !IsHillClimbingDisabled; } return false; @@ -1025,6 +1025,7 @@ private: static LARGE_INTEGER CurrentSampleStartTime; static unsigned int WorkerThreadSpinLimit; + static bool IsHillClimbingDisabled; static int ThreadAdjustmentInterval; SPTR_DECL(WorkRequest,WorkRequestHead); // Head of work request queue -- cgit v1.2.3