Miscellaneous Operating Systems/Hardware

CPU Core Counts: AIX, HP-UX, IRIX, Solaris, BSD, etc. - Page 1

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

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

_________________
Debian GNU/Linux on a ThinkPad, running a simple setup with Fvwm.
Well, in the interest of furthering education and all that... Here's what I can offer without starting anything up that wasn't already running.

FreeBSD 8.1:
Code:
soekris# ./proc-count
FreeBSD soekris.crash.com 8.1-RELEASE FreeBSD 8.1-RELEASE #0: Sun Nov 21 18:22:10 PST 2010     [email protected]:/usr/obj/usr/src/sys/TINYBSD  i386
1
soekris#

FreeBSD 8.2:
Code:
285 io% ./proc-count
FreeBSD io.crash.com 8.2-STABLE-201105 FreeBSD 8.2-STABLE-201105 #0: Tue May 17 05:18:48 UTC 2011     [email protected]:/usr/obj/usr/src/sys/GENERIC  amd64
4
286 io%

FreeBSD 9.1:
Code:
9 emt% ./proc-count
FreeBSD emt.crash.com 9.1-RELEASE FreeBSD 9.1-RELEASE #0 r243825: Tue Dec  4 09:23:10 UTC 2012     [email protected]:/usr/obj/usr/src/sys/GENERIC  amd64
8
10 emt%

Fedora 18:
Code:
209 abort% proc-count
Linux abort.crash.com 3.9.4-200.fc18.x86_64 #1 SMP Fri May 24 20:10:49 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
4
210 abort%

OSX 10.8.4:
Code:
46 shiny% ./proc-count
Darwin shiny.crash.com 12.4.0 Darwin Kernel Version 12.4.0: Wed May  1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64
Error: unknown platform!
47 shiny%

_________________
Then? :IRIS3130: ... Now? :O3x02L: :A3504L: - :A3502L: :1600SW: +MLA :Fuel: :Octane2: :Octane: :Indigo2IMP: ... Other: DEC :BA213: :BA123: Sun , DG AViiON , NeXT :Cube:
Cool, many thanks. I've updated the code to include detection for Darwin. :)

_________________
Debian GNU/Linux on a ThinkPad, running a simple setup with Fvwm.
Code:
NetBSD deuterium 1.6.2 NetBSD 1.6.2 (GENERIC) #0: Wed Feb 11 03:49:49 UTC 2004     [email protected]:/autobuild/netbsd-1-6-PATCH002/pmax/OBJ/autobuild/netbsd-1-6-PATCH002/src/sys/arch/pmax/compile/GENERIC pmax
1


Code:
Darwin Nelis-MacBook-Pro.local 12.4.0 Darwin Kernel Version 12.4.0: Wed May  1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64
4

_________________
"Look on wife/girlfriends/parents face when you drag all this stuff(IRIS CRIMSON, ONYX, ONYX2, ORIGIN 2000 ,etc.) home.... Priceless! ;) "
:Tezro: :O3x02L: :Indy: :Indigo2IMP: :O2: :O2: :Octane: Motorla Series 900(MVME197LE) , Atari TT030, Motorola Powerstack
Code:
% sh testcpu
AIX uppsala 1 6 000C3N50R3D
2


This is, technically, correct -- there are two physical cores. However, AIX recognizes four logical CPUs because of SMT:

Code:
% iostat

System configuration: lcpu=4 drives=2 paths=1 vdisks=0

tty:      tin         tout    avg-cpu: % user % sys % idle % iowait
0.1         45.0                1.5   0.4   97.9      0.3

Disks:        % tm_act     Kbps      tps    Kb_read   Kb_wrtn
hdisk0           0.9      85.5       3.5   366793833  542291156
cd1              0.1       0.0       0.0          0         0
% vmstat

System configuration: lcpu=4 mem=7712MB

kthr    memory              page              faults        cpu
----- ----------- ------------------------ ------------ -----------
r  b   avm   fre  re  pi  po  fr   sr  cy  in   sy  cs us sy id wa
1  1 649184 49871   0   0   0   8   17   0  40  580 185  1  0 98  0


As for Tiger on PowerPC (quad processor ist demonstrandum),

Code:
% sh testcpu
Darwin bruce 8.11.0 Darwin Kernel Version 8.11.0: Wed Oct 10 18:26:00 PDT 2007; root:xnu-792.24.17~1/RELEASE_PPC Power Macintosh powerpc
4


I see kokoboi beat me to NetBSD, but it worked on my mac68k install also.

_________________
smit happens.

