From e3ab8bda604dbafb6c69f4c42979e92ead445138 Mon Sep 17 00:00:00 2001 From: zyu Date: Fri, 6 Mar 2020 09:51:26 -0800 Subject: [PATCH] Avoid allocating slice for finding Process Signed-off-by: zyu --- runtime/v1/shim/service.go | 57 +++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/runtime/v1/shim/service.go b/runtime/v1/shim/service.go index c0ec03931..6bd6aefa2 100644 --- a/runtime/v1/shim/service.go +++ b/runtime/v1/shim/service.go @@ -503,42 +503,37 @@ func (s *Service) processExits() { } } -func (s *Service) allProcesses() []process.Process { - s.mu.Lock() - defer s.mu.Unlock() - - res := make([]process.Process, 0, len(s.processes)) - for _, p := range s.processes { - res = append(res, p) - } - return res -} - func (s *Service) checkProcesses(e runc.Exit) { - for _, p := range s.allProcesses() { - if p.Pid() != e.Pid { - continue + var p process.Process + s.mu.Lock() + for _, proc := range s.processes { + if proc.Pid() == e.Pid { + p = proc + break } - - if ip, ok := p.(*process.Init); ok { - // Ensure all children are killed - if shouldKillAllOnExit(s.context, s.bundle) { - if err := ip.KillAll(s.context); err != nil { - log.G(s.context).WithError(err).WithField("id", ip.ID()). - Error("failed to kill init's children") - } + } + s.mu.Unlock() + if p == nil { + log.G(s.context).Infof("process with id:%d wasn't found", e.Pid) + return + } + if ip, ok := p.(*process.Init); ok { + // Ensure all children are killed + if shouldKillAllOnExit(s.context, s.bundle) { + if err := ip.KillAll(s.context); err != nil { + log.G(s.context).WithError(err).WithField("id", ip.ID()). + Error("failed to kill init's children") } } + } - p.SetExited(e.Status) - s.events <- &eventstypes.TaskExit{ - ContainerID: s.id, - ID: p.ID(), - Pid: uint32(e.Pid), - ExitStatus: uint32(e.Status), - ExitedAt: p.ExitedAt(), - } - return + p.SetExited(e.Status) + s.events <- &eventstypes.TaskExit{ + ContainerID: s.id, + ID: p.ID(), + Pid: uint32(e.Pid), + ExitStatus: uint32(e.Status), + ExitedAt: p.ExitedAt(), } }