Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
da29c61
Add dependencies
Mar 4, 2020
423d830
Add config var
Mar 4, 2020
ad228ef
Move portable RegisteredWaitHandle implementation to shared ThreadPoo…
Mar 9, 2020
1155fb0
Merge RegisteredWaitHandle implementations
Mar 4, 2020
6f9b3dc
Separate portable-only portion of RegisteredWaitHandle
May 24, 2020
d792cd7
Fix timers, tiered compilation, introduced time-sensitive work item q…
Mar 15, 2020
a911883
Implement ResetThreadPoolThread, set thread names for diagnostics
Mar 15, 2020
40ce9d0
Cache-line-separate PortableThreadPool._numRequestedWorkers similarly…
Mar 16, 2020
a5d3c2b
Post wait completions to the IO completion port on Windows for corecl…
Mar 17, 2020
ad632be
Reroute managed gate thread into unmanaged side to perform gate activ…
Mar 18, 2020
f942773
Flow config values from CoreCLR to the portable thread pool for compat
Mar 22, 2020
0ef2079
Port - 44970522045f0c323f5735c4fe4b54bd8f71e800 - Fix hill climbing f…
Mar 23, 2020
3151057
Port - aa5ce2b1bc9ac553ddd493e269a53c999d61e965 - Limit min threads i…
Mar 23, 2020
5d8d4ae
Port - 8cc2aa35933677339b9e9ec5485754aa750907df - Optimize AdjustMaxW…
Mar 24, 2020
3916619
Fix ETW events
Mar 25, 2020
88ea9c4
Fix perf of counts structs
Mar 27, 2020
8ef63f9
Fix perf of dispatch loop
Mar 28, 2020
e01df4f
Fix perf of ThreadInt64PersistentCounter
Mar 29, 2020
7c89f81
Miscellaneous perf fixes
Apr 1, 2020
13c2d62
Fix starvation heuristic
Jun 1, 2020
dd23472
Implement worker tracking
May 23, 2020
6cd2b2d
Use smaller stack size for threads that don't run user code
May 27, 2020
c8bdfad
Note some SOS dependencies, small fixes in hill climbing to make equi…
Jun 1, 2020
38c00d5
Port some tests from CoreRT
Jun 7, 2020
0ff3b03
Fail-fast in thread pool native entry points specific to thread pool …
Jun 7, 2020
cc35f92
Fix SetMinThreads() and SetMaxThreads() to return true only when both…
Jun 8, 2020
79eb3b9
Fix registered wait removals for fairness since there can be duplicat…
Jun 9, 2020
8b309cc
Allow multiple DotNETRuntime event providers/sources in EventPipe
Jun 12, 2020
ac1917d
Fix registered wait handle timeout logic in the wait thread
Jun 16, 2020
73f911c
Fix Browser build
Jun 22, 2020
1b99da7
Remove unnecessary methods from Browser thread pool, address some fee…
Jun 24, 2020
17b3d2d
Fix build warning after merge
Jun 30, 2020
e8043ff
Address feedback for events, undo EventPipe change in mono, change ev…
Jun 30, 2020
8d9cc2d
Disable new timer test for browser
Jul 1, 2020
f03709b
Fix EventSource tests to expect the new provider name used in mono
Jul 1, 2020
ff48985
Revert event source name in mono to match coreclr
Jul 3, 2020
93563e6
Add issue link for prerequisites of enabling the portable thread pool…
Jul 3, 2020
100dad7
Address feedback
Jul 6, 2020
4c7f778
Fix race condition in registered wait unregister
Jul 11, 2020
d55c5fc
Fix a new issue in WaitThread after shifting items in arrays
Jul 12, 2020
996597f
Fix usage of LowLevelMonitor after rebase
Sep 10, 2020
82f7cda
Fix tests that use RemoteExecutor to include a test for whether it is…
Sep 11, 2020
b53a5b9
Fix build
Sep 11, 2020
7dcb267
Slight fix to starvation heuristic to be closer to what it was before
Sep 25, 2020
65840d8
Fix license headers in new files
Oct 9, 2020
fb28ad2
Address feedback
Oct 9, 2020
04326b6
Change pattern for accessing the event source for better ILLinker com…
Oct 16, 2020
ec038cf
Move hill climbing config reads into constructor instead of passing t…
Oct 16, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add config var
  • Loading branch information
Koundinya Veluri committed Oct 20, 2020
commit 423d83034cb3b42fa0656773e08a4eb0f4c5d455
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ILLinkTrimXml>$(IntermediateOutputPath)System.Private.CoreLib.xml</ILLinkTrimXml>
<ILLinkDirectory>$(MSBuildThisFileDirectory)src\ILLink\</ILLinkDirectory>

