Skip to content

Commit

Permalink
Cherry-pick #26334 to 7.x: Refactor of system/memory metricset (#26589)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fearful-symmetry authored Jun 30, 2021
1 parent b10ba67 commit 31f7689
Show file tree
Hide file tree
Showing 28 changed files with 1,511 additions and 640 deletions.
424 changes: 212 additions & 212 deletions NOTICE.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ require (
github.com/elastic/go-sysinfo v1.7.0
github.com/elastic/go-txfile v0.0.7
github.com/elastic/go-ucfg v0.8.3
github.com/elastic/go-windows v1.0.1 // indirect
github.com/elastic/go-windows v1.0.1
github.com/elastic/gosigar v0.14.1
github.com/fatih/color v1.9.0
github.com/fsnotify/fsevents v0.1.1
Expand Down
171 changes: 0 additions & 171 deletions libbeat/metric/system/memory/memory.go

This file was deleted.

96 changes: 0 additions & 96 deletions libbeat/metric/system/memory/memory_test.go

This file was deleted.

29 changes: 21 additions & 8 deletions libbeat/metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/match"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/metric/system/memory"
sysinfo "github.com/elastic/go-sysinfo"
sigar "github.com/elastic/gosigar"
"github.com/elastic/gosigar/cgroup"
)
Expand Down Expand Up @@ -234,22 +234,19 @@ func getProcEnv(pid int, out common.MapStr, filter func(v string) bool) error {
return nil
}

// GetProcMemPercentage returns process memory usage as a percent of total memory usage
func GetProcMemPercentage(proc *Process, totalPhyMem uint64) float64 {
// in unit tests, total_phymem is set to a value greater than zero
if totalPhyMem == 0 {
memStat, err := memory.Get()
if err != nil {
logp.Warn("Getting memory details: %v", err)
return 0
}
totalPhyMem = memStat.Mem.Total
return 0
}

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

return common.Round(perc, 4)
}

// Pids returns a list of PIDs
func Pids() ([]int, error) {
pids := sigar.ProcList{}
err := pids.Get()
Expand Down Expand Up @@ -290,6 +287,22 @@ func GetOwnResourceUsageTimeInMillis() (int64, int64, error) {
}

func (procStats *Stats) getProcessEvent(process *Process) common.MapStr {
// This is a holdover until we migrate this library to metricbeat/internal
// At which point we'll use the memory code there.
var totalPhyMem uint64
host, err := sysinfo.Host()
if err != nil {
logp.Warn("Getting host details: %v", err)
} else {
memStats, err := host.Memory()
if err != nil {
logp.Warn("Getting memory details: %v", err)
} else {
totalPhyMem = memStats.Total
}

}

proc := common.MapStr{
"pid": process.Pid,
"ppid": process.Ppid,
Expand All @@ -301,7 +314,7 @@ func (procStats *Stats) getProcessEvent(process *Process) common.MapStr {
"size": process.Mem.Size,
"rss": common.MapStr{
"bytes": process.Mem.Resident,
"pct": GetProcMemPercentage(process, 0 /* read total mem usage */),
"pct": GetProcMemPercentage(process, totalPhyMem),
},
"share": process.Mem.Share,
},
Expand Down
12 changes: 12 additions & 0 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -44499,6 +44499,18 @@ format: bytes
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).


type: long

format: bytes

--

*`system.memory.cached`*::
+
--
Total Cached memory on system.


type: long

format: bytes
Expand Down
Loading

0 comments on commit 31f7689

Please sign in to comment.