一.进程概述

1.什么是进程

进程就是一个正在运行的程序

2.程序与进程的区别

进程是动态概念,具有生命周期,无法长期存放系统中程序是静态概念,代码的集合,可以长期存放在系统中

3.进程的生命周期

接收任务-fork子进程-子进程处理任务-夫进程等待

正常结束任务

非正常结束任务

  • 产生僵尸进程
  • 产生孤儿进程

进程的生命周期

进程管理_nginx

1.用户发起请求
2.父进程会fork出子进程,子进程会继承父进程大部分的属性,如:文件描述符等,处理任务
3.子进程在处理任务的过程中,父进程是一个wait状态,等待子进程
4.正常结束
5.非正常结束
-僵尸进程
-孤儿进程

4.什么是僵尸进程

子进程比父进程先结束,父进程没有回收子进程的资源,此时的子进程就称为“僵尸进程”

产生原因:子进程的结束和父进程的运行是异步的,父进程永远不知道子进程什么时候结束,子进程在结束时,父进程繁忙来不及wait子进程。


5.什么是孤儿进程

父进程比子进程先结束,子进程还在执行任务,没有父进程管理,此时的子进程就称为孤儿进程。

产生原因:子进程的结束和父进程的运行是异步的,父进程永远不知道子进程什么时候结束,当父进程正常完成工作或其他原因被终止,则会导致,子进程变成孤儿进程。


二.监控进程的状态

1.进程状态管理命令-ps

ps  [选项]  [参数]

## 常用选项组合
[root@Quanyi <sub>]# ps -ef
[root@Quanyi </sub>]# ps aux
a:查看所有与终端相关的进程,由终端发起的进程
u:显示进程的管理用户
x:查看所有与终端无关的进程

##举例:
[root@Quanyi <sub>]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 2 0 0 Apr28 ? 00:00:00 [kthreadd]
root 3 2 0 Apr28 ? 00:00:01 [ksoftirqd/0]
root 5 2 0 Apr28 ? 00:00:00 [kworker/0:0H]
root 7 2 0 Apr28 ? 00:00:00 [migration/0]
root 8 2 0 Apr28 ? 00:00:00 [rcu_bh]
root 9 2 0 Apr28 ? 00:00:01 [rcu_sched]
root 10 2 0 Apr28 ? 00:00:00 [lru-add-drain]
root 11 2 0 Apr28 ? 00:00:00 [watchdog/0]
root 13 2 0 Apr28 ? 00:00:00 [kdevtmpfs]

[root@Quanyi </sub>]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S Apr28 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Apr28 0:01 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Apr28 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S Apr28 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S Apr28 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? R Apr28 0:01 [rcu_sched]
root 10 0.0 0.0 0 0 ? S< Apr28 0:00 [lru-add-drain]
root 11 0.0 0.0 0 0 ? S Apr28 0:00 [watchdog/0]
root 13 0.0 0.0 0 0 ? S Apr28 0:00 [kdevtmpfs]
root 14 0.0 0.0 0 0 ? S< Apr28 0:00 [netns]

##举例:
[root@Quanyi <sub>]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S Apr28 0:00 [kthreadd]

USER:该进程的管理用户
PID:进程号
%CPU:该进程占用CPU的百分比
%MEM:该进程占用内存的百分比
VSZ:该进程占用虚拟内存的大小
RSS:该进程占用物理内存的大小
TYY:
?:由内核发起的进程
tty:机器上的终端进程
pts/N:远程连接工具的终端进程

STAT:进程运行的状态
D:无法中断的休眠状态(通IO的进程)#常用
R:正在运行的状态 #常用
S:处于休眠状态的进程 #常用
T:暂停或者被追踪的状态
W:进入交换内存的进程(swap,centos7见不到)
X:死掉的进程(少见)
Z:僵尸进程 #常用
<:优先级高的进程
N:优先级低的进程
L:有些数据页被锁进内存的进程
s:父进程,在它下面有子进程
l:以线程的方式运行
|:存在多进程的进程
+:在前台运行 #常用
START:进程开启的时间
TIME:该进程占用CPU的时间
COMMAND:进程的名字或者进程的命令
[]:内核态进程
没[]:用户态进程

