概述
Linux系统当可用内存较低的时候oom killer机制会根据一定的规则去杀掉一些进程来释放内存,而Android系统的LowMemoryKiller机制就是以此功能为基础做了一些调整。Android系统中的APP在使用完成之后并不会马上被杀掉,而是驻留在内存中,当下一次在此进入此应用的时候可以省去进程创建的过程,加快启动速度。LowMemoryKiller机制会在内存资源紧张的时候,杀掉一些进程来回收内存。
整体架构
LowMemoryKiller机制分为三个部分
LowMemoryKiller整体结构图
- AMS部分的ProcessList
- Native进程lmkd
- 内核中的LowMemoryKiller部分
Framework中的ProcessList和Native的lmkd进程通过Socket进行进程间通信,而lmkd和内核中的LowMemoryKiller通过writeFileString向文件节点写内容方法进行通信。
Framework层通过一定的规则调整进程的adj的值和内存空间阀值,然后通过socket发送给lmkd进程,lmkd两种处理方式, 一种将阀值写入文件节点发送给内核的LowMemoryKiller,由内核进行杀进程处理,另一种是lmkd通过cgroup监控内存使用情况,自行计算杀掉进程。
lmkd的启动和初始化
lmkd是一个native进程,由init进程启动,定义在/system/core/lmkd/lmkd.rc中
service lmkd /system/bin/lmkd
class core
group root readproc
critical
socket lmkd seqpacket 0660 system system
writepid /dev/cpuset/system-background/tasks
在lmkd.rc中,启动了lmkd进程,并创建了一个名为lmkd的socket的描述符,用于socket进程间通信。lmkd启动后首先执行main方法。
int main(int argc __unused, char **argv __unused) {
struct sched_param param = {
.sched_priority = 1,
};
sched_setscheduler(0, SCHED_FIFO, ¶m);
if (!init())
mainloop();
}
main方法首先设置了当前进程的调度规则,然后执行了init方法和mainLoop方法。
static int init(void) {
struct epoll_event epev;
int i;
int ret;
//获取当前系统的页大小,单位kb
page_k = sysconf(_SC_PAGESIZE);
if (page_k == -1)
page_k = PAGE_SIZE;
page_k /= 1024;
// 创建一个epollfd描述符
epollfd = epoll_create(MAX_EPOLL_EVENTS);
if (epollfd == -1) {
ALOGE("epoll_create failed (errno=%d)", errno);
return -1;
}
// mark data connections as not connected
for (int i = 0; i < MAX_DATA_CONN; i++) {
data_sock[i].sock = -1;
}
// 获取init.rc中创建的lmkd socket描述符
ctrl_sock.sock = android_get_control_socket("lmkd");
if (ctrl_sock.sock < 0) {
ALOGE("get lmkd control socket failed");
return -1;
}
// 监听socket的连接,即ProcessList的Socket连接
ret = listen(ctrl_sock.sock, MAX_DATA_CONN);
if (ret < 0) {
ALOGE("lmkd control socket listen failed (errno=%d)", errno);
return -1;
}
// Epoll 设置监听socket中的可读事件,当有可读事件的时候回调ctrl_connect_hander方法
//处理socket连接过程
epev.events = EPOLLIN;
ctrl_sock.handler_info.handler = ctrl_connect_handler;
epev.data.ptr = (void *)&(ctrl_sock.handler_info);
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_sock.sock, &epev) == -1) {
ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno);
return -1;
}
maxevents++;
//检测内核是否支持lowMemoryKiller机制
has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK);
use_inkernel_interface = has_inkernel_module;
//如果内核不支持LowMemoryKiller,则调用init_mp_common初始化,在lmkd中实现进程查杀过程
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;
}
}
//初始化lmkd中的进程列表
for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) {
procadjslot_list[i].next = &procadjslot_list[i];
procadjslot_list[i].prev = &procadjslot_list[i];
}
return 0;
}
lmkd的init方法中做的工作
- 获取lmkd的socket描述符
- 创建epoll来监听socket的连接,如果有连接则回调ctrl_connect_handler方法来处理。
- 检测是否有minfree接口,即内核是否支持lowmemorykiller,如果内核不支持则调用init_mp_common初始化,在lmkd中实现进程查杀。
为了防止剩余内存过低,Android在内核空间有lowmemorykiller(简称LMK),LMK是通过注册shrinker来触发低内存回收的,这个机制并不太优雅,可能会拖慢Shrinkers内存扫描速度,已从内核4.12中移除,后续会采用用户空间的LMKD + memory cgroups机制
我们先分析内核实现的LowMemoryKiller进程查杀机制, 然后再分析lmkd实现的机制。两者最终的结果都是在内存紧张的时候杀死一些进程来释放内存, 但是实现机制去不太一样。
static void mainloop(void) {
struct event_handler_info* handler_info;
struct epoll_event *evt;
//循环等待epoll事件的上报
while (1) {
struct epoll_event events[maxevents];
int nevents;
int i;
nevents = epoll_wait(epollfd, events, maxevents, -1);
if (nevents == -1) {
if (errno == EINTR)
continue;
ALOGE("epoll_wait failed (errno=%d)", errno);
continue;
}
//获取到对应的epoll事件,分发给对应的handler处理
for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) {
if (evt->events & EPOLLERR)
ALOGD("EPOLLERR on event #%d", i);
if (evt->events & EPOLLHUP) {
/* This case was handled in the first pass */
continue;
}
if (evt->data.ptr) {
handler_info = (struct event_handler_info*)evt->data.ptr;
handler_info->handler(handler_info->data, evt->events);
}
}
}
}
init执行初始化完成之后, 进入mainloop方法,循环等待epoll事件的上报,init的时候epoll监听的socket连接, 当有socket连接的时候就会调用ctrl_connect_handler方法。
static void ctrl_connect_handler(int data __unused, uint32_t events __unused) {
struct epoll_event epev;
int free_dscock_idx = get_free_dsock();
if (free_dscock_idx < 0) {
/*
* Number of data connections exceeded max supported. This should not
* happen but if it does we drop all existing connections and accept
* the new one. This prevents inactive connections from monopolizing
* data socket and if we drop ActivityManager connection it will
* immediately reconnect.
*/
for (int i = 0; i < MAX_DATA_CONN; i++) {
ctrl_data_close(i);
}
free_dscock_idx = 0;
}
//接受framework的socket连接
data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL);
if (data_sock[free_dscock_idx].sock < 0) {
ALOGE("lmkd control socket accept failed; errno=%d", errno);
return;
}
ALOGI("lmkd data connection established");
//监听连接的socket通信,当socket有消息的时候会掉ctrl_data_handler方法。
data_sock[free_dscock_idx].handler_info.data = free_dscock_idx;
data_sock[free_dscock_idx].handler_info.handler = ctrl_data_handler;
epev.events = EPOLLIN;
epev.data.ptr = (void *)&(data_sock[free_dscock_idx].handler_info);
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, data_sock[free_dscock_idx].sock, &epev) == -1) {
ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno);
ctrl_data_close(free_dscock_idx);
return;
}
maxevents++;
}
监听到socket连接, 我们知道此时连接lmkd的socket客户端就是framework,当有连接到来的时候accept方法返回连接的socketFD, 然后将连接的socketFD同样加入epoll中, 当socketFD中有可读消息,即framework给lmkd发送消息的时候,epoll唤醒然后会掉ctrl_data_handler方法来处理。
Framework和lmkd通信
Framework和lmkd进程通过socket来进行进程间通信,在lmkd初始化的时候,通过监听socket描述符lmkd来等待Framework发送的消息。
Framework向lmkd发送命令相关的方法有三个。
- AMS.updateConfiguration
更新配置,手机屏幕的尺寸和内存大小不一样,对应的最小内存阀值和adj值也不一样, 最终调用ProcessList的updateOomLevel方法向lmkd发送调整命令- AMS.applyOomAdjLocked AMS根据一定的规则调整进程的adj值,最用通过ProcessList的setOomAdj方法发送给lmkd调整命令
- AMS.cleanUpApplicationRecordLocked & AMS.handleAppDiedLocked 进程死亡后,调用ProcessList的remove方法移除进程
上面的三种情况Framework最终是通过socket向lmkd发送了三种消息。
// LMK_TARGET <minfree> <minkillprio> ... (up to 6 pairs)
// LMK_PROCPRIO <pid> <uid> <prio>
// LMK_PROCREMOVE <pid>
//调整minfree和adj的值
static final byte LMK_TARGET = 0;
//设置对应进程的adj值
static final byte LMK_PROCPRIO = 1;
//移除对应进程
static final byte LMK_PROCREMOVE = 2;
lmkd接收命令处理逻辑
static void ctrl_command_handler(int dsock_idx) {
LMKD_CTRL_PACKET packet;
int len;
enum lmk_cmd cmd;
int nargs;
int targets;
//从socket中读取数据
len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE);
if (len <= 0)
return;
if (len < (int)sizeof(int)) {
ALOGE("Wrong control socket read length len=%d", len);
return;
}
//解析Socket的命令和参数
cmd = lmkd_pack_get_cmd(packet);
nargs = len / sizeof(int) - 1;
if (nargs < 0)
goto wronglen;
switch(cmd) {
case LMK_TARGET:
targets = nargs / 2;
if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj))
goto wronglen;
//调整minfree和adj阀值
cmd_target(targets, packet);
break;
case LMK_PROCPRIO:
if (nargs != 3)
goto wronglen;
//设置对应进程的adj值
cmd_procprio(packet);
break;
case LMK_PROCREMOVE:
if (nargs != 1)
goto wronglen;
//移除对应的进程
cmd_procremove(packet);
break;
default:
ALOGE("Received unknown command code %d", cmd);
return;
}
return;
lmkd通过epoll监听socket中是否有数据, 当接受的framework发送的socket命令之后,调用ctrl_cmmand_handler方法处理,显示解析socket中的命令和参数,根据对于的命令来调用不同的方法处理。
- cmd_target 调整最小内存阀值和adj值
- cmd_procprio 调整进程的adj值
- cmd_procremove 移除对应的进程
对于进程查杀有两种实现方式,一种是内核的LMK,通过shrinker来触发低内存回收, 另一种是lmkd通过cgroup监控内存使用情况,自行计算杀掉进程。两种实现不太一样,需要逐个分析。
内核LMK的实现
cmd_target
static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) {
int i;
struct lmk_target target;
lowmem_targets_size = ntargets;
//使用kernel中的LMK
if (has_inkernel_module) {
char minfreestr[128];
char killpriostr[128];
minfreestr[0] = '\0';
killpriostr[0] = '\0';
//将从framework收到的内存阀值和adj值封装成字符串,以,分隔
//如 18432,23040,27648,32256,55296,80640
//0,100,200,300,900,906
for (i = 0; i < lowmem_targets_size; i++) {
char val[40];
if (i) {
strlcat(minfreestr, ",", sizeof(minfreestr));
strlcat(killpriostr, ",", sizeof(killpriostr));
}
snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0);
strlcat(minfreestr, val, sizeof(minfreestr));
snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0);
strlcat(killpriostr, val, sizeof(killpriostr));
}
// 将字符串分别写入 /sys/module/lowmemorykiller/parameters/minfree
// 和/sys/module/lowmemorykiller/parameters/adj
writefilestring(INKERNEL_MINFREE_PATH, minfreestr);
writefilestring(INKERNEL_ADJ_PATH, killpriostr);
}
}
设置内存阀值和adj的值就是将从framework收到的数据封装成字符串,通过writefilestring写入到两个文件节点,以供内核LMK使用。
/sys/module/lowmemorykiller/parameters/minfree : 内存级别限额
/sys/module/lowmemorykiller/parameters/adj :内存级别限额对应的要杀掉的进程的adj值.
cmd_procprio
static void cmd_procprio(LMKD_CTRL_PACKET packet) {
struct proc *procp;
char path[80];
char val[20];
int soft_limit_mult;
struct lmk_procprio params;
//解析进程adj相关的参数
lmkd_pack_get_procprio(packet, ¶ms);
if (params.oomadj < OOM_SCORE_ADJ_MIN ||
params.oomadj > OOM_SCORE_ADJ_MAX) {
ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj);
return;
}
//将进程优先级写入到/proc/进程id/oom_score_adj文件中
snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid);
snprintf(val, sizeof(val), "%d", params.oomadj);
writefilestring(path, val);
if (use_inkernel_interface)
return;
}
由于使用内核LMK, 所以调整进程优先级直接将优先级写入对应进程的oom_adj_score文件即可。
cmd_procremove
static void cmd_procremove(LMKD_CTRL_PACKET packet) {
struct lmk_procremove params;
//内核LMK,移除进程什么都不需要做,全部有内核处理
if (use_inkernel_interface)
return;
}
移除进程的时候不需要做任何操作
内核LowMemoryKiller的实现原理
在linux中,有一个名为kswapd的内核线程,当linux回收存放分页的时候,kswapd线程将会遍历一张shrinker链表,并执行回调,或者某个app启动,发现可用内存不足时,则内核会阻塞请求分配内存的进程分配内存的过程,并在该进程中去执行lowmemorykiller来释放内存。虽然之前没有接触过,大体的理解就是向系统注册了这个shrinker回调函数之后,当系统空闲内存页面不足时会调用这个回调函数。 struct shrinker的定义在linux/kernel/include/linux/shrinker.h中:
内核LowMemoryKiller shrinker的注册过程如下:
static struct shrinker lowmem_shrinker = {
.scan_objects = lowmem_scan,
.count_objects = lowmem_count,
.seeks = DEFAULT_SEEKS * 16
};
static int __init lowmem_init(void)
{
register_shrinker(&lowmem_shrinker);
return 0;
}
static void __exit lowmem_exit(void)
{
unregister_shrinker(&lowmem_shrinker);
}
注册完成之后, 在内存紧张的时候就会回调shrinker, 其中最主要的是lowmem_scan方法。具体实现如下:
static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
{
struct task_struct *tsk;
//选中要杀掉进程的task_struct
struct task_struct *selected = NULL;
unsigned long rem = 0;
int tasksize;
int I;
//min_score_adj 初始值=1000
short min_score_adj = OOM_SCORE_ADJ_MAX + 1;
int minfree = 0;
//要杀掉进程占用内存的大小
int selected_tasksize = 0;
//要杀掉进程的adj值
short selected_oom_score_adj;
//获取并计算剩余内存的大小
int array_size = ARRAY_SIZE(lowmem_adj);
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) -
total_swapcache_pages();
//从minfree中和adj节点中获取的数据初始化内存限额和adj
if (lowmem_adj_size < array_size)
array_size = lowmem_adj_size;
if (lowmem_minfree_size < array_size)
array_size = lowmem_minfree_size;
// 内存限额从小到大遍历, 找到剩余内存属于哪一个阀值,并获取它对应的adj值
for (i = 0; i < array_size; i++) {
minfree = lowmem_minfree[i];
if (other_free < minfree && other_file < minfree) {
min_score_adj = lowmem_adj[i];
break;
}
}
lowmem_print(3, "lowmem_scan %lu, %x, ofree %d %d, ma %hd\n",
sc->nr_to_scan, sc->gfp_mask, other_free,
other_file, min_score_adj);
//如果adj值为初始值1000,则内存情况良好无需杀掉进程
if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
lowmem_print(5, "lowmem_scan %lu, %x, return 0\n",
sc->nr_to_scan, sc->gfp_mask);
return 0;
}
selected_oom_score_adj = min_score_adj;
rcu_read_lock();
//遍历进程列表
for_each_process(tsk) {
struct task_struct *p;
short oom_score_adj;
if (tsk->flags & PF_KTHREAD)
continue;
p = find_lock_task_mm(tsk);
if (!p)
continue;
if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
time_before_eq(jiffies, lowmem_deathpending_timeout)) {
task_unlock(p);
rcu_read_unlock();
return 0;
}
//获取进程的adj值,如果进程的adj值小于目标adj值,则继续寻找
oom_score_adj = p->signal->oom_score_adj;
if (oom_score_adj < min_score_adj) {
task_unlock(p);
continue;
}
//找到adj值大于目标adj的进程后,首先计算该进程占用的内存大小,
//如果两个进程的adj一般大,则找占用内存大的一个进程
tasksize = get_mm_rss(p->mm);
task_unlock(p);
if (tasksize <= 0)
continue;
if (selected) {
if (oom_score_adj < selected_oom_score_adj)
continue;
if (oom_score_adj == selected_oom_score_adj &&
tasksize <= selected_tasksize)
continue;
}
selected = p;
selected_tasksize = tasksize;
selected_oom_score_adj = oom_score_adj;
lowmem_print(2, "select '%s' (%d), adj %hd, size %d, to kill\n",
p->comm, p->pid, oom_score_adj, tasksize);
}
//将找到的进程发送SIGKILL杀掉该进程从而释放内存
if (selected) {
long cache_size = other_file * (long)(PAGE_SIZE / 1024);
long cache_limit = minfree * (long)(PAGE_SIZE / 1024);
long free = other_free * (long)(PAGE_SIZE / 1024);
trace_lowmemory_kill(selected, cache_size, cache_limit, free);
lowmem_print(1, "Killing '%s' (%d), adj %hd,\n" \
" to free %ldkB on behalf of '%s' (%d) because\n" \
" cache %ldkB is below limit %ldkB for oom_score_adj %hd\n" \
" Free memory is %ldkB above reserved\n",
selected->comm, selected->pid,
selected_oom_score_adj,
selected_tasksize * (long)(PAGE_SIZE / 1024),
current->comm, current->pid,
cache_size, cache_limit,
min_score_adj,
free);
lowmem_deathpending_timeout = jiffies + HZ;
set_tsk_thread_flag(selected, TIF_MEMDIE);
send_sig(SIGKILL, selected, 0);
rem += selected_tasksize;
}
lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n",
sc->nr_to_scan, sc->gfp_mask, rem);
rcu_read_unlock();
return rem;
}
内核LMK的原理很简单:首先注册了shrinker,在内存紧张的时候会触发lowmem_scan方法,这个方法要做的就是找打一个进程,然后杀掉他,释放一些内存。
- 获取剩余内存的大小,和Minfree内存阀值做比较,找到对应的内存阀值,找到对应的adj值。
- 遍历所有的进程,大于该adj的值的进程是要杀掉的目标进程, 但是并不是全部杀掉,而是找到adj最大的进程杀掉,如果最大adj有多个相同adj进程,则杀掉占用内存最大的一个
内核LMK的实现逻辑已经分析完了
lmkd内存查杀的实现
lmkd实现内存查实的方式是基于cgroup memory来实现的。
什么是cgroup memory?
Cgroup的memory子系统,即memory cgroup(本文以下简称memcg),提供了对系统中一组进程的内存行为的管理,从而对整个系统中对内存有不用需求的进程或应用程序区分管理,实现更有效的资源利用和隔离。
cgroup memory相关的文件
cgroup.event_control #用于eventfd的接口
memory.usage_in_bytes #显示当前已用的内存
memory.limit_in_bytes #设置/显示当前限制的内存额度
memory.failcnt #显示内存使用量达到限制值的次数
memory.max_usage_in_bytes #历史内存最大使用量
memory.soft_limit_in_bytes #设置/显示当前限制的内存软额度
memory.stat #显示当前cgroup的内存使用情况
memory.use_hierarchy #设置/显示是否将子cgroup的内存使用情况统计到当前cgroup里面
memory.force_empty #触发系统立即尽可能的回收当前cgroup中可以回收的内存
memory.pressure_level #设置内存压力的通知事件,配合cgroup.event_control一起使用
memory.swappiness #设置和显示当前的swappiness
memory.move_charge_at_immigrate #设置当进程移动到其他cgroup中时,它所占用的内存是否也随着移动过去
memory.oom_control #设置/显示oom controls相关的配置
memory.numa_stat #显示numa相关的内存
简单的了解了下cgroup的原理,再来看lmkd的init方法
static int init(void) {
has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK);
use_inkernel_interface = has_inkernel_module;
if (use_inkernel_interface) {
ALOGI("Using in-kernel low memory killer interface");
} else {
//如果没有使用内核LMK机制,则初始化memory pressure
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;
}
}
for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) {
procadjslot_list[i].next = &procadjslot_list[i];
procadjslot_list[i].prev = &procadjslot_list[i];
}
return 0;
}
static bool init_mp_common(enum vmpressure_level level) {
int mpfd;
int evfd;
int evctlfd;
char buf[256];
struct epoll_event epev;
int ret;
int level_idx = (int)level;
const char *levelstr = level_name[level_idx];
//打开pressure_level的文件节点
mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC);
if (mpfd < 0) {
ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
goto err_open_mpfd;
}
//打开event_control的文件节点
evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC);
if (evctlfd < 0) {
ALOGI("No kernel memory cgroup event control (errno=%d)", errno);
goto err_open_evctlfd;
}
//创建一个eventfd
evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
if (evfd < 0) {
ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno);
goto err_eventfd;
}
//往cgroup.event_control中写入:<event_fd> <pressure_level_fd> <level>
ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr);
if (ret >= (ssize_t)sizeof(buf)) {
ALOGE("cgroup.event_control line overflow for level %s", levelstr);
goto err;
}
ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1));
if (ret == -1) {
ALOGE("cgroup.event_control write failed for level %s; errno=%d",
levelstr, errno);
goto err;
}
//然后使用epoll监听evfd, 等待memory pressure_level的事件通知
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;
close(evctlfd);
return true;
err:
close(evfd);
err_eventfd:
close(evctlfd);
err_open_evctlfd:
close(mpfd);
err_open_mpfd:
return false;
}
先了解下memory pressure_level的用法
memory.pressure_level
这个文件主要用来监控当前cgroup的内存压力,当内存压力大时(即已使用内存快达到设置的限额),在分配内存之前需要先回收部分内存,从而影响内存分配速度,影响性能,而通过监控当前cgroup的内存压力,可以在有压力的时候采取一定的行动来改善当前cgroup的性能,比如关闭当前cgroup中不重要的服务等。目前有三种压力水平:
low
意味着系统在开始为当前cgroup分配内存之前,需要先回收内存中的数据了,这时候回收的是在磁盘上有对应文件的内存数据。
medium
意味着系统已经开始频繁为当前cgroup使用交换空间了。
critical
快撑不住了,系统随时有可能kill掉cgroup中的进程。如何配置相关的监听事件呢?和memory.oom_control类似,大概步骤如下:
利用函数eventfd(2)创建一个event_fd
打开文件memory.pressure_level,得到pressure_level_fd
往cgroup.event_control中写入这么一串:<event_fd> <pressure_level_fd> <level>
然后通过读event_fd得到通知
init_mp_common方法严格的按照pressure_level的用法,注册了pressure_level的事件回调, pressure_level分为三个等级
static const char *level_name[] = {
"low",
"medium",
"critical"
};
当内存达到相应的等级,就会回调mp_event_common方法, 由mp_event_common方法来处理。
static void mp_event_common(int data, uint32_t events __unused) {
int ret;
unsigned long long evcount;
int64_t mem_usage, memsw_usage;
int64_t mem_pressure;
enum vmpressure_level lvl;
union meminfo mi;
union zoneinfo zi;
static struct timeval last_report_tm;
static unsigned long skip_count = 0;
enum vmpressure_level level = (enum vmpressure_level)data;
long other_free = 0, other_file = 0;
int min_score_adj;
int pages_to_free = 0;
int minfree = 0;
static struct reread_data mem_usage_file_data = {
.filename = MEMCG_MEMORY_USAGE,
.fd = -1,
};
static struct reread_data memsw_usage_file_data = {
.filename = MEMCG_MEMORYSW_USAGE,
.fd = -1,
};
//检查触发该方法压力类型,如果大于一种类型出发,则选择压力大的一个
for (lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) {
if (mpevfd[lvl] != -1 &&
TEMP_FAILURE_RETRY(read(mpevfd[lvl],
&evcount, sizeof(evcount))) > 0 &&
evcount > 0 && lvl > level) {
level = lvl;
}
}
//获取剩余内存的大小
if (meminfo_parse(&mi) < 0 || zoneinfo_parse(&zi) < 0) {
ALOGE("Failed to get free memory!");
return;
}
//使用设置的最小内存阀值
if (use_minfree_levels) {
int i;
//获取剩余内存大小
other_free = mi.field.nr_free_pages - zi.field.totalreserve_pages;
if (mi.field.nr_file_pages > (mi.field.shmem + mi.field.unevictable + mi.field.swap_cached)) {
other_file = (mi.field.nr_file_pages - mi.field.shmem -
mi.field.unevictable - mi.field.swap_cached);
} else {
other_file = 0;
}
//根据剩余内存大小,找到对应的内存阀值及adj的值
min_score_adj = OOM_SCORE_ADJ_MAX + 1;
for (i = 0; i < lowmem_targets_size; i++) {
minfree = lowmem_minfree[i];
if (other_free < minfree && other_file < minfree) {
min_score_adj = lowmem_adj[i];
break;
}
}
//如果得到的adj值=1000, 则表示内存状况良好,无需查杀内存直接返回
if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
if (debug_process_killing) {
ALOGI("Ignore %s memory pressure event "
"(free memory=%ldkB, cache=%ldkB, limit=%ldkB)",
level_name[level], other_free * page_k, other_file * page_k,
(long)lowmem_minfree[lowmem_targets_size - 1] * page_k);
}
return;
}
//计算要达到最大内存阀值情况下需要释放的内存大小
/* Free up enough pages to push over the highest minfree level */
pages_to_free = lowmem_minfree[lowmem_targets_size - 1] -
((other_free < other_file) ? other_free : other_file);
goto do_kill;
}
if (level == VMPRESS_LEVEL_LOW) {
record_low_pressure_levels(&mi);
}
//内存状况良好, 无需查杀,直接退出
if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) {
/* Do not monitor this pressure level */
return;
}
if ((mem_usage = get_memory_usage(&mem_usage_file_data)) < 0) {
goto do_kill;
}
if ((memsw_usage = get_memory_usage(&memsw_usage_file_data)) < 0) {
goto do_kill;
}
// Calculate percent for swappinness.
mem_pressure = (mem_usage * 100) / memsw_usage;
//根据一定的规则,计算出压力level
if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) {
// We are swapping too much.
if (mem_pressure < upgrade_pressure) {
level = upgrade_level(level);
if (debug_process_killing) {
ALOGI("Event upgraded to %s", level_name[level]);
}
}
}
// If the pressure is larger than downgrade_pressure lmk will not
// kill any process, since enough memory is available.
if (mem_pressure > downgrade_pressure) {
if (debug_process_killing) {
ALOGI("Ignore %s memory pressure", level_name[level]);
}
return;
} else if (level == VMPRESS_LEVEL_CRITICAL &&
mem_pressure > upgrade_pressure) {
if (debug_process_killing) {
ALOGI("Downgrade critical memory pressure");
}
// Downgrade event, since enough memory available.
level = downgrade_level(level);
}
do_kill:
if (low_ram_device) {
//只杀掉一个进程
if (find_and_kill_processes(level, level_oomadj[level], 0) == 0) {
if (debug_process_killing) {
ALOGI("Nothing to kill");
}
}
} else {
int pages_freed;
//如果不使用设置的minfree内存阀值,则根据计算level对应的adj来查杀内存
if (!use_minfree_levels) {
/* If pressure level is less than critical and enough free swap then ignore */
if (level < VMPRESS_LEVEL_CRITICAL &&
mi.field.free_swap > low_pressure_mem.max_nr_free_pages) {
if (debug_process_killing) {
ALOGI("Ignoring pressure since %" PRId64
" swap pages are available ",
mi.field.free_swap);
}
return;
}
//计算出需要释放的内存大小
if (mi.field.nr_free_pages < low_pressure_mem.max_nr_free_pages) {
pages_to_free = low_pressure_mem.max_nr_free_pages -
mi.field.nr_free_pages;
} else {
if (debug_process_killing) {
ALOGI("Ignoring pressure since more memory is "
"available (%" PRId64 ") than watermark (%" PRId64 ")",
mi.field.nr_free_pages, low_pressure_mem.max_nr_free_pages);
}
return;
}
min_score_adj = level_oomadj[level];
}
//根据min_score_adj和page_to_free来杀掉进程
//大于min_score_adj的进程都属于目标进程,循环查杀,并累计杀掉线程释放内存的大小
//当释放的内存大于page_to_free的大小的时候,就可以停止了
pages_freed = find_and_kill_processes(level, min_score_adj, pages_to_free);
if (use_minfree_levels) {
ALOGI("Killing because cache %ldkB is below "
"limit %ldkB for oom_adj %d\n"
" Free memory is %ldkB %s reserved",
other_file * page_k, minfree * page_k, min_score_adj,
other_free * page_k, other_free >= 0 ? "above" : "below");
}
if (pages_freed < pages_to_free) {
ALOGI("Unable to free enough memory (pages to free=%d, pages freed=%d)",
pages_to_free, pages_freed);
} else {
ALOGI("Reclaimed enough memory (pages to free=%d, pages freed=%d)",
pages_to_free, pages_freed);
gettimeofday(&last_report_tm, NULL);
}
}
}
lmkd内存查杀原理:
- 当内存压力出发该方法的时候,读取当前的内存压力类型
- 获取当前剩余内存的大小
- 根据剩余内存计算要杀掉进程的adj值,以及需要释放内存的大小
分为两种情况:
a: 如果使用的是设置的minfree和adj值,则根据剩余内存大小找到对应的adj,和需要释放内存大小
b:如果不使用设置的内存阀值,则根据当前压力类型计算adj,以及达到上一个等级需要释放内存大小 - 根据以上计算的adj和需要释放内存大小来查杀内存,从大于adj的进程中开始查杀进程并释放内存,当释放内存大小达到需求 就可以停止查杀内存了。
总结
进程查杀的两种实现方式原理类似,都是注册是的回调,当内存紧张的时候根据剩余内存的adj来查杀大于该adj的内存。内核shrinker方式是只有内存紧张的时候才会去释放,而cgroup方式控制更加精细, 根据不同等级来触发内存回收。
网友评论