CPU Load + Frequency per core in linux terminal?

0

1

I am doing scientific computing and I need to keep an eye on the cpu frequency and load of each core (there are 2 cores, on Ubuntu 14.04 w/ Gnome3). I can see the frequency with this bash script:

echo ""
while true; do
 if [ ! -z $ind ] ; then ind=; else ind="."; fi
 f0=$(sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq)
 f1=$(sudo cat /sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_cur_freq)
 printf " CPU MHz: %5i %5i $ind \r" $(($f0/1000)) $(($f1/1000)) 
 sleep 0.5
done 

The output looks like this: CPU MHz: 800 2401

And see CPU current load with:

glances -1rmnd

I'd like to get the current load for each core (in e.g. %) together with the frequency in the same terminal window and a compact format. Ideally it would look like this:

CPU MHz: 800 2401 Load: 12% 100%

Any advice is welcome, even different solution, or programs. Cheers /J

Jonatan Öström

Posted 2015-04-09T13:39:52.707

Reputation: 165

Answers

0

Well I found a solution after googling and getting some help. I'll post if for reference. This is my modified bash script:

while true; do
 load=$(sar -P ALL 1 1 | awk 'NR==5,NR==6 {print $3}' | tr '\n' '\t')
 if [ ! -z $ind ] ; then ind=; else ind="_"; fi
 f0=$(sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq)
 f1=$(sudo cat /sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_cur_freq)
 clear
 echo ""
 printf "\t_cpu0_ _cpu1$ind\n"
 printf "MHz:\t%5i  %5i \n" $(($f0/1000)) $(($f1/1000)) 
 printf "Load:\t $load"
done

Using sar -P ALL 1 1 from the sysstat package gives you a printout of processing stuff. awk selects the rows 5 and 6, and column 3, while tr '\n' '\t' transforms a new-line to a tab. The output in a terminal window is:

        _cpu0_ _cpu1_
MHz:     2000   2400 
Load:    3.92   1.00

The blinking cursor feature from the if-statement is because the numbers can be static for a long time under load.

Jonatan Öström

Posted 2015-04-09T13:39:52.707

Reputation: 165