numcpus_linux.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2018 Tobias Klauser
  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. package numcpus
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "strconv"
  20. "strings"
  21. "golang.org/x/sys/unix"
  22. )
  23. const sysfsCPUBasePath = "/sys/devices/system/cpu"
  24. func getFromCPUAffinity() (int, error) {
  25. var cpuSet unix.CPUSet
  26. if err := unix.SchedGetaffinity(0, &cpuSet); err != nil {
  27. return 0, err
  28. }
  29. return cpuSet.Count(), nil
  30. }
  31. func readCPURange(file string) (int, error) {
  32. buf, err := ioutil.ReadFile(filepath.Join(sysfsCPUBasePath, file))
  33. if err != nil {
  34. return 0, err
  35. }
  36. return parseCPURange(strings.Trim(string(buf), "\n "))
  37. }
  38. func parseCPURange(cpus string) (int, error) {
  39. n := int(0)
  40. for _, cpuRange := range strings.Split(cpus, ",") {
  41. if len(cpuRange) == 0 {
  42. continue
  43. }
  44. rangeOp := strings.SplitN(cpuRange, "-", 2)
  45. first, err := strconv.ParseUint(rangeOp[0], 10, 32)
  46. if err != nil {
  47. return 0, err
  48. }
  49. if len(rangeOp) == 1 {
  50. n++
  51. continue
  52. }
  53. last, err := strconv.ParseUint(rangeOp[1], 10, 32)
  54. if err != nil {
  55. return 0, err
  56. }
  57. n += int(last - first + 1)
  58. }
  59. return n, nil
  60. }
  61. func getConfigured() (int, error) {
  62. d, err := os.Open(sysfsCPUBasePath)
  63. if err != nil {
  64. return 0, err
  65. }
  66. defer d.Close()
  67. fis, err := d.Readdir(-1)
  68. if err != nil {
  69. return 0, err
  70. }
  71. count := 0
  72. for _, fi := range fis {
  73. if name := fi.Name(); fi.IsDir() && strings.HasPrefix(name, "cpu") {
  74. _, err := strconv.ParseInt(name[3:], 10, 64)
  75. if err == nil {
  76. count++
  77. }
  78. }
  79. }
  80. return count, nil
  81. }
  82. func getKernelMax() (int, error) {
  83. buf, err := ioutil.ReadFile(filepath.Join(sysfsCPUBasePath, "kernel_max"))
  84. if err != nil {
  85. return 0, err
  86. }
  87. n, err := strconv.ParseInt(strings.Trim(string(buf), "\n "), 10, 32)
  88. if err != nil {
  89. return 0, err
  90. }
  91. return int(n), nil
  92. }
  93. func getOffline() (int, error) {
  94. return readCPURange("offline")
  95. }
  96. func getOnline() (int, error) {
  97. if n, err := getFromCPUAffinity(); err == nil {
  98. return n, nil
  99. }
  100. return readCPURange("online")
  101. }
  102. func getPossible() (int, error) {
  103. return readCPURange("possible")
  104. }
  105. func getPresent() (int, error) {
  106. return readCPURange("present")
  107. }