查看磁盤IO負載 - 看哪些進程在讀寫磁盤


 

原文:http://www.cnblogs.com/cloudstorage/archive/2012/11/11/2764623.html

今天晚上發現服務器io有點高,順帶看看哪些進程在讀寫磁盤。

1. lsof命令

用 lsof 查看某塊磁盤上的讀寫進程

gddg:~ # lsof /dev/xvda2 |head
COMMAND     PID       USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
init          1       root  cwd    DIR  202,2     4096      2 /
init          1       root  rtd    DIR  202,2     4096      2 /
init          1       root  txt    REG  202,2    40784 193218 /sbin/init
init          1       root  mem    REG  202,2    19114   8063 /lib64/libdl-2.11.1.so
init          1       root  mem    REG  202,2  1661454   8057 /lib64/libc-2.11.1.so
init          1       root  mem    REG  202,2   236384   8114 /lib64/libsepol.so.1
init          1       root  mem    REG  202,2   113904   8115 /lib64/libselinux.so.1
init          1       root  mem    REG  202,2   149797   8050 /lib64/ld-2.11.1.so
kthreadd      2       root  cwd    DIR  202,2     4096      2 /

輸出含義解釋:

COMMAND:進程的名稱
PID:進程標識符
USER:進程所有者
FD:文件描述符,應用程序通過文件描述符識別該文件。如cwd、txt等
TYPE:文件類型,如DIR、REG等
DEVICE:指定磁盤的名稱
SIZE:文件的大小
NODE:索引節點(文件在磁盤上的標識)
NAME:打開文件的確切名稱

FD 列中的文件描述符cwd 值表示應用程序的當前工作目錄,這是該應用程序啟動的目錄,除非它本身對這個目錄進行更改,txt 類型的文件是程序代碼,如應用程序二進制文件本身或共享庫,如上列表中顯示的 /sbin/init 程序。

其次數值表示應用程序的文件描述符,這是打開該文件時返回的一個整數。如上的最后一行文件/dev/initctl,其文件描述符為 10。u 表示該文件被打開並處於讀取/寫入模式,而不是只讀 ® 或只寫 (w) 模式。同時還有大寫 的W 表示該應用程序具有對整個文件的寫鎖。該文件描述符用於確保每次只能打開一個應用程序實例。初始打開每個應用程序時,都具有三個文件描述符,從 0 到 2,分別表示標准輸入、輸出和錯誤流。所以大多數應用程序所打開的文件的 FD 都是從 3 開始。

與 FD 列相比,Type 列則比較直觀。文件和目錄分別稱為 REG 和 DIR。而CHR 和 BLK,分別表示字符和塊設備;或者 UNIX、FIFO 和 IPv4,分別表示 UNIX 域套接字、先進先出 (FIFO) 隊列和網際協議 (IP) 套接字。

  

常用參數:

lsof `which httpd` //那個進程在使用apache的可執行文件
lsof /etc/passwd //那個進程在占用/etc/passwd
lsof /dev/hda6 //那個進程在占用hda6
lsof /dev/cdrom //那個進程在占用光驅
lsof -c sendmail //查看sendmail進程的文件使用情況
lsof -c courier -u ^zahn //顯示出那些文件被以courier打頭的進程打開,但是並不屬於用戶zahn
lsof -p 30297 //顯示那些文件被pid為30297的進程打開
lsof -D /tmp 顯示所有在/tmp文件夾中打開的instance和文件的進程。但是symbol文件並不在列

lsof -u1000 //查看uid是100的用戶的進程的文件使用情況
lsof -utony //查看用戶tony的進程的文件使用情況
lsof -u^tony //查看不是用戶tony的進程的文件使用情況(^是取反的意思)
lsof -i //顯示所有打開的端口
lsof -i:80 //顯示所有打開80端口的進程
lsof -i -U //顯示所有打開的端口和UNIX domain文件
lsof -i UDP@[url]www.akadia.com:123 //顯示那些進程打開了到www.akadia.com的UDP的123(ntp)端口的鏈接
lsof -i tcp@ohaha.ks.edu.tw:ftp -r //不斷查看目前ftp連接的情況(-r,lsof會永遠不斷的執行,直到收到中斷信號,+r,lsof會一直執行,直到沒有檔案被顯示,缺省是15s刷新)
lsof -i tcp@ohaha.ks.edu.tw:ftp -n //lsof -n 不將IP轉換為hostname,缺省是不加上-n參數

  

然后可以通過 lsof -p $pid 查看詳情

