MySQL OOM 系統(tǒng)二 OOM Killer
這里就涉及到一個問題,到底Kill掉誰呢?一般稍微了解一些Linux內(nèi)核的同學第一反應(yīng)是誰用的最多,就Kill掉誰。這當然是Linux內(nèi)核首先考慮的一種重要因素,但是也不完全是這樣的,我們查一些Linux的內(nèi)核方面的資料,可以知道其實Kill誰是由/proc/<pid>/oom_score來決定的,這個值每個進程一個,是由Linux內(nèi)核的oom_badness()函數(shù)負責計算的。那下面我們來仔細讀一讀badness()函數(shù)。
在badness()函數(shù)的注釋部分,寫明了badness()函數(shù)的處理思路:
1) we lose the minimum amount of work done
2) we recover a large amount of memory
3) we don't kill anything innocent of eating tons of memory
4) we want to kill the minimum amount of processes (one)
5) we try to kill the process the user expects us to kill, this algorithm has been meticulously tuned to meet the principle of least surprise ... (be careful when you change it)
總的來說就是Kill掉最小數(shù)量的進程來獲取最大數(shù)量的內(nèi)存,這與我們Kill掉占用內(nèi)存最大的進程是吻合的。
/*
* The memory size of the process is the basis for the badness.
*/
points = p->mm->total_vm;
分數(shù)的起始是進程實際使用的RAM內(nèi)存,注意這里不包括SWAP,即OOM Killer只會與進程實際的物理內(nèi)存有關(guān),與Swap是沒有關(guān)系的,并且我們可以看到,進程實際使用的物理內(nèi)存越多,分數(shù)就越高,分數(shù)越高就越容易被犧牲掉。
/*
* Processes which fork a lot of child processes are likely
* a good choice. We add the vmsize of the childs if they
* have an own mm. This prevents forking servers to flood the
* machine with an endless amount of childs
*/
...
if (chld->mm != p->mm && chld->mm)
points += chld->mm->total_vm;
這段表示子進程占用的內(nèi)存都會計算到父進程上。
s = int_sqrt(cpu_time);
if (s)
points /= s;
s = int_sqrt(int_sqrt(run_time));
if (s)
points /= s;
這表明進程占用的CPU時間越長或者進程運行的時間越長,分數(shù)越低,越不容易被Kill掉。
/*
* Niced processes are most likely less important, so double
* their badness points.
*/
if (task_nice(p) > 0)
points *= 2;
如果進程優(yōu)先級低(nice值,正值低優(yōu)先級,負值高優(yōu)先級),則Point翻倍。
/*
* Superuser processes are usually more important, so we make it
* less likely that we kill those.
*/
if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) ||
p->uid == 0 || p->euid == 0)
points /= 4;
super用戶的進程優(yōu)先級較低。
/*
* We don't want to kill a process with direct hardware access.
* Not only could that mess up the hardware, but usually users
* tend to only have this flag set on applications they think
* of as important.
*/
if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO))
points /= 4;
直接可以訪問原始設(shè)備的進程優(yōu)先級較高。
/*
* Adjust the score by oomkilladj.
*/
if (p->oomkilladj) {
if (p->oomkilladj > 0)
points <<= p->oomkilladj;
else
points >>= -(p->oomkilladj);
}
每個進程有個oomkilladj 可以設(shè)置該進程被kill的優(yōu)先級,這個參數(shù)看上去對Point影響還是比較大的,oomkilladj 最大+15,最小是-17,越大越容易被干掉,這個值由于是移位運算,所以影響還是比較大的。
下面我寫個小程序?qū)嶒炓幌?
#define MEGABYTE 1024*1024*1024 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char *argv[]) { void *myblock = NULL; myblock = (void *) malloc(MEGABYTE); printf("Currently allocating 1GB\n"); sleep(1); int count = 0; while( count < 10) { memset(myblock,1,100*1024*1024); myblock = myblock + 100*1024*1024; count++; printf("Currently allocating %d00 MB\n",count); sleep(10); } exit(0); }
上面的程序先申請一個1G的內(nèi)存空間,然后100M為單位,填充這些內(nèi)存空間。在一個2G內(nèi)存,400M Swap空間的機器上跑3個上面的進程。我們看一下運行結(jié)果:
test1、test2、test3分別申請了1G的虛擬內(nèi)存空間(VIRT),然后每隔10s,實際占用的RAM空間就增長100M(RES)。
當物理內(nèi)存空間不足時,OS開始進行Swap,可用的Swap空間開始減少。
當內(nèi)存是在沒有可分配的空間時,test1進程被操作系統(tǒng)Kill掉了。dmesg 我們可以看到,test1進程被OS Kill掉,同時oom_score為1000。
這3個進程的oom_adj全部都是默認值0。下面我們來實驗一下設(shè)置了oom_adj的效果。重新啟動3個進程,然后我們看到test2的PID是12640
我們運行一下下面的語句
echo 15 > /proc/12640/oom_adj
一段時間后,我們看到Swap空間急劇減少,基本上OS OOM_Killer要開動了。
果然,不出意料,12640進程被kill掉了。
所以為了避免自己需要的進程被kill掉,可以通過設(shè)置進程的oom_adj來實現(xiàn)。當然,有的人會說,這一切都是超售引起的,既然Linux提供了overcommit_memory可以禁用overcommit特性,那為什么不禁用呢。這有利也有弊,一旦禁用overcommit,就意味著MySQL根本無法申請超過實際內(nèi)存的空間,而在MySQL中,存在很多動態(tài)申請內(nèi)存空間的地方,如果申請不到,MySQL就會Crash,這大大增加了MySQL宕機的風險,這也是Linux為什么要overcommit的原因。
有了上面的分析,我們不難看出,如果在不設(shè)置oom_adj的前提下,MySQL一般都會成為OOM_Killer的首選對象,因為MySQL一般都是內(nèi)存的最大占用者。那作為MySQL,我們?nèi)绾伪M量的去規(guī)避被Kill的風險呢,下一章我們將重點從MySQL的角度分析如何規(guī)避OOM。
相關(guān)文章
mysql 數(shù)據(jù)庫安裝經(jīng)驗問題匯總
這篇文章主要介紹了mysql 數(shù)據(jù)庫安裝經(jīng)驗問題匯總,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下2016-09-09Mysql連接本地報錯:1130-host?...?is?not?allowed?to?connect?t
這篇文章主要給大家介紹了關(guān)于Mysql連接本地報錯:1130-host?...?is?not?allowed?to?connect?to?this?MySQL?server的解決方法,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-03-03MySQL用戶和數(shù)據(jù)權(quán)限管理詳解
這篇文章主要為大家詳細介紹了MySQL數(shù)據(jù)庫管理中的用戶和數(shù)據(jù)權(quán)限管理,文中的示例代碼講解詳細,對我們學習MySQL有一定幫助,需要的可以參考一下2022-08-08