github.com/gdevillele/moby@v1.13.0/pkg/sysinfo/sysinfo_solaris.go (about)

     1  // +build solaris,cgo
     2  
     3  package sysinfo
     4  
     5  import (
     6  	"bytes"
     7  	"os/exec"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  /*
    13  #cgo LDFLAGS: -llgrp
    14  #include <unistd.h>
    15  #include <stdlib.h>
    16  #include <sys/lgrp_user.h>
    17  int getLgrpCount() {
    18  	lgrp_cookie_t lgrpcookie = LGRP_COOKIE_NONE;
    19  	uint_t nlgrps;
    20  
    21  	if ((lgrpcookie = lgrp_init(LGRP_VIEW_OS)) == LGRP_COOKIE_NONE) {
    22  		return -1;
    23  	}
    24  	nlgrps = lgrp_nlgrps(lgrpcookie);
    25  	return nlgrps;
    26  }
    27  */
    28  import "C"
    29  
    30  // IsCPUSharesAvailable returns whether CPUShares setting is supported.
    31  // We need FSS to be set as default scheduling class to support CPU Shares
    32  func IsCPUSharesAvailable() bool {
    33  	cmd := exec.Command("/usr/sbin/dispadmin", "-d")
    34  	outBuf := new(bytes.Buffer)
    35  	errBuf := new(bytes.Buffer)
    36  	cmd.Stderr = errBuf
    37  	cmd.Stdout = outBuf
    38  
    39  	if err := cmd.Run(); err != nil {
    40  		return false
    41  	}
    42  	return (strings.Contains(outBuf.String(), "FSS"))
    43  }
    44  
    45  // New returns a new SysInfo, using the filesystem to detect which features
    46  // the kernel supports.
    47  //NOTE Solaris: If we change the below capabilities be sure
    48  // to update verifyPlatformContainerSettings() in daemon_solaris.go
    49  func New(quiet bool) *SysInfo {
    50  	sysInfo := &SysInfo{}
    51  	sysInfo.cgroupMemInfo = setCgroupMem(quiet)
    52  	sysInfo.cgroupCPUInfo = setCgroupCPU(quiet)
    53  	sysInfo.cgroupBlkioInfo = setCgroupBlkioInfo(quiet)
    54  	sysInfo.cgroupCpusetInfo = setCgroupCPUsetInfo(quiet)
    55  
    56  	sysInfo.IPv4ForwardingDisabled = false
    57  
    58  	sysInfo.AppArmor = false
    59  
    60  	return sysInfo
    61  }
    62  
    63  // setCgroupMem reads the memory information for Solaris.
    64  func setCgroupMem(quiet bool) cgroupMemInfo {
    65  
    66  	return cgroupMemInfo{
    67  		MemoryLimit:       true,
    68  		SwapLimit:         true,
    69  		MemoryReservation: false,
    70  		OomKillDisable:    false,
    71  		MemorySwappiness:  false,
    72  		KernelMemory:      false,
    73  	}
    74  }
    75  
    76  // setCgroupCPU reads the cpu information for Solaris.
    77  func setCgroupCPU(quiet bool) cgroupCPUInfo {
    78  
    79  	return cgroupCPUInfo{
    80  		CPUShares:          true,
    81  		CPUCfsPeriod:       false,
    82  		CPUCfsQuota:        true,
    83  		CPURealtimePeriod:  false,
    84  		CPURealtimeRuntime: false,
    85  	}
    86  }
    87  
    88  // blkio switches are not supported in Solaris.
    89  func setCgroupBlkioInfo(quiet bool) cgroupBlkioInfo {
    90  
    91  	return cgroupBlkioInfo{
    92  		BlkioWeight:       false,
    93  		BlkioWeightDevice: false,
    94  	}
    95  }
    96  
    97  // setCgroupCPUsetInfo reads the cpuset information for Solaris.
    98  func setCgroupCPUsetInfo(quiet bool) cgroupCpusetInfo {
    99  
   100  	return cgroupCpusetInfo{
   101  		Cpuset: true,
   102  		Cpus:   getCPUCount(),
   103  		Mems:   getLgrpCount(),
   104  	}
   105  }
   106  
   107  func getCPUCount() string {
   108  	ncpus := C.sysconf(C._SC_NPROCESSORS_ONLN)
   109  	if ncpus <= 0 {
   110  		return ""
   111  	}
   112  	return strconv.FormatInt(int64(ncpus), 16)
   113  }
   114  
   115  func getLgrpCount() string {
   116  	nlgrps := C.getLgrpCount()
   117  	if nlgrps <= 0 {
   118  		return ""
   119  	}
   120  	return strconv.FormatInt(int64(nlgrps), 16)
   121  }