Go 終于要解決容器化下 GOMAXPROCS 的問題了!
大家好,我是煎魚。
在如今,云原生浪潮已經(jīng)掀起了很多年,Kubernetes+Docker 的組合已經(jīng)占據(jù)主流。Go 這一門編程語言,有一部分是借助云原生起家的。
不過 Go 在此一直有一個 “坑“,多年以來一直靠著第三方包來解決。最近終于有了一些新進(jìn)展。
問題背景
在《runtime: make GOMAXPROCS cfs-aware on GOOS=linux[1]》中有明確到這個現(xiàn)象的表現(xiàn)是:
圖片
在 runtime.GOMAXPROCS()
中,其默認(rèn)設(shè)置是采用操作系統(tǒng)可見的處理器數(shù)量。
因此可能存在與容器 CPU 配額(例如通過 Docker CFS
帶寬控制實(shí)現(xiàn)的配額)存在嚴(yán)重數(shù)值偏差的情況。
這種情況可能導(dǎo)致程序出現(xiàn)顯著的延遲異常,特別是在以下兩類場景:
- 峰值負(fù)載期間
- 后臺 GC 階段占滿所有處理器時
現(xiàn)實(shí)情況
在現(xiàn)實(shí)的生產(chǎn)使用中,很多同學(xué)是遇到問題后,要去排查 GC 等。才發(fā)現(xiàn)的這個 “坑”。
因此如果我們關(guān)心應(yīng)用程序的性能延遲、可靠性等,正確的做法是通過設(shè)置:GOMAXPROCS=max(1,floor(cpu_quota))
,永遠(yuǎn)不要超過你的 CPU 配額。
我們最常用的 uber-go/automaxprocs 中的實(shí)現(xiàn) internal/runtime/cpu_quota_linux.go[2]:
// CPUQuotaToGOMAXPROCS converts the CPU quota applied to the calling process
// to a valid GOMAXPROCS value. The quota is converted from float to int using round.
// If round == nil, DefaultRoundFunc is used.
func CPUQuotaToGOMAXPROCS(minValue int, round func(v float64) int) (int, CPUQuotaStatus, error) {
if round == nil {
round = DefaultRoundFunc
}
cgroups, err := _newQueryer()
if err != nil {
return-1, CPUQuotaUndefined, err
}
quota, defined, err := cgroups.CPUQuota()
if !defined || err != nil {
return-1, CPUQuotaUndefined, err
}
maxProcs := round(quota)
if minValue > 0 && maxProcs < minValue {
return minValue, CPUQuotaMinUsed, nil
}
return maxProcs, CPUQuotaUsed, nil
}
將此作為 GOMAXPROCS
的默認(rèn)設(shè)置將讓 Go 程序變得可靠。這也是大家的微服務(wù)應(yīng)用都會用 uber-go/automaxprocs[3] 來做兜底的原因之一。
新提案
背景
在前幾周 Go 團(tuán)隊(duì)成員 @Michael Pratt 提出了新的提案《proposal: runtime: CPU limit-aware GOMAXPROCS default[4]》:
圖片
該提案的目的是:希望修改 Linux 上的 Go runtime(運(yùn)行時)機(jī)制,使用 CPU cgroup 配額限制(注意是:pod cpu limit 場景)來設(shè)置 GOMAXPROCS 的默認(rèn)值。
以此解決我們前面提到在云原生場景下的問題。
實(shí)施方向
具體計劃實(shí)施的方向如下:
1、層級化優(yōu)先級:
物理機(jī) → CPU 親和性 → cgroup 限制,逐層收斂最終值。
通過 min()
確保不超額使用資源(避免容器環(huán)境中的 CPU
節(jié)流)。
2、cgroup 適配:
對 cgroup v1/v2 的差異封裝統(tǒng)一接口。
遍歷 cgroup 層級時需處理 cpu 子系統(tǒng)的掛載點(diǎn)(可能分布在/sys/fs/cgroup 的不同路徑)
3、自動更新機(jī)制:通過后臺 goroutine 定期(如每 10 秒)檢查以下指標(biāo)并重新設(shè)置,偽代碼如下:
func monitorCPULimit() {
for {
newLimit := calculateCPULimit()
if newLimit != currentGOMAXPROCS {
runtime.SetDefaultGOMAXPROCS()
}
time.Sleep(10 * time.Second)
}
}
4、兼容性保障:本次兼容性保障將由 GODEBUG cgroupgomaxprocs=1
控制。對于較舊的語言版本,默認(rèn)值為 cgroupgomaxprocs=0
。因此,只有在升級 Go 語言版本時,行為才會發(fā)生變化,而不是在升級 go tool 工具鏈時。
文檔更新
本次變更后將會對現(xiàn)有的 GOMAXPROCS 有一定的改變,具體如下:
// GOMAXPROCS sets the maximum number of CPUs that can be executing
// simultaneously and returns the previous setting. If n < 1, it does not change
// the current setting.
//
// If the GOMAXPROCS environment variable is set to a positive whole number,
// GOMAXPROCS defaults to that value.
//
// Otherwise, the Go runtime selects an appropriate default value based on the
// number of logical CPUs on the machine, the process’s CPU affinity mask, and,
// on Linux, the process’s average CPU throughput limit based on cgroup CPU
// quota, if any.
//
// The Go runtime periodically updates the default value based on changes to
// the total logical CPU count, the CPU affinity mask, or cgroup quota. Setting
// a custom value with the GOMAXPROCS environment variable or by calling
// GOMAXPROCS disables automatic updates. The default value and automatic
// updates can be restored by calling [SetDefaultGOMAXPROCS].
//
// If GODEBUG=cgroupgomaxprocs=0 is set, GOMAXPROCS defaults to the value of
// [runtime.NumCPU] and does not perform automatic updating.
//
// The default GOMAXPROCS behavior may change as the scheduler improves.
func GOMAXPROCS(n int) int
// SetDefaultGOMAXPROCS updates the GOMAXPROCS setting to the runtime
// default, as described by [GOMAXPROCS], ignoring the GOMAXPROCS
// environment variable.
//
// SetDefaultGOMAXPROCS can be used to enable the default automatic updating
// GOMAXPROCS behavior if it has been disabled by the GOMAXPROCS
// environment variable or a prior call to [GOMAXPROCS], or to force an immediate
// update if the caller is aware of a change to the total logical CPU count, CPU
// affinity mask or cgroup quota.
func SetDefaultGOMAXPROCS()
總結(jié)
本提案作者計劃如果該提案被接受,將會馬上在 Go1.25 中實(shí)現(xiàn)和實(shí)施這份規(guī)劃。一旦實(shí)施,對于 Go 開發(fā)者來講是一個不錯的消息。
因?yàn)榧词乖?2025 年的現(xiàn)在,即使 Go 最大客群之一是云原生。但這個問題本身仍然還沒有被解決,大家還是靠 uber-go/automaxprocs 的庫為主。
但需要注意,本次修改主要還是針對 pod limit 的場景,而不是 pod request 的場景。本質(zhì)上還是不一樣的。
參考資料
[1] runtime: make GOMAXPROCS cfs-aware on GOOS=linux: https://github.com/golang/go/issues/33803
[2] internal/runtime/cpu_quota_linux.go: https://github.com/uber-go/automaxprocs/blob/master/internal/runtime/cpu_quota_linux.go#L35
[3] uber-go/automaxprocs: https://github.com/uber-go/automaxprocs
[4] proposal: runtime: CPU limit-aware GOMAXPROCS default: https://github.com/golang/go/issues/73193