manager.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 cgrouputils
  15. import (
  16. "strings"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/onecloud/pkg/util/cgrouputils/cgroup"
  19. "yunion.io/x/onecloud/pkg/util/cgrouputils/cgroupv1"
  20. "yunion.io/x/onecloud/pkg/util/cgrouputils/cgroupv2"
  21. "yunion.io/x/onecloud/pkg/util/fileutils2"
  22. "yunion.io/x/onecloud/pkg/util/procutils"
  23. )
  24. const (
  25. CGROUP_PATH_SYSFS = "/sys/fs/cgroup"
  26. CGROUP_PATH_ROOT = "/cgroup"
  27. CPUSET_SCHED_LOAD_BALANCE = "cpuset.sched_load_balance"
  28. CPUSET_CLONE_CHILDREN = "cgroup.clone_children"
  29. )
  30. type ICgroupManager interface {
  31. RebalanceProcesses(pids []string)
  32. CgroupCleanAll(subName string)
  33. CgroupDestroy(pid, name string) bool
  34. GetCgroupPath() string
  35. GetSubModulePath(module string) string
  36. NewCGroupCPUSetTask(pid, name, cpuset, mems string) cgroup.ICGroupTask
  37. NewCGroupCPUTask(pid, name string, cpuShares int) cgroup.ICGroupTask
  38. NewCGroupSubCPUSetTask(pid, name string, cpuset string, threadIds []string) cgroup.ICGroupTask
  39. }
  40. func GetCgroupVersion() string {
  41. return cgroupManager.GetCgroupPath()
  42. }
  43. func RebalanceProcesses(pids []string) {
  44. cgroupManager.RebalanceProcesses(pids)
  45. }
  46. func CgroupCleanAll(subName string) {
  47. cgroupManager.CgroupCleanAll(subName)
  48. }
  49. func CgroupDestroy(pid, name string) bool {
  50. return cgroupManager.CgroupDestroy(pid, name)
  51. }
  52. func GetCgroupPath() string {
  53. return cgroupManager.GetCgroupPath()
  54. }
  55. func GetSubModulePath(module string) string {
  56. return cgroupManager.GetSubModulePath(module)
  57. }
  58. func NewCGroupCPUTask(pid, name string, cpuShares int) cgroup.ICGroupTask {
  59. return cgroupManager.NewCGroupCPUTask(pid, name, cpuShares)
  60. }
  61. func NewCGroupCPUSetTask(pid, name, cpuset, mems string) cgroup.ICGroupTask {
  62. return cgroupManager.NewCGroupCPUSetTask(pid, name, cpuset, mems)
  63. }
  64. func NewCGroupSubCPUSetTask(pid, name string, cpuset string, threadIds []string) cgroup.ICGroupTask {
  65. return cgroupManager.NewCGroupSubCPUSetTask(pid, name, cpuset, threadIds)
  66. }
  67. var cgroupManager ICgroupManager
  68. func Init(ioScheduler string) error {
  69. if cgroupManager != nil {
  70. return nil
  71. }
  72. cgroupPath := ""
  73. if fileutils2.Exists(CGROUP_PATH_SYSFS) {
  74. cgroupPath = CGROUP_PATH_SYSFS
  75. } else if fileutils2.Exists("CGROUP_PATH_ROOT") {
  76. cgroupPath = CGROUP_PATH_ROOT
  77. }
  78. if cgroupPath == "" {
  79. return errors.Errorf("Can't detect cgroup path")
  80. }
  81. output, err := procutils.NewCommand("stat", "-fc", "%T", cgroupPath).Output()
  82. if err != nil {
  83. return errors.Wrapf(err, "stat cgroup path %s", cgroupPath)
  84. }
  85. cgroupfs := strings.TrimSpace(string(output))
  86. if cgroupfs == "cgroup2fs" {
  87. // cgroup v2
  88. cgroupManager, err = cgroupv2.Init(cgroupPath, ioScheduler)
  89. } else {
  90. // cgroup v1
  91. cgroupManager, err = cgroupv1.Init(cgroupPath, ioScheduler)
  92. }
  93. if err != nil {
  94. return errors.Wrap(err, "init cgroup")
  95. }
  96. return nil
  97. }