topology.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package topology
  14. import (
  15. "fmt"
  16. cadvisorapi "github.com/google/cadvisor/info/v1"
  17. "k8s.io/klog"
  18. "k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
  19. )
  20. // CPUDetails is a map from CPU ID to Core ID and Socket ID.
  21. type CPUDetails map[int]CPUInfo
  22. // CPUTopology contains details of node cpu, where :
  23. // CPU - logical CPU, cadvisor - thread
  24. // Core - physical CPU, cadvisor - Core
  25. // Socket - socket, cadvisor - Node
  26. type CPUTopology struct {
  27. NumCPUs int
  28. NumCores int
  29. NumSockets int
  30. CPUDetails CPUDetails
  31. }
  32. // CPUsPerCore returns the number of logical CPUs are associated with
  33. // each core.
  34. func (topo *CPUTopology) CPUsPerCore() int {
  35. if topo.NumCores == 0 {
  36. return 0
  37. }
  38. return topo.NumCPUs / topo.NumCores
  39. }
  40. // CPUsPerSocket returns the number of logical CPUs are associated with
  41. // each socket.
  42. func (topo *CPUTopology) CPUsPerSocket() int {
  43. if topo.NumSockets == 0 {
  44. return 0
  45. }
  46. return topo.NumCPUs / topo.NumSockets
  47. }
  48. // CPUInfo contains the socket and core IDs associated with a CPU.
  49. type CPUInfo struct {
  50. SocketID int
  51. CoreID int
  52. }
  53. // KeepOnly returns a new CPUDetails object with only the supplied cpus.
  54. func (d CPUDetails) KeepOnly(cpus cpuset.CPUSet) CPUDetails {
  55. result := CPUDetails{}
  56. for cpu, info := range d {
  57. if cpus.Contains(cpu) {
  58. result[cpu] = info
  59. }
  60. }
  61. return result
  62. }
  63. // Sockets returns all of the socket IDs associated with the CPUs in this
  64. // CPUDetails.
  65. func (d CPUDetails) Sockets() cpuset.CPUSet {
  66. b := cpuset.NewBuilder()
  67. for _, info := range d {
  68. b.Add(info.SocketID)
  69. }
  70. return b.Result()
  71. }
  72. // CPUsInSocket returns all of the logical CPU IDs associated with the
  73. // given socket ID in this CPUDetails.
  74. func (d CPUDetails) CPUsInSocket(id int) cpuset.CPUSet {
  75. b := cpuset.NewBuilder()
  76. for cpu, info := range d {
  77. if info.SocketID == id {
  78. b.Add(cpu)
  79. }
  80. }
  81. return b.Result()
  82. }
  83. // Cores returns all of the core IDs associated with the CPUs in this
  84. // CPUDetails.
  85. func (d CPUDetails) Cores() cpuset.CPUSet {
  86. b := cpuset.NewBuilder()
  87. for _, info := range d {
  88. b.Add(info.CoreID)
  89. }
  90. return b.Result()
  91. }
  92. // CoresInSocket returns all of the core IDs associated with the given
  93. // socket ID in this CPUDetails.
  94. func (d CPUDetails) CoresInSocket(id int) cpuset.CPUSet {
  95. b := cpuset.NewBuilder()
  96. for _, info := range d {
  97. if info.SocketID == id {
  98. b.Add(info.CoreID)
  99. }
  100. }
  101. return b.Result()
  102. }
  103. // CPUs returns all of the logical CPU IDs in this CPUDetails.
  104. func (d CPUDetails) CPUs() cpuset.CPUSet {
  105. b := cpuset.NewBuilder()
  106. for cpuID := range d {
  107. b.Add(cpuID)
  108. }
  109. return b.Result()
  110. }
  111. // CPUsInCore returns all of the logical CPU IDs associated with the
  112. // given core ID in this CPUDetails.
  113. func (d CPUDetails) CPUsInCore(id int) cpuset.CPUSet {
  114. b := cpuset.NewBuilder()
  115. for cpu, info := range d {
  116. if info.CoreID == id {
  117. b.Add(cpu)
  118. }
  119. }
  120. return b.Result()
  121. }
  122. // Discover returns CPUTopology based on cadvisor node info
  123. func Discover(machineInfo *cadvisorapi.MachineInfo) (*CPUTopology, error) {
  124. if machineInfo.NumCores == 0 {
  125. return nil, fmt.Errorf("could not detect number of cpus")
  126. }
  127. CPUDetails := CPUDetails{}
  128. numPhysicalCores := 0
  129. for _, socket := range machineInfo.Topology {
  130. numPhysicalCores += len(socket.Cores)
  131. for _, core := range socket.Cores {
  132. if coreID, err := getUniqueCoreID(core.Threads); err == nil {
  133. for _, cpu := range core.Threads {
  134. CPUDetails[cpu] = CPUInfo{
  135. CoreID: coreID,
  136. SocketID: socket.Id,
  137. }
  138. }
  139. } else {
  140. klog.Errorf("could not get unique coreID for socket: %d core %d threads: %v",
  141. socket.Id, core.Id, core.Threads)
  142. return nil, err
  143. }
  144. }
  145. }
  146. return &CPUTopology{
  147. NumCPUs: machineInfo.NumCores,
  148. NumSockets: len(machineInfo.Topology),
  149. NumCores: numPhysicalCores,
  150. CPUDetails: CPUDetails,
  151. }, nil
  152. }
  153. // getUniqueCoreID computes coreId as the lowest cpuID
  154. // for a given Threads []int slice. This will assure that coreID's are
  155. // platform unique (opposite to what cAdvisor reports - socket unique)
  156. func getUniqueCoreID(threads []int) (coreID int, err error) {
  157. if len(threads) == 0 {
  158. return 0, fmt.Errorf("no cpus provided")
  159. }
  160. if len(threads) != cpuset.NewCPUSet(threads...).Size() {
  161. return 0, fmt.Errorf("cpus provided are not unique")
  162. }
  163. min := threads[0]
  164. for _, thread := range threads[1:] {
  165. if thread < min {
  166. min = thread
  167. }
  168. }
  169. return min, nil
  170. }