[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Getting Number of CPU(-core)s and giving it as the --jobs argument t
From: |
Nordlöw |
Subject: |
Re: Getting Number of CPU(-core)s and giving it as the --jobs argument to GNU Make |
Date: |
Wed, 12 Sep 2007 01:20:52 -0700 |
User-agent: |
G2/1.0 |
On 12 Sep, 08:30, Michael Trausch <"mike|s/\\x40/\\./g;s/|.*|/\\x40/g;|
trausch"@us> wrote:
> Dieter Wilhelm, on 09/11/2007 04:20 PM said:
>
>
>
> > That's a bit confusing, I thought I had *one* processor with *two*
> > cores and the content in /proc/cpuinfo claims two processor, 0 and 1
> > with two cores, respectively, where am I wrong?
>
> That's the way it shows up - it's a dual core processor, and the
> information is the same for them except for the core number. If you had
> two dual core CPUs in your system, you would see four entires in
> /proc/cpuinfo. This is normal, because a dual core system (mostly)
> performs just as a dual processor system would. It's more or less like
> having two CPUs.
>
> In fact, many utilities and programming languages will even say that you
> have two CPUs when you really only have one dual-core CPU.
>
> That having been said, the only cross-platform method I know of for
> finding the number of cores (processors) on a system would be in
> .NET/Mono, since that works whereever there is an implmementation of
> .NET. Other than that, the only way that I know is to look in the
> /proc/cpuinfo file on Linux.
In C on Linux and Mac OS X the following detects number of CPUs:
#include <unistd.h>
#ifdef __APPLE__
#include <sys/sysctl.h>
#endif
/***
* Detect the number of CPUs/Processors/Cores Online.
* @return number of CPUs found.
*/
static inline int get_CPUmult(void)
{
int num = 1; /* default to one CPU */
#if defined(__APPLE__)
size_t len = sizeof(num);
int mib[2];
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
if (sysctl(mib, 2, &num, &len, 0, 0) < 0 ||
len != sizeof(num)) {
num = 1;
}
#elif defined(_SC_NPROCESSORS_ONLN)
num = sysconf(_SC_NPROCESSORS_ONLN);
#endif
return num;
}
/Nordlöw