:Fuel: bigred , 700MHz R16K, 4GB RAM, V12, 6.5.30
:Indy: indy , 150MHz R4400SC, 256MB RAM, XL24, 6.5.10
probably posted from Image bruce , 2x2x2.5GHz PowerPC 970MP, 8GB RAM, Mac OS X 10.4.11
plus IBM POWER6 p520 * Apple Network Server 500 * HP C8000 * BeBox * Solbourne S3000 * Commodore 128 * many more...
Code:
Linux adams 2.6.32-279.22.1.el6.x86_64 #1 SMP Wed Feb 6 03:10:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
16


Works on Centos 6

Code:

[mgt@db02 ~]$ sh testcpu
SunOS db02 5.10 Generic_137111-08 sun4u sparc SUNW,Ultra-80
4


Works under solaris 10
mgtremaine wrote:
[mgt@db02 ~]$ sh testcpu
SunOS db02 5.10 Generic_137111-08 sun4u sparc SUNW,Ultra-80
4

Works under solaris 10


An Ultra 80 has 4 processors? When the hell did this happen?! That's awesome!

_________________
Paint It Blue
ClassicHasClass wrote:
Code:
AIX uppsala 1 6 000C3N50R3D

Any particular reason you named your machine "uppsala"? (my hometown)

_________________
:Onyx2R: :IRIS3130: :Onyx2: :O2000: :O200: :PI: :Fuel: :Indigo: :Octane: :O2: :Indigo2IMP: :Indigo2: :Indy: :1600SW: :pdp8e:
:BA213: <- MicroVAX 3500 :BA213: <- DECsystem 5500 :BA215: <- MicroVAX 3300
Pictures of my collection: www.pdp8.se
ClassicHasClass wrote:
Code:
% sh testcpu
AIX uppsala 1 6 000C3N50R3D
2


This is, technically, correct -- there are two physical cores. However, AIX recognizes four logical CPUs because of SMT:

Code:
% iostat

System configuration: lcpu=4 drives=2 paths=1 vdisks=0

tty:      tin         tout    avg-cpu: % user % sys % idle % iowait
0.1         45.0                1.5   0.4   97.9      0.3

Disks:        % tm_act     Kbps      tps    Kb_read   Kb_wrtn
hdisk0           0.9      85.5       3.5   366793833  542291156
cd1              0.1       0.0       0.0          0         0
% vmstat

System configuration: lcpu=4 mem=7712MB

kthr    memory              page              faults        cpu
----- ----------- ------------------------ ------------ -----------
r  b   avm   fre  re  pi  po  fr   sr  cy  in   sy  cs us sy id wa
1  1 649184 49871   0   0   0   8   17   0  40  580 185  1  0 98  0


As for Tiger on PowerPC (quad processor ist demonstrandum),

Code:
% sh testcpu
Darwin bruce 8.11.0 Darwin Kernel Version 8.11.0: Wed Oct 10 18:26:00 PDT 2007; root:xnu-792.24.17~1/RELEASE_PPC Power Macintosh powerpc
4


I see kokoboi beat me to NetBSD, but it worked on my mac68k install also.


Very cool, it's nice to see that it runs fine on AIX. I'm just trying to detect the multiprocessing capability. For example, how many processes can run simultaneously on the system? So if I have a big CPU-intensive task that needs to be split up, how many processes would be optimal? If too few processes are created, then that wastes memory and can be slower. If not enough are created, then it isn't taking advantage of all the processor cores available.

I guess a similar script could be made to detect the SMT capability. Some CPUs like that have specific technologies for running more threads at a time, or some operating systems may have ways to allocate a certain number of "virtual processors" for executing threads.

_________________
Debian GNU/Linux on a ThinkPad, running a simple setup with Fvwm.
Quote:
Any particular reason you named your machine "uppsala"? (my hometown)


I name my servers after Scandinavian cities and there's a whole long story behind that, but tl;dr my AIX boxen are generally all "from Sweden" and uppsala was the successor to stockholm, my Apple Network Server 500, which is now a backup box and will eventually be turned into an overgrown workstation in a place of honour for its many years of service to me.

Quote:
Very cool, it's nice to see that it runs fine on AIX. I'm just trying to detect the multiprocessing capability. For example, how many processes can run simultaneously on the system? So if I have a big CPU-intensive task that needs to be split up, how many processes would be optimal?


On this two-way POWER6, it can run four execution threads simultaneously. This will be important to account for as IBM doubles down on this strategy for POWER8.

_________________
smit happens.

:Fuel: bigred , 700MHz R16K, 4GB RAM, V12, 6.5.30
:Indy: indy , 150MHz R4400SC, 256MB RAM, XL24, 6.5.10
probably posted from Image bruce , 2x2x2.5GHz PowerPC 970MP, 8GB RAM, Mac OS X 10.4.11
plus IBM POWER6 p520 * Apple Network Server 500 * HP C8000 * BeBox * Solbourne S3000 * Commodore 128 * many more...
ClassicHasClass wrote:
Code:
% sh testcpu
AIX uppsala 1 6 000C3N50R3D
2


