control.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "os"
  16. v1 "github.com/containerd/cgroups/v3/cgroup1/stats"
  17. specs "github.com/opencontainers/runtime-spec/specs-go"
  18. )
  19. type procType = string
  20. const (
  21. cgroupProcs procType = "cgroup.procs"
  22. cgroupTasks procType = "tasks"
  23. defaultDirPerm = 0o755
  24. )
  25. // defaultFilePerm is a var so that the test framework can change the filemode
  26. // of all files created when the tests are running. The difference between the
  27. // tests and real world use is that files like "cgroup.procs" will exist when writing
  28. // to a read cgroup filesystem and do not exist prior when running in the tests.
  29. // this is set to a non 0 value in the test code
  30. var defaultFilePerm = os.FileMode(0)
  31. type Process struct {
  32. // Subsystem is the name of the subsystem that the process / task is in.
  33. Subsystem Name
  34. // Pid is the process id of the process / task.
  35. Pid int
  36. // Path is the full path of the subsystem and location that the process / task is in.
  37. Path string
  38. }
  39. type Task = Process
  40. // Cgroup handles interactions with the individual groups to perform
  41. // actions on them as them main interface to this cgroup package
  42. type Cgroup interface {
  43. // New creates a new cgroup under the calling cgroup
  44. New(string, *specs.LinuxResources) (Cgroup, error)
  45. // Add adds a process to the cgroup (cgroup.procs). Without additional arguments,
  46. // the process is added to all the cgroup subsystems. When giving Add a list of
  47. // subsystem names, the process is only added to those subsystems, provided that
  48. // they are active in the targeted cgroup.
  49. Add(Process, ...Name) error
  50. // AddProc adds the process with the given id to the cgroup (cgroup.procs).
  51. // Without additional arguments, the process with the given id is added to all
  52. // the cgroup subsystems. When giving AddProc a list of subsystem names, the process
  53. // id is only added to those subsystems, provided that they are active in the targeted
  54. // cgroup.
  55. AddProc(uint64, ...Name) error
  56. // AddTask adds a process to the cgroup (tasks). Without additional arguments, the
  57. // task is added to all the cgroup subsystems. When giving AddTask a list of subsystem
  58. // names, the task is only added to those subsystems, provided that they are active in
  59. // the targeted cgroup.
  60. AddTask(Process, ...Name) error
  61. // Delete removes the cgroup as a whole
  62. Delete() error
  63. // MoveTo moves all the processes under the calling cgroup to the provided one
  64. // subsystems are moved one at a time
  65. MoveTo(Cgroup) error
  66. // Stat returns the stats for all subsystems in the cgroup
  67. Stat(...ErrorHandler) (*v1.Metrics, error)
  68. // Update updates all the subsystems with the provided resource changes
  69. Update(resources *specs.LinuxResources) error
  70. // Processes returns all the processes in a select subsystem for the cgroup
  71. Processes(Name, bool) ([]Process, error)
  72. // Tasks returns all the tasks in a select subsystem for the cgroup
  73. Tasks(Name, bool) ([]Task, error)
  74. // Freeze freezes or pauses all processes inside the cgroup
  75. Freeze() error
  76. // Thaw thaw or resumes all processes inside the cgroup
  77. Thaw() error
  78. // OOMEventFD returns the memory subsystem's event fd for OOM events
  79. OOMEventFD() (uintptr, error)
  80. // RegisterMemoryEvent returns the memory subsystems event fd for whatever memory event was
  81. // registered for. Can alternatively register for the oom event with this method.
  82. RegisterMemoryEvent(MemoryEvent) (uintptr, error)
  83. // State returns the cgroups current state
  84. State() State
  85. // Subsystems returns all the subsystems in the cgroup
  86. Subsystems() []Subsystem
  87. }