[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
AC_CHECK_FUNCS(getrlimit)
AC_CHECK_FUNCS(prctl)
+ AC_CHECK_FUNCS(sched_getaffinity)
AC_CHECK_FUNCS(sched_setaffinity)
AC_CHECK_FUNCS(sched_getcpu)
#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)
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)
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
+#ifdef HAVE_SCHED_GETAFFINITY
+#include <sched.h>
+#endif
#ifdef HOST_WIN32
#include <windows.h>
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)