fix(inputs.procstat): Collect swap via /proc/$pid/smaps (#13779)

This commit is contained in:
Joshua Powers 2023-09-08 03:41:48 -06:00 committed by GitHub
parent dcadf44bf9
commit 86a546c88d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 7 deletions

View File

@ -111,10 +111,15 @@ the `win_perf_counters` input plugin as a more mature alternative.
- cpu_usage (float)
- involuntary_context_switches (int)
- major_faults (int)
- memory_data (int)
- memory_locked (int)
- memory_anonymous (int)
- memory_private_clean (int)
- memory_private_dirty (int)
- memory_pss (int)
- memory_referenced (int)
- memory_rss (int)
- memory_stack (int)
- memory_shared_clean (int)
- memory_shared_dirty (int)
- memory_size (int)
- memory_swap (int)
- memory_usage (float)
- memory_vms (int)

View File

@ -0,0 +1,19 @@
//go:build linux
package procstat
func collectMemmap(proc Process, prefix string, fields map[string]any) {
memMapStats, err := proc.MemoryMaps(true)
if err == nil && len(*memMapStats) == 1 {
memMap := (*memMapStats)[0]
fields[prefix+"memory_size"] = memMap.Size
fields[prefix+"memory_pss"] = memMap.Pss
fields[prefix+"memory_shared_clean"] = memMap.SharedClean
fields[prefix+"memory_shared_dirty"] = memMap.SharedDirty
fields[prefix+"memory_private_clean"] = memMap.PrivateClean
fields[prefix+"memory_private_dirty"] = memMap.PrivateDirty
fields[prefix+"memory_referenced"] = memMap.Referenced
fields[prefix+"memory_anonymous"] = memMap.Anonymous
fields[prefix+"memory_swap"] = memMap.Swap
}
}

View File

@ -0,0 +1,5 @@
//go:build !linux
package procstat
func collectMemmap(Process, string, map[string]any) {}

View File

@ -16,6 +16,7 @@ type Process interface {
PageFaults() (*process.PageFaultsStat, error)
IOCounters() (*process.IOCountersStat, error)
MemoryInfo() (*process.MemoryInfoStat, error)
MemoryMaps(bool) (*[]process.MemoryMapsStat, error)
Name() (string, error)
Cmdline() (string, error)
NumCtxSwitches() (*process.NumCtxSwitchesStat, error)

View File

@ -225,16 +225,15 @@ func (p *Procstat) addMetric(proc Process, acc telegraf.Accumulator, t time.Time
}
}
// This only returns values for RSS and VMS
mem, err := proc.MemoryInfo()
if err == nil {
fields[prefix+"memory_rss"] = mem.RSS
fields[prefix+"memory_vms"] = mem.VMS
fields[prefix+"memory_swap"] = mem.Swap
fields[prefix+"memory_data"] = mem.Data
fields[prefix+"memory_stack"] = mem.Stack
fields[prefix+"memory_locked"] = mem.Locked
}
collectMemmap(proc, prefix, fields)
memPerc, err := proc.MemoryPercent()
if err == nil {
fields[prefix+"memory_usage"] = memPerc

View File

@ -129,6 +129,10 @@ func (p *testProc) MemoryInfo() (*process.MemoryInfoStat, error) {
return &process.MemoryInfoStat{}, nil
}
func (p *testProc) MemoryMaps(bool) (*[]process.MemoryMapsStat, error) {
return &[]process.MemoryMapsStat{}, nil
}
func (p *testProc) Name() (string, error) {
return "test_proc", nil
}