diff --git a/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.HillClimbing.cs b/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.HillClimbing.cs index e29e9e198d1..a84b75228cb 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.HillClimbing.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.HillClimbing.cs @@ -88,8 +88,8 @@ private struct LogEntry private readonly Random _randomIntervalGenerator = new Random(); private readonly LogEntry[] _log = new LogEntry[LogCapacity]; - private int _logStart = 0; - private int _logSize = 0; + private int _logStart; + private int _logSize; public HillClimbing(int wavePeriod, int maxWaveMagnitude, double waveMagnitudeMultiplier, int waveHistorySize, double targetThroughputRatio, double targetSignalToNoiseRatio, double maxChangePerSecond, double maxChangePerSample, int sampleIntervalMsLow, int sampleIntervalMsHigh, @@ -184,8 +184,11 @@ public HillClimbing(int wavePeriod, int maxWaveMagnitude, double waveMagnitudeMu // Add the current thread count and throughput sample to our history // double throughput = numCompletions / sampleDurationSeconds; - - PortableThreadPoolEventSource.Log.WorkerThreadAdjustmentSample(throughput); + PortableThreadPoolEventSource log = PortableThreadPoolEventSource.Log; + if (log.IsEnabled()) + { + log.WorkerThreadAdjustmentSample(throughput); + } int sampleIndex = (int)(_totalSamples % _samplesToMeasure); _samples[sampleIndex] = throughput; @@ -355,8 +358,11 @@ public HillClimbing(int wavePeriod, int maxWaveMagnitude, double waveMagnitudeMu // Record these numbers for posterity // - PortableThreadPoolEventSource.Log.WorkerThreadAdjustmentStats(sampleDurationSeconds, throughput, threadWaveComponent.Real, throughputWaveComponent.Real, + if (log.IsEnabled()) + { + log.WorkerThreadAdjustmentStats(sampleDurationSeconds, throughput, threadWaveComponent.Real, throughputWaveComponent.Real, throughputErrorEstimate, _averageThroughputNoise, ratio.Real, confidence, _currentControlSetting, (ushort)newThreadWaveMagnitude); + } // @@ -413,7 +419,11 @@ private void LogTransition(int newThreadCount, double throughput, StateOrTransit _logSize++; - PortableThreadPoolEventSource.Log.WorkerThreadAdjustmentAdjustment(throughput, newThreadCount, (int)stateOrTransition); + PortableThreadPoolEventSource log = PortableThreadPoolEventSource.Log; + if (log.IsEnabled()) + { + log.WorkerThreadAdjustmentAdjustment(throughput, newThreadCount, (int)stateOrTransition); + } } public void ForceChange(int newThreadCount, StateOrTransition state) diff --git a/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.WaitThread.cs b/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.WaitThread.cs index b7e78ac3b61..78a3b2db829 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.WaitThread.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.WaitThread.cs @@ -150,7 +150,7 @@ public CompletedWaitHandle(RegisteredWaitHandle completedHandle, bool timedOut) /// /// The number of user-registered waits on this wait thread. /// - private int _numUserWaits = 0; + private int _numUserWaits; /// /// A list of removals of wait handles that are waiting for the wait thread to process. @@ -159,7 +159,7 @@ public CompletedWaitHandle(RegisteredWaitHandle completedHandle, bool timedOut) /// /// The number of pending removals. /// - private int _numPendingRemoves = 0; + private int _numPendingRemoves; /// /// An event to notify the wait thread that there are pending adds or removals of wait handles so it needs to wake up. diff --git a/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.WorkerThread.cs b/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.WorkerThread.cs index dfab32c3e6c..2cedaa5e3bc 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.WorkerThread.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.WorkerThread.cs @@ -25,7 +25,11 @@ private static int SemaphoreSpinCount private static void WorkerThreadStart() { - PortableThreadPoolEventSource.Log.WorkerThreadStart(ThreadCounts.VolatileReadCounts(ref ThreadPoolInstance._separated.counts).numExistingThreads); + PortableThreadPoolEventSource log = PortableThreadPoolEventSource.Log; + if (log.IsEnabled()) + { + log.WorkerThreadStart(ThreadCounts.VolatileReadCounts(ref ThreadPoolInstance._separated.counts).numExistingThreads); + } while (true) { @@ -70,7 +74,11 @@ private static void WorkerThreadStart() if (oldCounts == counts) { HillClimbing.ThreadPoolHillClimber.ForceChange(newCounts.numThreadsGoal, HillClimbing.StateOrTransition.ThreadTimedOut); - PortableThreadPoolEventSource.Log.WorkerThreadStop(newCounts.numExistingThreads); + + if (log.IsEnabled()) + { + log.WorkerThreadStop(newCounts.numExistingThreads); + } return; } } @@ -88,7 +96,11 @@ private static void WorkerThreadStart() /// If this thread was woken up before it timed out. private static bool WaitForRequest() { - PortableThreadPoolEventSource.Log.WorkerThreadWait(ThreadCounts.VolatileReadCounts(ref ThreadPoolInstance._separated.counts).numExistingThreads); + PortableThreadPoolEventSource log = PortableThreadPoolEventSource.Log; + if (log.IsEnabled()) + { + log.WorkerThreadWait(ThreadCounts.VolatileReadCounts(ref ThreadPoolInstance._separated.counts).numExistingThreads); + } return s_semaphore.Wait(ThreadPoolThreadTimeoutMs); } diff --git a/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.cs b/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.cs index 2794e5cf29a..de23cda5e60 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/PortableThreadPool.cs @@ -27,7 +27,7 @@ internal sealed partial class PortableThreadPool private const int CpuUtilizationHigh = 95; private const int CpuUtilizationLow = 80; - private int _cpuUtilization = 0; + private int _cpuUtilization; private static readonly short s_forcedMinWorkerThreads = AppContextConfigHelper.GetInt16Config("System.Threading.ThreadPool.MinThreads", 0, false); private static readonly short s_forcedMaxWorkerThreads = AppContextConfigHelper.GetInt16Config("System.Threading.ThreadPool.MaxThreads", 0, false); @@ -63,7 +63,7 @@ private struct CacheLineSeparated private readonly LowLevelLock _hillClimbingThreadAdjustmentLock = new LowLevelLock(); - private volatile int _numRequestedWorkers = 0; + private volatile int _numRequestedWorkers; private PortableThreadPool() {