Gentoo Archives: gentoo-user

From: Rich Freeman <rich0@g.o>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Understanding fstrim...
Date: Mon, 13 Apr 2020 11:39:32
Message-Id: CAGfcS_=jRVLGEGOdps2cCYWcPrGcpsD7JJ49kpuHqwK6-NsvcQ@mail.gmail.com
In Reply to: [gentoo-user] Understanding fstrim... by tuxic@posteo.de
1 On Mon, Apr 13, 2020 at 1:32 AM <tuxic@××××××.de> wrote:
2 >
3 > fstrim clears blocks, which currently are not in use and which
4 > contents is != 0.
5 >...
6 > BUT: Clearing blocks is an action, which includes writes to the cells of
7 > the SSD.
8
9 I see a whole bunch of discussion, but it seems like many here don't
10 actually understand what fstrim actually does.
11
12 It doesn't "clear" anything, and it doesn't care what the contents of
13 a block are. It doesn't write to the cells of the SSD per se.
14
15 It issues the TRIM command to the drive for any unused blocks (or a
16 subset of them if you use non-default options). It doesn't care what
17 the contents of the blocks are when it does so - it shouldn't even try
18 to read the blocks to know what their content is.
19
20 Trimming a block won't clear it at all. It doesn't write to the cells
21 of the SSD either - at least not the ones being trimmed. It just
22 tells the drive controller that the blocks are no longer in use.
23
24 Now, the drive controller needs to keep track of which blocks are in
25 use (which it does whether you use fstrim or not), and that data is
26 probably stored in some kind of flash so that it is persistent, but
27 presumably that is managed in such a manner that it is unlikely to
28 fail before the rest of the drive fails.
29
30 On a well-implemented drive trimming actually REDUCES writes. When
31 you trim a block the drive controller will stop trying to preserve its
32 contents. If you don't trim it, then the controller will preserve its
33 contents. Preserving the content of unused blocks necessarily
34 involves more writing to the drive than just treating as
35 zero-fillled/etc.
36
37 Now, where you're probably getting at the concept of clearing and
38 zeroing data is that if you try to read a trimmed block the drive
39 controller probably won't even bother to read the block from the ssd
40 and will just return zeros. Those zeros were never written to flash -
41 they're just like a zero-filled file in a filesystem. If you write a
42 bazillion zeros to a file on ext4 it will just record in the
43 filesystem data that you have a bunch of blocks of zero and it won't
44 allocate any actual space on disk - reading that file requires no
45 reading of the actual disk beyond the metadata because they're not
46 stored in actual extents. Indeed blocks are more of a virtual thing
47 on an SSD (or even hard drive these days), so if a logical block isn't
48 mapped to a physical storage area there isn't anything to read in the
49 first place.
50
51 However, when you trimmed the file the drive didn't go find some area
52 of flash and fill it with zeros. It just marked it as unused or
53 removed its logical mapping to physical storage.
54
55 In theory you should be able to use discard or trim the filesystem
56 every 5 minutes with no negative effects at all. In theory. However,
57 many controllers (especially old ones) aren't well-implemented and may
58 not handle this efficiently. A trim operation is still an operation
59 the controller has to deal with, and so deferring it to a time when
60 the drive is idle could improve performance, especially for drives
61 that don't do a lot of writes. If a drive has a really lousy
62 controller then trims might cause its stupid firmware to do stupid
63 things. However, this isn't really anything intrinsic to the concept
64 of trimming.
65
66 Fundamentally trimming is just giving the drive more information about
67 the importance of the data it is storing. Just about any filesystem
68 benefits from having more information about what it is storing if it
69 is well-implemented. In a perfect world we'd just enable discard on
70 our mounts and be done with it.
71
72 I'd probably just look up the recommendations for your particular
73 drive around trimming and follow those. Somebody may have benchmarked
74 it to determine how brain-dead it is. If you bought a more name-brand
75 SSD you're probably more likely to benefit from more frequent
76 trimming.
77
78 I'm personally using zfs which didn't support trim/discard until very
79 recently, and I'm not on 0.8 yet, so for me it is a bit of a moot
80 point. I plan to enable it once I can do so.
81
82 --
83 Rich

Replies

Subject Author
Re: [gentoo-user] Understanding fstrim... Michael <confabulate@××××××××.com>