Merge pull request #2722 from ludovic-henry/fix-39279-docker-cpu-count
authormonojenkins <jo.shields+jenkins@xamarin.com>
Fri, 4 Mar 2016 18:00:30 +0000 (18:00 +0000)
committermonojenkins <jo.shields+jenkins@xamarin.com>
Fri, 4 Mar 2016 18:00:30 +0000 (18:00 +0000)
[utils] Get processor count from scheduler affinity if available

In case the user has set a scheduler CPU affinity, we should use it as the number of available CPU for the runtime. This is particularly important on docket which uses cgroups that set the container a cpu affinity, and by not using that, the runtime report the total number of CPU the machine has, and not the number of CPU the container has.

Fixes bug https://bugzilla.xamarin.com/show_bug.cgi?id=39279

configure.ac
mono/profiler/proflog.c
mono/utils/mono-proclib.c

index 227cc1f6a57a572254c1191660e16ab81886aafc..7eed7e189e293475bbfb3f62f64a8118c077f73e 100644 (file)
@@ -1156,6 +1156,7 @@ if test x$host_win32 = xno; then
        AC_CHECK_FUNCS(getrlimit)
        AC_CHECK_FUNCS(prctl)
 
+       AC_CHECK_FUNCS(sched_getaffinity)
        AC_CHECK_FUNCS(sched_setaffinity)
        AC_CHECK_FUNCS(sched_getcpu)
 
index b75bced23f79388008484c110c1f38899ad76cdb..3160bbe228f6dbed32ea8e9d4fd86b0daf1f5b50 100644 (file)
@@ -33,6 +33,9 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#ifdef HAVE_SCHED_GETAFFINITY
+#include <sched.h>
+#endif
 #include <fcntl.h>
 #include <errno.h>
 #if defined(HOST_WIN32) || defined(DISABLE_SOCKETS)
@@ -2513,6 +2516,13 @@ mono_cpu_count (void)
        if (count > 0)
                return count + 1;
 #endif
+#ifdef HAVE_SCHED_GETAFFINITY
+       {
+               cpu_set_t set;
+               if (sched_getaffinity (getpid (), sizeof (set), &set) == 0)
+                       return CPU_COUNT (&set);
+       }
+#endif
 #ifdef _SC_NPROCESSORS_ONLN
        count = sysconf (_SC_NPROCESSORS_ONLN);
        if (count > 0)
index 9bf6ca52ae0188a5f93badde282e87aa21ef4510..63a56324ba755c45ff024664b02bfd354156f075 100644 (file)
@@ -14,6 +14,9 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#ifdef HAVE_SCHED_GETAFFINITY
+#include <sched.h>
+#endif
 
 #ifdef HOST_WIN32
 #include <windows.h>
@@ -639,6 +642,13 @@ mono_cpu_count (void)
        if (count > 0)
                return count + 1;
 #endif
+#ifdef HAVE_SCHED_GETAFFINITY
+       {
+               cpu_set_t set;
+               if (sched_getaffinity (mono_process_current_pid (), sizeof (set), &set) == 0)
+                       return CPU_COUNT (&set);
+       }
+#endif
 #ifdef _SC_NPROCESSORS_CONF
        count = sysconf (_SC_NPROCESSORS_CONF);
        if (count > 0)