control.go 3.1 KB

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