howtouselinux

2 ways to Check CPU usage in Linux

Table of Contents

CPU usage is an important metric for CPU performance in Linux. Today we will learn how to use top and sar command to check cpu usage and what us sy ni wa hi st means.

Understanding CPU usage in Linux

CPU usage is a statistic of CPU usage per unit time, displayed as a percentage. Linux is a multitasking OS, which divides the time of each CPU into very short time slices, and then allocates them to each task in turn through the scheduler, thus creating the illusion of multitasking running at the same time.

Procedure to check cpu usage in Linux

  • Open the terminal.
  • Type top and press Enter to see all the running processes and their CPU usage. Press “q” to exit from top.
  • Type vmstat and press Enter to see more information about CPU usage.
  • Type mpstat -P ALL and press Enter to see more information about each individual CPU core.

 

Check CPU usage with top command in Linux

The best way to check cpu usage in Linux is using top command. Simply type “top” at the command prompt and press enter. You will see the CPU usage, memory usage, load average, and a list of the processes that are currently running. To exit the top command, press the q key.

The top command is a great tool to monitor CPU usage in Linux. It gives you a quick overview of how much CPU each process is using. This can be helpful for identifying which processes are using the most CPU and determining if there is anything that can be done to reduce the load.

Here are some other useful options for the top command:

  • -h : Show help
  • -d : Delay between updates (in seconds)
  • -s : Sort by CPU usage
  • -m : Sort by memory usage
  • -p : Processes to show (by PID)
  • -u : User to show

 

Understanding CPU Usage in Linux – us sy ni id wa

  • us: Time spent in running user space processes. This reflects the CPU consumption by your application.
  • sy: Time taken by system processes. A higher number here can indicate too many processes, and the CPU is spending more time process scheduling.
  • ni: Time spent with user space processes that are assigned with execution priority (nice value).
  • id: Indicates the time spent in idle mode, where the CPU is doing nothing.
  • wa: Waiting for IO. A higher value here means your CPU is spending too much time handling IO operations. Try improving IO performance or reducing IO at application level. More details about IOwait

 

Understanding CPU usage in Linux – hi si st

  • hi/si: Time spent in hardware interrupts or software interrupts.
  • st: Stolen CPU cycles. The hypervisor assigned these CPU cycles to another virtual machine. If you see a higher number in this field, try reducing the number of virtual machines from the host.

In general, we need to check which column plays a big part in CPU usage.

 

Check CPU usage with sar Command in Linux

The sar tool is a utility for managing system resources. It’s not limited strictly to CPU usage, but you can use the -u option to track CPU performance. Use the following command to direct sar to monitor CPU usage at set intervals: sar –u 5

The –u option tells it to display CPU usage. The 5 indicates that it should display every 5 seconds. This will run indefinitely. To cancel, press Ctrl-C.

We can see from the output that the “all” column shows the total amount of CPU time that has been used. The “usr” column shows the amount of time that has been spent in user-space by processes. The “sys” column shows the amount of time that has been spent in kernel-space by processes. The “iowait” column shows the amount of time that has been spent waiting for I/O operations to complete.

We can also use the sar -P command to see information about one CPU core utilization.

To see information about memory usage, we can use the sar -r command. This command will print information about memory utilization. We can see from the output that the “used” column shows the amount of memory that is currently being used by processes. The “buff/cache” column shows the amount of memory that is being used for buffers and cache. The “free” column shows the amount of memory that is not being used by any processes.

Troubleshoot high CPU usage in Linux

  • The user CPU and Nice CPU are high, indicating that the user mode process occupies more CPU, so you should focus on the performance of the process.
  • If the system CPU is high, it means that the kernel mode occupies more CPU, so you should focus on checking the performance of kernel threads or system calls.
  • I/O waiting for the CPU is high, indicating that the waiting time for I/O is relatively long, so you should focus on checking whether there is an I/O problem in the system storage.
  • Soft interrupt and hard interrupt are high, indicating that the processing program of soft interrupt or hard interrupt occupies more CPU, so you should focus on checking the interrupt service program in the kernel

 

Guide to fix high CPU usage in Linux

If your computer is experiencing high CPU usage, there are a few things that you can do to troubleshoot the problem.

  1. In the command prompt, type in “top” and hit Enter. This will show you a list of all the processes currently running on your computer, sorted by CPU usage.
  2. If you see a process that is using a lot of CPU, you can stop it by typing the following command: kill <pid>. Where “<pid>” is the process ID for the process that you want to stop.
  3. To identify the source of high CPU usage, you can check your system logs by typing the following command into the terminal: journalctl -xe. The “journalctl” command will show you all of the system logs for your computer. You can filter for entries that contain the word “cpu” by using the “-e” parameter.
  4. If you still don’t know what’s causing the high CPU usage, you can try to diagnose the problem by using a tool called “lsof”. The “lsof” tool can be used to show information about open files and network connections.
  5.  If you’re still having trouble, you can try to find more information about your problem by searching the internet or posting a question on a forum.

 

What are some common causes of high CPU usage in Linux

There are a few common causes of high CPU usage in Linux:

  • Unused or unnecessary processes: Sometimes, a process may be running on your computer even though you don’t need it. This can cause the CPU to become overloaded and result in high CPU usage.
  • Malware or viruses: Malware and viruses can also cause high CPU usage by using up a lot of system resources.
  • Hardware problems: If your computer’s hardware is not functioning properly, it can also cause high CPU usage.

Related Post:

Troubleshoot high iowait issue on Linux

Linux Performance: CPU Load Average

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.