谁能解释一下 OOM 杀手中的 oom_badness() 分数?

Could anyone explain about oom_badness() score in OOM killer?

/*
 * 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(), which is the function responsible of handling a critical out of memory state. When out_of_memory() is called (for example by the page allocator or by the page fault handler), it iterates over all tasks to determine their "badness", and the task with the highest value gets forcibly killed (the actual chain of calls is select_bad_process()oom_evaluate_task()间接调用→oom_badness()).

一项任务的 "badness" 取决于多种因素:

  • 它的虚拟内存中有多少页。
  • 它拥有多少个 SWAP 条目。
  • 进程使用了​​多少内存(总字节数/页面大小)。
  • 如果它 already marked to be killed 因为它是 OOM 的原因,它会获得最高分。
  • 如果是init进程或内核线程,则忽略。

在旧的内核版本中,oom_badness() 函数曾经更复杂,例如考虑到不同的缩放因子和 "bonuses" for privileged processes,但它被更新为 "as simple and predictable as possible" .