...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package procfs
15
16 import (
17 "bufio"
18 "bytes"
19 "fmt"
20 "io"
21 "strconv"
22 "strings"
23
24 "github.com/prometheus/procfs/internal/util"
25 )
26
27
28 type Meminfo struct {
29
30
31 MemTotal *uint64
32
33 MemFree *uint64
34
35
36
37
38
39
40
41
42 MemAvailable *uint64
43
44
45 Buffers *uint64
46 Cached *uint64
47
48
49
50
51 SwapCached *uint64
52
53
54 Active *uint64
55
56
57 Inactive *uint64
58 ActiveAnon *uint64
59 InactiveAnon *uint64
60 ActiveFile *uint64
61 InactiveFile *uint64
62 Unevictable *uint64
63 Mlocked *uint64
64
65 SwapTotal *uint64
66
67
68 SwapFree *uint64
69
70 Dirty *uint64
71
72 Writeback *uint64
73
74 AnonPages *uint64
75
76 Mapped *uint64
77 Shmem *uint64
78
79 Slab *uint64
80
81 SReclaimable *uint64
82
83 SUnreclaim *uint64
84 KernelStack *uint64
85
86
87 PageTables *uint64
88
89
90 NFSUnstable *uint64
91
92 Bounce *uint64
93
94 WritebackTmp *uint64
95
96
97
98
99
100
101
102
103
104
105
106
107
108 CommitLimit *uint64
109
110
111
112
113
114
115
116
117
118
119
120
121
122 CommittedAS *uint64
123
124 VmallocTotal *uint64
125
126 VmallocUsed *uint64
127
128 VmallocChunk *uint64
129 Percpu *uint64
130 HardwareCorrupted *uint64
131 AnonHugePages *uint64
132 ShmemHugePages *uint64
133 ShmemPmdMapped *uint64
134 CmaTotal *uint64
135 CmaFree *uint64
136 HugePagesTotal *uint64
137 HugePagesFree *uint64
138 HugePagesRsvd *uint64
139 HugePagesSurp *uint64
140 Hugepagesize *uint64
141 DirectMap4k *uint64
142 DirectMap2M *uint64
143 DirectMap1G *uint64
144
145
146
147
148 MemTotalBytes *uint64
149 MemFreeBytes *uint64
150 MemAvailableBytes *uint64
151 BuffersBytes *uint64
152 CachedBytes *uint64
153 SwapCachedBytes *uint64
154 ActiveBytes *uint64
155 InactiveBytes *uint64
156 ActiveAnonBytes *uint64
157 InactiveAnonBytes *uint64
158 ActiveFileBytes *uint64
159 InactiveFileBytes *uint64
160 UnevictableBytes *uint64
161 MlockedBytes *uint64
162 SwapTotalBytes *uint64
163 SwapFreeBytes *uint64
164 DirtyBytes *uint64
165 WritebackBytes *uint64
166 AnonPagesBytes *uint64
167 MappedBytes *uint64
168 ShmemBytes *uint64
169 SlabBytes *uint64
170 SReclaimableBytes *uint64
171 SUnreclaimBytes *uint64
172 KernelStackBytes *uint64
173 PageTablesBytes *uint64
174 NFSUnstableBytes *uint64
175 BounceBytes *uint64
176 WritebackTmpBytes *uint64
177 CommitLimitBytes *uint64
178 CommittedASBytes *uint64
179 VmallocTotalBytes *uint64
180 VmallocUsedBytes *uint64
181 VmallocChunkBytes *uint64
182 PercpuBytes *uint64
183 HardwareCorruptedBytes *uint64
184 AnonHugePagesBytes *uint64
185 ShmemHugePagesBytes *uint64
186 ShmemPmdMappedBytes *uint64
187 CmaTotalBytes *uint64
188 CmaFreeBytes *uint64
189 HugepagesizeBytes *uint64
190 DirectMap4kBytes *uint64
191 DirectMap2MBytes *uint64
192 DirectMap1GBytes *uint64
193 }
194
195
196
197 func (fs FS) Meminfo() (Meminfo, error) {
198 b, err := util.ReadFileNoStat(fs.proc.Path("meminfo"))
199 if err != nil {
200 return Meminfo{}, err
201 }
202
203 m, err := parseMemInfo(bytes.NewReader(b))
204 if err != nil {
205 return Meminfo{}, fmt.Errorf("%s: %w", ErrFileParse, err)
206 }
207
208 return *m, nil
209 }
210
211 func parseMemInfo(r io.Reader) (*Meminfo, error) {
212 var m Meminfo
213 s := bufio.NewScanner(r)
214 for s.Scan() {
215 fields := strings.Fields(s.Text())
216 var val, valBytes uint64
217
218 val, err := strconv.ParseUint(fields[1], 0, 64)
219 if err != nil {
220 return nil, err
221 }
222
223 switch len(fields) {
224 case 2:
225
226 valBytes = val
227 case 3:
228
229
230
231 if fields[2] != "kB" {
232 return nil, fmt.Errorf("%w: Unsupported unit in optional 3rd field %q", ErrFileParse, fields[2])
233 }
234
235 valBytes = 1024 * val
236
237 default:
238 return nil, fmt.Errorf("%w: Malformed line %q", ErrFileParse, s.Text())
239 }
240
241 switch fields[0] {
242 case "MemTotal:":
243 m.MemTotal = &val
244 m.MemTotalBytes = &valBytes
245 case "MemFree:":
246 m.MemFree = &val
247 m.MemFreeBytes = &valBytes
248 case "MemAvailable:":
249 m.MemAvailable = &val
250 m.MemAvailableBytes = &valBytes
251 case "Buffers:":
252 m.Buffers = &val
253 m.BuffersBytes = &valBytes
254 case "Cached:":
255 m.Cached = &val
256 m.CachedBytes = &valBytes
257 case "SwapCached:":
258 m.SwapCached = &val
259 m.SwapCachedBytes = &valBytes
260 case "Active:":
261 m.Active = &val
262 m.ActiveBytes = &valBytes
263 case "Inactive:":
264 m.Inactive = &val
265 m.InactiveBytes = &valBytes
266 case "Active(anon):":
267 m.ActiveAnon = &val
268 m.ActiveAnonBytes = &valBytes
269 case "Inactive(anon):":
270 m.InactiveAnon = &val
271 m.InactiveAnonBytes = &valBytes
272 case "Active(file):":
273 m.ActiveFile = &val
274 m.ActiveFileBytes = &valBytes
275 case "Inactive(file):":
276 m.InactiveFile = &val
277 m.InactiveFileBytes = &valBytes
278 case "Unevictable:":
279 m.Unevictable = &val
280 m.UnevictableBytes = &valBytes
281 case "Mlocked:":
282 m.Mlocked = &val
283 m.MlockedBytes = &valBytes
284 case "SwapTotal:":
285 m.SwapTotal = &val
286 m.SwapTotalBytes = &valBytes
287 case "SwapFree:":
288 m.SwapFree = &val
289 m.SwapFreeBytes = &valBytes
290 case "Dirty:":
291 m.Dirty = &val
292 m.DirtyBytes = &valBytes
293 case "Writeback:":
294 m.Writeback = &val
295 m.WritebackBytes = &valBytes
296 case "AnonPages:":
297 m.AnonPages = &val
298 m.AnonPagesBytes = &valBytes
299 case "Mapped:":
300 m.Mapped = &val
301 m.MappedBytes = &valBytes
302 case "Shmem:":
303 m.Shmem = &val
304 m.ShmemBytes = &valBytes
305 case "Slab:":
306 m.Slab = &val
307 m.SlabBytes = &valBytes
308 case "SReclaimable:":
309 m.SReclaimable = &val
310 m.SReclaimableBytes = &valBytes
311 case "SUnreclaim:":
312 m.SUnreclaim = &val
313 m.SUnreclaimBytes = &valBytes
314 case "KernelStack:":
315 m.KernelStack = &val
316 m.KernelStackBytes = &valBytes
317 case "PageTables:":
318 m.PageTables = &val
319 m.PageTablesBytes = &valBytes
320 case "NFS_Unstable:":
321 m.NFSUnstable = &val
322 m.NFSUnstableBytes = &valBytes
323 case "Bounce:":
324 m.Bounce = &val
325 m.BounceBytes = &valBytes
326 case "WritebackTmp:":
327 m.WritebackTmp = &val
328 m.WritebackTmpBytes = &valBytes
329 case "CommitLimit:":
330 m.CommitLimit = &val
331 m.CommitLimitBytes = &valBytes
332 case "Committed_AS:":
333 m.CommittedAS = &val
334 m.CommittedASBytes = &valBytes
335 case "VmallocTotal:":
336 m.VmallocTotal = &val
337 m.VmallocTotalBytes = &valBytes
338 case "VmallocUsed:":
339 m.VmallocUsed = &val
340 m.VmallocUsedBytes = &valBytes
341 case "VmallocChunk:":
342 m.VmallocChunk = &val
343 m.VmallocChunkBytes = &valBytes
344 case "Percpu:":
345 m.Percpu = &val
346 m.PercpuBytes = &valBytes
347 case "HardwareCorrupted:":
348 m.HardwareCorrupted = &val
349 m.HardwareCorruptedBytes = &valBytes
350 case "AnonHugePages:":
351 m.AnonHugePages = &val
352 m.AnonHugePagesBytes = &valBytes
353 case "ShmemHugePages:":
354 m.ShmemHugePages = &val
355 m.ShmemHugePagesBytes = &valBytes
356 case "ShmemPmdMapped:":
357 m.ShmemPmdMapped = &val
358 m.ShmemPmdMappedBytes = &valBytes
359 case "CmaTotal:":
360 m.CmaTotal = &val
361 m.CmaTotalBytes = &valBytes
362 case "CmaFree:":
363 m.CmaFree = &val
364 m.CmaFreeBytes = &valBytes
365 case "HugePages_Total:":
366 m.HugePagesTotal = &val
367 case "HugePages_Free:":
368 m.HugePagesFree = &val
369 case "HugePages_Rsvd:":
370 m.HugePagesRsvd = &val
371 case "HugePages_Surp:":
372 m.HugePagesSurp = &val
373 case "Hugepagesize:":
374 m.Hugepagesize = &val
375 m.HugepagesizeBytes = &valBytes
376 case "DirectMap4k:":
377 m.DirectMap4k = &val
378 m.DirectMap4kBytes = &valBytes
379 case "DirectMap2M:":
380 m.DirectMap2M = &val
381 m.DirectMap2MBytes = &valBytes
382 case "DirectMap1G:":
383 m.DirectMap1G = &val
384 m.DirectMap1GBytes = &valBytes
385 }
386 }
387
388 return &m, nil
389 }
390
View as plain text