Part 6 of 7 · 3 min read
TRIM
TRIM and what the OS can do
Think of a landlord who does not know which tenants have left. The landlord keeps their rooms reserved and cannot rent them out. A note from the tenant frees the rooms. The analogy stops in one place: a tenant knows when the lease ends, and a file system must be careful that the note reaches the drive before the space is reused.
In plain words: When a file is deleted, the file system marks its blocks as free, but the drive still thinks those logical pages hold valid data. It will copy them during garbage collection for no reason. The TRIM command (called discard in Linux) lets the OS tell the drive which logical blocks are unused. The drive then marks them stale and does not copy them. This lowers write amplification and keeps the drive faster. Linux can send TRIM at deletion with the discard mount option, or in batches with the fstrim command that a timer usually runs each week.
A worked example. The test machine reports its discard settings in /sys and in lsblk.
$ cat /sys/block/vda/queue/discard_granularity
4096
$ cat /sys/block/vda/queue/discard_max_bytes
1073741824
$ lsblk -D /dev/vda
NAME DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
vda 0 4K 1G 0
The disk accepts discard requests of 4 KiB granularity, and up to 1 GiB per request. If 20% of the logical space on a drive is trimmed, the drive behaves as if it had that much more spare space. In the simulator, 10% spare with 20% trimmed acts as about 28% spare: write amplification near 2.11 instead of 6.62.
The key lines. Rewriting old data in place on a hard disk costs nothing extra. On flash it costs write amplification, so a file system built for flash avoids small in-place updates and tells the drive about free space.
TRIM (discard)
TRIM, called discard in Linux, is a command that the OS sends to a storage device to say that a range of logical blocks no longer holds useful data. A flash drive can then treat those pages as stale and skip copying them during garbage collection.
Example: after deleting a 4 GB file, fstrim -v / tells the drive that the 4 GB of blocks are free.
Interview angle
A common question is "Why is an SSD slower after it has been full for a while?" A good answer says that the drive has little spare space, so garbage collection copies many valid pages, which raises write amplification and delays requests. TRIM and spare space help.
Check yourself: 1. Why does the drive not know that a deleted file's pages are free? 2. What are the two ways of sending TRIM on Linux? 3. Why does TRIM act like extra spare space?
Words you will meet
| Word | Meaning |
|---|---|
| Page | Smallest read and program unit of flash. |
| Block | Smallest erase unit. |
| FTL | Firmware that maps logical pages to physical pages. |
| Garbage collection | Copying valid pages out of a block so it can be erased. |
| Write amplification | Flash writes divided by host writes. |
| TRIM | A command that tells the drive that blocks are unused. |
Quick notes
- Flash is read and programmed by page and erased by block.
- A used page cannot be rewritten before its block is erased, so the FTL writes updates elsewhere.
- Garbage collection copies valid pages. Write amplification = flash writes / host writes = 1 / (1 - u).
- More spare space gives lower write amplification.
- Each block survives a limited number of erase cycles. Wear leveling spreads them.
- TRIM tells the drive that blocks are free. It acts as extra spare space.