//go:build windows // Package collector 提供 Windows 内存指标采集能力。 // Logan.Li package collector import ( "fmt" "syscall" "unsafe" "ezops/agent/model" ) type memoryStatusEx struct { Length uint32 MemoryLoad uint32 TotalPhys uint64 AvailPhys uint64 TotalPageFile uint64 AvailPageFile uint64 TotalVirtual uint64 AvailVirtual uint64 AvailExtendedVirtual uint64 } type MemCollector struct{} func NewMemCollector() *MemCollector { return &MemCollector{} } func (m *MemCollector) Collect() (model.MemoryStat, error) { var ms memoryStatusEx ms.Length = uint32(unsafe.Sizeof(ms)) ret, _, err := syscall.NewLazyDLL("kernel32.dll").NewProc("GlobalMemoryStatusEx").Call(uintptr(unsafe.Pointer(&ms))) if ret == 0 { return model.MemoryStat{}, fmt.Errorf("GlobalMemoryStatusEx 失败: %w", err) } total := ms.TotalPhys avail := ms.AvailPhys used := total - avail swapTotal := ms.TotalPageFile swapAvail := ms.AvailPageFile swapUsed := uint64(0) if swapTotal > swapAvail { swapUsed = swapTotal - swapAvail } stat := model.MemoryStat{ TotalGB: bytesToGB(total), UsedGB: bytesToGB(used), AvailableGB: bytesToGB(avail), SwapUsedGB: bytesToGB(swapUsed), UsagePercent: float64(ms.MemoryLoad), } return stat, nil }