topology.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. "io/ioutil"
  17. "strings"
  18. cadvisorapi "github.com/google/cadvisor/info/v1"
  19. "k8s.io/klog"
  20. "k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
  21. )
  22. // NUMANodeInfo is a map from NUMANode ID to a list of CPU IDs associated with
  23. // that NUMANode.
  24. type NUMANodeInfo map[int]cpuset.CPUSet
  25. // CPUDetails is a map from CPU ID to Core ID, Socket ID, and NUMA ID.
  26. type CPUDetails map[int]CPUInfo
  27. // CPUTopology contains details of node cpu, where :
  28. // CPU - logical CPU, cadvisor - thread
  29. // Core - physical CPU, cadvisor - Core
  30. // Socket - socket, cadvisor - Node
  31. type CPUTopology struct {
  32. NumCPUs int
  33. NumCores int
  34. NumSockets int
  35. CPUDetails CPUDetails
  36. }
  37. // CPUsPerCore returns the number of logical CPUs are associated with
  38. // each core.
  39. func (topo *CPUTopology) CPUsPerCore() int {
  40. if topo.NumCores == 0 {
  41. return 0
  42. }
  43. return topo.NumCPUs / topo.NumCores
  44. }
  45. // CPUsPerSocket returns the number of logical CPUs are associated with
  46. // each socket.
  47. func (topo *CPUTopology) CPUsPerSocket() int {
  48. if topo.NumSockets == 0 {
  49. return 0
  50. }
  51. return topo.NumCPUs / topo.NumSockets
  52. }
  53. // CPUInfo contains the NUMA, socket, and core IDs associated with a CPU.
  54. type CPUInfo struct {
  55. NUMANodeID int
  56. SocketID int
  57. CoreID int
  58. }
  59. // KeepOnly returns a new CPUDetails object with only the supplied cpus.
  60. func (d CPUDetails) KeepOnly(cpus cpuset.CPUSet) CPUDetails {
  61. result := CPUDetails{}
  62. for cpu, info := range d {
  63. if cpus.Contains(cpu) {
  64. result[cpu] = info
  65. }
  66. }
  67. return result
  68. }
  69. // NUMANodes returns all of the NUMANode IDs associated with the CPUs in this
  70. // CPUDetails.
  71. func (d CPUDetails) NUMANodes() cpuset.CPUSet {
  72. b := cpuset.NewBuilder()
  73. for _, info := range d {
  74. b.Add(info.NUMANodeID)
  75. }
  76. return b.Result()
  77. }
  78. // NUMANodesInSockets returns all of the logical NUMANode IDs associated with
  79. // the given socket IDs in this CPUDetails.
  80. func (d CPUDetails) NUMANodesInSockets(ids ...int) cpuset.CPUSet {
  81. b := cpuset.NewBuilder()
  82. for _, id := range ids {
  83. for _, info := range d {
  84. if info.SocketID == id {
  85. b.Add(info.NUMANodeID)
  86. }
  87. }
  88. }
  89. return b.Result()
  90. }
  91. // Sockets returns all of the socket IDs associated with the CPUs in this
  92. // CPUDetails.
  93. func (d CPUDetails) Sockets() cpuset.CPUSet {
  94. b := cpuset.NewBuilder()
  95. for _, info := range d {
  96. b.Add(info.SocketID)
  97. }
  98. return b.Result()
  99. }
  100. // CPUsInSockets returns all of the logical CPU IDs associated with the given
  101. // socket IDs in this CPUDetails.
  102. func (d CPUDetails) CPUsInSockets(ids ...int) cpuset.CPUSet {
  103. b := cpuset.NewBuilder()
  104. for _, id := range ids {
  105. for cpu, info := range d {
  106. if info.SocketID == id {
  107. b.Add(cpu)
  108. }
  109. }
  110. }
  111. return b.Result()
  112. }
  113. // SocketsInNUMANodes returns all of the logical Socket IDs associated with the
  114. // given NUMANode IDs in this CPUDetails.
  115. func (d CPUDetails) SocketsInNUMANodes(ids ...int) cpuset.CPUSet {
  116. b := cpuset.NewBuilder()
  117. for _, id := range ids {
  118. for _, info := range d {
  119. if info.NUMANodeID == id {
  120. b.Add(info.SocketID)
  121. }
  122. }
  123. }
  124. return b.Result()
  125. }
  126. // Cores returns all of the core IDs associated with the CPUs in this
  127. // CPUDetails.
  128. func (d CPUDetails) Cores() cpuset.CPUSet {
  129. b := cpuset.NewBuilder()
  130. for _, info := range d {
  131. b.Add(info.CoreID)
  132. }
  133. return b.Result()
  134. }
  135. // CoresInNUMANodes returns all of the core IDs associated with the given
  136. // NUMANode IDs in this CPUDetails.
  137. func (d CPUDetails) CoresInNUMANodes(ids ...int) cpuset.CPUSet {
  138. b := cpuset.NewBuilder()
  139. for _, id := range ids {
  140. for _, info := range d {
  141. if info.NUMANodeID == id {
  142. b.Add(info.CoreID)
  143. }
  144. }
  145. }
  146. return b.Result()
  147. }
  148. // CoresInSockets returns all of the core IDs associated with the given socket
  149. // IDs in this CPUDetails.
  150. func (d CPUDetails) CoresInSockets(ids ...int) cpuset.CPUSet {
  151. b := cpuset.NewBuilder()
  152. for _, id := range ids {
  153. for _, info := range d {
  154. if info.SocketID == id {
  155. b.Add(info.CoreID)
  156. }
  157. }
  158. }
  159. return b.Result()
  160. }
  161. // CPUs returns all of the logical CPU IDs in this CPUDetails.
  162. func (d CPUDetails) CPUs() cpuset.CPUSet {
  163. b := cpuset.NewBuilder()
  164. for cpuID := range d {
  165. b.Add(cpuID)
  166. }
  167. return b.Result()
  168. }
  169. // CPUsInNUMANodes returns all of the logical CPU IDs associated with the given
  170. // NUMANode IDs in this CPUDetails.
  171. func (d CPUDetails) CPUsInNUMANodes(ids ...int) cpuset.CPUSet {
  172. b := cpuset.NewBuilder()
  173. for _, id := range ids {
  174. for cpu, info := range d {
  175. if info.NUMANodeID == id {
  176. b.Add(cpu)
  177. }
  178. }
  179. }
  180. return b.Result()
  181. }
  182. // CPUsInCores returns all of the logical CPU IDs associated with the given
  183. // core IDs in this CPUDetails.
  184. func (d CPUDetails) CPUsInCores(ids ...int) cpuset.CPUSet {
  185. b := cpuset.NewBuilder()
  186. for _, id := range ids {
  187. for cpu, info := range d {
  188. if info.CoreID == id {
  189. b.Add(cpu)
  190. }
  191. }
  192. }
  193. return b.Result()
  194. }
  195. // Discover returns CPUTopology based on cadvisor node info
  196. func Discover(machineInfo *cadvisorapi.MachineInfo, numaNodeInfo NUMANodeInfo) (*CPUTopology, error) {
  197. if machineInfo.NumCores == 0 {
  198. return nil, fmt.Errorf("could not detect number of cpus")
  199. }
  200. CPUDetails := CPUDetails{}
  201. numPhysicalCores := 0
  202. for _, socket := range machineInfo.Topology {
  203. numPhysicalCores += len(socket.Cores)
  204. for _, core := range socket.Cores {
  205. if coreID, err := getUniqueCoreID(core.Threads); err == nil {
  206. for _, cpu := range core.Threads {
  207. numaNodeID := 0
  208. for id, cset := range numaNodeInfo {
  209. if cset.Contains(cpu) {
  210. numaNodeID = id
  211. }
  212. }
  213. CPUDetails[cpu] = CPUInfo{
  214. CoreID: coreID,
  215. SocketID: socket.Id,
  216. NUMANodeID: numaNodeID,
  217. }
  218. }
  219. } else {
  220. klog.Errorf("could not get unique coreID for socket: %d core %d threads: %v",
  221. socket.Id, core.Id, core.Threads)
  222. return nil, err
  223. }
  224. }
  225. }
  226. return &CPUTopology{
  227. NumCPUs: machineInfo.NumCores,
  228. NumSockets: len(machineInfo.Topology),
  229. NumCores: numPhysicalCores,
  230. CPUDetails: CPUDetails,
  231. }, nil
  232. }
  233. // getUniqueCoreID computes coreId as the lowest cpuID
  234. // for a given Threads []int slice. This will assure that coreID's are
  235. // platform unique (opposite to what cAdvisor reports - socket unique)
  236. func getUniqueCoreID(threads []int) (coreID int, err error) {
  237. if len(threads) == 0 {
  238. return 0, fmt.Errorf("no cpus provided")
  239. }
  240. if len(threads) != cpuset.NewCPUSet(threads...).Size() {
  241. return 0, fmt.Errorf("cpus provided are not unique")
  242. }
  243. min := threads[0]
  244. for _, thread := range threads[1:] {
  245. if thread < min {
  246. min = thread
  247. }
  248. }
  249. return min, nil
  250. }
  251. // GetNUMANodeInfo uses sysfs to return a map of NUMANode id to the list of
  252. // CPUs associated with that NUMANode.
  253. //
  254. // TODO: This is a temporary workaround until cadvisor provides this
  255. // information directly in machineInfo. We should remove this once this
  256. // information is available from cadvisor.
  257. func GetNUMANodeInfo() (NUMANodeInfo, error) {
  258. // Get the possible NUMA nodes on this machine. If reading this file
  259. // is not possible, this is not an error. Instead, we just return a
  260. // nil NUMANodeInfo, indicating that no NUMA information is available
  261. // on this machine. This should implicitly be interpreted as having a
  262. // single NUMA node with id 0 for all CPUs.
  263. nodelist, err := ioutil.ReadFile("/sys/devices/system/node/online")
  264. if err != nil {
  265. return nil, nil
  266. }
  267. // Parse the nodelist into a set of Node IDs
  268. nodes, err := cpuset.Parse(strings.TrimSpace(string(nodelist)))
  269. if err != nil {
  270. return nil, err
  271. }
  272. info := make(NUMANodeInfo)
  273. // For each node...
  274. for _, node := range nodes.ToSlice() {
  275. // Read the 'cpulist' of the NUMA node from sysfs.
  276. path := fmt.Sprintf("/sys/devices/system/node/node%d/cpulist", node)
  277. cpulist, err := ioutil.ReadFile(path)
  278. if err != nil {
  279. return nil, err
  280. }
  281. // Convert the 'cpulist' into a set of CPUs.
  282. cpus, err := cpuset.Parse(strings.TrimSpace(string(cpulist)))
  283. if err != nil {
  284. return nil, err
  285. }
  286. info[node] = cpus
  287. }
  288. return info, nil
  289. }