I've recently done some digging into fstrim feature on sabresd-6q. Here list it below detailed:
When Nexus 7 first came out, many users felt it was speedy and high-performance. But after months of installing applications and using the tablet, things began slowing down.This was a friction point that many hoped would be fixed in the new Nexus 7 (2013) model, which it was. There’s even more to the story though, it turns out Google has fixed that storage I/O aging problem on all Nexus devices with the Android 4.3 update which comes with a little-hyped feature that could be huge for performance in the future – TRIM support via fstrim.
Why slowing down？
Remember that deleting a file in software isn't actually communicated to solid state storage (whether SSD or eMMC). The space is freed up from the user's perspective, but the eMMC controller in this case still treats the pages in NAND as having valid data. Let's say you copy a 3GB movie to your internal storage, watch the movie and later delete it. You'd have 3GB free to re-use, but until you re-write those blocks the eMMC controller would treat all 3GB as valid data. There's a data structure used by the eMMC controller that tracks mapping logical locations to physical locations in NAND. I won't go into great detail here but the more complex that mapping becomes, and the more locations that have to be tracked, the slower internal NAND management works. Why SSD has such issue? Root cause is "Flash memory is divided into blocks, which is further divided in pages. The minimum write unit is a page, but the smallest erase unit is a block" which will cause fragments as time goes.
Fstrim is one mechanism of linux filesystem which derives from TRIM. TRIM is already introduced since linux2.6.33. It is the name of a command that the operating system can send to tell the SSD which blocks are free in the filesystem.The SSD uses this information to internally defragment the blocks and keep free pages available to be written quickly and efficiently.
How framework triggers fstrim in android4.3?
Android4.3 introduces a new service IdleMaintenanceService to manage when and how trigger fstrim in right time. Once system satisfies the following conditions, it will send out one "ACTION_IDLE_MAINTENANCE_START" intent which the MountService listens for.
- Last time "ACTION_IDLE_MAINTENANCE_START" intent has been sent before 24 hours.
- The device is either off-charger with 80% battery or on-charger with 30% battery
- The device can go idle state.
- User don't use it for 71 minutes.
Test fstrim on Imx.6
In order to wait little time to trigger fstrim, I tried to change the following variables not to wait 71 minutes and 24 hours. After changing, it will trigger one time fstrim two minutes after system goes into idle state every 30 minutes.
- private static final long MIN_IDLE_MAINTENANCE_INTERVAL_MILLIS = 24 * 60 * 60 * 1000; // 1 day
+ private static final long MIN_IDLE_MAINTENANCE_INTERVAL_MILLIS = 30 * 60 * 1000; // 30 minute
- private static final long MIN_USER_INACTIVITY_IDLE_MAINTENANCE_START = 71 * 60 * 1000; // 71 min
+ private static final long MIN_USER_INACTIVITY_IDLE_MAINTENANCE_START = 2 * 60 * 1000; // 2 min
- private static final long MAX_IDLE_MAINTENANCE_DURATION = 71 * 60 * 1000; // 71 min
+ private static final long MAX_IDLE_MAINTENANCE_DURATION = 2 * 60 * 1000; // 2 min
Firstly I run Quadrant apk to see my I/O performance at beginning, Its score is 3544.
During this 30 minutes, you can try to mess your system. Try your best to install most apks. Run monkey test in background.
Then I run Quadrant again to check my I/O performance, its score is only a little decline 3538.
After that I run "logcat | busybox grep -i fstrim" in console to wait for the action of fstrim, here is my logcat:
130|root@sabresd_6dq:/ # logcat -d | busybox grep -i fstrim
I/fstrim ( 2344): Starting fstrim work...
I/fstrim ( 2344): Invoking FITRIM ioctl on /data
I/fstrim ( 2344): Trimmed 6367305728 bytes on /data
I/fstrim ( 2344): Invoking FITRIM ioctl on /cache
I/fstrim ( 2344): Trimmed 526355456 bytes on /cache
I/fstrim ( 2344): Finished fstrim work.
OK, I checked the I/O information again, its score is restored to 3546:
Good, fstrim takes effect. So on imx.6 using android4.3, fstrim will also work automatically. We will also enjoy good performance in I/O memory.Great!