linux下可以通過iostat查看目前主機總的io使用情況,不過當通過top等命令查看時,發現cpu wait佔多過多,想定位具體是哪些程序佔用了IO,本篇就通過一些常用的手段進行彙總下。
一、top下的wait狀狀
wa = I/O waiting ,wa指的是CPU等待磁盤寫入完成的時間,就是說前提是要進行IO操作,在進行IO操作的時候,CPU等待時間。如一個程序執行的最後,從系統空間到dst硬盤空間的時候,如果程序是阻塞的,那麼這個時候cpu就要等待數據寫入磁盤才能完成寫操作了。所以這個時候cpu等待的時間就是wa。
所以wa狀態佔比越高,證明IO越繁忙。
我們可以通過以下指令查看當前io的總終態:
[root@361way ~]# sar -u 2 5
Linux 2.6.32-431.29.2.el6.x86_64 (361way) 01/25/2015 _x86_64_ (1 CPU)
06:59:58 PM CPU %user %nice %system %iowait %steal %idle
07:00:00 PM all 1.00 0.00 0.50 0.00 0.00 98.50
07:00:02 PM all 5.05 0.00 1.52 1.01 0.00 92.42
07:00:04 PM all 0.50 0.00 0.50 0.00 0.00 98.99
07:00:06 PM all 0.00 0.00 0.00 0.00 0.00 100.00
07:00:08 PM all 0.50 0.00 0.00 0.50 0.00 98.99
Average: all 1.41 0.00 0.50 0.30 0.00 97.79
[root@361way ~]# vmstat 2 5
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 98488 209916 218256 0 0 8 28 11 10 5 0 95 0 0
0 0 0 98480 209916 218256 0 0 0 0 155 198 0 0 100 0 0
0 0 0 98496 209916 218256 0 0 0 0 141 191 1 0 99 0 0
0 0 0 98496 209916 218256 0 0 0 0 142 189 0 1 99 0 0
0 0 0 98512 209916 218256 0 0 0 0 152 189 0 0 100 0 0
[root@361way ~]# iostat -x 1 2
Linux 2.6.32-431.29.2.el6.x86_64 (361way) 01/25/2015 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
4.71 0.00 0.42 0.24 0.00 94.63
Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
xvda 0.00 0.13 0.31 0.91 14.08 8.30 18.39 0.01 11.84 1.38 0.17
xvdb 0.00 2.12 0.05 3.90 1.52 48.10 12.57 0.05 11.84 0.57 0.22
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.00 0.00 0.00 100.00
Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
xvda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
xvdb 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
二、找出IO繁忙的進程
可以通過linux下最常用的兩個命令top、ps找出目前正在佔用IO的進程。在進行下一步之前我瞭解一下D狀態,ps命令中查看的狀態有D、R、S、Z、T ,D狀態指不可中斷睡眠 (通常是在IO操作) 收到信號不喚醒和不可運行, 進程必須等待直到有中斷髮生。
找出當前進程為D的狀態:
while true; do date; ps auxf | awk '{if($8=="D") print $0;}'; sleep 1; don
上面的這個操作有一個缺點,就是一旦匹配到一個進程為D的,就開始重新執行下一個循環。而想查看所有當於D狀態的進程,可以將其改為如下:
watch -d -n 1 "(ps aux | awk '\$8 ~ /D/ { print \$0 }')"
如下,是我在執行一個大文件的cp時,通過上面的指令查看到的結果:
除此之外,還可以通過專用工具去查看,如iotop或latencytop。這裡以iotop為例,執行後會顯示當前的讀寫速度及每個進程佔用的速度。執行後可以按o僅顯示佔用IO的進程:
[root@361way ~]# iotop --help
Usage: /usr/sbin/iotop [OPTIONS]
DISK READ and DISK WRITE are the block I/O bandwidth used during the sampling
period. SWAPIN and IO are the percentages of time the thread spent respectively
while swapping in and waiting on I/O more generally. PRIO is the I/O priority at
which the thread is running (set using the ionice command).
Controls: left and right arrows to change the sorting column, r to invert the
sorting order, o to toggle the --only option, p to toggle the --processes
option, a to toggle the --accumulated option, q to quit, any other key to force
a refresh.
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-o, --only only show processes or threads actually doing I/O
-b, --batch non-interactive mode
-n NUM, --iter=NUM number of iterations before ending [infinite]
-d SEC, --delay=SEC delay between iterations [1 second]
-p PID, --pid=PID processes/threads to monitor [all]
-u USER, --user=USER users to monitor [all]
-P, --processes only show processes, not all threads
-a, --accumulated show accumulated I/O instead of bandwidth
-k, --kilobytes use kilobytes instead of a human friendly unit
-t, --time add a timestamp on each line (implies --batch)
-q, --quiet suppress some lines of header (implies --batch)
閱讀更多 IT小白進階之路 的文章