diff --git a/process/process.go b/process/process.go index cc88ce7d4..b87ac0bb8 100644 --- a/process/process.go +++ b/process/process.go @@ -43,6 +43,7 @@ type FilledProcess struct { Nice int32 CreateTime int64 OpenFdCount int32 + LastCpu int32 // status Name string diff --git a/process/process_linux.go b/process/process_linux.go index 330288686..a4d11de97 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -226,7 +226,7 @@ func NewProcess(pid int32) (*Process, error) { // Ppid returns Parent Process ID of the process. func (p *Process) Ppid() (int32, error) { - ppid, _, _, _, _, err := p.fillFromStat() + ppid, _, _, _, _, _, err := p.fillFromStat() if err != nil { return -1, err } @@ -262,7 +262,7 @@ func (p *Process) CmdlineSlice() ([]string, error) { // CreateTime returns created time of the process in seconds since the epoch, in UTC. func (p *Process) CreateTime() (int64, error) { - _, _, _, createTime, _, err := p.fillFromStat() + _, _, _, createTime, _, _, err := p.fillFromStat() if err != nil { return 0, err } @@ -329,7 +329,7 @@ func (p *Process) Terminal() (string, error) { // Nice returns a nice value (priority). // Notice: gopsutil can not set nice value. func (p *Process) Nice() (int32, error) { - _, _, _, _, nice, err := p.fillFromStat() + _, _, _, _, nice, _, err := p.fillFromStat() if err != nil { return 0, err } @@ -385,7 +385,7 @@ func (p *Process) Threads() (map[string]string, error) { // Times returns CPU times of the process. func (p *Process) Times() (*cpu.TimesStat, error) { - _, _, cpuTimes, _, _, err := p.fillFromStat() + _, _, cpuTimes, _, _, _, err := p.fillFromStat() if err != nil { return nil, err } @@ -859,12 +859,12 @@ func (p *Process) fillTermFromStat() (string, error) { return terminal, err } -func (p *Process) fillFromStat() (int32, int32, *cpu.TimesStat, int64, int32, error) { +func (p *Process) fillFromStat() (int32, int32, *cpu.TimesStat, int64, int32, int32, error) { pid := p.Pid statPath := common.HostProc(strconv.Itoa(int(pid)), "stat") contents, err := ioutil.ReadFile(statPath) if err != nil { - return 0, 0, nil, 0, 0, err + return 0, 0, nil, 0, 0, 0, err } fields := strings.Fields(string(contents)) timestamp := time.Now().Unix() @@ -876,20 +876,20 @@ func (p *Process) fillFromStat() (int32, int32, *cpu.TimesStat, int64, int32, er ppid, err := strconv.ParseInt(fields[i+2], 10, 32) if err != nil { - return 0, 0, nil, 0, 0, err + return 0, 0, nil, 0, 0, 0, err } pgrp, err := strconv.ParseInt(fields[i+3], 10, 32) if err != nil { - return 0, 0, nil, 0, 0, err + return 0, 0, nil, 0, 0, 0, err } utime, err := strconv.ParseFloat(fields[i+12], 64) if err != nil { - return 0, 0, nil, 0, 0, err + return 0, 0, nil, 0, 0, 0, err } stime, err := strconv.ParseFloat(fields[i+13], 64) if err != nil { - return 0, 0, nil, 0, 0, err + return 0, 0, nil, 0, 0, 0, err } cpuTimes := &cpu.TimesStat{ @@ -906,7 +906,7 @@ func (p *Process) fillFromStat() (int32, int32, *cpu.TimesStat, int64, int32, er bootTime := CachedBootTime t, err := strconv.ParseUint(fields[i+20], 10, 64) if err != nil { - return 0, 0, nil, 0, 0, err + return 0, 0, nil, 0, 0, 0, err } ctime := (t / uint64(ClockTicks)) + uint64(bootTime) createTime := int64(ctime * 1000) @@ -916,7 +916,12 @@ func (p *Process) fillFromStat() (int32, int32, *cpu.TimesStat, int64, int32, er snice, _ := syscall.Getpriority(PrioProcess, int(pid)) nice := int32(snice) // FIXME: is this true? - return int32(ppid), int32(pgrp), cpuTimes, createTime, nice, nil + processor, err := strconv.ParseInt(fields[i+37], 10, 32) + if err != nil { + return 0, 0, nil, 0, 0, 0, err + } + + return int32(ppid), int32(pgrp), cpuTimes, createTime, nice, int32(processor), nil } // Pids returns a slice of process ID list which are running now. @@ -985,7 +990,7 @@ func AllProcesses() (map[int32]*FilledProcess, error) { log.Debugf("Unable to access /proc/%d/io: %s", pid, err) ioStat = &IOCountersStat{} } - ppid, _, t1, createTime, nice, err := p.fillFromStat() + ppid, _, t1, createTime, nice, processor, err := p.fillFromStat() if err != nil { log.Debugf("Unable to fill from /proc/%d/stat: %s", pid, err) t1 = &cpu.TimesStat{} @@ -1029,6 +1034,7 @@ func AllProcesses() (map[int32]*FilledProcess, error) { Nice: nice, CreateTime: createTime, OpenFdCount: openFdCount, + LastCpu: processor, // status Name: p.name,