In the Linux system maintenance, you may need to check the CPU usage frequently and analyze the overall operation of the system. The performance of monitoring CPU generally includes the following 3 points: run queue, CPU usage and context switching.
Alibaba Cloud Simple Application Server: Anti COVID-19 SME Enablement Program
$300 coupon package for all new SMEs and a $500 coupon for paying customers.
For each CPU, the run queue should not exceed 3, for example, if it is a dual-core CPU, it should not exceed 6. If the queue stays above 3 for a long time, it means that any process cannot get a response from the cpu immediately when it is running. At this time, you may need to consider upgrading the cpu. In addition, it is best to keep the CPU usage at full load at 65%~70% for user space, 30% for system space, and 0% to 5% for free space.
Below is a summary of several commands commonly used to view CPU usage.
1. top
This command is very commonly used. The current CPU usage is displayed on the third line.
[root@li676-235 ~]# top -bn 1 -i -c
top-14:19:51 up 138 days, 7:15, 1 user, load average: 0.20, 0.33, 0.39
Tasks: 115 total, 1 running, 114 sleeping, 0 stopped, 0 zombie
Cpu(s): 4.5%us, 3.8%sy, 0.0%ni, 91.0%id, 0.6%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1014660k total, 880512k used, 134148k free, 264904k buffers
Swap: 262140k total, 34788k used, 227352k free, 217144k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
12760 root 20 0 15084 1944 1632 R 2.0 0.2 0:00.01 top -bn 1 -i -c
root@li676-235 ~]# top -bn 1 -i -c
top-14:19:51 up 138 days, 7:15, 1 user, load average: 0.20, 0.33, 0.39
Tasks: 115 total, 1 running, 114 sleeping, 0 stopped, 0 zombie
Cpu(s): 4.5%us, 3.8%sy, 0.0%ni, 91.0%id, 0.6%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1014660k total, 880512k used, 134148k free, 264904k buffers
Swap: 262140k total, 34788k used, 227352k free, 217144k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
12760 root 20 0 15084 1944 1632 R 2.0 0.2 0:00.01 top -bn 1 -i -c
As shown above, the top command can see the overall system operating status and cpu usage.
%us: indicates the CPU usage rate of user space programs (not through nice scheduling)
%sy: indicates the CPU usage rate of the system space, mainly the kernel program.
%ni: Represents the cpu usage rate of programs scheduled by nice in user space.
%id: idle cpu
%wa: time waiting for io while cpu is running
%hi: the number of hard interrupts handled by the cpu
%si: the number of soft interrupts handled by the cpu
%st: cpu stolen by the virtual machine
2. vmstat
I have introduced the use of vmstat in this blog before. For detailed usage and parameter introduction, please refer to the website: http://www.sijitao.net/1925.html.
[root@li676-235 ~]# vmstat 1 5
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 34792 68376 147688 356132 16 16 421 385 2 2 5 4 91 1 0
3 0 34792 97368 147688 326884 0 0 0 0 133 134 15 6 79 0 0
1 0 34792 62432 147696 360704 0 0 0 88 267 278 63 18 19 0 0
0 0 34792 97160 147696 326904 0 0 0 0 71 82 12 4 84 0 0
1 0 34792 56572 147696 364540 0 0 0 4 261 246 61 23 16 0 0
root@li676-235 ~]# vmstat 1 5
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 34792 68376 147688 356132 16 16 421 385 2 2 5 4 91 1 0
3 0 34792 97368 147688 326884 0 0 0 0 133 134 15 6 79 0 0
1 0 34792 62432 147696 360704 0 0 0 88 267 278 63 18 19 0 0
0 0 34792 97160 147696 326904 0 0 0 0 71 82 12 4 84 0 0
1 0 34792 56572 147696 364540 0 0 0 4 261 246 61 23 16 0 0
3. sar
The sar command syntax is the same as vmstat. The sysstat package needs to be installed when the command does not exist. This package is useful.
CPU usage
For example, the CPU usage rate is collected every 1 second for 5 times in total.
[root@li676-235 ~]# sar -u 1 5
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
02:41:25 PM CPU %user %nice %system %iowait %steal %idle
02:41:26 PM all 64.71 0.00 18.63 0.00 0.98 15.69
02:41:27 PM all 29.47 0.00 22.11 14.74 0.00 33.68
02:41:28 PM all 67.33 0.00 31.68 0.99 0.00 0.00
02:41:29 PM all 7.00 0.00 2.00 0.00 0.00 91.00
02:41:30 PM all 69.00 0.00 23.00 0.00 0.00 8.00
Average: all 47.79 0.00 19.48 3.01 0.20 29.52
root@li676-235 ~]# sar -u 1 5
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
02:41:25 PM CPU %user %nice %system %iowait %steal %idle
02:41:26 PM all 64.71 0.00 18.63 0.00 0.98 15.69
02:41:27 PM all 29.47 0.00 22.11 14.74 0.00 33.68
02:41:28 PM all 67.33 0.00 31.68 0.99 0.00 0.00
02:41:29 PM all 7.00 0.00 2.00 0.00 0.00 91.00
02:41:30 PM all 69.00 0.00 23.00 0.00 0.00 8.00
Average: all 47.79 0.00 19.48 3.01 0.20 29.52
Like top, you can see the usage of all CPUs. If you need to check the usage of a certain cpu, you can use the -P parameter. For example, specify to display the usage of CPU No. 0.
[root@li676-235 ~]# sar -P 0 -u 1 5
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
02:45:14 PM CPU %user %nice %system %iowait %steal %idle
02:45:15 PM 0 44.00 0.00 52.00 4.00 0.00 0.00
02:45:16 PM 0 9.28 0.00 26.80 62.89 1.03 0.00
02:45:17 PM 0 3.06 0.00 14.29 81.63 1.02 0.00
02:45:18 PM 0 4.12 0.00 22.68 72.16 1.03 0.00
02:45:19 PM 0 4.12 0.00 22.68 72.16 1.03 0.00
Average: 0 13.09 0.00 27.81 58.28 0.82 0.00
root@li676-235 ~]# sar -P 0 -u 1 5
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
02:45:14 PM CPU %user %nice %system %iowait %steal %idle
02:45:15 PM 0 44.00 0.00 52.00 4.00 0.00 0.00
02:45:16 PM 0 9.28 0.00 26.80 62.89 1.03 0.00
02:45:17 PM 0 3.06 0.00 14.29 81.63 1.02 0.00
02:45:18 PM 0 4.12 0.00 22.68 72.16 1.03 0.00
02:45:19 PM 0 4.12 0.00 22.68 72.16 1.03 0.00
Average: 0 13.09 0.00 27.81 58.28 0.82 0.00
Process queue length and average load status
For example, collecting once every 1 second, collecting 5 times in total.
[root@li676-235 ~]# sar -q 1 5
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
02:48:47 PM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15
02:48:48 PM 1 133 0.34 0.43 0.41
02:48:49 PM 2 132 0.34 0.43 0.41
02:48:50 PM 1 133 0.34 0.43 0.41
02:48:51 PM 2 134 0.31 0.42 0.40
02:48:52 PM 1 133 0.31 0.42 0.40
Average: 1 133 0.33 0.43 0.41
root@li676-235 ~]# sar -q 1 5
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
02:48:47 PM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15
02:48:48 PM 1 133 0.34 0.43 0.41
02:48:49 PM 2 132 0.34 0.
43 0.41
02:48:50 PM 1 133 0.34 0.43 0.41
02:48:51 PM 2 134 0.31 0.42 0.40
02:48:52 PM 1 133 0.31 0.42 0.40
Average: 1 133 0.33 0.43 0.41
Output items:
runq-sz: The length of the run queue (the number of processes waiting to run)
plist-sz: the number of processes and threads in the process list
ldavg-1: System load average in the last minute
ldavg-5: The average load of the system in the past 5 minutes
ldavg-15: The average load of the system in the past 15 minutes
Average process creation and number of context switches
For example, collecting once every 1 second, collecting 5 times in total.
[root@li676-235 ~]# sar -w 1 5
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
02:54:03 PM proc/s cswch/s
02:54:04 PM 1.01 156.57
02:54:05 PM 1.00 132.00
02:54:06 PM 2.00 201.00
02:54:07 PM 2.02 126.26
02:54:08 PM 2.00 114.00
Average: 1.61 145.98
root@li676-235 ~]# sar -w 1 5
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
02:54:03 PM proc/s cswch/s
02:54:04 PM 1.01 156.57
02:54:05 PM 1.00 132.00
02:54:06 PM 2.00 201.00
02:54:07 PM 2.02 126.26
02:54:08 PM 2.00 114.00
Average: 1.61 145.98
The sar command can also get the performance parameters of the specified date in the past.
[root@li676-235 ~]# sar -u -f /var/log/sa/sa20
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
01:10:01 PM CPU %user %nice %system %iowait %steal %idle
01:20:02 PM all 25.78 0.00 8.88 3.59 0.15 61.60
01:30:01 PM all 26.06 0.00 9.96 3.33 0.10 60.55
01:40:01 PM all 25.73 0.00 9.17 3.70 0.09 61.32
01:50:01 PM all 25.70 0.00 9.50 2.79 0.12 61.89
02:00:01 PM all 26.70 0.00 9.73 2.20 0.10 61.28
02:10:01 PM all 26.16 0.00 9.56 4.34 0.11 59.82
02:20:01 PM all 25.49 0.00 9.61 2.76 0.07 62.07
02:30:01 PM all 26.47 0.00 9.94 0.64 0.30 62.65
02:40:02 PM all 27.32 0.00 10.37 3.86 0.15 58.30
02:50:02 PM all 26.98 0.00 10.38 4.56 0.13 57.95
Average: all 26.24 0.00 9.71 3.18 0.13 60.74
root@li676-235 ~]# sar -u -f /var/log/sa/sa20
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
01:10:01 PM CPU %user %nice %system %iowait %steal %idle
01:20:02 PM all 25.78 0.00 8.88 3.59 0.15 61.60
01:30:01 PM all 26.06 0.00 9.96 3.33 0.10 60.55
01:40:01 PM all 25.73 0.00 9.17 3.70 0.09 61.32
01:50:01 PM all 25.70 0.00 9.50 2.79 0.12 61.89
02:00:01 PM all 26.70 0.00 9.73 2.20 0.10 61.28
02:10:01 PM all 26.16 0.00 9.56 4.34 0.11 59.82
02:20:01 PM all 25.49 0.00 9.61 2.76 0.07 62.07
02:30:01 PM all 26.47 0.00 9.94 0.64 0.30 62.65
02:40:02 PM all 27.32 0.00 10.37 3.86 0.15 58.30
02:50:02 PM all 26.98 0.00 10.38 4.56 0.13 57.95
Average: all 26.24 0.00 9.71 3.18 0.13 60.74
4. mpstat
This command is also in the sysstat package, and the syntax is similar.
For example, collect once every 1 second, a total of 5 times.
[root@li676-235 ~]# mpstat 1 5
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
03:01:18 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
03:01:19 PM all 52.53 0.00 23.23 0.00 0.00 0.00 0.00 0.00 24.24
03:01:20 PM all 21.00 0.00 4.00 0.00 0.00 0.00 0.00 0.00 75.00
03:01:21 PM all 53.00 0.00 18.00 0.00 0.00 0.00 0.00 0.00 29.00
03:01:22 PM all 26.00 0.00 3.00 0.00 0.00 0.00 0.00 0.00 71.00
03:01:23 PM all 46.00 0.00 18.00 0.00 0.00 0.00 0.00 0.00 36.00
Average: all 39.68 0.00 13.23 0.00 0.00 0.00 0.00 0.00 47.09
root@li676-235 ~]# mpstat 1 5
Linux 3.18.5-x86_64-linode52 (li676-235) 07/20/2015 _x86_64_ (1 CPU)
03:01:18 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
03:01:19 PM all 52.53 0.00 23.23 0.00 0.00 0.00 0.00 0.00 24.24
03:01:20 PM all 21.00 0.00 4.00 0.00 0.00 0.00 0.00 0.00 75.00
03:01:21 PM all 53.00 0.00 18.00 0.00 0.00 0.00 0.00 0.00 29.00
03:01:22 PM all 26.00 0.00 3.00 0.00 0.00 0.00 0.00 0.00 71.00
03:01:23 PM all 46.00 0.00 18.00 0.00 0.00 0.00 0.00 0.00 36.00
Average: all 39.68 0.00 13.23 0.00 0.00 0.00 0.00 0.00 47.09
The cpu usage is more detailed than sar, and you can also specify a cpu with -P.