Android-V lmkd 中的那些属性值

源码基于:Android V

相关博文:

Android lmkd 机制详解(一)

Android lmkd 机制详解(二)

Android lmkd 机制从R到T

1. 汇总

属性名 说明 默认值
ro.lmk.debug 启动 lmkd 的debug 模式,会打印一些调试信息 false
ro.lmk.use_psi 启动 PSI monitor,否则使用memcg 的方式,详细可以查看 init_monitors() 函数 true
ro.lmk.use_new_strategy 启动mp_event_psi 处理策略,否则使用 mp_event_common 常规策略 默认值依赖:**`low_ram_device
ro.lmk.use_minfree_levels 使用水位等级,详细查看 ProcessList.java 否则,使用指定的等级,使用 ro.lmk.low、medium、critical 指定的水位adj false
ro.config.low_ram 低 ram 设备,用以确定是否使用 psi 策略,详细见 ro.lmk.use_new_strategy false
ro.config.per_app_memcg 日志记录是否精确到每个进程的memcg 默认值为 ro.config.low_ram
ro.lmk.low mp_event_common() 函数中使用,依赖于memcg 统计且值在cgroup v1 中生效。 三个prop 用以标记在三个等级下的被查杀的最小 adj。 1001
ro.lmk.medium mp_event_common() 函数中使用,依赖于memcg 统计且值在cgroup v1 中生效。 三个prop 用以标记在三个等级下的被查杀的最小 adj。 800
ro.lmk.critical mp_event_common() 函数中使用,依赖于memcg 统计且值在cgroup v1 中生效。 三个prop 用以标记在三个等级下的被查杀的最小 adj。 0
ro.lmk.critical_upgrade 允许提升pressure level 等级,mp_event_common() 函数中使用 false
ro.lmk.upgrade_pressure 提升压力等级的阈值,依赖 ro.lmk.critical_upgrade 使能 如果mem_pressure < upgrade_pressure,则调用 upgrade_level() 提示压力等级 100
ro.lmk.downgrade_pressure 降低压力等级的阈值,当mem_pressure 低于该值,会考虑降低压力等级 100
ro.lmk.kill_heaviest_task kill 符合条件的最繁重的task(最好决定) VS kill 符合条件的任何task(最快决定) false
ro.lmk.kill_timeout_ms 查杀进程后等待进程释放资源的 timeout,如果超过时间没有释放资源,系统将强制结束该进程。 100
ro.lmk.pressure_after_kill_min_score 当 cycle_after_kill && wmark < WMARK_LOW 时,指定min_score_adj 查杀回收无法满足进程对内存的消耗时,会进入连续查杀,此时指定最小 adj 0
ro.lmk.delay_monitors_until_boot 推迟 init_monitors,直到 boot completed false

|------------------------------------|---------------------------------------------------------------------|---------|---------|
| 属性名 | 说明 | 默认值 ||
| 属性名 | 说明 | 高性能 | 低性能 |
| ro.lmk.psi_partial_stall_ms | PSI stall ms,medium 等级 | 70 | 200 |
| ro.lmk.psi_complete_stall_ms | PSI stall ms,critical 等级 | 700 ||
| ro.lmk.stall_limit_critical | 严重失速限定值,当 FULL_10 大于该值,标记严重失速,min_score_adj 将指定到0 | 100 ||
| ro.lmk.swap_free_low_percentage | swap 分区可用内存占swap total 的百分比,用以判断swap 分区是否处于 low | 10 ||
| ro.lmk.swap_util_max | swap 内存的利用率,用以确定是否有可回收的内存,如果达到该阈值,则表示因无法swap引起的内存压力 | 100 ||
| ro.lmk.swap_compression_ratio | 用以 ZRAM 的压缩比,通常根据 easy_available 计算出 swap free 内存空间 | 1 ||
| ro.lmk.thrashing_limit | 某段时间内 workingset refault (Kernel5.9之后特指 file)在 file lru 中的百分比阈值 | 100 | 30 |
| ro.lmk.thrashing_limit_decay | 为了尽快停止抖动,thrashing_limit 会进行衰减,该值为每次衰减的比例 | 10 | 50 |
| ro.lmk.thrashing_limit_critical | 严重抖动的阈值,默认值为 ro.lmk.thrashing_limit * 3, | ||
| ro.lmk.filecache_min_kb | 上一轮查杀是因为抖动,这一轮查杀若无其他原因,则会确认系统 file lru 是否过低 | 0 ||
| ro.lmk.direct_reclaim_threshold_ms | 直接内存回收的耗时阈值,如果超过该阈值,则表示设备因为直接内存回收卡住了,0 表示不使能该检查 | 0 ||
| ro.lmk.lowmem_min_oom_score | LOW_MEM 原因查杀时,指定的 min_score_adj 最终与 PERCEPTIBLE_APP_ADJ + 1 比较,取最大值 | PREVIOUS_APP_ADJ + 1 ||
| | | | |

2. ro.lmk.swap_free_low_percentage

这用以标记 swap free 的水位线,当swap free 小于该线时,则表示处于 low 状态:

cpp 复制代码
    if (swap_free_low_percentage) {
        swap_low_threshold = mi.field.total_swap * swap_free_low_percentage / 100;
        swap_is_low = get_free_swap(&mi) < swap_low_threshold;
    } else {
        swap_low_threshold = 0;
    }

2.1 变量 swap_is_low

从上面代码可知,swap_is_low 是 get_free_swap() 小于 swap_low_threshold。

cpp 复制代码
static inline int64_t get_free_swap(union meminfo *mi) {
    if (swap_compression_ratio)
        return std::min(mi->field.free_swap, mi->field.easy_available * swap_compression_ratio);
    return mi->field.free_swap;
}

get_free_swap() 的返回值为 meminfo 中的 free_swap 以及 easy_available * swap_compression_ratio 之间的最小值。

这里设计到三个数值:

  • free_swap
  • easy_available
  • swap_compression_ratio

第一个 free_swap 不必多说,先来看下 swap_compression_ration。这里 zram 的压缩率,在V 之前直接是 3,即默认认为压缩比是 1:3,而在 V 中使用了prop 动态控制,默认值为 1。

再来看下 easy_available

cpp 复制代码
mi->field.easy_available = mi->field.nr_free_pages + mi->field.inactive_file;

swap 虽然还有很多,但实际可用的需要根据实际剩余的物理内存来判定,即系统留给 zram 压缩的可用物理空间。

从官方解释来看:

cpp 复制代码
lmkd: measure free swap as available and easily reclaimable memory In the case of ZRAM,
SwapFree does not represent the actual free swap amount because swap space is taken from
the free memory or reclaimed. Therefore use free memory and easily reclaimable memory as an 
approximation of how much free swap system can use. Use SwapFree as a measure of how much
swap space the system will consider using. Min of those two measurements is used to decide
how much usable swap the system still has.

因此,swap_is_low 不仅仅要看 free_swap,还需要考虑实际剩余物理内存以及压缩比。

3. ro.lmk.swap_util_max

是查杀策略中的一种,用以判断 swap 的利用率:

cpp 复制代码
    } else if (wmark < WMARK_HIGH && swap_util_max < 100 &&
               (swap_util = calc_swap_utilization(&mi)) > swap_util_max) {
        /*
         * Too much anon memory is swapped out but swap is not low.
         * Non-swappable allocations created memory pressure.
         */
        kill_reason = LOW_MEM_AND_SWAP_UTIL;
cpp 复制代码
static int calc_swap_utilization(union meminfo *mi) {
    int64_t swap_used = mi->field.total_swap - get_free_swap(mi);
    int64_t total_swappable = mi->field.active_anon + mi->field.inactive_anon +
                              mi->field.shmem + swap_used;
    return total_swappable > 0 ? (swap_used * 100) / total_swappable : 0;
}

系统匿名页越大,表示可以swap 的内存越大,最后比值就会越小。

即,该阈值用以确认系统中可回收的内存,默认值为100时,实际上会停止该检查。

4. ro.lmk.thrashing_limit

在某段时间内,系统会出现 workingset refault,从Kernel 5.9 开始特指文件页的workingset refault:

cpp 复制代码
thrashing = (workingset_refault_file - init_ws_refault) * 100 / (base_file_lru + 1);

抖动就是在某段时间内 workingset_refault_file 在file lru 中的百分比。

每次 mp_event_psi() 的触发都会检查抖动值,且在进入查杀后会对 thrashing_limit 进行衰减,更加严格控制抖动值,以此加快停止抖动。

5. ro.lmk.thrashing_limit_decay

thrashing_limit 的衰减比例,通常衰减10%,在 low ram 设备中衰减50%。

将 thrashing_limit 值进行衰减,用以在 1s 连续查杀释放内存,以此尽快停止抖动:

cpp 复制代码
        int pages_freed = find_and_kill_process(min_score_adj, &ki, &mi, &wi, &curr_tm, &psi_data);
        if (pages_freed > 0) {
            killing = true;
            max_thrashing = 0;
            if (cut_thrashing_limit) {
                /*
                 * Cut thrasing limit by thrashing_limit_decay_pct percentage of the current
                 * thrashing limit until the system stops thrashing.
                 */
                thrashing_limit = (thrashing_limit * (100 - thrashing_limit_decay_pct)) / 100;
            }
        }

注意 thrashing_limit 会在 THRASHING_RESET_INTERVAL_MS(1000) 时间后恢复到 thrashing_limit_pct,也就是恢复到 ro.lmk.thrashing_limit 设定的阈值:

cpp 复制代码
    since_thrashing_reset_ms = get_time_diff_ms(&thrashing_reset_tm, &curr_tm);
    if (since_thrashing_reset_ms > THRASHING_RESET_INTERVAL_MS) {
        ...
        thrashing_limit = thrashing_limit_pct;
    }
相关推荐
simplepeng3 小时前
我的天,我真是和androidx的字体加载杠上了
android
小猫猫猫◍˃ᵕ˂◍5 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式
CYRUS_STUDIO7 小时前
使用 AndroidNativeEmu 调用 JNI 函数
android·逆向·汇编语言
梦否7 小时前
【Android】类加载器&热修复-随记
android
徒步青云7 小时前
Java内存模型
android
今阳7 小时前
鸿蒙开发笔记-6-装饰器之@Require装饰器,@Reusable装饰器
android·app·harmonyos
-优势在我12 小时前
Android TabLayout 实现随意控制item之间的间距
android·java·ui
hedalei12 小时前
android13修改系统Launcher不跟随重力感应旋转
android·launcher
Indoraptor13 小时前
Android Fence 同步框架
android
峥嵘life14 小时前
DeepSeek本地搭建 和 Android
android