[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: number of processors
From: |
Philip Willoughby |
Subject: |
Re: number of processors |
Date: |
Tue, 27 Aug 2002 18:00:14 +0100 (BST) |
Today, Philipp Gortan wrote:
>case "$MACHTYPE" in
>~ i686-*-linux) test -f /proc/cpuinfo && \
> PROC_NR=`grep processor /proc/cpuinfo | \
> wc -l | awk '{print $1}'`
> ;;
This should work for *-*-linux* not just i686s.
>the script works fine on all machines, but i'm not especially satisfied,
>as for each new machine added, the script has to be manually modified.
All recent POSIX platforms should respond to
sysconf(_SC_NPROCESSORS_CONF)
although
sysconf(_SC_NPROCESSORS_ONLN)
is better in case you're running on a box with some processors offline e.g.
a mid-upgrade sun E10K or a S/390 or somesuch.
I'd suggest the following C code be used:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
long nprocs;
nprocs = sysconf(_SC_NPROCESSORS_ONLN);
if (nprocs < 1)
nprocs = 1;
printf ("%ld\n",nprocs);
exit (EXIT_SUCCESS);
}
If you just run this it will print out the number of cpus if available or 1
if not. If it doesn't compile, assume 1.
It'd be nice if your macro took the load average into account -- flooring
the 1-minute load average and subtracting that from the number of
processors, and setting the result to 1 if it's less than one would be
good.
Even better -- If you're using GNU make, make -j -l <NUM_CPUs> is the best
thing to do, but I dunno if this is portable.
HTH
Regards,
Philip Willoughby
PS the above code works on Solaris 8 and on Linux 2.4.x, tested.
Systems Programmer, Department of Computing, Imperial College, London, UK
--
echo address@hidden | tr "bizndfohces" "pwgd9ociaku"