a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
at main 1.9 kB view raw
1// Copyright 2018 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5// CPU affinity functions 6 7package unix 8 9import ( 10 "math/bits" 11 "unsafe" 12) 13 14const cpuSetSize = _CPU_SETSIZE / _NCPUBITS 15 16// CPUSet represents a CPU affinity mask. 17type CPUSet [cpuSetSize]cpuMask 18 19func schedAffinity(trap uintptr, pid int, set *CPUSet) error { 20 _, _, e := RawSyscall(trap, uintptr(pid), uintptr(unsafe.Sizeof(*set)), uintptr(unsafe.Pointer(set))) 21 if e != 0 { 22 return errnoErr(e) 23 } 24 return nil 25} 26 27// SchedGetaffinity gets the CPU affinity mask of the thread specified by pid. 28// If pid is 0 the calling thread is used. 29func SchedGetaffinity(pid int, set *CPUSet) error { 30 return schedAffinity(SYS_SCHED_GETAFFINITY, pid, set) 31} 32 33// SchedSetaffinity sets the CPU affinity mask of the thread specified by pid. 34// If pid is 0 the calling thread is used. 35func SchedSetaffinity(pid int, set *CPUSet) error { 36 return schedAffinity(SYS_SCHED_SETAFFINITY, pid, set) 37} 38 39// Zero clears the set s, so that it contains no CPUs. 40func (s *CPUSet) Zero() { 41 clear(s[:]) 42} 43 44func cpuBitsIndex(cpu int) int { 45 return cpu / _NCPUBITS 46} 47 48func cpuBitsMask(cpu int) cpuMask { 49 return cpuMask(1 << (uint(cpu) % _NCPUBITS)) 50} 51 52// Set adds cpu to the set s. 53func (s *CPUSet) Set(cpu int) { 54 i := cpuBitsIndex(cpu) 55 if i < len(s) { 56 s[i] |= cpuBitsMask(cpu) 57 } 58} 59 60// Clear removes cpu from the set s. 61func (s *CPUSet) Clear(cpu int) { 62 i := cpuBitsIndex(cpu) 63 if i < len(s) { 64 s[i] &^= cpuBitsMask(cpu) 65 } 66} 67 68// IsSet reports whether cpu is in the set s. 69func (s *CPUSet) IsSet(cpu int) bool { 70 i := cpuBitsIndex(cpu) 71 if i < len(s) { 72 return s[i]&cpuBitsMask(cpu) != 0 73 } 74 return false 75} 76 77// Count returns the number of CPUs in the set s. 78func (s *CPUSet) Count() int { 79 c := 0 80 for _, b := range s { 81 c += bits.OnesCount64(uint64(b)) 82 } 83 return c 84}