forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_windows.go
74 lines (67 loc) · 2.36 KB
/
memory_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
package ghw
import (
"github.com/StackExchange/wmi"
)
const wqlOperatingSystem = "SELECT FreePhysicalMemory, TotalVisibleMemorySize FROM Win32_OperatingSystem"
type win32OperatingSystem struct {
FreePhysicalMemory uint64
TotalVisibleMemorySize uint64
}
const wqlPhysicalMemory = "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
Tag string
TotalWidth uint16
}
func (ctx *context) memFillInfo(info *MemoryInfo) error {
// Getting info from WMI
var win32OSDescriptions []win32OperatingSystem
if err := wmi.Query(wqlOperatingSystem, &win32OSDescriptions); err != nil {
return err
}
var win32MemDescriptions []win32PhysicalMemory
if err := wmi.Query(wqlPhysicalMemory, &win32MemDescriptions); err != nil {
return err
}
// We calculate total physical memory size by summing the DIMM sizes
var totalPhysicalBytes uint64
info.Modules = make([]*MemoryModule, 0, len(win32MemDescriptions))
for _, description := range win32MemDescriptions {
totalPhysicalBytes += description.Capacity
info.Modules = append(info.Modules, &MemoryModule{
Label: description.BankLabel,
Location: description.DeviceLocator,
SerialNumber: description.SerialNumber,
SizeBytes: int64(description.Capacity),
Vendor: description.Manufacturer,
})
}
var totalAvailableBytes uint64
var totalUsableBytes uint64
for _, description := range win32OSDescriptions {
totalAvailableBytes += description.FreePhysicalMemory * uint64(KB)
// TotalVisibleMemorySize is the amount of memory available for us by
// the operating system **in Kilobytes**
totalUsableBytes += description.TotalVisibleMemorySize * uint64(KB)
}
info.TotalAvailableBytes = int64(totalAvailableBytes)
info.TotalUsableBytes = int64(totalUsableBytes)
info.TotalPhysicalBytes = int64(totalPhysicalBytes)
return nil
}