forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/sys/unix/affinity_linux.go

139 lines
1.9 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2018 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// 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.
2024-02-18 10:42:21 +00:00
type CPUSet [cpuSetSize]cpuMask
func schedAffinity(trap uintptr, pid int, set *CPUSet) error {
2024-02-18 10:42:21 +00:00
_, _, e := RawSyscall(trap, uintptr(pid), uintptr(unsafe.Sizeof(*set)), uintptr(unsafe.Pointer(set)))
2024-02-18 10:42:21 +00:00
if e != 0 {
2024-02-18 10:42:21 +00:00
return errnoErr(e)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// SchedGetaffinity gets the CPU affinity mask of the thread specified by pid.
2024-02-18 10:42:21 +00:00
// If pid is 0 the calling thread is used.
2024-02-18 10:42:21 +00:00
func SchedGetaffinity(pid int, set *CPUSet) error {
2024-02-18 10:42:21 +00:00
return schedAffinity(SYS_SCHED_GETAFFINITY, pid, set)
2024-02-18 10:42:21 +00:00
}
// SchedSetaffinity sets the CPU affinity mask of the thread specified by pid.
2024-02-18 10:42:21 +00:00
// If pid is 0 the calling thread is used.
2024-02-18 10:42:21 +00:00
func SchedSetaffinity(pid int, set *CPUSet) error {
2024-02-18 10:42:21 +00:00
return schedAffinity(SYS_SCHED_SETAFFINITY, pid, set)
2024-02-18 10:42:21 +00:00
}
// Zero clears the set s, so that it contains no CPUs.
2024-02-18 10:42:21 +00:00
func (s *CPUSet) Zero() {
2024-02-18 10:42:21 +00:00
for i := range s {
2024-02-18 10:42:21 +00:00
s[i] = 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func cpuBitsIndex(cpu int) int {
2024-02-18 10:42:21 +00:00
return cpu / _NCPUBITS
2024-02-18 10:42:21 +00:00
}
func cpuBitsMask(cpu int) cpuMask {
2024-02-18 10:42:21 +00:00
return cpuMask(1 << (uint(cpu) % _NCPUBITS))
2024-02-18 10:42:21 +00:00
}
// Set adds cpu to the set s.
2024-02-18 10:42:21 +00:00
func (s *CPUSet) Set(cpu int) {
2024-02-18 10:42:21 +00:00
i := cpuBitsIndex(cpu)
2024-02-18 10:42:21 +00:00
if i < len(s) {
2024-02-18 10:42:21 +00:00
s[i] |= cpuBitsMask(cpu)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Clear removes cpu from the set s.
2024-02-18 10:42:21 +00:00
func (s *CPUSet) Clear(cpu int) {
2024-02-18 10:42:21 +00:00
i := cpuBitsIndex(cpu)
2024-02-18 10:42:21 +00:00
if i < len(s) {
2024-02-18 10:42:21 +00:00
s[i] &^= cpuBitsMask(cpu)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// IsSet reports whether cpu is in the set s.
2024-02-18 10:42:21 +00:00
func (s *CPUSet) IsSet(cpu int) bool {
2024-02-18 10:42:21 +00:00
i := cpuBitsIndex(cpu)
2024-02-18 10:42:21 +00:00
if i < len(s) {
2024-02-18 10:42:21 +00:00
return s[i]&cpuBitsMask(cpu) != 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Count returns the number of CPUs in the set s.
2024-02-18 10:42:21 +00:00
func (s *CPUSet) Count() int {
2024-02-18 10:42:21 +00:00
c := 0
2024-02-18 10:42:21 +00:00
for _, b := range s {
2024-02-18 10:42:21 +00:00
c += bits.OnesCount64(uint64(b))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return c
2024-02-18 10:42:21 +00:00
}