此文于【《Linux性能优化实战》作者:倪朋飞】基础上演变而成
一:linux性能理论基础
1、系统卡了怎么看!
大多人第一反应很可能是top或者uptime一下
sanxi@sanxi-PC:~$ uptime 16:42:03 up 5:09, 1 user, load average: 0.52, 0.62, 0.67
uptime可以查看当前CPU平均负载状况:
-
16:42:03 当前时间
-
up 5:09 系统运行时间五小时九分
-
1 user 当前登录用户为1
-
load average: 0.52, 0.62, 0.67 1、5、15分钟内CPU平均负载值
2、平均负载简述
平均负载是指单位时间内,系统处于可运行状态和不可中断状态的平均进程数
-
可运行状态的进程:
- 正在使用 CPU 或者正在等待 CPU 的进程,即用ps命令时的R状态(runtime),如下所示
sanxi@sanxi-PC:~$ ps aux
sanxi 29659 0.0 0.0 11668 3164 pts/3 R+ 17:55 0:00 ps aux
-
不可中断状态的进程:
- 正处于内核态关键流程中的进程,且不可打断,如等待硬件设备的I/O 响应,也就是在 ps 命令中看到的D状态(Uninterruptible Sleep,也称为 Disk Sleep)的进程
- 当一个进程向磁盘读写数据时,为保证数据一致性,在得到磁盘响应前,它是不能被打断的,此时的进程就处于不可中断状态。如被打断,则容易出现磁盘数据与进程数据不一致的问题。
在文章开头时负载值是0.52,是不是意味着负载很高?不一定,这要和CPU核心数对应起来!如果只有一颗核心数,那当然算高了,但是我这台机器是拥有12核心数的,相当于(52/12)%=4.3%负载,不足为虑!
3、平均负载≠CPU使用率
-
CPU平均负载:
- 单位时间内,处于可运行状态和不可中断状态的进程数。所以,它不仅包括了正在使用 CPU 的进程,还包括等待 CPU 和等待 I/O 的进程。
-
CPU使用率:
- 单位时间内 CPU 繁忙情况的统计
为什么这么说呢?看下面的举例
-
CPU 密集型进程:使用大量 CPU 会导致平均负载升高,此时这两者是一致的;比如说编译!
-
I/O 密集型进程:等待 I/O 也会导致平均负载升高,但 CPU 使用率不一定很高;像zabbix采集监控数据!
-
进程过多:大量等待 CPU 的进程调度也会导致平均负载升高,此时的 CPU 使用率也会比较高;比如并发过高!
二、实践出真理
-
测试工具包:
- stress:Linux系统压力测试工具,此处用于模拟平均负载过高场景
- sysstat:Linux性能工具包,此文章用到的是mpstat、pidstat
- mpstat:多核CPU性能分析工具,可实时查看每颗CPU性能指标及所有CPU平均指标。
- pidstat:进程性能分析工具;可实时查看进程的CPU、内存、I/O及上下文切换等指标。
1、CPU密集型验证
1.1、模拟CPU负载100%
sanxi@sanxi-PC:~$ stress --cpu 1 --timeout 600
stress: info: [23297] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
1.2、高亮显示变化部分
sanxi@sanxi-PC:~$ watch -d uptime
Every 2.0s: uptime sanxi-PC: Tue Aug 11 17:01:16 2020
17:01:16 up 5:29, 1 user, load average: 1.34, 0.91, 0.79
1.3、查看CPU性能指标
#-P ALL指所有CPU,5代表5秒输出一次数据
sanxi@sanxi-PC:~$ mpstat -P ALL 5
Linux 5.3.0-3-amd64 (sanxi-PC) 2020年08月11日 _x86_64_ (12 CPU)
17时00分58秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
17时01分03秒 all 11.39 0.00 0.89 0.02 0.00 3.50 0.00 0.00 0.00 84.21
17时01分03秒 0 1.14 0.00 0.65 0.00 0.00 20.68 0.00 0.00 0.00 77.52
17时01分03秒 1 2.74 0.00 0.91 0.00 0.00 9.69 0.00 0.00 0.00 86.65
17时01分03秒 2 3.50 0.00 0.58 0.00 0.00 4.47 0.00 0.00 0.00 91.46
17时01分03秒 3 8.38 0.00 1.60 0.20 0.00 0.80 0.00 0.00 0.00 89.02
17时01分03秒 4 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
17时01分03秒 5 5.22 0.00 0.80 0.00 0.00 0.20 0.00 0.00 0.00 93.78
17时01分03秒 6 2.77 0.00 1.19 0.00 0.00 0.99 0.00 0.00 0.00 95.06
17时01分03秒 7 6.77 0.00 1.99 0.00 0.00 0.20 0.00 0.00 0.00 91.04
17时01分03秒 8 2.42 0.00 0.81 0.00 0.00 0.00 0.00 0.00 0.00 96.77
17时01分03秒 9 2.99 0.00 1.39 0.00 0.00 0.20 0.00 0.00 0.00 95.42
17时01分03秒 10 0.80 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 99.00
17时01分03秒 11 3.23 0.00 0.61 0.00 0.00 0.20 0.00 0.00 0.00 95.96
1.4、查看进程占用资源
sanxi@sanxi-PC:~$ pidstat -u 5 1
Linux 5.3.0-3-amd64 (sanxi-PC) 2020年08月11日 _x86_64_ (12 CPU)
平均时间: UID PID %usr %system %guest %wait %CPU CPU Command
平均时间: 1000 7734 99.80 0.00 0.00 0.00 99.80 - stress
#我过滤掉了其它进程的信息
2、I/O密集型
2.1、模拟I/O满负载
sanxi@sanxi-PC:~$ stress -i 1 --timeout 600
stress: info: [7889] dispatching hogs: 0 cpu, 1 io, 0 vm, 0 hdd
2.2、查看uptime
Every 2.0s: uptime sanxi-PC: Tue Aug 11 20:10:07 2020
20:10:07 up 18 min, 1 user, load average: 1.82, 1.44, 0.89
2.3、查看CPU资源占用
sanxi@sanxi-PC:~$ mpstat -P ALL 5 1
Linux 5.3.0-3-amd64 (sanxi-PC) 2020年08月11日 _x86_64_ (12 CPU)
平均时间: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
平均时间: all 9.76 0.00 5.95 5.29 0.00 0.95 0.00 0.00 0.00 78.04
平均时间: 0 5.11 0.00 5.11 5.11 0.00 6.44 0.00 0.00 0.00 78.22
平均时间: 1 6.29 0.00 6.29 6.68 0.00 2.55 0.00 0.00 0.00 78.19
平均时间: 2 7.45 0.00 6.67 6.86 0.00 0.59 0.00 0.00 0.00 78.43
平均时间: 3 15.94 0.00 5.31 7.28 0.00 0.59 0.00 0.00 0.00 70.87
平均时间: 4 7.21 0.00 6.41 11.02 0.00 0.40 0.00 0.00 0.00 74.95
平均时间: 5 6.18 0.00 6.37 5.98 0.00 0.20 0.00 0.00 0.00 81.27
平均时间: 6 9.11 0.00 6.59 3.10 0.00 0.39 0.00 0.00 0.00 80.81
平均时间: 7 8.59 0.00 6.05 5.08 0.00 0.00 0.00 0.00 0.00 80.27
平均时间: 8 19.76 0.00 7.19 3.19 0.00 0.00 0.00 0.00 0.00 69.86
平均时间: 9 19.44 0.00 5.36 2.98 0.00 0.00 0.00 0.00 0.00 72.22
平均时间: 10 6.46 0.00 4.65 2.63 0.00 0.00 0.00 0.00 0.00 86.26
平均时间: 11 5.81 0.00 5.41 3.61 0.00 0.00 0.00 0.00 0.00 85.17
2.4、查看进程占用资源
sanxi@sanxi-PC:~$ pidstat -u 5 1
Linux 5.3.0-3-amd64 (sanxi-PC) 2020年08月11日 _x86_64_ (12 CPU)
平均时间: UID PID %usr %system %guest %wait %CPU CPU Command
平均时间: 1000 7890 0.40 6.99 0.00 1.00 7.39 - stress
3、进程过多型
1.1、模拟进程过多
#我这里是6核12线程,给了20肯定超标。
sanxi@sanxi-PC:~$ stress -c 20 --timeout 600
stress: info: [26456] dispatching hogs: 20 cpu, 0 io, 0 vm, 0 hdd
1.2、查看平均负载
#CPU要炸了
Every 2.0s: uptime sanxi-PC: Tue Aug 11 20:28:03 2020
20:28:03 up 36 min, 1 user, load average: 11.10, 4.56, 2.90
1.3、看CPU负载情况
#CPU要炸了
sanxi@sanxi-PC:~$ mpstat -P ALL 5 1
Linux 5.3.0-3-amd64 (sanxi-PC) 2020年08月11日 _x86_64_ (12 CPU)
平均时间: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
平均时间: all 98.10 0.00 1.90 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 0 99.80 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 1 93.00 0.00 7.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 2 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 3 99.80 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 4 96.40 0.00 3.60 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 5 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 6 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 7 98.60 0.00 1.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 8 94.60 0.00 5.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 9 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 10 97.21 0.00 2.79 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: 11 97.80 0.00 2.20 0.00 0.00 0.00 0.00 0.00 0.00 0.00
1.4、查看占用资源的进程
sanxi@sanxi-PC:~$ pidstat -u 5 1
Linux 5.3.0-3-amd64 (sanxi-PC) 2020年08月11日 _x86_64_ (12 CPU)
平均时间: UID PID %usr %system %guest %wait %CPU CPU Command
平均时间: 1000 10023 63.75 0.00 0.00 36.39 63.75 - stress
平均时间: 1000 10024 63.08 0.00 0.00 36.92 63.08 - stress
平均时间: 1000 10025 56.84 0.07 0.00 43.23 56.91 - stress
平均时间: 1000 10026 57.90 0.07 0.00 42.10 57.97 - stress
平均时间: 1000 10027 49.60 0.00 0.00 50.46 49.60 - stress
平均时间: 1000 10028 54.12 0.07 0.00 45.95 54.18 - stress
平均时间: 1000 10029 51.46 0.00 0.00 48.61 51.46 - stress
平均时间: 1000 10030 62.22 0.00 0.00 37.85 62.22 - stress
平均时间: 1000 10031 55.05 0.07 0.00 45.02 55.11 - stress
平均时间: 1000 10032 60.76 0.27 0.00 39.04 61.02 - stress
平均时间: 1000 10033 57.97 0.07 0.00 42.03 58.03 - stress
平均时间: 1000 10034 55.64 0.20 0.00 44.29 55.84 - stress
平均时间: 1000 10035 54.52 0.00 0.00 45.55 54.52 - stress
平均时间: 1000 10036 52.26 0.00 0.00 47.81 52.26 - stress
平均时间: 1000 10037 52.92 0.07 0.00 47.08 52.99 - stress
三、总结
CPU平均负载是快速查看系统整体性能的一种手段,反映了整体的CPU负载情况。但只看平均负载本身,我们并不能直接发现,到底是哪里出现了瓶颈。所以,在理解平均负载时,也要注意:平均负载高有可能是 CPU 密集型进程导致的;平均负载高并不一定代表 CPU 使用率高,还有可能是 I/O 更繁忙了;当发现负载高的时候,你可以使用 mpstat、pidstat 等工具,辅助分析负载的来源。