/*
* If any of p's children has a different mm and is eligible for kill,
* the one with the highest oom_badness() score is sacrificed for its
* parent. This attempts to lose the minimal amount of work done while
* still freeing memory.
*/
我认为the source code中的注释已经很好地解释了它:
/**
* oom_badness - heuristic function to determine which candidate task to kill
* @p: task struct of which task we should calculate
* @totalpages: total present RAM allowed for page allocation
*
* The heuristic for determining which task to kill is made to be as simple and
* predictable as possible. The goal is to return the highest value for the
* task consuming the most memory to avoid subsequent oom failures.
*/
oom_badness()
函数由out_of_memory()
间接调用,该函数负责处理严重的内存不足状态。调用out_of_memory()
时(例如out_of_memory()
或by the page allocator),它将对所有任务进行迭代以确定它们的“不良”,并且最高价值的任务被强行杀死(实际的调用链为by the page fault handler →select_bad_process()
→select_bad_process()
)。
任务的“坏处”取决于多个因素:
oom_evaluate_task()
,因为它负责OOM,它将获得最高分。[在较早的内核版本中,oom_evaluate_task()
函数曾经更加复杂,例如考虑了不同的缩放因子和oom_badness()
,但对其进行了更新以使其“尽可能简单且可预测”。