TRIM your SSD

About a year ago SSD prices had fallen to around €300 for a 500 GB drive and I couldn't resist any longer to replace my notebook hard drive for an SSD. The speed-up has been tremendous and I haven't regretted it since. Just for the fun of it I've been keeping a close look at how much data gets written to the SSD and even with a huge safty margin, the 5 GB of data that gets written to the SSD per day with my usage pattern I will not wear out my SSD for at least 30 years. Always ready to optimize, however, I noticed recently, that TRIMing was not activated for the drive.

As the Flash cells contained in an SSD drive can only be rewritten a couple of thousand times, SSDs have sophisticated algorithms to ensure write operations are distributed evenly over the complete drive. That requires that sometimes data of static files that never change is relocated to another part of the drive so those little used Flash cells can be used for something else. This is known as 'wear leveling'.

When a file is deleted, the blocks are marked as empty in the file system. Unfortunately, the SSD knows nothing of file systems as this is a higher layer concepts and SSDs only deal with blocks. That means that once a block is written to the SSD doesn't know whether it contains valid data or whether the block contains to a file that was deleted. In both cases the wear leveling algorithm has to copy the data somewhere else before the block can be reused for other data. This is obviously sub-optimal as a block that contains data of a previously deleted file doesn't have to be copied before it is overwritten.

This is where the TRIM command comes in. When a file is deleted, the file system first deletes the file and marks the blocks the file used as empty. That's standard procedure. But then, in addition, it sends a TRIM command to the SSD informing it that the block is empty. The SSD can then also mark the block as empty and doesn't have to bother copying the useless data it now contains when that block is due for wear leveling replacement.

By default, Linux has TRIM disabled but it's pretty simple to activate it by putting the "discard" option in /etc/fstab for the partition that resides on the SDD. Here's an example:

UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /  ext4    discard,errors=remount-ro 0       1

After a reboot TRIM should be activated. This post explains how to make sure it is active. And for more details on TRIM here's the Wikipedia entry!

 

2 thoughts on “TRIM your SSD”

Comments are closed.