iotop是一个类似top的检测process IO 的Python 项目。可以在这里下载到源码,可以在这里找到具体的说明。下面做一个演示:

首先下载源码,要求python version>=2.6

wget http://guichaz.free.fr/iotop/files/iotop-0.5.tar.gz
wget http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tgz

编译python

[root@oel58 tmp]# cd /tmp/Python-2.6.6
[root@oel58 Python-2.6.6]#./configure && make && make install

....


checking for %zd printf() format support... yes
checking for socklen_t... yes
checking for build directories... done
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile


...

Writing /usr/local/lib/python2.6/lib-dynload/Python-2.6.6-py2.6.egg-info
if test -f /usr/local/bin/python -o -h /usr/local/bin/python; \
        then rm -f /usr/local/bin/python; \
        else true; \
        fi
(cd /usr/local/bin; ln python2.6 python)
rm -f /usr/local/bin/python-config
(cd /usr/local/bin; ln -s python2.6-config python-config)
/usr/bin/install -c -m 644 ./Misc/python.man \
                /usr/local/share/man/man1/python.1

编译完成之后,编译iotop 源码

[root@oel58 tmp]# cd iotop-0.5
[root@oel58 iotop-0.5]# ./setup.py  install
running install
running build
running build_py
creating build
creating build/lib
creating build/lib/iotop
copying iotop/version.py -> build/lib/iotop
copying iotop/netlink.py -> build/lib/iotop
copying iotop/__init__.py -> build/lib/iotop
copying iotop/ui.py -> build/lib/iotop
copying iotop/data.py -> build/lib/iotop
copying iotop/genetlink.py -> build/lib/iotop
copying iotop/vmstat.py -> build/lib/iotop
copying iotop/ioprio.py -> build/lib/iotop
running build_scripts
creating build/scripts-2.6
copying and adjusting sbin/iotop -> build/scripts-2.6
changing mode of build/scripts-2.6/iotop from 644 to 755
running install_lib
creating /usr/local/lib/python2.6/site-packages/iotop
copying build/lib/iotop/version.py -> /usr/local/lib/python2.6/site-packages/iotop
copying build/lib/iotop/netlink.py -> /usr/local/lib/python2.6/site-packages/iotop
copying build/lib/iotop/__init__.py -> /usr/local/lib/python2.6/site-packages/iotop
copying build/lib/iotop/ui.py -> /usr/local/lib/python2.6/site-packages/iotop
copying build/lib/iotop/data.py -> /usr/local/lib/python2.6/site-packages/iotop
copying build/lib/iotop/genetlink.py -> /usr/local/lib/python2.6/site-packages/iotop
copying build/lib/iotop/vmstat.py -> /usr/local/lib/python2.6/site-packages/iotop
copying build/lib/iotop/ioprio.py -> /usr/local/lib/python2.6/site-packages/iotop
byte-compiling /usr/local/lib/python2.6/site-packages/iotop/version.py to version.pyc
byte-compiling /usr/local/lib/python2.6/site-packages/iotop/netlink.py to netlink.pyc
byte-compiling /usr/local/lib/python2.6/site-packages/iotop/__init__.py to __init__.pyc
byte-compiling /usr/local/lib/python2.6/site-packages/iotop/ui.py to ui.pyc
byte-compiling /usr/local/lib/python2.6/site-packages/iotop/data.py to data.pyc
byte-compiling /usr/local/lib/python2.6/site-packages/iotop/genetlink.py to genetlink.pyc
byte-compiling /usr/local/lib/python2.6/site-packages/iotop/vmstat.py to vmstat.pyc
byte-compiling /usr/local/lib/python2.6/site-packages/iotop/ioprio.py to ioprio.pyc
running install_scripts
copying build/scripts-2.6/iotop -> /usr/local/bin
changing mode of /usr/local/bin/iotop to 755
running install_data
copying iotop.8 -> /usr/local/share/man/man8
running install_egg_info
Writing /usr/local/lib/python2.6/site-packages/iotop-0.5-py2.6.egg-info

--------------------------------------------------------------

[root@oel58 bin]# ./iotop --only
Total DISK READ :       0.00 B/s | Total DISK WRITE :     255.17 M/s
Actual DISK READ:       0.00 B/s | Actual DISK WRITE:     256.64 M/s
  TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND                                                                                                 
16389 be/4 root        0.00 B/s   48.40 M/s  0.00 % 11.61 % dd if /dev/zero of /tmp/temp.txt bs 1M count 10000
16418 be/4 root        0.00 B/s   58.64 M/s  0.00 % 11.55 % dd if /dev/zero of /tmp/temp.txt bs 1M count 10000
16359 be/4 root        0.00 B/s   73.66 M/s  0.00 %  3.48 % dd if /dev/zero of /tmp/temp.txt bs 1M count 10000
 6756 be/4 root        0.00 B/s    0.00 B/s  0.00 %  5.97 % [flush-8:0]
16418 be/4 root        0.00 B/s   21.15 M/s  0.00 %  4.52 % dd if /dev/zero of /tmp/temp.txt bs 1M count 10000
16447 be/4 root        0.00 B/s   23.91 M/s  0.00 %  3.02 % dd if /dev/zero of /tmp/temp.txt bs 1M count 10000
  109 be/4 root        3.54 K/s 1030.75 K/s  0.00 %  2.36 % [kjournald]
16359 be/4 root        0.00 B/s   22.91 M/s  0.00 %  1.69 % dd if /dev/zero of /tmp/temp.txt bs 1M count 10000

Python ≥ 2.7 and a Linux kernel ≥ 2.6.20 with the TASK_DELAY_ACCT CONFIG_TASKSTATS, TASK_IO_ACCOUNTING and CONFIG_VM_EVENT_COUNTERS options on.

经测试py version >2.6即可,此外需要说明如果kernel version < 2.6.20 会出现如下问题:

Total DISK READ :	0.00 B/s | Total DISK WRITE :       0.00 B/s
Actual DISK READ:	0.00 B/s | Actual DISK WRITE:      16.26 K/s
TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND


CONFIG_TASK_DELAY_ACCT not enabled in kernel, cannot determine SWAPIN and IO %   

strace iotop主要调用的python模块

open("/usr/local/lib/python2.6/lib-dynload/_socket.so", O_RDONLY) = 9
open("/usr/local/lib/python2.6/lib-dynload/_ssl.so", O_RDONLY|O_LARGEFILE) = 8
open("/usr/local/lib/python2.6/site-packages/iotop/vmstat.py", O_RDONLY|O_LARGEFILE) = 5
open("/usr/local/lib/python2.6/site-packages/iotop/vmstat.pyc", O_RDONLY|O_LARGEFILE) = 6
open("/usr/local/lib/python2.6/lib-dynload/cStringIO.so", O_RDONLY) = 7
open("/usr/local/lib/python2.6/lib-dynload/cStringIO.so", O_RDONLY|O_LARGEFILE) = 6

.....

iotop 对python的依赖性过高,以及对于version的要求过于严格,导致了通用性和移植性的下降。