day9.2
day9.2
进程的优先级
# 什么是进程的优先级?
优先级高的进程,可以优先享用系统的资源
# 优先级的定义和配置
在启动进程时,为不同的进程使用不同的调度策略。
nice值越高:表示优先级越低,例如19,该进程的容易将CPU使用量让给其他进程。
nice值越低:表示优先级越低,例如-20,该进程更不倾向让出cpu
指定优先级执行命令
# 指定优先级命令
nice -n 数值 命令或程序
# 重置已经在运行程序,优先级
1.查看sshd服务的优先级
[root@28technicians ~]# ps axo pid,user,nice,command|grep ssh
6924 root 0 /usr/sbin/sshd -D
# 设置sshd服务的优先级为-20
[root@28technicians ~]# renice -n -20 6924
6924 (process ID) old priority 0, new priority -20
# 再次查看
[root@28technicians ~]# ps axo pid,user,nice,command|grep ssh
6924 root -20 /usr/sbin/sshd -D
后台进程管理命令
# &
在执行的命令后面加 & 会直接将该命令放在后台运行
# 举例
[root@28technicians ~]# ping baidu.com &
# ctrl + z
jobs bg fg
先把进程放在后台暂停,配合bg将暂停的进程放在后台运行
# 举例
[root@28technicians ~]# ping baidu.com
PING baidu.com (220.181.38.251) 56(84) bytes of data.
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=128 time=30.6 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=2 ttl=128 time=30.4 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=3 ttl=128 time=30.7 ms
^Z
[1]+ Stopped ping baidu.com
[root@28technicians ~]# jobs
[1]+ Stopped ping baidu.com
[root@28technicians ~]# bg 1
# nohup
将执行的命令放在后台执行,并且将输出结果保存到 nohup.out文件中
# 举例
[root@28technicians ~]# nohup ping baidu.com &
[2] 7477
[root@28technicians ~]# nohup: ignoring input and appending output to ‘nohup.out’
# screen
将进程放入后台
# 举例
[root@28technicians ~]# yum install -y screen
# 选项
-ls:查看所有screen的后台进程
-r:指定后台进程号,进入该后台进程
-S:指定后台进程的名字
Ctrl + a + d:放在后台执行
平均负载
# 什么是平均负载?
平均负载是指,单位时间内,系统处于可运行状态(R)和不可中断状态(D)的平均进程数,也就是平均活跃进程数
# 平均负载多少时合理?
最理想的状态是每个CPU上都刚还运行着一个进程,这样每个CPU都得到了充分利用。
# 如何查看CPU个数
1./proc/CPUinfo
2.top 按 1
3.lscpu
架设我们在有2个CPU系统上看到平均负载为2.73,6.90,12.98那么说明在过去1分钟内,系统有136%的超载
1分钟 :(2.73/2*100%=136%)
5分钟:(6.90/2*100%=345%)
15分钟:(12.98/2*100%=649%)
但整体趋势来看,系统负载是在逐步降低。
CPU类型
cpu密集型:计算相关的
Io密集型:数据库相关服务
企业级负载分析实战
# stress
stress是linux系统压力测试工具,这里我们用作异常进程模拟平均负载升高场景
# 举例
CPU使用率导致负载升高
[root@28technicians ~]# stress --cpu 10 --timeout 600
磁盘io导致负载升高
[root@28technicians ~]# stress --io 10 --timeout 600
大量进程导致负载升高
[root@28technicians ~]# stress -c 10 --timeout 600
# mpstat是多核CPU性能分析工具,用来实时检查每个CPU的性能指标,以及所有CPU的平均指标
[root@28technicians ~]# mpstat -P ALL 2
Linux 3.10.0-957.el7.x86_64 (28technicians) 05/06/2022 _x86_64_ (1 CPU)
02:55:05 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
02:55:07 PM all 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
02:55:07 PM 0 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
# pidstat是一个常用的进程性能分析工具,用来实时查看进程的CPU,内存,IO,以及上下文切换等性能指标
[root@28technicians ~]# pidstat -u 3 2
Linux 3.10.0-957.el7.x86_64 (28technicians) 05/06/2022 _x86_64_ (1 CPU)
02:48:03 PM UID PID %usr %system %guest %CPU CPU Command
02:48:06 PM 0 5982 0.00 0.32 0.00 0.32 0 vmtoolsd
02:48:06 PM 0 7618 9.55 0.00 0.00 9.55 0 stress
02:48:06 PM 0 7619 9.87 0.00 0.00 9.87 0 stress
02:48:06 PM 0 7620 9.55 0.00 0.00 9.55 0 stress
02:48:06 PM 0 7621 9.87 0.00 0.00 9.87 0 stress
02:48:06 PM 0 7622 9.55 0.00 0.00 9.55 0 stress
02:48:06 PM 0 7623 9.24 0.00 0.00 9.24 0 stress
02:48:06 PM 0 7624 9.55 0.00 0.00 9.55 0 stress
02:48:06 PM 0 7625 9.87 0.00 0.00 9.87 0 stress
02:48:06 PM 0 7626 9.55 0.00 0.00 9.55 0 stress
02:48:06 PM 0 7627 9.24 0.00 0.00 9.24 0 stress
02:48:06 PM 0 7634 0.00 0.32 0.00 0.32 0 pidstat
总结分析
# 总结分析流程:
1.使用uptime或者top命令查看负载
2.看load average的负载趋势
3.是什么导致负载上升
mpstat -P ALL 2
是用户态,还是内核态,导致负载上升
用户态:CPU使用率,大量进程
内核态:磁盘io,压缩文件,网络存储挂载,下载文件,数据库查询语句
4.查看到底是哪个程序,引起用户态或者内核态的负载上升?
pidstat -u 2 2
5.查到了是某个进程后
-运维
执行了某条命令
启动了某个服务
-开发
查看开发写好的程序日志,导出日志,交给开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了