Skip to content
11 changes: 10 additions & 1 deletion memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ import (
"math"
)

type MemoryModule struct {
Label string `json:"label"`
Location string `json:"location"`
SerialNumber string `json:"serial_number"`
SizeBytes int64 `json:"size_bytes"`
Vendor string `json:"vendor"`
}

type MemoryInfo struct {
TotalPhysicalBytes int64 `json:"total_physical_bytes"`
TotalUsableBytes int64 `json:"total_usable_bytes"`
// An array of sizes, in bytes, of memory pages supported by the host
SupportedPageSizes []uint64 `json:"supported_page_sizes"`
SupportedPageSizes []uint64 `json:"supported_page_sizes"`
Modules []*MemoryModule `json:"modules"`
}

func Memory(opts ...*WithOption) (*MemoryInfo, error) {
Expand Down
41 changes: 38 additions & 3 deletions memory_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/StackExchange/wmi"
)

const wmqlMemory = "SELECT FreePhysicalMemory, FreeSpaceInPagingFiles, FreeVirtualMemory, TotalSwapSpaceSize, TotalVirtualMemorySize, TotalVisibleMemorySize FROM Win32_OperatingSystem"
const wmqlOperatingSystem = "SELECT FreePhysicalMemory, FreeSpaceInPagingFiles, FreeVirtualMemory, TotalSwapSpaceSize, TotalVirtualMemorySize, TotalVisibleMemorySize FROM Win32_OperatingSystem"

type win32OperatingSystem struct {
FreePhysicalMemory uint64
Expand All @@ -21,12 +21,47 @@ type win32OperatingSystem struct {
TotalVisibleMemorySize uint64
}

const wmqlPhysicalMemory = "SELECT BankLabel, Capacity, DataWidth, Description, DeviceLocator, Manufacturer, Model, Name, PartNumber, PositionInRow, SerialNumber, Speed, Tag, TotalWidth FROM Win32_PhysicalMemory"

type win32PhysicalMemory struct {
BankLabel string
Capacity uint64
DataWidth uint16
Description string
DeviceLocator string
Manufacturer string
Model string
Name string
PartNumber string
PositionInRow uint32
SerialNumber string
Speed uint32
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a future PR (not this one), I'd be interested in adding a Speed field to the MemoryModule struct. Need to have a conversation about what the appropriate data type is, though, so let's leave that for a future commit.

Tag string
TotalWidth uint16
}

func (ctx *context) memFillInfo(info *MemoryInfo) error {
// Getting info from WMI
var win32OSDescriptions []win32OperatingSystem
if err := wmi.Query(wmqlMemory, &win32OSDescriptions); err != nil {
if err := wmi.Query(wmqlOperatingSystem, &win32OSDescriptions); err != nil {
return err
}
var win32MemDescriptions []win32PhysicalMemory
if err := wmi.Query(wmqlPhysicalMemory, &win32MemDescriptions); err != nil {
return err
}
// Converting into standard structures
// Handling physical memory modules
info.Modules = make([]*MemoryModule, 0, len(win32MemDescriptions))
for _, description := range win32MemDescriptions {
info.Modules = append(info.Modules, &MemoryModule{
Label: description.BankLabel,
Location: description.DeviceLocator,
SerialNumber: description.SerialNumber,
SizeBytes: int64(description.Capacity),
Vendor: description.Manufacturer,
})
}
// Handling physical memory total/free size (as seen by OS)
var totalUsableBytes uint64
var totalPhysicalBytes uint64
Expand All @@ -37,4 +72,4 @@ func (ctx *context) memFillInfo(info *MemoryInfo) error {
info.TotalUsableBytes = int64(totalUsableBytes)
info.TotalPhysicalBytes = int64(totalPhysicalBytes)
return nil
}
}