Re: [PATCH] cpumask 5/10 rewrite cpumask.h - single bitmap based implementation

Paul Jackson
2004-06-04T16:15:48Z

William Lee Irwin III wrote:
> Some interface is needed to export NR_CPUS

Well ... technically ... such an interface already exists.
However the word "interface" might be too kind a description.

/*
* Ugly hack to get size of cpumask - keep calling sched_getaffinity
* with masks of increasing size until it stops complaining -EINVAL
* that the mask is too small. Determines size of kernel cpumask,
* in number of bytes. Size will always be some multiple of the
* size of an unsigned long. Since it's ok to be too big, and since
* kernel NR_CPUS is usually a power of two, just try each power
* of two, until one works. Contrary to the man page, a successful
* sched_getaffinity() returns a positive number (the sizeof(cpumask_t),
* to be exact), not zero. Consider any return >= 0 to mean success.
*/

#include <errno.h>
extern int errno;

int cpumasksz()
{
int nbytes;
unsigned long *mask = 0;

for (nbytes = sizeof(unsigned long); nbytes < 10000; nbytes *= 2) {
int r;
mask = (unsigned long *)realloc(mask, nbytes);
if (mask == 0)
return -ENOMEM;
errno = 0;
r = sched_getaffinity(0, nbytes, mask);
if (r < 0 && errno == EINVAL)
continue;
if (r >= 0)
break;
return -errno;
}
free(mask);
return nbytes;
}

--
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <pj@sgi.com> 1.650.933.1373