How to monitor cpu performance metrics on Ubuntu

By | November 12, 2022

In a pervious article we discussed about the important cpu performance metrics like usage, temperature, power and clock frequency. In this article we shall take a look at various methods of monitoring the same on Ubuntu or similar linux distros.

There are quite a few commands on linux for monitoring cpu metrics. If you just want to see the cpu usage, you can use the System Monitor which is available on both gnome and kde desktops.

For more details like the clock frequency, temperature etc, use the commands shown below.

1. Turbostat

$ sudo turbostat --Summary --quiet --show Busy%,Avg_MHz,PkgTmp,PkgWatt --interval 1
$ sudo turbostat --Summary --quiet --show Busy%,Avg_MHz,Bzy_MHz,TSC_MHz,PkgTmp,PkgWatt --interval 1
Avg_MHz Busy%   Bzy_MHz TSC_MHz PkgTmp  PkgWatt
188     5.54    3392    3000    70      10.75
314     9.21    3408    3000    66      12.73
236     6.95    3394    3000    63      11.63
320     9.38    3411    3000    63      13.17
287     8.41    3413    3000    65      12.33
361     10.70   3371    3000    63      12.74
298     8.76    3404    3000    64      12.23
281     8.22    3415    3000    63      12.20
515     15.23   3380    3000    65      15.27
1404    42.30   3319    3000    75      23.06
3300    100.00  3300    3000    76      40.66
3300    100.00  3300    3000    76      40.87
3300    100.00  3300    3000    77      40.92
3300    100.00  3300    3000    78      41.05
3300    100.00  3300    3000    78      41.13

There are 3 different columns for frequency: Avg_MHz, Bzy_MHz, TSC_MHz. The Avg_MHz fluctuates in real-time as per the load on the cpu. This is the actual average frequency. The Bzy_Mhz measures the frequency of those cycles when the cpu is under load. The TSC_MHz field seems to indicate the base clock frequency of the processor.

The turbostat command can be combined with ttyplot to generate nice terminal graphs. With multi-tab terminals like Konsole you can view each graph in its own tab.

Monitor cpu clock graph

The cpu clock speed is the frequency and is measured in Mhz or Ghz.

$ sudo turbostat --Summary --quiet --show Avg_MHz --interval 1 | gawk '{ printf("%.2f\n" , $1); fflush(); }' | ttyplot -s 100 -t "Turbostat - CPU Clock (Mhz)" -u "Mhz"

Turbostat actually has multiple fields related to the clock frequency which are:

Avg_Mhz
Bzy_Mhz
TSC_Mhz

To learn more about each of the measurement check the turbostat man page. The Avg_Mhz is what indicates the actual average frequency over equal sized time durations.

Monitor the CPU temperature graph

$ sudo turbostat --Summary --quiet --show PkgTmp --interval 1 | gawk '{ printf("%.2f\n" , $1); fflush(); }' | ttyplot -s 100 -t "Turbostat - CPU Temp (C)" -u "C"

Monitor power graph

$ sudo turbostat --Summary --quiet --show PkgWatt --interval 1 | gawk '{ printf("%.2f\n" , $1); fflush(); }' | ttyplot -s 100 -t "Turbostat - CPU Power (watts)" -u "watts"

If you want to view all graphs together in 1 terminal screen then use the tmux program. Use the Ctrl-B + " and Ctrl-B + % option to create horizontal and vertical dividers to get 4 equal sized child terminals and then run one turbostat command in each of the child terminal.

2. s-tui - The Stress Terminal UI

The next tool is s-tui and it monitors all parameters including usage, temperature, frequency and power usage and plots multiple graphs in

In order to monitor the power consumption, it must be run with root privileges.

sudo s-tui

It will show summary and graph of various cpu parameters. Here is how it looks.

3. i7z - For Intel Core series cpus

There is a command line utility called i7z that works for intel core i3/i5/i7 cpus. It shows a bunch of info about the individual cpu cores including clock frequency, temperature and voltage. This can be useful for advanced level diagnostics or like when you are trying to overclock your cpu.