## 如果后台有被暂停的进程
## 查看所有被暂停的进程
[root@Quanyi </sub>]# jobs
[1]- Stopped vim 1.txt
[2]+ Stopped ping baidu.com

bg:让暂停的进程运行起来,后面加数字,就可以运行第几个被暂停的进程(默认是最后一个)
fg:是把后台暂停的进程,调到前台运行,后面加数字,可以将第N个进程调到前台运行(默认是最后一个)

## ps命令的用法
# 1.根据%CPU进行排序
[root@Quanyi <sub>]# ps aux|sort -k 3
[root@Quanyi </sub>]# ps aux --sort %cpu
[root@Quanyi <sub>]# ps aux --sort %mem

##举例:
[root@Quanyi </sub>]# ps aux --sort %mem
root 8150 0.0 0.0 0 0 ? S 14:11 0:00 [kworker/0:0]
root 6191 0.0 0.0 55528 852 ? S<sl Apr28 0:00 /sbin/auditd
root 6376 0.0 0.1 126392 1672 ? Ss Apr28 0:00 /usr/sbin/crond -n

# 2.自定义显示字段
o:自定义显示字段
##举例:
[root@Quanyi <sub>]# ps axo pid,%cpu,command,user
PID %CPU COMMAND USER
1 0.0 /usr/lib/systemd/systemd -- root
2 0.0 [kthreadd] root
3 0.0 [ksoftirqd/0] root
5 0.0 [kworker/0:0H] root
7 0.0 [migration/0] root
8 0.0 [rcu_bh] root
9 0.0 [rcu_sched] root
10 0.0 [lru-add-drain] root
11 0.0 [watchdog/0] root
13 0.0 [kdevtmpfs] root

