SSD: enable the TRIM function

When you’re using a SSD which does not support TRIM or it is not enabled by the OS, the write speed can drop dramatically after a while. With TRIM enabled the operating system tells the SSD which blocks are not used anymore because, for example, a file was deleted. This way, the SSD can cleanup during idle time.

Below I demonstrate how to detect if TRIM is working and how to enable it

First of all, make sure your SSD supports TRIM

  $> hdparm -I /dev/sda | grep TRIM 
     * Data Set Management TRIM supported (limit 1 block) 
     * Deterministic read data after TRIM

If you get more or less the same output, you’re fine. If not, you can try to upgrade the firmware of you SSD! Anyway, if TRIM is supported you can test if it is working as follows (not for btrfs):

  $> if=/dev/urandom of=tempfile count=10 bs=512k oflag=direct
   10+0 records in 
   10+0 records out 
   5242880 bytes (5.2 MB) copied, 3.26443 s, 1.6 MB/s 
  $> hdparm --fibmap tempfile 

  tempfile:
    filesystem blocksize 4096, begins at LBA 411648; assuming 512 byte sectors.
    byte_offset  begin_LBA    end_LBA    sectors
              0    8062976    8063999       1024
         524288    8078336    8079359       1024
        1048576    8212480    8220671       8192

Lets read the third block

  $>: hdparm --read-sector 8062979 /dev/sda

  /dev/sda:
  reading sector 8062979: succeeded
  7233 f2fd a002 201e 6fbf 38ca 43ae 2a4e
  2213 967a 8e9f c9f4 0e71 4652 35d7 91a6
  e5b9 85e1 263a 6bea 2b98 7d35 6eb4 8223
  2213 019c 95d6 ba2d f8a2 cf0f f10a 80c6
  ...

These numbers represent the random content of our tempfile. Now if we delete this file and run this command again it should only display zeros, if TRIM is working of course!!

  $> rm tempfil
  $> sync
  $> hdparm --read-sector 8062979 /dev/sda

  /dev/sda:
  reading sector 8062979: succeeded
  0000 0000 0000 0000 0000 0000 0000 0000
  0000 0000 0000 0000 0000 0000 0000 0000
  0000 0000 0000 0000 0000 0000 0000 0000
  0000 0000 0000 0000 0000 0000 0000 0000

You can inspect the first block too, but I’ve seen situations in which the first block wasn’t TRIMTed.

So, if all this didn’t work, you probably need to enable TRIM. Open up /etc/fstab and make sure you add the option ‘discard’. This is how my fstab file looks like

#
# /etc/fstab
# Created by anaconda on Wed Mar 28 11:43:34 2012
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=f76c8818-...ee836e6 /            ext4    defaults,noatime,discard        1 1
UUID=d60313d0-...cd71455 /boot        ext4    defaults,noatime,discard        1 2
UUID=8c408e1d-...c91a1e7 /home        btrfs   defaults,noatime,discard        1 2

This test is not working for btrfs filesystems, but I think that if it is working for ext4, you can very well assume it is working for your btrfs partitions too. If you still want to test this, check out this link. Below I’ve copied the steps needed:

 

  $> mount -t btrfs -o discard /dev/sdc5 /mnt
  $> dd if=/dev/urandom of=/mnt/tmp bs=1M count=5
  $> sync
  $> hexdump /mnt/tmp
  0000000 064c ded5 c386 4721 060c 13e8 b090 b4a0
  ...
  $> hexdump /dev/sdc5 | grep '064c ded5'
  0c00000 064c ded5 c386 4721 060c 13e8 b090 b4a0

Now verify the discard feature:

  $> rm /mnt/tmp
  $> sync
  $> echo 3 > /proc/sys/vm/drop_caches
  $> hexdump /dev/sdc5 | grep '064c ded5'

grep returns nothing, as expected.

I must say that I didn’t do any testing on btrfs, so I’m not sure if this is working at all!

One Response to SSD: enable the TRIM function

  1. Pingback: Stephen