Use sched_getaffinity instead of /proc parsing for _SC_NPROCESSORS_ONLN

"Indan Zupancic" <[email protected]> Mon, 6 Dec 2010 12:09:50 +0100 (CET)
Newsgroups gmane.linux.lib.dietlibc
Message-ID <[email protected]>
Hello,

In my affinity patch I accidentally added the _HAVE_LINUX26 bit,
but that should be here. I assume dietlibc supports Linux 2.4,
if not this option is redundant and sysconf_cpus.c can be removed
altogether.

This has the same max_nr_cpus == system bits limit (32/64) as the
CPU_SET macros, but it's easy to increase if that's ever needed.

Regards,

Indan


Index: dietfeatures.h
===================================================================
RCS file: /cvs/dietlibc/dietfeatures.h,v
retrieving revision 1.69
diff -u -p -b -B -w -r1.69 dietfeatures.h
--- dietfeatures.h	1 Aug 2010 19:56:21 -0000	1.69
+++ dietfeatures.h	6 Dec 2010 10:58:17 -0000
@@ -22,6 +22,12 @@
 /* this is only for meaningful for ttyname and sysconf_cpus so far */
 #define SLASH_PROC_OK

+/*
+ * Only meaningful for sysconf_cpus so far.
+ * If set, you can unset SLASH_PROC_OK for sysconf(_SC_NPROCESSORS_ONLN).
+ */
+#define _HAVE_LINUX26
+
 /* use errno_location instead of errno; NEEDED FOR MULTI-THREADING! */
 #define WANT_THREAD_SAFE

Index: libcruft/sysconf.c
===================================================================
RCS file: /cvs/dietlibc/libcruft/sysconf.c,v
retrieving revision 1.7
diff -u -p -b -B -w -r1.7 sysconf.c
--- libcruft/sysconf.c	15 May 2009 01:11:58 -0000	1.7
+++ libcruft/sysconf.c	6 Dec 2010 10:58:18 -0000
@@ -3,6 +3,12 @@
 #include <limits.h>
 #include <sys/resource.h>
 #include <fcntl.h>
+#include "dietfeatures.h"
+
+#ifdef _HAVE_LINUX26
+#define _GNU_SOURCE
+#include <sched.h>
+#endif

 extern int __sc_nr_cpus();

@@ -63,8 +69,16 @@ long sysconf(int name)
     return NGROUPS_MAX;

   case _SC_NPROCESSORS_ONLN:
+#ifndef _HAVE_LINUX26
     return __sc_nr_cpus();
-
+#else
+    {
+    cpu_set_t m;
+    if (sched_getaffinity(0, sizeof(m), &m))
+        return 1;
+    return CPU_COUNT(&m);
+    }
+#endif
   }
   errno=ENOSYS;
   return -1;
Index: libcruft/sysconf_cpus.c
===================================================================
RCS file: /cvs/dietlibc/libcruft/sysconf_cpus.c,v
retrieving revision 1.8
diff -u -p -b -B -w -r1.8 sysconf_cpus.c
--- libcruft/sysconf_cpus.c	23 Nov 2010 03:28:45 -0000	1.8
+++ libcruft/sysconf_cpus.c	6 Dec 2010 10:58:18 -0000
@@ -16,6 +16,7 @@
  * default	->	processor\t: <cpunr>\n	(one per cpu)
  */

+#ifndef _HAVE_LINUX26
 #ifdef SLASH_PROC_OK
 int __sc_nr_cpus(void);
 int __sc_nr_cpus() {
@@ -63,3 +64,4 @@ int __sc_nr_cpus() {
   return 1;	/* kludge kludge ;-) */
 }
 #endif
+#endif