Linux性能分析-平均负载

平均负载的理解 一般系统变慢时,我们会使用top或uptime命令来查看下系统的负载情况

[root@localhost shell]# uptime 13:51:08 up 5 days, 21:50,3 users,load average: 0.00, 0.02, 0.05

load average:0.00,0.02,0.05 分别代表了1min/5min/15min的平均负载,那么平均负载到底是什么意思呢?
使用man uptime查看下详细的说明
man uptime

【Linux性能分析-平均负载】其中关于load average的解释如下:
System load averages is the average number of processes that are either in a runnable or uninterruptable state. A process in a runnable state is either using the CPU or waiting to use the CPU. A process in uninterruptable state is waiting for some I/O access, eg waiting for disk.
The averages are taken over the three time intervals. Load averages are not normalized for the number of CPUs in a system, so a load average of 1 means a single CPU system is loaded all the time while on a 4 CPU system it means it was idle 75% of the time.
可以看到,平均负载统计的是 处于runnable or uninterruptable状态的进程数量,并且这个数据需要和系统的cpu数量进行比较才有意义。
场景模拟 平均负载,计算的是进程对系统资源的需求程度,包括CPU和IO,所以平均负载高,CPU使用率不一定高。
下面模拟三种场景,这三种场景的平均负载都很高,但是分别对应的CPU密集型进程、IO密集型进程、大量等待CPU调度的进程组。
首先安装stress,stress是一个压力测试工具。
yum install -y epel-release yum install -y stress

第一种:CPU密集型 用stress执行下面的命令,模拟占用一个核
stress --cpu 1 --timeout 600

在新开的终端中查看不同核的占用情况,5代表等待5秒,20代表一共打印20次数据
可以看到第三个核的CPU占用率是100%
[root@localhost shell]# mpstat -P ALL 5 20 Linux 3.10.0-1062.el7.x86_64 (localhost.localdomain)2020年07月07日_x86_64_(4 CPU)14时11分49秒CPU%usr%nice%sys %iowait%irq%soft%steal%guest%gnice%idle 14时11分54秒all25.290.000.050.000.000.000.000.000.0074.66 14时11分54秒00.600.000.200.000.000.000.000.000.0099.20 14时11分54秒114.000.000.200.000.000.000.000.000.0085.80 14时11分54秒20.200.000.000.000.000.000.000.000.0099.80 14时11分54秒386.230.000.200.000.000.000.000.000.0013.5714时11分54秒CPU%usr%nice%sys %iowait%irq%soft%steal%guest%gnice%idle 14时11分59秒all25.230.000.050.000.000.000.000.000.0074.72 14时11分59秒00.200.000.000.000.000.000.000.000.0099.80 14时11分59秒10.200.000.000.000.000.000.000.000.0099.80 14时11分59秒20.400.000.200.000.000.000.000.000.0099.40 14时11分59秒3100.000.000.000.000.000.000.000.000.000.00

在另一个终端中,打印进程对CPU的占用情。可以看到pid为6325的stress的进程占用cpu达到100%
[root@localhost shell]# pidstat -u 5 10 Linux 3.10.0-1062.el7.x86_64 (localhost.localdomain)2020年07月07日_x86_64_(4 CPU)14时13分11秒UIDPID%usr %system%guest%CPUCPUCommand 14时13分16秒018910.400.000.000.403X 14时13分16秒025810.600.200.000.793gnome-shell 14时13分16秒026040.000.200.000.200ibus-daemon 14时13分16秒026680.000.200.000.200goa-identity-se 14时13分16秒028470.200.000.000.203vmtoolsd 14时13分16秒0632599.400.000.0099.401stress 14时13分16秒064870.000.200.000.203pidstat14时13分16秒UIDPID%usr %system%guest%CPUCPUCommand 14时13分21秒018910.400.000.000.403X 14时13分21秒025810.800.000.000.803gnome-shell 14时13分21秒06325100.000.000.00100.001stress 14时13分21秒064870.200.200.000.403pidstat14时13分21秒UIDPID%usr %system%guest%CPUCPUCommand 14时13分26秒05000.000.200.000.200xfsaild/dm-0 14时13分26秒018910.600.400.001.001X 14时13分26秒025811.000.400.001.401gnome-shell 14时13分26秒062060.200.200.000.401gnome-terminal- 14时13分26秒06325100.000.000.00100.002stress 14时13分26秒064870.000.200.000.203pidstat

第二种IO密集型 安装stress-ng
yum install stress-ng

执行下面的命令,模拟IO密集
stress-ng -i 1 --hdd 1 --timeout 600

查看mpstat,可以看到iowait%明显提高
[root@localhost shell]# mpstat -P ALL 5 20 Linux 3.10.0-1062.el7.x86_64 (localhost.localdomain)2020年07月07日_x86_64_(4 CPU)14时30分20秒CPU%usr%nice%sys %iowait%irq%soft%steal%guest%gnice%idle 14时30分25秒all0.810.0012.6837.670.001.830.000.000.0047.02 14时30分25秒00.220.0015.0832.590.001.110.000.000.0051.00 14时30分25秒11.650.0022.6337.650.000.410.000.000.0037.65 14时30分25秒20.880.008.1043.980.003.060.000.000.0043.98 14时30分25秒30.000.004.7536.290.002.810.000.000.0056.16

查看哪个应用的IO占比较高,可以看到stress的应用,对磁盘的写入很大
[root@localhost shell]# pidstat -d 5 10 | grep "stress" UIDPIDkB_rd/skB_wr/skB_ccwr/sCommand14时32分34秒067300.00 549011.95 143466.14stress-ng-hdd 14时32分34秒067310.000.0013406.37stress-ng-io

第三种大量进程场景 执行下面命令,模拟16个进程执行
stress -c 16 --timeout 600

查看uptime,可以看到最近一分钟的平均负载显著升高
[root@localhost shell]# uptime 14:38:53 up 5 days, 22:38,4 users,load average: 12.05, 5.38, 2.74

查看进程的cpu占用,可以看到stress的进程占用cpu为20左右,大量的stress进程在竞争cpu
[root@localhost shell]# pidstat -u 5 5 Linux 3.10.0-1062.el7.x86_64 (localhost.localdomain)2020年07月07日_x86_64_(4 CPU)14时38分26秒UIDPID%usr %system%guest%CPUCPUCommand 14时38分31秒018910.000.180.000.182X 14时38分31秒025810.180.180.000.371gnome-shell 14时38分31秒0722523.290.000.0023.293stress 14时38分31秒0722623.480.000.0023.480stress 14时38分31秒0722723.110.000.0023.113stress 14时38分31秒0722823.290.000.0023.290stress 14时38分31秒0722923.290.000.0023.293stress 14时38分31秒0723023.110.000.0023.112stress 14时38分31秒0723123.110.000.0023.111stress 14时38分31秒0723223.110.000.0023.110stress 14时38分31秒0723323.290.000.0023.291stress 14时38分31秒0723423.110.000.0023.112stress 14时38分31秒0723523.290.000.0023.291stress 14时38分31秒0723623.480.000.0023.482stress 14时38分31秒0723723.290.000.0023.290stress 14时38分31秒0723823.290.000.0023.293stress 14时38分31秒0723923.110.000.0023.111stress 14时38分31秒0724022.920.000.0022.922stress

    推荐阅读