subsystem.go 2.4 KB

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