-
Notifications
You must be signed in to change notification settings - Fork 898
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
Next
Next commit
feat: implement agent process management
- An opt-in feature has been added to the agent to allow deprioritizing non coder-related processes for both CPU and memory. Non coder processes have their niceness set to 10 and their oom_score_adj set to 100
- Loading branch information
commit 4ed40699cfd4eb2b18d534d1e3d49f61237a6c4a
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,3 @@ | ||
// Package agentproc contains logic for interfacing with local | ||
// processes running in the same context as the agent. | ||
package agentproc |
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,99 @@ | ||
package agentproc | ||
|
||
import ( | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/spf13/afero" | ||
"golang.org/x/sys/unix" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
const DefaultProcDir = "/proc" | ||
sreya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type Syscaller interface { | ||
SetPriority(pid int32, priority int) error | ||
} | ||
|
||
type UnixSyscaller struct{} | ||
|
||
func (UnixSyscaller) SetPriority(pid int32, nice int) error { | ||
err := unix.Setpriority(unix.PRIO_PROCESS, int(pid), nice) | ||
if err != nil { | ||
return xerrors.Errorf("set priority: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
type Process struct { | ||
Dir string | ||
CmdLine string | ||
PID int32 | ||
fs afero.Fs | ||
} | ||
|
||
func (p *Process) SetOOMAdj(score int) error { | ||
sreya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
path := filepath.Join(p.Dir, "oom_score_adj") | ||
err := afero.WriteFile(p.fs, | ||
path, | ||
[]byte(strconv.Itoa(score)), | ||
0644, | ||
) | ||
if err != nil { | ||
return xerrors.Errorf("write %q: %w", path, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *Process) SetNiceness(sc Syscaller, score int) error { | ||
err := sc.SetPriority(p.PID, score) | ||
if err != nil { | ||
return xerrors.Errorf("set priority for %q: %w", p.CmdLine, err) | ||
} | ||
return nil | ||
} | ||
|
||
func (p *Process) Name() string { | ||
args := strings.Split(p.CmdLine, "\x00") | ||
// Split will always return at least one element. | ||
return args[0] | ||
} | ||
|
||
func List(fs afero.Fs, dir string) ([]*Process, error) { | ||
d, err := fs.Open(dir) | ||
if err != nil { | ||
return nil, xerrors.Errorf("open dir %q: %w", dir, err) | ||
} | ||
|
||
sreya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
entries, err := d.Readdirnames(0) | ||
if err != nil { | ||
return nil, xerrors.Errorf("readdirnames: %w", err) | ||
} | ||
|
||
processes := make([]*Process, 0, len(entries)) | ||
for _, entry := range entries { | ||
pid, err := strconv.ParseInt(entry, 10, 32) | ||
if err != nil { | ||
continue | ||
} | ||
cmdline, err := afero.ReadFile(fs, filepath.Join(dir, entry, "cmdline")) | ||
if err != nil { | ||
var errNo syscall.Errno | ||
if xerrors.As(err, &errNo) && errNo == syscall.EPERM { | ||
continue | ||
} | ||
return nil, xerrors.Errorf("read cmdline: %w", err) | ||
} | ||
processes = append(processes, &Process{ | ||
PID: int32(pid), | ||
CmdLine: string(cmdline), | ||
Dir: filepath.Join(dir, entry), | ||
fs: fs, | ||
}) | ||
} | ||
|
||
return processes, nil | ||
} |
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,12 @@ | ||
package agentproc_test | ||
|
||
type mockSyscaller struct { | ||
SetPriorityFn func(int32, int) error | ||
} | ||
|
||
func (f mockSyscaller) SetPriority(pid int32, nice int) error { | ||
if f.SetPriorityFn == nil { | ||
return nil | ||
} | ||
return f.SetPriorityFn(pid, nice) | ||
} |
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.