Skip to content

Commit 1073868

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 c6da899 commit 1073868

2 files changed

Lines changed: 51 additions & 44 deletions

File tree

runtime/v1/shim/service.go

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

516516
func (s *Service) checkProcesses(e runc.Exit) {
517-
shouldKillAll, err := shouldKillAllOnExit(s.bundle)
518-
if err != nil {
519-
log.G(s.context).WithError(err).Error("failed to check shouldKillAll")
520-
}
521-
522517
for _, p := range s.allProcesses() {
523-
if p.Pid() == e.Pid {
518+
if p.Pid() != e.Pid {
519+
continue
520+
}
524521

522+
if ip, ok := p.(*process.Init); ok {
523+
shouldKillAll, err := shouldKillAllOnExit(s.bundle)
524+
if err != nil {
525+
log.G(s.context).WithError(err).Error("failed to check shouldKillAll")
526+
}
527+
528+
// Ensure all children are killed
525529
if shouldKillAll {
526-
if ip, ok := p.(*process.Init); ok {
527-
// Ensure all children are killed
528-
if err := ip.KillAll(s.context); err != nil {
529-
log.G(s.context).WithError(err).WithField("id", ip.ID()).
530-
Error("failed to kill init's children")
531-
}
530+
if err := ip.KillAll(s.context); err != nil {
531+
log.G(s.context).WithError(err).WithField("id", ip.ID()).
532+
Error("failed to kill init's children")
532533
}
533534
}
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
543535
}
536+
537+
p.SetExited(e.Status)
538+
s.events <- &eventstypes.TaskExit{
539+
ContainerID: s.id,
540+
ID: p.ID(),
541+
Pid: uint32(e.Pid),
542+
ExitStatus: uint32(e.Status),
543+
ExitedAt: p.ExitedAt(),
544+
}
545+
return
544546
}
545547
}
546548

runtime/v2/runc/v2/service.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -645,36 +645,41 @@ func (s *service) checkProcesses(e runcC.Exit) {
645645
defer s.mu.Unlock()
646646

647647
for _, container := range s.containers {
648-
if container.HasPid(e.Pid) {
649-
shouldKillAll, err := shouldKillAllOnExit(container.Bundle)
650-
if err != nil {
651-
log.G(s.context).WithError(err).Error("failed to check shouldKillAll")
648+
if !container.HasPid(e.Pid) {
649+
continue
650+
}
651+
652+
for _, p := range container.All() {
653+
if p.Pid() != e.Pid {
654+
continue
652655
}
653656

654-
for _, p := range container.All() {
655-
if p.Pid() == e.Pid {
656-
if shouldKillAll {
657-
if ip, ok := p.(*process.Init); ok {
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")
662-
}
663-
}
657+
if ip, ok := p.(*process.Init); ok {
658+
shouldKillAll, err := shouldKillAllOnExit(container.Bundle)
659+
if err != nil {
660+
log.G(s.context).WithError(err).Error("failed to check shouldKillAll")
661+
}
662+
663+
// Ensure all children are killed
664+
if shouldKillAll {
665+
if err := ip.KillAll(s.context); err != nil {
666+
logrus.WithError(err).WithField("id", ip.ID()).
667+
Error("failed to kill init's children")
664668
}
665-
p.SetExited(e.Status)
666-
s.sendL(&eventstypes.TaskExit{
667-
ContainerID: container.ID,
668-
ID: p.ID(),
669-
Pid: uint32(e.Pid),
670-
ExitStatus: uint32(e.Status),
671-
ExitedAt: p.ExitedAt(),
672-
})
673-
return
674669
}
675670
}
671+
672+
p.SetExited(e.Status)
673+
s.sendL(&eventstypes.TaskExit{
674+
ContainerID: container.ID,
675+
ID: p.ID(),
676+
Pid: uint32(e.Pid),
677+
ExitStatus: uint32(e.Status),
678+
ExitedAt: p.ExitedAt(),
679+
})
676680
return
677681
}
682+
return
678683
}
679684
}
680685

0 commit comments

Comments
 (0)