12

On Linux, I want to know which process uses my disk I/O bandwith.

I know I can use iotop but I have a machine where, for various reasons, iotop cannot be installed.

How can I manually get that information? (How does iotop itself proceed?)

1 Answer 1

17

The file in /proc/<pid>/io represent what you need. It's a bit scripting work to get an output similar to iotop. See the linux kernel documentation to the /proc/<pid>/io file values:

rchar

I/O counter: chars read The number of bytes which this task has caused to be read from storage. This is simply the sum of bytes which this process passed to read() and pread(). It includes things like tty IO and it is unaffected by whether or not actual physical disk IO was required (the read might have been satisfied from pagecache)

wchar

I/O counter: chars written The number of bytes which this task has caused, or shall cause to be written to disk. Similar caveats apply here as with rchar.

[...]

read_bytes

I/O counter: bytes read Attempt to count the number of bytes which this process really did cause to be fetched from the storage layer. Done at the submit_bio() level, so it is accurate for block-backed filesystems.

write_bytes

I/O counter: bytes written Attempt to count the number of bytes which this process caused to be sent to the storage layer. This is done at page-dirtying time.

Now, you can use that small bash script:

#!/bin/bash

if [ "$(id -u)" -ne 0 ] ; then
  echo "Must be root" 2>&1
  exit 1
fi

delay=2
lista=$(for p in $(pgrep "."); do echo -n "$p "; grep ^rchar /proc/$p/io 2>/dev/null; done)

while :; do

  echo "-----"
  listb=$(for p in $(pgrep "."); do echo -n "$p "; grep ^rchar /proc/$p/io 2>/dev/null; done)

  echo "$lista" | while read -r pida xa bytesa; do
    [ -e "/proc/$pida" ] || continue
    echo -en "$pida:\t"
    bytesb=$(echo "$listb" | awk -v pid=$pida '$1==pid{print $3}')
    echo "$((($bytesb - $bytesa) / $delay)) b/s"
  done | sort -nk2 | tail
  sleep $delay
  listb=$lista
done

It creates two lists with a delay of 2 seconds ($delay: can be in- or decreased) between them, and then compares the lists and calculates the differences. The 10 process with the most I/O are printed with their I/O bandwith in the last 2 seconds. If you want write I/O instead of read I/O just edit the grep command in the lists to wchar instead of rchar:

lista=$(for p in $(pgrep "."); do echo -n "$p "; grep ^wchar /proc/$p/io 2>/dev/null; done)
listb=$(for p in $(pgrep "."); do echo -n "$p "; grep ^wchar /proc/$p/io 2>/dev/null; done)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .