github.com/jaypipes/ghw@v0.21.1/pkg/memory/memory_windows.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package memory
     8  
     9  import (
    10  	"github.com/yusufpapurcu/wmi"
    11  
    12  	"github.com/jaypipes/ghw/pkg/unitutil"
    13  )
    14  
    15  const wqlOperatingSystem = "SELECT TotalVisibleMemorySize FROM Win32_OperatingSystem"
    16  
    17  type win32OperatingSystem struct {
    18  	TotalVisibleMemorySize *uint64
    19  }
    20  
    21  const wqlPhysicalMemory = "SELECT BankLabel, Capacity, DataWidth, Description, DeviceLocator, Manufacturer, Model, Name, PartNumber, PositionInRow, SerialNumber, Speed, Tag, TotalWidth FROM Win32_PhysicalMemory"
    22  
    23  type win32PhysicalMemory struct {
    24  	BankLabel     *string
    25  	Capacity      *uint64
    26  	DataWidth     *uint16
    27  	Description   *string
    28  	DeviceLocator *string
    29  	Manufacturer  *string
    30  	Model         *string
    31  	Name          *string
    32  	PartNumber    *string
    33  	PositionInRow *uint32
    34  	SerialNumber  *string
    35  	Speed         *uint32
    36  	Tag           *string
    37  	TotalWidth    *uint16
    38  }
    39  
    40  func (i *Info) load() error {
    41  	// Getting info from WMI
    42  	var win32OSDescriptions []win32OperatingSystem
    43  	if err := wmi.Query(wqlOperatingSystem, &win32OSDescriptions); err != nil {
    44  		return err
    45  	}
    46  	var win32MemDescriptions []win32PhysicalMemory
    47  	if err := wmi.Query(wqlPhysicalMemory, &win32MemDescriptions); err != nil {
    48  		return err
    49  	}
    50  	// We calculate total physical memory size by summing the DIMM sizes
    51  	var totalPhysicalBytes uint64
    52  	i.Modules = make([]*Module, 0, len(win32MemDescriptions))
    53  	for _, description := range win32MemDescriptions {
    54  		totalPhysicalBytes += *description.Capacity
    55  		i.Modules = append(i.Modules, &Module{
    56  			Label:        *description.BankLabel,
    57  			Location:     *description.DeviceLocator,
    58  			SerialNumber: *description.SerialNumber,
    59  			SizeBytes:    int64(*description.Capacity),
    60  			Vendor:       *description.Manufacturer,
    61  		})
    62  	}
    63  	var totalUsableBytes uint64
    64  	for _, description := range win32OSDescriptions {
    65  		// TotalVisibleMemorySize is the amount of memory available for us by
    66  		// the operating system **in Kilobytes**
    67  		totalUsableBytes += *description.TotalVisibleMemorySize * uint64(unitutil.KB)
    68  	}
    69  	i.TotalUsableBytes = int64(totalUsableBytes)
    70  	i.TotalPhysicalBytes = int64(totalPhysicalBytes)
    71  	return nil
    72  }