Repository navigation
Enable CLR Thread Pool via a MSBuild flag (default enabled on Unix) - #4124
jkoritzinsky wants to merge 44 commits into
Conversation
|
@jkoritzinsky, |
| internal struct SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION | ||
| { | ||
| // These fields are all assigned by the native call. | ||
| #pragma warning disable CS0649 |
There was a problem hiding this comment.
Mark the struct as [StructLayout(LayoutKind.Sequential)]. It will disable the warning as well, but it looks better.
There was a problem hiding this comment.
I didn't know that would disable the warning. Good to know!
| public struct CRITICAL_SECTION | ||
| { | ||
| IntPtr _debugInfo; | ||
| long _lockCount; |
There was a problem hiding this comment.
LONG in Windows C++ does not map to long in C#. LONG in Windows C++ is 32-bit integer, and thus it should be int in C#.
| { | ||
| if (s_frequency == 0) | ||
| { | ||
| Interop.Kernel32.QueryPerformanceFrequency(out s_frequency); |
There was a problem hiding this comment.
This is not thread-safe for multiple reasons:
- Assignment of 64-bit numbers is not atomic on 32-bit platforms
- You do not know whether
QueryPerformanceFrequencyassigns the result just once.
Instead, you should assign it via before field init constructor. Something like:
private static readonly ulong s_frequency = GetFrequency();
It will guarantee atomicity.
…s with CppCodeGen tests
…ported on Windows Thread Pool
…vation as long as there is a requested worker
… can enable on Windows during build (enabled on Unix by default).
e815906 to
03aac74
Compare
| { | ||
| if(s_querySystemInformation == null) | ||
| { | ||
| IntPtr ntDll = LoadLibraryEx("NtDll.dll", IntPtr.Zero, 0); |
There was a problem hiding this comment.
This should pass LOAD_LIBRARY_SEARCH_SYSTEM32 flag instead of 0 - we do not want to be loading any random ntdll.dll laying around.
| _cpuInfo.numberOfProcessors = ThreadPoolGlobals.processorCount; | ||
|
|
||
| _cpuInfo.usageBuffer = new Interop.mincore.SystemProcessorPerformanceInformation[ThreadPoolGlobals.processorCount]; | ||
| GetCpuUtilization(); // Call once to initialize the usage buffer |
There was a problem hiding this comment.
Don't think this is necessary anymore for the purpose noted. It would be better to initialize it in GateThreadStart before the loop, such that every calculation of the CPU utilization would be the value over a 500 ms interval instead of some arbitrary interval.
| } | ||
| s_requested = false; | ||
|
|
||
| ThreadPoolInstance._cpuUtilization = s_cpu.CurrentUtilization; |
There was a problem hiding this comment.
I think this should be done on every iteration of the loop even if there are no pending requests since hill climbing also uses the cpu utilization to make decisions.
| _cpuInfo.userTime = userTime; | ||
| _cpuInfo.idleTime = idleTime; | ||
|
|
||
| if (cpuTotalTime > 0) |
There was a problem hiding this comment.
This check would have caught negative values before when it was signed. Could make cpuTotalTime and cpuBusyTime signed. Should also check that cpuBusyTime >= 0.
|
|
||
| if (cpuTotalTime > 0) | ||
| { | ||
| ulong reading = cpuBusyTime * 100 / cpuTotalTime; |
There was a problem hiding this comment.
Not sure if the values returned by GetSystemTimes are monotonic, but we shouldn't allow values greater than 100, if cpuBusyTime >= cpuTotalTime, reading could be set to 100 and changed to signed long as well.
| if (cpuTotalTime > 0) | ||
| { | ||
| ulong reading = cpuBusyTime * 100 / cpuTotalTime; | ||
| Debug.Assert(0 <= reading && reading <= int.MaxValue); |
There was a problem hiding this comment.
Then this assert could say <= 100
|
|
||
| public CpuUtilizationReader() | ||
| { | ||
| GetCpuUtilization(); // Call once to initialize the usage buffer |
There was a problem hiding this comment.
Don't think this is necessary anymore for the reason indicated. It would be better to initialize this at the top of GateThreadStart before the loop, such that every calculated CPU utilization is a value taken over a 500 ms interval rather than over an arbitrary interval.
There was a problem hiding this comment.
We still need to do this initial read to initialize our data structure. Without it, the first read would give us the CPU utilization since system startup I believe, which is not what we want.
There was a problem hiding this comment.
I was just suggesting to do the first read in GateThreadStart, since that function is the only one that takes any readings. That's sort of what CoreCLR is doing, to ensure that every stored reading is over a 500 ms interval. Otherwise, the first stored reading would give us CPU utilization between process start time and first use of the thread pool.
| // TODO: CoreCLR: Worker Tracking in CoreCLR? (Config name: ThreadPool_EnableWorkerTracking) | ||
| private static void GateThreadStart() | ||
| { | ||
| CpuUtilizationReader cpu = new CpuUtilizationReader(); |
There was a problem hiding this comment.
This could OOM, may need to precreate and call some Initialize method
|
I'm told there are occasional crashes during GC root scan during thread startup, marked as no-merge until that is resolved. |
|
@dotnet-bot test this please |
|
@jkoritzinsky, |
|
@dotnet-bot test this please |
|
I'll rebase and squash this into a new PR, I'll need to revert a change from CoreCLR that was merged into this change as well, I'll do that in a separate commit. Closing for now. |
This PR completes, adds tests for, and enables the CLR Thread Pool on Unix. It also adds Windows implementations for the low level primitives used in the CLR Thread Pool. The thread pool is enabled by setting the MSBuild property FeaturePortableThreadPool to true, which is the default on Unix. Supersedes #4039. Rebased and squashed replacement for original PR #4124.
This PR adds tests for the CLR Thread Pool. It also adds Windows implementations for the low level primitives used in the CLR Thread Pool.
The thread pool is enabled by setting the MSBuild property
FeaturePortableThreadPooltotrue, which is the default on Unix.Supersedes #4039.
@kouvel @sergiy-k @AntonLapounov @jkotas
Note: This is tagged * NO MERGE * because it intermittently exposes a GC crash in root scan during thread setup. That bug is being tracked by #4214.