This is the mail archive of the libc-hacker@sources.redhat.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: get_nprocs question


Andreas Jaeger <aj@suse.de> writes:

> What exactly is the semantics of get_nprocs, the manual states:
> The @code{get_nprocs} function returns the number of available processors.
>
> Does available mean the number of all processors the kernel runs on?
> Or does it mean the number of CPUs upon which the process and its
> children will be permitted to execute?
>
> In the latter case we can use the sched_getaffinity system call for
> get_nprocs if it's available.  If this is ok, I'll prepare a patch...

Here's the proposed patch.  Is this ok?

Andreas

2004-03-16  Andreas Jaeger  <aj@suse.de>
            Andi Kleen  <ak@suse.de>

        * sysdeps/unix/sysv/linux/getsysstats.c (popcnt): New.
        (__get_nprocs): Use sched_getaffinity to get number of processors.

============================================================
Index: sysdeps/unix/sysv/linux/getsysstats.c
--- sysdeps/unix/sysv/linux/getsysstats.c	4 Sep 2003 08:25:11 -0000	1.27
+++ sysdeps/unix/sysv/linux/getsysstats.c	16 Mar 2004 20:04:23 -0000
@@ -1,5 +1,5 @@
 /* Determine various system internal values, Linux version.
-   Copyright (C) 1996-2001, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 1996-2001, 2002, 2003, 2004 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
 
@@ -133,6 +133,26 @@ get_proc_path (char *buffer, size_t bufs
   while (0)
 #endif
 
+static unsigned char pop4 [16] =
+{
+  0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
+};
+
+/* Population count: count number of bits set.  */
+static int
+popcnt (unsigned char *buffer, int num)
+{
+  int count = 0;
+  int i;
+  for (i = 0; i < num; i++)
+    {
+      unsigned char w = buffer[i];
+      count += pop4[w >> 4] + pop4[w & 0xF];
+    }
+  return count;
+}
+
+
 int
 __get_nprocs ()
 {
@@ -141,7 +161,17 @@ __get_nprocs ()
   const char *proc_path;
   int result = 1;
 
-  /* XXX Here will come a test for the new system call.  */
+#ifdef __NR_sched_getaffinity
+  cpu_set_t cpuset;
+
+  result = INLINE_SYSCALL (sched_getaffinity, 3, __getpid(), sizeof (cpu_set_t),
+			   &cpuset);
+  if (result != -1)
+    {
+      return popcnt ((unsigned char *) &cpuset, result);
+    }
+
+#endif
 
   /* Get mount point of proc filesystem.  */
   proc_path = get_proc_path (buffer, sizeof buffer);

-- 
 Andreas Jaeger, aj@suse.de, http://www.suse.de/~aj
  SuSE Linux AG, Maxfeldstr. 5, 90409 Nürnberg, Germany
   GPG fingerprint = 93A3 365E CE47 B889 DF7F  FED1 389A 563C C272 A126

Attachment: pgp00000.pgp
Description: PGP signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]