Skip to content

feat: implement agent process management #9461

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

Merged
merged 21 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
remove oom_score_adj
  • Loading branch information
sreya committed Sep 14, 2023
commit d132480a017450421d2ea0e6357e6c444b914996
13 changes: 2 additions & 11 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1327,8 +1327,7 @@ func (a *agent) manageProcessPriorityLoop(ctx context.Context) {

func (a *agent) manageProcessPriority(ctx context.Context) ([]*agentproc.Process, error) {
const (
niceness = 10
oomScoreAdj = -500
niceness = 10
)

procs, err := agentproc.List(a.filesystem, a.syscaller)
Expand All @@ -1351,18 +1350,10 @@ func (a *agent) manageProcessPriority(ctx context.Context) ([]*agentproc.Process
contains := strings.Contains(proc.Cmd(), e)
return contains
}

// If the process is prioritized we should adjust
// it's oom_score_adj and avoid lowering its niceness.
if slices.ContainsFunc[[]string, string](prioritizedProcs, containsFn) {
err = proc.SetOOMAdj(oomScoreAdj)
if err != nil {
logger.Warn(ctx, "unable to set proc oom_score_adj",
slog.F("oom_score_adj", oomScoreAdj),
slog.Error(err),
)
continue
}
modProcs = append(modProcs, proc)
continue
}

Expand Down
15 changes: 1 addition & 14 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2460,20 +2460,7 @@ func TestAgent_ManageProcessPriority(t *testing.T) {
o.ProcessManagementTick = ticker
})
actualProcs := <-modProcs
require.Len(t, actualProcs, 4)

for _, actual := range actualProcs {
expectedScore := "0"
expected, ok := expectedProcs[actual.PID]
require.True(t, ok)
if expected.PID == 0 {
expectedScore = "-500"
}

score, err := afero.ReadFile(fs, filepath.Join(actual.Dir, "oom_score_adj"))
require.NoError(t, err)
require.Equal(t, expectedScore, strings.TrimSpace(string(score)))
}
require.Len(t, actualProcs, len(expectedProcs)-1)
})

t.Run("IgnoreCustomNice", func(t *testing.T) {
Expand Down
4 changes: 0 additions & 4 deletions agent/agentproc/agentproctest/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func GenerateProcess(t *testing.T, fs afero.Fs, muts ...func(*agentproc.Process)
process := agentproc.Process{
CmdLine: cmdline,
PID: int32(pid),
FS: fs,
}

for _, mut := range muts {
Expand All @@ -46,8 +45,5 @@ func GenerateProcess(t *testing.T, fs afero.Fs, muts ...func(*agentproc.Process)
err = afero.WriteFile(fs, fmt.Sprintf("%s/cmdline", process.Dir), []byte(process.CmdLine), 0o444)
require.NoError(t, err)

err = afero.WriteFile(fs, fmt.Sprintf("%s/oom_score_adj", process.Dir), []byte("0"), 0o444)
require.NoError(t, err)

return process
}
4 changes: 0 additions & 4 deletions agent/agentproc/proc_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (
"github.com/spf13/afero"
)

func (p *Process) SetOOMAdj(score int) error {
return errUnimplemented
}

func (p *Process) Niceness(sc Syscaller) (int, error) {
return 0, errUnimplemented
}
Expand Down
19 changes: 0 additions & 19 deletions agent/agentproc/proc_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package agentproc_test

import (
"fmt"
"runtime"
"strings"
"syscall"
"testing"

Expand Down Expand Up @@ -137,23 +135,6 @@ func TestProcess(t *testing.T) {
t.Skipf("skipping non-linux environment")
}

t.Run("SetOOMAdj", func(t *testing.T) {
t.Parallel()

var (
fs = afero.NewMemMapFs()
proc = agentproctest.GenerateProcess(t, fs)
expectedScore = -1000
)

err := proc.SetOOMAdj(expectedScore)
require.NoError(t, err)

actualScore, err := afero.ReadFile(fs, fmt.Sprintf("/proc/%d/oom_score_adj", proc.PID))
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("%d", expectedScore), strings.TrimSpace(string(actualScore)))
})

t.Run("SetNiceness", func(t *testing.T) {
t.Parallel()

Expand Down
15 changes: 0 additions & 15 deletions agent/agentproc/proc_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func List(fs afero.Fs, syscaller Syscaller) ([]*Process, error) {
PID: int32(pid),
CmdLine: string(cmdline),
Dir: filepath.Join(defaultProcDir, entry),
FS: fs,
})
}

Expand Down Expand Up @@ -85,20 +84,6 @@ func isProcessExist(syscaller Syscaller, pid int32) (bool, error) {
return false, xerrors.Errorf("kill: %w", err)
}

func (p *Process) SetOOMAdj(score int) error {
path := filepath.Join(p.Dir, "oom_score_adj")
err := afero.WriteFile(p.FS,
path,
[]byte(strconv.Itoa(score)),
0o644,
)
if err != nil {
return xerrors.Errorf("write %q: %w", path, err)
}

return nil
}

func (p *Process) Niceness(sc Syscaller) (int, error) {
nice, err := sc.GetPriority(p.PID)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions agent/agentproc/syscaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package agentproc

import (
"syscall"

"github.com/spf13/afero"
)

type Syscaller interface {
Expand All @@ -18,5 +16,4 @@ type Process struct {
Dir string
CmdLine string
PID int32
FS afero.Fs
}
167 changes: 0 additions & 167 deletions agent/out.txt

This file was deleted.