github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/containerd/supervisor/machine_solaris.go (about)

     1  package supervisor
     2  
     3  /*
     4  #include <unistd.h>
     5  */
     6  import "C"
     7  
     8  import (
     9  	"errors"
    10  )
    11  
    12  // Machine holds the current machine cpu count and ram size
    13  type Machine struct {
    14  	Cpus   int
    15  	Memory int64
    16  }
    17  
    18  // CollectMachineInformation returns information regarding the current
    19  // machine (e.g. CPU count, RAM amount)
    20  func CollectMachineInformation() (Machine, error) {
    21  	m := Machine{}
    22  	ncpus := C.sysconf(C._SC_NPROCESSORS_ONLN)
    23  	if ncpus <= 0 {
    24  		return m, errors.New("Unable to get number of cpus")
    25  	}
    26  	m.Cpus = int(ncpus)
    27  
    28  	memTotal := getTotalMem()
    29  	if memTotal < 0 {
    30  		return m, errors.New("Unable to get total memory")
    31  	}
    32  	m.Memory = int64(memTotal / 1024 / 1024)
    33  	return m, nil
    34  }
    35  
    36  // Get the system memory info using sysconf same as prtconf
    37  func getTotalMem() int64 {
    38  	pagesize := C.sysconf(C._SC_PAGESIZE)
    39  	npages := C.sysconf(C._SC_PHYS_PAGES)
    40  	return int64(pagesize * npages)
    41  }