From: Paul Jackson <pj@sgi.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: drepper@redhat.com, linux-kernel@vger.kernel.org
Subject: Re: getting processor numbers
Date: Tue, 3 Apr 2007 19:04:58 -0700 [thread overview]
Message-ID: <20070403190458.18165884.pj@sgi.com> (raw)
In-Reply-To: <20070403131623.c6831607.akpm@linux-foundation.org>
Andrew wrote:
> I'd have thought that in general an application should be querying its
> present affinity mask - something like sched_getaffinity()? That fixes the
> CPU hotplug issues too, of course.
The sched_getaffinity call is quick, too, and it nicely reflects any
cpuset constraints, while still working on kernels which don't have
CPUSETs configured.
There are really at least four "number of CPUs" answers here, and we
should be aware of which we are providing. There are, in order of
decreasing size:
1) the size of the kernels cpumask_t (NR_CPUS),
2) the maximum number of CPUs that might ever be hotplugged into a
booted system,
3) the current number of CPUs online in that system, and
4) the number of CPUs that the current task is allowed to use.
I would suggest that (4) is what we should typically return.
Certainly it would seem that the use that Ulrich is concerned with,
by OpenMP, wants (4).
Currently, the sysconf(_SC_NPROCESSORS_CONF) returns (3), by counting
the CPUs in /proc/stat, which is rather bogus on cpuset, or even
sched_setaffinity, constrained systems.
> But we discussed this all a couple years back and it was decided that
> sched_getaffinity() was unsuitable. I remember at the time not really
> understanding why?
Perhaps it was because a robust invocation of sched_getaffinity takes
a page of code to write, as Andi Kleen noticed in his libnuma coding of
"static int number_of_cpus(void)". One has to size the mask passed in,
in case the kernel was compiled with a larger cpumask_t size than you
guessed up front. In other words, to get (4) using sched_getaffinity,
one first needs an upper bound on (1), above, the kernels configured
NR_CPUS.
One can either size it by repeatedly invoking sched_getaffinity with
larger masks until it stops failing EINVAL, or one can examine the
length of the "Cpus_allowed" mask displayed in /proc/self/status
(it takes 9 ascii chars, if you include the commas and trailing
newline, to display each 32 bits of cpumask_t.) There may be other
ways as well; those seem to be the most common.
At least the kernel cpumask_t size can be cached for the life of the
process and any descendents, so the cost of obtaining it should be less
critical.
--
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <pj@sgi.com> 1.925.600.0401
next prev parent reply other threads:[~2007-04-04 2:05 UTC|newest]
Thread overview: 56+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-03 16:54 getting processor numbers Ulrich Drepper
2007-04-03 17:30 ` linux-os (Dick Johnson)
2007-04-03 17:37 ` Ulrich Drepper
2007-04-03 17:56 ` Dr. David Alan Gilbert
2007-04-03 18:11 ` Andi Kleen
2007-04-03 17:17 ` Ulrich Drepper
2007-04-03 17:22 ` Alan Cox
2007-04-03 17:30 ` Andi Kleen
2007-04-03 20:24 ` Jeremy Fitzhardinge
2007-04-03 17:27 ` Andi Kleen
2007-04-03 17:30 ` Ulrich Drepper
2007-04-03 17:35 ` Andi Kleen
2007-04-03 17:45 ` Ulrich Drepper
2007-04-03 17:58 ` Andi Kleen
2007-04-03 18:05 ` Ulrich Drepper
2007-04-03 18:11 ` Andi Kleen
2007-04-03 18:21 ` Ulrich Drepper
2007-04-03 17:44 ` Siddha, Suresh B
2007-04-03 17:59 ` Ulrich Drepper
2007-04-03 19:40 ` Jakub Jelinek
2007-04-03 20:13 ` Ingo Oeser
2007-04-03 23:38 ` J.A. Magallón
2007-04-03 19:55 ` Ulrich Drepper
2007-04-03 20:13 ` Siddha, Suresh B
2007-04-03 20:19 ` Ulrich Drepper
2007-04-03 20:32 ` Eric Dumazet
2007-04-03 20:20 ` Nathan Lynch
2007-04-03 19:15 ` Davide Libenzi
2007-04-03 19:32 ` Ulrich Drepper
2007-04-04 0:31 ` H. Peter Anvin
2007-04-04 0:35 ` Jeremy Fitzhardinge
2007-04-04 0:38 ` H. Peter Anvin
2007-04-04 5:09 ` Eric Dumazet
2007-04-04 5:16 ` H. Peter Anvin
2007-04-04 5:22 ` Jeremy Fitzhardinge
2007-04-04 5:40 ` H. Peter Anvin
2007-04-04 5:46 ` Eric Dumazet
2007-04-04 5:29 ` Eric Dumazet
2007-04-03 20:16 ` Andrew Morton
[not found] ` <4612BB89.8040102@redhat.com>
[not found] ` <20070403141348.9bcdb13e.akpm@linux-foundation.org>
2007-04-03 22:13 ` Ulrich Drepper
2007-04-03 22:48 ` Andrew Morton
2007-04-03 23:00 ` Ulrich Drepper
2007-04-03 23:23 ` Andrew Morton
2007-04-03 23:54 ` Ulrich Drepper
2007-04-04 2:55 ` Paul Jackson
2007-04-04 8:39 ` Oleg Nesterov
2007-04-04 9:39 ` Ingo Molnar
2007-04-04 8:57 ` Oleg Nesterov
2007-04-04 10:01 ` Ingo Molnar
2007-04-04 2:58 ` Paul Jackson
2007-04-04 3:04 ` Paul Jackson
2007-04-04 2:52 ` Paul Jackson
2007-04-04 2:04 ` Paul Jackson [this message]
2007-04-04 6:47 ` Jakub Jelinek
2007-04-04 7:02 ` Paul Jackson
2007-04-04 14:51 ` Cliff Wickman
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=20070403190458.18165884.pj@sgi.com \
--to=pj@sgi.com \
--cc=akpm@linux-foundation.org \
--cc=drepper@redhat.com \
--cc=linux-kernel@vger.kernel.org \
/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