[WIP][6.0][CM13] Custom 3.0 kernel with lowmemorykiller optimizations

Search This thread

u.b.o.o.t

Senior Member
May 27, 2006
175
76

TLDR: in Oct 2016, the patch has been merged into official Cyanogenmod / Lineage kernel sources: https://github.com/LineageOS/androi...mmit/0e899ff2828df771b524dc52f19f29c0c5cd5842


-------------------------------------------------------------------------

There has been much discussion on UI freezes / lags with CM13 on the Droid 4 in another thread which turned out to be related to some quarrel between Android lowmemorykiller (LMK), ZRAM and kswapd direct reclaim...

As a workaround, increasing extra_free_kbytes (to 75000 in my case) turned out to reduce lags a great deal. (This can be done e.g. with Kernel Adiutor Mod)

Now, I made a small kernel patch adapted from 3.4 kernel which makes LMK account for swap memory, i.e. LMK does only consider memory pages as free which won't require swapping:

Code:
diff --git a/drivers/staging/android/lowmemorykiller.c b/drivers/staging/android/lowmemorykiller.c
index 86d5195..d4e4513 100644
--- a/drivers/staging/android/lowmemorykiller.c
+++ b/drivers/staging/android/lowmemorykiller.c
@@ -34,6 +34,7 @@
 #include <linux/mm.h>
 #include <linux/oom.h>
 #include <linux/sched.h>
+#include <linux/swap.h>
 #include <linux/notifier.h>
 
 static uint32_t lowmem_debug_level = 2;
@@ -90,7 +91,7 @@ static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
        int selected_tasksize = 0;
        int selected_oom_adj;
        int array_size = ARRAY_SIZE(lowmem_adj);
-       int other_free = global_page_state(NR_FREE_PAGES);
+       int other_free = global_page_state(NR_FREE_PAGES) - totalreserve_pages;
        int other_file = global_page_state(NR_FILE_PAGES) -
                                                global_page_state(NR_SHMEM);

If you want to try, just flash the following zip file via safestrap recovery over existing CM13 (both - kernel and ramdisk.img - are included): cm13_lmkpatch_v1.zip

I am not sure yet, if extra_free_kbytes can be reduced now (seemingly not, I'm still at 75000), nor if LMK watermarks can be lowered (maybe yes, I have set it to Kernel Adiutor's Agressive Profile)....

....but I'm very much interested in your findings!

If this turns out to be an improvement, I'll commit a change request to review.cyanogenmod.org
 
Last edited:

u.b.o.o.t

Senior Member
May 27, 2006
175
76
Seems that I was wrong with extra_free_kbytes and LMK watermarks for the patched kernel...

...just leave extra_free_kbytes at stock settings in the first place and maybe try Kernel Adiutor aggressive profile for LMK with empty app watermark increased to 256MB.



Also, I have another gem for you: 3.0 kernel with LMK optimizations adapted from https://github.com/XperiaSTE/android_kernel_sony_u8500/ and https://gitlab.com/k2wl/g2_kernel/