This is, technically, correct -- there are two physical cores. However, AIX recognizes four logical CPUs because of SMT


Interesting. On my Debian Linux boxes, which have Intel Hyperthreading enabled on their CPUs, the script detects the total number of logical cores rather than physical ones. It's surprsing, to say the least, that one platform counts physical cores and another counts logical ones. Personally, I'd consider that a bug.

Unfortunately, I don't see a quick work-around to get Linux to only count physical cores. (It's easy enough if all the cores are on one socket. Not so easy if you have multiple chips/sockets.) And I don't have any AIX experience at all to suggest how to get it to count logical ones the way Linux does. Sorry I can't be more helpful.

_________________
:Indigo2IMP: :Octane: :Indigo: :O3x0:
Sun SPARCstation 20, Blade 2500
HP C8000
FYI

On my Fuel all I'm getting is the output from the uname command.

Yes, I know my Fuel only has 1 cpu but I have a couple of dual cpus in my Octanes.

_________________
thegoldbug
:ChallengeXL: :ChallengeXL: :ChallengeXL: :OnyxR: :ChallengeL: :ChallengeL: :ChallengeL: :ChallengeL: :Onyx: :O200: :Fuel: :Fuel: :Octane2: :Octane: :Octane: :Octane: :Octane: :Indigo2IMP: :Indigo2IMP: :Indigo2IMP: :Indigo2IMP: :O2: :Indigo2: :Indigo2: :Indigo2: :Indigo2: :320: :320: :320: :320: :Indy: x5 :Indy: x2 :1600SW: x5 :Indigo: x7
Vault L, 6-1200s, Fibre Hub, Dials, Boards, spares, 3 Sun Blade 2000s, Sun Fire V480, 2-Hero 2000 Robots, 5-Hero 1 Robots
Quote:
And I don't have any AIX experience at all to suggest how to get it to count logical ones the way Linux does.


Easy, just use iostat or vmstat, as in my example above.

The most portable way that would work with nearly any version of AIX of any recent vintage is to see if vmstat/iostat lists an lcpu figure, and if it doesn't, fall back on lsattr or some such.

_________________
smit happens.

:Fuel: bigred , 700MHz R16K, 4GB RAM, V12, 6.5.30
:Indy: indy , 150MHz R4400SC, 256MB RAM, XL24, 6.5.10
probably posted from Image bruce , 2x2x2.5GHz PowerPC 970MP, 8GB RAM, Mac OS X 10.4.11
plus IBM POWER6 p520 * Apple Network Server 500 * HP C8000 * BeBox * Solbourne S3000 * Commodore 128 * many more...
thegoldbug wrote:
FYI

On my Fuel all I'm getting is the output from the uname command.

Yes, I know my Fuel only has 1 cpu but I have a couple of dual cpus in my Octanes.

Hmm... even if it only has 1 CPU, it should still report the 1 CPU...

Would you be able to post the output of the "hinv" and "hinv -c processor" commands?

_________________
Debian GNU/Linux on a ThinkPad, running a simple setup with Fvwm.
Code:
HP-UX ultralt B.11.11 U 9000/871 2006564319 unlimited-user license
2

_________________
:Indy: :Indigo2IMP: :Octane: :Indy: 4xRS6K 2xHP9K 6xSUN 1xDEC 14xMAC 7xPC 2xPS2
That reminds me, I should fire up the HP 9000/350 and see what it does with that. Unfortunately the C8000 is not set up right now.

_________________
smit happens.

:Fuel: bigred , 700MHz R16K, 4GB RAM, V12, 6.5.30
:Indy: indy , 150MHz R4400SC, 256MB RAM, XL24, 6.5.10
probably posted from Image bruce , 2x2x2.5GHz PowerPC 970MP, 8GB RAM, Mac OS X 10.4.11
plus IBM POWER6 p520 * Apple Network Server 500 * HP C8000 * BeBox * Solbourne S3000 * Commodore 128 * many more...
It works properly in OSX 10.8 as a virtual machine with a varied quantity of processors.

Code:
MountainLionVM:~ mjw$ ./proc-count
Darwin MountainLionVM.local 12.4.0 Darwin Kernel Version 12.4.0: Wed May  1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64
1

MountainLionVM:~ mjw$ ./proc-count
Darwin MountainLionVM.local 12.4.0 Darwin Kernel Version 12.4.0: Wed May  1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64
2

MountainLionVM:~ mjw$ ./proc-count
Darwin MountainLionVM.local 12.4.0 Darwin Kernel Version 12.4.0: Wed May  1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64
4

_________________
:Onyx: (Maradona) :Octane: (DavidVilla) A1186 (Xavi) d800 (Pique) d820 (Neymar)
A1370 (Messi) dp43tf (Puyol) A1387 (Abidal) A1408 (Guardiola)
ClassicHasClass wrote:
Quote:
Any particular reason you named your machine "uppsala"? (my hometown)


