affinity_linux.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // CPU affinity functions
  5. package unix
  6. import (
  7. "unsafe"
  8. )
  9. const cpuSetSize = _CPU_SETSIZE / _NCPUBITS
  10. // CPUSet represents a CPU affinity mask.
  11. type CPUSet [cpuSetSize]cpuMask
  12. func schedAffinity(trap uintptr, pid int, set *CPUSet) error {
  13. _, _, e := RawSyscall(trap, uintptr(pid), uintptr(unsafe.Sizeof(*set)), uintptr(unsafe.Pointer(set)))
  14. if e != 0 {
  15. return errnoErr(e)
  16. }
  17. return nil
  18. }
  19. // SchedGetaffinity gets the CPU affinity mask of the thread specified by pid.
  20. // If pid is 0 the calling thread is used.
  21. func SchedGetaffinity(pid int, set *CPUSet) error {
  22. return schedAffinity(SYS_SCHED_GETAFFINITY, pid, set)
  23. }
  24. // SchedSetaffinity sets the CPU affinity mask of the thread specified by pid.
  25. // If pid is 0 the calling thread is used.
  26. func SchedSetaffinity(pid int, set *CPUSet) error {
  27. return schedAffinity(SYS_SCHED_SETAFFINITY, pid, set)
  28. }
  29. // Zero clears the set s, so that it contains no CPUs.
  30. func (s *CPUSet) Zero() {
  31. for i := range s {
  32. s[i] = 0
  33. }
  34. }
  35. func cpuBitsIndex(cpu int) int {
  36. return cpu / _NCPUBITS
  37. }
  38. func cpuBitsMask(cpu int) cpuMask {
  39. return cpuMask(1 << (uint(cpu) % _NCPUBITS))
  40. }
  41. // Set adds cpu to the set s.
  42. func (s *CPUSet) Set(cpu int) {
  43. i := cpuBitsIndex(cpu)
  44. if i < len(s) {
  45. s[i] |= cpuBitsMask(cpu)
  46. }
  47. }
  48. // Clear removes cpu from the set s.
  49. func (s *CPUSet) Clear(cpu int) {
  50. i := cpuBitsIndex(cpu)
  51. if i < len(s) {
  52. s[i] &^= cpuBitsMask(cpu)
  53. }
  54. }
  55. // IsSet reports whether cpu is in the set s.
  56. func (s *CPUSet) IsSet(cpu int) bool {
  57. i := cpuBitsIndex(cpu)
  58. if i < len(s) {
  59. return s[i]&cpuBitsMask(cpu) != 0
  60. }
  61. return false
  62. }
  63. // Count returns the number of CPUs in the set s.
  64. func (s *CPUSet) Count() int {
  65. c := 0
  66. for _, b := range s {
  67. c += onesCount64(uint64(b))
  68. }
  69. return c
  70. }
  71. // onesCount64 is a copy of Go 1.9's math/bits.OnesCount64.
  72. // Once this package can require Go 1.9, we can delete this
  73. // and update the caller to use bits.OnesCount64.
  74. func onesCount64(x uint64) int {
  75. const m0 = 0x5555555555555555 // 01010101 ...
  76. const m1 = 0x3333333333333333 // 00110011 ...
  77. const m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ...
  78. const m3 = 0x00ff00ff00ff00ff // etc.
  79. const m4 = 0x0000ffff0000ffff
  80. // Implementation: Parallel summing of adjacent bits.
  81. // See "Hacker's Delight", Chap. 5: Counting Bits.
  82. // The following pattern shows the general approach:
  83. //
  84. // x = x>>1&(m0&m) + x&(m0&m)
  85. // x = x>>2&(m1&m) + x&(m1&m)
  86. // x = x>>4&(m2&m) + x&(m2&m)
  87. // x = x>>8&(m3&m) + x&(m3&m)
  88. // x = x>>16&(m4&m) + x&(m4&m)
  89. // x = x>>32&(m5&m) + x&(m5&m)
  90. // return int(x)
  91. //
  92. // Masking (& operations) can be left away when there's no
  93. // danger that a field's sum will carry over into the next
  94. // field: Since the result cannot be > 64, 8 bits is enough
  95. // and we can ignore the masks for the shifts by 8 and up.
  96. // Per "Hacker's Delight", the first line can be simplified
  97. // more, but it saves at best one instruction, so we leave
  98. // it alone for clarity.
  99. const m = 1<<64 - 1
  100. x = x>>1&(m0&m) + x&(m0&m)
  101. x = x>>2&(m1&m) + x&(m1&m)
  102. x = (x>>4 + x) & (m2 & m)
  103. x += x >> 8
  104. x += x >> 16
  105. x += x >> 32
  106. return int(x) & (1<<7 - 1)
  107. }