subsystem.go (2538B)
1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cgroup1 18 19 import ( 20 "fmt" 21 "os" 22 23 "github.com/containerd/cgroups/v3" 24 v1 "github.com/containerd/cgroups/v3/cgroup1/stats" 25 specs "github.com/opencontainers/runtime-spec/specs-go" 26 ) 27 28 // Name is a typed name for a cgroup subsystem 29 type Name string 30 31 const ( 32 Devices Name = "devices" 33 Hugetlb Name = "hugetlb" 34 Freezer Name = "freezer" 35 Pids Name = "pids" 36 NetCLS Name = "net_cls" 37 NetPrio Name = "net_prio" 38 PerfEvent Name = "perf_event" 39 Cpuset Name = "cpuset" 40 Cpu Name = "cpu" 41 Cpuacct Name = "cpuacct" 42 Memory Name = "memory" 43 Blkio Name = "blkio" 44 Rdma Name = "rdma" 45 ) 46 47 // Subsystems returns a complete list of the default cgroups 48 // available on most linux systems 49 func Subsystems() []Name { 50 n := []Name{ 51 Freezer, 52 Pids, 53 NetCLS, 54 NetPrio, 55 PerfEvent, 56 Cpuset, 57 Cpu, 58 Cpuacct, 59 Memory, 60 Blkio, 61 Rdma, 62 } 63 if !cgroups.RunningInUserNS() { 64 n = append(n, Devices) 65 } 66 if _, err := os.Stat("/sys/kernel/mm/hugepages"); err == nil { 67 n = append(n, Hugetlb) 68 } 69 return n 70 } 71 72 type Subsystem interface { 73 Name() Name 74 } 75 76 type pather interface { 77 Subsystem 78 Path(path string) string 79 } 80 81 type creator interface { 82 Subsystem 83 Create(path string, resources *specs.LinuxResources) error 84 } 85 86 type deleter interface { 87 Subsystem 88 Delete(path string) error 89 } 90 91 type stater interface { 92 Subsystem 93 Stat(path string, stats *v1.Metrics) error 94 } 95 96 type updater interface { 97 Subsystem 98 Update(path string, resources *specs.LinuxResources) error 99 } 100 101 // SingleSubsystem returns a single cgroup subsystem within the base Hierarchy 102 func SingleSubsystem(baseHierarchy Hierarchy, subsystem Name) Hierarchy { 103 return func() ([]Subsystem, error) { 104 subsystems, err := baseHierarchy() 105 if err != nil { 106 return nil, err 107 } 108 for _, s := range subsystems { 109 if s.Name() == subsystem { 110 return []Subsystem{ 111 s, 112 }, nil 113 } 114 } 115 return nil, fmt.Errorf("unable to find subsystem %s", subsystem) 116 } 117 }