cpu_linux.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // +build linux
  2. package cpu
  3. import (
  4. "bufio"
  5. "fmt"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "github.com/pkg/errors"
  10. )
  11. const nanoSecondsPerSecond = 1e9
  12. // ErrNoCFSLimit is no quota limit
  13. var ErrNoCFSLimit = errors.Errorf("no quota limit")
  14. var clockTicksPerSecond = uint64(GetClockTicks())
  15. // systemCPUUsage returns the host system's cpu usage in
  16. // nanoseconds. An error is returned if the format of the underlying
  17. // file does not match.
  18. //
  19. // Uses /proc/stat defined by POSIX. Looks for the cpu
  20. // statistics line and then sums up the first seven fields
  21. // provided. See man 5 proc for details on specific field
  22. // information.
  23. func systemCPUUsage() (usage uint64, err error) {
  24. var (
  25. line string
  26. f *os.File
  27. )
  28. if f, err = os.Open("/proc/stat"); err != nil {
  29. return
  30. }
  31. bufReader := bufio.NewReaderSize(nil, 128)
  32. defer func() {
  33. bufReader.Reset(nil)
  34. f.Close()
  35. }()
  36. bufReader.Reset(f)
  37. for err == nil {
  38. if line, err = bufReader.ReadString('\n'); err != nil {
  39. err = errors.WithStack(err)
  40. return
  41. }
  42. parts := strings.Fields(line)
  43. switch parts[0] {
  44. case "cpu":
  45. if len(parts) < 8 {
  46. err = errors.WithStack(fmt.Errorf("bad format of cpu stats"))
  47. return
  48. }
  49. var totalClockTicks uint64
  50. for _, i := range parts[1:8] {
  51. var v uint64
  52. if v, err = strconv.ParseUint(i, 10, 64); err != nil {
  53. err = errors.WithStack(fmt.Errorf("error parsing cpu stats"))
  54. return
  55. }
  56. totalClockTicks += v
  57. }
  58. usage = (totalClockTicks * nanoSecondsPerSecond) / clockTicksPerSecond
  59. return
  60. }
  61. }
  62. err = errors.Errorf("bad stats format")
  63. return
  64. }
  65. func totalCPUUsage() (usage uint64, err error) {
  66. var cg *cgroup
  67. if cg, err = currentcGroup(); err != nil {
  68. return
  69. }
  70. return cg.CPUAcctUsage()
  71. }
  72. func perCPUUsage() (usage []uint64, err error) {
  73. var cg *cgroup
  74. if cg, err = currentcGroup(); err != nil {
  75. return
  76. }
  77. return cg.CPUAcctUsagePerCPU()
  78. }
  79. func cpuSets() (sets []uint64, err error) {
  80. var cg *cgroup
  81. if cg, err = currentcGroup(); err != nil {
  82. return
  83. }
  84. return cg.CPUSetCPUs()
  85. }
  86. func cpuQuota() (quota int64, err error) {
  87. var cg *cgroup
  88. if cg, err = currentcGroup(); err != nil {
  89. return
  90. }
  91. return cg.CPUCFSQuotaUs()
  92. }
  93. func cpuPeriod() (peroid uint64, err error) {
  94. var cg *cgroup
  95. if cg, err = currentcGroup(); err != nil {
  96. return
  97. }
  98. return cg.CPUCFSPeriodUs()
  99. }
  100. func cpuFreq() uint64 {
  101. lines, err := readLines("/proc/cpuinfo")
  102. if err != nil {
  103. return 0
  104. }
  105. for _, line := range lines {
  106. fields := strings.Split(line, ":")
  107. if len(fields) < 2 {
  108. continue
  109. }
  110. key := strings.TrimSpace(fields[0])
  111. value := strings.TrimSpace(fields[1])
  112. if key == "cpu MHz" || key == "clock" {
  113. // treat this as the fallback value, thus we ignore error
  114. if t, err := strconv.ParseFloat(strings.Replace(value, "MHz", "", 1), 64); err == nil {
  115. return uint64(t * 1000.0 * 1000.0)
  116. }
  117. }
  118. }
  119. return 0
  120. }
  121. func cpuMaxFreq() uint64 {
  122. feq := cpuFreq()
  123. data, err := readFile("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq")
  124. if err != nil {
  125. return feq
  126. }
  127. // override the max freq from /proc/cpuinfo
  128. cfeq, err := parseUint(data)
  129. if err == nil {
  130. feq = cfeq
  131. }
  132. return feq
  133. }