I wonder what clock speed my CPU cores are boosting to. How do I find out under Linux in BASH?
In today’s dynamically boosting CPUs, the clock speed depends on the current workload. Modern games for example are relatively multithreaded, using 6 or 8 cores is common in AAA titles. When I can decide if I use more cores in my own applications, I need to consider how much single-core clock speed I am trading for the extra parallelism.
I can produce arbitrarily multithreaded CPU load like so:
stress-ng --cpu 1
There is information about the CPU clock in sysfs:
$ cat /proc/cpuinfo
processor : 0
vendor_id : AuthenticAMD
cpu family : 23
model : 1
model name : AMD Ryzen Threadripper 1950X 16-Core Processor
stepping : 1
microcode : 0x8001137
cpu MHz : 2200.000
cache size : 512 KB
physical id : 0
siblings : 16
core id : 0
cpu cores : 16
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid amd_dcm aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb hw_pstate ssbd ibpb vmmcall fsgsbase bmi1 avx2 smep bmi2 rdseed adx smap clflushopt sha_ni xsaveopt xsavec xgetbv1 clzero irperf xsaveerptr arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif overflow_recov succor smca sev
bugs : sysret_ss_attrs null_seg spectre_v1 spectre_v2 spec_store_bypass retbleed smt_rsb srso div0
bogomips : 6799.35
TLB size : 2560 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 43 bits physical, 48 bits virtual
power management: ts ttp tm hwpstate eff_freq_ro [13] [14]
[...]
2
Monitoring cpu frequences:
As a function:
monitCpuFreq() {
local top=0 lowest freqs init bgcolor delay=${1:-0.1} strtop nl
local -i cRose=217 cPeach=209 cBrYellow=229 cBrGreen=151 cPaleBlue=110 i
while ! read -sn 1 -t $delay _; do
mapfile -t freqs < <(sed -ne '/^cpu .Hz/s/.*: //p' < /proc/cpuinfo |
sort -rn)
(( ${freqs[0]/.} > ${top/.} )) && top=${freqs[0]}
! [[ $lowest ]] || (( ${freqs[-1]/.} < ${lowest/.} )) &&
lowest=${freqs[-1]}
if ! [[ -n $init ]]; then
printf '%.0sn' ${freqs[@]:1}
printf 'e[%dAe7' $(( ${#freqs[@]} -1 ))
init='done'
fi
printf 'e8'
printf -v strtop 'Top: %8.3f Mhz' $top
for i in ${!freqs[@]}; do
(( i == ${#freqs[@]} -1 )) && nl=\r || nl=\n
bgcolor=$(( ${freqs[i]/.*} >= 3675 ? cRose :
${freqs[i]/.*} >= 3275 ? cPeach :
${freqs[i]/.*} >= 2175 ? cBrYellow :
${freqs[i]/.*} >= 1975 ? cBrGreen : cPaleBlue ))
printf 'e[48;5;%dme[30m%s MHze[0m re[16C%s %b'
"$bgcolor" "${freqs[i]}" "$strtop" $nl
strtop=''
done
printf 'e[16CLowest: %8.3f Mhz ' $lowest
done
echo
}
Will keep lowest and highest frequences:
2011.592 MHz Top: 3192.718 Mhz
2000.754 MHz
...
1999.129 MHz
1995.587 MHz Lowest: 1197.234 Mhz
Hit Any key to exit loop!
3
To get a nice list of current CPU speeds, you can filter the content of sysfs
using grep
and order it using sort
get an ordered list of your CPU clock speeds using grep
watch 'cat /proc/cpuinfo | grep MHz | sort -rnk 4'
This creates real-time lists like so:
cpu MHz : 3729.014
cpu MHz : 2200.000
cpu MHz : 2200.000
cpu MHz : 2200.000
cpu MHz : 2200.000
cpu MHz : 2200.000
cpu MHz : 2200.000
cpu MHz : 2200.000
cpu MHz : 2200.000
cpu MHz : 2200.000
cpu MHz : 2200.000
cpu MHz : 2200.000
cpu MHz : 2199.772
cpu MHz : 2199.756
cpu MHz : 2182.517
cpu MHz : 2109.182
1
If you are okay with using a script, you can get a color coded list:
#!/bin/bash
clear
while true; do
line=1
grep 'MHz' /proc/cpuinfo | awk '{printf "%.0fn", $4}'
| sort -rn | while read freq; do
if [ "$freq" -ge 3675 ]; then
bgcolor=217 # Rose (very high)
elif [ "$freq" -ge 3275 ]; then
bgcolor=209 # Peach (high)
elif [ "$freq" -ge 2175 ]; then
bgcolor=229 # bright yellow (medium)
elif [ "$freq" -ge 1975 ]; then
bgcolor=151 # Bright green (medium low)
else
bgcolor=110 # Pale blue (low)
fi
# Move cursor to correct line and print clock
printf "e[%d;1He[48;5;%dme[30m%4d MHze[0m" "$line" "$bgcolor" "$freq"
((line++))
done
# Move cursor below last line
printf "e[%d;1H" "$line"
sleep 0.1
done
1