cpuset.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "bytes"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. specs "github.com/opencontainers/runtime-spec/specs-go"
  20. )
  21. func NewCpuset(root string) *cpusetController {
  22. return &cpusetController{
  23. root: filepath.Join(root, string(Cpuset)),
  24. }
  25. }
  26. type cpusetController struct {
  27. root string
  28. }
  29. func (c *cpusetController) Name() Name {
  30. return Cpuset
  31. }
  32. func (c *cpusetController) Path(path string) string {
  33. return filepath.Join(c.root, path)
  34. }
  35. func (c *cpusetController) Create(path string, resources *specs.LinuxResources) error {
  36. if err := c.ensureParent(c.Path(path), c.root); err != nil {
  37. return err
  38. }
  39. if err := os.MkdirAll(c.Path(path), defaultDirPerm); err != nil {
  40. return err
  41. }
  42. if err := c.copyIfNeeded(c.Path(path), filepath.Dir(c.Path(path))); err != nil {
  43. return err
  44. }
  45. if resources.CPU != nil {
  46. for _, t := range []struct {
  47. name string
  48. value string
  49. }{
  50. {
  51. name: "cpus",
  52. value: resources.CPU.Cpus,
  53. },
  54. {
  55. name: "mems",
  56. value: resources.CPU.Mems,
  57. },
  58. } {
  59. if t.value != "" {
  60. if err := os.WriteFile(
  61. filepath.Join(c.Path(path), "cpuset."+t.name),
  62. []byte(t.value),
  63. defaultFilePerm,
  64. ); err != nil {
  65. return err
  66. }
  67. }
  68. }
  69. }
  70. return nil
  71. }
  72. func (c *cpusetController) Update(path string, resources *specs.LinuxResources) error {
  73. return c.Create(path, resources)
  74. }
  75. func (c *cpusetController) getValues(path string) (cpus []byte, mems []byte, err error) {
  76. if cpus, err = os.ReadFile(filepath.Join(path, "cpuset.cpus")); err != nil && !os.IsNotExist(err) {
  77. return
  78. }
  79. if mems, err = os.ReadFile(filepath.Join(path, "cpuset.mems")); err != nil && !os.IsNotExist(err) {
  80. return
  81. }
  82. return cpus, mems, nil
  83. }
  84. // ensureParent makes sure that the parent directory of current is created
  85. // and populated with the proper cpus and mems files copied from
  86. // it's parent.
  87. func (c *cpusetController) ensureParent(current, root string) error {
  88. parent := filepath.Dir(current)
  89. if _, err := filepath.Rel(root, parent); err != nil {
  90. return nil
  91. }
  92. // Avoid infinite recursion.
  93. if parent == current {
  94. return fmt.Errorf("cpuset: cgroup parent path outside cgroup root")
  95. }
  96. if cleanPath(parent) != root {
  97. if err := c.ensureParent(parent, root); err != nil {
  98. return err
  99. }
  100. }
  101. if err := os.MkdirAll(current, defaultDirPerm); err != nil {
  102. return err
  103. }
  104. return c.copyIfNeeded(current, parent)
  105. }
  106. // copyIfNeeded copies the cpuset.cpus and cpuset.mems from the parent
  107. // directory to the current directory if the file's contents are 0
  108. func (c *cpusetController) copyIfNeeded(current, parent string) error {
  109. var (
  110. err error
  111. currentCpus, currentMems []byte
  112. parentCpus, parentMems []byte
  113. )
  114. if currentCpus, currentMems, err = c.getValues(current); err != nil {
  115. return err
  116. }
  117. if parentCpus, parentMems, err = c.getValues(parent); err != nil {
  118. return err
  119. }
  120. if isEmpty(currentCpus) {
  121. if err := os.WriteFile(
  122. filepath.Join(current, "cpuset.cpus"),
  123. parentCpus,
  124. defaultFilePerm,
  125. ); err != nil {
  126. return err
  127. }
  128. }
  129. if isEmpty(currentMems) {
  130. if err := os.WriteFile(
  131. filepath.Join(current, "cpuset.mems"),
  132. parentMems,
  133. defaultFilePerm,
  134. ); err != nil {
  135. return err
  136. }
  137. }
  138. return nil
  139. }
  140. func isEmpty(b []byte) bool {
  141. return len(bytes.Trim(b, "\n")) == 0
  142. }