...

Source file src/github.com/containerd/cgroups/control.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  	"os"
    21  
    22  	v1 "github.com/containerd/cgroups/stats/v1"
    23  	specs "github.com/opencontainers/runtime-spec/specs-go"
    24  )
    25  
    26  type procType = string
    27  
    28  const (
    29  	cgroupProcs    procType = "cgroup.procs"
    30  	cgroupTasks    procType = "tasks"
    31  	defaultDirPerm          = 0755
    32  )
    33  
    34  // defaultFilePerm is a var so that the test framework can change the filemode
    35  // of all files created when the tests are running.  The difference between the
    36  // tests and real world use is that files like "cgroup.procs" will exist when writing
    37  // to a read cgroup filesystem and do not exist prior when running in the tests.
    38  // this is set to a non 0 value in the test code
    39  var defaultFilePerm = os.FileMode(0)
    40  
    41  type Process struct {
    42  	// Subsystem is the name of the subsystem that the process / task is in.
    43  	Subsystem Name
    44  	// Pid is the process id of the process / task.
    45  	Pid int
    46  	// Path is the full path of the subsystem and location that the process / task is in.
    47  	Path string
    48  }
    49  
    50  type Task = Process
    51  
    52  // Cgroup handles interactions with the individual groups to perform
    53  // actions on them as them main interface to this cgroup package
    54  type Cgroup interface {
    55  	// New creates a new cgroup under the calling cgroup
    56  	New(string, *specs.LinuxResources) (Cgroup, error)
    57  	// Add adds a process to the cgroup (cgroup.procs). Without additional arguments,
    58  	// the process is added to all the cgroup subsystems. When giving Add a list of
    59  	// subsystem names, the process is only added to those subsystems, provided that
    60  	// they are active in the targeted cgroup.
    61  	Add(Process, ...Name) error
    62  	// AddProc adds the process with the given id to the cgroup (cgroup.procs).
    63  	// Without additional arguments, the process with the given id is added to all
    64  	// the cgroup subsystems. When giving AddProc a list of subsystem names, the process
    65  	// id is only added to those subsystems, provided that they are active in the targeted
    66  	// cgroup.
    67  	AddProc(uint64, ...Name) error
    68  	// AddTask adds a process to the cgroup (tasks). Without additional arguments, the
    69  	// task is added to all the cgroup subsystems. When giving AddTask a list of subsystem
    70  	// names, the task is only added to those subsystems, provided that they are active in
    71  	// the targeted cgroup.
    72  	AddTask(Process, ...Name) error
    73  	// Delete removes the cgroup as a whole
    74  	Delete() error
    75  	// MoveTo moves all the processes under the calling cgroup to the provided one
    76  	// subsystems are moved one at a time
    77  	MoveTo(Cgroup) error
    78  	// Stat returns the stats for all subsystems in the cgroup
    79  	Stat(...ErrorHandler) (*v1.Metrics, error)
    80  	// Update updates all the subsystems with the provided resource changes
    81  	Update(resources *specs.LinuxResources) error
    82  	// Processes returns all the processes in a select subsystem for the cgroup
    83  	Processes(Name, bool) ([]Process, error)
    84  	// Tasks returns all the tasks in a select subsystem for the cgroup
    85  	Tasks(Name, bool) ([]Task, error)
    86  	// Freeze freezes or pauses all processes inside the cgroup
    87  	Freeze() error
    88  	// Thaw thaw or resumes all processes inside the cgroup
    89  	Thaw() error
    90  	// OOMEventFD returns the memory subsystem's event fd for OOM events
    91  	OOMEventFD() (uintptr, error)
    92  	// RegisterMemoryEvent returns the memory subsystems event fd for whatever memory event was
    93  	// registered for. Can alternatively register for the oom event with this method.
    94  	RegisterMemoryEvent(MemoryEvent) (uintptr, error)
    95  	// State returns the cgroups current state
    96  	State() State
    97  	// Subsystems returns all the subsystems in the cgroup
    98  	Subsystems() []Subsystem
    99  }
   100  

View as plain text