Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PROC-1539] Export last CPU id used by a process #41

Open
wants to merge 1 commit into
base: dd
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type FilledProcess struct {
Nice int32
CreateTime int64
OpenFdCount int32
LastCpu int32

// status
Name string
Expand Down
32 changes: 19 additions & 13 deletions process/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -1029,6 +1034,7 @@ func AllProcesses() (map[int32]*FilledProcess, error) {
Nice: nice,
CreateTime: createTime,
OpenFdCount: openFdCount,
LastCpu: processor,

// status
Name: p.name,
Expand Down