Skip to content

Commit

Permalink
Add support for reading AnonHugePages from /proc/meminfo
Browse files Browse the repository at this point in the history
This commit adds support for reading the `AnonHugePages` field from `/proc/meminfo`.

The values in this field allow monitoring the [THP](https://www.kernel.org/doc/Documentation/vm/transhuge.txt) usage by systems that use this type of memory
  • Loading branch information
kevinconaway committed Aug 31, 2023
1 parent faad806 commit 7f4efa5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mem/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type VirtualMemoryStat struct {
// https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html
// https://www.kernel.org/doc/Documentation/filesystems/proc.txt
// https://www.kernel.org/doc/Documentation/vm/overcommit-accounting
// https://www.kernel.org/doc/Documentation/vm/transhuge.txt
Buffers uint64 `json:"buffers"`
Cached uint64 `json:"cached"`
WriteBack uint64 `json:"writeBack"`
Expand Down Expand Up @@ -78,6 +79,7 @@ type VirtualMemoryStat struct {
HugePagesRsvd uint64 `json:"hugePagesRsvd"`
HugePagesSurp uint64 `json:"hugePagesSurp"`
HugePageSize uint64 `json:"hugePageSize"`
AnonHugePages uint64 `json:"anonHugePages"`
}

type SwapMemoryStat struct {
Expand Down
6 changes: 6 additions & 0 deletions mem/mem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ func fillFromMeminfoWithContext(ctx context.Context) (*VirtualMemoryStat, *Virtu
return ret, retEx, err
}
ret.HugePageSize = t * 1024
case "AnonHugePages":
t, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return ret, retEx, err
}
ret.AnonHugePages = t * 1024
}
}

Expand Down
10 changes: 10 additions & 0 deletions mem/mem_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ var virtualMemoryTests = []struct {
HugePageSize: 0,
},
},
{
"anonhugepages", &VirtualMemoryStat{
Total: 260799420 * 1024,
Available: 127880216 * 1024,
Free: 119443248 * 1024,
AnonHugePages: 50409472 * 1024,
Used: 144748720128,
UsedPercent: 54.20110673559013,
},
},
}

func TestVirtualMemoryLinux(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion mem/mem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestVirtualMemoryStat_String(t *testing.T) {
Free: 40,
}
t.Log(v)
e := `{"total":10,"available":20,"used":30,"usedPercent":30.1,"free":40,"active":0,"inactive":0,"wired":0,"laundry":0,"buffers":0,"cached":0,"writeBack":0,"dirty":0,"writeBackTmp":0,"shared":0,"slab":0,"sreclaimable":0,"sunreclaim":0,"pageTables":0,"swapCached":0,"commitLimit":0,"committedAS":0,"highTotal":0,"highFree":0,"lowTotal":0,"lowFree":0,"swapTotal":0,"swapFree":0,"mapped":0,"vmallocTotal":0,"vmallocUsed":0,"vmallocChunk":0,"hugePagesTotal":0,"hugePagesFree":0,"hugePagesRsvd":0,"hugePagesSurp":0,"hugePageSize":0}`
e := `{"total":10,"available":20,"used":30,"usedPercent":30.1,"free":40,"active":0,"inactive":0,"wired":0,"laundry":0,"buffers":0,"cached":0,"writeBack":0,"dirty":0,"writeBackTmp":0,"shared":0,"slab":0,"sreclaimable":0,"sunreclaim":0,"pageTables":0,"swapCached":0,"commitLimit":0,"committedAS":0,"highTotal":0,"highFree":0,"lowTotal":0,"lowFree":0,"swapTotal":0,"swapFree":0,"mapped":0,"vmallocTotal":0,"vmallocUsed":0,"vmallocChunk":0,"hugePagesTotal":0,"hugePagesFree":0,"hugePagesRsvd":0,"hugePagesSurp":0,"hugePageSize":0,"anonHugePages":0}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("VirtualMemoryStat string is invalid: %v", v)
}
Expand Down
4 changes: 4 additions & 0 deletions mem/testdata/linux/virtualmemory/anonhugepages/proc/meminfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MemTotal: 260799420 kB
MemFree: 119443248 kB
MemAvailable: 127880216 kB
AnonHugePages: 50409472 kB

0 comments on commit 7f4efa5

Please sign in to comment.