control.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // Cgroup handles interactions with the individual groups to perform
  38. // actions on them as them main interface to this cgroup package
  39. type Cgroup interface {
  40. // New creates a new cgroup under the calling cgroup
  41. New(string, *specs.LinuxResources) (Cgroup, error)
  42. // Add adds a process to the cgroup (cgroup.procs)
  43. Add(Process) error
  44. // AddTask adds a process to the cgroup (tasks)
  45. AddTask(Process) error
  46. // Delete removes the cgroup as a whole
  47. Delete() error
  48. // MoveTo moves all the processes under the calling cgroup to the provided one
  49. // subsystems are moved one at a time
  50. MoveTo(Cgroup) error
  51. // Stat returns the stats for all subsystems in the cgroup
  52. Stat(...ErrorHandler) (*Metrics, error)
  53. // Update updates all the subsystems with the provided resource changes
  54. Update(resources *specs.LinuxResources) error
  55. // Processes returns all the processes in a select subsystem for the cgroup
  56. Processes(Name, bool) ([]Process, error)
  57. // Freeze freezes or pauses all processes inside the cgroup
  58. Freeze() error
  59. // Thaw thaw or resumes all processes inside the cgroup
  60. Thaw() error
  61. // OOMEventFD returns the memory subsystem's event fd for OOM events
  62. OOMEventFD() (uintptr, error)
  63. // State returns the cgroups current state
  64. State() State
  65. // Subsystems returns all the subsystems in the cgroup
  66. Subsystems() []Subsystem
  67. }