subsystem.go 2.5 KB

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