linux系統發現系統變慢
阿新 • • 發佈:2020-07-13
發現系統變慢首先執行top和uptime命令
[root@localhost ~]# uptime
02:32:11 up 10 min, 2 users, load average: 0.00, 0.11, 0.15
當前時間-系統執行時間-系統正在登陸的使用者數-過去一分鐘、五分鐘、十五分鐘的平均負載
- 如果1分鐘、5分鐘、15分鐘的三個值基本相同,或者相差不大,那就說明系統負載很平穩。
- 但如果1分鐘的值遠小於15 分鐘的值,就說明系統最近1分鐘的負載在減少,而過去15分鐘內卻有很大的負載。
- 反過來,如果1分鐘的值遠大於 15 分鐘的值,就說明最近1分鐘的負載在增加,這種增加有可能只是臨時性的,也有可能還會持續增加下去,所以就需要持續觀察。一旦1分鐘的平均負載接近或超過了CPU的個數,就意味著系統正在發生過載的問題,這時就得分析調查是哪裡導致的問題,並要想辦法優化了。
- 當平均負載高於CPU數量的70%的時候就要查詢原因了,一旦平均負載過高就會導致系統程序響應變慢,進而影響服務的正常功能
CPU 使用率,是單位時間內 CPU 繁忙情況的統計,跟平均負載並不一定完全對應CPU 密集型程序,使用大量 CPU 會導致平均負載升高,此時這兩者是一致的;2I/O 密集型程序,等待 I/O 也會導致平均負載升高,但 CPU 使用率不一定很高;大量等待 CPU 的程序排程也會導致平均負載升高,此時的CPU使用率也會比較高。
使用工具iostat(stress)、mpstat、pidstat 等工具,找出平均負載升高的根源
yum install -y epel-release
yum install -y stress
yum install -y sysstat
- stress 是一個 Linux 系統壓力測試工具,這裡我們用作異常程序模擬平均負載升高的場景
- 而 sysstat 包含了常用的 Linux 效能工具,用來監控和分析系統的效能。我們的案例會用到這個包的兩個命令 mpstat 和 pidstat。
- mpstat 是一個常用的多核 CPU 效能分析工具,用來實時檢視每個 CPU 的效能指標,以及所有CPU的平均指標。
- pidstat 是一個常用的程序效能分析工具,用來實時檢視程序的 CPU、記憶體、I/O 以及上下文切換等效能指標
模擬環境
[root@localhost ~]# stress --cpu 1 --timeout 600 stress: info: [7272] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd 這是模擬CPU使用率超高環境,然後在第二個終端執行uptime檢視平均負載
然後在第三個終端檢視CPU使用率的變化情況
-
使用mpstat 命令
[root@localhost ~]# mpstat -P ALL 5 Linux 3.10.0-957.el7.x86_64 (localhost.localdomain) 04/23/2020 _x86_64_ (1 CPU) 03:00:11 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 03:00:16 AM all 99.60 0.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 03:00:16 AM 0 99.60 0.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 03:00:16 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 03:00:21 AM all 99.60 0.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 03:00:21 AM 0 99.60 0.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 03:00:21 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 03:00:26 AM all 99.60 0.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 03:00:26 AM 0 99.60 0.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00
這時候我們可以看到有一個CPU的使用率太高了,那麼就用pidstat 找到這個程序
-
pidstat -u 5 2 間隔5秒後輸出2組資料,-u表示CPU指標
[root@localhost ~]# pidstat -u 5 2
Linux 3.10.0-957.el7.x86_64 (localhost.localdomain) 04/23/2020 _x86_64_ (1 CPU)
03:06:39 AM UID PID %usr %system %guest %wait %CPU CPU Command
03:06:44 AM 0 7275 0.20 0.20 0.00 0.20 0.40 0 watch
03:06:44 AM 0 7861 98.80 0.00 0.00 0.80 98.80 0 stress
03:06:44 AM 0 7901 0.00 0.20 0.00 0.00 0.20 0 pidstat
這時可以看到是stress佔用較高,可以用ps -ef|grep stress找出他的程序號用kill -9 殺掉
[root@localhost ~]# ps -ef |grep stress
root 7860 7201 0 03:06 pts/0 00:00:00 stress --cpu 1 --timeout 600
root 7861 7860 99 03:06 pts/0 00:06:25 stress --cpu 1 --timeout 600
root 8250 7233 0 03:12 pts/1 00:00:00 grep --color=auto stress
[root@localhost ~]# kill -9 7860
[root@localhost ~]#
開機自啟動
7 個執行級別
-
init 0 關機
-
init 6 重啟
-
init 5 圖形化介面
-
init 3 完整的多使用者模式(命令列)
-
init 2 多使用者模式(沒有檔案系統和網路)
-
init 1 單使用者模式