Sphere
Log in Join
☆ 5

GOMAXPROCS and a CPU-bound job on a 32-core box

We have a CPU-heavy batch job running in a container on a 32-core host. Go 1.22 defaults GOMAXPROCS to 32, but the container has a 4-core CPU quota. We're seeing tons of context switching and worse throughput than expected. Should I set GOMAXPROCS manually? I'd like to understand the mechanism before I go changing settings again, since the job behaves differently on my machine.

⚑ Report

4 replies

☆ 0
R redis ·2026-10-10#1

Go 1.25 added automatic container-aware GOMAXPROCS, but on 1.22 you're right that it reads the host's CPU count, not the cgroup quota. Setting GOMAXPROCS=4 to match the quota usually helps a lot by reducing scheduler overhead. You can also use the go.uber.org/automaxprocs package to read the cgroup limit at startup.

⚑ Report
☆ 0
P proto ·2026-10-18#2

I'll try GOMAXPROCS=4. Is there a downside to setting it below the quota, like 3, to leave room for the runtime and other processes? I'll measure with schedtrace before and after so I have numbers rather than a vibe. The 32-versus-4 mismatch explains a lot about why this job behaved worse in the container than on my laptop.

⚑ Report
☆ 0
L loki ·2026-10-22#3

Slightly counterintuitive: GOMAXPROCS counts OS threads executing Go code, not total system CPUs. It doesn't reserve cores for other processes. For a pure CPU-bound job set it at or just under the quota. If you share the box with other workloads, then leave headroom. Measure with GODEBUG=schedtrace=1000.

⚑ Report
☆ 0
R rust88 ·2026-10-22#4

Set GOMAXPROCS=4 to match the cgroup quota and throughput improved noticeably, fewer goroutine migrations. schedtrace confirmed the scheduler was much calmer. automaxprocs is on the list for when we upgrade past 1.24. Take it with a grain of salt. That has been my experience anyway.

⚑ Report
Log in to reply.