Samsung SSDs: Reading Total Bytes Written Under Linux

Thanks to some CPP compensation, I now have a Samsung 840 EVO as the system drive in my work PC. I am hugely impressed with its performance, but soon became obsessed with trying to work out how long it would last (SSDs can only survive a limited number of writes, as you probably well know). Under Windows, it’s easy: you can get the total data written using Samsung’s own ‘Magician Software’. But under Linux? No. You’re stuffed.

At least, I thought I was. But I had a sniff around Samsung’s ‘SSD White Paper’, and found this titbit:

SMART Attributes

ID # 241 Total LBAs Written

Represents the total size of all LBAs (Logical Block Address) required for all of the write requests sent to the SSD from the OS. To calculate the total size (in Bytes), multiply the raw value of this attribute by 512B. Alternatively, users may simply consult the Total Bytes Written indicator in Magician 4.0.

Aha!

So for any other Linux-using Samsung SSD owners, here’s a bash one-liner for printing out the number of gigabytes written to your drive (NB: this assumes your SSD is ‘/dev/sda’ – change as required):

echo "GB Written: $(echo "scale=3; $(sudo /usr/sbin/smartctl -A /dev/sda | grep "Total_LBAs_Written" | awk '{print $10}') * 512 / 1073741824" | bc | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')"

Or if you prefer, download and use this lazy little script: samsung_ssd_get_lifetime_writes.bash

Again, you’ll need to change line 7 if your SSD isn’t ‘/dev/sda’, but otherwise it’s a simple:

$ chmod a+x samsung_ssd_get_lifetime_writes.bash
$ ./samsung_ssd_get_lifetime_writes.bash

…entering your root password when requested.

The script should output something like:

------------------------------
 SSD Status:   /dev/sda
------------------------------
 On time:      34 hr
------------------------------
 Data written:
           MB: 14,314.035
           GB: 13.978
           TB: .013
------------------------------
 Mean write rate:
        MB/hr: 421.001
------------------------------
 Drive health: 100 %
------------------------------

How nifty.

Note that the ‘Mean write rate’ is a lifetime value, and obviously skewed by the large amounts of data written during an initial OS install (it’ll settle down with age).

And now you too can watch your SSD slowly die…