mirror of
https://github.com/golang/sys.git
synced 2026-01-29 15:12:09 +03:00
Some programs (such as container runtimes) want to reset their CPU
affinity if they are spawned by processes with a particular CPU
affinity. Container runtimes didn't really have to deal with this issue
until Linux 6.2 when the cpuset cgroup was changed to no longer
auto-reset CPU affinity in this case.
A naive approach to resetting your CPU affinity would be to get the
number of CPUs by looking at "/proc/stat" or "/sys/devices/system/cpu"
(note that runtime.NumCPU() actually returns the CPU affinity of the
process at startup time, which isn't useful for this purpose) and then
asking for all of those CPUs.
However, sched_setaffinity(2) will silently ignore any CPU bits set in
the provided CPUSet if they do not exist or are not enabled in the
cpuset cgroup of the process. This means that you can reset your CPU
affinity by just setting every CPU bit in CPUSet and passing it to
sched_setaffinity(2).
Unfortunately, setting every CPU bit in CPUSet with (*CPUSet).Set() is
very inefficient. If it were possible to just memset(0xFF) the CPUSet
array, users would be able to reset their CPU affinity even more
cheaply. However, Go doesn't have a memset primitive that can be used in
that way.
Obvious solutions like setting the array elements of CPUSet to (^0) do
not work because CPUSet is an array of a private newtype and so the
compiler complains if you try to use a constant like (^0) without a
cast (and we cannot use a cast because the type is private):
cannot use ^0 (untyped int constant -1) as
"golang.org/x/sys/unix".cpuMask value in assignment (overflows)
The only real alternative is to do something quite hacky like:
cpuset := unix.CPUSet{}
for i := range cpuset {
cpuset[i]-- // underflow to 0xFF..FF
}
... which is the solution we use in runc.
It would be much nicer to have a helper that does this memset for us in
a less hacky way, since resetting CPU affinity seems like a fairly
common operation.
Ref: Linux kernel commit da019032819a ("sched: Enforce user requested affinity")
Fixes golang/go#75186
Change-Id: I211ddeafd54ce35079a67493123d28e1bb76966a
GitHub-Last-Rev: 71871db0f3
GitHub-Pull-Request: golang/sys#259
Reviewed-on: https://go-review.googlesource.com/c/sys/+/698015
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Kirill Kolyshkin <kolyshkin@gmail.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
94 lines
2.1 KiB
Go
94 lines
2.1 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// CPU affinity functions
|
|
|
|
package unix
|
|
|
|
import (
|
|
"math/bits"
|
|
"unsafe"
|
|
)
|
|
|
|
const cpuSetSize = _CPU_SETSIZE / _NCPUBITS
|
|
|
|
// CPUSet represents a CPU affinity mask.
|
|
type CPUSet [cpuSetSize]cpuMask
|
|
|
|
func schedAffinity(trap uintptr, pid int, set *CPUSet) error {
|
|
_, _, e := RawSyscall(trap, uintptr(pid), uintptr(unsafe.Sizeof(*set)), uintptr(unsafe.Pointer(set)))
|
|
if e != 0 {
|
|
return errnoErr(e)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SchedGetaffinity gets the CPU affinity mask of the thread specified by pid.
|
|
// If pid is 0 the calling thread is used.
|
|
func SchedGetaffinity(pid int, set *CPUSet) error {
|
|
return schedAffinity(SYS_SCHED_GETAFFINITY, pid, set)
|
|
}
|
|
|
|
// SchedSetaffinity sets the CPU affinity mask of the thread specified by pid.
|
|
// If pid is 0 the calling thread is used.
|
|
func SchedSetaffinity(pid int, set *CPUSet) error {
|
|
return schedAffinity(SYS_SCHED_SETAFFINITY, pid, set)
|
|
}
|
|
|
|
// Zero clears the set s, so that it contains no CPUs.
|
|
func (s *CPUSet) Zero() {
|
|
clear(s[:])
|
|
}
|
|
|
|
// Fill adds all possible CPU bits to the set s. On Linux, [SchedSetaffinity]
|
|
// will silently ignore any invalid CPU bits in [CPUSet] so this is an
|
|
// efficient way of resetting the CPU affinity of a process.
|
|
func (s *CPUSet) Fill() {
|
|
for i := range s {
|
|
s[i] = ^cpuMask(0)
|
|
}
|
|
}
|
|
|
|
func cpuBitsIndex(cpu int) int {
|
|
return cpu / _NCPUBITS
|
|
}
|
|
|
|
func cpuBitsMask(cpu int) cpuMask {
|
|
return cpuMask(1 << (uint(cpu) % _NCPUBITS))
|
|
}
|
|
|
|
// Set adds cpu to the set s.
|
|
func (s *CPUSet) Set(cpu int) {
|
|
i := cpuBitsIndex(cpu)
|
|
if i < len(s) {
|
|
s[i] |= cpuBitsMask(cpu)
|
|
}
|
|
}
|
|
|
|
// Clear removes cpu from the set s.
|
|
func (s *CPUSet) Clear(cpu int) {
|
|
i := cpuBitsIndex(cpu)
|
|
if i < len(s) {
|
|
s[i] &^= cpuBitsMask(cpu)
|
|
}
|
|
}
|
|
|
|
// IsSet reports whether cpu is in the set s.
|
|
func (s *CPUSet) IsSet(cpu int) bool {
|
|
i := cpuBitsIndex(cpu)
|
|
if i < len(s) {
|
|
return s[i]&cpuBitsMask(cpu) != 0
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Count returns the number of CPUs in the set s.
|
|
func (s *CPUSet) Count() int {
|
|
c := 0
|
|
for _, b := range s {
|
|
c += bits.OnesCount64(uint64(b))
|
|
}
|
|
return c
|
|
}
|