manager.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cgroupv2
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path"
  20. "regexp"
  21. "strings"
  22. "yunion.io/x/log"
  23. "yunion.io/x/pkg/errors"
  24. "yunion.io/x/onecloud/pkg/util/cgrouputils/cgroup"
  25. "yunion.io/x/onecloud/pkg/util/fileutils2"
  26. )
  27. var (
  28. manager *cgroupManager
  29. )
  30. type cgroupManager struct {
  31. cgroupPath string
  32. ioScheduler string
  33. }
  34. func (m *cgroupManager) GetCgroupPath() string {
  35. return m.cgroupPath
  36. }
  37. func (m *cgroupManager) GetSubModulePath(module string) string {
  38. return m.cgroupPath
  39. }
  40. func (m *cgroupManager) GetCgroupVersion() string {
  41. return cgroup.CGROUP_V2
  42. }
  43. func (m *cgroupManager) GetIoScheduler() string {
  44. return m.ioScheduler
  45. }
  46. func setParam(name, value string, groups ...string) error {
  47. groupPath := manager.GetCgroupPath()
  48. if len(groups) > 0 {
  49. groups = append([]string{groupPath}, groups...)
  50. groupPath = path.Join(groups...)
  51. }
  52. configPath := path.Join(groupPath, name)
  53. return ioutil.WriteFile(configPath, []byte(value), 0644)
  54. }
  55. func getParam(name, group string) string {
  56. configPath := path.Join(manager.GetCgroupPath(), group, name)
  57. param, err := fileutils2.FileGetContents(configPath)
  58. if err != nil {
  59. log.Errorf("failed get cgroup config %s: %s", configPath, err)
  60. return ""
  61. }
  62. return strings.TrimSpace(param)
  63. }
  64. func Init(cgroupPath, ioScheduler string) (*cgroupManager, error) {
  65. if manager != nil {
  66. return manager, nil
  67. }
  68. manager = &cgroupManager{
  69. cgroupPath: cgroupPath,
  70. ioScheduler: ioScheduler,
  71. }
  72. for _, module := range []string{"cpu", "cpuset"} {
  73. err := initSubGroups(module)
  74. if err != nil {
  75. manager = nil
  76. return nil, err
  77. }
  78. }
  79. return manager, nil
  80. }
  81. func initSubGroups(module string, groups ...string) error {
  82. err := setParam(CGROUP_SUBTREE_CONTROL, fmt.Sprintf("+%s", module), groups...)
  83. if err != nil {
  84. return errors.Wrapf(err, "failed add %s to cgroup.subtree_control", module)
  85. }
  86. return nil
  87. }
  88. func (m *cgroupManager) RebalanceProcesses(pids []string) {}
  89. func (m *cgroupManager) CgroupDestroy(pid, name string) bool {
  90. tasks := []cgroup.ICGroupTask{
  91. &CGroupCPUTask{CgroupTask: NewCGroupBaseTask(pid, name, nil)},
  92. &CGroupCPUSetTask{CgroupTask: NewCGroupBaseTask(pid, name, nil)},
  93. }
  94. for _, t := range tasks {
  95. t.SetHand(t)
  96. if !t.RemoveTask() {
  97. return false
  98. }
  99. }
  100. return true
  101. }
  102. func (m *cgroupManager) CgroupCleanAll(subName string) {
  103. cgroupPath := manager.GetCgroupPath()
  104. if subName != "" {
  105. cgroupPath = path.Join(cgroupPath, subName)
  106. }
  107. files, err := ioutil.ReadDir(cgroupPath)
  108. if err != nil {
  109. log.Errorf("%s GetTasks failed: %s", cgroupPath, err)
  110. return
  111. }
  112. ids := []string{}
  113. for _, file := range files {
  114. ids = append(ids, file.Name())
  115. }
  116. re1 := regexp.MustCompile(`^\d+$`)
  117. re2 := regexp.MustCompile(`^server_[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}_\d+$`)
  118. for _, pid := range ids {
  119. spid := ""
  120. if re1.MatchString(pid) {
  121. spid = pid
  122. } else if re2.MatchString(pid) {
  123. segs := strings.Split(pid, "_")
  124. spid = segs[len(segs)-1]
  125. }
  126. if fileutils2.IsDir(path.Join(cgroupPath, pid)) {
  127. if !fileutils2.Exists(path.Join("/proc", spid)) {
  128. log.Infof("Cgroup cleanup %s", path.Join(cgroupPath, pid))
  129. }
  130. subFiles, err := ioutil.ReadDir(path.Join(cgroupPath, pid))
  131. if err != nil {
  132. log.Errorf("sub dir %s GetTaskIds failed: %s", path.Join(cgroupPath, pid), err)
  133. } else {
  134. for _, fi := range subFiles {
  135. if !fi.IsDir() {
  136. continue
  137. }
  138. if err := os.Remove(path.Join(cgroupPath, pid, fi.Name())); err != nil {
  139. log.Errorf("CgroupCleanAll pid=%s tid=%s error: %s", pid, fi.Name(), err)
  140. }
  141. }
  142. }
  143. if err := os.Remove(path.Join(cgroupPath, pid)); err != nil {
  144. log.Errorf("CgroupCleanAll pid=%s error: %s", pid, err)
  145. }
  146. }
  147. }
  148. }