How many processors / cores do you have on your systems, and does this script successfully detect all of them?
Basically, the idea is a single cross-platform code snippet that runs fast and is portable across Unixes, even AIX, HP-UX, or Solaris machines. The script below should be vanilla Bourne shell and portable between any of them. I would be very interested in whether it works across all these platforms, and what types of machines you guys may be able to try it with.
Update: added basic Darwin support with hwprefs
Update: plain old psrinfo for Solaris processor detection (logical CPUs)
Update: added (maybe) better AIX support by checking for pmcycles and using that before attempting lsdev
Update: fixed IRIX processor detection, so it will get "Processor" in addition to "Processors"
Update: switched IRIX processor detection to use sysconf, for more reliable detection on mixed processor systems
Basically, the idea is a single cross-platform code snippet that runs fast and is portable across Unixes, even AIX, HP-UX, or Solaris machines. The script below should be vanilla Bourne shell and portable between any of them. I would be very interested in whether it works across all these platforms, and what types of machines you guys may be able to try it with.
Update: added basic Darwin support with hwprefs
Update: plain old psrinfo for Solaris processor detection (logical CPUs)
Update: added (maybe) better AIX support by checking for pmcycles and using that before attempting lsdev
Update: fixed IRIX processor detection, so it will get "Processor" in addition to "Processors"
Update: switched IRIX processor detection to use sysconf, for more reliable detection on mixed processor systems
Code:
#!/bin/sh
uname -a
if [ -f /bin/cp ] && [ -f /bin/sh ]; then
if [ -r /proc/cpuinfo ]; then
grep -c ^processor /proc/cpuinfo
elif [ -x /usr/bin/hwprefs ]; then
/usr/bin/hwprefs cpu_count
elif [ -x /usr/sbin/psrinfo ]; then
/usr/sbin/psrinfo | grep -c on-line
elif [ -x /usr/sbin/ioscan ]; then
/usr/sbin/ioscan -kC processor | grep -c processor
elif [ -x /usr/sbin/pmcycles ]; then
/usr/sbin/pmcycles -m | grep -c .
elif [ -x /usr/sbin/lsdev ]; then
/usr/sbin/lsdev -Cc processor -S 1 | grep -c .
elif [ -f /sbin/hinv ] && [ -x /usr/sbin/sysconf ]; then
/usr/sbin/sysconf NPROC_ONLN
elif [ -x /usr/sbin/sysctl ]; then
/usr/sbin/sysctl -n hw.ncpu
elif [ -x /sbin/sysctl ]; then
/sbin/sysctl -n hw.ncpu
else
echo 'Error: unknown platform!' 1>&2
exit 1
fi
else
echo 'Unix without the Unix?' 1>&2
exit 1
fi
uname -a
if [ -f /bin/cp ] && [ -f /bin/sh ]; then
if [ -r /proc/cpuinfo ]; then
grep -c ^processor /proc/cpuinfo
elif [ -x /usr/bin/hwprefs ]; then
/usr/bin/hwprefs cpu_count
elif [ -x /usr/sbin/psrinfo ]; then
/usr/sbin/psrinfo | grep -c on-line
elif [ -x /usr/sbin/ioscan ]; then
/usr/sbin/ioscan -kC processor | grep -c processor
elif [ -x /usr/sbin/pmcycles ]; then
/usr/sbin/pmcycles -m | grep -c .
elif [ -x /usr/sbin/lsdev ]; then
/usr/sbin/lsdev -Cc processor -S 1 | grep -c .
elif [ -f /sbin/hinv ] && [ -x /usr/sbin/sysconf ]; then
/usr/sbin/sysconf NPROC_ONLN
elif [ -x /usr/sbin/sysctl ]; then
/usr/sbin/sysctl -n hw.ncpu
elif [ -x /sbin/sysctl ]; then
/sbin/sysctl -n hw.ncpu
else
echo 'Error: unknown platform!' 1>&2
exit 1
fi
else
echo 'Unix without the Unix?' 1>&2
exit 1
fi
_________________
Debian GNU/Linux on a ThinkPad, running a simple setup with Fvwm.