-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
479 lines (466 loc) · 9.98 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package memory
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
)
const MEMINFO_MAX = 57
// something in a comment
type Meminfo struct {
MemTotal int
MemFree int
MemAvailable int
Buffers int
Cached int
SwapCached int
Active int
Inactive int
Active_anon int
Inactive_anon int
Active_file int
Inactive_file int
Unevictable int
Mlocked int
SwapTotal int
SwapFree int
Zswap int
Zswapped int
Dirty int
Writeback int
AnonPages int
Mapped int
Shmem int
KReclaimable int
Slab int
SReclaimable int
SUnreclaim int
KernelStack int
PageTables int
SecPageTables int
NFS_Unstable int
Bounce int
WritebackTmp int
CommitLimit int
Committed_AS int
VmallocTotal int
VmallocUsed int
VmallocChunk int
Percpu int
HardwareCorrupted int
AnonHugePages int
ShmemHugePages int
ShmemPmdMapped int
FileHugePages int
FilePmdMapped int
CmaTotal int
CmaFree int
Unaccepted int
HugePages_Total int
HugePages_Free int
HugePages_Rsvd int
HugePages_Surp int
Hugepagesize int
Hugetlb int
DirectMap4k int
DirectMap2M int
DirectMap1G int
}
type MemInfoFileLine int
const (
MEMMemTotal MemInfoFileLine = iota
MEMMemFree
MEMMemAvailable
MEMBuffers
MEMCached
MEMSwapCached
MEMActive
MEMInactive
MEMActive_anon
MEMInactive_anon
MEMActive_file
MEMInactive_file
MEMUnevictable
MEMMlocked
MEMSwapTotal
MEMSwapFree
MEMZswap
MEMZswapped
MEMDirty
MEMWriteback
MEMAnonPages
MEMMapped
MEMShmem
MEMKReclaimable
MEMSlab
MEMSReclaimable
MEMSUnreclaim
MEMKernelStack
MEMPageTables
MEMSecPageTables
MEMNFS_Unstable
MEMBounce
MEMWritebackTmp
MEMCommitLimit
MEMCommitted_AS
MEMVmallocTotal
MEMVmallocUsed
MEMVmallocChunk
MEMPercpu
MEMHardwareCorrupted
MEMAnonHugePages
MEMShmemHugePages
MEMShmemPmdMapped
MEMFileHugePages
MEMFilePmdMapped
MEMCmaTotal
MEMCmaFree
MEMUnaccepted
MEMHugePages_Total
MEMHugePages_Free
MEMHugePages_Rsvd
MEMHugePages_Surp
MEMHugepagesize
MEMHugetlb
MEMDirectMap4k
MEMDirectMap2M
MEMDirectMap1G
)
var scale int
func init() {
scale = 1024
}
func SetScale(v int) {
scale = v
}
func (m *Meminfo) InfoPrint() string {
// free/total cache buff
scale := 1024
s := fmt.Sprintf(
"free/tot: %d/%d\t\tbuff/cache:%d/%d",
m.MemFree/scale,
m.MemTotal/scale,
m.Buffers/scale,
m.Cached/scale,
)
return s
}
func Getmeminfo() (*Meminfo, error) {
memfile, err := os.Open("/proc/meminfo")
// memfile, err := os.Open("./meminfo_test.txt")
if err != nil {
log.Fatal(err)
}
defer memfile.Close()
c := csv.NewReader(memfile)
c.Comma = ':'
m := new(Meminfo)
var i MemInfoFileLine
for i = MEMMemTotal; i < MEMINFO_MAX; i++ {
rec, err := c.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal("error from csv read", err)
}
vw := strings.TrimSpace(strings.Fields(rec[1])[0])
value, err := strconv.Atoi(vw)
if err != nil {
log.Fatal("can't convert value", err)
}
switch i {
case MEMMemTotal:
m.MemTotal = value
if err != nil {
log.Fatal("Unable to parse MemTotal", err)
}
case MEMMemFree:
m.MemFree = value
if err != nil {
log.Fatal("Unable to parse MemFree", err)
}
case MEMMemAvailable:
m.MemAvailable = value
if err != nil {
log.Fatal("Unable to parse MemAvailable", err)
}
case MEMBuffers:
m.Buffers = value
if err != nil {
log.Fatal("Unable to parse Buffers", err)
}
case MEMCached:
m.Cached = value
if err != nil {
log.Fatal("Unable to parse Cached", err)
}
case MEMSwapCached:
m.SwapCached = value
if err != nil {
log.Fatal("Unable to parse SwapCached", err)
}
case MEMActive:
m.Active = value
if err != nil {
log.Fatal("Unable to parse Active", err)
}
case MEMInactive:
m.Inactive = value
if err != nil {
log.Fatal("Unable to parse Inactive", err)
}
case MEMActive_anon:
m.Active_anon = value
if err != nil {
log.Fatal("Unable to parse Active_anon", err)
}
case MEMInactive_anon:
m.Inactive_anon = value
if err != nil {
log.Fatal("Unable to parse Inactive_anon", err)
}
case MEMActive_file:
m.Active_file = value
if err != nil {
log.Fatal("Unable to parse Active_file", err)
}
case MEMInactive_file:
m.Inactive_file = value
if err != nil {
log.Fatal("Unable to parse Inactive_file", err)
}
case MEMUnevictable:
m.Unevictable = value
if err != nil {
log.Fatal("Unable to parse Unevictable", err)
}
case MEMMlocked:
m.Mlocked = value
if err != nil {
log.Fatal("Unable to parse Mlocked", err)
}
case MEMSwapTotal:
m.SwapTotal = value
if err != nil {
log.Fatal("Unable to parse SwapTotal", err)
}
case MEMSwapFree:
m.SwapFree = value
if err != nil {
log.Fatal("Unable to parse SwapFree", err)
}
case MEMZswap:
m.Zswap = value
if err != nil {
log.Fatal("Unable to parse Zswap", err)
}
case MEMZswapped:
m.Zswapped = value
if err != nil {
log.Fatal("Unable to parse Zswapped", err)
}
case MEMDirty:
m.Dirty = value
if err != nil {
log.Fatal("Unable to parse Dirty", err)
}
case MEMWriteback:
m.Writeback = value
if err != nil {
log.Fatal("Unable to parse Writeback", err)
}
case MEMAnonPages:
m.AnonPages = value
if err != nil {
log.Fatal("Unable to parse AnonPages", err)
}
case MEMMapped:
m.Mapped = value
if err != nil {
log.Fatal("Unable to parse Mapped", err)
}
case MEMShmem:
m.Shmem = value
if err != nil {
log.Fatal("Unable to parse Shmem", err)
}
case MEMKReclaimable:
m.KReclaimable = value
if err != nil {
log.Fatal("Unable to parse KReclaimable", err)
}
case MEMSlab:
m.Slab = value
if err != nil {
log.Fatal("Unable to parse Slab", err)
}
case MEMSReclaimable:
m.SReclaimable = value
if err != nil {
log.Fatal("Unable to parse SReclaimable", err)
}
case MEMSUnreclaim:
m.SUnreclaim = value
if err != nil {
log.Fatal("Unable to parse SUnreclaim", err)
}
case MEMKernelStack:
m.KernelStack = value
if err != nil {
log.Fatal("Unable to parse KernelStack", err)
}
case MEMPageTables:
m.PageTables = value
if err != nil {
log.Fatal("Unable to parse PageTables", err)
}
case MEMSecPageTables:
m.SecPageTables = value
if err != nil {
log.Fatal("Unable to parse SecPageTables", err)
}
case MEMNFS_Unstable:
m.NFS_Unstable = value
if err != nil {
log.Fatal("Unable to parse NFS_Unstable", err)
}
case MEMBounce:
m.Bounce = value
if err != nil {
log.Fatal("Unable to parse Bounce", err)
}
case MEMWritebackTmp:
m.WritebackTmp = value
if err != nil {
log.Fatal("Unable to parse WritebackTemp", err)
}
case MEMCommitLimit:
m.CommitLimit = value
if err != nil {
log.Fatal("Unable to parse CommitLimit", err)
}
case MEMCommitted_AS:
m.Committed_AS = value
if err != nil {
log.Fatal("Unable to parse CommitLimit_AS", err)
}
case MEMVmallocTotal:
m.VmallocTotal = value
if err != nil {
log.Fatal("Unable to parse VmallocTotal", err)
}
case MEMVmallocUsed:
m.VmallocUsed = value
if err != nil {
log.Fatal("Unable to parse VmallocUsed", err)
}
case MEMVmallocChunk:
m.VmallocChunk = value
if err != nil {
log.Fatal("Unable to parse VmallocChunk", err)
}
case MEMPercpu:
m.Percpu = value
if err != nil {
log.Fatal("Unable to parse Percpu", err)
}
case MEMHardwareCorrupted:
m.HardwareCorrupted = value
if err != nil {
log.Fatal("Unable to parse HardwareCorrupted", err)
}
case MEMAnonHugePages:
m.AnonHugePages = value
if err != nil {
log.Fatal("Unable to parse AnonHugePages", err)
}
case MEMShmemHugePages:
m.ShmemHugePages = value
if err != nil {
log.Fatal("Unable to parse ShmemHugePages", err)
}
case MEMShmemPmdMapped:
m.ShmemPmdMapped = value
if err != nil {
log.Fatal("Unable to parse ShmemPmdMapped", err)
}
case MEMFileHugePages:
m.FileHugePages = value
if err != nil {
log.Fatal("Unable to parse FileHugePages", err)
}
case MEMFilePmdMapped:
m.FilePmdMapped = value
if err != nil {
log.Fatal("Unable to parse FilePmdMapped", err)
}
case MEMCmaTotal:
m.CmaTotal = value
if err != nil {
log.Fatal("Unable to parse CmaTotal", err)
}
case MEMCmaFree:
m.CmaFree = value
if err != nil {
log.Fatal("Unable to parse CmaFree", err)
}
case MEMUnaccepted:
m.Unaccepted = value
if err != nil {
log.Fatal("Unable to parse Unaccepted", err)
}
case MEMHugePages_Total:
m.HugePages_Total = value
if err != nil {
log.Fatal("Unable to parse HugePages_Total", err)
}
case MEMHugePages_Free:
m.HugePages_Free = value
if err != nil {
log.Fatal("Unable to parse HugePages_Free", err)
}
case MEMHugePages_Rsvd:
m.HugePages_Rsvd = value
if err != nil {
log.Fatal("Unable to parse HugePages_Rsvd", err)
}
case MEMHugePages_Surp:
m.HugePages_Surp = value
if err != nil {
log.Fatal("Unable to parse HugePages_Surp", err)
}
case MEMHugepagesize:
m.Hugepagesize = value
if err != nil {
log.Fatal("Unable to parse Hugepagesize", err)
}
case MEMHugetlb:
m.Hugetlb = value
if err != nil {
log.Fatal("Unable to parse Hugetlb", err)
}
case MEMDirectMap4k:
m.DirectMap4k = value
if err != nil {
log.Fatal("Unable to parse DirectMap4k", err)
}
case MEMDirectMap2M:
m.DirectMap2M = value
if err != nil {
log.Fatal("Unable to parse DirectMap2M", err)
}
case MEMDirectMap1G:
m.DirectMap1G = value
if err != nil {
log.Fatal("Unable to parse DirectMap1G", err)
}
default:
log.Println("Unkown field, not parsing ", rec)
}
}
return m, nil
}