devutils/ATFQ/book ATFQ git

Part I · How it works

4.2 Inside an SSD: flash, TRIM and wear

alsotrimfstrimdiscardssd wearssd healthtbwwrite amplificationslc cacheqlctlcnandsmartctlsmartnvme smart-log

TL;DR

  • Flash can't overwrite in place: it writes pages of a few KiB, but only erases whole blocks of many pages. The SSD's controller writes new data elsewhere and cleans up old blocks in the background.
  • TRIM tells the SSD which blocks the filesystem freed, so it doesn't copy dead data around. Run it regularly: fstrim.timer does it weekly.
  • Each cell survives a limited number of erase cycles. Drives are rated in terabytes written (TBW); health tools say how much is used.
systemctl status fstrim.timer
sudo smartctl -a /dev/nvme0           # look for "Percentage Used"

On your machine

alice@laptopexample
$ atfq 4.2
  nvme0n1   Samsung SSD 980 PRO 500GB: NVMe flash, 476.9 GiB · scheduler
            none · 8 hardware queues · volatile write cache · TRIM
  sda       USB DISK 3.0: USB drive, 30.0 GiB · scheduler mq-deadline · no
            volatile write cache

An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.

Pitfalls

Deep dive 2 questions

What does the controller do?

It runs a flash translation layer: a map from the block numbers the system uses to the flash pages that hold them. A rewrite goes to a fresh page and the old one is marked stale. Garbage collection later copies the live pages out of a mostly stale block and erases it. Those extra copies are write amplification; TRIM and free space reduce them. The controller also spreads writes evenly over all cells (wear levelling).

Continuous discard or periodic TRIM?

The discard mount option sends a TRIM on every delete, which can slow heavy deletes on some drives. A periodic fstrim batches them, and is what most distros set up. Btrfs, since Linux 6.2, uses an asynchronous discard by default on SSDs, which batches them itself.

Commands
fstrim smartctl nvme systemctl
Paths
/sys/block/DEV/queue/discard_max_bytes
See also
14. Disk
4.1 Hard drives, SSDs and NVMe
13.5 fstab and mount options