As you are most likely aware, SSDs (or Solid State Disks) are capable of achieving much faster read and write speeds of data compared to traditional hard disks. What you are possibly not aware of is that over time SSDs can lose some of that speed when it comes to data writes as the disk fills up. If you are running SSDs in your server in the interests of speed, here’s how you can use TRIM to keep the SSD running at its best.
Why Do SSDs Slow Down?
First let’s look at why this problem occurs. This is to do with how SSDs write data to the storage. SSDs store data in fixed-size chunks, called pages. These pages are then arranged in larger groups called blocks. While the SSD can read and write to pages individually, they can only erase blocks of data rather than the individual pages. Unlike a hard drive which can overwrite blocks of data without any issues, SSDs need to have the data in a block erased before fresh data can be written to the pages inside. This becomes a problem as operating systems and file systems are designed such that if a file is deleted the blocks the file used are flagged as available to be written over for the file system, but the data in those blocks is left in place until new data is written over the top. This is the principle that undelete and file recovery tools make use of to rescue deleted files from a disk. Unfortunately, as an SSD fills up this can turn writing data into a two-step process for an SSD which can have a heavy impact on write performance.
How To Schedule TRIM To Erase SSD Data Blocks
This is where TRIM comes in. TRIM is a command built into the ATA command set for SSDs that is part of how the disk interfaces with the computer. The operating system is able to send TRIM commands to the disk to let it know which blocks are part of deleted files, and allow the SSD to erase the blocks in advance of needing to write to them. While the operating system is capable of signaling the drive to erase these sections every time they delete a file on the file system, this can also have an impact on performance and slow things down. So the recommendation is to run TRIM on a schedule to clear the blocks intermittently.
To activate the TRIM function you need to use the fstrim command at the command line. It needs to be run with root privileges to work and should be already installed. You can test it with the following command:
sudo fstrim -a -v
The -a flag tells fstrim to check all available valid partitions and the -v flag provides a verbose output showing you what fstrim has done. You should see the output from the command to see how things went and if you get a positive output you can then look to adding the command to your cron entries. Note that for Ubuntu systems from 14.10 onwards fstrim is already configured to run for you and you should find a script for scheduling at /etc/cron.weekly/fstrim that activates it for you.
If that file doesn’t exist on your system then you will need to create it with the following command:
sudo nano /etc/cron.weekly/fstrim
Then paste in the following code:
/sbin/fstrim -a || true
Save and exit the file, then make it executable with the following command:
sudo chmod +x /etc/cron.weekly/fstrim
The fstrim command will now be run on a weekly basis by cron on your system. If your system has lots of file deletions taking place then it may be that you will see more benefit by running the fstrim command daily. To do this, move the file from the /etc/cron.weekly directory to the /etc/cron.daily directory.