Download:
cm13_lmkpatch_v2.zip (first try)
cm13_lmkpatch_v2.1.zip (added some fixes from https://gitlab.com/k2wl/g2_kernel/commits/kitkat-mr1-rel-razrm)

...this one runs even smoother!

I have extra_free_kbytes = 16000 and LMK watermarks @ Kernel Adiutor aggressive profile and there is plenty of free RAM (around 200MB) and the phone seems to have much less trouble with running several heavy apps in parallel!
 
Last edited:

rabinhood

Senior Member
Feb 16, 2009
395
74
Katowice
Thanks. Great job! The phone's much more responsive now, almost as on stock JB. But could this patch be integrated with Swap SDCard and undervolt mods?
 

u.b.o.o.t

Senior Member
May 27, 2006
175
76
Thanks. Great job! The phone's much more responsive now, almost as on stock JB. But could this patch be integrated with Swap SDCard and undervolt mods?
...of course. Just wait a couple of days to give us some more time for testing and optimization, I will then commit the patch to official Cyanogenmod and @joojoobee666 will integrate it into his UV kernel.

I'd also like to have more feedback from other users.

My current configuration with cm13_lmkpatch_v2.1 is:
- extra_free_kbytes = 32000 (keeping more free RAM for ZRAM decompression)
- LMK watermarks: Kernel Aduitor medium with empty app = 160MB.
 

rabinhood

Senior Member
Feb 16, 2009
395
74
Katowice
Thanks. I just set 'aggresive' profile and extra_free_kbytes=16000, as you suggested before, but I'll try other settings too.

[edit]
I've found that with your patch, D4 is much, much smoother, even without Kernel Adiutor tweaks.
 
Last edited:

Bobcus Leper

Senior Member
Feb 6, 2014
2,078
1,257
Moto X4
Moto G Stylus
...of course. Just wait a couple of days to give us some more time for testing and optimization, I will then commit the patch to official Cyanogenmod and @joojoobee666 will integrate it into his UV kernel.

I'd also like to have more feedback from other users.

My current configuration with cm13_lmkpatch_v2.1 is:
- extra_free_kbytes = 32000 (keeping more free RAM for ZRAM decompression)
- LMK watermarks: Kernel Aduitor medium with empty app = 160MB.

Hi,

Your custom kernel and ramdisk caused my RAZR to boot endlessly. I tried the custom kernel with the default ramdisk, and the default kernel with the custom ramdisk. Both didn't work.
 

u.b.o.o.t

Senior Member
May 27, 2006
175
76
Your custom kernel and ramdisk caused my RAZR to boot endlessly. I tried the custom kernel with the default ramdisk, and the default kernel with the custom ramdisk. Both didn't work.
Sorry for that but.... my kernel is for the Droid 4, only... I don't know anything about the RAZR... same hardware / device tree?

Seems that @joojoobee666 has included some magic into his builds which make it work for RAZR and Droid4 simultaneously...

But wait - in a couple of days, there should be a RAZR kernel with lmk optimizations for you to play with: http://forum.xda-developers.com/showpost.php?p=68904722&postcount=221
 
  • Like
Reactions: Flash-A-Holic

u.b.o.o.t

Senior Member
May 27, 2006
175
76
Hi folks,

I created a new kernel with some further optimizations.

...credits go to Motorola people at https://gitlab.com/k2wl/g2_kernel

In particular, I merged the following patches:
https://gitlab.com/k2wl/g2_kernel/commit/22d990a58fc17b3f0155e15eb2dc3efa037bea1c
https://gitlab.com/k2wl/g2_kernel/commit/7093310c60c972387b5a375ac3b407e2b04f591e
https://gitlab.com/k2wl/g2_kernel/commit/04c5d59093fa8148e9caf48c5c8b8a3f7a4db159
https://gitlab.com/k2wl/g2_kernel/commit/39eaf73c50d34287ef3c79a2b00d977655c1ceeb
https://gitlab.com/k2wl/g2_kernel/commit/dabca8deb03554f8da8d4f88774b02f004ad99c7

I also incorporated a small change from Wiko Sunny kernel which includes swapped pages when calculating the memory currently used by a process.

Download (Droid 4 only!): cm13_lmkpatch_v2.2.zip


Now, we need some further testing and I'd like to have your feedback regarding which values for extra_free_kbytes and LMK watermarks are optimal for you.
 
  • Like
Reactions: wsemi

rabinhood

Senior Member
Feb 16, 2009
395
74
Katowice
Thanks. Now I wanted to ask one, maybe silly, question. I've just installed your v2.2 mod, but since I'm really in need of traditional/normal sdcard access, I flashed SDCard-swap mod afterwards. So right now I'm using 'kernel' file from your mod, and 'ramdisk.img' from the other. Everything seems normal, and I have a feeling my D4 runs smoother than without your mod. Is it enough to flash only 'kernel' file to improve lags, or do I have some speed delusions caused by placebo effect?
 

u.b.o.o.t

Senior Member
May 27, 2006
175
76
The ramdisk contains kernel modules which must match the kernel. For some time I was working on a patch that also touches some core functionality of the kernel. In such a case, kernel modules & ramdisk is affected too. But I did not include these changes yet.

Hence, there should not be any incompatibility between my kernel and official CM13.0 ramdisk at the moment.
 
  • Like
Reactions: rabinhood

schmatzler

Senior Member
Sep 12, 2009
122
39
- LMK watermarks: Kernel Aduitor medium with empty app = 160MB.
Works really well with 2.2, thank you for this! :) Even without changing anything in Adiutor, the phone already responds a lot better than before. :victory:

I didn't touch extra_free_kbytes, though. Increasing it to 32000 makes starting up apps considerably slower. The default of 6075 is fine for me.
 

u.b.o.o.t

Senior Member
May 27, 2006
175
76
You're right - with latest patch, increasing extra_free_kbytes does not seem to be necessary anymore.

