1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Linux性能优化 CPU性能调优

Linux性能优化 CPU性能调优

时间:2019-05-19 09:37:57

相关推荐

Linux性能优化 CPU性能调优

Linux性能优化 CPU性能调优(一)

系统的平均负载

可以通过uptime命令查看系统的平均负载: (top命令可以也可以,不过显示信息更多)

$ uptime10: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 ~]# toptop - 14:33:58 up 23 days, 23:28, 4 users, load average: 4.28, 1.42, 0.54Tasks: 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 stKiB Mem : 8008684 total, 5493028 free, 195948 used, 2319708 buff/cacheKiB Swap: 0 total, 0 free, 0 used. 7506152 avail Mem PID USERPR NI VIRT RES SHR S %CPU %MEMTIME+ COMMAND3128 root20 0 7312960 R 99.3 0.0 1:25.59 stress 3126 root20 0 7312960 R 99.0 0.0 1:25.34 stress 3127 root20 0 7312960 R 99.0 0.0 1:25.38 stress 3129 root20 0 7312960 R 96.7 0.0 1:24.92 stress 3146 root20 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 1Linux 3.10.0-1062.18.1.el7.x86_64 (jessy) 07/22/ _x86_64_(4 CPU)02:32:47 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle02:32:52 PM all 99.74 0.00 0.19 0.00 0.00 0.06 0.00 0.00 0.00 0.0002:32:52 PM 0 99.44 0.00 0.28 0.00 0.00 0.28 0.00 0.00 0.00 0.0002:32:52 PM 1 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.0002:32:52 PM 2 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.0002:32:52 PM 3 99.42 0.00 0.29 0.00 0.00 0.29 0.00 0.00 0.00 0.00Average:CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idleAverage:all 99.74 0.00 0.19 0.00 0.00 0.06 0.00 0.00 0.00 0.00Average: 0 99.44 0.00 0.28 0.00 0.00 0.28 0.00 0.00 0.00 0.00Average: 1 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00Average: 2 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00Average: 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 1Linux 3.10.0-1062.18.1.el7.x86_64 (jessy) 07/22/ _x86_64_(4 CPU)02:37:07 PM UID PID %usr %system %guest %CPU CPU Command02:37:08 PM03126 99.01 0.00 0.00 99.011 stress02:37:08 PM03127 100.00 0.00 0.00 100.002 stress02:37:08 PM03128 98.02 0.00 0.00 98.023 stress02:37:08 PM03129 99.01 0.00 0.00 99.010 stress02:37:08 PM UID PID %us

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。