关于mp_event_common接受的内存压力事件,是从内核而来,简单说下。

先来看下官方的介绍:

Historically, Android used in-kernel lowmemorykiller driver to handle memory pressure situations by killing non-essential processes. This mechanism is rigid and depends on hard-coded values. In addition, starting with kernel 4.12, the lowmemorykiller driver is excluded from the upstream kernel.

The userspace lmkd process implements the same functionality but with already existing kernel mechanisms to detect and estimate memory pressure. It uses vmpressure events generated by the kernel to get notifications about memory pressure levels. It also can use memory cgroup features to limit memory resources allocated to each process based on its importance.

这里还说了老的内核minfree的缺点,就是太死板,我去,那7.0还用着呢, 9.0依然可以用,除非内核不支持了。

介绍也说了用已经存在的内核机制就是kernel vmpressure探测评估内存压力,然后lmkd去获取这个状态,也就是代码看到的event相关代码。

vmpressure以前也大概分析过,可以看这里:vmpressure分析

vmpressure本身就定义了low, medium, critical三类内存压力状态,所以能看到在lmkd 初始化时init了这三种状态,至于怎么和kernel交互,已经偏用户态了,主要是epoll。

if (use_inkernel_interface) {
ALOGI("Using in-kernel low memory killer interface");
} else {
if (!init_mp_common(VMPRESS_LEVEL_LOW) ||
!init_mp_common(VMPRESS_LEVEL_MEDIUM) ||
!init_mp_common(VMPRESS_LEVEL_CRITICAL)) {
ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer");
return -1;
}
}

init_mp_common里注册 by EPOLL_CTL_ADD:

epev.events = EPOLLIN;
/* use data to store event level */
vmpressure_hinfo[level_idx].data = level_idx;
vmpressure_hinfo[level_idx].handler = mp_event_common;
epev.data.ptr = (void *)&vmpressure_hinfo[level_idx];
ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev);
if (ret == -1) {
ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno);
goto err;
}
maxevents++;
mpevfd[level] = evfd;

另外就是策略的选择,看了下骁龙625 P kernel4. 9还是用的in kernel。

至于用户态属性怎么配置,机制大概都明白了,我想还是根据遇到的问题以及性能测试为主,rt?