From 412997f5668c23262161fe7c3a33b90a6d0ad3a5 Mon Sep 17 00:00:00 2001 From: Lang Martin Date: Wed, 17 Jul 2019 17:28:40 -0400 Subject: [PATCH 1/6] pid_collector getAllPids -> getAllPidsByScanning --- drivers/shared/executor/pid_collector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/shared/executor/pid_collector.go b/drivers/shared/executor/pid_collector.go index 3652cf93eb8..20d4dca8ad3 100644 --- a/drivers/shared/executor/pid_collector.go +++ b/drivers/shared/executor/pid_collector.go @@ -210,7 +210,7 @@ func aggregatedResourceUsage(systemCpuStats *stats.CpuStats, pidStats map[string } } -func getAllPids() (map[int]*nomadPid, error) { +func getAllPidsByScanning() (map[int]*nomadPid, error) { allProcesses, err := ps.Processes() if err != nil { return nil, err From d3ef456bd7da6e003589be4fffcd04111e3116a8 Mon Sep 17 00:00:00 2001 From: Lang Martin Date: Tue, 9 Jul 2019 18:47:51 -0400 Subject: [PATCH 2/6] resource_container_linux new getAllPidsByCgroup --- .../executor/resource_container_linux.go | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/drivers/shared/executor/resource_container_linux.go b/drivers/shared/executor/resource_container_linux.go index 5a414c9cf0a..50ab7cef9e6 100644 --- a/drivers/shared/executor/resource_container_linux.go +++ b/drivers/shared/executor/resource_container_linux.go @@ -4,6 +4,8 @@ import ( "os" "sync" + "github.com/hashicorp/nomad/client/stats" + "github.com/opencontainers/runc/libcontainer/cgroups" cgroupConfig "github.com/opencontainers/runc/libcontainer/configs" ) @@ -23,3 +25,38 @@ func (rc *resourceContainerContext) executorCleanup() error { } return nil } + +func (rc *resourceContainerContext) isEmpty() bool { + return rc.groups == nil +} + +func (rc *resourceContainerContext) getAllPidsByCgroup() (map[int]*nomadPid, error) { + nPids := map[int]*nomadPid{} + + if rc.groups == nil { + return nPids, nil + } + + var path string + if p, ok := rc.groups.Paths["freezer"]; ok { + path = p + } else { + path = rc.groups.Path + } + + pids, err := cgroups.GetAllPids(path) + if err != nil { + return nPids, err + } + + for _, pid := range pids { + nPids[pid] = &nomadPid{ + pid: pid, + cpuStatsTotal: stats.NewCpuStats(), + cpuStatsUser: stats.NewCpuStats(), + cpuStatsSys: stats.NewCpuStats(), + } + } + + return nPids, nil +} From 3834616691d108e759dcde4868a7b3fde4b02cb3 Mon Sep 17 00:00:00 2001 From: Lang Martin Date: Tue, 9 Jul 2019 18:48:16 -0400 Subject: [PATCH 3/6] executor use e.getAllPids() --- drivers/shared/executor/executor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/shared/executor/executor.go b/drivers/shared/executor/executor.go index 7e625f34ddd..46ddc57f85a 100644 --- a/drivers/shared/executor/executor.go +++ b/drivers/shared/executor/executor.go @@ -312,7 +312,7 @@ func (e *UniversalExecutor) Launch(command *ExecCommand) (*ProcessState, error) return nil, fmt.Errorf("failed to start command path=%q --- args=%q: %v", path, e.childCmd.Args, err) } - go e.pidCollector.collectPids(e.processExited, getAllPids) + go e.pidCollector.collectPids(e.processExited, e.getAllPids) go e.wait() return &ProcessState{Pid: e.childCmd.Process.Pid, ExitCode: -1, Time: time.Now()}, nil } From 1a9c598fc2385531b5f1e92333d3484f7aaab0f2 Mon Sep 17 00:00:00 2001 From: Lang Martin Date: Wed, 10 Jul 2019 13:45:32 -0400 Subject: [PATCH 4/6] executor_universal_linux getAllPids chooses cgroup when available --- .../shared/executor/executor_universal_linux.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/drivers/shared/executor/executor_universal_linux.go b/drivers/shared/executor/executor_universal_linux.go index 30fdd9c69cc..f201b4f798b 100644 --- a/drivers/shared/executor/executor_universal_linux.go +++ b/drivers/shared/executor/executor_universal_linux.go @@ -75,11 +75,24 @@ func (e *UniversalExecutor) configureResourceContainer(pid int) error { }, } - configureBasicCgroups(cfg) + err := configureBasicCgroups(cfg) + if err != nil { + e.logger.Warn("failed to create cgroup", "error", err) + return err + } + e.resConCtx.groups = cfg.Cgroups return cgroups.EnterPid(cfg.Cgroups.Paths, pid) } +func (e *UniversalExecutor) getAllPids() (map[int]*nomadPid, error) { + if e.resConCtx.isEmpty() { + return getAllPidsByScanning() + } else { + return e.resConCtx.getAllPidsByCgroup() + } +} + // DestroyCgroup kills all processes in the cgroup and removes the cgroup // configuration from the host. This function is idempotent. func DestroyCgroup(groups *lconfigs.Cgroup, executorPid int) error { From ab3e6259d08db9c8be80d4b7ec96ba288146c7c5 Mon Sep 17 00:00:00 2001 From: Lang Martin Date: Wed, 17 Jul 2019 17:28:10 -0400 Subject: [PATCH 5/6] executor_unix and _windows stub getAllPids ByScanning --- drivers/shared/executor/executor_unix.go | 4 ++++ drivers/shared/executor/executor_windows.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/drivers/shared/executor/executor_unix.go b/drivers/shared/executor/executor_unix.go index 6f90b7ade4f..2506a49e0a9 100644 --- a/drivers/shared/executor/executor_unix.go +++ b/drivers/shared/executor/executor_unix.go @@ -43,3 +43,7 @@ func (e *UniversalExecutor) shutdownProcess(sig os.Signal, proc *os.Process) err return nil } + +func (e *UniversalExecutor) getAllPids() (map[int]*nomadPid, error) { + return getAllPidsByScanning() +} diff --git a/drivers/shared/executor/executor_windows.go b/drivers/shared/executor/executor_windows.go index 9d0b1407011..443ce25083d 100644 --- a/drivers/shared/executor/executor_windows.go +++ b/drivers/shared/executor/executor_windows.go @@ -67,3 +67,7 @@ func (e *UniversalExecutor) shutdownProcess(_ os.Signal, proc *os.Process) error return nil } + +func (e *UniversalExecutor) getAllPids() (map[int]*nomadPid, error) { + return getAllPidsByScanning() +} From 7bd881cbf71793f91b089508e01374429695d1a2 Mon Sep 17 00:00:00 2001 From: Lang Martin Date: Thu, 18 Jul 2019 10:57:27 -0400 Subject: [PATCH 6/6] default e.getAllPids in executor_basic --- drivers/shared/executor/executor_basic.go | 4 ++++ drivers/shared/executor/executor_unix.go | 4 ---- drivers/shared/executor/executor_windows.go | 4 ---- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/drivers/shared/executor/executor_basic.go b/drivers/shared/executor/executor_basic.go index e34c3682b5b..137891c5eb5 100644 --- a/drivers/shared/executor/executor_basic.go +++ b/drivers/shared/executor/executor_basic.go @@ -15,3 +15,7 @@ func NewExecutorWithIsolation(logger hclog.Logger) Executor { func (e *UniversalExecutor) configureResourceContainer(_ int) error { return nil } func (e *UniversalExecutor) runAs(_ string) error { return nil } + +func (e *UniversalExecutor) getAllPids() (map[int]*nomadPid, error) { + return getAllPidsByScanning() +} diff --git a/drivers/shared/executor/executor_unix.go b/drivers/shared/executor/executor_unix.go index 2506a49e0a9..6f90b7ade4f 100644 --- a/drivers/shared/executor/executor_unix.go +++ b/drivers/shared/executor/executor_unix.go @@ -43,7 +43,3 @@ func (e *UniversalExecutor) shutdownProcess(sig os.Signal, proc *os.Process) err return nil } - -func (e *UniversalExecutor) getAllPids() (map[int]*nomadPid, error) { - return getAllPidsByScanning() -} diff --git a/drivers/shared/executor/executor_windows.go b/drivers/shared/executor/executor_windows.go index 443ce25083d..9d0b1407011 100644 --- a/drivers/shared/executor/executor_windows.go +++ b/drivers/shared/executor/executor_windows.go @@ -67,7 +67,3 @@ func (e *UniversalExecutor) shutdownProcess(_ os.Signal, proc *os.Process) error return nil } - -func (e *UniversalExecutor) getAllPids() (map[int]*nomadPid, error) { - return getAllPidsByScanning() -}