Opening Trim Instruction for SSD Solid-State Hard Disk on MacOS

Time:2019-9-24

Solid-state hard disk (SSD) breaks through the speed bottleneck of traditional hard disk, and works stably, without noise and consumes less power. With the decrease of cost, SSD will replace traditional hard disk greatly. Most people who use SSD have heard of TRIM. Operating system must support TRIM to optimize the performance of SSD. So what is TRIM? Why does SSD need TRIM instead of traditional hard disk?

The emergence of TRIM is actually due to the difference in principle between SSD and traditional hard disk in data deletion and coverage operations. We know that storage devices (whether SSDs or traditional hard drives) are only responsible for the bottom physical storage, and they don’t know what the stored data really means. Anyway, I can read or write whatever data the operating system allows me to do, and organize the data in physical devices into directories, files and give them meaning. File System is responsible for the work. File system is part of the operating system, which is managed by the operating system. Different operating systems support different file systems, such as older FAT16/32, Windows NTFS, OS X HFS+, Linux ext (ext2/3/4), and so on.

So why is the problem with data deletion and coverage? Let’s see how the operating system handles deleting files. First, a file is stored in multiple data blocks on a physical device. These blocks can be discontinuous. There will be a table in the file system (such as the file allocation table in FAT) to manage the corresponding storage location of each file’s data. Then, when deleting a file, the operating system only needs to mark the corresponding data block of the file as “idle” in the file system, and there is no need to actually clean up the data stored in the data block.

Result: The storage device only knows where the data is stored, but it doesn’t know whether the data is useful (because the data may actually remain in the data block after deletion of the file). Only the operating system knows whether the data is useful.

When we need to store new files, those blocks that have been marked as “idle” are used as empty blocks (although they are not actually empty inside), which are exactly the same as empty blocks that have no content at all for the operating system. But such a design must be based on a premise:

For physical storage devices, “writing blank data blocks” and “covering existing data blocks” require exactly the same operations.

The above premise is completely valid for the traditional hard disk. The traditional hard disk works in the same way as tape. The recording of data is based on the direction of magnetization of a recording unit on the medium. That is to say, when writing data, the head only needs to magnetize the designated recording unit (sector) into the required state, and it does not need to care about the original state of the unit.

However, the above premises are not valid on SSD. Why? Because in SSD, only empty data blocks can directly perform write operations; for non-empty data blocks, erase operations need to be performed before writing.

At first glance, the difference between SSD and traditional hard disk is just one more step of erasing operation, but in fact it is not so, and even worse still lies ahead. In SSD, the smallest unit of data storage is page. The size of a page is usually 4KB. Several pages are grouped into blocks, and the size of a block is 512KB. Due to hardware limitations, SSD reads and writes a page separately, but erase can only be done on the whole block, that is to say, once erased, the whole block must be erased at one time. Think about what SSD needs to do if the operating system wants SSD to rewrite data on a page.

The entire block of data in which the target page will be rewritten is read into the cache.
Modify the data of the target page in the cache.
Erase the entire block.
Rewrite the data in the cache to the whole block.

This means that if I want to modify a 4KB page, I have to toss the whole 512KB block over. You can imagine what huge performance and lifetime losses this will bring.

For these reasons, a TRIM command is provided in SSD. When deleting a file, the operating system can send TRIM command to SSD to tell it which data blocks are no longer used. After receiving TRIM commands, SSDs usually reorganize these blocks in regular garbage collection operations to prepare for future write data. However, the execution mechanism of each SSD to TRIM commands at the bottom is different, but in any case, the performance and lifetime of SSDs can be significantly improved through TRIM. Of course, you may have found that with TRIM, deleted file data will be automatically recovered by SSD, which means that some data recovery (anti-deletion) software that can be used on traditional hard disk may no longer work on SSD.

Since TRIM is so important, how should I enable it? Generally speaking, as long as the operating system you use supports TRIM, there is no need for additional operations. Operating systems supporting TRIM: Windows 7 and subsequent versions, OS X 10.6.8 and subsequent versions, Linux kernel version 2.6.33 and subsequent versions.

Let’s take a look at how SSD trim can be opened from the command line on Mac OS. It’s very simple:
Run the following four commands in the terminal.
1.

Copy code

The code is as follows:

sudo cp /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage.bak

2.

Copy code

The code is as follows:

sudo perl -pi -e ‘s|\x00\x41\x50\x50\x4c\x45\x20\x53\x53\x44\x00|\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|’ /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage

3.

Copy code

The code is as follows:

sudo touch /System/Library/Extensions

4.

Copy code

The code is as follows:

sudo kextcache -prelinked-kernel /System/Library/Caches/com.apple.kext.cache/Startup/kernelcache -K /System/Library/Kernels/kernel /System/Library/Extensions

Recommended Today

MySQL partition

MySQL partition Advantages of partitioning Store more data To optimize the query, only scan one or more necessary partitions. For count () and sum (), only summarize the partition statistics For data that is out of date or does not need to be saved, partitioning is faster Data queries are distributed across multiple disks to […]