有没有办法在 Ubuntu 中根据进程查看 IO 统计数据。我有一台 ubuntu 服务器 10.10,它会不时地在硬盘上磨 30 秒,我想找出是什么原因造成的。提前致谢。我运行的只有 mysql,但即使 mysql 没有任何负载(没有连接,只是空闲),这种情况也会发生。
答案1
我用iotop
(命令行工具)。
sudo apt-get install iotop
答案2
iotop 大部分时间都在做这项工作。不过,我发现以下 Python 脚本更有用:
(它仅显示当前活动的进程)
#!/usr/bin/python
from glob import *
from time import *
from os.path import *
import sys, os
memory = {}
sleep_time = 5;
unit = 1024.0;
unit_name = "KiB"
for argument in sys.argv:
if argument.find( "=" ) >= 0:
field, value = argument.split( "=" );
if field == "s":
sleep_time = int( value );
if argument == "KB": #fake Kilobyte
unit = 1000.0;
unit_name = "KB"
if argument == "KiB": #real Kilobyte
unit = float(2**10)
unit_name = "KiB"
if argument == "MB": #fake Megabyte
unit = 1e6;
unit_name = "MB"
if argument == "MiB": #real Megabyte
unit = float(2**20)
unit_name = "MiB"
if argument == "GB": #fake Gigabyte
unit = 1e9;
unit_name = "GB"
if argument == "GiB": #real Gigabyte
unit = float(2**30)
unit_name = "GiB"
while True:
os.system( "clear" );
table = [];
for item in memory.items():
item[1]["to_delete"] = 1;
process_list = glob( "/proc/*/status" );
for process in process_list:
name = ""
pid = ""
rb = 0
wb = 0
try:
f = open( process, "r" );
for line in f:
field, value = line.split( ":" );
if field == "Name":
name = value.strip();
if field == "Pid":
pid = value.strip();
break;
f.close();
io_file = dirname( process ) + "/io"
f = open( io_file, "r" )
for line in f:
field, value = line.split( ":" );
if field == "read_bytes":
rb = int(value);
if field == "write_bytes":
wb = int(value);
break;
f.close();
except:
pass
item = memory.get( pid, { "PID":pid, "NAME":name, "READ":rb, "WRITE":wb } );
item["to_delete"] = 0;
if ( rb - item["READ"] > 0 or wb - item["WRITE"] > 0 ):
table += [[ pid, name, rb - item["READ"], wb - item["WRITE"] ]]
item["READ"] = rb;
item["WRITE"] = wb;
memory[pid] = item;
for item in memory.items():
if item[1]["to_delete"]:
memory.pop( item[0] )
print "PID".rjust(7) + " " + "PROCESS".ljust(30)+"READ".rjust(20) + "WRITE".rjust(20)
for row in table:
print row[0].rjust(7) + " " + str(row[1]).ljust(30) + ("%5.2f"%(row[2]/(sleep_time*unit)) + unit_name+"/s").rjust(20) \
+ ("%5.2f"%( row[3]/(sleep_time*unit) ) + unit_name+"/s").rjust(20)
print "\n* ", unit_name, "=", int(unit), "Bytes"
sleep( sleep_time );