From ab22e78f2da2120bc000b25a481ef05b2181b6a2 Mon Sep 17 00:00:00 2001 From: WAKAYAMA Shirou Date: Sat, 19 Apr 2014 01:04:22 +0900 Subject: [PATCH] implements memory information on windows. --- mem_windows.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/mem_windows.go b/mem_windows.go index 5bbaa8f..05cbfdc 100644 --- a/mem_windows.go +++ b/mem_windows.go @@ -2,9 +2,42 @@ package main +import ( + "syscall" + "unsafe" +) + +var ( + modKernel32 = syscall.NewLazyDLL("kernel32.dll") + globalMemoryStatusEx = modKernel32.NewProc("GlobalMemoryStatusEx") +) + +type MEMORYSTATUSEX struct { + cbSize uint32 + dwMemoryLoad uint32 + ullTotalPhys uint64 // in bytes + ullAvailPhys uint64 + ullTotalPageFile uint64 + ullAvailPageFile uint64 + ullTotalVirtual uint64 + ullAvailVirtual uint64 + ullAvailExtendedVirtual uint64 +} + func (m Mem) Virtual_memory() (Virtual_memory, error) { ret := Virtual_memory{} + var memInfo MEMORYSTATUSEX + memInfo.cbSize = uint32(unsafe.Sizeof(memInfo)) + mem, _, _ := globalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo))) + if mem == 0 { + return ret, syscall.GetLastError() + } + + ret.Total = memInfo.ullTotalPhys + ret.Available = memInfo.ullAvailPhys + ret.UsedPercent = float64(memInfo.dwMemoryLoad) + ret.Used = ret.Total - ret.Available return ret, nil }