diff options
author | Koundinya Veluri <kouvel@users.noreply.github.com> | 2021-01-25 23:21:48 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-25 23:21:48 +0300 |
commit | e42de0e2decb1df9e8dae6a063054ead2510a3f0 (patch) | |
tree | 962a64de7973c67eea7cb9474e1312e2356db901 /src/coreclr/classlibnative | |
parent | 6959d9850526ec5a17fdd107bb15b37663456d9e (diff) |
Update Environment.ProcessorCount on Windows to take into account the processor affinity mask (#45943)
- Similarly to cases on Unixes where sched_getaffinity is available
- If `GCCpuGroup` and `Thread_UseAllCpuGroups` are both enabled, I'm not sure if the `CPUGroupInfo` count of active processors takes affinity into account as the docs are not clear, for now I'm not modifying that path until I can verify it
- Otherwise, a process that is started with a specific processor affinity mask still shows full CPU count
- This is one of the differences in the portable managed thread pool implementation, which relies on Environment.ProcessorCount, as opposed to the native thread pool, which uses the affinity mask
- After this change, in affinitized cases on Windows the behavior is consistent perf-wise with Linux in similar situations:
- The portable thread pool uses the same worker thread count as the native thread pool
- `Environment.ProcessorCount` returns the number of processors the the process is affinitized to, which may be less than it would have returned before
Diffstat (limited to 'src/coreclr/classlibnative')
-rw-r--r-- | src/coreclr/classlibnative/bcltype/system.cpp | 22 |
1 files changed, 6 insertions, 16 deletions
diff --git a/src/coreclr/classlibnative/bcltype/system.cpp b/src/coreclr/classlibnative/bcltype/system.cpp index 91a39ef9d08..2bcf2d56990 100644 --- a/src/coreclr/classlibnative/bcltype/system.cpp +++ b/src/coreclr/classlibnative/bcltype/system.cpp @@ -185,29 +185,19 @@ INT32 QCALLTYPE SystemNative::GetProcessorCount() #ifndef TARGET_UNIX CPUGroupInfo::EnsureInitialized(); - if(CPUGroupInfo::CanEnableThreadUseAllCpuGroups()) + if (CPUGroupInfo::CanEnableThreadUseAllCpuGroups()) { processorCount = CPUGroupInfo::GetNumActiveProcessors(); } + else #endif // !TARGET_UNIX - // Processor count will be 0 if CPU groups are disabled/not supported - if(processorCount == 0) { - SYSTEM_INFO systemInfo; - ZeroMemory(&systemInfo, sizeof(systemInfo)); - - GetSystemInfo(&systemInfo); - - processorCount = systemInfo.dwNumberOfProcessors; + // This similar to GetSystemInfo() + dwNumberOfProcessors except: + // - GetCurrentProcessCpuCount() tries to take into account the processor affinity mask where applicable + // - GetCurrentProcessCpuCount() on Unixes tries to take into account cgroups CPU quota limits where applicable + processorCount = GetCurrentProcessCpuCount(); } -#ifdef TARGET_UNIX - uint32_t cpuLimit; - - if (PAL_GetCpuLimit(&cpuLimit) && cpuLimit < (uint32_t)processorCount) - processorCount = cpuLimit; -#endif - END_QCALL; return processorCount; |