#!/bin/bash
# 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

HOSTNAME=`hostname`
# 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`

CORESPER=`expr ${CORES} / ${PHYSICAL}`

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

VIRTUAL=`/usr/bin/mpstat | grep -v CPU | wc -l`
VIRTUALPER=`expr ${VIRTUAL} / ${CORES}`

# Remove odd whitespaces from beginning of results
PHYSICAL="${PHYSICAL#"${PHYSICAL%%[![:space:]]*}"}"
CORES="${CORES#"${CORES%%[![:space:]]*}"}"
CORESPER="${CORESPER#"${CORESPER%%[![:space:]]*}"}"
VIRTUAL="${VIRTUAL#"${VIRTUAL%%[![:space:]]*}"}"
VIRTUALPER="${VIRTUALPER#"${VIRTUALPER%%[![:space:]]*}"}"

## Print results in a pretty form :^)
echo
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}


