Skip to content

Commit a714470

Browse files
committed
runtime: allow more CPUs on FreeBSD
Currently the FreeBSD CPU affinity code assumes that the maximum GOMAXPROCS is 256, but we just removed that limit. This commit rewrites the FreeBSD CPU affinity code to raise the CPU count limit to 65,536, like the Linux CPU affinity code, and to degrade more gracefully if we do somehow go over that. Change-Id: Ic4ca7f88bd8b9448aae4dbd43ef21a6c1b8fea63 Reviewed-on: https://go-review.googlesource.com/66291 Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent e900e27 commit a714470

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/runtime/os_freebsd.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,19 @@ func sysctlnametomib(name []byte, mib *[_CTL_MAXNAME]uint32) uint32 {
6969
}
7070

7171
const (
72-
_CPU_SETSIZE_MAX = 32 // Limited by _MaxGomaxprocs(256) in runtime2.go.
7372
_CPU_CURRENT_PID = -1 // Current process ID.
7473
)
7574

7675
//go:noescape
7776
func cpuset_getaffinity(level int, which int, id int64, size int, mask *byte) int32
7877

78+
//go:systemstack
7979
func getncpu() int32 {
80-
var mask [_CPU_SETSIZE_MAX]byte
80+
// Use a large buffer for the CPU mask. We're on the system
81+
// stack, so this is fine, and we can't allocate memory for a
82+
// dynamically-sized buffer at this point.
83+
const maxCPUs = 64 * 1024
84+
var mask [maxCPUs / 8]byte
8185
var mib [_CTL_MAXNAME]uint32
8286

8387
// According to FreeBSD's /usr/src/sys/kern/kern_cpuset.c,
@@ -99,21 +103,20 @@ func getncpu() int32 {
99103
return 1
100104
}
101105

102-
size := maxcpus / _NBBY
103-
ptrsize := uint32(unsafe.Sizeof(uintptr(0)))
104-
if size < ptrsize {
105-
size = ptrsize
106+
maskSize := int(maxcpus+7) / 8
107+
if maskSize < sys.PtrSize {
108+
maskSize = sys.PtrSize
106109
}
107-
if size > _CPU_SETSIZE_MAX {
108-
return 1
110+
if maskSize > len(mask) {
111+
maskSize = len(mask)
109112
}
110113

111114
if cpuset_getaffinity(_CPU_LEVEL_WHICH, _CPU_WHICH_PID, _CPU_CURRENT_PID,
112-
int(size), (*byte)(unsafe.Pointer(&mask[0]))) != 0 {
115+
maskSize, (*byte)(unsafe.Pointer(&mask[0]))) != 0 {
113116
return 1
114117
}
115118
n := int32(0)
116-
for _, v := range mask[:size] {
119+
for _, v := range mask[:maskSize] {
117120
for v != 0 {
118121
n += int32(v & 1)
119122
v >>= 1

0 commit comments

Comments
 (0)