Linux Kernel 2.6.20 以上的内核支持进程 IO 统计,可以用类似 iotop 这样的工具来监测每个进程对 IO 操作的情况,就像用 top 来实时查看进程内存、CPU 等占用情况那样。但是对于 2.6.20 以下的 Linux 内核版本就没那么幸运了,根据 Stack Overflow 的这篇回帖 给出的方法,VPSee 写了一个简单的 Python 脚本用来在 linux kernel < 2.6.20 下打印进程 IO 状况。
Kernel < 2.6.20
这个脚本的想法很简单,把 dmesg 的结果重定向到一个文件后再解析出来,每隔1秒钟打印一次进程 IO 读写的统计信息,执行这个脚本需要 root:
#!/usr/bin/python # Monitoring per-process disk I/O activity # written by http://www.vpsee.com import sys, os, time, signal, re class DiskIO: def __init__(self, pname=None, pid=None, reads=0, writes=0): self.pname = pname self.pid = pid self.reads = 0 self.writes = 0 def main(): argc = len(sys.argv) if argc != 1: print "usage: ./iotop" sys.exit(0) if os.getuid() != 0: print "must be run as root" sys.exit(0) signal.signal(signal.SIGINT, signal_handler) os.system('echo 1 > /proc/sys/vm/block_dump') print "TASK PID READ WRITE" while True: os.system('dmesg -c > /tmp/diskio.log') l = [] f = open('/tmp/diskio.log', 'r') line = f.readline() while line: m = re.match(\ '^(\S+)\((\d+)\): (READ|WRITE) block (\d+) on (\S+)', line) if m != None: if not l: l.append(DiskIO(m.group(1), m.group(2))) line = f.readline() continue found = False for item in l: if item.pid == m.group(2): found = True if m.group(3) == "READ": item.reads = item.reads + 1 elif m.group(3) == "WRITE": item.writes = item.writes + 1 if not found: l.append(DiskIO(m.group(1), m.group(2))) line = f.readline() time.sleep(1) for item in l: print "%-10s %10s %10d %10d" % \ (item.pname, item.pid, item.reads, item.writes) def signal_handler(signal, frame): os.system('echo 0 > /proc/sys/vm/block_dump') sys.exit(0) if __name__=="__main__": main()
Kernel >= 2.6.20
如果想用 iotop 来实时查看进程 IO 活动状况的话,需要下载和升级新内核(2.6.20 或以上版本)。编译新内核时需要打开 TASK_DELAY_ACCT 和 TASK_IO_ACCOUNTING 选项。解压内核后进入配置界面:
# tar jxvf linux-2.6.30.5.tar.bz2 # mv linux-2.6.30.5 /usr/src/ # cd /usr/src/linux-2.6.30.5 # make menuconfig
选择 Kernel hacking –> Collect scheduler debugging info 和 Collect scheduler statistics,保存内核后编译内核:
# make; make modules; make modules_install; make install
修改 grub,确认能正确启动新内核:
# vi /boot/grub/menu.lst
出了新内核外,iotop 还需要 Python 2.5 或以上才能运行,所以如果当前 Python 是 2.4 的话需要下载和安装最新的 Python 包。这里使用源代码编译安装:
# tar jxvf Python-2.6.2.tar.bz2 # cd Python-2.6.2 # ./configure # make; make install
别忘了下载 setuptools:
# mv setuptools-0.6c9-py2.6.egg.sh setuptools-0.6c9-py2.6.egg # sh setuptools-0.6c9-py2.6.egg
附:
linux@joeyon:~$ sudo iotop
16075 be/4 www-data 0.00 B/s 7.71 K/s 0.00 % 0.00 % nginx: worker process
Total DISK READ: 0.00 B/s | Total DISK WRITE: 193.00 K/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
943 be/4 root 0.00 B/s 84.92 K/s li% 5.34 % [kjournald]
16479 be/4 linux 0.00 B/s 0.00 B/s 0.00 % 0.12 % java -Djava.util.logging.config.file=/data/websites/a~-back/temp org.apache.catalina.startup.Bootstrap start
15941 be/4 linux 0.00 B/s 3.86 K/s 0.00 % 0.00 % java -Djava.util.logging.config.file=/data/websites/a~slave/temp org.apache.catalina.startup.Bootstrap start
16075 be/4 www-data 0.00 B/s 11.58 K/s 0.00 % 0.00 % nginx: worker process
16295 be/4 linux 0.00 B/s 3.86 K/s 0.00 % 0.00 % java -Djava.util.logging.config.file=/data/websites/a~eiver/temp org.apache.catalina.startup.Bootstrap start
16384 be/4 linux 0.00 B/s 0.00 B/s 0.00 % 0.00 % java -Djava.util.logging.config.file=/data/websites/a~ister/temp org.apache.catalina.startup.Bootstrap start
1 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % init
2 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kthreadd]
3 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/0]
16388 be/4 linux 0.00 B/s 0.00 B/s 0.00 % 0.00 % java -Djava.util.logging.config.file=/data/websites/a~t-app/temp org.apache.catalina.startup.Bootstrap start
16389 be/4 linux 0.00 B/s 0.00 B/s 0.00 % 0.00 % java -Djava.util.logging.config.file=/data/websites/a~t-app/temp org.apache.catalina.startup.Bootstrap start
6 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/0]
7 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/0]
8 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/1]
9 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kworker/1:0]
10 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/1]
12 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/1]
13 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/2]
15 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/2]
16 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/2]
17 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/3]
18 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kworker/3:0]
19 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/3]
20 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/3]
21 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/4]
23 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/4]
24 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/4]
25 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/5]
26 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kworker/5:0]
27 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/5]
28 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/5]
29 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/6]
30 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kworker/6:0]
31 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/6]
32 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/6]
33 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/7]
34 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kworker/7:0]
35 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/7]
36 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/7]
37 be/0 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [cpuset]
38 be/0 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [khelper]
39 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kdevtmpfs]
40 be/0 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [netns]
42 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [sync_supers]
参考:http://www.vpsee.com/2009/08/monitor-process-io-activity/