Fixed calculation of cpu usage percent in user and sys mode for pids

This commit is contained in:
Diptanu Choudhury 2016-06-10 04:06:32 +01:00
parent ef2d630ea4
commit 9056be6d86
2 changed files with 9 additions and 7 deletions

View file

@ -135,8 +135,10 @@ type ProcessState struct {
// nomadPid holds a pid and it's cpu percentage calculator
type nomadPid struct {
pid int
cpuStats *stats.CpuStats
pid int
cpuStatsTotal *stats.CpuStats
cpuStatsUser *stats.CpuStats
cpuStatsSys *stats.CpuStats
}
// SyslogServerState holds the address and islation information of a launched
@ -506,11 +508,11 @@ func (e *UniversalExecutor) pidStats() (map[string]*cstructs.ResourceUsage, erro
cs := &cstructs.CpuStats{}
if cpuStats, err := p.Times(); err == nil {
cs.SystemMode = cpuStats.System
cs.UserMode = cpuStats.User
cs.SystemMode = pid.cpuStatsSys.Percent(cpuStats.System)
cs.UserMode = pid.cpuStatsUser.Percent(cpuStats.User)
// calculate cpu usage percent
cs.Percent = pid.cpuStats.Percent(cpuStats.Total())
cs.Percent = pid.cpuStatsTotal.Percent(cpuStats.Total())
}
stats[strconv.Itoa(pid.pid)] = &cstructs.ResourceUsage{MemoryStats: ms, CpuStats: cs}
}
@ -739,7 +741,7 @@ func (e *UniversalExecutor) scanPids(parentPid int, allPids []ps.Process) ([]*no
}
res := make([]*nomadPid, 0, len(processFamily))
for pid := range processFamily {
res = append(res, &nomadPid{pid, stats.NewCpuStats()})
res = append(res, &nomadPid{pid, stats.NewCpuStats(), stats.NewCpuStats(), stats.NewCpuStats()})
}
return res, nil
}

View file

@ -271,7 +271,7 @@ func (e *UniversalExecutor) getAllPids() ([]*nomadPid, error) {
}
np := make([]*nomadPid, len(pids))
for idx, pid := range pids {
np[idx] = &nomadPid{pid, stats.NewCpuStats()}
np[idx] = &nomadPid{pid, stats.NewCpuStats(), stats.NewCpuStats(), stats.NewCpuStats()}
}
return np, nil
}