Measuring 'total bytes written' under Linux

Solution 1:

Another possibility is to look at /proc/diskstats. It's not persistent across reboots, but it has data for every block device. Probably most interesting to you is field 10, which contains the total number of sectors written. On a system with scsi disks with a sector size of 512 bytes, you could run

awk '/sd/ {print $3"\t"$10 / 2 / 1024}' /proc/diskstats

to see how many megabytes were written to each device. The output will look like

sda 728.759
sda1 79.0908
sda2 649.668

Solution 2:

I was struggling with the same problem on my notebook, but as I reboot it pretty much on a daily basis, the accepted answer wasn't helpful. I have a Samsung mSATA SSD, which happens to have the SMART attribute #241 Total_LBAs_Written. According to the official documentation,

To calculate the total size (in Bytes), multiply the raw value of this attribute by 512B.

So the following command gives me the total TB written on my SSD disk (sdb)

sudo smartctl -A /dev/sdb | awk '/^241/ { print "TBW: "($10 * 512) * 1.0e-12, "TB" } '

As it also works on my HDD, I assume that it should work on pretty much every modern hard disk.


Solution 3:

You can see how much data has been written to an ext4 filesystem by looking in /sys/fs/ext4/$DEVICE/lifetime_write_kbytes.


Solution 4:

You can try iostat. It gives you statistics related to IO and CPU usage. Have a look at the manual man iostat.


Solution 5:

For my KINGSTON SEDC400S37480G, under Ubuntu Server 14.04, I use the following script to monitor TBW. I run it from cron once every day. The script outputs to a log file.

You need calc for it to work. Install calc using:

apt-get install apcalc

Script content:

echo "*** $(date -R) ***" >> /scripts/tbw/log.txt
tbw=$(cat /sys/fs/ext4/sda1/lifetime_write_kbytes)
TB=1000000000
echo TB written to sda1: >> /scripts/tbw/log.txt | calc $tbw / $TB >> /scripts/tbw/log.txt

Will output:

*** Thu, 20 Jul 2017 03:02:51 +0300 ***
TB written to sda1:
    3.062138442