]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: use hw.ncpuonline sysctl in getncpu on openbsd
authorTobias Klauser <tklauser@distanz.ch>
Fri, 8 Feb 2019 08:25:05 +0000 (09:25 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 26 Feb 2019 22:56:59 +0000 (22:56 +0000)
The number of CPUs reported by the hw.ncpu sysctl is twice as high as
the actual number of CPUs running on OpenBSD 6.4. with hyperthreading
disabled (hw.smt=0). Try hw.cpuonline first and fall back to hw.ncpu
in case it fails (which is the case on older OpenBSD before 6.4).

Fixes #30127

Change-Id: Id091234b8038cc9f7c40519d039fc1a05437c40d
Reviewed-on: https://go-review.googlesource.com/c/161757
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/runtime/os_openbsd.go

index 96112cb25b778ed7ae7f7730182048b296496623..353a5d94ba077ab1867d2a157464f2ff57d9620e 100644 (file)
@@ -84,9 +84,10 @@ const (
        _CTL_KERN   = 1
        _KERN_OSREV = 3
 
-       _CTL_HW      = 6
-       _HW_NCPU     = 3
-       _HW_PAGESIZE = 7
+       _CTL_HW        = 6
+       _HW_NCPU       = 3
+       _HW_PAGESIZE   = 7
+       _HW_NCPUONLINE = 25
 )
 
 func sysctlInt(mib []uint32) (int32, bool) {
@@ -100,9 +101,14 @@ func sysctlInt(mib []uint32) (int32, bool) {
 }
 
 func getncpu() int32 {
-       // Fetch hw.ncpu via sysctl.
-       if ncpu, ok := sysctlInt([]uint32{_CTL_HW, _HW_NCPU}); ok {
-               return int32(ncpu)
+       // Try hw.ncpuonline first because hw.ncpu would report a number twice as
+       // high as the actual CPUs running on OpenBSD 6.4 with hyperthreading
+       // disabled (hw.smt=0). See https://golang.org/issue/30127
+       if n, ok := sysctlInt([]uint32{_CTL_HW, _HW_NCPUONLINE}); ok {
+               return int32(n)
+       }
+       if n, ok := sysctlInt([]uint32{_CTL_HW, _HW_NCPU}); ok {
+               return int32(n)
        }
        return 1
 }