Linux – Why don’t reads from /dev/zero count as IO_RBYTES

devicesiolinuxnull

I am emptying out a hard drive on some Linux 4.x OS using this command:

sudo sh -c 'pv -pterb /dev/zero > /dev/sda'

And I opened another tty and started sudo htop and noticed this:

  PID USER      PRI  NI CPU%   RES   SHR   IO_RBYTES   IO_WBYTES S   TIME+  Command
 4598 root       20   0 15.5  1820  1596        4096    17223823 D  1:14.11 pv -pterb /dev/zero

The value for IO_WBYTES seems quite normal, but IO_RBYTES remains at 4 KiB and never changes.

I ran a few other programs, for example

dd if=/dev/zero of=/dev/zero
cat /dev/zero > /dev/zero

and was surprised to see none of them generates a lot of IO_RBYTES or IO_WBYTES.

I think this is not specific to any program, but why don't reads from /dev/zero and writes to /dev/{zero,null} count as I/O bytes?

Best Answer

They do count as I/O, but not of the type measured by the fields you’re looking at.

In htop, IO_RBYTES and IO_WBYTES show the read_bytes and write_bytes fields from /proc/<pid>/io, and those fields measure bytes which go through the block layer. /dev/zero doesn’t involve the block layer, so reads from it don’t show up there.

To see I/O from /dev/zero, you need to look at the rchar and wchar fields in /proc/<pid>/io, which show up in htop as RCHAR and WCHAR:

rchar: characters 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(2) and similar system calls. It includes things such as terminal I/O and is unaffected by whether or not actual physical disk I/O was required (the read might have been satisfied from pagecache).

wchar: characters 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.

See man 5 proc and man 1 htop for details.

Related Question