Merge pull request #423 from derekwbrown/db/windows_swap_memory

Add implementation of SwapMemory() for windows
tags/v2.17.09 v2.17.09
shirou 8 years ago committed by GitHub
commit 6e221c4826

@ -48,7 +48,8 @@ var (
Modkernel32 = windows.NewLazyDLL("kernel32.dll")
ModNt = windows.NewLazyDLL("ntdll.dll")
ModPdh = windows.NewLazyDLL("pdh.dll")
ModPsapi = windows.NewLazyDLL("psapi.dll")
ProcGetSystemTimes = Modkernel32.NewProc("GetSystemTimes")
ProcNtQuerySystemInformation = ModNt.NewProc("NtQuerySystemInformation")
PdhOpenQuery = ModPdh.NewProc("PdhOpenQuery")

@ -11,6 +11,7 @@ import (
var (
procGlobalMemoryStatusEx = common.Modkernel32.NewProc("GlobalMemoryStatusEx")
procGetPerformanceInfo = common.ModPsapi.NewProc("GetPerformanceInfo")
)
type memoryStatusEx struct {
@ -43,8 +44,40 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
return ret, nil
}
type performanceInformation struct {
cb uint32
commitTotal uint64
commitLimit uint64
commitPeak uint64
physicalTotal uint64
physicalAvailable uint64
systemCache uint64
kernelTotal uint64
kernelPaged uint64
kernelNonpaged uint64
pageSize uint64
handleCount uint32
processCount uint32
threadCount uint32
}
func SwapMemory() (*SwapMemoryStat, error) {
ret := &SwapMemoryStat{}
var perfInfo performanceInformation
perfInfo.cb = uint32(unsafe.Sizeof(perfInfo))
mem, _, _ := procGetPerformanceInfo.Call(uintptr(unsafe.Pointer(&perfInfo)), uintptr(perfInfo.cb))
if mem == 0 {
return nil, windows.GetLastError()
}
tot := perfInfo.commitLimit * perfInfo.pageSize
used := perfInfo.commitTotal * perfInfo.pageSize
free := tot - used
ret := &SwapMemoryStat{
Total: tot,
Used: used,
Free: free,
UsedPercent: float64(used/tot),
}
return ret, nil
}

Loading…
Cancel
Save