CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 3 of 7 · 2 min read

The counters

Think of a car's trip meter. It only ever counts up. To learn the speed you read it twice and divide the difference by the time between. The analogy stops in one place: a trip meter counts one thing, while the kernel keeps more than a dozen counters for each disk.

In plain words: The kernel keeps running counters for each block device in /proc/diskstats. After the device numbers and the name come fields such as: reads completed, reads merged, sectors read, milliseconds spent reading, then the same four for writes, then the number of I/Os in progress, the milliseconds in which any I/O was in progress (io_ticks), and a weighted sum of the time. A sector here is always 512 bytes, whatever the real sector size. The counters only grow, so a rate always needs two samples and the time between them.

A worked example. One line of the test machine is 254 0 vda 146968 5491 19538474 100707 300 98 4192 2038 0 55356 102759 .... The device is vda. It had finished 146,968 reads since boot, and 19,538,474 sectors had been read, which is 19,538,474 x 512 = 10.0 GB. Reading used 100,707 ms in total, so the average read took 100,707 / 146,968 = 0.69 ms. That is an average over the whole life of the machine, and a sample over two seconds shows a different figure.

The key lines. To get a rate, take the difference of a counter between two samples and divide it by the time. Divide the milliseconds spent by the number of requests for the average latency. Divide the io_ticks difference by the elapsed milliseconds for the busy share.

Disk counters

The disk counters of Linux are monotonically growing numbers in /proc/diskstats and /sys/block/<dev>/stat that count requests, sectors and milliseconds for each block device. Rates and averages come from the difference between two readings.

Example: a rise of 59,370 in the reads-completed counter over 2 seconds is 29,685 reads per second.

Check yourself: 1. Why can one reading of /proc/diskstats not give a rate? 2. How many bytes is one sector in the counters? 3. What does the io_ticks field tell you?