Re: libcruft/sysconf_cpus.c patch

Tino Reichardt <[email protected]> Tue, 23 Nov 2010 23:22:52 +0100
Newsgroups gmane.linux.lib.dietlibc
Message-ID <[email protected]>
* Indan Zupancic <[email protected]> wrote:
> Hello,
> 
> On Tue, November 23, 2010 04:41, Felix von Leitner wrote:
> >> __sc_nr_cpus() [ == sysconf(_SC_NPROCESSORS_ONLN) ] can run off the end
> >> of its /proc/cpuid buffer and (depending on process memory layout) dump
> >> core.  This caused an intermittent dump in diet `x86info`.
> >
> > Thanks, added patch.
> >
> >> This will still get a wrong answer if "processor" or one of the
> >> other-arch strings happens to get cut by the buffer end.  Not important
> >> for my application.  I would suggest a malloc'd buffer the full stat'd
> >> size of /proc/cpuid, but that seems a bit scary with 160-core x86 boxes
> >> running around out there this year...
> >
> > The problem is that stat always returns size 0.
> > So you'd have to have a loop and try bigger and bigger sizes.  That's
> > too much effort to get the number of CPUs.  I'm sure there must be some
> > smarter way to do this than parsing /proc/cpuinfo.
> >
> > Maybe we should use /proc/stat.  There is no sysctl for this,
> > unfortunately, and it's not in the ELF auxvec.  Too bad, really.
> > I'd prefer a way that works if /proc if not mounted.
> 
> You can use sched_getaffinity() to figure out the number of cpus.
> Just count the number of bits set. Advantage of this is that it
> gives the number of useable cpus, not the theoretical maximum.
> Disadvantage is that it only works for Linux 2.6.

Cool idea ;)

-- 
regards, TR