Tuesday, January 21, 2025
HomeQ&ALinux - How can I determine the current CPU utilization?

Linux – How can I determine the current CPU utilization?

To determine the current CPU utilization in a Linux system, you have several tools and commands available to check real-time CPU usage. Here are some commonly used methods:

1. Using top command

The top command is one of the most commonly used utilities for monitoring system performance, including CPU utilization. When you run top, it displays a dynamic, real-time view of system processes, including CPU usage.

top

In the top output, you’ll see a summary area at the top that shows CPU statistics:

%Cpu(s):  3.4 us,  2.1 sy,  0.0 ni, 93.8 id,  0.1 wa,  0.1 hi,  0.5 si,  0.0 st
  • us: User CPU time (time spent in user mode).
  • sy: System CPU time (time spent in kernel mode).
  • ni: Nice CPU time (time spent running user processes with a positive nice value).
  • id: Idle CPU time (time when the CPU is doing nothing).
  • wa: IO Wait (time waiting for disk operations).
  • hi: Hardware interrupt time.
  • si: Software interrupt time.
  • st: Steal time (time when the virtual machine is waiting for real CPU time).

2. Using mpstat command (from sysstat package)

The mpstat command can be used to display CPU utilization statistics for each processor.

Install sysstat if it’s not installed:

sudo apt-get install sysstat    # On Ubuntu/Debian
sudo yum install sysstat        # On CentOS/RHEL

Once installed, you can use the following command:

mpstat

Output will look something like this:

CPU    %usr   %nice   %sys   %iowait   %idle   %steal   %soft   %irq   %srv
all    5.05    0.01    2.29    0.25     92.40    0.00    0.00    0.00    0.00
  • %usr: Percentage of CPU used by user space processes.
  • %sys: Percentage of CPU used by system processes.
  • %iowait: Time spent waiting for I/O to complete.
  • %idle: Time when the CPU is idle.
  • %steal: Time the virtual machine is waiting for CPU.
See also  How Fast can an Lion Run in Yards?

3. Using uptime command

The uptime command provides a brief summary of system uptime and the load averages for the past 1, 5, and 15 minutes. It also gives a rough indication of CPU utilization based on load averages.

uptime

Example output:

 14:32:45 up 2 days,  3:51,  3 users,  load average: 0.01, 0.05, 0.07
  • Load average: Indicates how many processes are waiting to be executed by the CPU.
    • A load average of 1.0 means the system is fully utilizing one CPU core.
    • A load average of 0.5 means the system is using 50% of one core’s capacity.

4. Using vmstat command

The vmstat (virtual memory statistics) command gives a snapshot of various system statistics, including CPU usage.

vmstat 1

This will show a summary of system performance, updating every second. The columns related to CPU utilization are:

procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
 r  b   swpd   free   buff  cache   si  so   bi   bo   in   cs us sy id wa st
 2  0      0  123456  78901 234567    0   0    5    3   234  567  5  3  92  0  0
  • us: User CPU time.
  • sy: System CPU time.
  • id: Idle CPU time.
  • wa: Time spent waiting for I/O.
  • st: Steal time.
See also  First-time PC builder PC Part Picker

5. Using /proc/stat

You can also directly check the contents of the /proc/stat file, which contains detailed information about CPU usage.

cat /proc/stat

The first line of /proc/stat contains the total CPU usage information in the following format:

cpu  3355 18 1161 23237 117 0 37 0 0 0

Each number represents:

  • user time: Time spent in user mode.
  • nice time: Time spent in user mode with a positive nice value.
  • system time: Time spent in kernel mode.
  • idle time: Time when the CPU is idle.
  • iowait time: Time spent waiting for I/O operations.
  • And other categories.

To compute the total CPU usage, you would need to subtract the idle time from the total time.

6. Using sar command (from sysstat package)

The sar command collects, reports, and saves system activity information, including CPU usage.

See also  What is 2 ml in tsp

To see the CPU usage, use:

sar -u 1 3

This will show CPU usage stats every 1 second, three times.

Output example:

Linux 4.15.0-54-generic (myhost)  01/21/2025  _x86_64_    (2 CPU)

12:00:01 AM     CPU     %user     %nice   %system   %iowait    %idle
12:00:02 AM     all      5.23      0.01      2.13      0.15      92.48
12:00:03 AM     all      4.92      0.01      2.05      0.16      92.86
12:00:04 AM     all      5.13      0.01      2.17      0.14      92.55
  • This gives a more detailed and historical CPU utilization overview.

Summary

To check the current CPU utilization in Linux:

  1. Use top for a real-time dynamic view.
  2. Use mpstat from the sysstat package for detailed CPU statistics.
  3. Use uptime for quick load averages.
  4. Use vmstat for a snapshot of system performance.
  5. Access /proc/stat for raw CPU usage data.
  6. Use sar for historical CPU performance stats.

Let me know if you’d like further help with any of these tools!

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x