[Galaxy S II Android Development] Lanchon:
TRIM On Other Roms And Kernels

TRIM on custom roms

when running any non-trim enabled kernel, significant speed benefits can be obtained by overprovisioning the eMMC. as long as a portion of the eMMC is in the erased state (trimmed) it will perform well, even if the kernel is not able to trim. this can be seen for example when the device is new: non-trim kernel and still the device runs nicely. as time goes on, normal usage causes the eMMC to be written all over, reducing the amount of trimmed space to zero and killing performance. this situation can be avoided in two ways: 1) by using a trim-enabled kernel that will trim space once it is no longer used by files, or 2) by setting aside an area of the eMMC and never write to it, effectively keeping it in the erased state. this second option is called overprovisioning in SSD parlance.

those of you wanting to run official CM kernels, CM nightlies, or other custom roms altogether can still obtain most of the benefits of a trim-enabled kernel without one by overprovisioning your eMMC. the stock partitioning of the 4210-based devices includes an 0.5 GB /preload partition that is just perfect for the job.

Requirements:

you have not repartitioned your device and shrank the /preload partition to enlarge other partitions.

your custom rom does not use the /preload partition. (CM does not, and I do not know of any that does... but google!)

you are not using dual-boot or other mods that use the /preload partition.

NOTE: if you have shrunk /preload and enlarged /system to 1 GB you can still follow these steps to overprovision using the free space in /system, but you will need to redo them every time you flash a new rom. otherwise, if you have an 0.5 GB /preload, you can do these steps once and just forget about the whole thing (until you flash something to the /preload partition, that is).

Instructions:

NOTE: please read step 9 now and decide if you want to use a root file manager to delete everything in /preload before you start or if you want to try to format the partition with your current recovery.

READ THIS POST IN FULL. find out which bugs your eMMC has if any, and decide whether to run the risk of trimming.

download to your device the newest trim-enabled kernel for your particular device from here.

download to your device a recovery-flashable copy of the kernel that you are currently using. (or else make a nandroid backup in step 6.)

if you want, download to your device the recovery trimmer script attached to this post. (see step 11 for more information.)

reboot to recovery.

make a nandroid backup if you do not have a flashable copy of your current kernel on your device. (make sure your nandroid is compatible with CWM-based recoveries.)

flash the trim-enabled kernel.

in the advanced section, choose reboot recovery. now you are temporarily running a trim-enabled kernel.

in the mounts and storage section, choose format /preload. (make a nandroid backup first if unsure of its contents.)

NOTE: it has been reported that format /preload does not work. this is a bug in CM's recovery. you may want to adb shell to the device to delete all files and folders under /preload, including those hidden. free space in this partition will remain trimmed when you later use the phone so it is important that most of the partition be empty after this step. (bug report)

still in the mounts and storage section, mount (if necessary) the following partitions: /system, /cache, /data and /preload.

choose one of these two options:

attach your device via USB to your PC, open a terminal, and type adb devices to verify that your device is reachable and authorized. (if it is not, under linux type adb kill-server; sudo adb devices to troubleshoot the issue; under windows try restarting the adb server from an administrator console.) in the terminal type adb shell "fstrim -v /system; fstrim -v /data; fstrim -v /cache; fstrim -v /preload" to trim. for each partition, fstrim should output a message stating the number of bytes trimmed; this indicates success.

flash the attached recovery trimmer script. you will not have any indication of success using this method. (make sure you have mounted the applicable partitions in the previous step!)

flash your old kernel back or, equivalently, restore your nandroid. (you can advance-restore only the boot partition if you want.)

reboot and profit.

TRIM on rooted stock android 4.1.2

this is beyond the scope of this project, but still some people may be interested.

Instructions:

make sure you are rooted.

WARNING: MAKE SURE YOU ARE RUNNING STOCK ANDROID VERSION 4.1.2 (THE RELEASE, NOT A LEAKED VERSION) OR YOU WILL DESTROY YOUR DEVICE DUE TO BRICKBUG!!!

READ THIS POST IN FULL. find out which bugs your eMMC has if any, and decide whether to run the risk of trimming.

WARNING: MAKE SURE YOUR EMMC IS NOT AFFECTED BY TRIM BUG OR YOU WILL DESTROY YOUR DEVICE!!! if you have trim bug, you must not trim on a stock kernel, end of story.

also, it is assumed that release (not a leak) 4.1.2 stock kernel contains this patch and thus is brickbug safe. but there might be different versions, and there is no way to be sure if the corresponding source code was patched by samsung, so...

WARNING: IF YOUR EMMC IS AFFECTED BY BRICKBUG, THE POSSIBILITY HARD BRICKING YOUR DEVICE CANNOT BE COMPLETELY RULED OUT without access to the kernel source code. proceed at your own peril, or better yet, switch to a custom rom/kernel.

install the LagFix (free) app from xda (the market version is declared to be incompatible with some 4210-based devices). go to the LagFix tab, check the 3 partitions, and tap on run. grant root access. the 3 fstrim operations should be successful ("partition was trimmed" means success). alternatively, those with busybox installed can try issuing the fstrim commands themselves. in particular, you must do this to trim /preload. you can also look for the fstrim command in the private files of LagFix.

UPDATE: there is a replacement app for LagFix called Trimmer that has several advantages over the former: is fully free, can schedule TRIMs, and is compatible with Android 5.

reboot and profit.

NOTE: i assume there is little free space in /system and /preload in stock roms, so most benefits will come from trimmed free space in /data. this space will get overwritten in time so you will need to periodically trim.

Recreating My Kernels From Source

i have been wrongly accused of not providing full source code to my kernels. to counter this accusation i am providing step-by-step instructions on how to exactly recreate any of the kernels published in this project from source. to start, all you need to know is the filename of the kernel you want to recreate. then simply follow these steps:

identify and obtain the CM release that corresponds to the kernel based on the kernel filename. example:

kernel: kernel-cm-11-20140915-NIGHTLY-Lanchon-TRIM-20140916-n7000.zip

CM release: cm-11-20140915-NIGHTLY-n7000.zip​

note that nightly releases are not kept for long in CM's download servers. that is why i mirror all relevant nightlies right beside my kernels in the downloads section.

extract the build manifest (/system/etc/build-manifest.xml) from the CM release zip file.

using the manifest, checkout the source code corresponding to the release to ~/android/system by following these instructions.

identify the version of the patches that corresponds to the kernel based on the kernel filename. example:

kernel: kernel-cm-11-20140915-NIGHTLY-Lanchon-TRIM-20140916-n7000.zip

branch: cm-11

date: 20140916

tag to match: cm-11-20140916​

identify the corresponding tag in my github repo and checkout its tree to ~/android/brickbug/BrickbugAftermath-SGS2. if no tag matches exactly, use the tag in the same branch that sports the closest earlier date.

run ~/android/brickbug/BrickbugAftermath-SGS2/scripts/repo-patch apply to apply the patches.

(repo-patch apply functionality used to be provided by standalone script apply in old versions.)

build the kernel using these instructions.

finally, you can run ~/android/brickbug/BrickbugAftermath-SGS2/scripts/repo-patch reset to unpatch your source tree.

(repo-patch reset functionality used to be provided by standalone script reset in old versions.)