Linux-CPU性能篇

平均负载

  • 单位时间内系统处于可运行状态和不可中断状态的平均进程数,即平均活跃进程数。
  • 进程状态:Running(R)
  • 不可中断状态:Uninterruptible Sleep(D)
[root@cnsz-ansible-110 ~]# w
 09:53:36 up 58 days, 18:49,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      31Jan21 52days  0.05s  0.05s -bash
root     pts/0    10.240.20.5      09:51    0.00s  0.06s  0.01s w
  • 合理的平均负载值
  1. 获取当前机器的cpu核数
  2. 平均负载高于CPU数量70%,需考虑负载过高的情况
[root@cnsz-ansible-110 ~]# grep 'model name' /proc/cpuinfo 
model name	: Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz
model name	: Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz
model name	: Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz
model name	: Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz
[root@cnsz-ansible-110 ~]# grep 'model name' /proc/cpuinfo | wc -l
4

平均负载案例分析

  • Ubuntu20.4 4C8G,安装stress sysstat
  • stress: Linux系统压力测试工具
  • sysstat: Linux性能工具
apt update; apt install stress sysstat -y
  • 当前系统负载
root@CNSZ-K8S-NFS-SERVER:~# uptime
 02:22:47 up 26 days, 16:25,  2 users,  load average: 0.04, 0.05, 0.01
root@CNSZ-K8S-NFS-SERVER:~# 
  • CPU密集进程
# 消耗一个cpu,运行10min
$ stress --cpu 1 --timeout 600

1. 终端二,查看平均负载变化情况
$ watch -d uptime 

最终结果
root@CNSZ-K8S-NFS-SERVER:~# uptime
 02:31:11 up 26 days, 16:33,  3 users,  load average: 1.04, 0.71, 0.34

2. 终端三,运行mpstat查看cpu使用情况
# -P ALL 表示监控所有cpu,每5秒输出一组
root@CNSZ-K8S-NFS-SERVER:~# mpstat -P ALL 5
Linux 5.4.0-42-generic (CNSZ-K8S-NFS-SERVER) 	03/25/2021 	_x86_64_	(4 CPU)

02:27:38 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
02:27:43 AM  all   24.99    0.00    0.10    0.00    0.00    0.05    0.00    0.00    0.00   74.86
02:27:43 AM    0    0.00    0.00    0.20    0.00    0.00    0.20    0.00    0.00    0.00   99.60
02:27:43 AM    1    0.00    0.00    0.20    0.00    0.00    0.00    0.00    0.00    0.00   99.80
02:27:43 AM    2    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
02:27:43 AM    3  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00

02:27:43 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
02:27:48 AM  all   25.07    0.00    0.05    0.00    0.00    0.05    0.00    0.00    0.00   74.83
02:27:48 AM    0    0.20    0.00    0.20    0.00    0.00    0.20    0.00    0.00    0.00   99.40
02:27:48 AM    1    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
02:27:48 AM    2    0.20    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.80
02:27:48 AM    3  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00

3. 查询导致cpu100%的进程
# 间隔5秒输出一组数据
root@CNSZ-K8S-NFS-SERVER:~# pidstat -u 5 1
Linux 5.4.0-42-generic (CNSZ-K8S-NFS-SERVER) 	03/25/2021 	_x86_64_	(4 CPU)

02:32:28 AM   UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
02:32:33 AM     0   1985666   99.80    0.00    0.00    0.00   99.80     3  stress
02:32:33 AM     0   1986009    0.00    0.20    0.00    0.00    0.20     0  pidstat

Average:      UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
Average:        0   1985666   99.80    0.00    0.00    0.00   99.80     -  stress
Average:        0   1986009    0.00    0.20    0.00    0.00    0.20     -  pidstat
  • 结论

  • 1分钟负载会慢慢增加到1.00

  • 终端三正好有一个cpu使用率100%,但iowait为0,说明平均负载升高由于CPU使用率增加导致

  • I/O密集型进程

  1. 模拟I/O压力
$ stress -i 1 --timeout 600
  1. uptime平均负载变化情况
root@CNSZ-K8S-NFS-SERVER:~# uptime 
 02:38:27 up 26 days, 16:40,  3 users,  load average: 0.94, 0.86, 0.57
  1. CPU使用率变化情况
root@CNSZ-K8S-NFS-SERVER:~# mpstat -P ALL 5 1
Linux 5.4.0-42-generic (CNSZ-K8S-NFS-SERVER) 	03/25/2021 	_x86_64_	(4 CPU)

02:39:29 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
02:39:34 AM  all    0.45    0.00   24.59    0.00    0.00    0.25    0.00    0.00    0.00   74.71
02:39:34 AM    0    1.00    0.00   68.94    0.00    0.00    0.00    0.00    0.00    0.00   30.06
02:39:34 AM    1    0.79    0.00   29.31    0.00    0.00    0.79    0.00    0.00    0.00   69.11
02:39:34 AM    2    0.00    0.00    0.00    0.00    0.00    0.20    0.00    0.00    0.00   99.80
02:39:34 AM    3    0.00    0.00    0.20    0.00    0.00    0.00    0.00    0.00    0.00   99.80
posted @ 2021-08-11 18:13  独孤云翔  阅读(137)  评论(0)    收藏  举报