subsystem.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "fmt"
  16. specs "github.com/opencontainers/runtime-spec/specs-go"
  17. )
  18. // Name is a typed name for a cgroup subsystem
  19. type Name string
  20. const (
  21. Devices Name = "devices"
  22. Hugetlb Name = "hugetlb"
  23. Freezer Name = "freezer"
  24. Pids Name = "pids"
  25. NetCLS Name = "net_cls"
  26. NetPrio Name = "net_prio"
  27. PerfEvent Name = "perf_event"
  28. Cpuset Name = "cpuset"
  29. Cpu Name = "cpu"
  30. Cpuacct Name = "cpuacct"
  31. Memory Name = "memory"
  32. Blkio Name = "blkio"
  33. Rdma Name = "rdma"
  34. )
  35. // Subsystems returns a complete list of the default cgroups
  36. // available on most linux systems
  37. func Subsystems() []Name {
  38. n := []Name{
  39. Hugetlb,
  40. Freezer,
  41. Pids,
  42. NetCLS,
  43. NetPrio,
  44. PerfEvent,
  45. Cpuset,
  46. Cpu,
  47. Cpuacct,
  48. Memory,
  49. Blkio,
  50. Rdma,
  51. }
  52. if !isUserNS {
  53. n = append(n, Devices)
  54. }
  55. return n
  56. }
  57. type Subsystem interface {
  58. Name() Name
  59. }
  60. type pather interface {
  61. Subsystem
  62. Path(path string) string
  63. }
  64. type creator interface {
  65. Subsystem
  66. Create(path string, resources *specs.LinuxResources) error
  67. }
  68. type deleter interface {
  69. Subsystem
  70. Delete(path string) error
  71. }
  72. type stater interface {
  73. Subsystem
  74. Stat(path string, stats *Metrics) error
  75. }
  76. type updater interface {
  77. Subsystem
  78. Update(path string, resources *specs.LinuxResources) error
  79. }
  80. // SingleSubsystem returns a single cgroup subsystem within the base Hierarchy
  81. func SingleSubsystem(baseHierarchy Hierarchy, subsystem Name) Hierarchy {
  82. return func() ([]Subsystem, error) {
  83. subsystems, err := baseHierarchy()
  84. if err != nil {
  85. return nil, err
  86. }
  87. for _, s := range subsystems {
  88. if s.Name() == subsystem {
  89. return []Subsystem{
  90. s,
  91. }, nil
  92. }
  93. }
  94. return nil, fmt.Errorf("unable to find subsystem %s", subsystem)
  95. }
  96. }