Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

125 righe
3.2 KiB

  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. }