2014-04-18 20:28:00 +08:00
|
|
|
// +build windows
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package mem
|
2014-04-18 20:28:00 +08:00
|
|
|
|
2014-04-19 00:04:22 +08:00
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2014-11-27 21:28:05 +08:00
|
|
|
|
2015-10-19 23:04:57 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-04-19 00:04:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-11-27 21:28:05 +08:00
|
|
|
procGlobalMemoryStatusEx = common.Modkernel32.NewProc("GlobalMemoryStatusEx")
|
2014-04-19 00:04:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type MEMORYSTATUSEX struct {
|
|
|
|
cbSize uint32
|
|
|
|
dwMemoryLoad uint32
|
|
|
|
ullTotalPhys uint64 // in bytes
|
|
|
|
ullAvailPhys uint64
|
|
|
|
ullTotalPageFile uint64
|
|
|
|
ullAvailPageFile uint64
|
|
|
|
ullTotalVirtual uint64
|
|
|
|
ullAvailVirtual uint64
|
|
|
|
ullAvailExtendedVirtual uint64
|
|
|
|
}
|
|
|
|
|
2014-05-01 11:01:30 +08:00
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
2014-04-19 00:04:22 +08:00
|
|
|
var memInfo MEMORYSTATUSEX
|
|
|
|
memInfo.cbSize = uint32(unsafe.Sizeof(memInfo))
|
2014-04-20 00:54:14 +08:00
|
|
|
mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo)))
|
2014-04-19 00:04:22 +08:00
|
|
|
if mem == 0 {
|
2014-05-01 11:01:30 +08:00
|
|
|
return nil, syscall.GetLastError()
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := &VirtualMemoryStat{
|
|
|
|
Total: memInfo.ullTotalPhys,
|
|
|
|
Available: memInfo.ullAvailPhys,
|
|
|
|
UsedPercent: float64(memInfo.dwMemoryLoad),
|
2014-04-19 00:04:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret.Used = ret.Total - ret.Available
|
2014-04-18 20:28:00 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2014-05-01 11:01:30 +08:00
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
|
|
|
ret := &SwapMemoryStat{}
|
2014-04-18 20:28:00 +08:00
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|