How To Check CPU Usage/Utilization in Linux system
(: July 29, 2019)
Time to time, while you administer servers in your day to day roles, you would wish to check how the CPU of your system is faring especially if you suspect that something might be consuming more than it is supposed to. To be able to do that, there are many Linux tools that can help you inspect how it is performing in real-time.
Before we proceed you can check your cpu details with the lscpu command.
# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 8 On-line CPU(s) list: 0-7 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 142 Model name: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz Stepping: 10 CPU MHz: 1992.000 CPU max MHz: 1992.0000 BogoMIPS: 3984.00 Hypervisor vendor: Windows Subsystem for Linux Virtualization type: container Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm pni pclmulqdq est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt aes xsave osxsave avx f16c rdrand hypervisor
For more details, you can check the cpuinfo file.
# cat /proc/cpuinfo | less
Let us begin
1. Using top command
The top command displays Linux processes and has a column that details how the processes are utilizing available/allocated CPU.
top - 10:50:24 up 9 days, 23:32, 4 users, load average: 4.50, 4.46, 4.02 Tasks: 140 total, 2 running, 138 sleeping, 0 stopped, 0 zombie %Cpu(s): 13.8 us, 3.4 sy, 0.0 ni, 82.7 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st KiB Mem : 8001400 total, 126308 free, 2132428 used, 5742664 buff/cache KiB Swap: 8257532 total, 8255732 free, 1800 used. 5508964 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 736 root 20 0 2290772 979100 99752 S 27.2 12.2 1787:02 kesl 61548 root 20 0 4090296 993816 27168 S 3.0 12.4 348:22.96 java 1778 root 20 0 1312512 66428 48888 S 1.0 0.8 156:42.14 klnagent 734 root 20 0 20720 6100 5684 S 0.7 0.1 95:48.03 wdserver 657 root 20 0 116332 4292 3104 S 0.3 0.1 29:56.68 kesl_launcher.s
2. Using vmstat command
The vmstat command reports virtual memory statistics about processes, memory, paging, block IO, traps, and CPU utilization. Sample vmstat output is shown below. The last column is labelled cpu and should be where we are interested in here.
# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 4 0 520 135564 2168 5859468 0 0 5 4 11 19 6 2 91 0 0
- The cpu columns are expressed as percentages of total CPU time.
- The first cpu column, “us” (user code) shows the percentage of time spent running non-kernel code.
- The second cpu column, “sy” (system code) shows the percentage of time spent running kernel code.
- The third cpu column, “id” shows the percentage of idle time.
- The fourth cpu column, “wa” shows the percentage of time spent waiting for I/O (Input/Output).
- The fifth cpu column, “st” (steal time) shows the percentage of time stolen from a virtual machine.
3. Using top command
From its man pages, Htop is a free (GPL) ncurses-based process viewer for Linux. It is similar to top, but allows you to scroll vertically and horizontally, so you can see all the processes running on the system, along with their full command lines, as well as viewing them as a process tree, selecting multiple processes and acting on them all at once.
Tasks related to processes (killing, renicing) can be done without entering their PIDs.
4. Using sar command
An acronym for System Activity Report, this command is used to collect, report, or save system activity information in Unix like operating system.
The sysntax for sar is as follows:
sar [options] [interval [count] ]
-- Install sar on Ubuntu/Debian --- $ sudo apt -y install sysstat -- Install sar on Arch/Manjaro --- $ sudo pacman -S sysstat -- Install sar on Fedora / CentOS --- $ sudo dnf install sysstat $ sudo yum install sysstat
After installation, enable sysstat to start collecting system information. Change ENABLED=”false” to ENABLED=”true” in the config file as shown below. After the edit, restart sysstat.
$ cat /etc/default/sysstat # # Default settings for /etc/init.d/sysstat, /etc/cron.d/sysstat # and /etc/cron.daily/sysstat files # # Should sadc collect system activity informations? Valid values # are "true" and "false". Please do not put other values, they # will be overwritten by debconf! ENABLED="true"
Restart sysstat after making the changes.
sudo systemctl restart sysstat
Generate CPU reports 10 times every 5 seconds
# sar 5 10 Linux 4.4.0-17134-Microsoft (Kibet-pc) 07/26/19 x86_64 (8 CPU) 10:59:40 CPU %user %nice %system %iowait %steal %idle 10:59:45 all 8.38 0.00 13.42 0.00 0.00 78.20
If you are using a GUI based window manager such as GNOME or KDE or others, look for the system monitor utility for your distribution. It can be easily installed if it is missing.
Checking cpu resources in your server is very essential in your day to day house-keeping. It is important for capacity planning and for checking the health of your system. Thank you for reading through and we hope the guide was helpful.