影響
IO負載高會導致IO處理時間變長,IO等待隊列變長,系統負載變高;
IO負載高排查思路
當服務器的IO負載很高時(iostat中的util),可以通過以下思路快速定位到IO負載的來源進程和來源文件,甚至具體的代碼從而快速解決問題:
- 查看系統IO情況,iostat命令;
- 查看各進程IO使用情況,iotop/pidstat命令;
- 查看某個進程文件讀寫情況,ioprofile/lsof + strace;
查看系統IO情況
說明:iostat和top命令中的iowait並不能等於IO負載情況,只是反映了CPU等待IO時間佔用CPU總時間的百分比;使用iostat命令查看系統IO情況,如下:
[root@master ~]# iostat -xdm 1
Linux 3.10.0-514.16.1.el7.x86_64 (master) 05/05/2019 _x86_64_ (48 CPU)
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 4.28 3.81 1.93 3.75 0.03 0.04 26.21 0.06 10.25 26.09 2.08 0.45 0.25
sdb 5.70 9.00 13.21 139.56 0.38 1.87 30.23 0.07 0.43 2.80 0.20 0.09 1.41
dm-0 0.00 0.00 0.59 3.02 0.00 0.01 8.00 0.00 0.85 4.06 0.22 0.55 0.20
dm-1 0.00 0.00 12.11 138.73 0.35 1.64 27.02 0.10 0.64 3.96 0.35 0.09 1.32
dm-2 0.00 0.00 12.11 138.73 0.35 1.64 27.02 0.10 0.64 3.96 0.35 0.09 1.34
dm-3 0.00 0.00 6.16 5.20 0.03 0.21 42.40 0.01 0.57 0.51 0.65 0.20 0.22
# iostat 選項
-x Display extended statistics.
This option works with post 2.5 kernels since it needs /proc/diskstats file or a mounted sysfs to get the statistics.
This option may also work with older kernels (e.g. 2.4) only if extended statistics are available in /proc/partitions
(the kernel needs to be patched for that).
-d Display the device utilization report.
-m Display statistics in megabytes per second instead of blocks or kilobytes per second.
Data displayed are valid only with kernels 2.4 and later.
- await:IO請求平均響應時間(隊列排隊時間+IO處理時間),一般系統 I/O 響應時間應該低於 5ms,如果大於 10ms 就比較大了;
- svctm:IO處理時間,即尋道 + 旋轉延遲時間;
- %util:磁盤繁忙程度。 例如,如果統計間隔 1 秒,該設備有 0.8 秒在處理 I/O,而 0.2 秒閒置,那麼該設備的 %util = 0.8/1 = 80%;
- avgqu-sz:IO請求的平均隊列長度;
查看進程使用IO
iotop/pidstat命令可以查看系統中每個進程IO使用情況,利用這個信息可以找出佔用IO最多的進程;
[root@master ~]# pidstat -d 1
Linux 3.10.0-514.16.1.el7.x86_64 (master) 05/05/2019 _x86_64_ (48 CPU)
07:21:45 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
07:21:46 PM 0 34197 0.00 4.00 4.00 gunicorn:
07:21:48 PM 0 465 0.00 16.00 16.00 mongod
07:21:51 PM 0 14989 0.00 8.00 0.00 java
查看進程文件讀寫信息
ioprofile可以統計出某個進程對每個文件的讀寫情況,本質上是 lsof + strace
#lsof:查看文件打開情況
查看某個進程打開的所有文件: lsof -p <pid>
查看打開某個某個文件的所有進程: lsof <file>
#strace:跟蹤系統調用
#timeout:限制命令執行時間
timeout 5 strace -f -F -o <outputfile> -p <pid>
參考:
- https://blueswind8306.iteye.com/blog/2032980
- iotop和pidstat使用:http://blog.itpub.net/21980353/viewspace-2113906/
- lsof使用:https://www.cnblogs.com/peida/archive/2013/02/26/2932972.html
- Linux下的IO監控與分析:http://www.cnblogs.com/quixotic/p/3258730.html
- strace:https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/strace.html