I name my servers after Scandinavian cities and there's a whole long story behind that, but tl;dr my AIX boxen are generally all "from Sweden" and uppsala was the successor to stockholm, my Apple Network Server 500, which is now a backup box and will eventually be turned into an overgrown workstation in a place of honour for its many years of service to me.

Quote:
Very cool, it's nice to see that it runs fine on AIX. I'm just trying to detect the multiprocessing capability. For example, how many processes can run simultaneously on the system? So if I have a big CPU-intensive task that needs to be split up, how many processes would be optimal?


On this two-way POWER6, it can run four execution threads simultaneously. This will be important to account for as IBM doubles down on this strategy for POWER8.


Good point, I guess I hadn't considered chip-level technologies for which the OS will actually see two or more logical processors per physical core. I've switched the Solaris code to use only psrinfo to just try getting the virtual processors rather than physical cores. Also added an entry for AIX, so it will use the pmcycles command if it is available, to find the number of logical processors.

Unfortunately I only have an x86-64 laptop with Linux here, along with VMs for FreeBSD, OpenBSD, NetBSD, DragonFly, and Illumian. The script seems to run fine on those platforms, but I'm more concerned with basic support for platforms that I don't have access to (HP-UX, AIX, IRIX, etc.).

_________________
Debian GNU/Linux on a ThinkPad, running a simple setup with Fvwm.
Here is the output from an hinv and a hinv -c command on my Fuel

I'm running Irix 6.5.29

IRIS 45# uname -a
IRIX64 IRIS 6.5 01090133 IP35
IRIS 46#



IRIS 35# hinv
1 600 MHZ IP35 Processor
CPU: MIPS R14000 Processor Chip Revision: 2.4
FPU: MIPS R14010 Floating Point Chip Revision: 2.4
Main memory size: 1536 Mbytes
Instruction cache size: 32 Kbytes
Data cache size: 32 Kbytes
Secondary unified instruction/data cache size: 4 Mbytes
Integral SCSI controller 3: Version IEEE1394 SBP2
Integral SCSI controller 2: Version QL1080, low voltage differential
Integral SCSI controller 0: Version QL12160, single ended
Disk drive: unit 1 on SCSI controller 0
Disk drive: unit 15 on SCSI controller 0
Integral SCSI controller 1: Version QL12160, single ended
CDROM: unit 6 on SCSI controller 1
IOC3/IOC4 serial port: tty1
IOC3/IOC4 serial port: tty2
IOC3 parallel port: plp1
Graphics board: V12
Integral Fast Ethernet: ef0, version 1, module 001c01, pci 4
Iris Audio Processor: version EMU revision A4, number 1
DMediaPro DM10 FW option: unit 0, revision 1.1.0
USB controller: type OHCI
USB Human Interface Device: device id 0 type keyboard
USB Human Interface Device: device id 0 type mouse
IRIS 36#

_________________
thegoldbug
:ChallengeXL: :ChallengeXL: :ChallengeXL: :OnyxR: :ChallengeL: :ChallengeL: :ChallengeL: :ChallengeL: :Onyx: :O200: :Fuel: :Fuel: :Octane2: :Octane: :Octane: :Octane: :Octane: :Indigo2IMP: :Indigo2IMP: :Indigo2IMP: :Indigo2IMP: :O2: :Indigo2: :Indigo2: :Indigo2: :Indigo2: :320: :320: :320: :320: :Indy: x5 :Indy: x2 :1600SW: x5 :Indigo: x7
Vault L, 6-1200s, Fibre Hub, Dials, Boards, spares, 3 Sun Blade 2000s, Sun Fire V480, 2-Hero 2000 Robots, 5-Hero 1 Robots
the output from the "hinv -c processor" command

IRIS 46# hinv -c processor
1 600 MHZ IP35 Processor
CPU: MIPS R14000 Processor Chip Revision: 2.4
FPU: MIPS R14010 Floating Point Chip Revision: 2.4
IRIS 47#

_________________
thegoldbug
:ChallengeXL: :ChallengeXL: :ChallengeXL: :OnyxR: :ChallengeL: :ChallengeL: :ChallengeL: :ChallengeL: :Onyx: :O200: :Fuel: :Fuel: :Octane2: :Octane: :Octane: :Octane: :Octane: :Indigo2IMP: :Indigo2IMP: :Indigo2IMP: :Indigo2IMP: :O2: :Indigo2: :Indigo2: :Indigo2: :Indigo2: :320: :320: :320: :320: :Indy: x5 :Indy: x2 :1600SW: x5 :Indigo: x7
Vault L, 6-1200s, Fibre Hub, Dials, Boards, spares, 3 Sun Blade 2000s, Sun Fire V480, 2-Hero 2000 Robots, 5-Hero 1 Robots