gddg:~ # lsof -p 32597
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
bash    32597 root  cwd    DIR  202,2     4096  16097 /root
bash    32597 root  rtd    DIR  202,2     4096      2 /
bash    32597 root  txt    REG  202,2   584016  32203 /bin/bash
bash    32597 root  mem    REG  202,2   293936   8125 /lib64/libncurses.so.5.6
bash    32597 root  mem    REG  202,2  1661454   8057 /lib64/libc-2.11.1.so
bash    32597 root  mem    REG  202,2    19114   8063 /lib64/libdl-2.11.1.so
bash    32597 root  mem    REG  202,2   263568   8153 /lib64/libreadline.so.5.2
bash    32597 root  mem    REG  202,2   149797   8050 /lib64/ld-2.11.1.so
bash    32597 root  mem    REG  202,2   217016  16498 /var/run/nscd/passwd
bash    32597 root  mem    REG  202,2   256324 149503 /usr/lib/locale/en_US.utf8/LC_CTYPE
bash    32597 root  mem    REG  202,2       54 149490 /usr/lib/locale/en_US.utf8/LC_NUMERIC
bash    32597 root  mem    REG  202,2     2454 133112 /usr/lib/locale/en_US.utf8/LC_TIME
bash    32597 root  mem    REG  202,2  1163682 149504 /usr/lib/locale/en_US.utf8/LC_COLLATE
bash    32597 root  mem    REG  202,2      286 133111 /usr/lib/locale/en_US.utf8/LC_MONETARY
bash    32597 root  mem    REG  202,2       57 149408 /usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
bash    32597 root  mem    REG  202,2       34 149428 /usr/lib/locale/en_US.utf8/LC_PAPER
bash    32597 root  mem    REG  202,2       77 149438 /usr/lib/locale/en_US.utf8/LC_NAME
bash    32597 root  mem    REG  202,2      155 133108 /usr/lib/locale/en_US.utf8/LC_ADDRESS
bash    32597 root  mem    REG  202,2       59 149407 /usr/lib/locale/en_US.utf8/LC_TELEPHONE
bash    32597 root  mem    REG  202,2       23 149429 /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
bash    32597 root  mem    REG  202,2    26050 149293 /usr/lib64/gconv/gconv-modules.cache

  

2. cat /proc/$pid/io

如果內核版本大於2.6.20,通過cat /proc/pid/io 便可以獲取進程的io信息。詳細解釋

gddg:~ # cat /proc/4140/io
rchar: 197448798054        // 讀出的總字節數,read()或者pread()中的長度參數總和(pagecache中統計而來,不代表實際磁盤的讀入)
wchar: 209896059897        // 寫入的總字節數,write()或者pwrite()中的長度參數總和
syscr: 6491904             // read()或者pread()總的調用次數
syscw: 13633940            // write()或者pwrite()總的調用次數
read_bytes: 49616125952    // 實際從磁盤中讀取的字節總數
write_bytes: 14038130688   // 實際寫入到磁盤中的字節總數
cancelled_write_bytes: 2473984     // 由於截斷pagecache導致應該發生而沒有發生的寫入字節數

  

3. block_dump

通過echo 1 > /proc/sys/vm/block_dump ,來把 block 讀寫(WRITE/READ/DIRTY)狀況 dump 到日志里,通過 dmesg 命令來查看

#!/bin/sh

/etc/init.d/syslog stop
echo 1 > /proc/sys/vm/block_dump

sleep 60
dmesg | awk '/(READ|WRITE|dirtied)/ {process[$1]++} END {for (x in process) \
print process[x],x}' |sort -nr |awk '{print $2 " " $1}' | \
head -n 10
#dmesg | egrep "READ|WRITE|dirtied" | egrep -o '([a-zA-Z]*)' | sort | uniq -c | sort -rn |
head

echo 0 > /proc/sys/vm/block_dump
/etc/init.d/syslog start

  

輸出如下:

pdflush(10423): 4000
nginx(1167): 179
nginx(1229): 172
nginx(1187): 111
nginx(1243): 105
nginx(1213): 92
nginx(1233): 69
nginx(1157): 61
nginx(1161): 50
nginx(1155): 32

  

或者參考這段代碼吧:
http://stackoverflow.com/questions/249570/how-can-i-record-what-process-or-kernel-activity-is-using-the-disk-in-gnu-linux

sudo -s
dmesg -c
/etc/init.d/klogd stop
echo 1 > /proc/sys/vm/block_dump
rm /tmp/disklog
watch "dmesg -c >> /tmp/disklog"
   CTRL-C when you're done collecting data
echo 0 > /proc/sys/vm/block_dump
/etc/init.d/klogd start
exit (quit root shell)

cat /tmp/disklog | awk -F"[() \t]" '/(READ|WRITE|dirtied)/ {activity[$1]++} END {for (x in activity) print x, activity[x]}'| sort -nr -k2

  

參考:

http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316599.html

http://blog.csdn.net/guoguo1980/article/details/2324454

http://www.vpsee.com/2009/08/monitor-process-io-activity/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM