public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
From: Andreas Jaeger <aj@suse.de>
To: Glibc hackers <libc-hacker@sources.redhat.com>
Subject: Re: get_nprocs question
Date: Tue, 16 Mar 2004 20:05:00 -0000	[thread overview]
Message-ID: <m3d67ch817.fsf@gromit.moeb> (raw)
In-Reply-To: <m3brmxia3m.fsf@gromit.moeb> (Andreas Jaeger's message of "Tue, 16 Mar 2004 07:23:25 +0100")

[-- Attachment #1: Type: text/plain, Size: 2616 bytes --]

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 #2: Type: application/pgp-signature, Size: 188 bytes --]

  reply	other threads:[~2004-03-16 20:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-16  6:23 Andreas Jaeger
2004-03-16 20:05 ` Andreas Jaeger [this message]
2004-03-16 20:12   ` Ulrich Drepper
2004-03-17  5:33     ` Andreas Jaeger
2004-03-17  5:50       ` Ulrich Drepper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m3d67ch817.fsf@gromit.moeb \
    --to=aj@suse.de \
    --cc=libc-hacker@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).