Linux性能优化 CPU性能调优(一)
系统的平均负载
可以通过uptime命令查看系统的平均负载: (top命令可以也可以,不过显示信息更多)
$ uptime
10:35:08 up 23 days, 19:29, 3 users, load average: 0.11, 0.07, 0.06
执行uptime命令,可以看到 当前时间、系统运行时间以及正在登录用户数。后面是过去 1 分钟、5 分钟、15 分钟的平均负载。
通过man uptime看一下到底什么是平均负载:
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.
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.
也就是说,平均负载是指单位时间内,系统处于可运行状态和不可中断状态的平均进程数,也就是平均活跃进程数。
- 可运行状态:使用CPU或者正在等待使用CPU的进程所处的状态
- 不可中断状态:进程在等待硬件设备IO等时的状态,这些进程不可打断,用kill也不能消灭。 ps状态下是D状态的进程。当一个进程向磁盘读写数据时,为了保证数据的一致性,在得到磁盘回复前,它是不能被其他进程或者中断打断的,这个时候的进程就处于不可中断状态。如果此时的进程被打断了,就容易出现磁盘数据与进程数据不一致的问题。也就是说这是一种保护机制。 (当一个机器的D状态进程较多,通常是有一些问题的)
因此,想通过平均负载来看系统的繁忙情况,需要知道自己的系统有多少个CPU 【可以通过top或者查看/proc/cpuinfo查看】。如果是单CPU并且平均负载是1,这说明CPU一直被占用。如果是4CPU的机器平均负载是1的话,说明CPU的75%是空闲的。如果是单CPU平均负载是4,那么说明大部分进程都是在竞争CPU并且竞争不到的。
一般当平均负载高于CPU数量的0.7时,就说明可能是存在问题导致高负载了,高于1的话就需要找一下问题修复了,如果更高的话可能导致响应慢等情况了。
一般来说CPU的使用率和平均负载的关系如下:
- 对于CPU 密集型进程,会单位时间使用大量 CPU ,因此平均负载会较大,这种情况二者是较为一致的
- 对于IO密集型进程,大部分时间都在等待IO,因此平均负载也会比较大,但是CPU使用率是比较低的
案例分析
工具:stress (系统压力测试工具)和sysstat (监控分析系统性能的工具) 下载方法:用yum install 即可 或者 apt-get install
环境:Xshell连接的Linux远程主机,版本3.10
需要开启多个终端,部分终端用于运行监测程序,部分终端用来运行实例模拟高负载 【虚拟机图形界面的话可以创建多个命令行即可,远程Linux则建立多个连接窗口即可】
可以看一下man stress,可以模拟多种系统压力施加
-
模拟高CPU密集的进程
stress -c 4 #运行4个高CPU进程
在一个终端运行stress,另外的终端用于监视系统负载以及其他性能
top监视的情况:
[root@jessy ~]# top top - 14:33:58 up 23 days, 23:28, 4 users, load average: 4.28, 1.42, 0.54 Tasks: 118 total, 6 running, 111 sleeping, 1 stopped, 0 zombie %Cpu(s): 99.7 us, 0.2 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 8008684 total, 5493028 free, 195948 used, 2319708 buff/cache KiB Swap: 0 total, 0 free, 0 used. 7506152 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3128 root 20 0 7312 96 0 R 99.3 0.0 1:25.59 stress 3126 root 20 0 7312 96 0 R 99.0 0.0 1:25.34 stress 3127 root 20 0 7312 96 0 R 99.0 0.0 1:25.38 stress 3129 root 20 0 7312 96 0 R 96.7 0.0 1:24.92 stress 3146 root 20 0 159952 2200 1500 R 0.7 0.0 0:00.13 top
可以看到平均负载逐步上升接近到4,并且可以看到有4个CPU使用率接近100%的进程,平均CPU使用率几乎达到100%,几乎所有的时间都在用户态
mpstat监视情况:
[root@jessy ~]# mpstat -P ALL 5 1 Linux 3.10.0-1062.18.1.el7.x86_64 (jessy) 07/22/2020 _x86_64_ (4 CPU) 02:32:47 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 02:32:52 PM all 99.74 0.00 0.19 0.00 0.00 0.06 0.00 0.00 0.00 0.00 02:32:52 PM 0 99.44 0.00 0.28 0.00 0.00 0.28 0.00 0.00 0.00 0.00 02:32:52 PM 1 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:32:52 PM 2 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:32:52 PM 3 99.42 0.00 0.29 0.00 0.00 0.29 0.00 0.00 0.00 0.00 Average: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle Average: all 99.74 0.00 0.19 0.00 0.00 0.06 0.00 0.00 0.00 0.00 Average: 0 99.44 0.00 0.28 0.00 0.00 0.28 0.00 0.00 0.00 0.00 Average: 1 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: 2 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: 3 99.42 0.00 0.29 0.00 0.00 0.29 0.00 0.00 0.00 0.00
mpstst可以看到每个cpu 的情况,可知每个CPU都有接近100%的使用率
pidstat监视的情况:
[root@jessy ~]# pidstat 1 1 Linux 3.10.0-1062.18.1.el7.x86_64 (jessy) 07/22/2020 _x86_64_ (4 CPU) 02:37:07 PM UID PID %usr %system %guest %CPU CPU Command 02:37:08 PM 0 3126 99.01 0.00 0.00 99.01 1 stress 02:37:08 PM 0 3127 100.00 0.00 0.00 100.00 2 stress 02:37:08 PM 0 3128 98.02 0.00 0.00 98.02 3 stress 02:37:08 PM 0 3129 99.01 0.00 0.00 99.