# 3.显示子进程
f:查看子进程和父进程的关系
##举例:
[root@Quanyi </sub>]# ps auxf
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S Apr28 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Apr28 0:01 \_ [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Apr28 0:00 \_ [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S Apr28 0:00 \_ [migration/0]
root 8 0.0 0.0 0 0 ? S Apr28 0:00 \_ [rcu_bh]
root 9 0.0 0.0 0 0 ? R Apr28

# 4.查看指定进程的pid
##举例:
[root@localhost <sub>]# ps aux|grep [n]ginx
root 18256 0.0 0.1 46036 1984 ? Ss Apr28 0:00 nginx: master process nginx
nobody 18314 0.0 0.2 46468 2244 ? S Apr28 0:00 nginx: worker process
root 28699 0.0 0.0 112812 980 pts/0 S+ 15:11 0:00 grep --color=auto nginx

[root@localhost </sub>]# pgrep nginx
18256
18314

-l:显示该进程的启动命令
-a:显示该进程的完整描述信息

[root@localhost ~]# pidof nginx
18314 18256

2.进程管理命令-top

[root@Quanyi ~]# top
top - 19:22:35 up 5 days, 7:10, 3 users, load average: 0.01, 0.04, 0.05
Tasks: 95 total, 1 running, 94 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 995896 total, 687784 free, 139012 used, 169100 buff/cache
KiB Swap: 1048572 total, 1048572 free, 0 used. 678088 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 127908 6480 4136 S 0.0 0.7 0:05.87 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:03.64 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:03.86 rcu_sched

## 第一行:系统相关
top - 19:22:35 up 5 days, 7:10, 3 users, load average: 0.01, 0.04, 0.05

top:命令
19:22:35:当前系统时间
up 5 days, 7:10:该服务器,运行的时间
3 user:当前登录的用户数量
load average: 系统的平均负载
0.01:1分钟
0.04:5分钟
0.05:15分钟

## 第二行:进程状态
Tasks: 95 total, 1 running, 94 sleeping, 0 stopped, 0 zombie
95 total:当前系统中所有的进程数量
1 running:处于R状态,正在运行状态的进程数
94 sleeping:处于S状态,sleep状态的进程数
0 stopped:处于T状态,后台挂起暂停状态的进程数
0 zombie:处于Z状态,僵尸进程的进程数量

## 第三行:CPU百分比
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
us:用户进程占用cpu的百分比(用户态)
sy:系统进程占用cpu的百分比(内核态)
id:cpu的空闲程度
ni:优先级较高的进程占用cpu的百分比
wa:等待状态的进程占用cpu的百分S
hi:硬中断占用cpu的百分比
si:软中断占用cpu的百分比
st:虚拟化技术占用cpu的百分比

## 第四行:物理内存
KiB Mem : 995896 total, 687784 free, 139012 used, 169100 buff/cache
995896 total:总内存数
687784 free:空闲内存数
139012 used:已使用的内存数
169100 buff/cache:buffer缓冲区/缓存区

## 第五行:Swap虚拟内存
KiB Swap: 1048572 total, 1048572 free, 0 used. 678088 avail Mem
1048572 total:总共的虚拟内存数
1048572 free:空闲的虚拟内存数
0 used:已使用的虚拟内存数
678088 avail Mem:可用的虚拟内存数

3.什么是中断

中断是系统用来影响硬件设备请求的一种机制,它会打断进程的正常调度和执行,然后调用内核中的中断处理程序来影响设备的请求。

4.top命令用法

# top
-d:指定更新的时间(默认是3s更新一次)
-p:只查看指定pid的进程
-u:指定相关用户
-b:将top内容保存到文件中
-n:指定次数

# top 常见指令
h 查看帮出
z 高亮显示
1 显示所有CPU的负载
s 设置刷新时间
b 高亮现实处于R状态的进程
M 按内存使用百分比排序输出
P 按CPU使用百分比排序输出
R 对排序进行反转
f 自定义显示字段
k kill掉指定PID进程
W 保存top环境设置 ~/.toprc
q 退出

PID:进程ID号
USER:该进程的用户
PR NI:进程优先级
VIRT:虚拟内存占用空间
RES:物理内存占用空间
SHR:共享内存占用空间
S:进程的状态
%CPU:占用CPU的百分比
%MEM:内存占用的百分比
TIME+:运行时间
COMMAND:进程的运行命令

三.进程的信号管理

[root@Quanyi ~]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX

# //常见信号列表:
# 数字信号 信号别名 作用
1 HUP 挂起信号,往往可以让进程重新配置(重新加载配置文件)
2 INT 中断信号,起到结束进程的作用,和ctrl + c 的作用一样
3 QUIT 让进程退出,结果是进程退出
9 KILL 直接结束进程,不能被进程捕获
15 TERM 进程终止,这是默认信号
18 CONT 被暂停的进程将继续恢复运行(放在后台运行,bg)
19 STOP 暂停进程
20 TSTP 用户停止请求,作用类似于ctrl + z 把进程放到后台并暂停

killall:杀掉所有进程,不用指定pid
pkill:杀掉所有进程,不用指定pid

[root@Quanyi ~]# pkill -t pts/0
[root@Quanyi ~]# w
19:38:24 up 5 days, 7:26, 3 users, load average: 0.00, 0.01, 0.05

四.进程的优先级

1.什么优先级

优先级高的进程,可以优先享用系统的资源

2.优先级的定义和配置

在启动进程时,为不同的进程使用不同的调度策略。
nice值越高:表示优先级越低,例如19,该进程容易将CPU使用量让给其他进程。
nice值越低:表示优先级越高,例如-20,该进程更不倾向于让出CPU。

## 举例:
# 指定优先级执行命令
[root@Quanyi <sub>]# nice -n 19 vim 123

# 重置已经在运行的程序,优先级
# 查看sshd服务的优先级
[root@Quanyi </sub>]# ps axo pid,user,nice,command|grep ssh
6881 root 0 /usr/sbin/sshd -D
11474 root 0 sshd: root@pts/0
11630 root 0 grep --color=auto ssh

# 重新设置sshd服务的优先级为-20
[root@Quanyi <sub>]# renice -n -20 6881
6881 (process ID) old priority 0, new priority -20

# 再次查看
[root@Quanyi </sub>]# ps axo pid,user,nice,command|grep ssh
6881 root -20 /usr/sbin/sshd -D
11474 root 0 sshd: root@pts/0
11639 root 0 grep --color=auto ssh

1.后台进程管理

# 在执行的命令后面加 & 会直接将该命令放在后台执行
&
## 举例:
[root@Quanyi <sub>]# ping jd.com &
[4] 11645
[root@Quanyi </sub>]# PING jd.com (211.144.27.126) 56(84) bytes of data.
64 bytes from 211.144.27.126 (211.144.27.126): icmp_seq=1 ttl=128 time=30.0 ms
64 bytes from 211.144.27.126 (211.144.27.126): icmp_seq=2 ttl=128 time=29.2 ms
64 bytes from 211.144.27.126 (211.144.27.126): icmp_seq=3 ttl=128 time=29.2 ms
64 bytes from 211.144.27.126 (211.144.27.126): icmp_seq=4 ttl=128 time=30.3 ms
64 bytes from 211.144.27.126 (211.144.27.126): icmp_seq=5 ttl=128 time=30.7 ms

# 先把进程放在后台暂停 配合 bg将暂停的进程,在后台恢复运行
Ctrl + z
jobs bg fg

[root@Quanyi <sub>]# ping jd.com
PING jd.com (118.193.98.63) 56(84) bytes of data.
64 bytes from 118.193.98.63 (118.193.98.63): icmp_seq=1 ttl=128 time=32.8 ms
64 bytes from 118.193.98.63 (118.193.98.63): icmp_seq=2 ttl=128 time=32.4 ms
64 bytes from 118.193.98.63 (118.193.98.63): icmp_seq=3 ttl=128 time=32.8 ms
64 bytes from 118.193.98.63 (118.193.98.63): icmp_seq=4 ttl=128 time=32.8 ms
64 bytes from 118.193.98.63 (118.193.98.63): icmp_seq=5 ttl=128 time=33.0 ms
64 bytes from 118.193.98.63 (118.193.98.63): icmp_seq=6 ttl=128 time=32.5 ms
64 bytes from 118.193.98.63 (118.193.98.63): icmp_seq=7 ttl=128 time=32.7 ms
^Z
[4]+ Stopped ping jd.com

[root@Quanyi </sub>]# jobs
[1]+ Stopped ping jd.com
[root@Quanyi <sub>]# bg 1
[1]+ ping jd.com &

# 将执行的命令放入后台执行,并且将输出结果保存到 nohup.out文件中
nohup

[root@Quanyi </sub>]# nohup ping jd.com &
[1] 11702
[root@Quanyi ~]# nohup: ignoring input and appending output to ‘nohup.out’

# 将进程放入后台(开启一个子shell)
# screen
# 如果没有screen包,用yum安装包装上 yum install -y screen
-ls:查看所有screen的后台进程
-r:指定后台进程号,进入该后台进程
-S:指定后台进程的名字
Ctrl + a + d:放在后台执行

2.平均负载

1.什么是平均负载重点:

死记硬背也要记住:平均负载是指,单位时间内,系统处于可运行状态(R)和不可中断状态(D)的平均进程数,也就是平均活跃进程数

注意:平均负载与CPU使用率并没有直接关系

2.可运行状态和不可中断状态是什么

(1)可运行状态进程:是指正在使用CPU或者正在等待CPU的进程,也就是我们用PS命令看的处于R状态的进程

(2)不可中断状态是什么:系统中最常见的是等待硬件设备的IO相应,也就是我们PS命令中看到的D状态(也成为Disk Sleep)的进程。

注意:我们可以理解为,平均负载其实就是单位时间内的活跃进程数


## 如何查看cpu的个数
#举例:
[root@Quanyi <sub>]# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 61
model name : Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
stepping : 4
microcode : 0x2b
cpu MHz : 2194.920
cache size : 3072 KB
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 20
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt arat spec_ctrl intel_stibp flush_l1d arch_capabilities
bogomips : 4389.84
clflush size : 64
cache_alignment : 64
address sizes : 43 bits physical, 48 bits virtual
power management:

# top 按 1
[root@Quanyi </sub>]# top
top - 14:04:42 up 5 days, 18:04, 3 users, load average: 0.00, 0.01, 0.05
Tasks: 95 total, 1 running, 94 sleeping, 0 stopped, 0 zombie
%Cpu0 : 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 995896 total, 538904 free, 139912 used, 317080 buff/cache
KiB Swap: 1048572 total, 1048572 free, 0 used. 664296 avail Mem

[root@Quanyi ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 1
On-line CPU(s) list: 0
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 61
Model name: Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
Stepping: 4
CPU MHz: 2194.920
BogoMIPS: 4389.84
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 3072K
NUMA node0 CPU(s): 0
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt arat spec_ctrl intel_stibp flush_l1d arch_capabilities

# 假设我们在有2个CPU系统上看到平均负载为2.73,6.90,12.98那么说明在过去1分钟内,系统有136%的超载
(2.73/2*100%=136%)
5分钟:(6.90/2*100%=345%)
15分钟:(12.98/2*100%=649%)
但整体趋势来看,系统负载是在逐步降低。

3.企业中平均负载多高需要重点关注:

当平均负载高于CPU数量70%的时候,你就应该分析排查负载高的问题了,一旦负载过高,就可能导致进程相应变慢,进而影响服务的正常功能。
但70%这个数字并不是绝对的,最推荐的方法,还是把系统的平均负载监控起来,然后根据更多的历史数据,判断负载的变化趋势,当发现负载有明显升高的趋势时,比如说负载翻倍了,你再去做分析和调查。

4.平均负载与CPU的使用率有什么关系

我们经常容易把平均负载和CPU使用率混淆,所以在这里,我也做一个区分,可能你会感觉到疑惑,既然平均负载代表的是活跃进程数,那平均负载搞了,不就意味着CPU使用率高嘛?
我们还是要回到平均负载的含义上来,平均负载指的是每单位时间内,处于可运行状态和不可中断状态的进程数,所以,它不仅包括了正在使用CPU的进程数,还包括等待CPU和等待IO的进程数。
而CPU的使用率是单位时间内,CPU繁忙情况的统计,跟平均浮现在并不一定完全对应。
比如:
CPU密集型进程,使用大量的CPU会导致平均负载升高,此时这两者是一致的。
IO密集型进程,等待IO也会导致平均负载升高,但CPU使用率不一定很高。
大量等待CPU的进程调度也会导致平均负载升高,此时的CPU使用率也会比较高。

## cpu类型:
cpu密集型:计算相关
IO密集型:数据库相关服务

3.企业级负载分析实战

## 举例:
比如领导说:某一台服务器,很卡,怎么办?
卡的原因:服务器,负载太高
如何定位,什么程序,导致负载高?
如何定位,负载高,是哪个硬件引起的?

## 完全是因为目前没有用户量
stress是Linux系统压力测试工具,这里我们用作异常进程模拟平均负载升高的场景。
[root@Quanyi <sub>]# yum install -y stress

## 分析负载
mpstat是多核CPU性能分析工具,用来实时检查每个CPU的性能指标,以及所有CPU的平均指标。
pidstat是一个常用的进程性能分析工具,用来实时查看进程的CPU,内存,IO,以及上下文切换等性能指标。

## 启动了5个cpu密集型的进程,5个占用CPU的进程
[root@Quanyi </sub>]# stress --cpu 5 --timeout 300
stress: info: [11764] dispatching hogs: 5 cpu, 0 io, 0 vm, 0 hdd

[root@Quanyi <sub>]# top
top - 14:46:38 up 5 days, 18:46, 3 users, load average: 4.52, 1.45, 0.55
Tasks: 101 total, 8 running, 93 sleeping, 0 stopped, 0 zombie
%Cpu(s): 99.2 us, 0.4 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.4 si, 0.0 st
KiB Mem : 995896 total, 537900 free, 140672 used, 317324 buff/cache
KiB Swap: 1048572 total, 1048572 free, 0 used. 663540 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11768 root 0 -20 7312 100 0 R 20.3 0.0 0:16.98 stress
11765 root 0 -20 7312 100 0 R 19.9 0.0 0:16.96 stress
11766 root 0 -20 7312 100 0 R 19.9 0.0 0:16.96 stress
11769 root 0 -20 7312 100 0 R 19.9 0.0 0:16.97 stress
11767 root 0 -20 7312 100 0 R 19.6 0.0 0:16.97 stress
11770 root 0 -20 161988 2188 1552 R 0.3 0.2 0:00.12 top
1 root 20 0 127908 6480 4136 S 0.0 0.7 0:06.50 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.06 kthreadd

## 如果没有sysstat包 则需要安装yum install sysstat
## 查看所有的CPU,5s显示一次数据
[root@Quanyi </sub>]# mpstat -P ALL 5
Linux 3.10.0-957.el7.x86_64 (Quanyi) 05/06/2022 _x86_64_ (1 CPU)

02:49:43 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
02:49:48 PM all 0.00 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 99.80
02:49:48 PM 0 0.00 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 99.80

02:49:48 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
02:49:53 PM all 0.20 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 99.60
02:49:53 PM 0 0.20 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 99.60

02:49:53 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
02:49:58 PM all 0.00 0.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 99.60
02:49:58 PM 0 0.00 0.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 99.60

## -u指定5s输出一次数据 3,总共输出3组数据
[root@Quanyi <sub>]# pidstat -u 5 3
Linux 3.10.0-957.el7.x86_64 (Quanyi) 05/06/2022 _x86_64_ (1 CPU)

02:57:24 PM UID PID %usr %system %guest %CPU CPU Command
02:57:29 PM 0 6340 0.20 0.00 0.00 0.20 0 vmtoolsd
02:57:29 PM 0 11818 0.00 0.20 0.00 0.20 0 pidstat

02:57:29 PM UID PID %usr %system %guest %CPU CPU Command
02:57:34 PM 0 6340 0.20 0.40 0.00 0.60 0 vmtoolsd
02:57:34 PM 0 11761 0.00 0.20 0.00 0.20 0 kworker/0:3
02:57:34 PM 0 11818 0.20 0.20 0.00 0.40 0 pidstat

02:57:34 PM UID PID %usr %system %guest %CPU CPU Command
02:57:39 PM 0 11818 0.00 0.20 0.00 0.20 0 pidstat

Average: UID PID %usr %system %guest %CPU CPU Command
Average: 0 6340 0.13 0.13 0.00 0.27 - vmtoolsd
Average: 0 11761 0.00 0.07 0.00 0.07 - kworker/0:3
Average: 0 11818 0.07 0.20 0.00 0.27 - pidstat

# IO:磁盘IO导致负载升高
[root@localhost </sub>]# stress --io 10 --timeout 300
# CPU:CPU会用率会导致负载升高
[root@localhost <sub>]# stress --cpu 10 --timeout 300
# 启动大量进程:导致负载升高
[root@localhost </sub>]# stress -c 10 --timeout 300

# IO:磁盘IO导致负载升高
[root@Quanyi <sub>]# stress --io 10 --timeout 300
[root@Quanyi </sub>]# top
top - 15:02:25 up 5 days, 19:01, 3 users, load average: 7.64, 2.24, 1.06
Tasks: 107 total, 11 running, 96 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.7 us, 97.9 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
KiB Mem : 995896 total, 516228 free, 141928 used, 337740 buff/cache
KiB Swap: 1048572 total, 1048572 free, 0 used. 661360 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11850 root 0 -20 7312 100 0 R 17.1 0.0 0:04.49 stress
11842 root 0 -20 7312 100 0 R 13.8 0.0 0:06.49 stress
11843 root 0 -20 7312 100 0 R 10.9 0.0 0:04.50 stress
11849 root 0 -20 7312 100 0 R 8.9 0.0 0:05.01 stress
11845 root 0 -20 7312 100 0 D 8.6 0.0 0:04.36 stress
11847 root 0 -20 7312 100 0 R 8.6 0.0 0:04.97 stress
11851 root 20 0 0 0 0 R 6.9 0.0 0:01.88 kworker/u256:1
11844 root 0 -20 7312 100 0 R 6.6 0.0 0:04.67 stress
11848 root 0 -20 7312 100 0 R 6.2 0.0 0:04.13 stress
11841 root 0 -20 7312 100 0 R 5.6 0.0 0:04.72 stress
9948 root 20 0 0 0 0 S 3.9 0.0 0:05.27 kworker/u256:0
11846 root 0 -20 7312 100 0 R 3.6 0.0 0:06.21 stress
6340 root 20 0 300940 6340 4988 S 0.3 0.6 9:12.96 vmtoolsd
11852 root 0 -20 162092 2204 1552 R 0.3 0.2 0:00.14 top
1 root 20 0 128068 6644 4168 S 0.0 0.7 0:06.74 systemd

# CPU:CPU会用率会导致负载升高
[root@Quanyi <sub>]# stress --cpu 10 --timeout 300
stress: info: [11887] dispatching hogs: 10 cpu, 0 io, 0 vm, 0 hdd
23 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 edac-poller
[root@Quanyi </sub>]# top
top - 15:10:58 up 5 days, 19:10, 3 users, load average: 5.60, 4.63, 3.26
Tasks: 108 total, 11 running, 97 sleeping, 0 stopped, 0 zombie
%Cpu(s): 99.3 us, 0.4 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.4 si, 0.0 st
KiB Mem : 995896 total, 516188 free, 142008 used, 337700 buff/cache
KiB Swap: 1048572 total, 1048572 free, 0 used. 661328 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11889 root 0 -20 7312 100 0 R 10.3 0.0 0:03.78 stress
11890 root 0 -20 7312 100 0 R 10.3 0.0 0:03.79 stress
11896 root 0 -20 7312 100 0 R 10.3 0.0 0:03.79 stress
11888 root 0 -20 7312 100 0 R 10.0 0.0 0:03.78 stress
11891 root 0 -20 7312 100 0 R 10.0 0.0 0:03.78 stress
11892 root 0 -20 7312 100 0 R 10.0 0.0 0:03.78 stress
11897 root 0 -20 7312 100 0 R 10.0 0.0 0:03.79 stress
11893 root 0 -20 7312 100 0 R 9.7 0.0 0:03.77 stress
11894 root 0 -20 7312 100 0 R 9.7 0.0 0:03.78 stress
11895 root 0 -20 7312 100 0 R 9.7 0.0 0:03.77 stress
6340 root 20 0 300940 6340 4988 S 0.3 0.6 9:13.84 vmtoolsd
1 root 20 0 128068 6644 4168 S 0.0 0.7 0:06.83 systemd

# 启动大量进程:导致负载升高
[root@Quanyi <sub>]# stress -c 10 --timeout 300
stress: info: [11902] dispatching hogs: 10 cpu, 0 io, 0 vm, 0 hdd

[root@Quanyi </sub>]# top
top - 15:15:30 up 5 days, 19:14, 3 users, load average: 6.81, 4.74, 3.63
Tasks: 106 total, 11 running, 95 sleeping, 0 stopped, 0 zombie
%Cpu(s): 99.6 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.4 si, 0.0 st
KiB Mem : 995896 total, 516204 free, 141992 used, 337700 buff/cache
KiB Swap: 1048572 total, 1048572 free, 0 used. 661344 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11906 root 0 -20 7312 100 0 R 10.3 0.0 0:05.74 stress
11907 root 0 -20 7312 100 0 R 10.3 0.0 0:05.74 stress
11909 root 0 -20 7312 100 0 R 10.3 0.0 0:05.74 stress
11903 root 0 -20 7312 100 0 R 10.0 0.0 0:05.73 stress
11904 root 0 -20 7312 100 0 R 10.0 0.0 0:05.73 stress
11905 root 0 -20 7312 100 0 R 10.0 0.0 0:05.73 stress
11908 root 0 -20 7312 100 0 R 10.0 0.0 0:05.73 stress
11910 root 0 -20 7312 100 0 R 10.0 0.0 0:05.73 stress
11911 root 0 -20 7312 100 0 R 10.0 0.0 0:05.73 stress
11912 root 0 -20 7312 100 0 R 9.6 0.0 0:05.73 stress
6340 root 20 0 300940 6340 4988 S 0.3 0.6 9:14.24 vmtoolsd
1 root 20 0 128068 6644 4168 S 0.0 0.7 0:06.87 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.06 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:04.37 ksoftirqd/0


## 总结分析流程:
1.使用uptime或者top命令查看,系统负载
2.看1分钟,5分钟,15分钟的负载趋势
3.是什么情况导致负载上升
mpstat -P ALL 5   是用户态,还是内核态,导致负载上升
用户态:cpu使用率,大量进程
内核态:磁盘IO,压缩文件,网络存储挂载,下载文件,数据库查询语句
4.查看到底是哪个程序,引起用户态或者内核态的负载上升?
pidstat -u 5 2
5.查到了是某个进程后
- 运维
执行了某条命令?
启动了某个服务?
 
- 开发
查看开发写好的程序日志,导出日志,交给开发