前言
记录一次线上主机的诊断,还有一些命令的使用和说明
平均负载 load average
定义:系统单位时间内的活跃进程数。
$ uptime
14:57:48 up 1228 days, 33 min, 1 user, load average: 0.78, 0.43, 0.31
一般来说,平均负载不能超过cpu核数的70%。
# cpu 核数的查询方法。
$ grep 'model name' /proc/cpuinfo | wc -l
8
$ grep 'model name' /proc/cpuinfo
model name : Intel(R) Xeon(R) CPU E5-26xx v3
model name : Intel(R) Xeon(R) CPU E5-26xx v3
model name : Intel(R) Xeon(R) CPU E5-26xx v3
model name : Intel(R) Xeon(R) CPU E5-26xx v3
model name : Intel(R) Xeon(R) CPU E5-26xx v3
model name : Intel(R) Xeon(R) CPU E5-26xx v3
model name : Intel(R) Xeon(R) CPU E5-26xx v3
model name : Intel(R) Xeon(R) CPU E5-26xx v3
现在看来,整个系统目前的负载是极轻的。
平均负载和 CPU 使用率
平均负载是指单位时间内,处于可运行状态和不可中断状态的进程数。所以,它不仅包括了正在使用 CPU 的进程,还包括等待 CPU 和等待 I/O 的进程。 -- 极客时间 《Linux性能分析实践》 而 CPU 使用率,是单位时间内 CPU 繁忙情况的统计,跟平均负载并不一定完全对应。比如:
- CPU 密集型进程,使用大量 CPU 会导致平均负载升高,此时这两者是一致的;
- I/O 密集型进程,等待 I/O 也会导致平均负载升高,但 CPU 使用率不一定很高;
- 大量等待 CPU 的进程调度也会导致平均负载升高,此时的 CPU 使用率也会比较高
mpstat pidstat
观察每个cpu的状况
$ mpstat -P ALL 5
Linux 2.6.32-573.el6.x86_64 (VM_144_131_centos) 2020年04月10日 _x86_64_ (8 CPU)
15时11分49秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
15时11分54秒 all 8.07 0.00 1.16 0.00 0.00 0.20 0.13 0.00 90.44
15时11分54秒 0 8.08 0.00 1.21 0.00 0.00 0.61 0.40 0.00 89.70
15时11分54秒 1 5.85 0.00 1.21 0.00 0.00 0.00 0.20 0.00 92.74
15时11分54秒 2 6.85 0.00 1.01 0.00 0.00 0.00 0.00 0.00 92.14
15时11分54秒 3 7.51 0.00 1.01 0.00 0.00 0.41 0.20 0.00 90.87
15时11分54秒 4 8.03 0.00 1.00 0.00 0.00 0.20 0.20 0.00 90.56
15时11分54秒 5 10.46 0.00 1.41 0.00 0.00 0.20 0.00 0.00 87.93
15时11分54秒 6 9.11 0.00 1.21 0.00 0.00 0.20 0.20 0.00 89.27
15时11分54秒 7 8.48 0.00 1.21 0.00 0.00 0.00 0.00 0.00 90.30
观察程序的健康状况
$ pidstat -u 5 1
Linux 2.6.32-573.el6.x86_64 (VM_144_131_centos) 2020年04月10日 _x86_64_ (8 CPU)
15时13分53秒 PID %usr %system %guest %CPU CPU Command
15时13分58秒 4941 0.00 0.20 0.00 0.20 5 YDService
15时13分58秒 6650 0.20 0.00 0.00 0.20 1 barad_agent
15时13分58秒 6654 0.40 0.00 0.00 0.40 5 barad_agent
15时13分58秒 15563 14.34 1.99 0.00 16.33 7 jsvc
15时13分58秒 15588 3.19 0.80 0.00 3.98 2 jsvc
15时13分58秒 15607 24.10 2.39 0.00 26.49 5 jsvc
15时13分58秒 16152 6.57 1.39 0.00 7.97 1 jsvc
15时13分58秒 16178 7.77 1.39 0.00 9.16 3 jsvc
15时13分58秒 16573 4.18 1.39 0.00 5.58 3 jsvc
15时13分58秒 22976 8.96 1.39 0.00 10.36 0 jsvc
15时13分58秒 25799 0.20 0.20 0.00 0.40 6 pidstat
15时13分58秒 30405 0.00 0.20 0.00 0.20 3 salt-minion
Top
top - 14:55:40 up 1228 days, 30 min, 1 user, load average: 0.40, 0.34, 0.27
Tasks: 222 total, 1 running, 221 sleeping, 0 stopped, 0 zombie
Cpu(s): 23.9%us, 2.3%sy, 0.0%ni, 72.9%id, 0.0%wa, 0.0%hi, 0.6%si, 0.3%st
Mem: 32878400k total, 27613612k used, 5264788k free, 331224k buffers
Swap: 0k total, 0k used, 0k free, 9486760k cached
上下文切换
CPU 上下文切换,就是先把前一个任务的 CPU 上下文(也就是 CPU 寄存器和程序计数器)保存起来,然后加载新任务的上下文到这些寄存器和程序计数器,最后再跳转到程序计数器所指的新位置,运行新任务。
- vmstat 是一个常用的系统性能分析工具,主要用来分析系统的内存使用情况,也常用来分析 CPU 上下文切换和中断的次数。
$ vmstat 5
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 5203364 331336 9499568 0 0 0 11 0 0 24 3 73 0 0
1 0 0 5202652 331336 9499580 0 0 0 64 10220 16085 8 1 90 0 0
1 0 0 5202404 331336 9499596 0 0 0 65 10274 15876 9 1 89 0 0
3 0 0 5201708 331336 9499628 0 0 0 75 10584 16416 9 1 89 0 0
- cs 每秒上下文切换次数
- in 每秒中断次数
- r 就绪队列长度,正在运行和等待CPU的进程数
- b 处于不可中断睡眠状态的进程数
pidstat 加上 -w,查看每个进程上下文切换的情况。
$ pidstat -w 5
Linux 2.6.32-573.el6.x86_64 (VM_144_131_centos) 2020年04月10日 _x86_64_ (8 CPU)
15时25分37秒 PID cswch/s nvcswch/s Command
15时25分42秒 3 1.80 0.00 migration/0
15时25分42秒 4 11.18 0.00 ksoftirqd/0
15时25分42秒 7 1.00 0.00 migration/1
15时25分42秒 9 6.39 0.00 ksoftirqd/1
15时25分42秒 11 0.80 0.00 migration/2
15时25分42秒 13 6.99 0.00 ksoftirqd/2
15时25分42秒 15 1.40 0.00 migration/3
15时25分42秒 17 6.39 0.00 ksoftirqd/3
15时25分42秒 19 1.40 0.00 migration/4
15时25分42秒 21 4.99 0.00 ksoftirqd/4
15时25分42秒 23 1.40 0.00 migration/5
15时25分42秒 25 8.98 0.00 ksoftirqd/5
15时25分42秒 27 1.00 0.00 migration/6
15时25分42秒 29 7.78 0.00 ksoftirqd/6
15时25分42秒 31 0.60 0.00 migration/7
15时25分42秒 33 7.58 0.00 ksoftirqd/7
15时25分42秒 35 1.00 0.00 events/0
15时25分42秒 36 1.00 0.00 events/1
15时25分42秒 37 1.20 0.00 events/2
15时25分42秒 38 1.00 0.00 events/3
15时25分42秒 39 1.00 0.00 events/4
15时25分42秒 40 1.00 0.00 events/5
15时25分42秒 41 1.00 0.00 events/6
15时25分42秒 42 1.00 0.00 events/7
15时25分42秒 72 0.20 0.00 sync_supers
15时25分42秒 73 0.20 0.00 bdi-default
15时25分42秒 82 2.20 0.00 kblockd/0
15时25分42秒 126 0.20 0.00 khugepaged
15时25分42秒 485 0.80 0.00 kjournald
15时25分42秒 701 0.20 0.00 flush-252:0
15时25分42秒 4238 0.20 0.60 pidstat
15时25分42秒 4941 1.20 0.00 YDService
15时25分42秒 4980 1.40 0.00 YDLive
15时25分42秒 6649 0.20 0.00 barad_agent
15时25分42秒 6650 1.80 0.00 barad_agent
15时25分42秒 16178 0.20 0.00 jsvc
15时25分42秒 19273 0.20 0.00 flush-252:16
15时25分42秒 19294 0.80 0.00 kjournald
15时25分42秒 26354 1.00 0.00 zabbix_agentd
15时25分42秒 26355 1.40 0.00 zabbix_agentd
15时25分42秒 26356 0.80 0.00 zabbix_agentd
15时25分42秒 26357 3.59 0.00 zabbix_agentd
15时25分42秒 26358 1.00 0.00 zabbix_agentd
15时25分42秒 30405 1.00 0.00 salt-minion
- cswch 表示每秒自愿上下文切换(voluntary context switches)的次数
- nvcswch 表示每秒非自愿上下文切换(non voluntary context switches)的次数。