Skip to content

Commit 5020eb4

Browse files
committed
only prioritize ourselves
1 parent cea4851 commit 5020eb4

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

agent/agent.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ type Options struct {
7979
ModifiedProcesses chan []*agentproc.Process
8080
// ProcessManagementTick is used for testing process priority management.
8181
ProcessManagementTick <-chan time.Time
82+
// PrioritizedPIDs are processes that should have preferred CPU allocation
83+
// and be de-prioritized for OOM Killer selection.
84+
PrioritizedPIDs []int
8285
}
8386

8487
type Client interface {
@@ -161,6 +164,7 @@ func New(options Options) Agent {
161164
syscaller: options.Syscaller,
162165
modifiedProcs: options.ModifiedProcesses,
163166
processManagementTick: options.ProcessManagementTick,
167+
prioritizedPIDs: options.PrioritizedPIDs,
164168

165169
prometheusRegistry: prometheusRegistry,
166170
metrics: newAgentMetrics(prometheusRegistry),
@@ -221,6 +225,9 @@ type agent struct {
221225
modifiedProcs chan []*agentproc.Process
222226
// processManagementTick is used for testing process priority management.
223227
processManagementTick <-chan time.Time
228+
// prioritizedPIDs are processes that should have preferred CPU allocation
229+
// and be de-prioritized for OOM Killer selection.
230+
prioritizedPIDs []int
224231
}
225232

226233
func (a *agent) TailnetConn() *tailnet.Conn {
@@ -1278,8 +1285,6 @@ func (a *agent) startReportingConnectionStats(ctx context.Context) {
12781285
}
12791286
}
12801287

1281-
var prioritizedProcs = []string{"coder"}
1282-
12831288
func (a *agent) manageProcessPriorityLoop(ctx context.Context) {
12841289
if val := a.envVars[EnvProcPrioMgmt]; val == "" || runtime.GOOS != "linux" {
12851290
a.logger.Debug(ctx, "process priority not enabled, agent will not manage process niceness/oom_score_adj ",
@@ -1337,11 +1342,9 @@ func (a *agent) manageProcessPriority(ctx context.Context) ([]*agentproc.Process
13371342
slog.F("pid", proc.PID),
13381343
)
13391344

1340-
// Trim off the path e.g. "./coder" -> "coder"
1341-
name := filepath.Base(proc.Name())
13421345
// If the process is prioritized we should adjust
13431346
// it's oom_score_adj and avoid lowering its niceness.
1344-
if slices.Contains(prioritizedProcs, name) {
1347+
if slices.Contains(a.prioritizedPIDs, int(proc.PID)) {
13451348
err = proc.SetOOMAdj(oomScoreAdj)
13461349
if err != nil {
13471350
logger.Warn(ctx, "unable to set proc oom_score_adj",

agent/agent_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,12 +2411,13 @@ func TestAgent_ManageProcessPriority(t *testing.T) {
24112411
}
24122412

24132413
var (
2414-
expectedProcs = map[int32]agentproc.Process{}
2415-
fs = afero.NewMemMapFs()
2416-
syscaller = agentproctest.NewMockSyscaller(gomock.NewController(t))
2417-
ticker = make(chan time.Time)
2418-
modProcs = make(chan []*agentproc.Process)
2419-
logger = slog.Make(sloghuman.Sink(io.Discard))
2414+
expectedProcs = map[int32]agentproc.Process{}
2415+
fs = afero.NewMemMapFs()
2416+
syscaller = agentproctest.NewMockSyscaller(gomock.NewController(t))
2417+
ticker = make(chan time.Time)
2418+
modProcs = make(chan []*agentproc.Process)
2419+
logger = slog.Make(sloghuman.Sink(io.Discard))
2420+
prioritizedProc = 123
24202421
)
24212422

24222423
// Create some processes.
@@ -2429,12 +2430,15 @@ func TestAgent_ManageProcessPriority(t *testing.T) {
24292430
proc = agentproctest.GenerateProcess(t, fs,
24302431
func(p *agentproc.Process) {
24312432
p.CmdLine = "./coder\x00agent\x00--no-reap"
2432-
p.PID = 1
2433+
p.PID = int32(prioritizedProc)
24332434
},
24342435
)
24352436
} else {
24362437
// The rest are peasants.
24372438
proc = agentproctest.GenerateProcess(t, fs)
2439+
if proc.PID == int32(prioritizedProc) {
2440+
proc.PID = 1234
2441+
}
24382442
syscaller.EXPECT().SetPriority(proc.PID, 10).Return(nil)
24392443
syscaller.EXPECT().GetPriority(proc.PID).Return(20, nil)
24402444
}
@@ -2452,6 +2456,7 @@ func TestAgent_ManageProcessPriority(t *testing.T) {
24522456
o.Filesystem = fs
24532457
o.Logger = logger
24542458
o.ProcessManagementTick = ticker
2459+
o.PrioritizedPIDs = []int{prioritizedProc}
24552460
})
24562461
actualProcs := <-modProcs
24572462
require.Len(t, actualProcs, 4)
@@ -2460,7 +2465,7 @@ func TestAgent_ManageProcessPriority(t *testing.T) {
24602465
expectedScore := "0"
24612466
expected, ok := expectedProcs[actual.PID]
24622467
require.True(t, ok)
2463-
if expected.PID == 1 {
2468+
if expected.PID == int32(prioritizedProc) {
24642469
expectedScore = "-500"
24652470
}
24662471

cli/agent.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
299299
// Intentionally set this to nil. It's mainly used
300300
// for testing.
301301
ModifiedProcesses: nil,
302+
PrioritizedPIDs: []int{os.Getpid()},
302303
})
303304

304305
prometheusSrvClose := ServeHandler(ctx, logger, prometheusMetricsHandler(prometheusRegistry, logger), prometheusAddress, "prometheus")

0 commit comments

Comments
 (0)