Android Native 之 lmkd进程分析
lwkd进程属于native层启动的一个守护进程,他的作用贯穿android世界的始终。他的另外一个大家都属于的名字lowmemorykiller。
根据readme的介绍lmkd是用来对android系统内存检查的守护进程,它通过终止不重要的进程来达到系统稳定运行的状态。在Linux Kernel 4.12之前是这项任务是放在了kernel进行管理,在Linux Kernel 4.12之后移到了native层中,因为在kernel进程管理中直接干掉某个应用进程太过暴力。
后面介绍了我们可以通过一些属性的值来控制lmkd进程的效果,如下:
-
ro.config.low_ram
: choose between low-memory vs high-performance device. Default = false. -
ro.lmk.use_minfree_levels
: use free memory and file cache thresholds for making decisions when to kill. This mode works the same way kernel lowmemorykiller driver used to work. Default = false -
ro.lmk.low
: min oom_adj score for processes eligible to be killed at low vmpressure level. Default = 1001 (disabled) -
ro.lmk.medium
: min oom_adj score for processes eligible to be killed at medium vmpressure level. Default = 800 (non-essential processes) -
ro.lmk.critical
: min oom_adj score for processes eligible to be killed at critical vmpressure level. Default = 0 (all processes) -
ro.lmk.critical_upgrade
: enables upgrade to critical level. Default = false -
ro.lmk.upgrade_pressure
: max mem_pressure at which level will be upgraded because system is swapping too much. Default = 100 (disabled) -
ro.lmk.downgrade_pressure
: min mem_pressure at which vmpressure event will be ignored because enough free memory is still available. Default = 100 (disabled) -
ro.lmk.kill_heaviest_task
: kill heaviest eligible task (best decision) vs. any eligible task (fast decision). Default = false -
ro.lmk.kill_timeout_ms
: duration in ms after a kill when no additional kill will be done. Default = 0 (disabled) -
ro.lmk.debug
: enable lmkd debug logs, Default = false -
ro.lmk.swap_free_low_percentage
: level of free swap as a percentage of the total swap space used as a threshold to consider the system as swap space starved. Default for low-RAM devices = 10, for high-end devices = 20 -
ro.lmk.thrashing_limit
: number of workingset refaults as a percentage of the file-backed pagecache size used as a threshold to consider system thrashing its pagecache. Default for low-RAM devices = 30, for high-end devices = 100 -
ro.lmk.thrashing_limit_decay
: thrashing threshold decay expressed as a percentage of the original threshold used to lower the threshold when system does not recover even after a kill. Default for low-RAM devices = 50, for high-end devices = 10 -
ro.lmk.psi_partial_stall_ms
: partial PSI stall threshold in milliseconds for triggering low memory notification. Default for low-RAM devices = 200, for high-end devices = 70 -
ro.lmk.psi_complete_stall_ms
: complete PSI stall threshold in milliseconds for triggering critical memory notification. Default = 700
lmkd will set the following Android properties according to current system configurations:
-
sys.lmk.minfree_levels
: minfree:oom_adj_score pairs, delimited by comma -
sys.lmk.reportkills
: whether or not it supports reporting process kills to clients. Test app should check this property before testing low memory kill notification. Default will be unset.
一、LMKD源码分析
1、LMKD进程的启动
1.1 三种启动方式
同其他native进程一致,他的启动还是通过init rc机制来启动。如下代码
总结如下:
- 标记为
core
类的服务会在init.rc
的on early-init
或on init
阶段被启动,未显式设置disabled
属性时,core
类服务会随init进程初始化自动启动。PS:在core进行启动是为了防止核心服务没有启动导致系统异常 - 监听sys.boot_completed开机属性然后传递--reinit参数进行完整启动。PS:从后文可以了解到reinit会去读取一系列相关属性,即这是完整启动
- 最后监听属性变更,传递--reinit参数,应该是做成不需要重启系统就能够动态生效这些参数的效果
1.2 主函数三部曲
2、