<FeaturePortableThreadPool>true</FeaturePortableThreadPool>
<FeatureSharedLowLevelLock>true</FeatureSharedLowLevelLock>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,45 +193,131 @@ public static partial class ThreadPool
// Time in ms for which ThreadPoolWorkQueue.Dispatch keeps executing work items before returning to the OS
private const uint DispatchQuantum = 30;

internal static readonly bool UsePortableThreadPool = InitializeConfigAndDetermineUsePortableThreadPool();

internal static readonly bool EnableWorkerTracking = GetEnableWorkerTracking();

internal static bool KeepDispatching(int startTickCount)
{
if (UsePortableThreadPool)
{
return true;
}

// Note: this function may incorrectly return false due to TickCount overflow
// if work item execution took around a multiple of 2^32 milliseconds (~49.7 days),
// which is improbable.
return (uint)(Environment.TickCount - startTickCount) < DispatchQuantum;
}

private static unsafe bool InitializeConfigAndDetermineUsePortableThreadPool()
{
bool usePortableThreadPool = false;
int configVariableIndex = 0;
while (true)
{
int nextConfigVariableIndex =
GetNextConfigUInt32Value(
configVariableIndex,
out uint configValue,
out bool isBoolean,
out char* appContextConfigNameUnsafe);
if (nextConfigVariableIndex < 0)
{
break;
}

Debug.Assert(nextConfigVariableIndex > configVariableIndex);
configVariableIndex = nextConfigVariableIndex;

if (appContextConfigNameUnsafe == null)
{
// Special case for UsePortableThreadPool, which doesn't go into the AppContext
Debug.Assert(configValue != 0);
Debug.Assert(isBoolean);
usePortableThreadPool = true;
continue;
}

var appContextConfigName = new string(appContextConfigNameUnsafe);
if (isBoolean)
{
AppContext.SetSwitch(appContextConfigName, configValue != 0);
}
else
{
AppContext.SetData(appContextConfigName, configValue);
}
}

return usePortableThreadPool;
}

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern unsafe int GetNextConfigUInt32Value(
int configVariableIndex,
out uint configValue,
out bool isBoolean,
out char* appContextConfigName);

public static bool SetMaxThreads(int workerThreads, int completionPortThreads)
{
return
workerThreads >= 0 &&
completionPortThreads >= 0 &&
SetMaxThreadsNative(workerThreads, completionPortThreads);
if (workerThreads < 0 || completionPortThreads < 0)
{
return false;
}

if (UsePortableThreadPool && !PortableThreadPool.ThreadPoolInstance.SetMaxThreads(workerThreads))
{
return false;
}

return SetMaxThreadsNative(workerThreads, completionPortThreads);
}

public static void GetMaxThreads(out int workerThreads, out int completionPortThreads)
{
GetMaxThreadsNative(out workerThreads, out completionPortThreads);

if (UsePortableThreadPool)
{
workerThreads = PortableThreadPool.ThreadPoolInstance.GetMaxThreads();
}
}

public static bool SetMinThreads(int workerThreads, int completionPortThreads)
{
return
workerThreads >= 0 &&
completionPortThreads >= 0 &&
SetMinThreadsNative(workerThreads, completionPortThreads);
if (workerThreads < 0 || completionPortThreads < 0)
{
return false;
}

if (UsePortableThreadPool && !PortableThreadPool.ThreadPoolInstance.SetMinThreads(workerThreads))
{
return false;
}

return SetMinThreadsNative(workerThreads, completionPortThreads);
}

public static void GetMinThreads(out int workerThreads, out int completionPortThreads)
{
GetMinThreadsNative(out workerThreads, out completionPortThreads);

if (UsePortableThreadPool)
{
workerThreads = PortableThreadPool.ThreadPoolInstance.GetMinThreads();
}
}

public static void GetAvailableThreads(out int workerThreads, out int completionPortThreads)
{
GetAvailableThreadsNative(out workerThreads, out completionPortThreads);

if (UsePortableThreadPool)
{
workerThreads = PortableThreadPool.ThreadPoolInstance.GetAvailableThreads();
}
}

