2017-02-01 18:26:00 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package mem
|
|
|
|
|
|
|
|
import (
|
2018-10-19 18:33:23 +00:00
|
|
|
"context"
|
2017-02-01 18:26:00 +00:00
|
|
|
"unsafe"
|
|
|
|
|
2020-11-07 01:48:38 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
2018-10-19 18:33:23 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2017-02-01 18:26:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
procGlobalMemoryStatusEx = common.Modkernel32.NewProc("GlobalMemoryStatusEx")
|
2018-10-19 18:33:23 +00:00
|
|
|
procGetPerformanceInfo = common.ModPsapi.NewProc("GetPerformanceInfo")
|
2017-02-01 18:26:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type memoryStatusEx struct {
|
|
|
|
cbSize uint32
|
|
|
|
dwMemoryLoad uint32
|
|
|
|
ullTotalPhys uint64 // in bytes
|
|
|
|
ullAvailPhys uint64
|
|
|
|
ullTotalPageFile uint64
|
|
|
|
ullAvailPageFile uint64
|
|
|
|
ullTotalVirtual uint64
|
|
|
|
ullAvailVirtual uint64
|
|
|
|
ullAvailExtendedVirtual uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return VirtualMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
var memInfo memoryStatusEx
|
|
|
|
memInfo.cbSize = uint32(unsafe.Sizeof(memInfo))
|
|
|
|
mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo)))
|
|
|
|
if mem == 0 {
|
2018-10-19 18:33:23 +00:00
|
|
|
return nil, windows.GetLastError()
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret := &VirtualMemoryStat{
|
|
|
|
Total: memInfo.ullTotalPhys,
|
|
|
|
Available: memInfo.ullAvailPhys,
|
2020-10-07 16:57:18 +00:00
|
|
|
Free: memInfo.ullAvailPhys,
|
2017-02-01 18:26:00 +00:00
|
|
|
UsedPercent: float64(memInfo.dwMemoryLoad),
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Used = ret.Total - ret.Available
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:26:00 +00:00
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return SwapMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
|
|
|
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
|
|
|
|
var usedPercent float64
|
|
|
|
if tot == 0 {
|
|
|
|
usedPercent = 0
|
|
|
|
} else {
|
2020-07-01 12:47:56 +00:00
|
|
|
usedPercent = float64(used) / float64(tot) * 100
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
ret := &SwapMemoryStat{
|
|
|
|
Total: tot,
|
|
|
|
Used: used,
|
|
|
|
Free: free,
|
|
|
|
UsedPercent: usedPercent,
|
|
|
|
}
|
2017-02-01 18:26:00 +00:00
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|