From a3bbd9e3cdb1e1c7e757b395e6e806fc4353e442 Mon Sep 17 00:00:00 2001 From: Chris Gilling Date: Sat, 27 Feb 2016 18:20:14 -0800 Subject: [PATCH] process: change linux NewProcess to only stat /proc/[pid] Before it was doing a fillFromStatus() call which was much slower and none of the information was needed, except by the Name() func which now will call fillFromStatus() if p.name is not set. --- process/process_linux.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/process/process_linux.go b/process/process_linux.go index 1783d2f..a160e4d 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -61,13 +61,15 @@ func (m MemoryMapsStat) String() string { return string(s) } -// Create new Process instance -// This only stores Pid +// NewProcess creates a new Process instance, it only stores the pid and +// checks that the process exists. Other method on Process can be used +// to get more information about the process. An error will be returned +// if the process does not exist. func NewProcess(pid int32) (*Process, error) { p := &Process{ Pid: int32(pid), } - err := p.fillFromStatus() + _, err := os.Open(common.HostProc(strconv.Itoa(int(p.Pid)))) return p, err } @@ -79,6 +81,11 @@ func (p *Process) Ppid() (int32, error) { return ppid, nil } func (p *Process) Name() (string, error) { + if p.name == "" { + if err := p.fillFromStatus(); err != nil { + return "", err + } + } return p.name, nil } func (p *Process) Exe() (string, error) {