什麼是優先級
優先級高的程序,可以優先享用系統的資源
優先級的定義和配置
在啟動程序時,為不同的程序使用不同的排程政策。
nice值越高:表示優先級越低,例如19,該程序容易将CPU使用量讓給其他程序
nice值越低:表示優先級越高,例如-20,該程序更不傾向于讓出CPU
## 指定優先級執行指令
127 ✗ 07:09:07 root@miaosen,10.0.0.100:<sub> # nice -n 5 vim 2.txt
USER PID NI COMMAND
root 8576 5 vim 2.txt
## 重置,已經在運作的程式,優先級
0 ✓ 07:11:25 root@miaosen,10.0.0.100:</sub> # ps axo user,pid,nice,command |grep ssh
root 6879 0 /usr/sbin/sshd -D
root 7174 0 sshd: root@pts/0
root 8443 0 sshd: root@pts/1
root 8648 0 grep --color=auto ssh
# 重新設定sshd服務的優先級為-20 (修改已啟動的優先級renice)
0 ✓ 07:11:48 root@miaosen,10.0.0.100:<sub> # renice -n -20 6879
6879 (process ID) old priority 0, new priority -20
# 再次檢視
0 ✓ 07:13:01 root@miaosen,10.0.0.100:</sub> # ps axo user,pid,nice,command |grep ssh
root 6879 -20 /usr/sbin/sshd -D
背景程序管理
# 在執行的指令後面加 & 會直接将該指令放在背景執行
&
0 ✓ 07:22:16 root@miaosen,10.0.0.100:<sub> # ping baidu.com &
[1] 8724
# 關閉就在另一台終端上 kill 8724(pid)
# 先把程序放在背景暫停配合 bg将暫停的程序,在背景恢複運作
Ctrl + z
jobs bg fg
# 栗子
0 ✓ 07:39:06 root@miaosen,10.0.0.100:</sub> # ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=44.2 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=34.9 ms
^Z
[1]+ Stopped ping baidu.com
# 檢視有哪些被暫停了
148 ✗ 07:41:57 root@miaosen,10.0.0.100:<sub> # jobs
[1]+ Stopped ping baidu.com
0 ✓ 07:42:44 root@miaosen,10.0.0.100:</sub> # bg 1
[1]+ ping baidu.com &
# 将執行的指令放入背景執行,并且将輸出結果儲存到 nohup.out檔案中
nohup
0 ✓ 07:49:40 root@miaosen,10.0.0.100:<sub> # nohup ping baidu.com &
[1] 8922
# 不會覆寫,忽略輸入并追加
nohup: ignoring input and appending output to ‘nohup.out’
# 可以使用 tail -f nohup.out 追蹤檢視
# 結束就殺掉程序号
0 ✓ 07:57:59 root@miaosen,10.0.0.100:</sub> # kill 8922
[1]+ Terminated nohup ping baidu.com
# 将程序放入背景(開啟一個子shell)
screen
# 先安裝
130 ✗ 07:44:11 root@miaosen,10.0.0.100:<sub> # yum install -y screen
-ls:檢視所有screen的背景程序
-r:指定背景程序号,進入該背景程序
-S:指定背景程序的名字
Ctrl + a + d:放在背景執行
# 栗子
# 先-S指定背景程序的名字
0 ✓ 08:01:02 root@miaosen,10.0.0.100:</sub> # screen -S 10.0.0.111
# 然後在裡面操作
0 ✓ 08:07:16 root@miaosen,10.0.0.100:<sub> # ping 10.0.0.111
# 按 Ctrl + a + d:放在背景執行
# 檢視所有screen的背景程序
0 ✓ 08:07:32 root@miaosen,10.0.0.100:</sub> # screen -ls
There are screens on:
9101.ping_10.0.0.111 (Detached)
9033.ping_baidu (Detached)
2 Sockets in /var/run/screen/S-root.
# 進入背景
screen -r 9101
# 關閉執行
ctrl+c
--- 10.0.0.111 ping statistics ---
#發送144個包, 接受了144個包。 丢包率為0, 用時
144 packets transmitted, 144 received, 0% packet loss, time 144485ms
rtt min/avg/max/mdev = 0.294/1.375/19.781/2.631 ms
平均負載
# 如何檢視cpu的個數
/proc/cpuinfo
top 按 1
lscpu
假設我們在有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%)
但整體趨勢來看,系統負載是在逐漸降低
cpu類型
CPU密集型:計算相關 (打遊戲用這個)
IO密集型:資料庫相關服務 (使用者多這個)
企業級負載分析實戰
上司:某一台伺服器,很卡,怎麼辦
卡的原因:伺服器,負載太高
如何定位,什麼程式,導緻負載高?
如何定位,負載高,是哪個硬體引起的
## 完全是因為目前沒有使用者量
stress是Linux系統壓力測試工具,這裡我們用作異常程序模拟平均負載升高的場景
✗ 08:42:41 root@miaosen,10.0.0.100:<sub> # yum install -y stress
## 啟動了1個cpu密集型的程序,1個占用CPU的程序
0 ✓ 08:44:56 root@miaosen,10.0.0.100:</sub> # stress --cpu 1 --timeout 600
stress: info: [9380] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
## 檢視所有的CPU,5s顯示一次資料
0 ✓ 09:04:44 root@miaosen,10.0.0.100:<sub> # mpstat -P ALL 5
下面可以看出,是使用者态的程序導緻CPU過高
Linux 3.10.0-957.el7.x86_64 (miaosen) 05/08/2022 _x86_64_ (1 CPU)
09:04:52 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
09:04:57 AM all 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
09:04:57 AM 0 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
09:04:57 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
09:05:02 AM all 99.57 0.00 0.22 0.00 0.00 0.22 0.00 0.00 0.00 0.00
09:05:02 AM 0 99.57 0.00 0.22 0.00 0.00 0.22 0.00 0.00 0.00 0.00
## -u指定5s輸出一次資料 2 ,總共輸出2組資料
0 ✓ 09:05:57 root@miaosen,10.0.0.100:</sub> # pidstat -u 5 2
IO:磁盤IO導緻負載升高
[root@localhost <sub>]# stress --io 100 --timeout 600
CPU:CPU會用率會導緻負載升高
[root@localhost </sub>]# stress --cpu 100 --timeout 600
啟動大量程序:導緻負載升高
[root@localhost ~]# stress -c 100 --timeout 600
## 總結分析流程:
1.使用uptime或者top指令檢視,系統負載
2.看1分鐘,5分鐘,15分鐘的負載趨勢
3.是什麼情況導緻負載上升
mpstat -P ALL 5是使用者态,還是核心态,導緻負載上升
使用者态:cpu使用率,大量程序
核心态:磁盤IO,壓縮檔案,網絡存儲挂載,下載下傳檔案,資料庫查詢語句
4.檢視到底是哪個程式,引起使用者态或者核心态的負載上升?
pidstat -u 5 2
5.查到了是某個程序後
-運維
執行了某條指令?
啟動了某個服務?
-開發
檢視開發寫好的程式日志,導出日志,交給開發