Implemented cpu stats

This commit is contained in:
Diptanu Choudhury 2016-05-19 13:32:03 -07:00
parent 1789dbd1ff
commit e37a108b4c
4 changed files with 69 additions and 3 deletions

View File

@ -990,7 +990,21 @@ func (h *DockerHandle) monitorUsage() {
Swap: s.MemoryStats.Stats.Swap,
MaxUsage: s.MemoryStats.MaxUsage,
}
h.resourceUsage = &cstructs.TaskResourceUsage{MemoryStats: ms, Timestamp: s.Read}
cs := &cstructs.CpuUsage{
SystemMode: float64(s.CPUStats.CPUUsage.UsageInKernelmode),
UserMode: float64(s.CPUStats.CPUUsage.UsageInKernelmode),
ThrottledPeriods: s.CPUStats.ThrottlingData.ThrottledPeriods,
ThrottledTime: s.CPUStats.ThrottlingData.ThrottledTime,
}
// Calculate percentage
cs.Percent = 0.0
cpuDelta := float64(s.CPUStats.CPUUsage.TotalUsage) - float64(s.PreCPUStats.CPUUsage.TotalUsage)
systemDelta := float64(s.CPUStats.SystemCPUUsage) - float64(s.PreCPUStats.SystemCPUUsage)
if cpuDelta > 0.0 && systemDelta > 0.0 {
cs.Percent = (cpuDelta / systemDelta) * float64(len(s.CPUStats.CPUUsage.PercpuUsage)) * 100.0
}
h.resourceUsage = &cstructs.TaskResourceUsage{MemoryStats: ms, CpuStats: cs, Timestamp: s.Read}
}
case <-h.doneMonitoring:
case <-h.doneCh:

View File

@ -24,6 +24,7 @@ import (
"github.com/hashicorp/nomad/client/driver/env"
"github.com/hashicorp/nomad/client/driver/logging"
cstructs "github.com/hashicorp/nomad/client/driver/structs"
"github.com/hashicorp/nomad/client/stats"
"github.com/hashicorp/nomad/nomad/structs"
)
@ -171,15 +172,22 @@ type UniversalExecutor struct {
consulService *consul.ConsulService
consulCtx *ConsulContext
cpuStats *stats.CpuStats
logger *log.Logger
}
// NewExecutor returns an Executor
func NewExecutor(logger *log.Logger) Executor {
return &UniversalExecutor{
exec := &UniversalExecutor{
logger: logger,
processExited: make(chan interface{}),
}
if cpuStats, err := stats.NewCpuStats(); err == nil {
exec.cpuStats = cpuStats
}
return exec
}
// Version returns the api version of the executor
@ -676,7 +684,6 @@ func (e *UniversalExecutor) collectPids() {
select {
case <-timer.C:
pids, err := e.getAllPids()
e.logger.Printf("DIPTANU PIDS %#v", pids)
timer.Reset(pidScanInterval)
if err != nil {
e.logger.Printf("[DEBUG] executor: error collecting pids: %v", err)

View File

@ -150,6 +150,7 @@ func (e *UniversalExecutor) Stats() (*cstructs.TaskResourceUsage, error) {
}
// CPU Related Stats
totalProcessCPUUsage := stats.CpuStats.CpuUsage.TotalUsage
userModeTime := stats.CpuStats.CpuUsage.UsageInUsermode
kernelModeTime := stats.CpuStats.CpuUsage.UsageInKernelmode
@ -162,6 +163,9 @@ func (e *UniversalExecutor) Stats() (*cstructs.TaskResourceUsage, error) {
ThrottledPeriods: stats.CpuStats.ThrottlingData.ThrottledPeriods,
ThrottledTime: stats.CpuStats.ThrottlingData.ThrottledTime,
}
if e.cpuStats != nil {
cs.Percent = e.cpuStats.Percent(totalProcessCPUUsage)
}
return &cstructs.TaskResourceUsage{MemoryStats: ms, CpuStats: cs, Timestamp: time.Now()}, nil
}

41
client/stats/cpu.go Normal file
View File

@ -0,0 +1,41 @@
package stats
import (
"github.com/shirou/gopsutil/cpu"
)
type CpuStats struct {
prevSystemUsage float64
prevProcessUsage uint64
totalCpus int
}
func NewCpuStats() (*CpuStats, error) {
cpuInfo, err := cpu.Info()
if err != nil {
return nil, err
}
return &CpuStats{totalCpus: len(cpuInfo)}, nil
}
func (c *CpuStats) Percent(currentProcessUsage uint64) float64 {
percent := 0.0
sysCPUStats, err := cpu.Times(false)
if err != nil {
return 0
}
currentSysUsage := 0.0
for _, cpuStat := range sysCPUStats {
currentSysUsage += cpuStat.Total() * 1000000000
}
delta := float64(currentProcessUsage) - float64(c.prevProcessUsage)
sysDelta := float64(currentSysUsage) - float64(c.prevSystemUsage)
percent = (delta / sysDelta) * float64(c.totalCpus) * 100.0
c.prevSystemUsage = currentSysUsage
c.prevProcessUsage = currentProcessUsage
return percent
}