The number of MB read and written since system start

It may be interesting how much data you write to the disk per day. System administrators can use this information to think about resource efficiency.

Recently, I discovered that in a day I have a lot of writing on the disc. But I just surf the Internet and especially don’t download anything. I managed to find out that Firefox browser stores session cache on disk and not in RAM. I read some information on the Internet and moved the sessions cache to RAM. Now my problem is solved.

In this article we will take a trip to /sys/block directory. It provides some information of the block devices of your computer. We will learn how to get how many megabytes read and written to disk at the current moment.

About /sys/block

As mentioned above, this interface provides access to information about the block devices of the computer.

Let’s see it:

ls -l /sys/block
        fd0 -> ../devices/platform/floppy.0/block/fd0
	sda -> ../devices/pci0000:00/0000:00:1f.2/ata4/host3/target3:0:0/3:0:0:0/block/sda
	sdb -> ../devices/pci0000:00/0000:00:1f.2/ata4/host3/target3:0:1/3:0:1:0/block/sdb

Here sda is my HDD and sdb is SSD. These are symbolic links to /sys/devices directories. We can use /sys/block for a short way.

You may have more or less devices. We can get a list of devices and their model names using the following command:

	fd0   disk     4K 
	sda   disk 465.8G WDC WD5000AAKS-0
	sda1  part 258.8G 
	sda2  part  99.7G 
	sda3  part 107.3G 
	sdb   disk 223.6G WDC WDS240G2G0A-
	sdb1  part   150G 

Get the number of sectors and convert them to bytes

Suppose we choose /dev/sdb. Make the following command:

cat /sys/block/sdb/stat
	24330     3545  1370730      748     4633     6248   142160     1212        0    11256    11256

This file provides some statistics on kernel I / O performance. We are only interested in the third and seventh columns. The third column is the number of read sectors that were read successfully. The seventh column is the number of sectors written successfully.

What is sector? Sector is a minimum addressable information storage unit. In simple terms, this is the minimum amount of information that can be written and read from disk. On old disks the (PHYSICAL) sector size is 512 bytes, but on new drives this value may be greater (2048 bytes PHYSICAL).

But in Linux, the system and applications use the LOGICAL size of the sector. It is 512 bytes. We can notice this by the following command:

sudo fdisk -l /dev/sdb
	Disk /dev/sdb: 223.6 GiB, 240065183744 bytes, 468877312 sectors
	Disk model: WDC WDS240G2G0A-
	Units: sectors of 1 * 512 = 512 bytes
	Sector size (logical/physical): 512 bytes / 512 bytes
	I/O size (minimum/optimal): 512 bytes / 512 bytes

Notice the sector physical size is the same on my disk.

Now everything is clear. We just need to use mathematics to count It in megabytes. Assume we want to get the number of sectors read. It is possible to use awk command to grab the third column:

echo "$(awk '{print $3}' /sys/block/sdb/stat)"

Now it can be counted. The sector logical size is 512 bytes. If we multiply 1985434 by 512, we get the number of bytes that were read from the disk. And if we divide the resulting number by 1024, we get the size in kilobytes, we divide it again by 1024 and we get the size in megabytes.

1985434 * 512 / 1024 / 1024 = 969 megabytes were read from /dev/sdb

It is even possible to get this statistics from a disk partition:

cat /sys/block/sdb/sdb1/stat
	28296     4211  1988378      608     6505     8610   748032     3716        0    15208    15208

Assume we want to get the number of bytes that were written to /dev/sdb/sdb1 in one command (we need to use the seventh column to grab sectors written info). Use the following command in your terminal:

echo "$(expr $(awk '{print $7}' /sys/block/sdb/sdb1/stat) \* 512 / 1024 / 1024)M WRITTEN to /dev/sdb/sdb1"
	365M WRITTEN to /dev/sdb/sdb1

Bash script example

So, this is how it works. With that commands we just need to change the columns and device path. Here is the script:


echo "$(expr $(awk '{print $3}' /sys/block/sda/stat) \* 512 / 1024 / 1024)M READ from /dev/sda"
echo "$(expr $(awk '{print $7}' /sys/block/sda/stat) \* 512 / 1024 / 1024)M WRITTEN to /dev/sda"
echo "___________________________________"
echo "$(expr $(awk '{print $3}' /sys/block/sdb/stat) \* 512 / 1024 / 1024)M READ from /dev/sdb"
echo "$(expr $(awk '{print $7}' /sys/block/sdb/stat) \* 512 / 1024 / 1024)M WRITTEN to /dev/sdb"

The result is:

	79M READ from /dev/sda
	1M WRITTEN to /dev/sda
	972M READ from /dev/sdb
	368M WRITTEN to /dev/sdb

Leave a Reply

Your email address will not be published.