cpu_linux.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package internal
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "sync"
  7. "time"
  8. "github.com/zeromicro/go-zero/core/iox"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. const (
  12. cpuTicks = 100
  13. cpuFields = 8
  14. )
  15. var (
  16. preSystem uint64
  17. preTotal uint64
  18. quota float64
  19. cores uint64
  20. )
  21. var initonce sync.Once
  22. // if /proc not present, ignore the cpu calculation, like wsl linux
  23. func initialize() {
  24. cpus, err := cpuSets()
  25. if err != nil {
  26. logx.Error(err)
  27. return
  28. }
  29. cores = uint64(len(cpus))
  30. sets, err := cpuSets()
  31. if err != nil {
  32. logx.Error(err)
  33. return
  34. }
  35. quota = float64(len(sets))
  36. cq, err := cpuQuota()
  37. if err == nil {
  38. if cq != -1 {
  39. period, err := cpuPeriod()
  40. if err != nil {
  41. logx.Error(err)
  42. return
  43. }
  44. limit := float64(cq) / float64(period)
  45. if limit < quota {
  46. quota = limit
  47. }
  48. }
  49. }
  50. preSystem, err = systemCpuUsage()
  51. if err != nil {
  52. logx.Error(err)
  53. return
  54. }
  55. preTotal, err = totalCpuUsage()
  56. if err != nil {
  57. logx.Error(err)
  58. return
  59. }
  60. }
  61. // RefreshCpu refreshes cpu usage and returns.
  62. func RefreshCpu() uint64 {
  63. initonce.Do(initialize)
  64. total, err := totalCpuUsage()
  65. if err != nil {
  66. return 0
  67. }
  68. system, err := systemCpuUsage()
  69. if err != nil {
  70. return 0
  71. }
  72. var usage uint64
  73. cpuDelta := total - preTotal
  74. systemDelta := system - preSystem
  75. if cpuDelta > 0 && systemDelta > 0 {
  76. usage = uint64(float64(cpuDelta*cores*1e3) / (float64(systemDelta) * quota))
  77. }
  78. preSystem = system
  79. preTotal = total
  80. return usage
  81. }
  82. func cpuQuota() (int64, error) {
  83. cg, err := currentCgroup()
  84. if err != nil {
  85. return 0, err
  86. }
  87. return cg.cpuQuotaUs()
  88. }
  89. func cpuPeriod() (uint64, error) {
  90. cg, err := currentCgroup()
  91. if err != nil {
  92. return 0, err
  93. }
  94. return cg.cpuPeriodUs()
  95. }
  96. func cpuSets() ([]uint64, error) {
  97. cg, err := currentCgroup()
  98. if err != nil {
  99. return nil, err
  100. }
  101. return cg.cpus()
  102. }
  103. func systemCpuUsage() (uint64, error) {
  104. lines, err := iox.ReadTextLines("/proc/stat", iox.WithoutBlank())
  105. if err != nil {
  106. return 0, err
  107. }
  108. for _, line := range lines {
  109. fields := strings.Fields(line)
  110. if fields[0] == "cpu" {
  111. if len(fields) < cpuFields {
  112. return 0, fmt.Errorf("bad format of cpu stats")
  113. }
  114. var totalClockTicks uint64
  115. for _, i := range fields[1:cpuFields] {
  116. v, err := parseUint(i)
  117. if err != nil {
  118. return 0, err
  119. }
  120. totalClockTicks += v
  121. }
  122. return (totalClockTicks * uint64(time.Second)) / cpuTicks, nil
  123. }
  124. }
  125. return 0, errors.New("bad stats format")
  126. }
  127. func totalCpuUsage() (usage uint64, err error) {
  128. var cg cgroup
  129. if cg, err = currentCgroup(); err != nil {
  130. return
  131. }
  132. return cg.usageAllCpus()
  133. }