Chinaunix首页 | 论坛 | 博客
  • 博客访问: 829975
  • 博文数量: 167
  • 博客积分: 7173
  • 博客等级: 少将
  • 技术积分: 1671
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-04 23:07
文章分类

全部博文(167)

文章存档

2018年(1)

2017年(11)

2012年(2)

2011年(27)

2010年(88)

2009年(38)

分类: LINUX

2010-06-08 18:00:47

linux系统上可以使用(centos 2.6.18-144开始支持),dstat版本至少是:dstat-0.6.7-1.rf.noarch.rpm

安装

wget -c

rpm -Uvh dstat-0.6.7-1.rf.noarch.rpm

使用

# dstat -M topio -d -M topbio

—-most-expensive—- -dsk/total- —-most-expensive—-

i/o process      | read  writ|  block i/o process

owl_agent  9642B: 439B|  38k   42k|init       8317B:  41B

nginx         0 :2005B|   0    26k|

gmond         0 :  16k|   0    17k|

gmond         0 : 444B|   0     0 |

其他

低于这个kernel版本的可以参考这个方 法:http://www.xaprb.com/blog/2009/08/23/how-to-find-per-process-io- statistics-on-linux/


以上转载


因为我的机器上达不了这个版本所以改写了一下程序


效果变为:


程序代码
!/usr/bin/python
# Monitoring per-process disk I/O activity

import sys, os, time, signal, re , string

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)
    interatime = 0
    maxtry = 100000
    i=0
    if argc != 1 and argc !=2 and argc !=3:
        print "usage: python prethread.py"
        print "usage: python prethread.py count"
        print "usage: python prethread.py count interval"
        sys.exit(0)

    if os.getuid() != 0:
        print "must be run as root"
        sys.exit(0)
    if argc == 2:
        maxtry = string.atoi(sys.argv[1])
    if argc == 3:
        maxtry = string.atoi(sys.argv[1])
        interatime = string.atoi(sys.argv[2])

    signal.signal(signal.SIGINT, signal_handler)
    os.system('echo 1 > /proc/sys/vm/block_dump')
    print "TASK              PID       READ      WRITE"
    while i < maxtry:
        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)
        print "-----------------------------------------------"
        if interatime >0:
            time.sleep(interatime)
        i= i+1
    os.system("rm -f  /tmp/diskio.log")

def signal_handler(signal, frame):
    os.system('echo 0 > /proc/sys/vm/block_dump')
    os.system("rm -f  /tmp/diskio.log")
    sys.exit(0)

if __name__=="__main__":
    main()
阅读(2309) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

hkebao2010-07-11 12:45:22

这个方法据说误差较大。我正在想能否用C实现一个小插件来做这件事情

hkebao2010-07-11 12:44:37

http://blog.chinaunix.net/u2/84280/showart_2267955.html