...

Source file src/github.com/containerd/cgroups/subsystem.go

Documentation: github.com/containerd/cgroups

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

View as plain text