cpu_linux.go 2.6 KB

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