cpu和核心关系[root@catlogs]#lscpuArchitecture:x86_64CPUop-mode(s):32-bit,64-bitByteOrder:LittleEndianCPU(s):8//8个核心On-lineCPU(s)list:0-7Thread(s)percore:2Core(s)persocket:4//每颗cpu有几个核心Socket(s):1//1颗cpuNUM

[root@cat logs]# lscpuArchitecture:x86_64CPU op-mode(s):32-bit, 64-bitByte Order:Little EndianCPU(s):8//8个核心On-line CPU(s) list:0-7Thread(s) per core:2Core(s) per socket:4//每颗cpu有几个核心Socket(s):1//1颗cpuNUMA node(s):1Vendor ID:GenuineIntelCPU family:6Model:94Model name:Intel(R) Core(TM) i7-6700 CPU @ 3.40GHzStepping:3CPU MHz:800.000BogoMIPS:6816.07Virtualization:VT-xL1d cache:32KL1i cache:32KL2 cache:256KL3 cache:8192KNUMA node0 CPU(s):0-7
1分钟、5分钟、15分钟
$ uptime 07:40:47 up 24 min,2 users,load average: 0.14, 0.14, 0.19
- 单位时间 内,系统处于 可运行状态 和 不可中断状态 的平均进程数,也就是 平均活跃进程数 ,它和CPU使用率并没有直接关系
- 所谓 可运行状态 的进程,是指 正在使用CPU 或者 正在等待CPU 的进程也就是我们常用ps命令看到的,处于 R 状态( Running 或 Runnable )的进程
- 不可中断状态 的进程则是正处于 内核态关键流程 中的进程,并且这些流程是 不可打断 的比如最常见的是 等待硬件设备的I/O响应也就是我们在ps命令中看到的 D 状态( Uninterruptible Sleep ,也称为 Disk Sleep )的进程当一个进程向磁盘读写数据时,为了 保证数据的一致性在得到磁盘回复前,它是不能被其他进程或者中断打断的,这个时候的进程就处于不可中断状态不可中断状态实际上是系统对 进程 和 硬件设备 的一种 保护机制
$ grep 'model name' /proc/cpuinfo | wc -l32$ top 1top - 08:04:15 up 48 min,2 users,load average: 0.07, 0.09, 0.09Tasks: 201 total,1 running, 200 sleeping,0 stopped,0 zombie%Cpu0:0.0 us,0.0 sy,0.0 ni,100.0 id,0.0 wa,0.0 hi,0.0 si,0.0 st%Cpu1:0.0 us,0.0 sy,0.0 ni, 99.7 id,0.0 wa,0.0 hi,0.3 si,0.0 st
关注时机: 平均负载 >= CPU * 70%
平均负载 / CPU 使用率- 平均负载 – 平均活跃进程数CPU 使用率 – 单位时间内 CPU繁忙情况 的统计单位时间内,处于 可运行状态 和 不可中断状态 的进程数
- 两者不一定完全对应CPU密集型 进程,使用大量CPU会导致平均负载升高,此时这两者是 一致 的I/O密集型 进程, 等待I/O 也会导致平均负载升高,但 CPU使用率不一定很高(并发)大量等待CPU的进程调度 也会导致平均负载升高,此时的CPU使用率也会比较高
[root@cat logs]# w 09:40:30 up 441 days, 23:36,5 users,load average: 0.92, 1.12, 1.22USERTTYFROMLOGIN@IDLEJCPUPCPU WHATroottty1-01Jun21 441days0.17s0.17s -bashliexin52 pts/0192.168.1.16808Aug220.00s0.19s0.03s sshd: liexin521 [priv]liexin52 pts/1lcrm_new.liexin. 08Aug22 46:41m0.09s0.02s sshd: liexin521 [priv]rootpts/3192.168.1.1826Jul22 43:25m0.08s0.08s -bashrootpts/8192.168.1.192Mon1245:34m0.07s0.07s -bash
工具 | 用途 |
stress | Linux系统压力测试工具 |
sysstat | 常用的 Linux 性能工具,监控和分析系统的性能 |
mpstat :多核CPU性能分析工具,用来实时查看每个CPU的性能指标,以及所有CPU的平均指标 | |
pidstat :进程性能分析工具,用来实时查看进程的CPU、内存、I/O以及上下文切换等性能指标 |
平均负载高,CPU 使用率也高
发起压力root@ubuntu:~# stress --cpu 1 --timeout 600stress: info: [4783] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
root@ubuntu:~# watch -d uptime 08:35:03 up1:19,5 users,load average: 1.08, 0.89, 0.50
CPU 1的使用率为99.8%,但 iowait为0,平均负载的升高正是由于 CPU 的使用率为100%
root@ubuntu:~# mpstat -P ALL 5Linux 5.4.0-62-generic (ubuntu)02/02/21_x86_64_(2 CPU)08:40:12CPU%usr%nice%sys%iowait%irq%soft%steal%guest%gnice%idle08:40:17all50.050.000.200.000.000.100.000.000.0049.6508:40:1700.000.000.200.000.000.200.000.000.0099.6008:40:17199.800.000.200.000.000.000.000.000.000.00
root@ubuntu:~# pidstat -u 5 1Linux 5.4.0-62-generic (ubuntu)02/02/21_x86_64_(2 CPU)08:40:26UIDPID%usr %system%guest%wait%CPUCPUCommand08:40:3106940.000.200.000.000.201multipathd08:40:3107430.200.000.000.000.200vmtoolsd08:40:31038140.000.400.000.000.400kworker/0:1-events08:40:310618799.800.000.000.2099.801stressAverage:UIDPID%usr %system%guest%wait%CPUCPUCommandAverage:06940.000.200.000.000.20-multipathdAverage:07430.200.000.000.000.20-vmtoolsdAverage:038140.000.400.000.000.40-kworker/0:1-eventsAverage:0618799.800.000.000.2099.80-stress
平均负载高,但 CPU 使用率不高
发起压力模拟IO 压力,不断执行sync
root@ubuntu:~# stress --io 1 --timeout 600stress: info: [8941] dispatching hogs: 0 cpu, 1 io, 0 vm, 0 hdd
root@ubuntu:~# watch -d uptime 08:59:04 up1:43,5 users,load average: 1.06, 0.58, 0.37
CPU 使用率不高,但 %iowait 很高
root@ubuntu:~# mpstat -P ALL 5Linux 5.4.0-62-generic (ubuntu)02/02/21_x86_64_(2 CPU)08:55:52CPU%usr%nice%sys%iowait%irq%soft%steal%guest%gnice%idle08:55:57all0.210.0012.0732.670.000.210.000.000.0054.8408:55:5700.430.0023.8767.530.000.430.000.000.007.7408:55:5710.000.000.810.200.000.000.000.000.0098.99
压力测试:8个进程 一个进程写1g
iostat
[root@host-234 ~]# iostatLinux 3.10.0-1127.19.1.el7.x86_64 (host-234)2022年08月17日_x86_64_(32 CPU)avg-cpu:%user%nice %system %iowait%steal%idle 1.610.000.740.300.0097.34Device:tpskB_read/skB_wrtn/skB_readkB_wrtnsdb14.401.41308.5653843909 11814063450sda17.4713.29373.83508979129 14313025448dm-03.989.9127.04379521960 1035293816dm-10.270.290.791092915230295048dm-25.633.10345.98118521576 13246791308dm-318.151.41308.5853814784 11814705692dm-461.061276.28231.08 48866147007 8847722188dm-50.080.012.6143777699887371dm-60.100.022.5462650097072884dm-170.060.021.9367639674035226dm-180.000.000.0012995610067dm-190.000.000.0014696111486dm-100.060.091.55335334259327101dm-80.000.000.0015892363592dm-90.070.361.411379116954042151dm-130.000.020.0060833295174dm-110.000.010.0020005876432dm-122.549.9381.39380211145 3116143442dm-140.000.020.00908663107019dm-150.000.010.0022929588952dm-160.000.000.01100564352063
iotop
进程的 CPU 使用率
root@ubuntu:~# pidstat -u 5 1Linux 5.4.0-62-generic (ubuntu)02/02/21_x86_64_(2 CPU)08:40:31UIDPID%usr %system%guest%wait%CPUCPUCommand08:40:3101040.003.390.000.003.391kworker/1:1H08:40:3101090.000.400.000.000.400kworker/0:1H08:40:31029972.0035.530.003.9937.521stress08:40:31030570.000.400.000.000.400pidstat
平均负载高,CPU 使用率也高
发起压力运行进程超出 CPU 的运行能力,出现等待 CPU 的进程
root@ubuntu:~# stress --cpu 8 --timeout 600或stree -c 4 --timeout 600stress: info: [7773] dispatching hogs: 8 cpu, 0 io, 0 vm, 0 hdd
root@ubuntu:~# watch -d uptime 08:59:04 up1:43,5 users,load average: 8.10, 6.11, 3.29
CPU 的使用率都已经是100%
%iowait : Show the percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.root@ubuntu:~# mpstat -P ALL 5Linux 5.4.0-62-generic (ubuntu)02/02/21_x86_64_(2 CPU)08:55:52CPU%usr%nice%sys %iowait%irq%soft%steal%guest%gnice%idle08:55:57all100.000.000.000.000.000.000.000.000.000.0008:55:570100.000.000.000.000.000.000.000.000.000.0008:55:571100.000.000.000.000.000.000.000.000.000.00
8个进程争抢2个 CPU, %wait 很高
%wait : Percentage of CPU spent by the task while waiting to run.
root@ubuntu:~# pidstat -u 5 1Linux 5.4.0-62-generic (ubuntu)02/02/21_x86_64_(2 CPU)08:56:26UIDPID%usr %system%guest%wait%CPUCPUCommand08:56:31076440.000.200.000.000.200kworker/0:2-pm08:56:310777424.750.000.0075.0524.750stress08:56:310777524.950.000.0074.8524.951stress08:56:310777624.950.000.0075.2524.951stress08:56:310777724.750.000.0075.0524.750stress08:56:310777824.750.000.0075.2524.750stress08:56:310777924.950.000.0075.2524.951stress08:56:310778024.950.000.0075.0524.951stress08:56:310778124.750.000.0075.0524.750stress08:56:31082140.000.200.000.000.201pidstatAverage:UIDPID%usr %system%guest%wait%CPUCPUCommandAverage:076440.000.200.000.000.20-kworker/0:2-pmAverage:0777424.750.000.0075.0524.75-stressAverage:0777524.950.000.0074.8524.95-stressAverage:0777624.950.000.0075.2524.95-stressAverage:0777724.750.000.0075.0524.75-stressAverage:0777824.750.000.0075.2524.75-stressAverage:0777924.950.000.0075.2524.95-stressAverage:0778024.950.000.0075.0524.95-stressAverage:0778124.750.000.0075.0524.75-stressAverage:082140.000.200.000.000.20-pidstat
总结:
