This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: What *is* the API for sched_getaffinity? Should sched_getaffinity always succeed when using cpu_set_t?
- From: KOSAKI Motohiro <kosaki dot motohiro at gmail dot com>
- To: Chris Metcalf <cmetcalf at tilera dot com>
- Cc: kosaki dot motohiro at gmail dot com, Carlos O'Donell <carlos at redhat dot com>, libc-alpha <libc-alpha at sourceware dot org>
- Date: Mon, 22 Jul 2013 17:11:55 -0400
- Subject: Re: What *is* the API for sched_getaffinity? Should sched_getaffinity always succeed when using cpu_set_t?
- References: <51E42BFE dot 7000301 at redhat dot com> <51E4A0BB dot 2070802 at gmail dot com> <51E4A123 dot 9070001 at gmail dot com> <51E6F3ED dot 8000502 at redhat dot com> <51E6F956 dot 5050902 at gmail dot com> <51E714DE dot 6060802 at redhat dot com> <CAHGf_=oZW3kNA3V-9u+BZNs3tL3JKCsO2a0Q6f0iJzo=N4Wb8w at mail dot gmail dot com> <51E7B205 dot 3060905 at redhat dot com> <51E7EC90 dot 7020707 at tilera dot com>
(7/18/13 9:24 AM), Chris Metcalf wrote:
On 7/18/2013 5:14 AM, Carlos O'Donell wrote:
(1) Should glibc's sched_getaffinity never fail?
I think as long as no "one" bits would be set beyond 1024, it's preferable for the lglibc API not to fail, regardless of how many cpus the kernel is configured with.
I tend to agree. So, I'd like to sort out how to write application code. Does this cross your
mind to get cpu mask?
cpu_set_t *mask;
size_t size;
int i;
int nrcpus = 1024;
ret = sysconf(_SC_NPROCESSORS_MAX_NP);
if (ret > 0) {
nrcpus = ret;
}
realloc:
mask = CPU_ALLOC(nrcpus);
size = CPU_ALLOC_SIZE(nrcpus);
CPU_ZERO_S(size, mask);
ret = sched_getaffinity(0, size, mask)
if (ret == -1) {
CPU_FREE(mask);
/* Even if nrcpus was got from sysconf(), we need to retry
because kernel 2.6.33 or earlier use CONFIG_NR_CPUS compile
time constant instead of _SC_NPROCESSORS_MAX_NP. */
if (errno == EINVAL &&
nrcpus < (1024 << 8)) {
nrcpus = nrcpus << 2;
goto realloc;
}
perror("sched_getaffinity");
return -1;
}
This is a bit complex than I expected at first, but I have no idea better way.