Skip to content

Commit 4288ba1

Browse files
committed
runtime: only check killall for init process
When containerd-shim does reaper, the most processes are not init process. Since json.Decode consumes more CPU resource, we should check killall option for init process only. Signed-off-by: Wei Fu <[email protected]>
1 parent 79f4c65 commit 4288ba1

2 files changed

Lines changed: 47 additions & 42 deletions

File tree

runtime/v1/shim/service.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -511,33 +511,35 @@ func (s *Service) allProcesses() []rproc.Process {
511511
}
512512

513513
func (s *Service) checkProcesses(e runc.Exit) {
514-
shouldKillAll, err := shouldKillAllOnExit(s.bundle)
515-
if err != nil {
516-
log.G(s.context).WithError(err).Error("failed to check shouldKillAll")
517-
}
518-
519514
for _, p := range s.allProcesses() {
520-
if p.Pid() == e.Pid {
515+
if p.Pid() != e.Pid {
516+
continue
517+
}
521518

519+
if ip, ok := p.(*proc.Init); ok {
520+
shouldKillAll, err := shouldKillAllOnExit(s.bundle)
521+
if err != nil {
522+
log.G(s.context).WithError(err).Error("failed to check shouldKillAll")
523+
}
524+
525+
// Ensure all children are killed
522526
if shouldKillAll {
523-
if ip, ok := p.(*proc.Init); ok {
524-
// Ensure all children are killed
525-
if err := ip.KillAll(s.context); err != nil {
526-
log.G(s.context).WithError(err).WithField("id", ip.ID()).
527-
Error("failed to kill init's children")
528-
}
527+
if err := ip.KillAll(s.context); err != nil {
528+
log.G(s.context).WithError(err).WithField("id", ip.ID()).
529+
Error("failed to kill init's children")
529530
}
530531
}
531-
p.SetExited(e.Status)
532-
s.events <- &eventstypes.TaskExit{
533-
ContainerID: s.id,
534-
ID: p.ID(),
535-
Pid: uint32(e.Pid),
536-
ExitStatus: uint32(e.Status),
537-
ExitedAt: p.ExitedAt(),
538-
}
539-
return
540532
}
533+
534+
p.SetExited(e.Status)
535+
s.events <- &eventstypes.TaskExit{
536+
ContainerID: s.id,
537+
ID: p.ID(),
538+
Pid: uint32(e.Pid),
539+
ExitStatus: uint32(e.Status),
540+
ExitedAt: p.ExitedAt(),
541+
}
542+
return
541543
}
542544
}
543545

runtime/v2/runc/service.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -642,32 +642,35 @@ func (s *service) processExits() {
642642
}
643643

644644
func (s *service) checkProcesses(e runcC.Exit) {
645-
shouldKillAll, err := shouldKillAllOnExit(s.bundle)
646-
if err != nil {
647-
log.G(s.context).WithError(err).Error("failed to check shouldKillAll")
648-
}
649-
650645
for _, p := range s.allProcesses() {
651-
if p.Pid() == e.Pid {
646+
if p.Pid() != e.Pid {
647+
continue
648+
649+
}
650+
651+
if ip, ok := p.(*proc.Init); ok {
652+
shouldKillAll, err := shouldKillAllOnExit(s.bundle)
653+
if err != nil {
654+
log.G(s.context).WithError(err).Error("failed to check shouldKillAll")
655+
}
656+
652657
if shouldKillAll {
653-
if ip, ok := p.(*proc.Init); ok {
654-
// Ensure all children are killed
655-
if err := ip.KillAll(s.context); err != nil {
656-
logrus.WithError(err).WithField("id", ip.ID()).
657-
Error("failed to kill init's children")
658-
}
658+
// Ensure all children are killed
659+
if err := ip.KillAll(s.context); err != nil {
660+
logrus.WithError(err).WithField("id", ip.ID()).
661+
Error("failed to kill init's children")
659662
}
660663
}
661-
p.SetExited(e.Status)
662-
s.events <- &eventstypes.TaskExit{
663-
ContainerID: s.id,
664-
ID: p.ID(),
665-
Pid: uint32(e.Pid),
666-
ExitStatus: uint32(e.Status),
667-
ExitedAt: p.ExitedAt(),
668-
}
669-
return
670664
}
665+
p.SetExited(e.Status)
666+
s.events <- &eventstypes.TaskExit{
667+
ContainerID: s.id,
668+
ID: p.ID(),
669+
Pid: uint32(e.Pid),
670+
ExitStatus: uint32(e.Status),
671+
ExitedAt: p.ExitedAt(),
672+
}
673+
return
671674
}
672675
}
673676

0 commit comments

Comments
 (0)