Improve detection of CPU limits when running inside a Container
authorLudovic Henry <luhenry@microsoft.com>
Thu, 21 Mar 2019 21:37:14 +0000 (14:37 -0700)
committerLudovic Henry <luhenry@microsoft.com>
Fri, 22 Mar 2019 20:09:07 +0000 (13:09 -0700)
This focuse on better supporting `--cpuset-cpus` which limits the number of processors we have access to on the CPU; it also specifies which specific processor we have access to, but that’s irrelevant here

The work has been done here for all runtime components except `Environment.ProcessorCount`. The work consist in fixing `PAL_GetLogicalCpuCountFromOS` to use `sched_getaffinity`.

Fixes https://github.com/dotnet/coreclr/issues/22302

src/pal/src/misc/sysinfo.cpp

index 495cc8b..2c14949 100644 (file)
@@ -102,7 +102,17 @@ PAL_GetLogicalCpuCountFromOS()
 {
     int nrcpus = 0;
 
-#if HAVE_SYSCONF
+#if HAVE_SCHED_GETAFFINITY
+
+    cpu_set_t cpuSet;
+    int st = sched_getaffinity(0, sizeof(cpu_set_t), &cpuSet);
+    if (st != 0)
+    {
+        ASSERT("sched_getaffinity failed (%d)\n", errno);
+    }
+
+    nrcpus = CPU_COUNT(&cpuSet);
+#elif HAVE_SYSCONF
 
 #if defined(_ARM_) || defined(_ARM64_)
 #define SYSCONF_GET_NUMPROCS       _SC_NPROCESSORS_CONF