forked from zcalusic/sysinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.go
241 lines (203 loc) · 5.47 KB
/
memory.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright © 2016 Zlatko Čalušić
//
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.
package sysinfo
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"io/ioutil"
"os"
"strconv"
"strings"
"syscall"
)
// Memory information.
type Memory struct {
Type string `json:"type,omitempty"`
Speed uint `json:"speed,omitempty"` // RAM data rate in MT/s
Size uint `json:"size,omitempty"` // RAM size in MB
}
const epsSize = 0x1f
// ErrNotExist indicates that SMBIOS entry point could not be found.
var ErrNotExist = errors.New("SMBIOS entry point not found")
func word(data []byte, index int) uint16 {
return binary.LittleEndian.Uint16(data[index : index+2])
}
func dword(data []byte, index int) uint32 {
return binary.LittleEndian.Uint32(data[index : index+4])
}
func qword(data []byte, index int) uint64 {
return binary.LittleEndian.Uint64(data[index : index+8])
}
func epsChecksum(sl []byte) (sum byte) {
for _, v := range sl {
sum += v
}
return
}
func epsValid(eps []byte) bool {
if epsChecksum(eps) == 0 && bytes.Equal(eps[0x10:0x15], []byte("_DMI_")) && epsChecksum(eps[0x10:]) == 0 {
return true
}
return false
}
func getStructureTableAddressEFI(f *os.File) (address int64, length int, err error) {
systab, err := os.Open("/sys/firmware/efi/systab")
if err != nil {
return 0, 0, err
}
defer systab.Close()
s := bufio.NewScanner(systab)
for s.Scan() {
sl := strings.Split(s.Text(), "=")
if len(sl) != 2 || sl[0] != "SMBIOS" {
continue
}
addr, err := strconv.ParseInt(sl[1], 0, 64)
if err != nil {
return 0, 0, err
}
eps, err := syscall.Mmap(int(f.Fd()), addr, epsSize, syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
return 0, 0, err
}
defer syscall.Munmap(eps)
if !epsValid(eps) {
break
}
return int64(dword(eps, 0x18)), int(word(eps, 0x16)), nil
}
if err := s.Err(); err != nil {
return 0, 0, err
}
return 0, 0, ErrNotExist
}
func getStructureTableAddress(f *os.File) (address int64, length int, err error) {
// SMBIOS Reference Specification Version 3.0.0, page 21
mem, err := syscall.Mmap(int(f.Fd()), 0xf0000, 0x10000, syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
return 0, 0, err
}
defer syscall.Munmap(mem)
for i := range mem {
if i > len(mem)-epsSize {
break
}
// Search for the anchor string on paragraph (16 byte) boundaries.
if i%16 != 0 || !bytes.Equal(mem[i:i+4], []byte("_SM_")) {
continue
}
eps := mem[i : i+epsSize]
if !epsValid(eps) {
continue
}
return int64(dword(eps, 0x18)), int(word(eps, 0x16)), nil
}
return 0, 0, ErrNotExist
}
func getStructureTable() ([]byte, error) {
f, err := os.Open("/dev/mem")
if err != nil {
dmi, err := ioutil.ReadFile("/sys/firmware/dmi/tables/DMI")
if err != nil {
return nil, err
}
return dmi, nil
}
defer f.Close()
address, length, err := getStructureTableAddressEFI(f)
if err != nil {
if address, length, err = getStructureTableAddress(f); err != nil {
return nil, err
}
}
// Mandatory page aligning for mmap() system call, lest we get EINVAL
align := address & (int64(os.Getpagesize()) - 1)
mem, err := syscall.Mmap(int(f.Fd()), address-align, length+int(align), syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
return nil, err
}
return mem[align:], nil
}
func (si *SysInfo) getMemoryInfo() {
mem, err := getStructureTable()
if err != nil {
if targetKB := slurpFile("/sys/devices/system/xen_memory/xen_memory0/target_kb"); targetKB != "" {
si.Memory.Type = "DRAM"
size, _ := strconv.ParseUint(targetKB, 10, 64)
si.Memory.Size = uint(size) / 1024
}
return
}
defer syscall.Munmap(mem)
var memSizeAlt uint
loop:
for p := 0; p < len(mem)-1; {
recType := mem[p]
recLen := mem[p+1]
switch recType {
case 4:
if si.CPU.Speed == 0 {
si.CPU.Speed = uint(word(mem, p+0x16))
}
case 17:
size := uint(word(mem, p+0x0c))
if size == 0 || size == 0xffff || size&0x8000 == 0x8000 {
break
}
if size == 0x7fff {
if recLen >= 0x20 {
size = uint(dword(mem, p+0x1c))
} else {
break
}
}
si.Memory.Size += size
if si.Memory.Type == "" {
// SMBIOS Reference Specification Version 3.0.0, page 92
memTypes := [...]string{
"Other", "Unknown", "DRAM", "EDRAM", "VRAM", "SRAM", "RAM", "ROM", "FLASH",
"EEPROM", "FEPROM", "EPROM", "CDRAM", "3DRAM", "SDRAM", "SGRAM", "RDRAM",
"DDR", "DDR2", "DDR2 FB-DIMM", "Reserved", "Reserved", "Reserved", "DDR3",
"FBD2", "DDR4", "LPDDR", "LPDDR2", "LPDDR3", "LPDDR4",
}
if index := int(mem[p+0x12]); index >= 1 && index <= len(memTypes) {
si.Memory.Type = memTypes[index-1]
}
}
if si.Memory.Speed == 0 && recLen >= 0x17 {
if speed := uint(word(mem, p+0x15)); speed != 0 {
si.Memory.Speed = speed
}
}
case 19:
start := uint(dword(mem, p+0x04))
end := uint(dword(mem, p+0x08))
if start == 0xffffffff && end == 0xffffffff {
if recLen >= 0x1f {
start64 := qword(mem, p+0x0f)
end64 := qword(mem, p+0x17)
memSizeAlt += uint((end64 - start64 + 1) / 1048576)
}
} else {
memSizeAlt += (end - start + 1) / 1024
}
case 127:
break loop
}
for p += int(recLen); p < len(mem)-1; {
if bytes.Equal(mem[p:p+2], []byte{0, 0}) {
p += 2
break
}
p++
}
}
// Sometimes DMI type 17 has no information, so we fall back to DMI type 19, to at least get the RAM size.
if si.Memory.Size == 0 && memSizeAlt > 0 {
si.Memory.Type = "DRAM"
si.Memory.Size = memSizeAlt
}
}