github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/sysinfo/memtotal_freebsd.go (about)

     1  package sysinfo
     2  
     3  import (
     4  	"syscall"
     5  	"unsafe"
     6  )
     7  
     8  // PhysicalMemoryBytes returns the total amount of host memory.
     9  func PhysicalMemoryBytes() (uint64, error) {
    10  	mib := []int32{6 /* CTL_HW */, 5 /* HW_PHYSMEM */}
    11  
    12  	buf := make([]byte, 8)
    13  	bufLen := uintptr(8)
    14  
    15  	_, _, e1 := syscall.Syscall6(syscall.SYS___SYSCTL,
    16  		uintptr(unsafe.Pointer(&mib[0])), uintptr(len(mib)),
    17  		uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&bufLen)),
    18  		uintptr(0), uintptr(0))
    19  
    20  	if e1 != 0 {
    21  		return 0, e1
    22  	}
    23  
    24  	switch bufLen {
    25  	case 4:
    26  		return uint64(*(*uint32)(unsafe.Pointer(&buf[0]))), nil
    27  	case 8:
    28  		return *(*uint64)(unsafe.Pointer(&buf[0])), nil
    29  	default:
    30  		return 0, syscall.EIO
    31  	}
    32  }