Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: process avoid-being-killed for mem exp #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions exec/executor_common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"os/exec"
"path"
"strconv"
"strings"
"syscall"
"time"
Expand All @@ -32,6 +33,7 @@ import (
"github.com/chaosblade-io/chaosblade-spec-go/log"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"

"github.com/containerd/cgroups"
cgroupsv2 "github.com/containerd/cgroups/v2"
)
Expand Down Expand Up @@ -259,9 +261,55 @@ func execForHangAction(uid string, ctx context.Context, expModel *spec.ExpModel,
}
}
}

if expModel.Target == "mem" && expModel.ActionFlags["avoid-being-killed"] == "true" {
if err := exec.Command("choom", "-n", "-1000", "-p", strconv.Itoa(command.Process.Pid)).Run(); err != nil { //nolint:gosec
log.Errorf(ctx, "choom failed, %s", err.Error())
} else {
log.Infof(ctx, "choom success, target pid: %v, current pid: %v", command.Process.Pid, os.Getpid())
choomChildProcesses(ctx, command.Process.Pid)
}
}

return spec.ReturnSuccess(command.Process.Pid)
}

func choomChildProcesses(ctx context.Context, pid int) {
childPids, err := getChildPids(pid)
if err != nil {
log.Errorf(ctx, "failed to get child pids for pid %d, %s", pid, err.Error())
return
}

for _, childPid := range childPids {
if err := exec.Command("choom", "-n", "-1000", "-p", strconv.Itoa(childPid)).Run(); err != nil { //nolint:gosec
log.Errorf(ctx, "choom failed for child pid %d, %s", childPid, err.Error())
} else {
log.Infof(ctx, "choom success for child pid %d", childPid)
choomChildProcesses(ctx, childPid)
}
}
}

func getChildPids(pid int) ([]int, error) {
procPath := fmt.Sprintf("/proc/%d/task/%d/children", pid, pid)
data, err := os.ReadFile(procPath)
if err != nil {
return nil, err
}

childPidsStr := strings.Fields(string(data))
childPids := make([]int, len(childPidsStr))
for i, pidStr := range childPidsStr {
childPids[i], err = strconv.Atoi(pidStr)
if err != nil {
return nil, err
}
}

return childPids, nil
}

func getProcessComm(pid int) (string, error) {
f, err := os.Open(fmt.Sprintf("%s/%d/comm", "/proc", pid))
if err != nil {
Expand Down