Skip to content

Commit 31f7689

Browse files
Cherry-pick #26334 to 7.x: Refactor of system/memory metricset (#26589)
* Refactor of system/memory metricset (#26334) * init commit * start on linux implementation * finish linux, start work on darwin * fix build platform issues * fix metrics on darwin * add openbsd * add freebsd * add windows, aix * fix aix build * finish memory * fix up opt changes * cleanup metricset code * fix up folder methods * fix calculations, Opt API, gomod * make notice * go mod tidy, mage fmt * fix up linux/memory * update fields * update system tests * fix system tests, again * fix extra print statements * fix if block in fillPercentages * vix Value API * fix up tests, opt (cherry picked from commit e008024) * remove libbeat mem * fix process
1 parent b10ba67 commit 31f7689

File tree

28 files changed

+1511
-640
lines changed

28 files changed

+1511
-640
lines changed

NOTICE.txt

Lines changed: 212 additions & 212 deletions
Large diffs are not rendered by default.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ require (
7373
github.com/elastic/go-sysinfo v1.7.0
7474
github.com/elastic/go-txfile v0.0.7
7575
github.com/elastic/go-ucfg v0.8.3
76-
github.com/elastic/go-windows v1.0.1 // indirect
76+
github.com/elastic/go-windows v1.0.1
7777
github.com/elastic/gosigar v0.14.1
7878
github.com/fatih/color v1.9.0
7979
github.com/fsnotify/fsevents v0.1.1

libbeat/metric/system/memory/memory.go

Lines changed: 0 additions & 171 deletions
This file was deleted.

libbeat/metric/system/memory/memory_test.go

Lines changed: 0 additions & 96 deletions
This file was deleted.

libbeat/metric/system/process/process.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
"github.com/elastic/beats/v7/libbeat/common"
3333
"github.com/elastic/beats/v7/libbeat/common/match"
3434
"github.com/elastic/beats/v7/libbeat/logp"
35-
"github.com/elastic/beats/v7/libbeat/metric/system/memory"
35+
sysinfo "github.com/elastic/go-sysinfo"
3636
sigar "github.com/elastic/gosigar"
3737
"github.com/elastic/gosigar/cgroup"
3838
)
@@ -234,22 +234,19 @@ func getProcEnv(pid int, out common.MapStr, filter func(v string) bool) error {
234234
return nil
235235
}
236236

237+
// GetProcMemPercentage returns process memory usage as a percent of total memory usage
237238
func GetProcMemPercentage(proc *Process, totalPhyMem uint64) float64 {
238239
// in unit tests, total_phymem is set to a value greater than zero
239240
if totalPhyMem == 0 {
240-
memStat, err := memory.Get()
241-
if err != nil {
242-
logp.Warn("Getting memory details: %v", err)
243-
return 0
244-
}
245-
totalPhyMem = memStat.Mem.Total
241+
return 0
246242
}
247243

248244
perc := (float64(proc.Mem.Resident) / float64(totalPhyMem))
249245

250246
return common.Round(perc, 4)
251247
}
252248

249+
// Pids returns a list of PIDs
253250
func Pids() ([]int, error) {
254251
pids := sigar.ProcList{}
255252
err := pids.Get()
@@ -290,6 +287,22 @@ func GetOwnResourceUsageTimeInMillis() (int64, int64, error) {
290287
}
291288

292289
func (procStats *Stats) getProcessEvent(process *Process) common.MapStr {
290+
// This is a holdover until we migrate this library to metricbeat/internal
291+
// At which point we'll use the memory code there.
292+
var totalPhyMem uint64
293+
host, err := sysinfo.Host()
294+
if err != nil {
295+
logp.Warn("Getting host details: %v", err)
296+
} else {
297+
memStats, err := host.Memory()
298+
if err != nil {
299+
logp.Warn("Getting memory details: %v", err)
300+
} else {
301+
totalPhyMem = memStats.Total
302+
}
303+
304+
}
305+
293306
proc := common.MapStr{
294307
"pid": process.Pid,
295308
"ppid": process.Ppid,
@@ -301,7 +314,7 @@ func (procStats *Stats) getProcessEvent(process *Process) common.MapStr {
301314
"size": process.Mem.Size,
302315
"rss": common.MapStr{
303316
"bytes": process.Mem.Resident,
304-
"pct": GetProcMemPercentage(process, 0 /* read total mem usage */),
317+
"pct": GetProcMemPercentage(process, totalPhyMem),
305318
},
306319
"share": process.Mem.Share,
307320
},

metricbeat/docs/fields.asciidoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44499,6 +44499,18 @@ format: bytes
4449944499
The total amount of free memory in bytes. This value does not include memory consumed by system caches and buffers (see system.memory.actual.free).
4450044500

4450144501

44502+
type: long
44503+
44504+
format: bytes
44505+
44506+
--
44507+
44508+
*`system.memory.cached`*::
44509+
+
44510+
--
44511+
Total Cached memory on system.
44512+
44513+
4450244514
type: long
4450344515

4450444516
format: bytes

0 commit comments

Comments
 (0)