From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11266 invoked by alias); 16 Mar 2004 20:05:43 -0000 Mailing-List: contact libc-hacker-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-hacker-owner@sources.redhat.com Received: (qmail 11246 invoked from network); 16 Mar 2004 20:05:43 -0000 Received: from unknown (HELO Cantor.suse.de) (195.135.220.2) by sources.redhat.com with SMTP; 16 Mar 2004 20:05:43 -0000 Received: from hermes.suse.de (Hermes.suse.de [195.135.221.8]) (using TLSv1 with cipher EDH-RSA-DES-CBC3-SHA (168/168 bits)) (No client certificate requested) by Cantor.suse.de (Postfix) with ESMTP id A5CCD308F66 for ; Tue, 16 Mar 2004 21:05:42 +0100 (CET) Received: from aj by arthur.inka.de with local (Exim 4.22) id 1B3Kom-0005ov-At for libc-hacker@sources.redhat.com; Tue, 16 Mar 2004 21:05:40 +0100 To: Glibc hackers Subject: Re: get_nprocs question References: From: Andreas Jaeger Date: Tue, 16 Mar 2004 20:05:00 -0000 In-Reply-To: (Andreas Jaeger's message of "Tue, 16 Mar 2004 07:23:25 +0100") Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Rational FORTRAN, linux) MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha1; protocol="application/pgp-signature" X-SW-Source: 2004-03/txt/msg00085.txt.bz2 --=-=-= Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Content-length: 2690 Andreas Jaeger 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 Andi Kleen * sysdeps/unix/sysv/linux/getsysstats.c (popcnt): New. (__get_nprocs): Use sched_getaffinity to get number of processors. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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 , 1996. =20 @@ -133,6 +133,26 @@ get_proc_path (char *buffer, size_t bufs while (0) #endif =20 +static unsigned char pop4 [16] =3D +{ + 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 =3D 0; + int i; + for (i =3D 0; i < num; i++) + { + unsigned char w =3D buffer[i]; + count +=3D pop4[w >> 4] + pop4[w & 0xF]; + } + return count; +} + + int __get_nprocs () { @@ -141,7 +161,17 @@ __get_nprocs () const char *proc_path; int result =3D 1; =20 - /* XXX Here will come a test for the new system call. */ +#ifdef __NR_sched_getaffinity + cpu_set_t cpuset; + + result =3D INLINE_SYSCALL (sched_getaffinity, 3, __getpid(), sizeof (cpu= _set_t), + &cpuset); + if (result !=3D -1) + { + return popcnt ((unsigned char *) &cpuset, result); + } + +#endif =20 /* Get mount point of proc filesystem. */ proc_path =3D get_proc_path (buffer, sizeof buffer); --=20 Andreas Jaeger, aj@suse.de, http://www.suse.de/~aj SuSE Linux AG, Maxfeldstr. 5, 90409 N=FCrnberg, Germany GPG fingerprint =3D 93A3 365E CE47 B889 DF7F FED1 389A 563C C272 A126 --=-=-= Content-Type: application/pgp-signature Content-length: 188 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQBAV14UOJpWPMJyoSYRAnN6AJ9otNAfVkZ1gQVrMER/5q4RJ2jaZACeIOaG ceqk1lN7pHQv1LVuwtcmudE= =htHI -----END PGP SIGNATURE----- --=-=-=--