From 2c5233814a30825abfd731ef2e39a802b68b11aa Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Wed, 25 May 2016 17:46:24 -0700 Subject: [PATCH] simplified the stats method in basic executor --- client/driver/executor/executor.go | 48 +----------------------- client/driver/executor/executor_basic.go | 42 ++++++++++++++++++++- 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/client/driver/executor/executor.go b/client/driver/executor/executor.go index d0ffff68b..5beaa9514 100644 --- a/client/driver/executor/executor.go +++ b/client/driver/executor/executor.go @@ -717,11 +717,10 @@ func (e *UniversalExecutor) scanPids(parentPid int, allPids []ps.Process) ([]*no foundNewPid := false for _, pid := range allPids { - _, parentPid := processFamily[pid.Pid()] _, childPid := processFamily[pid.PPid()] // checking if the pid is a child of any of the parents - if parentPid || childPid { + if childPid { processFamily[pid.Pid()] = struct{}{} foundNewPid = true } else { @@ -743,48 +742,3 @@ func (e *UniversalExecutor) scanPids(parentPid int, allPids []ps.Process) ([]*no } return res, nil } - -// resourceUsagePids aggregates the resources used by all the pids that are -// spawned by the executor and the user process. -func (e *UniversalExecutor) resourceUsagePids() (*cstructs.TaskResourceUsage, error) { - ts := time.Now() - pidStats, err := e.pidStats() - if err != nil { - return nil, err - } - var ( - systemModeCPU, userModeCPU, percent float64 - totalRSS, totalSwap uint64 - ) - - for _, pidStat := range pidStats { - systemModeCPU += pidStat.CpuStats.SystemMode - userModeCPU += pidStat.CpuStats.UserMode - percent += pidStat.CpuStats.Percent - - totalRSS += pidStat.MemoryStats.RSS - totalSwap += pidStat.MemoryStats.Swap - } - - totalCPU := &cstructs.CpuStats{ - SystemMode: systemModeCPU, - UserMode: userModeCPU, - Percent: percent, - } - - totalMemory := &cstructs.MemoryStats{ - RSS: totalRSS, - Swap: totalSwap, - } - - resourceUsage := cstructs.ResourceUsage{ - MemoryStats: totalMemory, - CpuStats: totalCPU, - Timestamp: ts, - } - return &cstructs.TaskResourceUsage{ - ResourceUsage: &resourceUsage, - Timestamp: ts, - Pids: pidStats, - }, nil -} diff --git a/client/driver/executor/executor_basic.go b/client/driver/executor/executor_basic.go index eb07a406d..a21bb9c64 100644 --- a/client/driver/executor/executor_basic.go +++ b/client/driver/executor/executor_basic.go @@ -4,6 +4,7 @@ package executor import ( "os" + "time" cstructs "github.com/hashicorp/nomad/client/driver/structs" @@ -36,7 +37,46 @@ func (e *UniversalExecutor) configureIsolation() error { } func (e *UniversalExecutor) Stats() (*cstructs.TaskResourceUsage, error) { - return e.resourceUsagePids() + ts := time.Now() + pidStats, err := e.pidStats() + if err != nil { + return nil, err + } + var ( + systemModeCPU, userModeCPU, percent float64 + totalRSS, totalSwap uint64 + ) + + for _, pidStat := range pidStats { + systemModeCPU += pidStat.CpuStats.SystemMode + userModeCPU += pidStat.CpuStats.UserMode + percent += pidStat.CpuStats.Percent + + totalRSS += pidStat.MemoryStats.RSS + totalSwap += pidStat.MemoryStats.Swap + } + + totalCPU := &cstructs.CpuStats{ + SystemMode: systemModeCPU, + UserMode: userModeCPU, + Percent: percent, + } + + totalMemory := &cstructs.MemoryStats{ + RSS: totalRSS, + Swap: totalSwap, + } + + resourceUsage := cstructs.ResourceUsage{ + MemoryStats: totalMemory, + CpuStats: totalCPU, + Timestamp: ts, + } + return &cstructs.TaskResourceUsage{ + ResourceUsage: &resourceUsage, + Timestamp: ts, + Pids: pidStats, + }, nil } func (e *UniversalExecutor) getAllPids() ([]*nomadPid, error) {