Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: go uses too many CPUs on OpenBSD #30127

tedu opened this issue Feb 8, 2019 · 4 comments

runtime: go uses too many CPUs on OpenBSD #30127

tedu opened this issue Feb 8, 2019 · 4 comments


Copy link

@tedu tedu commented Feb 8, 2019

What version of Go are you using (go version)?


Does this issue reproduce with the latest release?


What operating system and processor architecture are you using (go env)?

OpenBSD 6.4 and later.

What did you do?

By default, OpenBSD has hw.smt=0 (hyperthreading disabled) which causes hw.ncpu to be twice as high as the actual CPUs running, hw.cpuonline. Go tries to schedule this many threads and they end up fighting each other.

See also:

The diff below, against go/src/runtime, changes to use the number of CPUs
online. It's possible for this number to change, and thus become stale, but
that's unlikely, and not the default. But at least it's less wrong.

(This sysctl was added in 6.4.)

Apologies for pasted patch. I think it conveys the idea. I'm not sure how you want to handle backwards compatibility.

--- os_openbsd.go.orig	Fri Feb  8 00:02:27 2019
+++ os_openbsd.go	Fri Feb  8 00:06:21 2019
@@ -85,8 +85,8 @@
 	_CTL_HW      = 6
-	_HW_NCPU     = 3
 func sysctlInt(mib []uint32) (int32, bool) {
@@ -101,7 +101,7 @@
 func getncpu() int32 {
 	// Fetch hw.ncpu via sysctl.
-	if ncpu, ok := sysctlInt([]uint32{_CTL_HW, _HW_NCPU}); ok {
+	if ncpu, ok := sysctlInt([]uint32{_CTL_HW, _HW_NCPUONLINE}); ok {
 		return int32(ncpu)
 	return 1
@mikioh mikioh changed the title go uses too many CPUs on OpenBSD runtime: go uses too many CPUs on OpenBSD Feb 8, 2019
Copy link

@mikioh mikioh commented Feb 8, 2019

As far as I can see, the function hw_sysctl in sys/kern/kern_sysctl.c returns an error for unknown state. It`s fine to test the kernel states one after another, HW_NCPUONLINE then HW_NCPU.

Copy link

@gopherbot gopherbot commented Feb 8, 2019

Change mentions this issue: runtime: use hw.ncpuonline sysctl in getncpu on openbsd

@bradfitz bradfitz added this to the Go1.13 milestone Feb 9, 2019
Copy link

@bradfitz bradfitz commented Feb 9, 2019

/cc @mdempsky

Copy link

@mdempsky mdempsky commented Feb 9, 2019

Checking hw.ncpuonline and then falling back to hw.ncpu for 6.2 compat sgtm.

@gopherbot gopherbot closed this in c55eeeb Feb 26, 2019
@golang golang locked and limited conversation to collaborators Feb 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.