-
Notifications
You must be signed in to change notification settings - Fork 887
feat: add agent exec pkg #15577
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
feat: add agent exec pkg #15577
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
90281bd
feat: add agentexec pkg
sreya 538c6c3
remove CLI references
sreya 00c9cd7
add basic test
sreya 196c8a9
fixup some test stuff
sreya 97e68f4
idk
sreya 712b328
more tests
sreya 5f633e1
lint
sreya 05702c3
skip nonlinux
sreya 0132fb3
build files
sreya ae30643
test file only for linux
sreya 986e18e
use flag parsing instead
sreya b158919
flag parsing
sreya 7235bfb
add comment for the use of flags
sreya 521956f
update cmdtest for other oses
sreya 471ea8d
stray deletion
sreya 5076cf0
pr changes
sreya b8725f2
overlooked changes
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package agentexec | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
|
||
"golang.org/x/sys/unix" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
// unset is set to an invalid value for nice and oom scores. | ||
const unset = -2000 | ||
|
||
// CLI runs the agent-exec command. It should only be called by the cli package. | ||
func CLI() error { | ||
// We lock the OS thread here to avoid a race condition where the nice priority | ||
// we get is on a different thread from the one we set it on. | ||
runtime.LockOSThread() | ||
// Nop on success but we do it anyway in case of an error. | ||
defer runtime.UnlockOSThread() | ||
|
||
var ( | ||
fs = flag.NewFlagSet("agent-exec", flag.ExitOnError) | ||
nice = fs.Int("coder-nice", unset, "") | ||
oom = fs.Int("coder-oom", unset, "") | ||
) | ||
|
||
if len(os.Args) < 3 { | ||
return xerrors.Errorf("malformed command %+v", os.Args) | ||
} | ||
|
||
// Parse everything after "coder agent-exec". | ||
err := fs.Parse(os.Args[2:]) | ||
if err != nil { | ||
return xerrors.Errorf("parse flags: %w", err) | ||
} | ||
|
||
// Get everything after "coder agent-exec --" | ||
args := execArgs(os.Args) | ||
if len(args) == 0 { | ||
return xerrors.Errorf("no exec command provided %+v", os.Args) | ||
} | ||
|
||
if *nice == unset { | ||
// If an explicit nice score isn't set, we use the default. | ||
*nice, err = defaultNiceScore() | ||
if err != nil { | ||
return xerrors.Errorf("get default nice score: %w", err) | ||
} | ||
} | ||
|
||
if *oom == unset { | ||
// If an explicit oom score isn't set, we use the default. | ||
*oom, err = defaultOOMScore() | ||
if err != nil { | ||
return xerrors.Errorf("get default oom score: %w", err) | ||
} | ||
} | ||
|
||
err = unix.Setpriority(unix.PRIO_PROCESS, 0, *nice) | ||
if err != nil { | ||
return xerrors.Errorf("set nice score: %w", err) | ||
} | ||
|
||
err = writeOOMScoreAdj(*oom) | ||
if err != nil { | ||
return xerrors.Errorf("set oom score: %w", err) | ||
} | ||
|
||
path, err := exec.LookPath(args[0]) | ||
if err != nil { | ||
return xerrors.Errorf("look path: %w", err) | ||
} | ||
|
||
return syscall.Exec(path, args, os.Environ()) | ||
} | ||
|
||
func defaultNiceScore() (int, error) { | ||
score, err := unix.Getpriority(unix.PRIO_PROCESS, 0) | ||
if err != nil { | ||
return 0, xerrors.Errorf("get nice score: %w", err) | ||
} | ||
// See https://linux.die.net/man/2/setpriority#Notes | ||
score = 20 - score | ||
|
||
score += 5 | ||
if score > 19 { | ||
return 19, nil | ||
} | ||
return score, nil | ||
} | ||
|
||
func defaultOOMScore() (int, error) { | ||
score, err := oomScoreAdj() | ||
if err != nil { | ||
return 0, xerrors.Errorf("get oom score: %w", err) | ||
} | ||
|
||
// If the agent has a negative oom_score_adj, we set the child to 0 | ||
// so it's treated like every other process. | ||
if score < 0 { | ||
return 0, nil | ||
} | ||
|
||
// If the agent is already almost at the maximum then set it to the max. | ||
if score >= 998 { | ||
return 1000, nil | ||
} | ||
|
||
// If the agent oom_score_adj is >=0, we set the child to slightly | ||
// less than the maximum. If users want a different score they set it | ||
// directly. | ||
return 998, nil | ||
} | ||
|
||
func oomScoreAdj() (int, error) { | ||
scoreStr, err := os.ReadFile("/proc/self/oom_score_adj") | ||
if err != nil { | ||
return 0, xerrors.Errorf("read oom_score_adj: %w", err) | ||
} | ||
return strconv.Atoi(strings.TrimSpace(string(scoreStr))) | ||
} | ||
|
||
func writeOOMScoreAdj(score int) error { | ||
return os.WriteFile("/proc/self/oom_score_adj", []byte(fmt.Sprintf("%d", score)), 0o600) | ||
} | ||
|
||
// execArgs returns the arguments to pass to syscall.Exec after the "--" delimiter. | ||
func execArgs(args []string) []string { | ||
for i, arg := range args { | ||
if arg == "--" { | ||
return args[i+1:] | ||
} | ||
} | ||
return 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,178 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package agentexec_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sys/unix" | ||
|
||
"github.com/coder/coder/v2/testutil" | ||
) | ||
|
||
func TestCLI(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("OK", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := testutil.Context(t, testutil.WaitMedium) | ||
cmd, path := cmd(ctx, t, 123, 12) | ||
err := cmd.Start() | ||
require.NoError(t, err) | ||
go cmd.Wait() | ||
|
||
waitForSentinel(ctx, t, cmd, path) | ||
requireOOMScore(t, cmd.Process.Pid, 123) | ||
requireNiceScore(t, cmd.Process.Pid, 12) | ||
}) | ||
|
||
t.Run("Defaults", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := testutil.Context(t, testutil.WaitMedium) | ||
cmd, path := cmd(ctx, t, 0, 0) | ||
err := cmd.Start() | ||
require.NoError(t, err) | ||
go cmd.Wait() | ||
|
||
waitForSentinel(ctx, t, cmd, path) | ||
|
||
expectedNice := expectedNiceScore(t) | ||
expectedOOM := expectedOOMScore(t) | ||
requireOOMScore(t, cmd.Process.Pid, expectedOOM) | ||
requireNiceScore(t, cmd.Process.Pid, expectedNice) | ||
}) | ||
} | ||
|
||
func requireNiceScore(t *testing.T, pid int, score int) { | ||
t.Helper() | ||
|
||
nice, err := unix.Getpriority(unix.PRIO_PROCESS, pid) | ||
require.NoError(t, err) | ||
// See https://linux.die.net/man/2/setpriority#Notes | ||
require.Equal(t, score, 20-nice) | ||
} | ||
|
||
func requireOOMScore(t *testing.T, pid int, expected int) { | ||
t.Helper() | ||
|
||
actual, err := os.ReadFile(fmt.Sprintf("/proc/%d/oom_score_adj", pid)) | ||
require.NoError(t, err) | ||
score := strings.TrimSpace(string(actual)) | ||
require.Equal(t, strconv.Itoa(expected), score) | ||
} | ||
|
||
func waitForSentinel(ctx context.Context, t *testing.T, cmd *exec.Cmd, path string) { | ||
t.Helper() | ||
|
||
ticker := time.NewTicker(testutil.IntervalFast) | ||
defer ticker.Stop() | ||
|
||
// RequireEventually doesn't work well with require.NoError or similar require functions. | ||
for { | ||
err := cmd.Process.Signal(syscall.Signal(0)) | ||
require.NoError(t, err) | ||
|
||
_, err = os.Stat(path) | ||
if err == nil { | ||
return | ||
} | ||
|
||
select { | ||
case <-ticker.C: | ||
case <-ctx.Done(): | ||
require.NoError(t, ctx.Err()) | ||
} | ||
} | ||
} | ||
|
||
func cmd(ctx context.Context, t *testing.T, oom, nice int) (*exec.Cmd, string) { | ||
var ( | ||
args = execArgs(oom, nice) | ||
dir = t.TempDir() | ||
file = filepath.Join(dir, "sentinel") | ||
) | ||
|
||
args = append(args, "sh", "-c", fmt.Sprintf("touch %s && sleep 10m", file)) | ||
//nolint:gosec | ||
cmd := exec.CommandContext(ctx, TestBin, args...) | ||
|
||
// We set this so we can also easily kill the sleep process the shell spawns. | ||
cmd.SysProcAttr = &syscall.SysProcAttr{ | ||
Setpgid: true, | ||
} | ||
|
||
cmd.Env = os.Environ() | ||
var buf bytes.Buffer | ||
cmd.Stdout = &buf | ||
cmd.Stderr = &buf | ||
t.Cleanup(func() { | ||
// Print output of a command if the test fails. | ||
if t.Failed() { | ||
t.Logf("cmd %q output: %s", cmd.Args, buf.String()) | ||
} | ||
if cmd.Process != nil { | ||
// We use -cmd.Process.Pid to kill the whole process group. | ||
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGINT) | ||
} | ||
}) | ||
return cmd, file | ||
} | ||
|
||
func expectedOOMScore(t *testing.T) int { | ||
t.Helper() | ||
|
||
score, err := os.ReadFile(fmt.Sprintf("/proc/%d/oom_score_adj", os.Getpid())) | ||
require.NoError(t, err) | ||
|
||
scoreInt, err := strconv.Atoi(strings.TrimSpace(string(score))) | ||
require.NoError(t, err) | ||
|
||
if scoreInt < 0 { | ||
return 0 | ||
} | ||
if scoreInt >= 998 { | ||
return 1000 | ||
} | ||
return 998 | ||
} | ||
|
||
func expectedNiceScore(t *testing.T) int { | ||
t.Helper() | ||
|
||
score, err := unix.Getpriority(unix.PRIO_PROCESS, os.Getpid()) | ||
require.NoError(t, err) | ||
|
||
// Priority is niceness + 20. | ||
score = 20 - score | ||
score += 5 | ||
if score > 19 { | ||
return 19 | ||
} | ||
return score | ||
} | ||
|
||
func execArgs(oom int, nice int) []string { | ||
execArgs := []string{"agent-exec"} | ||
if oom != 0 { | ||
execArgs = append(execArgs, fmt.Sprintf("--coder-oom=%d", oom)) | ||
} | ||
if nice != 0 { | ||
execArgs = append(execArgs, fmt.Sprintf("--coder-nice=%d", nice)) | ||
} | ||
execArgs = append(execArgs, "--") | ||
return execArgs | ||
} |
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,10 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package agentexec | ||
|
||
import "golang.org/x/xerrors" | ||
|
||
func CLI() error { | ||
return xerrors.New("agent-exec is only supported on Linux") | ||
} |
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,19 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/coder/coder/v2/agent/agentexec" | ||
) | ||
|
||
func main() { | ||
err := agentexec.CLI() | ||
if err != nil { | ||
_, _ = fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get why we're translating from kernel to userspace scores here if we just call
Setpriority()
with this value. Shouldn't we need to translate back?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getpriority
man page says the following:Further up it says the following for
setpriority
:Pretty confusing tbh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woof. OK, initially read this as glibc doing the translation in both cases, but I guess it's only the get operation that has issues with negative numbers.