cpu_linux.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package internal
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "git.i2edu.net/i2/go-zero/core/iox"
  8. "git.i2edu.net/i2/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 calculation, 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. // RefreshCpu refreshes cpu usage and returns.
  60. func RefreshCpu() uint64 {
  61. total, err := totalCpuUsage()
  62. if err != nil {
  63. return 0
  64. }
  65. system, err := systemCpuUsage()
  66. if err != nil {
  67. return 0
  68. }
  69. var usage uint64
  70. cpuDelta := total - preTotal
  71. systemDelta := system - preSystem
  72. if cpuDelta > 0 && systemDelta > 0 {
  73. usage = uint64(float64(cpuDelta*cores*1e3) / (float64(systemDelta) * quota))
  74. }
  75. preSystem = system
  76. preTotal = total
  77. return usage
  78. }
  79. func cpuQuota() (int64, error) {
  80. cg, err := currentCgroup()
  81. if err != nil {
  82. return 0, err
  83. }
  84. return cg.cpuQuotaUs()
  85. }
  86. func cpuPeriod() (uint64, error) {
  87. cg, err := currentCgroup()
  88. if err != nil {
  89. return 0, err
  90. }
  91. return cg.cpuPeriodUs()
  92. }
  93. func cpuSets() ([]uint64, error) {
  94. cg, err := currentCgroup()
  95. if err != nil {
  96. return nil, err
  97. }
  98. return cg.cpus()
  99. }
  100. func perCpuUsage() ([]uint64, error) {
  101. cg, err := currentCgroup()
  102. if err != nil {
  103. return nil, err
  104. }
  105. return cg.acctUsagePerCpu()
  106. }
  107. func systemCpuUsage() (uint64, error) {
  108. lines, err := iox.ReadTextLines("/proc/stat", iox.WithoutBlank())
  109. if err != nil {
  110. return 0, err
  111. }
  112. for _, line := range lines {
  113. fields := strings.Fields(line)
  114. if fields[0] == "cpu" {
  115. if len(fields) < cpuFields {
  116. return 0, fmt.Errorf("bad format of cpu stats")
  117. }
  118. var totalClockTicks uint64
  119. for _, i := range fields[1:cpuFields] {
  120. v, err := parseUint(i)
  121. if err != nil {
  122. return 0, err
  123. }
  124. totalClockTicks += v
  125. }
  126. return (totalClockTicks * uint64(time.Second)) / cpuTicks, nil
  127. }
  128. }
  129. return 0, errors.New("bad stats format")
  130. }
  131. func totalCpuUsage() (usage uint64, err error) {
  132. var cg *cgroup
  133. if cg, err = currentCgroup(); err != nil {
  134. return
  135. }
  136. return cg.acctUsageAllCpus()
  137. }