Display the I/O Usage of a Process

2014-02-21

The proc file system of Linux kernels contains a file listing how many read/write operations in number of syscalls a process has executed. The file which is located at /proc/PID/io (replace PID with the actual process ID) also displays how many bytes a process has read/written. This is, for example, useful to monitor the network usage of a process. IFCONFIG(8) and other tools just show aggregate statistics per network interface or estimate the current bandwidth usage per process, but do not display the overall network usage of a process since the beginning of its lifetime. The file /proc/PID/io provides aggregate statistics for all kinds of I/O including socket I/O, disk I/O, etc.

Here is a simple test in which data is transferred between a netcat client and netcat server. The server can be started with the command nc -k -l localhost 1234. The next commands create a 10 MB file containing random data (the quality of the randomness is potentially low) which is fed into the netcat client that sends the data to the server.

dd if=/dev/urandom bs=$((1000 * 1000)) count=10 of=urandom_10MB.bin
nc localhost 1234 < urandom_10MB.bin

The /proc/PID/io file where PID is the process ID of the netcat server process, may look like:

rchar: 10005358
wchar: 10000000
syscr: 9791
syscw: 9766
read_bytes: 0
write_bytes: 0
cancelled_write_bytes: 0

Unfortunately, the man page PROC(5) is outdated and does not explain what the terms above mean. However, the Linux kernel comes with a /proc/PID/io documentation.