亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

golang進(jìn)程在docker中OOM后hang住問題解析

 更新時間:2022年10月21日 09:31:11   作者:硅基生命  
這篇文章主要介紹了golang進(jìn)程在docker中OOM后hang住問題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

golang版本:1.16

背景:golang進(jìn)程在docker中運行,因為使用內(nèi)存較多,經(jīng)常在內(nèi)存未達(dá)到docker上限時,就被oom-kill,為了避免程序頻繁被殺,在docker啟動時禁用了oom-kill,但是出現(xiàn)了新的問題。

現(xiàn)象:docker內(nèi)存用滿后,golang進(jìn)程hang住,無任何響應(yīng)(沒有額外內(nèi)存系統(tǒng)無法分配新的fd,無法服務(wù)),即使在程序內(nèi)置了內(nèi)存達(dá)到上限就重啟,也不會生效,只能kill

因為pprof查看進(jìn)程內(nèi)存有很多是能在gc時釋放的,起初懷疑是golang進(jìn)程問題

在hang住之前,先登錄到docker上,寫一個golang測試程序,只申請一小段內(nèi)存后sleep,啟動時加GODEBUG=GCTRACE=1打印gc信息,發(fā)現(xiàn)mark 階段stw耗時達(dá)到31s(31823+15+0.11 ms對應(yīng)STW Mark Prepare,Concurrent Marking,STW Mark Termination)

懷疑是不是申請內(nèi)存失敗后,沒有觸發(fā)oom退出。在golang標(biāo)準(zhǔn)庫中查看oom相關(guān)的邏輯

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分配內(nèi)存使用了mmap,繼續(xù)懷疑是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寫一段調(diào)用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機(jī)制的問題,應(yīng)該是阻塞在了系統(tǒng)調(diào)用上。查看調(diào)用堆棧,發(fā)現(xiàn)是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程序,也有相同的調(diào)用堆棧

[<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內(nèi)存控制的代碼,策略是沒有可用內(nèi)存并且未配置oom kill的程序,會鎖在一個等待隊列里,當(dāng)有可用內(nèi)存時再從隊首喚醒。這個邏輯沒辦法通過配置或者其他方式繞過去。

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;
}

結(jié)論:

docker內(nèi)存耗光后,golang在gc的mark階段,需要申請新的內(nèi)存記錄被標(biāo)記的對象時,需要調(diào)用mmap,因為沒有可用內(nèi)存,就會被hang在cgroup中,gc無法完成也就無法釋放內(nèi)存,就會導(dǎo)致golang程序一直在stw階段,無法對外服務(wù),即使壓力下降也無法恢復(fù)。最好還是不要關(guān)閉docker的oom-kill

以上就是golang進(jìn)程在docker中OOM后hang住問題解析的詳細(xì)內(nèi)容,更多關(guān)于golang進(jìn)程docker OOM hang的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解Golang time包中的time.Duration類型

    詳解Golang time包中的time.Duration類型

    在日常開發(fā)過程中,會頻繁遇到對時間進(jìn)行操作的場景,使用 Golang 中的 time 包可以很方便地實現(xiàn)對時間的相關(guān)操作,本文講解一下 time 包中的 time.Duration 類型,需要的朋友可以參考下
    2023-07-07
  • Go中阻塞以及非阻塞操作實現(xiàn)(Goroutine和main Goroutine)

    Go中阻塞以及非阻塞操作實現(xiàn)(Goroutine和main Goroutine)

    本文主要介紹了Go中阻塞以及非阻塞操作實現(xiàn)(Goroutine和main Goroutine),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • Go語言開發(fā)技巧必知的小細(xì)節(jié)提升效率

    Go語言開發(fā)技巧必知的小細(xì)節(jié)提升效率

    這篇文章主要介紹了Go語言開發(fā)技巧必知的小細(xì)節(jié)提升效率,分享幾個你可能不知道的Go語言小細(xì)節(jié),希望能幫助大家更好地學(xué)習(xí)這門語言
    2024-01-01
  • Golang?Fasthttp選擇使用slice而非map?存儲請求數(shù)據(jù)原理探索

    Golang?Fasthttp選擇使用slice而非map?存儲請求數(shù)據(jù)原理探索

    本文將從簡單到復(fù)雜,逐步剖析為什么?Fasthttp?選擇使用?slice?而非?map,并通過代碼示例解釋這一選擇背后高性能的原因,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-02-02
  • Go語言中的Base64編碼原理介紹以及使用

    Go語言中的Base64編碼原理介紹以及使用

    Base64是網(wǎng)絡(luò)上最常見的用于傳輸8Bit字節(jié)代碼的編碼方式之一,可用于在HTTP環(huán)境下傳遞較長的標(biāo)識信息,下面這篇文章主要給大家介紹了關(guān)于Go語言中的Base64編碼原理介紹以及使用的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • 如何通過go自定義一個分頁插件

    如何通過go自定義一個分頁插件

    分頁是我們?nèi)粘i_發(fā)中經(jīng)常會遇到的一個需求,下面這篇文章主要給大家介紹了關(guān)于如何通過go自定義一個分頁插件的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • Go 實現(xiàn)一次性打包各個平臺的可執(zhí)行程序

    Go 實現(xiàn)一次性打包各個平臺的可執(zhí)行程序

    這篇文章主要介紹了Go 實現(xiàn)一次性打包各個平臺的可執(zhí)行程序,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Go語言使用AES加密解密的示例代碼

    Go語言使用AES加密解密的示例代碼

    這篇文章主要介紹了Go語言使用AES加密解密的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • Golang中crypto/cipher加密標(biāo)準(zhǔn)庫全面指南

    Golang中crypto/cipher加密標(biāo)準(zhǔn)庫全面指南

    本文主要介紹了Golang中crypto/cipher加密標(biāo)準(zhǔn)庫,包括對稱加密、非對稱加密以及使用流加密和塊加密算法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-02-02
  • Go中RPC遠(yuǎn)程過程調(diào)用的實現(xiàn)

    Go中RPC遠(yuǎn)程過程調(diào)用的實現(xiàn)

    本文主要介紹了Go中RPC遠(yuǎn)程過程調(diào)用的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評論