// 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 functionspackage uniximport ()constcpuSetSize = _CPU_SETSIZE / _NCPUBITS// CPUSet represents a bit mask of CPUs, to be used with [SchedGetaffinity], [SchedSetaffinity],// and [SetMemPolicy].//// Note this type can only represent CPU IDs 0 through 1023.// Use [CPUSetDynamic]/[NewCPUSet] instead to avoid this limit.typeCPUSet [cpuSetSize]cpuMask// CPUSetDynamic represents a bit mask of CPUs, to be used with [SchedGetaffinityDynamic],// [SchedSetaffinityDynamic], and [SetMemPolicyDynamic]. Use [NewCPUSet] to allocate.typeCPUSetDynamic []cpuMaskfunc ( uintptr, int, uintptr, unsafe.Pointer) error { , , := RawSyscall(, uintptr(), uintptr(), uintptr())if != 0 {returnerrnoErr() }returnnil}// SchedGetaffinity gets the CPU affinity mask of the thread specified by pid.// If pid is 0 the calling thread is used.func ( int, *CPUSet) error {returnschedAffinity(SYS_SCHED_GETAFFINITY, , unsafe.Sizeof(*), unsafe.Pointer())}// SchedSetaffinity sets the CPU affinity mask of the thread specified by pid.// If pid is 0 the calling thread is used.func ( int, *CPUSet) error {returnschedAffinity(SYS_SCHED_SETAFFINITY, , unsafe.Sizeof(*), unsafe.Pointer())}// Zero clears the set s, so that it contains no CPUs.func ( *CPUSet) () {clear([:])}// 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 ( *CPUSet) () {cpuMaskFill([:])}func ( int) int {return / _NCPUBITS}func ( int) cpuMask {returncpuMask(1 << (uint() % _NCPUBITS))}func ( []cpuMask) {for := range { [] = ^cpuMask(0) }}func ( []cpuMask, int) { := cpuBitsIndex()if < len() { [] |= cpuBitsMask() }}func ( []cpuMask, int) { := cpuBitsIndex()if < len() { [] &^= cpuBitsMask() }}func ( []cpuMask, int) bool { := cpuBitsIndex()if < len() {return []&cpuBitsMask() != 0 }returnfalse}func ( []cpuMask) int { := 0for , := range { += bits.OnesCount64(uint64()) }return}// Set adds cpu to the set s. If cpu is out of bounds for s, no action is taken.func ( *CPUSet) ( int) {cpuMaskSet([:], )}// Clear removes cpu from the set s. If cpu is out of bounds for s, no action is taken.func ( *CPUSet) ( int) {cpuMaskClear([:], )}// IsSet reports whether cpu is in the set s.func ( *CPUSet) ( int) bool {returncpuMaskIsSet([:], )}// Count returns the number of CPUs in the set s.func ( *CPUSet) () int {returncpuMaskCount([:])}// NewCPUSet creates a CPU affinity mask capable of representing CPU IDs// up to maxCPU (exclusive).func ( int) CPUSetDynamic { := ( + _NCPUBITS - 1) / _NCPUBITSif == 0 { = 1 }returnmake(CPUSetDynamic, )}// Zero clears the set s, so that it contains no CPUs.func ( CPUSetDynamic) () {clear()}// Fill adds all possible CPU bits to the set s. On Linux, [SchedSetaffinityDynamic]// will silently ignore any invalid CPU bits in [CPUSetDynamic] so this is an// efficient way of resetting the CPU affinity of a process.func ( CPUSetDynamic) () {cpuMaskFill()}// Set adds cpu to the set s. If cpu is out of bounds for s, no action is taken.func ( CPUSetDynamic) ( int) {cpuMaskSet(, )}// Clear removes cpu from the set s. If cpu is out of bounds for s, no action is taken.func ( CPUSetDynamic) ( int) {cpuMaskClear(, )}// IsSet reports whether cpu is in the set s.func ( CPUSetDynamic) ( int) bool {returncpuMaskIsSet(, )}// Count returns the number of CPUs in the set s.func ( CPUSetDynamic) () int {returncpuMaskCount()}func ( CPUSetDynamic) () uintptr {returnuintptr(len()) * unsafe.Sizeof(cpuMask(0))}func ( CPUSetDynamic) () unsafe.Pointer {iflen() == 0 {returnnil }returnunsafe.Pointer(&[0])}// SchedGetaffinityDynamic gets the CPU affinity mask of the thread specified by pid.// If pid is 0 the calling thread is used.//// If the set is smaller than the size of the affinity mask used by the kernel,// [EINVAL] is returned.func ( int, CPUSetDynamic) error {returnschedAffinity(SYS_SCHED_GETAFFINITY, , .size(), .pointer())}// SchedSetaffinityDynamic sets the CPU affinity mask of the thread specified by pid.// If pid is 0 the calling thread is used.func ( int, CPUSetDynamic) error {returnschedAffinity(SYS_SCHED_SETAFFINITY, , .size(), .pointer())}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)