OOM killer
http://mkosaki.blog46.fc2.com/blog-entry-416.html
このへん見ててなんか前なんかの拍子に OOM killer をながめたのを思い出しました。場所は mm/oom_kill.c 。
こんなかの badness() で各プロセスのメモリに関しての危険度を調べてるんだけど、さっきの switch でどの最適化使うか、って部分もそうなんですが、こういうヒューリスティックスの塊を見るのは面白いなぁと思いました。
例えば一番最初の
/* * swapoff can easily use up all memory, so kill those first. */ if (p->flags & PF_SWAPOFF) return ULONG_MAX;
とかはああなるほどねえと。 PF_SWAPOFF ってのは swapoff(2) を発行したプロセスにくっつく属性みたい。
で次に子がいるプロセスはその子プロセスのメモリの半分だけ親プロセスにペナルティがかかる、っていうのがあって、
/* * Processes which fork a lot of child processes are likely * a good choice. We add half the vmsize of the children if they * have an own mm. This prevents forking servers to flood the * machine with an endless amount of children. In case a single * child is eating the vast majority of memory, adding only half * to the parents will make the child our kill candidate of choice. */ list_for_each_entry(child, &p->children, sibling) { task_lock(child); if (child->mm != mm && child->mm) points += child->mm->total_vm/2 + 1; task_unlock(child); }
適当に要約すると、子プロセスいっぱいいる子は悪い子なのでペナルティあげる☆、でも一つの子プロセスだけがアホみたいに使ってる時に親が死ぬ(Apache+アホCGIとか)と悲しいので半分だけね☆、って感じでしょうか。
でもこれだとひたすら fork しまくる fork bomb には無力だよなーとか思ってちょっとぐぐってみるとやっぱダメらしいと書いてあった。
https://lwn.net/Articles/133924/
で、なんかその後も root だったら 4 割るとかそんなんばっかで面白い。 Robocode とかそいうの思い出す感じ。
まぁ echo -17 > /proc/
http://itpro.nikkeibp.co.jp/article/COLUMN/20061117/254053/
あと init はそもそも最初から殺す候補から除外されてて、 sshd 殺すくらいならいっそ init 殺して欲しいよなぁとか思った。