machine.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // The machine package contains functions that extract machine-level specs.
  15. package machine
  16. import (
  17. "bytes"
  18. "fmt"
  19. "io/ioutil"
  20. "path/filepath"
  21. "regexp"
  22. "strconv"
  23. "strings"
  24. // s390/s390x changes
  25. "runtime"
  26. info "github.com/google/cadvisor/info/v1"
  27. "github.com/google/cadvisor/utils"
  28. "github.com/google/cadvisor/utils/sysfs"
  29. "github.com/google/cadvisor/utils/sysinfo"
  30. "k8s.io/klog"
  31. "golang.org/x/sys/unix"
  32. )
  33. var (
  34. cpuRegExp = regexp.MustCompile(`^processor\s*:\s*([0-9]+)$`)
  35. coreRegExp = regexp.MustCompile(`^core id\s*:\s*([0-9]+)$`)
  36. nodeRegExp = regexp.MustCompile(`^physical id\s*:\s*([0-9]+)$`)
  37. nodeBusRegExp = regexp.MustCompile(`^node([0-9]+)$`)
  38. // Power systems have a different format so cater for both
  39. cpuClockSpeedMHz = regexp.MustCompile(`(?:cpu MHz|clock)\s*:\s*([0-9]+\.[0-9]+)(?:MHz)?`)
  40. memoryCapacityRegexp = regexp.MustCompile(`MemTotal:\s*([0-9]+) kB`)
  41. swapCapacityRegexp = regexp.MustCompile(`SwapTotal:\s*([0-9]+) kB`)
  42. )
  43. const maxFreqFile = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
  44. const cpuBusPath = "/sys/bus/cpu/devices/"
  45. const nodePath = "/sys/devices/system/node"
  46. // GetClockSpeed returns the CPU clock speed, given a []byte formatted as the /proc/cpuinfo file.
  47. func GetClockSpeed(procInfo []byte) (uint64, error) {
  48. // s390/s390x, aarch64 and arm32 changes
  49. if isSystemZ() || isAArch64() || isArm32() {
  50. return 0, nil
  51. }
  52. // First look through sys to find a max supported cpu frequency.
  53. if utils.FileExists(maxFreqFile) {
  54. val, err := ioutil.ReadFile(maxFreqFile)
  55. if err != nil {
  56. return 0, err
  57. }
  58. var maxFreq uint64
  59. n, err := fmt.Sscanf(string(val), "%d", &maxFreq)
  60. if err != nil || n != 1 {
  61. return 0, fmt.Errorf("could not parse frequency %q", val)
  62. }
  63. return maxFreq, nil
  64. }
  65. // Fall back to /proc/cpuinfo
  66. matches := cpuClockSpeedMHz.FindSubmatch(procInfo)
  67. if len(matches) != 2 {
  68. return 0, fmt.Errorf("could not detect clock speed from output: %q", string(procInfo))
  69. }
  70. speed, err := strconv.ParseFloat(string(matches[1]), 64)
  71. if err != nil {
  72. return 0, err
  73. }
  74. // Convert to kHz
  75. return uint64(speed * 1000), nil
  76. }
  77. // GetMachineMemoryCapacity returns the machine's total memory from /proc/meminfo.
  78. // Returns the total memory capacity as an uint64 (number of bytes).
  79. func GetMachineMemoryCapacity() (uint64, error) {
  80. out, err := ioutil.ReadFile("/proc/meminfo")
  81. if err != nil {
  82. return 0, err
  83. }
  84. memoryCapacity, err := parseCapacity(out, memoryCapacityRegexp)
  85. if err != nil {
  86. return 0, err
  87. }
  88. return memoryCapacity, err
  89. }
  90. // GetMachineSwapCapacity returns the machine's total swap from /proc/meminfo.
  91. // Returns the total swap capacity as an uint64 (number of bytes).
  92. func GetMachineSwapCapacity() (uint64, error) {
  93. out, err := ioutil.ReadFile("/proc/meminfo")
  94. if err != nil {
  95. return 0, err
  96. }
  97. swapCapacity, err := parseCapacity(out, swapCapacityRegexp)
  98. if err != nil {
  99. return 0, err
  100. }
  101. return swapCapacity, err
  102. }
  103. // parseCapacity matches a Regexp in a []byte, returning the resulting value in bytes.
  104. // Assumes that the value matched by the Regexp is in KB.
  105. func parseCapacity(b []byte, r *regexp.Regexp) (uint64, error) {
  106. matches := r.FindSubmatch(b)
  107. if len(matches) != 2 {
  108. return 0, fmt.Errorf("failed to match regexp in output: %q", string(b))
  109. }
  110. m, err := strconv.ParseUint(string(matches[1]), 10, 64)
  111. if err != nil {
  112. return 0, err
  113. }
  114. // Convert to bytes.
  115. return m * 1024, err
  116. }
  117. /* Look for sysfs cpu path containing core_id */
  118. /* Such as: sys/bus/cpu/devices/cpu0/topology/core_id */
  119. func getCoreIdFromCpuBus(cpuBusPath string, threadId int) (int, error) {
  120. path := filepath.Join(cpuBusPath, fmt.Sprintf("cpu%d/topology", threadId))
  121. file := filepath.Join(path, "core_id")
  122. num, err := ioutil.ReadFile(file)
  123. if err != nil {
  124. return threadId, err
  125. }
  126. coreId, err := strconv.ParseInt(string(bytes.TrimSpace(num)), 10, 32)
  127. if err != nil {
  128. return threadId, err
  129. }
  130. if coreId < 0 {
  131. // report threadId if found coreId < 0
  132. coreId = int64(threadId)
  133. }
  134. return int(coreId), nil
  135. }
  136. /* Look for sysfs cpu path containing node id */
  137. /* Such as: /sys/bus/cpu/devices/cpu0/node%d */
  138. func getNodeIdFromCpuBus(cpuBusPath string, threadId int) (int, error) {
  139. path := filepath.Join(cpuBusPath, fmt.Sprintf("cpu%d", threadId))
  140. files, err := ioutil.ReadDir(path)
  141. if err != nil {
  142. return 0, err
  143. }
  144. nodeId := 0
  145. for _, file := range files {
  146. filename := file.Name()
  147. isNode, error := regexp.MatchString("^node([0-9]+)$", filename)
  148. if error != nil {
  149. continue
  150. }
  151. if !isNode {
  152. continue
  153. }
  154. ok, val, _ := extractValue(filename, nodeBusRegExp)
  155. if err != nil {
  156. continue
  157. }
  158. if ok {
  159. if val < 0 {
  160. continue
  161. }
  162. nodeId = val
  163. }
  164. }
  165. return nodeId, nil
  166. }
  167. // GetHugePagesInfo returns information about pre-allocated huge pages
  168. // hugepagesDirectory should be top directory of hugepages
  169. // Such as: /sys/kernel/mm/hugepages/
  170. func GetHugePagesInfo(hugepagesDirectory string) ([]info.HugePagesInfo, error) {
  171. var hugePagesInfo []info.HugePagesInfo
  172. files, err := ioutil.ReadDir(hugepagesDirectory)
  173. if err != nil {
  174. // treat as non-fatal since kernels and machine can be
  175. // configured to disable hugepage support
  176. return hugePagesInfo, nil
  177. }
  178. for _, st := range files {
  179. nameArray := strings.Split(st.Name(), "-")
  180. pageSizeArray := strings.Split(nameArray[1], "kB")
  181. pageSize, err := strconv.ParseUint(string(pageSizeArray[0]), 10, 64)
  182. if err != nil {
  183. return hugePagesInfo, err
  184. }
  185. numFile := hugepagesDirectory + st.Name() + "/nr_hugepages"
  186. val, err := ioutil.ReadFile(numFile)
  187. if err != nil {
  188. return hugePagesInfo, err
  189. }
  190. var numPages uint64
  191. // we use sscanf as the file as a new-line that trips up ParseUint
  192. // it returns the number of tokens successfully parsed, so if
  193. // n != 1, it means we were unable to parse a number from the file
  194. n, err := fmt.Sscanf(string(val), "%d", &numPages)
  195. if err != nil || n != 1 {
  196. return hugePagesInfo, fmt.Errorf("could not parse file %v contents %q", numFile, string(val))
  197. }
  198. hugePagesInfo = append(hugePagesInfo, info.HugePagesInfo{
  199. NumPages: numPages,
  200. PageSize: pageSize,
  201. })
  202. }
  203. return hugePagesInfo, nil
  204. }
  205. func GetTopology(sysFs sysfs.SysFs, cpuinfo string) ([]info.Node, int, error) {
  206. nodes := []info.Node{}
  207. // s390/s390x changes
  208. if true == isSystemZ() {
  209. return nodes, getNumCores(), nil
  210. }
  211. numCores := 0
  212. lastThread := -1
  213. lastCore := -1
  214. lastNode := -1
  215. for _, line := range strings.Split(cpuinfo, "\n") {
  216. if line == "" {
  217. continue
  218. }
  219. ok, val, err := extractValue(line, cpuRegExp)
  220. if err != nil {
  221. return nil, -1, fmt.Errorf("could not parse cpu info from %q: %v", line, err)
  222. }
  223. if ok {
  224. thread := val
  225. numCores++
  226. if lastThread != -1 {
  227. // New cpu section. Save last one.
  228. nodeIdx, err := addNode(&nodes, lastNode)
  229. if err != nil {
  230. return nil, -1, fmt.Errorf("failed to add node %d: %v", lastNode, err)
  231. }
  232. nodes[nodeIdx].AddThread(lastThread, lastCore)
  233. lastCore = -1
  234. lastNode = -1
  235. }
  236. lastThread = thread
  237. /* On Arm platform, no 'core id' and 'physical id' in '/proc/cpuinfo'. */
  238. /* So we search sysfs cpu path directly. */
  239. /* This method can also be used on other platforms, such as x86, ppc64le... */
  240. /* /sys/bus/cpu/devices/cpu%d contains the information of 'core_id' & 'node_id'. */
  241. /* Such as: /sys/bus/cpu/devices/cpu0/topology/core_id */
  242. /* Such as: /sys/bus/cpu/devices/cpu0/node0 */
  243. if isAArch64() {
  244. val, err = getCoreIdFromCpuBus(cpuBusPath, lastThread)
  245. if err != nil {
  246. // Report thread id if no NUMA
  247. val = lastThread
  248. }
  249. lastCore = val
  250. val, err = getNodeIdFromCpuBus(cpuBusPath, lastThread)
  251. if err != nil {
  252. // Report node 0 if no NUMA
  253. val = 0
  254. }
  255. lastNode = val
  256. }
  257. continue
  258. }
  259. if isAArch64() {
  260. /* On Arm platform, no 'core id' and 'physical id' in '/proc/cpuinfo'. */
  261. continue
  262. }
  263. ok, val, err = extractValue(line, coreRegExp)
  264. if err != nil {
  265. return nil, -1, fmt.Errorf("could not parse core info from %q: %v", line, err)
  266. }
  267. if ok {
  268. lastCore = val
  269. continue
  270. }
  271. ok, val, err = extractValue(line, nodeRegExp)
  272. if err != nil {
  273. return nil, -1, fmt.Errorf("could not parse node info from %q: %v", line, err)
  274. }
  275. if ok {
  276. lastNode = val
  277. continue
  278. }
  279. }
  280. nodeIdx, err := addNode(&nodes, lastNode)
  281. if err != nil {
  282. return nil, -1, fmt.Errorf("failed to add node %d: %v", lastNode, err)
  283. }
  284. nodes[nodeIdx].AddThread(lastThread, lastCore)
  285. if numCores < 1 {
  286. return nil, numCores, fmt.Errorf("could not detect any cores")
  287. }
  288. for idx, node := range nodes {
  289. caches, err := sysinfo.GetCacheInfo(sysFs, node.Cores[0].Threads[0])
  290. if err != nil {
  291. klog.Errorf("failed to get cache information for node %d: %v", node.Id, err)
  292. continue
  293. }
  294. numThreadsPerCore := len(node.Cores[0].Threads)
  295. numThreadsPerNode := len(node.Cores) * numThreadsPerCore
  296. for _, cache := range caches {
  297. c := info.Cache{
  298. Size: cache.Size,
  299. Level: cache.Level,
  300. Type: cache.Type,
  301. }
  302. if cache.Cpus == numThreadsPerNode && cache.Level > 2 {
  303. // Add a node-level cache.
  304. nodes[idx].AddNodeCache(c)
  305. } else if cache.Cpus == numThreadsPerCore {
  306. // Add to each core.
  307. nodes[idx].AddPerCoreCache(c)
  308. }
  309. // Ignore unknown caches.
  310. }
  311. }
  312. return nodes, numCores, nil
  313. }
  314. func extractValue(s string, r *regexp.Regexp) (bool, int, error) {
  315. matches := r.FindSubmatch([]byte(s))
  316. if len(matches) == 2 {
  317. val, err := strconv.ParseInt(string(matches[1]), 10, 32)
  318. if err != nil {
  319. return false, -1, err
  320. }
  321. return true, int(val), nil
  322. }
  323. return false, -1, nil
  324. }
  325. func findNode(nodes []info.Node, id int) (bool, int) {
  326. for i, n := range nodes {
  327. if n.Id == id {
  328. return true, i
  329. }
  330. }
  331. return false, -1
  332. }
  333. func addNode(nodes *[]info.Node, id int) (int, error) {
  334. var idx int
  335. if id == -1 {
  336. // Some VMs don't fill topology data. Export single package.
  337. id = 0
  338. }
  339. ok, idx := findNode(*nodes, id)
  340. if !ok {
  341. // New node
  342. node := info.Node{Id: id}
  343. // Add per-node memory information.
  344. meminfo := fmt.Sprintf("/sys/devices/system/node/node%d/meminfo", id)
  345. out, err := ioutil.ReadFile(meminfo)
  346. // Ignore if per-node info is not available.
  347. if err == nil {
  348. m, err := parseCapacity(out, memoryCapacityRegexp)
  349. if err != nil {
  350. return -1, err
  351. }
  352. node.Memory = uint64(m)
  353. }
  354. // Look for per-node hugepages info using node id
  355. // Such as: /sys/devices/system/node/node%d/hugepages
  356. hugepagesDirectory := fmt.Sprintf("%s/node%d/hugepages/", nodePath, id)
  357. hugePagesInfo, err := GetHugePagesInfo(hugepagesDirectory)
  358. if err != nil {
  359. return -1, err
  360. }
  361. node.HugePages = hugePagesInfo
  362. *nodes = append(*nodes, node)
  363. idx = len(*nodes) - 1
  364. }
  365. return idx, nil
  366. }
  367. // s390/s390x changes
  368. func getMachineArch() (string, error) {
  369. uname := unix.Utsname{}
  370. err := unix.Uname(&uname)
  371. if err != nil {
  372. return "", err
  373. }
  374. return string(uname.Machine[:]), nil
  375. }
  376. // arm32 chanes
  377. func isArm32() bool {
  378. arch, err := getMachineArch()
  379. if err == nil {
  380. return strings.Contains(arch, "arm")
  381. }
  382. return false
  383. }
  384. // aarch64 changes
  385. func isAArch64() bool {
  386. arch, err := getMachineArch()
  387. if err == nil {
  388. return strings.Contains(arch, "aarch64")
  389. }
  390. return false
  391. }
  392. // s390/s390x changes
  393. func isSystemZ() bool {
  394. arch, err := getMachineArch()
  395. if err == nil {
  396. return strings.Contains(arch, "390")
  397. }
  398. return false
  399. }
  400. // s390/s390x changes
  401. func getNumCores() int {
  402. maxProcs := runtime.GOMAXPROCS(0)
  403. numCPU := runtime.NumCPU()
  404. if maxProcs < numCPU {
  405. return maxProcs
  406. }
  407. return numCPU
  408. }