Commit 79f021f6 authored by shirou's avatar shirou

Merge pull request #158 from walles/walles/darwin-memorypercent

process: Implement MemoryPercent() for Darwin
parents 956f9274 70444b57
......@@ -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
}
......@@ -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)
......
......@@ -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)
......
......@@ -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)
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment