How to Calculate and Monitor Linux CPU Usage with /proc/stat, top, and pidstat
Learn the theory behind Linux CPU usage metrics, how the kernel’s jiffies and /proc/stat values are interpreted, the formula for calculating utilization over intervals, and practical commands like top, ps, and pidstat to observe real‑time CPU performance on servers and desktops.
CPU Usage Calculation
Before diving into CPU usage, it is useful to understand the kernel’s jiffies counter, which increments every HZ ticks since boot. The HZ value can be read from the kernel configuration, for example:
root@node:~# grep 'CONFIG_HZ=' /boot/config-$(uname -r)
CONFIG_HZ=250With CONFIG_HZ=250, the system records 250 ticks per second, i.e., each tick represents 4 ms. User‑space typically uses a fixed 10 ms tick.
CPU statistics are exposed in /proc/stat. A snippet of its content looks like:
root@node:~# cat /proc/stat
cpu 1549370658 9 1805063913 174156780720 9707711 0 216981980 0 725037436 0
cpu0 14624038 0 12618687 2715504522 206472 0 193511845 0 7216830 0
cpu1 12030579 0 11686870 2751300042 180759 0 10469844 0 4488307 0
cpu2 11639548 0 11156338 2752924400 140756 0 2195205 0 4473860 0
cpu3 12235160 0 11439675 2751463622 361104 0 892799 0 4916428 0The columns represent, in order:
user (us): time spent in user mode, including guest time. nice (ni): time spent in user mode with low priority. system (sys): time spent in kernel mode. idle (id): time spent idle. iowait (wa): time waiting for I/O. irq (hi): time handling hardware interrupts. softirq (si): time handling software interrupts. steal (st): time stolen by other virtual machines (zero on bare metal). guest (guest): time spent running a virtual CPU. guest_nice (gnice): low‑priority guest time.
To compute CPU utilization, tools such as top sample the values periodically (default every 3 seconds). The usage is calculated as:
CPU% = ((Total_time_now - Idle_now) - (Total_time_prev - Idle_prev)) / (Total_time_now - Total_time_prev) * 100This matches the algorithm used by most monitoring utilities.
Viewing CPU Usage
The most common commands are top and ps. A typical top snapshot:
top - 20:12:00 up 70 days, 3:49, 1 user, load average: 0.10, 0.18, 0.14
Tasks: 2229 total, 1 running, 332 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni, 99.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 52468537+total, 47777491+free, 36061900 used, 10848532 buff/cache
KiB Swap: 31250428 total, 31250428 free, 0 used. 48045312+avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
29973 root 20 0 42728 5972 3136 R 0.7 0.0 0:00.16 top
9 root 20 0 0 0 0 I 0.3 0.0 0:13.52 rcu_sched
22809 root 20 0 1024352 88780 19280 S 0.3 0.0 0:53.22 zk_proxyThe line beginning with %Cpu shows the overall CPU usage; the individual fields are explained in the previous section.
For per‑process statistics, pidstat is handy. Example:
# pidstat 1 5
Linux 4.15.0-58-generic (cs1ahyper01n07) 12/06/2025 _x86_64
10:41:09 AM UID PID %usr %system %guest %CPU CPU Command
10:41:10 AM 0 65 0.00 0.00 0.00 0.00 0 top
10:41:10 AM 0 3565 26.67 0.00 0.00 26.67 0 telegraf
10:41:10 AM 0 8788 1.90 0.00 0.00 1.90 0 mq_proxyField meanings: User‑mode CPU usage (%usr) Kernel‑mode CPU usage (%system) Virtual‑machine CPU usage (%guest) Total CPU usage (%CPU)
The final “Average” section of pidstat aggregates the five samples to show mean values.
This article provides the background, the raw data sources, the calculation method, and practical commands for accurately measuring CPU utilization on Linux systems.
Tech Stroll Journey
The philosophy behind "Stroll": continuous learning, curiosity‑driven, and practice‑focused.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
