Diary of a Lazy SysAdmin

Skip to: Content | Sidebar | Footer

Finding how many CPUs/Cores/Threads in a Solaris server

14 October, 2009 (16:32) | howto, Operating Systems, Solaris, Unix | By: ender

I think this will only work in Solaris 10, but this should give a nice output of how many Physical CPUs, Physical Cores, and Threads [Virtual CPUs] are recognized by Solaris. This will work on Solaris 10 for SPARC or x86.

Note: I’m told that the script gives strange results when run in an LDOM. I don’t have any LDOMs to test with and I’m not really familiar with them. The script was meant to be run in the global zone. It does give proper results in an un-capped local zone.

The output looks like this:

user@host:~$ ~/bin/cpucount.sh

In host.example.com there are:

1        Physical CPUs/Sockets
8        Physical Cores across all CPUs
8        Cores per Physical CPU
64        Total virtual CPUs recognized by the OS
8        Threads per Core (Virtual CPUs per Core)


You can download cpucount.sh script [Right-Click -> Save Link As] or:

Here’s the actual script to produce the above output:

# Quick and **dirty** script to count the number of physical CPUs,
# physical cores and virtual CPUs recognized by the OS
# The CORES part will only work in Solaris 10
# The rest I'm not sure about.
# Gives odd results in an LDOM I'm told.
# But don't have any to test on

# Physical - the number of physical sockets/CPUs

PHYSICAL=`/usr/sbin/psrinfo -pv | grep physical | wc -l`

# Cores - The number of physical cores total [across all CPUs]
# I'm pretty sure this part will only work in Solaris 10

CORES=`/usr/bin/kstat cpu_info | \
 egrep "cpu_info |core_id" | \
 awk \
 'BEGIN { printf "%4s %4s", "CPU", "core" } \
 /module/ { printf "\n%4s", $4 } \
 /core_id/ { printf "%4s", $2} \
 END { printf "\n" }' | awk '{print $2}' | grep -v core | sort -u | wc -l`


# This is the TOTAL number of processors [virtual and real]
# that the OS recognizes

VIRTUAL=`/usr/bin/mpstat | grep -v CPU | wc -l`

# Remove odd whitespaces from beginning of results

## Print results in a pretty form :^)
printf "In ${HOSTNAME} there are:\n"
printf "%4s %3s Physical CPUs/Sockets\n" ${PHYSICAL}
printf "%4s %3s Physical Cores across all CPUs\n" ${CORES}
printf "%4s %3s Cores per Physical CPU\n" ${CORESPER}
printf "%4s %3s Total virtual CPUs recognized by the OS\n" ${VIRTUAL}
printf "%4s %3s Threads per Core (Virtual CPUs per Core)\n" ${VIRTUALPER}

##################### END SCRIPT #################

UPDATE: 2010-04-05 – updated script to print the output a little more nicely and aligned.


Comment from Nilesh Joshi
Time March 9, 2010 at 10:09 am

Gr8 script. Good one, it helped me a lot for addressing end users queries….

Comment from edgar
Time March 28, 2010 at 2:35 pm

nice script. just made my auditing a lot easier. thanks.

Comment from Mike Shaw
Time September 13, 2011 at 2:10 pm

Still of great use 2 years later!

Comment from deepak
Time May 14, 2012 at 8:07 am

Gr8 scipt

Comment from nic
Time September 19, 2012 at 3:22 am

excellent thanks :)

Comment from mrAibo
Time January 10, 2013 at 5:18 am

If you have virtual servet, than here will be an error:
becouse ${CORES} == 0

Comment from ender
Time January 10, 2013 at 2:23 pm

I’m not sure what kind of virtual server you mean. I’ve tested this from within zones and from a server running in a VMWare VM and it works fine.
If you mean it fails from within an LDOM, I’m aware and it’s mentioned at the top of the post. I just don’t have an LDOMs to test it against.

Write a comment