-
Notifications
You must be signed in to change notification settings - Fork 894
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
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4ed4069
feat: implement agent process management
sreya 7e59db6
improve process detection
sreya 8c65216
add agentproc tests
sreya 760cbb3
some minor agent tests
sreya f4b864e
add a proper test for proc management
sreya cbcb854
custom nice
sreya 8230247
refactor into build files
sreya 3e1defd
tick tock
sreya 2fe9c70
make fmt
sreya ef41e9a
pr comments
sreya 05baba0
lint
sreya 478d57c
whoops
sreya 0ced5ce
skip non-linux
sreya 8aaa6d5
skip non-linux
sreya cea4851
prevent race
sreya 5020eb4
only prioritize ourselves
sreya 11ab047
Revert "only prioritize ourselves"
sreya 46ef05a
only prioritize coder agent
sreya d132480
remove oom_score_adj
sreya 04ee5cb
defer first
sreya ffbeab9
avoid resetting niceness for already niced procs
sreya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add agentproc tests
- Loading branch information
commit 8c652162f3c30ecbb71cae6607242b6b0388ab49
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package agentproctest | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/v2/agent/agentproc" | ||
"github.com/coder/coder/v2/cryptorand" | ||
) | ||
|
||
func GenerateProcess(t *testing.T, fs afero.Fs, dir string) agentproc.Process { | ||
t.Helper() | ||
|
||
pid, err := cryptorand.Intn(1<<31 - 1) | ||
require.NoError(t, err) | ||
|
||
err = fs.MkdirAll(fmt.Sprintf("/%s/%d", dir, pid), 0555) | ||
require.NoError(t, err) | ||
|
||
arg1, err := cryptorand.String(5) | ||
require.NoError(t, err) | ||
|
||
arg2, err := cryptorand.String(5) | ||
require.NoError(t, err) | ||
|
||
arg3, err := cryptorand.String(5) | ||
require.NoError(t, err) | ||
|
||
cmdline := fmt.Sprintf("%s\x00%s\x00%s", arg1, arg2, arg3) | ||
|
||
err = afero.WriteFile(fs, fmt.Sprintf("/%s/%d/cmdline", dir, pid), []byte(cmdline), 0444) | ||
require.NoError(t, err) | ||
|
||
return agentproc.Process{ | ||
PID: int32(pid), | ||
CmdLine: cmdline, | ||
Dir: fmt.Sprintf("%s/%d", dir, pid), | ||
FS: fs, | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// Package agentproc contains logic for interfacing with local | ||
// processes running in the same context as the agent. | ||
package agentproc | ||
|
||
//go:generate mockgen -destination ./syscallermock_test.go -package agentproc_test github.com/coder/coder/v2/agent/agentproc Syscaller |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,180 @@ | ||
package agentproc_test | ||
|
||
type mockSyscaller struct { | ||
SetPriorityFn func(int32, int) error | ||
import ( | ||
"fmt" | ||
"strings" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/v2/agent/agentproc" | ||
"github.com/coder/coder/v2/agent/agentproc/agentproctest" | ||
) | ||
|
||
func TestList(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("OK", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
fs = afero.NewMemMapFs() | ||
sc = NewMockSyscaller(gomock.NewController(t)) | ||
expectedProcs = make(map[int32]agentproc.Process) | ||
rootDir = agentproc.DefaultProcDir | ||
) | ||
|
||
for i := 0; i < 4; i++ { | ||
proc := agentproctest.GenerateProcess(t, fs, rootDir) | ||
expectedProcs[proc.PID] = proc | ||
|
||
sc.EXPECT(). | ||
Kill(proc.PID, syscall.Signal(0)). | ||
Return(nil) | ||
} | ||
|
||
actualProcs, err := agentproc.List(fs, sc, rootDir) | ||
require.NoError(t, err) | ||
require.Len(t, actualProcs, 4) | ||
sreya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, proc := range actualProcs { | ||
expected, ok := expectedProcs[proc.PID] | ||
require.True(t, ok) | ||
require.Equal(t, expected.PID, proc.PID) | ||
require.Equal(t, expected.CmdLine, proc.CmdLine) | ||
require.Equal(t, expected.Dir, proc.Dir) | ||
} | ||
}) | ||
|
||
t.Run("FinishedProcess", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
fs = afero.NewMemMapFs() | ||
sc = NewMockSyscaller(gomock.NewController(t)) | ||
expectedProcs = make(map[int32]agentproc.Process) | ||
rootDir = agentproc.DefaultProcDir | ||
) | ||
|
||
for i := 0; i < 3; i++ { | ||
proc := agentproctest.GenerateProcess(t, fs, rootDir) | ||
expectedProcs[proc.PID] = proc | ||
|
||
sc.EXPECT(). | ||
Kill(proc.PID, syscall.Signal(0)). | ||
Return(nil) | ||
} | ||
|
||
// Create a process that's already finished. We're not adding | ||
// it to the map because it should be skipped over. | ||
proc := agentproctest.GenerateProcess(t, fs, rootDir) | ||
sc.EXPECT(). | ||
Kill(proc.PID, syscall.Signal(0)). | ||
Return(xerrors.New("os: process already finished")) | ||
|
||
actualProcs, err := agentproc.List(fs, sc, rootDir) | ||
require.NoError(t, err) | ||
require.Len(t, actualProcs, 3) | ||
sreya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, proc := range actualProcs { | ||
expected, ok := expectedProcs[proc.PID] | ||
require.True(t, ok) | ||
require.Equal(t, expected.PID, proc.PID) | ||
require.Equal(t, expected.CmdLine, proc.CmdLine) | ||
require.Equal(t, expected.Dir, proc.Dir) | ||
} | ||
}) | ||
|
||
t.Run("NoSuchProcess", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
fs = afero.NewMemMapFs() | ||
sc = NewMockSyscaller(gomock.NewController(t)) | ||
expectedProcs = make(map[int32]agentproc.Process) | ||
rootDir = agentproc.DefaultProcDir | ||
) | ||
|
||
for i := 0; i < 3; i++ { | ||
proc := agentproctest.GenerateProcess(t, fs, rootDir) | ||
expectedProcs[proc.PID] = proc | ||
|
||
sc.EXPECT(). | ||
Kill(proc.PID, syscall.Signal(0)). | ||
Return(nil) | ||
} | ||
|
||
// Create a process that doesn't exist. We're not adding | ||
// it to the map because it should be skipped over. | ||
proc := agentproctest.GenerateProcess(t, fs, rootDir) | ||
sc.EXPECT(). | ||
Kill(proc.PID, syscall.Signal(0)). | ||
Return(syscall.ESRCH) | ||
|
||
actualProcs, err := agentproc.List(fs, sc, rootDir) | ||
require.NoError(t, err) | ||
require.Len(t, actualProcs, 3) | ||
for _, proc := range actualProcs { | ||
expected, ok := expectedProcs[proc.PID] | ||
require.True(t, ok) | ||
require.Equal(t, expected.PID, proc.PID) | ||
require.Equal(t, expected.CmdLine, proc.CmdLine) | ||
require.Equal(t, expected.Dir, proc.Dir) | ||
} | ||
}) | ||
} | ||
|
||
func (f mockSyscaller) SetPriority(pid int32, nice int) error { | ||
if f.SetPriorityFn == nil { | ||
return nil | ||
} | ||
return f.SetPriorityFn(pid, nice) | ||
// These tests are not very interesting but they provide some modicum of | ||
// confidence. | ||
func TestProcess(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("SetOOMAdj", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
fs = afero.NewMemMapFs() | ||
dir = agentproc.DefaultProcDir | ||
proc = agentproctest.GenerateProcess(t, fs, agentproc.DefaultProcDir) | ||
expectedScore = -1000 | ||
) | ||
|
||
err := proc.SetOOMAdj(expectedScore) | ||
require.NoError(t, err) | ||
|
||
actualScore, err := afero.ReadFile(fs, fmt.Sprintf("%s/%d/oom_score_adj", dir, proc.PID)) | ||
require.NoError(t, err) | ||
require.Equal(t, fmt.Sprintf("%d", expectedScore), strings.TrimSpace(string(actualScore))) | ||
sreya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
|
||
t.Run("SetNiceness", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
sc = NewMockSyscaller(gomock.NewController(t)) | ||
proc = &agentproc.Process{ | ||
PID: 32, | ||
} | ||
score = 20 | ||
) | ||
|
||
sc.EXPECT().SetPriority(proc.PID, score).Return(nil) | ||
err := proc.SetNiceness(sc, score) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("Name", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
proc = &agentproc.Process{ | ||
CmdLine: "helloworld\x00--arg1\x00--arg2", | ||
} | ||
expectedName = "helloworld" | ||
) | ||
|
||
require.Equal(t, expectedName, proc.Name()) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.