From 70444b571b92fc376c54d238341058b7f905cf10 Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Tue, 16 Feb 2016 19:15:41 +0100 Subject: [PATCH] process: Implement platform independent MemoryPercent() It calls mem.VirtualMemory() and process.MemoryInfo(), so on the platforms where those work MemoryPercent() will as well. Tested on Darwin. --- process/process.go | 18 ++++++++++++++++++ process/process_darwin.go | 3 --- process/process_freebsd.go | 3 --- process/process_linux.go | 3 --- process/process_windows.go | 3 --- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/process/process.go b/process/process.go index 4f63a2a..6f6d9d7 100644 --- a/process/process.go +++ b/process/process.go @@ -7,6 +7,7 @@ import ( "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/internal/common" + "github.com/shirou/gopsutil/mem" ) var invoke common.Invoker @@ -145,3 +146,20 @@ func calculatePercent(t1, t2 *cpu.CPUTimesStat, delta float64, numcpu int) float overall_percent := ((delta_proc / delta) * 100) * float64(numcpu) return overall_percent } + +// MemoryPercent returns how many percent of the total RAM this process uses +func (p *Process) MemoryPercent() (float32, error) { + machineMemory, err := mem.VirtualMemory() + if err != nil { + return 0, err + } + total := machineMemory.Total + + processMemory, err := p.MemoryInfo() + if err != nil { + return 0, err + } + used := processMemory.RSS + + return (100 * float32(used) / float32(total)), nil +} diff --git a/process/process_darwin.go b/process/process_darwin.go index a09650d..414ea98 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -274,9 +274,6 @@ func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { return nil, common.NotImplementedError } -func (p *Process) MemoryPercent() (float32, error) { - return 0, common.NotImplementedError -} func (p *Process) Children() ([]*Process, error) { pids, err := common.CallPgrep(invoke, p.Pid) diff --git a/process/process_freebsd.go b/process/process_freebsd.go index bd5ff6b..9835bc9 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -195,9 +195,6 @@ func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { return nil, common.NotImplementedError } -func (p *Process) MemoryPercent() (float32, error) { - return 0, common.NotImplementedError -} func (p *Process) Children() ([]*Process, error) { pids, err := common.CallPgrep(invoke, p.Pid) diff --git a/process/process_linux.go b/process/process_linux.go index 3fa6b7c..05a75c9 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -201,9 +201,6 @@ func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { } return memInfoEx, nil } -func (p *Process) MemoryPercent() (float32, error) { - return 0, common.NotImplementedError -} func (p *Process) Children() ([]*Process, error) { pids, err := common.CallPgrep(invoke, p.Pid) diff --git a/process/process_windows.go b/process/process_windows.go index 987a20a..51a0ee1 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -227,9 +227,6 @@ func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { return nil, common.NotImplementedError } -func (p *Process) MemoryPercent() (float32, error) { - return 0, common.NotImplementedError -} func (p *Process) Children() ([]*Process, error) { return nil, common.NotImplementedError