Also, lowering LMK watermarks seems reasonible now, which allows more apps to stay in memory. I'm currently using medium profile with empty app at 128MB.
 
  • Like
Reactions: Flash-A-Holic

Careless_

Senior Member
Nov 13, 2010
626
64
what da hellllll

y dis fone so fast now? lol

good job, uboot.

gonna put it through its paces tomorrow during work hours.

<3
 

u.b.o.o.t

Senior Member
May 27, 2006
175
76
I folks!

There are now 2 versions of my patch which I'd like to evaluate / get feedback from you:

1.) ANDROID_LMK_PARAM_AUTO_TUNE enabled. This is the one you already know: cm13_lmkpatch_v2.2.zip It contains (among others) the following changes by Motorola people:
https://gitlab.com/k2wl/g2_kernel/commit/22d990a58fc17b3f0155e15eb2dc3efa037bea1c
https://gitlab.com/k2wl/g2_kernel/commit/7093310c60c972387b5a375ac3b407e2b04f591e


2.) ANDROID_LMK_PARAM_AUTO_TUNE disabled. This is what I commited to Cyanogenmod code review and what has just been integrated by @joojoobee666 into his OC kernel. It is closer to Google's official kernel sources. Droid4 owners may as well flash this one, which is based on official cm-13.0: cm13_lmkpatch_v2.3.zip
 
  • Like
Reactions: Flash-A-Holic

Flash-A-Holic

Senior Member
Jul 8, 2015
159
85
@u.b.o.o.t
I just flashed new kernel by joojoobee666. Think that overall performance is better but big apps are still causing troubles. Browser, settings etc seems to be opening faster. Good job! :good:

Currently I'm using these settings: LMK medium profile with empty app at 128MB and and extra_free_kbytes=16000.

I guess that newest kernels by joojoobee666 has cm13_lmkpatch_v2.3.zip built in?
 
Last edited:

u.b.o.o.t

Senior Member
May 27, 2006
175
76
@u.b.o.o.t
I just flashed new kernel by joojoobee666. Think that overall performance is better but big apps are still causing troubles. Browser, settings etc seems to be opening faster. Good job! :good:
better than cm13_lmkpatch_v2.2.zip or better than previous OC kernel?

Currently I'm using these settings: LMK medium profile with empty app at 128MB and and extra_free_kbytes=16000.

I guess that newest kernels by joojoobee666 has cm13_lmkpatch_v2.3.zip built in?
Yes, exactly.

Yesterday I had some annoying lags with incoming call with lmkpatch_v2.3, empty app watermark at 128MB and extra_free_kbytes=8000. Increasing to 16000 seemed to help a bit, but I'm now back at v2.2 with lmk auto tuning enabled.

I will keep that now for a couple of days of testing...

PS: with stock lmk watermarks, there should be less trouble, but more app killing, and what I'm trying here is to find the sweet spot between maximum performance and minimum lmk kills / app restarts.
 

u.b.o.o.t

Senior Member
May 27, 2006
175
76
Does anyone of you guys have increased battery drain with one of my lmk patches?

I'm facing SUSPEND_BACKOFF kernel wakelocks (https://github.com/asksven/BetterBatteryStats-Knowledge-Base/wiki/suspend_backoff) preventing the device from going deep asleep and it seems this occures as soon as Wifi has been switched on and off...


EDIT: it's not my patches' fault... having the same issues with standard cm13 kernel...
 
Last edited:

rabinhood

Senior Member
Feb 16, 2009
395
74
Katowice
Do you have any guesses what kernel parameters provide the smoothest operation? I'm using aggresive LMK policy, min free kbytes and extra free kbytes 10000, and Z-RAM 200 MB. Definitely my phone's smoother than only with Your latest patch, but that's only my feeling. Do you know any objective means to measure performance gain? Maybe some benchmarks? Considering battery use, I found no striking changes. I believe stock JB kernel was better in terms of energy use, but IMHO the difference is only slighty visible.
 

u.b.o.o.t

Senior Member
May 27, 2006
175
76
I'm still evaluating but at the moment I'm most confident with 2.2 patch with min_free_kbytes, extra_free_kbytes, and zram at stock settings and lmk profile = aggressive.

Patch 2.3 as well as @joojoobee666's current build have lmk autotuning disabled and seem to require extra_free_kbytes=16000...32000.

But I won't touch min_free_kbytes - there are too many implications / side effects.
 
  • Like
Reactions: rabinhood

Top Liked Posts