Skip to content

Commit

Permalink
[mem][linux] Fix shirou#1002 only try to parse /proc/meminfo numeric …
Browse files Browse the repository at this point in the history
…values on fields we're interested in
  • Loading branch information
Lomanic committed Nov 29, 2020
1 parent 07887a9 commit cd25417
Show file tree
Hide file tree
Showing 6 changed files with 446 additions and 8 deletions.
148 changes: 144 additions & 4 deletions mem/mem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,86 +73,226 @@ func fillFromMeminfoWithContext(ctx context.Context) (*VirtualMemoryStat, *Virtu
value := strings.TrimSpace(fields[1])
value = strings.Replace(value, " kB", "", -1)

t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx,err
}
switch key {
case "MemTotal":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.Total = t * 1024
case "MemFree":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.Free = t * 1024
case "MemAvailable":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
memavail = true
ret.Available = t * 1024
case "Buffers":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.Buffers = t * 1024
case "Cached":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.Cached = t * 1024
case "Active":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.Active = t * 1024
case "Inactive":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.Inactive = t * 1024
case "Active(anon)":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
retEx.ActiveAnon = t * 1024
case "Inactive(anon)":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
retEx.InactiveAnon = t * 1024
case "Active(file)":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
activeFile = true
retEx.ActiveFile = t * 1024
case "Inactive(file)":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
inactiveFile = true
retEx.InactiveFile = t * 1024
case "Unevictable":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
retEx.Unevictable = t * 1024
case "Writeback":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.Writeback = t * 1024
case "WritebackTmp":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.WritebackTmp = t * 1024
case "Dirty":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.Dirty = t * 1024
case "Shmem":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.Shared = t * 1024
case "Slab":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.Slab = t * 1024
case "SReclaimable":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
sReclaimable = true
ret.SReclaimable = t * 1024
case "SUnreclaim":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.SUnreclaim = t * 1024
case "PageTables":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.PageTables = t * 1024
case "SwapCached":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.SwapCached = t * 1024
case "CommitLimit":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.CommitLimit = t * 1024
case "Committed_AS":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.CommittedAS = t * 1024
case "HighTotal":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.HighTotal = t * 1024
case "HighFree":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.HighFree = t * 1024
case "LowTotal":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.LowTotal = t * 1024
case "LowFree":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.LowFree = t * 1024
case "SwapTotal":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.SwapTotal = t * 1024
case "SwapFree":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.SwapFree = t * 1024
case "Mapped":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.Mapped = t * 1024
case "VmallocTotal":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.VMallocTotal = t * 1024
case "VmallocUsed":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.VMallocUsed = t * 1024
case "VmallocChunk":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.VMallocChunk = t * 1024
case "HugePages_Total":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.HugePagesTotal = t
case "HugePages_Free":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.HugePagesFree = t
case "Hugepagesize":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.HugePageSize = t * 1024
}
}
Expand Down
37 changes: 37 additions & 0 deletions mem/mem_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,43 @@ var virtualMemoryTests = []struct {
HugePagesFree: 0,
HugePageSize: 2097152},
},
{"issue1002", &VirtualMemoryStat{
Total: 260579328,
Available: 215199744,
Used: 34328576,
UsedPercent: 13.173944481121694,
Free: 124506112,
Active: 108785664,
Inactive: 8581120,
Wired: 0,
Laundry: 0,
Buffers: 4915200,
Cached: 96829440,
Writeback: 0,
Dirty: 0,
WritebackTmp: 0,
Shared: 0,
Slab: 9293824,
SReclaimable: 2764800,
SUnreclaim: 6529024,
PageTables: 405504,
SwapCached: 0,
CommitLimit: 130289664,
CommittedAS: 25567232,
HighTotal: 134217728,
HighFree: 67784704,
LowTotal: 126361600,
LowFree: 56721408,
SwapTotal: 0,
SwapFree: 0,
Mapped: 38793216,
VMallocTotal: 1996488704,
VMallocUsed: 0,
VMallocChunk: 0,
HugePagesTotal: 0,
HugePagesFree: 0,
HugePageSize: 0},
},
}

func TestVirtualMemoryLinux(t *testing.T) {
Expand Down
42 changes: 42 additions & 0 deletions mem/testdata/linux/virtualmemory/issue1002/proc/meminfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
total: used: free: shared: buffers: cached:
Mem: 260579328 136073216 124506112 0 4915200 94064640
Swap: 0 0 0
MemTotal: 254472 kB
MemFree: 121588 kB
MemShared: 0 kB
Buffers: 4800 kB
Cached: 91860 kB
SwapCached: 0 kB
Active: 106236 kB
Inactive: 8380 kB
MemAvailable: 210156 kB
Active(anon): 17956 kB
Inactive(anon): 0 kB
Active(file): 88280 kB
Inactive(file): 8380 kB
Unevictable: 0 kB
Mlocked: 0 kB
HighTotal: 131072 kB
HighFree: 66196 kB
LowTotal: 123400 kB
LowFree: 55392 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 17992 kB
Mapped: 37884 kB
Shmem: 0 kB
Slab: 9076 kB
SReclaimable: 2700 kB
SUnreclaim: 6376 kB
KernelStack: 624 kB
PageTables: 396 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 127236 kB
Committed_AS: 24968 kB
VmallocTotal: 1949696 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
Loading

0 comments on commit cd25417

Please sign in to comment.