29

As is common knowledge, SSDs have a limited number of PE (Program-Erase) cycles before the NAND cells die.

Therefore, it is very helpful to know how much data has been written to your SSD, in order to determine how much longer it will last before the NAND dies.

I have a Samsung 850 Pro 512GB SSD, and I am running Ubuntu 14.04.

How can I get the TBW (Total-Bytes-Written) for my drive?

1

4 Answers 4

30

FULL DISCLOSURE: The scipt/commands present in this answer are not my own, but rather the work of J. D. G. Leaver. They were sourced from a blog post on his website.


NB:

  • This will only report accurate numbers for Samsung SSDs.
  • You need to have smartctl installed, from the package smartmontools.

Method 1:

Here's a handy little script that will allow you to monitor the TBW of your SSD, along with some other information:

#!/bin/bash

#######################################
# Variables                           #
#######################################

SSD_DEVICE="/dev/sda"

ON_TIME_TAG="Power_On_Hours"
WEAR_COUNT_TAG="Wear_Leveling_Count"
LBAS_WRITTEN_TAG="Total_LBAs_Written"
LBA_SIZE=512 # Value in bytes

BYTES_PER_MB=1048576
BYTES_PER_GB=1073741824
BYTES_PER_TB=1099511627776

#######################################
# Get total data written...           #
#######################################

# Get SMART attributes
SMART_INFO=$(sudo /usr/sbin/smartctl -A "$SSD_DEVICE")

# Extract required attributes
ON_TIME=$(echo "$SMART_INFO" | grep "$ON_TIME_TAG" | awk '{print $10}')
WEAR_COUNT=$(echo "$SMART_INFO" | grep "$WEAR_COUNT_TAG" | awk '{print $4}' | sed 's/^0*//')
LBAS_WRITTEN=$(echo "$SMART_INFO" | grep "$LBAS_WRITTEN_TAG" | awk '{print $10}')

# Convert LBAs -> bytes
BYTES_WRITTEN=$(echo "$LBAS_WRITTEN * $LBA_SIZE" | bc)
MB_WRITTEN=$(echo "scale=3; $BYTES_WRITTEN / $BYTES_PER_MB" | bc)
GB_WRITTEN=$(echo "scale=3; $BYTES_WRITTEN / $BYTES_PER_GB" | bc)
TB_WRITTEN=$(echo "scale=3; $BYTES_WRITTEN / $BYTES_PER_TB" | bc)

# Output results...
echo "------------------------------"
echo " SSD Status:   $SSD_DEVICE"
echo "------------------------------"
echo " On time:      $(echo $ON_TIME | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta') hr"
echo "------------------------------"
echo " Data written:"
echo "           MB: $(echo $MB_WRITTEN | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')"
echo "           GB: $(echo $GB_WRITTEN | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')"
echo "           TB: $(echo $TB_WRITTEN | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')"
echo "------------------------------"
echo " Mean write rate:"
echo "        MB/hr: $(echo "scale=3; $MB_WRITTEN / $ON_TIME" | bc | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')"
echo "------------------------------"
echo " Drive health: ${WEAR_COUNT} %"
echo "------------------------------"

Here's a sample of the output:

------------------------------
 SSD Status:   /dev/sda
------------------------------
 On time:      2 hr
------------------------------
 Data written:
           MB: 25,098.917
           GB: 24.510
           TB: .023
------------------------------
 Mean write rate:
        MB/hr: 12,549.458
------------------------------
 Drive health: 100 %
------------------------------

This data is accurate, as I only just installed my new 850 Pro.


Method 2:

