perf: auto set runtime maxprocs in docker #1339
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
summary
加入在容器环境中配置准确的并行度,go 的 runtime.maxprocs 默认读取的是宿主机的 cpu 配置,那么会存在一个问题。 服务 pod 通过 resource limit 配置了资源限制,比如物理机是 128 核心,而 pod 限定到 8 个核心,但 go init 实例化 runtime.maxprocs 时,按照物理机 cpu 资源配置,也就是会配置 128 个 gmp'p ,一定量级下,128 个线程想要并行处理,但 pod 的cpu为 8,如何让 128 个线程都得到运行 ? 时间片切得更细,让其在一个时间窗口内都得到 cpu 调度。使这些线程因为时间片到期,强制来回上下文切换,开销是不小的。
建议加上这个参数,尤其物理机 cpu 和 resource limit 差距较大时,服务的长尾很明显的。
几年前我做行情推送时,有遇到相关问题。另外,对该问题,写过一篇文章,大家可以参考下,https://xiaorui.cc/archives/6334