網站首頁 編程語言 正文
正文
golang版本:1.16
背景:golang進程在docker中運行,因為使用內存較多,經常在內存未達到docker上限時,就被oom-kill,為了避免程序頻繁被殺,在docker啟動時禁用了oom-kill,但是出現了新的問題。
現象:docker內存用滿后,golang進程hang住,無任何響應(沒有額外內存系統無法分配新的fd,無法服務),即使在程序內置了內存達到上限就重啟,也不會生效,只能kill
因為pprof查看進程內存有很多是能在gc時釋放的,起初懷疑是golang進程問題
在hang住之前,先登錄到docker上,寫一個golang測試程序,只申請一小段內存后sleep,啟動時加GODEBUG=GCTRACE=1打印gc信息,發現mark 階段stw耗時達到31s(31823+15+0.11 ms對應STW Mark Prepare,Concurrent Marking,STW Mark Termination)
懷疑是不是申請內存失敗后,沒有觸發oom退出。在golang標準庫中查看oom相關的邏輯
mgcwork.go:374
if s == nil { systemstack(func() { s = mheap_.allocManual(workbufAlloc/pageSize, spanAllocWorkBuf) }) if s == nil { throw("out of memory") } // Record the new span in the busy list. lock(&work.wbufSpans.lock) work.wbufSpans.busy.insert(s) unlock(&work.wbufSpans.lock) }
mheap分配內存使用了mmap,繼續懷疑是mmap返回的錯誤碼在docker中不是非0
func sysMap(v unsafe.Pointer, n uintptr, sysStat *sysMemStat) { sysStat.add(int64(n)) p, err := mmap(v, n, _PROT_READ| _PROT_WRITE, _MAP_ANON| _MAP_FIXED| _MAP_PRIVATE, -1, 0) if err == _ENOMEM { throw("runtime: out of memory") } if p != v || err != 0 { throw("runtime: cannot map pages in arena address space") } }
為了對比驗證,用c寫一段調用mmap的代碼,在同一個docker中同時跑看下
#include <sys/mman.h> #include <unistd.h> #include <errno.h> #include <stdio.h> #define BUF_SIZE 393216 void main() { char *addr; int i; for(i=0;i<1000000;i++) { addr = (char *)mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (addr != MAP_FAILED) { addr[0] = 'a'; addr[BUF_SIZE-1] = 'b'; printf("i:%d, sz: %d, addr[0]: %c, addr[-1]: %c\n", i, BUF_SIZE, addr[0], addr[BUF_SIZE-1]); munmap(addr, BUF_SIZE); } else { printf("error no: %d\n", errno); } usleep(1000000); } }
mmap沒有失敗,而且同樣會hang住,說明不是golang機制的問題,應該是阻塞在了系統調用上。查看調用堆棧,發現是hang在了cgroup中
[<ffffffff81224d65>] mem_cgroup_oom_synchronize+0x275/0x340 [<ffffffff811a068f>] pagefault_out_of_memory+0x2f/0x74 [<ffffffff81066bed>] __do_page_fault+0x4bd/0x4f0 [<ffffffff81801605>] async_page_fault+0x45/0x50 [<ffffffffffffffff>] 0xffffffffffffffff
查看go程序,也有相同的調用堆棧
[<ffffffff81103681>] futex_wait_queue_me+0xc1/0x120 [<ffffffff81104086>] futex_wait+0xf6/0x250 [<ffffffff8110647b>] do_futex+0x2fb/0xb20 [<ffffffff81106d1a>] SyS_futex+0x7a/0x170 [<ffffffff81003948>] do_syscall_64+0x68/0x100 [<ffffffff81800081>] entry_SYSCALL_64_after_hwframe+0x3d/0xa2 [<ffffffffffffffff>] 0xffffffffffffffff [<ffffffff810f3ffe>] hrtimer_nanosleep+0xce/0x1e0 [<ffffffff810f419b>] SyS_nanosleep+0x8b/0xa0 [<ffffffff81003948>] do_syscall_64+0x68/0x100 [<ffffffff81800081>] entry_SYSCALL_64_after_hwframe+0x3d/0xa2 [<ffffffffffffffff>] 0xffffffffffffffff [<ffffffff81224c5a>] mem_cgroup_oom_synchronize+0x16a/0x340 [<ffffffff811a068f>] pagefault_out_of_memory+0x2f/0x74 [<ffffffff81066bed>] __do_page_fault+0x4bd/0x4f0 [<ffffffff81801605>] async_page_fault+0x45/0x50 [<ffffffffffffffff>] 0xffffffffffffffff [<ffffffff81224c5a>] mem_cgroup_oom_synchronize+0x16a/0x340 [<ffffffff811a068f>] pagefault_out_of_memory+0x2f/0x74 [<ffffffff81066bed>] __do_page_fault+0x4bd/0x4f0 [<ffffffff81801605>] async_page_fault+0x45/0x50 [<ffffffffffffffff>] 0xffffffffffffffff [<ffffffff81224c5a>] mem_cgroup_oom_synchronize+0x16a/0x340 [<ffffffff811a068f>] pagefault_out_of_memory+0x2f/0x74 [<ffffffff81066bed>] __do_page_fault+0x4bd/0x4f0 [<ffffffff81801605>] async_page_fault+0x45/0x50 [<ffffffffffffffff>] 0xffffffffffffffff
看了下cgroup內存控制的代碼,策略是沒有可用內存并且未配置oom kill的程序,會鎖在一個等待隊列里,當有可用內存時再從隊首喚醒。這個邏輯沒辦法通過配置或者其他方式繞過去。
elixir.bootlin.com/linux/v4.14…
/** * mem_cgroup_oom_synchronize - complete memcg OOM handling * @handle: actually kill/wait or just clean up the OOM state * * This has to be called at the end of a page fault if the memcg OOM * handler was enabled. * * Memcg supports userspace OOM handling where failed allocations must * sleep on a waitqueue until the userspace task resolves the * situation. Sleeping directly in the charge context with all kinds * of locks held is not a good idea, instead we remember an OOM state * in the task and mem_cgroup_oom_synchronize() has to be called at * the end of the page fault to complete the OOM handling. * * Returns %true if an ongoing memcg OOM situation was detected and * completed, %false otherwise. */ bool mem_cgroup_oom_synchronize(bool handle) { struct mem_cgroup *memcg = current->memcg_in_oom; struct oom_wait_info owait; bool locked; /* OOM is global, do not handle */ if (!memcg) return false; if (!handle) goto cleanup; owait.memcg = memcg; owait.wait.flags = 0; owait.wait.func = memcg_oom_wake_function; owait.wait.private = current; INIT_LIST_HEAD(&owait.wait.entry); prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); mem_cgroup_mark_under_oom(memcg); locked = mem_cgroup_oom_trylock(memcg); if (locked) mem_cgroup_oom_notify(memcg); if (locked && !memcg->oom_kill_disable) { mem_cgroup_unmark_under_oom(memcg); finish_wait(&memcg_oom_waitq, &owait.wait); mem_cgroup_out_of_memory(memcg, current->memcg_oom_gfp_mask, current->memcg_oom_order); } else { schedule(); mem_cgroup_unmark_under_oom(memcg); finish_wait(&memcg_oom_waitq, &owait.wait); } if (locked) { mem_cgroup_oom_unlock(memcg); /* * There is no guarantee that an OOM-lock contender * sees the wakeups triggered by the OOM kill * uncharges. Wake any sleepers explicitly. */ memcg_oom_recover(memcg); } cleanup: current->memcg_in_oom = NULL; css_put(&memcg->css); return true; }
結論:
docker內存耗光后,golang在gc的mark階段,需要申請新的內存記錄被標記的對象時,需要調用mmap,因為沒有可用內存,就會被hang在cgroup中,gc無法完成也就無法釋放內存,就會導致golang程序一直在stw階段,無法對外服務,即使壓力下降也無法恢復。最好還是不要關閉docker的oom-kill
原文鏈接:https://juejin.cn/post/7153523299668983815
相關推薦
- 2022-07-12 Docker-swarm快速搭建redis集群的方法步驟_docker
- 2022-07-06 C語言超詳細講解字符串函數和內存函數_C 語言
- 2022-07-30 C語言數組長度的計算方法實例總結(sizeof與strlen)_C 語言
- 2022-07-07 淺談Redis的異步機制_Redis
- 2022-03-03 element樹組件父子關聯
- 2022-07-28 C++超詳細講解強制類型轉換_C 語言
- 2022-12-27 React組件間通訊傳值實現詳解_React
- 2024-03-19 Rust 中Self 關鍵字的兩種不同用法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支