Alternatively, here's a one-liner to get the TBW only:

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')"
7
  • This doesn't work for my SAMSUNG MZ7LN512HMJP-000L7 SSD. smartctl reports Total_LBAs_Written to be 2268 and this produces 1.1 MB written -- clearly incorrect for a drive that's over a year old and is my primary system drive :(
    – Icydog
    Jul 22, 2018 at 18:40
  • @Icydog :( what version of Ubuntu are you running? Jul 22, 2018 at 19:21
  • I'm on Fedora 27. I think my SSD is probably just not reporting the numbers correctly.
    – Icydog
    Jul 23, 2018 at 20:33
  • My research shows that each SSD seems to have it's own multiplier for what each unit of "LBAs written" actually means. I don't have the numbers for the Samsung 850, but it's absolutely NOT just a sector...
    – ljwobker
    Mar 26, 2019 at 2:24
  • Doesn't work with WD ssd in ubuntu 18.04
    – acgbox
    Apr 4, 2020 at 16:46
7

How to find the information

We can use smartctl to find the value of TBW.

  1. Install smartctl with $ sudo apt install smartmontools

  2. Get sector size and LBA written with $ sudo smartctl -Ai /dev/sda in this case for device /dev/sda

  3. Some math: [sector size] * [LBA written] / 1024^3 = X GiB written so far

Example for device /dev/sda

$ sudo smartctl -Ai /dev/sda | grep -E 'Sector Size|Total_LBAs_Written'
Sector Size:      512 bytes logical/physical
241 Total_LBAs_Written      0x0032   099   099   000    Old_age   Always       -       1214641768

$ calc 1214641768*512/1024^3
    579.186328887939453125

In this case the sector size is 512 bytes, which is common, and total LBA written is 1214641768. The result is 579 GiB written so far. This makes sense since this drive is relatively new.

Command calc can be install with $ sudo apt install calc or use something else.

More info

The command $ sudo smartctl -A /dev/sda gives information on "vendor specific SMART Attributes" meaning what your drive shows may not be the same as this one. In the example I used a Samsung SSD which has the needed information.


Kingston SSD

Following what @JuliSmz said in their comment, and this PDF, I suspect Total_LBAs_Written returned by (at least some) Kingston SSDs is already in GB.

Look for id 241 on the last page of the PDF. It says:

Description: Indicates the number of bytes (in 1 GB resolution) written to the drive by a host system, over the life of the drive.

Rationale: This Attribute returns a byte count, in units of gigabytes at an update resolution of 1 GBytes. The count represents the number of bytes written. The Attribute reads 0 until the number of bytes written reaches 1GB; at additional GB the Attribute increments to a value of 1 (decimal).

Note: Client firmware older than 520ABBF0 and Enterprise firmware older than 510ABBF0 did not update resolution until 64GB of data was written to the drive.

3
  • 1
    In the case of the Kingston SSDs the division by 1024^3 trhows a bad result or SMART is bad... My disc has more than 2 years of full use (7196 hours) and LBA is 21161
    – JuliSmz
    Jun 1, 2021 at 14:56
  • 1
    @JuliSmz: I suspect that's already in GB. I found a Kingston's PDF about SMART attributes and it seems to corroborate this. Don't know if it applies to your drive or not. I'll update the answer with the extra info.
    – Daniel
    Jun 3, 2021 at 19:59
  • 1
    Thanks @daniel!
    – JuliSmz
    Jun 3, 2021 at 22:58
6

Crucial SSD Lifetime remaining

For Crucial SSD percentage lifetime remaining see

https://www.crucial.com/support/articles-faq-ssd/ssds-and-smart-data

This doc identifies 202 as Percent Lifetime Remaining. As an example on Ubuntu 16.04 (sudo smartctl /dev/sda1 -a)(part of smartmontools) reports 202 as unknown, but the value of 90 indicates 90% life remaining. On Ubuntu 20.04, the Lifetime Remaining is recognized and listed correctly

1
  • The link is a goner, but that's fine, i can live w/o it. The raw value says 45, so i guess that s 45% remaining, thanks! Dec 28, 2021 at 10:14
4

The accepted answer has bloated output, too much useless script-wizardry and hides initial parameter names from smartctl. Here is a better version;

#!/bin/bash

device=${1:-/dev/sda}
sudo smartctl -A $device |awk '
$0 ~ /Power_On_Hours/ { poh=$10; printf "%s / %d hours / %d days / %.2f years\n",  $2, $10, $10 / 24, $10 / 24 / 365.25 }
$0 ~ /Total_LBAs_Written/ {
    lbas=$10;
    bytes=$10 * 512;
    mb= bytes / 1024^2;
    gb= bytes / 1024^3;
    tb= bytes / 1024^4;
    printf "%s / %s  / %d mb / %.1f gb / %.3f tb\n", $2, $10, mb, gb, tb
    printf "mean writes per hour:  / %.2f",  mb/poh
}
$0 ~ /Airflow_Temperature_Cel/ { print $2 " / " $10}
$0 ~ /Wear_Leveling_Count/ { printf "%s / %d (%% health)\n", $2, int($4) }
' |
    sed -e 's:/:@:' |
    sed -e "s\$^\$$device @ \$" |
    column -ts@

sample output:

$ for i in /dev/sd{a,b,c,d}; do ssd-tbw $i;done   |sort -k2,2
/dev/sda    Airflow_Temperature_Cel    49
/dev/sdb    Airflow_Temperature_Cel    49
/dev/sdc    Airflow_Temperature_Cel    45
/dev/sdd    Airflow_Temperature_Cel    47
/dev/sda    mean writes per hour:      655.80
/dev/sdb    mean writes per hour:      646.97
/dev/sdc    mean writes per hour:      874.49
/dev/sdd    mean writes per hour:      733.95
/dev/sda    Power_On_Hours             27292 hours / 1137 days / 3.11 years
/dev/sdb    Power_On_Hours             27300 hours / 1137 days / 3.11 years
/dev/sdc    Power_On_Hours             14432 hours / 601 days / 1.65 years
/dev/sdd    Power_On_Hours             23255 hours / 968 days / 2.65 years
/dev/sda    Total_LBAs_Written         36655329806  / 17898110 mb / 17478.6 gb / 17.069 tb
/dev/sdb    Total_LBAs_Written         36172538301  / 17662372 mb / 17248.4 gb / 16.844 tb
/dev/sdc    Total_LBAs_Written         25846999325  / 12620605 mb / 12324.8 gb / 12.036 tb
/dev/sdd    Total_LBAs_Written         34955224738  / 17067980 mb / 16668.0 gb / 16.277 tb
/dev/sda    Wear_Leveling_Count        93 (% health)
/dev/sdb    Wear_Leveling_Count        93 (% health)
/dev/sdc    Wear_Leveling_Count        95 (% health)
/dev/sdd    Wear_Leveling_Count        94 (% health)

and the one-liner

$ sudo /usr/sbin/smartctl -A /dev/sda | 
     awk '$0~/LBAs/{ printf "TBW %.1f\n", $10 * 512 / 1024^4 }'
TBW 17.1
2
  • Doesn't work with WD ssd in ubuntu 18.04
    – acgbox
    Apr 4, 2020 at 16:46
  • Doesn't work is a very poor description. I just ran it again and it works. The distro should be irrelevant. Besides, the question is about Samasung. Did you specify the device correctly? what was the output when you ran the command? And you are probably on a laptop with nvme, for nvme drives all you need is smartctl -A Apr 7, 2020 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.