CPU usage in Linux

ZOHEB IQBALZOHEB IQBAL
2 min read

To check and interpret CPU usage in Linux, we can use several commands. Here are the most common ones:


1. Using top Command

top
  • Press 1 to see CPU usage per core.

  • The %Cpu(s) section shows:

    • us (user) → CPU used by user processes.

    • sy (system) → CPU used by kernel processes.

    • id (idle) → CPU that is not being used.

    • wa (wait) → CPU waiting for I/O operations.

    • ni (nice) → CPU used by processes with nice values.

    • hi/si (hardware/software interrupts) → CPU consumed by interrupts.

    • st (steal) → CPU stolen by the hypervisor (in virtualized environments).

Example:

%Cpu(s):  10.0 us,  5.0 sy,  0.0 ni, 80.0 id,  5.0 wa,  0.0 hi,  0.0 si,  0.0 st
  • If us + sy is high, it indicates heavy CPU usage.

  • If wa is high, it suggests disk I/O bottleneck.


2. Using htop (Better Visualization)

htop
  • Displays real-time CPU usage per core.

  • Shows a graphical representation of CPU load.

  • Allows sorting processes by CPU usage.


3. Using mpstat (Part of sysstat package)

mpstat -P ALL 1
  • Shows CPU usage for all cores (-P ALL).

  • Updates every 1 second.

Example Output:

CPU    %usr   %nice   %sys   %iowait   %irq   %soft   %steal   %idle
all     15.0    0.0    5.0       1.0    0.0     0.0       0.0   79.0
  0     10.0    0.0    5.0       1.0    0.0     0.0       0.0   84.0
  1     20.0    0.0    5.0       1.0    0.0     0.0       0.0   74.0
  • The "all" row aggregates all CPU cores.

  • A high %iowait suggests an I/O bottleneck.


4. Using sar (Historical CPU Usage)

sar -u 5 10
  • Collects CPU usage every 5 seconds, 10 times.

  • Useful for analyzing past CPU usage trends.


5. Checking Load Average (uptime or w)

uptime

or

w

Example output:

10:30:15 up 5 days,  3:45,  2 users,  load average: 2.50, 1.90, 1.80
  • The load average values (2.50, 1.90, 1.80) correspond to:

    • 1-minute average (2.50)

    • 5-minute average (1.90)

    • 15-minute average (1.80)

  • If the load average exceeds the number of CPU cores, the system is overloaded.


Conclusion

  • Use top or htop for real-time monitoring.

  • Use mpstat to check CPU usage per core.

  • Use sar for historical analysis.

  • Use uptime or w to check load average.

0
Subscribe to my newsletter

Read articles from ZOHEB IQBAL directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ZOHEB IQBAL
ZOHEB IQBAL