This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


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: What *is* the API for sched_getaffinity? Should sched_getaffinity always succeed when using cpu_set_t?


(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.



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