mirror of
https://github.com/shirou/gopsutil.git
synced 2025-05-08 19:29:25 +08:00
Merge pull request #158 from walles/walles/darwin-memorypercent
process: Implement MemoryPercent() for Darwin
This commit is contained in:
commit
79f021f66e
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user