Fstrim and iostat

iossdtrim

I have a new samsung ssd in my new notebook. I tuned up everything to avoid writings in the disk (firefox profiles and /var/log in memory), I'm using a ext4 fs with relatime and I use fstrim once a day to TRIM my ssd.

I monitor disk load with iostat -dhm. Before running fstrim the command iostat shown

Device:            tps    MB_read/s    MB_wrtn/s    MB_read    MB_wrtn
sda
              4.97         0.05         0.59       1856      20924

after trimming /home

# fstrim -v /home
/home: 100.2 GiB (107555401728 bytes) trimmed

ìostat shows

Device:            tps    MB_read/s    MB_wrtn/s    MB_read    MB_wrtn
sda
              5.91         0.05         3.51       1859     123498

MB_wrtn increased 102574 MB.
Does that mean fstrim is writing 102574 MB = 100.2 GB to my disk ?

Trim is supposed to "mark" the pages that do not contain valid data, not writing them.

I guess iostat saw the operation made by fstrim as writing (even if it is not a writing). What do you think? Is there a better way to monitor my disk?

Note: Using iotop is not an answer, because it doesn't show the amount of data of processes that were killed. Thus, iotop measurement
is inexact.

Best Answer

MB_wrtn increased 102574 MB. Does that mean fstrim is writing 102574 MB = 100.2 GB to my disk ?

Yes, it means exactly that. fstrim essentially looks at every single block of your block device and decides if the block is being used by the filesystem (that is, contains important data):

  • block is in use: ignore it.
  • block is not in use: send an ATA TRIM command for this block to the underlying block device.

When it's done, the amount of trimmed blocks should always be equivalent to the free space on the filesystem. fstrim TRIMs each free block every time you run it, regardless if it already has been TRIMmed in the past, so it won't become faster if you run it more often.

The kernel probably considers the TRIM command like a write command, so every executed TRIM command counts as a 4k write command.

Trim is supposed to "mark" the pages that do not contain valid data, not writing them.

"Marking" blocks as trimmed is equivalent to "writing" them, because their contents change. If you'd accidentally TRIM the wrong block, you would lose data - just as writing zeros to the wrong block causes you to lose data.

Related Question