Measuring disk I/O usage of a program

iomeasuretime

time is a brilliant command if you want to figure out how much CPU time a given command takes.

I am looking for something similar that can measure the disk I/O of the program and any children. Preferably it should distinguish between I/O that was cached (and thus did not cause the disk to spin) and I/O that was not cached.

So I would like to do:

iomeassure my_program my_args

and get output similar to:

Cached read: 10233303 Bytes
Cached write: 33303 Bytes  # This was probably a tmp file that was erased before making it to the disk
Non-cached read: 200002020 Bytes
Non-cached write: 202020 Bytes

I have looked at vmstat, iostat, and sar, but none of these are looking at a single process. Instead they look at the whole system.

I have looked at iotop, but that only gives me a view this instant.

— edit —

snap's answer seems close.

'File system inputs:' is the non-cached reads in 512-byte blocks.

'File system outputs:' is the cached writes in 512-byte blocks.

You can force the cache empty with:

sync ; echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null

I tested with:

 seq 10000000 > seq
 /usr/bin/time -v bash -c 'perl -e "open(G,\">f\"); print G <>;close G; unlink \"f\";" seq'

Best Answer

You did not specify which operating system you use.

Linux

Instead of using time foo which is (usually) a shell built-in you could try the external command /usr/bin/time foo. It gives some additional information such as number of file system inputs and outputs (but no information about cache hits or byte amounts). See man time and man getrusage for further instructions.

Note that this feature requires Linux kernel version 2.6.22 or newer.

FreeBSD

Use /usr/bin/time -l foo. It gives the number of inputs and outputs. See man time and man getrusage for further instructions.

Related Question