jww jww - 1 year ago 184
Bash Question

Determine CPU frequency from command line

I have a Bash script that detects CPU frequency from the command line and then calls the benchmark suit for a C/C++ library. Here's the essence of the script:

# CPU frequency in GHz
if [ "$IS_LINUX" -ne "0" ] && [ -e "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" ]; then
CPU_FREQ=$(cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq)
CPU_FREQ=$(awk "BEGIN {print $CPU_FREQ/1024/1024}")
elif [ "$IS_DARWIN" -ne "0" ]; then
CPU_FREQ=$(sysctl -a 2>/dev/null | $GREP 'hw.cpufrequency' | head -1 | awk '{print $3}')
CPU_FREQ=$(awk "BEGIN {print $CPU_FREQ/1024/1024/1024}")
elif [ "$IS_SOLARIS" -ne "0" ]; then
CPU_FREQ=$(psrinfo -v 2>/dev/null | $GREP 'MHz' | head -1 | nawk '{print $6}')
CPU_FREQ=$(nawk "BEGIN {print $CPU_FREQ/1024}")

It just occurred to me the script is failing under Cygwin and Cygwin-64.

How do I determine CPU frequency in the script under Cygwin and Cygwin-64?

Answer Source

This should work:

grep 'cpu MHz' /proc/cpuinfo | head -1 | awk -F: '{print $2/1024}'

(when I run this in my cygwin terminal I get 1.85156).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download