github.com/cdoern/storage@v1.12.13/pkg/system/meminfo_windows.go (about)

     1  package system
     2  
     3  import (
     4  	"unsafe"
     5  
     6  	"golang.org/x/sys/windows"
     7  )
     8  
     9  var (
    10  	modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
    11  
    12  	procGlobalMemoryStatusEx = modkernel32.NewProc("GlobalMemoryStatusEx")
    13  )
    14  
    15  // https://msdn.microsoft.com/en-us/library/windows/desktop/aa366589(v=vs.85).aspx
    16  // https://msdn.microsoft.com/en-us/library/windows/desktop/aa366770(v=vs.85).aspx
    17  type memorystatusex struct {
    18  	dwLength                uint32
    19  	dwMemoryLoad            uint32
    20  	ullTotalPhys            uint64
    21  	ullAvailPhys            uint64
    22  	ullTotalPageFile        uint64
    23  	ullAvailPageFile        uint64
    24  	ullTotalVirtual         uint64
    25  	ullAvailVirtual         uint64
    26  	ullAvailExtendedVirtual uint64
    27  }
    28  
    29  // ReadMemInfo retrieves memory statistics of the host system and returns a
    30  //  MemInfo type.
    31  func ReadMemInfo() (*MemInfo, error) {
    32  	msi := &memorystatusex{
    33  		dwLength: 64,
    34  	}
    35  	r1, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(msi)))
    36  	if r1 == 0 {
    37  		return &MemInfo{}, nil
    38  	}
    39  	return &MemInfo{
    40  		MemTotal:  int64(msi.ullTotalPhys),
    41  		MemFree:   int64(msi.ullAvailPhys),
    42  		SwapTotal: int64(msi.ullTotalPageFile),
    43  		SwapFree:  int64(msi.ullAvailPageFile),
    44  	}, nil
    45  }