cpu.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cgroup1
  14. import (
  15. "bufio"
  16. "os"
  17. "path/filepath"
  18. "strconv"
  19. v1 "github.com/containerd/cgroups/v3/cgroup1/stats"
  20. specs "github.com/opencontainers/runtime-spec/specs-go"
  21. )
  22. func NewCpu(root string) *cpuController {
  23. return &cpuController{
  24. root: filepath.Join(root, string(Cpu)),
  25. }
  26. }
  27. type cpuController struct {
  28. root string
  29. }
  30. func (c *cpuController) Name() Name {
  31. return Cpu
  32. }
  33. func (c *cpuController) Path(path string) string {
  34. return filepath.Join(c.root, path)
  35. }
  36. func (c *cpuController) Create(path string, resources *specs.LinuxResources) error {
  37. if err := os.MkdirAll(c.Path(path), defaultDirPerm); err != nil {
  38. return err
  39. }
  40. if cpu := resources.CPU; cpu != nil {
  41. for _, t := range []struct {
  42. name string
  43. ivalue *int64
  44. uvalue *uint64
  45. }{
  46. {
  47. name: "rt_period_us",
  48. uvalue: cpu.RealtimePeriod,
  49. },
  50. {
  51. name: "rt_runtime_us",
  52. ivalue: cpu.RealtimeRuntime,
  53. },
  54. {
  55. name: "shares",
  56. uvalue: cpu.Shares,
  57. },
  58. {
  59. name: "cfs_period_us",
  60. uvalue: cpu.Period,
  61. },
  62. {
  63. name: "cfs_quota_us",
  64. ivalue: cpu.Quota,
  65. },
  66. } {
  67. var value []byte
  68. if t.uvalue != nil {
  69. value = []byte(strconv.FormatUint(*t.uvalue, 10))
  70. } else if t.ivalue != nil {
  71. value = []byte(strconv.FormatInt(*t.ivalue, 10))
  72. }
  73. if value != nil {
  74. if err := os.WriteFile(
  75. filepath.Join(c.Path(path), "cpu."+t.name),
  76. value,
  77. defaultFilePerm,
  78. ); err != nil {
  79. return err
  80. }
  81. }
  82. }
  83. }
  84. return nil
  85. }
  86. func (c *cpuController) Update(path string, resources *specs.LinuxResources) error {
  87. return c.Create(path, resources)
  88. }
  89. func (c *cpuController) Stat(path string, stats *v1.Metrics) error {
  90. f, err := os.Open(filepath.Join(c.Path(path), "cpu.stat"))
  91. if err != nil {
  92. return err
  93. }
  94. defer f.Close()
  95. // get or create the cpu field because cpuacct can also set values on this struct
  96. sc := bufio.NewScanner(f)
  97. for sc.Scan() {
  98. key, v, err := parseKV(sc.Text())
  99. if err != nil {
  100. return err
  101. }
  102. switch key {
  103. case "nr_periods":
  104. stats.CPU.Throttling.Periods = v
  105. case "nr_throttled":
  106. stats.CPU.Throttling.ThrottledPeriods = v
  107. case "throttled_time":
  108. stats.CPU.Throttling.ThrottledTime = v
  109. }
  110. }
  111. return sc.Err()
  112. }