成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Go 終于要解決容器化下 GOMAXPROCS 的問題了!

開發 前端
本提案作者計劃如果該提案被接受,將會馬上在 Go1.25 中實現和實施這份規劃。一旦實施,對于 Go 開發者來講是一個不錯的消息。

大家好,我是煎魚。

在如今,云原生浪潮已經掀起了很多年,Kubernetes+Docker 的組合已經占據主流。Go 這一門編程語言,有一部分是借助云原生起家的。

不過 Go 在此一直有一個 “坑“,多年以來一直靠著第三方包來解決。最近終于有了一些新進展。

問題背景

在《runtime: make GOMAXPROCS cfs-aware on GOOS=linux[1]》中有明確到這個現象的表現是:

圖片圖片

在 runtime.GOMAXPROCS() 中,其默認設置是采用操作系統可見的處理器數量。

因此可能存在與容器 CPU 配額(例如通過 Docker CFS 帶寬控制實現的配額)存在嚴重數值偏差的情況。

這種情況可能導致程序出現顯著的延遲異常,特別是在以下兩類場景:

  1. 峰值負載期間
  2. 后臺 GC 階段占滿所有處理器時

現實情況

在現實的生產使用中,很多同學是遇到問題后,要去排查 GC 等。才發現的這個 “坑”。

因此如果我們關心應用程序的性能延遲、可靠性等,正確的做法是通過設置:GOMAXPROCS=max(1,floor(cpu_quota)),永遠不要超過你的 CPU 配額。

我們最常用的 uber-go/automaxprocs 中的實現 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 的默認設置將讓 Go 程序變得可靠。這也是大家的微服務應用都會用 uber-go/automaxprocs[3] 來做兜底的原因之一。

新提案

背景

在前幾周 Go 團隊成員 @Michael Pratt 提出了新的提案《proposal: runtime: CPU limit-aware GOMAXPROCS default[4]》:

圖片圖片

該提案的目的是:希望修改 Linux 上的 Go runtime(運行時)機制,使用 CPU cgroup 配額限制(注意是:pod cpu limit 場景)來設置 GOMAXPROCS 的默認值。

以此解決我們前面提到在云原生場景下的問題。

實施方向

具體計劃實施的方向如下:

1、層級化優先級:

物理機 → CPU 親和性 → cgroup 限制,逐層收斂最終值。

通過 min() 確保不超額使用資源(避免容器環境中的 CPU 節流)。

2、cgroup 適配:

     對 cgroup v1/v2 的差異封裝統一接口。

     遍歷 cgroup 層級時需處理 cpu 子系統的掛載點(可能分布在/sys/fs/cgroup 的不同路徑)

3、自動更新機制:通過后臺 goroutine 定期(如每 10 秒)檢查以下指標并重新設置,偽代碼如下:

func monitorCPULimit() {
    for {
        newLimit := calculateCPULimit()
        if newLimit != currentGOMAXPROCS {
            runtime.SetDefaultGOMAXPROCS()
        }
        time.Sleep(10 * time.Second)
    }
}

4、兼容性保障:本次兼容性保障將由 GODEBUG cgroupgomaxprocs=1 控制。對于較舊的語言版本,默認值為 cgroupgomaxprocs=0。因此,只有在升級 Go 語言版本時,行為才會發生變化,而不是在升級 go tool 工具鏈時。

文檔更新

本次變更后將會對現有的 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()

總結

本提案作者計劃如果該提案被接受,將會馬上在 Go1.25 中實現和實施這份規劃。一旦實施,對于 Go 開發者來講是一個不錯的消息。

因為即使在 2025 年的現在,即使 Go 最大客群之一是云原生。但這個問題本身仍然還沒有被解決,大家還是靠 uber-go/automaxprocs 的庫為主。

但需要注意,本次修改主要還是針對 pod limit 的場景,而不是 pod request 的場景。本質上還是不一樣的。

參考資料

[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


責任編輯:武曉燕 來源: 腦子進了煎魚
相關推薦

2023-11-02 08:43:08

protocgo兼容

2021-03-05 14:40:49

Chrome瀏覽器內存

2016-09-23 11:08:35

前端Javascript模塊化

2023-07-13 09:13:18

Docker容器

2021-03-02 06:02:03

Kafka高并發系統

2022-12-05 10:30:30

Gossti漏洞

2016-06-05 17:35:44

容器/虛擬化/東網科技

2024-09-04 17:49:27

2012-05-16 13:43:20

操作系統故障檢修系統管理

2011-03-25 13:26:45

Cacti

2021-12-13 20:09:33

GoElasticsearJava

2022-11-08 08:29:43

Goslog 庫工具

2023-12-18 09:31:35

Go容器Linux

2022-04-01 15:43:52

iOS蘋果耗電

2018-11-22 15:07:17

代碼github程序

2025-06-11 02:33:00

2020-09-02 08:04:59

多線程互聯網高并發

2022-12-05 16:02:56

iOSiOS 16信號

2023-10-07 11:18:23

iOS 17蘋果

2022-12-01 16:01:39

iOSiOS 16信號
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲欧美在线免费观看 | 日韩精品在线免费观看视频 | 亚洲一区二区三区免费在线观看 | 综合久久综合久久 | 精品成人免费一区二区在线播放 | 国产精品久久久久久久久动漫 | 久久精品二区 | 国产乱码精品一区二区三区五月婷 | 中文字幕国产精品 | 欧美日韩三区 | 黄网站涩免费蜜桃网站 | 国产一卡二卡三卡 | 欧美自拍日韩 | 蜜臀久久 | 中文字幕在线一区二区三区 | 亚洲一区二区三区免费在线观看 | 国产成人麻豆免费观看 | 激情视频中文字幕 | 亚洲国产欧美91 | 337p日本欧洲亚洲大胆 | 亚洲一区 中文字幕 | 天天躁人人躁人人躁狂躁 | 69av在线视频| 成人精品一区二区三区 | 日本一区二区高清不卡 | 亚洲人在线播放 | 日韩不卡视频在线观看 | 欧美极品少妇xxxxⅹ免费视频 | 亚洲欧美中文日韩在线v日本 | 视频一区在线观看 | 欧美成人免费在线 | 懂色av色香蕉一区二区蜜桃 | 亚洲草草视频 | 亚洲视频一区在线 | 在线观看成人 | 青青草这里只有精品 | 狠狠干网站 | 日韩在线一区二区三区 | 成人国产精品久久 | 欧美精品一区三区 | 一级毛片免费 |