Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a way to view IO stats based on process in Ubuntu. I have an ubuntu server 10.10 that every once and while will grind on the hdd's for 30 seconds, I'm trying to figure out what is causing that. Thanks in advance. All I'm running on it is mysql however it happens even when mysql is not under any load (no connections just idle).

share|improve this question

2 Answers 2

up vote 3 down vote accepted

I use iotop Install iotop (command line tool).

share|improve this answer

iotop is doing the job most of the time. However, I found the following python script to be more useful:

(it shows only the currently active processes)

#!/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 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.