Skip to content

Commit

Permalink
Metricbeat: Support huge pages on Linux (elastic#6351)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano committed Mar 26, 2018
1 parent 1a61b26 commit 02fff19
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di
- Fix dealing with new process status codes in Linux kernel 4.14+. {pull}6306[6306]
- Add config option for windows/perfmon metricset to ignore non existent counters. {pull}6432[6432]
- Support apache status pages for versions older than 2.4.16. {pull}6450[6450]
- Add support for huge pages on Linux. {pull}6436[6436]

*Packetbeat*

Expand Down
32 changes: 32 additions & 0 deletions libbeat/metric/system/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,35 @@ func AddSwapPercentage(s *SwapStat) {
perc := float64(s.Swap.Used) / float64(s.Swap.Total)
s.UsedPercent = common.Round(perc, common.DefaultDecimalPlacesCount)
}

// HugeTLBPagesStat includes metrics about huge pages usage
type HugeTLBPagesStat struct {
sigar.HugeTLBPages
UsedPercent float64 `json:"used_p"`
}

// GetHugeTLBPages returns huge pages usage metrics
func GetHugeTLBPages() (*HugeTLBPagesStat, error) {
pages := sigar.HugeTLBPages{}
err := pages.Get()

if err == nil {
return &HugeTLBPagesStat{HugeTLBPages: pages}, nil
}

if sigar.IsNotImplemented(err) {
return nil, nil
}

return nil, err
}

// AddHugeTLBPagesPercentage calculates ratio of used huge pages
func AddHugeTLBPagesPercentage(s *HugeTLBPagesStat) {
if s.Total == 0 {
return
}

perc := float64(s.Total-s.Free+s.Reserved) / float64(s.Total)
s.UsedPercent = common.Round(perc, common.DefaultDecimalPlacesCount)
}
76 changes: 76 additions & 0 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -10882,6 +10882,82 @@ format: percent
The percentage of used swap memory.
[float]
== hugepages fields
This group contains statistics related to huge pages usage on the system.
[float]
=== `system.memory.hugepages.total`
type: long
format: number
Number of huge pages in the pool.
[float]
=== `system.memory.hugepages.used.bytes`
type: long
format: bytes
Memory used in allocated huge pages.
[float]
=== `system.memory.hugepages.used.pct`
type: long
format: percent
Percentage of huge pages used.
[float]
=== `system.memory.hugepages.free`
type: long
format: number
Number of available huge pages in the pool.
[float]
=== `system.memory.hugepages.reserved`
type: long
format: number
Number of reserved but not allocated huge pages in the pool.
[float]
=== `system.memory.hugepages.surplus`
type: long
format: number
Number of overcommited huge pages.
[float]
=== `system.memory.hugepages.default_size`
type: long
format: bytes
Default size for huge pages.
[float]
== network fields
Expand Down
47 changes: 47 additions & 0 deletions metricbeat/module/system/memory/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,50 @@
format: percent
description: >
The percentage of used swap memory.
- name: hugepages
type: group
prefix: "[float]"
description: This group contains statistics related to huge pages usage on the system.
fields:
- name: total
type: long
format: number
description: >
Number of huge pages in the pool.
- name: used.bytes
type: long
format: bytes
description: >
Memory used in allocated huge pages.
- name: used.pct
type: long
format: percent
description: >
Percentage of huge pages used.
- name: free
type: long
format: number
description: >
Number of available huge pages in the pool.
- name: reserved
type: long
format: number
description: >
Number of reserved but not allocated huge pages in the pool.
- name: surplus
type: long
format: number
description: >
Number of overcommited huge pages.
- name: default_size
type: long
format: bytes
description: >
Default size for huge pages.
21 changes: 20 additions & 1 deletion metricbeat/module/system/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,26 @@ func (m *MetricSet) Fetch() (event common.MapStr, err error) {
},
"free": swapStat.Free,
}

memory["swap"] = swap

hugePagesStat, err := mem.GetHugeTLBPages()
if err != nil {
return nil, errors.Wrap(err, "hugepages")
}
if hugePagesStat != nil {
mem.AddHugeTLBPagesPercentage(hugePagesStat)
memory["hugepages"] = common.MapStr{
"total": hugePagesStat.Total,
"used": common.MapStr{
"bytes": hugePagesStat.TotalAllocatedSize,
"pct": hugePagesStat.UsedPercent,
},
"free": hugePagesStat.Free,
"reserved": hugePagesStat.Reserved,
"surplus": hugePagesStat.Surplus,
"default_size": hugePagesStat.DefaultSize,
}
}

return memory, nil
}
5 changes: 4 additions & 1 deletion metricbeat/tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
SYSTEM_FSSTAT_FIELDS = ["count", "total_files", "total_size"]

SYSTEM_MEMORY_FIELDS = ["swap", "actual.free", "free", "total", "used.bytes", "used.pct", "actual.used.bytes",
"actual.used.pct"]
"actual.used.pct", "hugepages"]

SYSTEM_NETWORK_FIELDS = ["name", "out.bytes", "in.bytes", "out.packets",
"in.packets", "in.error", "out.error", "in.dropped", "out.dropped"]
Expand Down Expand Up @@ -288,6 +288,9 @@ def test_memory(self):
self.assert_fields_are_documented(evt)

memory = evt["system"]["memory"]
if not re.match("(?i)linux", sys.platform) and not "hugepages" in memory:
# Ensure presence of hugepages only in Linux
memory["hugepages"] = None
self.assertItemsEqual(self.de_dot(SYSTEM_MEMORY_FIELDS), memory.keys())

# Check that percentages are calculated.
Expand Down

0 comments on commit 02fff19

Please sign in to comment.