github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/cgroups/config.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package cgroups
     7  
     8  import (
     9  	"io/ioutil"
    10  	"path/filepath"
    11  
    12  	"github.com/pelletier/go-toml"
    13  )
    14  
    15  // LinuxHugepageLimit structure corresponds to limiting kernel hugepages
    16  type LinuxHugepageLimit struct {
    17  	// Pagesize is the hugepage size
    18  	Pagesize string `toml:"pageSize" json:"pageSize"`
    19  	// Limit is the limit of "hugepagesize" hugetlb usage
    20  	Limit uint64 `toml:"limit" json:"limit"`
    21  }
    22  
    23  // LinuxInterfacePriority for network interfaces
    24  type LinuxInterfacePriority struct {
    25  	// Name is the name of the network interface
    26  	Name string `toml:"name" json:"name"`
    27  	// Priority for the interface
    28  	Priority uint32 `toml:"priority" json:"priority"`
    29  }
    30  
    31  // LinuxWeightDevice struct holds a `major:minor weight` pair for weightDevice
    32  type LinuxWeightDevice struct {
    33  	// Major is the device's major number.
    34  	Major int64 `toml:"major" json:"major"`
    35  	// Minor is the device's minor number.
    36  	Minor int64 `toml:"minor" json:"minor"`
    37  	// Weight is the bandwidth rate for the device.
    38  	Weight *uint16 `toml:"weight" json:"weight,omitempty"`
    39  	// LeafWeight is the bandwidth rate for the device while competing with the cgroup's child cgroups, CFQ scheduler only
    40  	LeafWeight *uint16 `toml:"leafWeight" json:"leafWeight,omitempty"`
    41  }
    42  
    43  // LinuxThrottleDevice struct holds a `major:minor rate_per_second` pair
    44  type LinuxThrottleDevice struct {
    45  	// Major is the device's major number.
    46  	Major int64 `toml:"major" json:"major"`
    47  	// Minor is the device's minor number.
    48  	Minor int64 `toml:"minor" json:"minor"`
    49  	// Rate is the IO rate limit per cgroup per device
    50  	Rate uint64 `toml:"rate" json:"rate"`
    51  }
    52  
    53  // LinuxBlockIO for Linux cgroup 'blkio' resource management
    54  type LinuxBlockIO struct {
    55  	// Specifies per cgroup weight
    56  	Weight *uint16 `toml:"weight" json:"weight,omitempty"`
    57  	// Specifies tasks' weight in the given cgroup while competing with the cgroup's child cgroups, CFQ scheduler only
    58  	LeafWeight *uint16 `toml:"leafWeight" json:"leafWeight,omitempty"`
    59  	// Weight per cgroup per device, can override BlkioWeight
    60  	WeightDevice []LinuxWeightDevice `toml:"weightDevice" json:"weightDevice,omitempty"`
    61  	// IO read rate limit per cgroup per device, bytes per second
    62  	ThrottleReadBpsDevice []LinuxThrottleDevice `toml:"throttleReadBpsDevice" json:"throttleReadBpsDevice,omitempty"`
    63  	// IO write rate limit per cgroup per device, bytes per second
    64  	ThrottleWriteBpsDevice []LinuxThrottleDevice `toml:"throttleWriteBpsDevice" json:"throttleWriteBpsDevice,omitempty"`
    65  	// IO read rate limit per cgroup per device, IO per second
    66  	ThrottleReadIOPSDevice []LinuxThrottleDevice `toml:"throttleReadIOPSDevice" json:"throttleReadIOPSDevice,omitempty"`
    67  	// IO write rate limit per cgroup per device, IO per second
    68  	ThrottleWriteIOPSDevice []LinuxThrottleDevice `toml:"throttleWriteIOPSDevice" json:"throttleWriteIOPSDevice,omitempty"`
    69  }
    70  
    71  // LinuxMemory for Linux cgroup 'memory' resource management
    72  type LinuxMemory struct {
    73  	// Memory limit (in bytes).
    74  	Limit *int64 `toml:"limit" json:"limit,omitempty"`
    75  	// Memory reservation or soft_limit (in bytes).
    76  	Reservation *int64 `toml:"reservation" json:"reservation,omitempty"`
    77  	// Total memory limit (memory + swap).
    78  	Swap *int64 `toml:"swap" json:"swap,omitempty"`
    79  	// Kernel memory limit (in bytes).
    80  	Kernel *int64 `toml:"kernel" json:"kernel,omitempty"`
    81  	// Kernel memory limit for tcp (in bytes)
    82  	KernelTCP *int64 `toml:"kernelTCP" json:"kernelTCP,omitempty"`
    83  	// How aggressive the kernel will swap memory pages.
    84  	Swappiness *uint64 `toml:"swappiness" json:"swappiness,omitempty"`
    85  	// DisableOOMKiller disables the OOM killer for out of memory conditions
    86  	DisableOOMKiller *bool `toml:"disableOOMKiller" json:"disableOOMKiller,omitempty"`
    87  }
    88  
    89  // LinuxCPU for Linux cgroup 'cpu' resource management
    90  type LinuxCPU struct {
    91  	// CPU shares (relative weight (ratio) vs. other cgroups with cpu shares).
    92  	Shares *uint64 `toml:"shares" json:"shares,omitempty"`
    93  	// CPU hardcap limit (in usecs). Allowed cpu time in a given period.
    94  	Quota *int64 `toml:"quota" json:"quota,omitempty"`
    95  	// CPU period to be used for hardcapping (in usecs).
    96  	Period *uint64 `toml:"period" json:"period,omitempty"`
    97  	// How much time realtime scheduling may use (in usecs).
    98  	RealtimeRuntime *int64 `toml:"realtimeRuntime" json:"realtimeRuntime,omitempty"`
    99  	// CPU period to be used for realtime scheduling (in usecs).
   100  	RealtimePeriod *uint64 `toml:"realtimePeriod" json:"realtimePeriod,omitempty"`
   101  	// CPUs to use within the cpuset. Default is to use any CPU available.
   102  	Cpus string `toml:"cpus" json:"cpus,omitempty"`
   103  	// List of memory nodes in the cpuset. Default is to use any available memory node.
   104  	Mems string `toml:"mems" json:"mems,omitempty"`
   105  }
   106  
   107  // LinuxPids for Linux cgroup 'pids' resource management (Linux 4.3)
   108  type LinuxPids struct {
   109  	// Maximum number of PIDs. Default is "no limit".
   110  	Limit int64 `toml:"limit" json:"limit"`
   111  }
   112  
   113  // LinuxNetwork identification and priority configuration
   114  type LinuxNetwork struct {
   115  	// Set class identifier for container's network packets
   116  	ClassID *uint32 `toml:"classID" json:"classID,omitempty"`
   117  	// Set priority of network traffic for container
   118  	Priorities []LinuxInterfacePriority `toml:"priorities" json:"priorities,omitempty"`
   119  }
   120  
   121  // LinuxRdma for Linux cgroup 'rdma' resource management (Linux 4.11)
   122  type LinuxRdma struct {
   123  	// Maximum number of HCA handles that can be opened. Default is "no limit".
   124  	HcaHandles *uint32 `toml:"hcaHandles" json:"hcaHandles,omitempty"`
   125  	// Maximum number of HCA objects that can be created. Default is "no limit".
   126  	HcaObjects *uint32 `toml:"hcaObjects" json:"hcaObjects,omitempty"`
   127  }
   128  
   129  // LinuxDeviceCgroup represents a device rule for the whitelist controller
   130  type LinuxDeviceCgroup struct {
   131  	// Allow or deny
   132  	Allow bool `toml:"allow" json:"allow" comment:"test"`
   133  	// Device type, block, char, etc.
   134  	Type string `toml:"type" json:"type,omitempty"`
   135  	// Major is the device's major number.
   136  	Major *int64 `toml:"major" json:"major,omitempty"`
   137  	// Minor is the device's minor number.
   138  	Minor *int64 `toml:"minor" json:"minor,omitempty"`
   139  	// Cgroup access permissions format, rwm.
   140  	Access string `toml:"access" json:"access,omitempty"`
   141  }
   142  
   143  // Config has container runtime resource constraints
   144  type Config struct {
   145  	// Devices configures the device whitelist.
   146  	Devices []LinuxDeviceCgroup `toml:"devices" json:"devices,omitempty"`
   147  	// Memory restriction configuration
   148  	Memory *LinuxMemory `toml:"memory" json:"memory,omitempty"`
   149  	// CPU resource restriction configuration
   150  	CPU *LinuxCPU `toml:"cpu" json:"cpu,omitempty"`
   151  	// Task resource restriction configuration.
   152  	Pids *LinuxPids `toml:"pids" json:"pids,omitempty"`
   153  	// BlockIO restriction configuration
   154  	BlockIO *LinuxBlockIO `toml:"blockIO" json:"blockIO,omitempty"`
   155  	// Hugetlb limit (in bytes)
   156  	HugepageLimits []LinuxHugepageLimit `toml:"hugepageLimits" json:"hugepageLimits,omitempty"`
   157  	// Network restriction configuration
   158  	Network *LinuxNetwork `toml:"network" json:"network,omitempty"`
   159  	// Rdma resource restriction configuration.
   160  	// Limits are a set of key value pairs that define RDMA resource limits,
   161  	// where the key is device name and value is resource limits.
   162  	Rdma map[string]LinuxRdma `toml:"rdma" json:"rdma,omitempty"`
   163  }
   164  
   165  // LoadConfig opens cgroups controls config file and unmarshals it into structures
   166  func LoadConfig(confPath string) (config Config, err error) {
   167  	path, err := filepath.Abs(confPath)
   168  	if err != nil {
   169  		return
   170  	}
   171  
   172  	// read in the Cgroups config file
   173  	b, err := ioutil.ReadFile(path)
   174  	if err != nil {
   175  		return
   176  	}
   177  
   178  	// Unmarshal config file
   179  	err = toml.Unmarshal(b, &config)
   180  	return
   181  }
   182  
   183  // PutConfig takes the content of a CgroupsConfig struct and Marshals it to file
   184  func PutConfig(config Config, confPath string) (err error) {
   185  	data, err := toml.Marshal(config)
   186  	if err != nil {
   187  		return
   188  	}
   189  
   190  	return ioutil.WriteFile(confPath, data, 0600)
   191  }