cgroup.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. "os"
  18. "path"
  19. "path/filepath"
  20. "regexp"
  21. "strings"
  22. "yunion.io/x/log"
  23. "yunion.io/x/onecloud/pkg/util/cgrouputils/cgroup"
  24. "yunion.io/x/onecloud/pkg/util/fileutils2"
  25. )
  26. const (
  27. CGROUP_SUBTREE_CONTROL = "cgroup.subtree_control"
  28. CGROUP_PROCS = "cgroup.procs"
  29. CGROUP_THREADS = "cgroup.threads"
  30. CGROUP_TYPE = "cgroup.type"
  31. CGROUP_TYPE_THREADED = "threaded"
  32. CPU_WEIGHT = "cpu.weight"
  33. )
  34. type CgroupTask struct {
  35. pid string
  36. threadIds []string
  37. name string
  38. hand cgroup.ICGroupTask
  39. }
  40. func NewCGroupBaseTask(pid, name string, threadIds []string) *CgroupTask {
  41. return &CgroupTask{
  42. pid: pid,
  43. name: name,
  44. threadIds: threadIds,
  45. }
  46. }
  47. func (c *CgroupTask) InitTask(hand cgroup.ICGroupTask, cpuShares int, pid, name string) {}
  48. func (c *CgroupTask) SetPid(string) {}
  49. func (c *CgroupTask) SetName(string) {}
  50. func (c *CgroupTask) SetWeight(coreNum int) {}
  51. func (c *CgroupTask) Init() bool {
  52. //initSubGroups()
  53. return true
  54. }
  55. func (c *CgroupTask) Module() string {
  56. return ""
  57. }
  58. func (c *CgroupTask) SetHand(hand cgroup.ICGroupTask) {
  59. c.hand = hand
  60. }
  61. func (c *CgroupTask) GetStaticConfig() map[string]string {
  62. return nil
  63. }
  64. func (c *CgroupTask) GetConfig() map[string]string {
  65. return nil
  66. }
  67. func (c *CgroupTask) CustomConfig(key, value string) bool {
  68. configPath := path.Join(manager.GetCgroupPath(), c.GroupName(), key)
  69. if !fileutils2.Exists(configPath) {
  70. return true
  71. }
  72. return c.SetParam(key, value)
  73. }
  74. func (c *CgroupTask) GetParentGroup() string {
  75. return filepath.Dir(c.GroupName())
  76. }
  77. func (c *CgroupTask) GetParam(name string) string {
  78. return getParam(name, c.GroupName())
  79. }
  80. func (c *CgroupTask) RemoveTask() bool {
  81. if c.TaskIsExist() {
  82. if c.GetParam(CGROUP_TYPE) == CGROUP_TYPE_THREADED {
  83. // move threads to parents
  84. threads := c.GetParam(CGROUP_THREADS)
  85. if len(threads) > 0 {
  86. parentGroup := c.GetParentGroup()
  87. for _, thread := range strings.Split(threads, "\n") {
  88. thread = strings.TrimSpace(thread)
  89. if len(thread) > 0 {
  90. err := setParam(CGROUP_THREADS, thread, parentGroup)
  91. if err != nil {
  92. log.Errorf("failed remove thread %s of %s: %s", thread, c.GroupName(), err)
  93. }
  94. }
  95. }
  96. }
  97. } else {
  98. // move procs to root
  99. procs := c.GetParam(CGROUP_PROCS)
  100. if len(procs) > 0 {
  101. for _, proc := range strings.Split(procs, "\n") {
  102. proc = strings.TrimSpace(proc)
  103. if len(proc) > 0 {
  104. err := setParam(CGROUP_PROCS, proc)
  105. if err != nil {
  106. log.Errorf("failed remove proc %s of %s: %s", proc, c.GroupName(), err)
  107. }
  108. }
  109. }
  110. }
  111. }
  112. log.Infof("Remove task path %s %s", c.TaskPath(), c.name)
  113. if err := os.Remove(c.TaskPath()); err != nil {
  114. log.Errorf("Remove task path failed %s", err)
  115. return false
  116. }
  117. }
  118. return true
  119. }
  120. func (c *CgroupTask) Configure() bool {
  121. if !c.ensureTask() {
  122. return false
  123. }
  124. conf := c.hand.GetStaticConfig()
  125. if !c.SetParams(conf) {
  126. return false
  127. }
  128. conf = c.hand.GetConfig()
  129. return c.SetParams(conf)
  130. }
  131. func (c *CgroupTask) SetTask() bool {
  132. if !c.ensureTask() {
  133. return false
  134. }
  135. conf := c.hand.GetStaticConfig()
  136. if len(conf) > 0 {
  137. if !c.SetParams(conf) {
  138. return false
  139. }
  140. }
  141. conf = c.hand.GetConfig()
  142. if c.SetParams(conf) {
  143. if len(c.pid) > 0 {
  144. c.PushPid()
  145. return true
  146. }
  147. }
  148. return false
  149. }
  150. func (c *CgroupTask) GroupName() string {
  151. if len(c.name) > 0 {
  152. return c.name
  153. }
  154. return c.pid
  155. }
  156. func (c *CgroupTask) TaskPath() string {
  157. return path.Join(manager.GetCgroupPath(), c.GroupName())
  158. }
  159. func (c *CgroupTask) TaskIsExist() bool {
  160. return fileutils2.Exists(c.TaskPath())
  161. }
  162. func (c *CgroupTask) createTask() bool {
  163. if err := os.Mkdir(c.TaskPath(), os.ModePerm); err != nil {
  164. log.Errorf("cgroup path create failed %s", err)
  165. return false
  166. }
  167. return true
  168. }
  169. func (c *CgroupTask) ensureTask() bool {
  170. if !c.TaskIsExist() {
  171. if !c.createTask() {
  172. return false
  173. }
  174. }
  175. if len(c.threadIds) > 0 {
  176. // switch to threaded mode
  177. if !c.SetParam(CGROUP_TYPE, CGROUP_TYPE_THREADED) {
  178. return false
  179. }
  180. }
  181. if !c.SetParam(CGROUP_SUBTREE_CONTROL, fmt.Sprintf("+%s", c.hand.Module())) {
  182. return false
  183. }
  184. return true
  185. }
  186. func (c *CgroupTask) SetParam(name, value string) bool {
  187. err := setParam(name, value, c.GroupName())
  188. if err != nil {
  189. log.Errorf("Fail to set %s=%s for %s: %s", name, value, c.GroupName(), err)
  190. return false
  191. }
  192. return true
  193. }
  194. func (c *CgroupTask) SetParams(conf map[string]string) bool {
  195. for k, v := range conf {
  196. if !c.SetParam(k, v) {
  197. return false
  198. }
  199. }
  200. return true
  201. }
  202. func (c CgroupTask) PushPid() {
  203. if c.pid == "" {
  204. return
  205. }
  206. if len(c.threadIds) > 0 {
  207. for i := range c.threadIds {
  208. subdir := fmt.Sprintf("/proc/%s/task/%s", c.pid, c.threadIds[i])
  209. if fi, err := os.Stat(subdir); err != nil {
  210. log.Errorf("Fail to stat %s in task %s", subdir, err)
  211. continue
  212. } else if fi.Mode().IsDir() {
  213. stat, err := fileutils2.FileGetContents(path.Join(subdir, "stat"))
  214. if err != nil {
  215. log.Errorf("Fail to stat %s in task %s", stat, err)
  216. continue
  217. }
  218. re := regexp.MustCompile(`\s+`)
  219. data := re.Split(stat, -1)
  220. if data[2] != "Z" {
  221. c.SetParam(CGROUP_THREADS, c.threadIds[i])
  222. }
  223. }
  224. }
  225. } else {
  226. c.SetParam(CGROUP_PROCS, c.pid)
  227. }
  228. }
  229. // cgroup cpu.weight
  230. // Convert cgroup v1 cpu.shares value to cgroup v2 cpu.weight
  231. // https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/2254-cgroup-v2#phase-1-convert-from-cgroups-v1-settings-to-v2
  232. func CpuSharesToCpuWeight(cpuShares uint64) uint64 {
  233. return uint64((((cpuShares - 2) * 9999) / 262142) + 1)
  234. }
  235. type CGroupCPUTask struct {
  236. *CgroupTask
  237. weight uint64
  238. }
  239. func (c *CGroupCPUTask) Module() string {
  240. return "cpu"
  241. }
  242. func (c *CGroupCPUTask) GetConfig() map[string]string {
  243. return map[string]string{CPU_WEIGHT: fmt.Sprintf("%d", c.weight)}
  244. }
  245. func (m *cgroupManager) NewCGroupCPUTask(pid, name string, cpuShares int) cgroup.ICGroupTask {
  246. task := &CGroupCPUTask{
  247. CgroupTask: NewCGroupBaseTask(pid, name, nil),
  248. weight: CpuSharesToCpuWeight(uint64(cpuShares)),
  249. }
  250. task.SetHand(task)
  251. return task
  252. }
  253. // cgroup cpuset.cpus
  254. const (
  255. CPUSET_CPUS = "cpuset.cpus"
  256. CPUSET_MEMS = "cpuset.mems"
  257. )
  258. type CGroupCPUSetTask struct {
  259. *CgroupTask
  260. cpuset string
  261. mems string
  262. }
  263. func (c *CGroupCPUSetTask) Module() string {
  264. return "cpuset"
  265. }
  266. func (c *CGroupCPUSetTask) GetConfig() map[string]string {
  267. if c.cpuset == "" {
  268. c.cpuset = getParam(CPUSET_CPUS, c.GetParentGroup())
  269. }
  270. if c.mems == "" {
  271. c.mems = getParam(CPUSET_MEMS, c.GetParentGroup())
  272. }
  273. config := map[string]string{}
  274. if c.cpuset != "" {
  275. config[CPUSET_CPUS] = c.cpuset
  276. }
  277. if c.mems != "" {
  278. config[CPUSET_MEMS] = c.mems
  279. }
  280. return config
  281. }
  282. func (m *cgroupManager) NewCGroupCPUSetTask(pid, name, cpuset, mems string) cgroup.ICGroupTask {
  283. task := &CGroupCPUSetTask{
  284. CgroupTask: NewCGroupBaseTask(pid, name, nil),
  285. cpuset: cpuset,
  286. mems: mems,
  287. }
  288. task.SetHand(task)
  289. return task
  290. }
  291. func (m *cgroupManager) NewCGroupSubCPUSetTask(pid, name string, cpuset string, threadIds []string) cgroup.ICGroupTask {
  292. task := &CGroupCPUSetTask{
  293. CgroupTask: NewCGroupBaseTask(pid, name, threadIds),
  294. cpuset: cpuset,
  295. }
  296. task.SetHand(task)
  297. return task
  298. }