cpu.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package cgroup
  2. import (
  3. "bufio"
  4. "os"
  5. "path/filepath"
  6. )
  7. // CPUSubsystem contains metrics and limits from the "cpu" subsystem. This
  8. // subsystem is used to guarantee a minimum number of cpu shares to the cgroup
  9. // when the system is busy. This subsystem does not track CPU usage, for that
  10. // information see the "cpuacct" subsystem.
  11. type CPUSubsystem struct {
  12. Metadata
  13. // Completely Fair Scheduler (CFS) settings.
  14. CFS CFS `json:"cfs,omitempty"`
  15. // Real-time (RT) Scheduler settings.
  16. RT RT `json:"rt,omitempty"`
  17. // CPU time statistics for tasks in this cgroup.
  18. Stats ThrottleStats `json:"stats,omitempty"`
  19. }
  20. // RT contains the tunable parameters for the real-time scheduler.
  21. type RT struct {
  22. // Period of time in microseconds for how regularly the cgroup's access to
  23. // CPU resources should be reallocated.
  24. PeriodMicros uint64 `json:"period_us"`
  25. // Period of time in microseconds for the longest continuous period in which
  26. // the tasks in the cgroup have access to CPU resources.
  27. RuntimeMicros uint64 `json:"quota_us"`
  28. }
  29. // CFS contains the tunable parameters for the completely fair scheduler.
  30. type CFS struct {
  31. // Period of time in microseconds for how regularly the cgroup's access to
  32. // CPU resources should be reallocated.
  33. PeriodMicros uint64 `json:"period_us"`
  34. // Total amount of time in microseconds for which all tasks in the cgroup
  35. // can run during one period.
  36. QuotaMicros uint64 `json:"quota_us"`
  37. // Relative share of CPU time available to tasks the cgroup. The value is
  38. // an integer greater than or equal to 2.
  39. Shares uint64 `json:"shares"`
  40. }
  41. // ThrottleStats contains stats that indicate the extent to which this cgroup's
  42. // CPU usage was throttled.
  43. type ThrottleStats struct {
  44. // Number of periods with throttling active.
  45. Periods uint64 `json:"periods,omitempty"`
  46. // Number of periods when the cgroup hit its throttling limit.
  47. ThrottledPeriods uint64 `json:"throttled_periods,omitempty"`
  48. // Aggregate time the cgroup was throttled for in nanoseconds.
  49. ThrottledTimeNanos uint64 `json:"throttled_nanos,omitempty"`
  50. }
  51. // get reads metrics from the "cpu" subsystem. path is the filepath to the
  52. // cgroup hierarchy to read.
  53. func (cpu *CPUSubsystem) get(path string) error {
  54. if err := cpuCFS(path, cpu); err != nil {
  55. return err
  56. }
  57. if err := cpuRT(path, cpu); err != nil {
  58. return err
  59. }
  60. if err := cpuStat(path, cpu); err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. func cpuStat(path string, cpu *CPUSubsystem) error {
  66. f, err := os.Open(filepath.Join(path, "cpu.stat"))
  67. if err != nil {
  68. if os.IsNotExist(err) {
  69. return nil
  70. }
  71. return err
  72. }
  73. defer f.Close()
  74. sc := bufio.NewScanner(f)
  75. for sc.Scan() {
  76. t, v, err := parseCgroupParamKeyValue(sc.Text())
  77. if err != nil {
  78. return err
  79. }
  80. switch t {
  81. case "nr_periods":
  82. cpu.Stats.Periods = v
  83. case "nr_throttled":
  84. cpu.Stats.ThrottledPeriods = v
  85. case "throttled_time":
  86. cpu.Stats.ThrottledTimeNanos = v
  87. }
  88. }
  89. return sc.Err()
  90. }
  91. func cpuCFS(path string, cpu *CPUSubsystem) error {
  92. var err error
  93. cpu.CFS.PeriodMicros, err = parseUintFromFile(path, "cpu.cfs_period_us")
  94. if err != nil {
  95. return err
  96. }
  97. cpu.CFS.QuotaMicros, err = parseUintFromFile(path, "cpu.cfs_quota_us")
  98. if err != nil {
  99. return err
  100. }
  101. cpu.CFS.Shares, err = parseUintFromFile(path, "cpu.shares")
  102. if err != nil {
  103. return err
  104. }
  105. return nil
  106. }
  107. func cpuRT(path string, cpu *CPUSubsystem) error {
  108. var err error
  109. cpu.RT.PeriodMicros, err = parseUintFromFile(path, "cpu.rt_period_us")
  110. if err != nil {
  111. return err
  112. }
  113. cpu.RT.RuntimeMicros, err = parseUintFromFile(path, "cpu.rt_runtime_us")
  114. if err != nil {
  115. return err
  116. }
  117. return nil
  118. }