๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/Linux

[Linux] OOM Killer๋ž€? Kill ๋Œ€์ƒ์—์„œ ์ œ์™ธ์‹œํ‚ค๊ธฐ

by ynzu๐Ÿค 2022. 1. 18.
๋ฐ˜์‘ํ˜•

 

ํšŒ์‚ฌ์—์„œ ๋‹ค๋ฅธ ํŒ€๋“ค๊ณผ ๊ณต์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ฒ„๊ฐ€ ์žˆ๋Š”๋ฐ ์ž๊พธ ์ด์œ ์—†์ด ๋ฌด์ž‘์œ„๋กœ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ฃฝ๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ–ˆ๋‹ค.

๊ฐ ํ”„๋กœ์ ํŠธ์˜ ๋กœ๊ทธ๋ฅผ ๋ด๋„ ํŠน๋ณ„ํ•˜๊ฒŒ ๋‚จ์•„์žˆ๋Š”๊ฒŒ ์—†์—ˆ๊ณ .. ์›์ธ์€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ๋ฆฌ๋ˆ…์Šค ์ปค๋„์ด OOM Killer ์ž‘์—…์„ ์‹คํ–‰ํ•œ ๊ฒƒ!

 

 OOM Killer๋ž€ ? 

OOM : Out of Memory

๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ์‹œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ™•๋ณดํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์œผ๋กœ ํ”„๋กœ์„ธ์Šค์˜ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์‹œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•œ ์ƒํ™ฉ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด linux kernel์ด OOM Killer๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. (์ ์ˆ˜๋ฅผ ๋งค๊ฒจ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์—ฌ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ™•๋ณดํ•จ)

 

 ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒ์‹œํ‚ค๋Š” ์ˆœ์œ„ 

oom_badnesss() ๋ฉ”์†Œ๋“œ์—์„œ ํ”„๋กœ์„ธ์Šค๋ณ„ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์„ ํƒ
OOM Killer๋Š” ์ ์ˆ˜๋ฅผ ๋งค๊ฒจ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ํ”„๋กœ์„ธ์Šค๋ถ€ํ„ฐ kill ์‹œํ‚ค๋Š”๋ฐ ์ด๋ฅผ 'OOM Scoring' ์ด๋ผ๊ณ  ํ•œ๋‹ค.

ํ”„๋กœ์„ธ์Šค์˜ ์ ์ˆ˜๋Š” /proc/<pid>/oom_score ํŒŒ์ผ์—์„œ ์ง์ ‘ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ ์ˆ˜๋ฅผ ๋งค๊ธฐ๋Š” ๋กœ์ง์€ ์ปค๋„ ์ฝ”๋“œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์•„๋ž˜ ์ฃผ์„์œผ๋กœ OOM Killer์˜  ๋ชฉํ‘œ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

/*
 * oom_badness - calculate a numeric value for how bad this task has been
 * @p: task struct of which task we should calculate
 * @p: current uptime in seconds
 *
 * The formula used is relatively simple and documented inline in the
 * function. The main rationale is that we want to select a good task
 * to kill when we run out of memory.
 *
 * Good in this context means that:
 * 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)
 */

1) ์™„๋ฃŒ๋œ ์ž‘์—…์˜ ์ˆ˜๋ฅผ ์ตœ๋Œ€๋กœ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
2) ๋งŽ์€ ์–‘์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ณต๊ตฌํ•œ๋‹ค.
3) ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ์†Œ๋น„ํ•˜์ง€ ์•Š๋Š”(Leak์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์€) ํ”„๋กœ์„ธ์Šค๋Š” ์ฃฝ์ด์ง€ ์•Š๋Š”๋‹ค.
4) ์ตœ์†Œํ•œ์˜ ํ”„๋กœ์„ธ์Šค๋งŒ ํฌ์ƒ์‹œํ‚จ๋‹ค.
5) ์‚ฌ์šฉ์ž๊ฐ€ ํŠน๋ณ„ํžˆ ์ง€์ •ํ•œ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์ธ๋‹ค.

 

 Kill ๋Œ€์ƒ์—์„œ ์ œ์™ธ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ• 

OOM Killer๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•˜๋Š” ๊ฒƒ์„ ๋ถˆ๊ฐ€๋Šฅํ•˜์ง€๋งŒ scoring ๋Œ€์ƒ์—์„œ ๋ฒ—์–ด๋‚˜๊ฒŒํ•˜๋Š” ๊ฒƒ(์šฐ์„ ์ˆœ์œ„์—์„œ ๋ฐ€๋ ค๋‚˜๊ฒŒํ•˜๋Š” ๊ฒƒ)์€ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋”ฐ๋ผ์„œ ์ข…๋ฃŒ๋˜๋ฉด ์•ˆ๋˜๋Š” ํ”„๋กœ์„ธ์Šค๋Š” ์ œ์™ธ์‹œ์ผœ์•ผ ํ•˜๋Š”๋ฐ ๋น„๊ต์  ๊ฐ„๋‹จํ•œ๋ฐ ๋จผ์ € ํŠน์ • ํ”„๋กœ์„ธ์Šค์˜ PID๋ฅผ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

 

๋ฐฉ๋ฒ• 1

/proc/<pid>/oom_adj ํŒŒ์ผ์— -17์„ ์ž…๋ ฅํ•œ๋‹ค. oom_adj๋Š” -17~ 15 ๋ฒ”์œ„์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ ๋‚ฎ์€ ๊ฐ’์ผ์ˆ˜๋ก ์šฐ์„ ์ˆœ์œ„์—์„œ ๋ฐ€๋ ค๋‚œ๋‹ค.

$ echo -17 > /proc/<pid>/oom_adj

 

๋ฐฉ๋ฒ• 2

/proc/<pid>/oom_score_adj ํŒŒ์ผ์— -1000์„ ์ž…๋ ฅํ•œ๋‹ค. oom_score_adj๋Š” -1000~1000 ๋ฒ”์œ„์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ ๋‚ฎ์€ ๊ฐ’์ผ์ˆ˜๋ก ์šฐ์„ ์ˆœ์œ„์—์„œ ๋ฐ€๋ ค๋‚œ๋‹ค.

$ echo -1000 > /proc/<pid>/oom_score_adj

 

๋กœ๊ทธ ์œ„์น˜

/var/log/messages

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€