let sysconf accurately report # of cpus available
authorRich Felker <dalias@aerifal.cx>
Sun, 13 May 2012 04:44:35 +0000 (00:44 -0400)
committerRich Felker <dalias@aerifal.cx>
Sun, 13 May 2012 04:44:35 +0000 (00:44 -0400)
i've been trying out openmp and it seems like it won't be much use
without this...

src/conf/sysconf.c

index 07072ccfdba7209dd1424fcd8bec4786bd808269..d6691df8b0de371ad9b74db1d46860fb069651e6 100644 (file)
@@ -6,6 +6,7 @@
 
 #define VER (-2)
 #define OFLOW (-3)
+#define CPUCNT (-4)
 #define RLIM(x) (-32768|(RLIMIT_ ## x))
 
 long sysconf(int name)
@@ -94,8 +95,8 @@ long sysconf(int name)
                [_SC_THREAD_PRIO_INHERIT] = -1,
                [_SC_THREAD_PRIO_PROTECT] = -1,
                [_SC_THREAD_PROCESS_SHARED] = VER,
-               [_SC_NPROCESSORS_CONF] = -1,
-               [_SC_NPROCESSORS_ONLN] = -1,
+               [_SC_NPROCESSORS_CONF] = CPUCNT,
+               [_SC_NPROCESSORS_ONLN] = CPUCNT,
                [_SC_PHYS_PAGES] = -1,
                [_SC_AVPHYS_PAGES] = -1,
                [_SC_ATEXIT_MAX] = -1,
@@ -220,6 +221,13 @@ long sysconf(int name)
        } else if (values[name] == OFLOW) {
                if (name == _SC_ARG_MAX) return ARG_MAX;
                if (name == _SC_SEM_VALUE_MAX) return SEM_VALUE_MAX;
+       } else if (values[name] == CPUCNT) {
+               unsigned char set[128] = {1};
+               int i, cnt;
+               __syscall(SYS_sched_getaffinity, 0, sizeof set, set);
+               for (i=cnt=0; i<sizeof set; i++)
+                       for (; set[i]; set[i]&=set[i]-1, cnt++);
+               return cnt;
        } else if (values[name] < OFLOW) {
                long lim[2];
                __syscall(SYS_getrlimit, values[name]&16383, lim);