Verify TRIM support with BtrFS on SSD
Solution 1:
So after many days working on this, I was able to demonstrate that BtrFS does use TRIM. I was unable to successfully have TRIM work on the server that we will be deploying these SSDs to. However, when testing using the same drive plugged into a laptop, the tests succeed.
Hardware used for all of this testing:
- Crucial m4 SSD 512GB
- HP DL160se G6
- LSI LSISAS9200-8e HBA
- generic SAS enclosure
- Dell XPS m1210 laptop
After many failed attempts at verifying BtrFS on the server, I decided to try this same test using an old laptop (remove the RAID card layer). The initial attempts of this test using both Ext4 and BtrFS on the laptop fail (data not TRIM'd).
I then upgraded the SSD drive firmware from version 0001 (as shipped out of the box) to version 0009. The tests were repeated with Ext4 and BtrFS and both filesystems successfully TRIM'd the data.
To ensure the TRIM command had time to run, I did a rm /mnt/testfile && sync && sleep 120
before performing validation.
One thing to note if you're attempting this same test: SSDs have erase blocks that they operate on (I don't know the size of the Crucial m4 erase blocks). When the file system sends the TRIM command to the drive, the drive will only erase a complete block; if the TRIM command is specified for a portion of a block, that block will not be TRIM'd due to the remaining valid data within the erase block.
So to demonstrate what I'm talking about (output of the sectors.pl
script above). This is with the test file on the SSD. Periods are sectors that only contain zeros. Pluses have one or more non-zero bytes.
Test file on drive:
24600 .......................................+++++++++++
24650 ++++++++++++++++++++++++++++++++++++++++++++++++++
24700 ++++++++++++++++++++++++++++++++++++++++++++++++++
-- cut --
34750 ++++++++++++++++++++++++++++++++++++++++++++++++++
34800 ++++++++++++++++++++++++++++++++++++++++++++++++++
34850 +++++++++++++++++++++++++++++.....................
Test file deleted from drive (after a sync && sleep 120
):
24600 .......................................+..........
24650 ..................................................
24700 ..................................................
-- cut --
34750 ..................................................
34800 ..................................................
34850 ......................+++++++.....................
It appears that the first and last sectors of the file are within a different erase blocks from the rest of the file. Therefore some sectors were left untouched.
A takeaway form this: some Ext4 TRIM testing instructions ask the user to only verify that the first sector was TRIM'd from the file. The tester should view a larger portion of the test file to really see if the TRIM was successful or not.
Now to figure out why manually issued TRIM commands sent to the SSD through the RAID card work but automatic TRIM commands to not...
Solution 2:
Based on what I've read, there may be a flaw in your methodology.
You are assuming that TRIM will result in your SSD zeroing the blocks which have been deleted. However this is often not the case.
That is only if the SSD implements TRIM so that it zeroes the discarded blocks. You can check if the device at least knows enough to report discard_zeroes_data:
cat /sys/block/sda/queue/discard_zeroes_data
Also, even if the SSD does zero, it may take some time -- well after the discard has completed -- for the SSD to actually zero the blocks (this is true of some lesser quality SSDs).
http://www.redhat.com/archives/linux-lvm/2011-April/msg00048.html
BTW I was looking for a reliable way to verify TRIM and haven't found one yet. I'd love know to if anyone finds a way.