/// <summary>
Expand All @@ -240,28 +326,38 @@ public static void GetAvailableThreads(out int workerThreads, out int completion
/// <remarks>
/// For a thread pool implementation that may have different types of threads, the count includes all types.
/// </remarks>
public static extern int ThreadCount
{
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
public static int ThreadCount =>
(UsePortableThreadPool ? PortableThreadPool.ThreadPoolInstance.ThreadCount : 0) + GetThreadCount();

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern int GetThreadCount();

/// <summary>
/// Gets the number of work items that have been processed so far.
/// </summary>
/// <remarks>
/// For a thread pool implementation that may have different types of work items, the count includes all types.
/// </remarks>
public static long CompletedWorkItemCount => GetCompletedWorkItemCount();
public static long CompletedWorkItemCount
{
get
{
long count = GetCompletedWorkItemCount();
if (UsePortableThreadPool)
{
count += PortableThreadPool.ThreadPoolInstance.CompletedWorkItemCount;
}
return count;
}
}

[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
private static extern long GetCompletedWorkItemCount();

private static extern long PendingUnmanagedWorkItemCount
{
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
private static long PendingUnmanagedWorkItemCount => UsePortableThreadPool ? 0 : GetPendingUnmanagedWorkItemCount();

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern long GetPendingUnmanagedWorkItemCount();

private static RegisteredWaitHandle RegisterWaitForSingleObject(
WaitHandle waitObject,
Expand Down Expand Up @@ -295,8 +391,19 @@ bool compressStack
return registeredWaitHandle;
}

internal static void RequestWorkerThread()
{
if (UsePortableThreadPool)
{
PortableThreadPool.ThreadPoolInstance.RequestWorker();
return;
}

RequestWorkerThreadNative();
}

[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
internal static extern Interop.BOOL RequestWorkerThread();
private static extern Interop.BOOL RequestWorkerThreadNative();

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern unsafe bool PostQueuedCompletionStatus(NativeOverlapped* overlapped);
Expand All @@ -322,19 +429,46 @@ public static unsafe bool UnsafeQueueNativeOverlapped(NativeOverlapped* overlapp
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void GetAvailableThreadsNative(out int workerThreads, out int completionPortThreads);

internal static bool NotifyWorkItemComplete()
{
if (UsePortableThreadPool)
{
return PortableThreadPool.ThreadPoolInstance.NotifyWorkItemComplete();
}

return NotifyWorkItemCompleteNative();
}

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool NotifyWorkItemComplete();
private static extern bool NotifyWorkItemCompleteNative();

internal static void ReportThreadStatus(bool isWorking)
{
if (UsePortableThreadPool)
{
// TODO: PortableThreadPool - Implement worker tracking
return;
}

ReportThreadStatusNative(isWorking);
}

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void ReportThreadStatus(bool isWorking);
private static extern void ReportThreadStatusNative(bool isWorking);

internal static void NotifyWorkItemProgress()
{
if (UsePortableThreadPool)
{
PortableThreadPool.ThreadPoolInstance.NotifyWorkItemComplete();
return;
}

NotifyWorkItemProgressNative();
}

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void NotifyWorkItemProgressNative();
private static extern void NotifyWorkItemProgressNative();

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool GetEnableWorkerTracking();
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/src/inc/clrconfigvalues.h
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ RETAIL_CONFIG_DWORD_INFO(EXTERNAL_Thread_AssignCpuGroups, W("Thread_AssignCpuGro
///
/// Threadpool
///
RETAIL_CONFIG_DWORD_INFO(INTERNAL_ThreadPool_UsePortableThreadPool, W("ThreadPool_UsePortableThreadPool"), 0, "Uses the managed portable thread pool implementation instead of the unmanaged one.")
RETAIL_CONFIG_DWORD_INFO(INTERNAL_ThreadPool_ForceMinWorkerThreads, W("ThreadPool_ForceMinWorkerThreads"), 0, "Overrides the MinThreads setting for the ThreadPool worker pool")
RETAIL_CONFIG_DWORD_INFO(INTERNAL_ThreadPool_ForceMaxWorkerThreads, W("ThreadPool_ForceMaxWorkerThreads"), 0, "Overrides the MaxThreads setting for the ThreadPool worker pool")
RETAIL_CONFIG_DWORD_INFO(INTERNAL_ThreadPool_DisableStarvationDetection, W("ThreadPool_DisableStarvationDetection"), 0, "Disables the ThreadPool feature that forces new threads to be added when workitems run for too long")
Expand All @@ -581,8 +582,6 @@ RETAIL_CONFIG_DWORD_INFO(INTERNAL_ThreadPool_UnfairSemaphoreSpinLimit, W("Thread
RETAIL_CONFIG_DWORD_INFO(INTERNAL_ThreadPool_UnfairSemaphoreSpinLimit, W("ThreadPool_UnfairSemaphoreSpinLimit"), 0x46, "Maximum number of spins a thread pool worker thread performs before waiting for work")
#endif // TARGET_ARM64

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, "");
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/src/vm/ceemain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@
#include "stacksampler.h"
#endif

#ifndef CROSSGEN_COMPILE
#include "win32threadpool.h"
#endif

#include <shlwapi.h>

#include "bbsweep.h"
Expand Down Expand Up @@ -674,6 +678,8 @@ void EEStartupHelper()
// This needs to be done before the EE has started
InitializeStartupFlags();

ThreadpoolMgr::StaticInitialize();

MethodDescBackpatchInfoTracker::StaticInitialize();
CodeVersionManager::StaticInitialize();
TieredCompilationManager::StaticInitialize();
Expand Down
Loading