Install it with apt and run with sudo

$ sudo apt-get install i7z
sudo i7z

It would output information like this:

Cpu speed from cpuinfo 2999.00Mhz
cpuinfo might be wrong if cpufreq is enabled. To guess correctly try estimating via tsc
Linux's inbuilt cpu_khz code emulated now
True Frequency (without accounting Turbo) 2999 MHz
  CPU Multiplier 30x || Bus clock frequency (BCLK) 99.97 MHz
Socket [0] - [physical cores=4, logical cores=4, max online cores ever=4]
  TURBO ENABLED on 4 Cores, Hyper Threading OFF
  Max Frequency without considering Turbo 3098.97 MHz (99.97 x [31])
  Max TURBO Multiplier (if Enabled) with 1/2/3/4 Cores is  35x/34x/34x/33x
  Real Current Frequency 3423.33 MHz [99.97 x 34.24] (Max of below)
        Core [core-id]  :Actual Freq (Mult.)      C0%   Halt(C1)%  C3 %   C6 %  Temp      VCore
        Core 1 [0]:       3381.43 (33.83x)      7.23    68.7       1    22.1    57      1.1217
        Core 2 [1]:       3423.33 (34.24x)      13.1    68.5       1    15.5    57      1.1118
        Core 3 [2]:       3396.27 (33.97x)      9.47    56.7       1    31.6    58      1.1108
        Core 4 [3]:       3371.24 (33.72x)      6.58    69.6       1      22    57      1.1036
C0 = Processor running without halting
C1 = Processor running with halts (States >C0 are power saver modes with cores idling)
C3 = Cores running with PLL turned off and core cache turned off
C6, C7 = Everything in C3 + core state saved to last level cache, C7 is deeper than C6
  Above values in table are in percentage over the last 1 sec
[core-id] refers to core-id number in /proc/cpuinfo
'Garbage Values' message printed when garbage values are read
  Ctrl+C to exit

4. cpupower

The cpupower command from cpupowerutils also displays the frequency

watch -n1 sudo cpupower monitor

The output would look something like this

Every 1.0s: sudo cpupower monitor                                  enlightened: Sat Nov 12 18:46:40 2022
    | Nehalem                   || Mperf              || Idle_Stats
 CPU| C3   | C6   | PC3  | PC6   || C0   | Cx   | Freq  || POLL | C1   | C1E  | C3   | C6   | C7s  | C8
   0|  1.42| 33.62|  0.00|  0.00||  5.99| 94.01|  3382||  0.01|  1.83|  2.33|  2.06| 37.37|  0.00| 50.00
   1|  0.26| 13.96|  0.00|  0.00|| 12.55| 87.45|  3407||  0.00|  0.76|  0.90|  0.43| 15.50|  0.01| 69.21
   2|  0.52| 23.60|  0.00|  0.00||  9.58| 90.42|  3404||  0.01|  3.18|  0.57|  0.80| 26.25|  0.00| 59.04
   3|  1.53| 23.64|  0.00|  0.00||  6.73| 93.27|  3374||  0.03|  1.39|  0.88|  2.08| 26.57|  0.33| 61.50

There is a "Freq" column which shows the frequency in Mhz. This value is similar to the values shown by the i7z and /cpu/proc commands. For a detailed explanation of other columns check this article.

Discrepancy in the clock frequency

The clock frequency reported by turbostat Avg_Mhz metric is different from that reported by i7z and /proc/cpuinfo.

The values reported by i7z, /proc/cpuinfo, turbostat Bzy_Mhz are closer and similar.

Stressing the CPU

If you want to quickly load the cpu and see how the metrics change, use the stress command. Here is a quick and simple example

$ stress --cpu 8 --timeout 100s

The above command will create 8 threads that will run cpu-intensive tasks for 100 seconds continuously. Meanwhile you can monitor the metrics using the above shown methods.

Conclusion

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *