From 4ac6f93049f2013a6f6f9855baa8d4160cedb112 Mon Sep 17 00:00:00 2001 From: John Howard Date: Mon, 1 Jun 2015 12:40:33 -0700 Subject: [PATCH] Windows: Implement ReadMemInfo() Signed-off-by: John Howard --- system/meminfo_unsupported.go | 2 +- system/meminfo_windows.go | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 system/meminfo_windows.go diff --git a/system/meminfo_unsupported.go b/system/meminfo_unsupported.go index 63b8b16..604d338 100644 --- a/system/meminfo_unsupported.go +++ b/system/meminfo_unsupported.go @@ -1,4 +1,4 @@ -// +build !linux +// +build !linux,!windows package system diff --git a/system/meminfo_windows.go b/system/meminfo_windows.go new file mode 100644 index 0000000..d466425 --- /dev/null +++ b/system/meminfo_windows.go @@ -0,0 +1,44 @@ +package system + +import ( + "syscall" + "unsafe" +) + +var ( + modkernel32 = syscall.NewLazyDLL("kernel32.dll") + + procGlobalMemoryStatusEx = modkernel32.NewProc("GlobalMemoryStatusEx") +) + +// https://msdn.microsoft.com/en-us/library/windows/desktop/aa366589(v=vs.85).aspx +// https://msdn.microsoft.com/en-us/library/windows/desktop/aa366770(v=vs.85).aspx +type memorystatusex struct { + dwLength uint32 + dwMemoryLoad uint32 + ullTotalPhys uint64 + ullAvailPhys uint64 + ullTotalPageFile uint64 + ullAvailPageFile uint64 + ullTotalVirtual uint64 + ullAvailVirtual uint64 + ullAvailExtendedVirtual uint64 +} + +// ReadMemInfo retrieves memory statistics of the host system and returns a +// MemInfo type. +func ReadMemInfo() (*MemInfo, error) { + msi := &memorystatusex{ + dwLength: 64, + } + r1, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(msi))) + if r1 == 0 { + return &MemInfo{}, nil + } + return &MemInfo{ + MemTotal: int64(msi.ullTotalPhys), + MemFree: int64(msi.ullAvailPhys), + SwapTotal: int64(msi.ullTotalPageFile), + SwapFree: int64(msi.ullAvailPageFile), + }, nil +}