@@ -1260,7 +1260,7 @@ func (a *agent) startReportingConnectionStats(ctx context.Context) {
1260
1260
}
1261
1261
}
1262
1262
1263
- var exemptProcesses = []string {"coder" }
1263
+ var prioritizedProcs = []string {"coder" }
1264
1264
1265
1265
func (a * agent ) manageProcessPriorityLoop (ctx context.Context ) {
1266
1266
ticker := time .NewTicker (time .Minute )
@@ -1269,7 +1269,7 @@ func (a *agent) manageProcessPriorityLoop(ctx context.Context) {
1269
1269
const (
1270
1270
procDir = agentproc .DefaultProcDir
1271
1271
niceness = 10
1272
- oomScoreAdj = - 1000
1272
+ oomScoreAdj = 100
1273
1273
)
1274
1274
1275
1275
if val := a .envVars [EnvProcMemNice ]; val == "" || runtime .GOOS != "linux" {
@@ -1284,7 +1284,7 @@ func (a *agent) manageProcessPriorityLoop(ctx context.Context) {
1284
1284
for {
1285
1285
select {
1286
1286
case <- ticker .C :
1287
- procs , err := agentproc .List (a .filesystem , agentproc .DefaultProcDir )
1287
+ procs , err := agentproc .List (a .filesystem , a . syscaller , agentproc .DefaultProcDir )
1288
1288
if err != nil {
1289
1289
a .logger .Error (ctx , "failed to list procs" ,
1290
1290
slog .F ("dir" , agentproc .DefaultProcDir ),
@@ -1295,31 +1295,52 @@ func (a *agent) manageProcessPriorityLoop(ctx context.Context) {
1295
1295
for _ , proc := range procs {
1296
1296
// Trim off the path e.g. "./coder" -> "coder"
1297
1297
name := filepath .Base (proc .Name ())
1298
- if slices .Contains (exemptProcesses , name ) {
1299
- a .logger .Debug (ctx , "skipping exempt process" ,
1298
+ // If the process is prioritized we should adjust
1299
+ // it's oom_score_adj and avoid lowering its niceness.
1300
+ if slices .Contains (prioritizedProcs , name ) {
1301
+ err = proc .SetOOMAdj (oomScoreAdj )
1302
+ if err != nil {
1303
+ a .logger .Error (ctx , "unable to set proc oom_score_adj" ,
1304
+ slog .F ("name" , proc .Name ()),
1305
+ slog .F ("pid" , proc .PID ),
1306
+ slog .F ("oom_score_adj" , oomScoreAdj ),
1307
+ slog .Error (err ),
1308
+ )
1309
+ continue
1310
+ }
1311
+
1312
+ a .logger .Debug (ctx , "decreased process oom_score" ,
1300
1313
slog .F ("name" , proc .Name ()),
1301
1314
slog .F ("pid" , proc .PID ),
1315
+ slog .F ("oom_score_adj" , oomScoreAdj ),
1302
1316
)
1303
1317
continue
1304
1318
}
1305
1319
1306
- err := proc .SetNiceness (a .syscaller , niceness )
1320
+ score , err := proc .Nice (a .syscaller )
1307
1321
if err != nil {
1308
- a .logger .Error (ctx , "unable to set proc niceness" ,
1322
+ a .logger .Error (ctx , "unable to get proc niceness" ,
1309
1323
slog .F ("name" , proc .Name ()),
1310
1324
slog .F ("pid" , proc .PID ),
1311
- slog .F ("niceness" , niceness ),
1312
1325
slog .Error (err ),
1313
1326
)
1314
1327
continue
1315
1328
}
1329
+ if score != 20 {
1330
+ a .logger .Error (ctx , "skipping process due to custom niceness" ,
1331
+ slog .F ("name" , proc .Name ()),
1332
+ slog .F ("pid" , proc .PID ),
1333
+ slog .F ("niceness" , score ),
1334
+ )
1335
+ continue
1336
+ }
1316
1337
1317
- err = proc .SetOOMAdj ( oomScoreAdj )
1338
+ err = proc .SetNiceness ( a . syscaller , niceness )
1318
1339
if err != nil {
1319
- a .logger .Error (ctx , "unable to set proc oom_score_adj " ,
1340
+ a .logger .Error (ctx , "unable to set proc niceness " ,
1320
1341
slog .F ("name" , proc .Name ()),
1321
1342
slog .F ("pid" , proc .PID ),
1322
- slog .F ("oom_score_adj " , oomScoreAdj ),
1343
+ slog .F ("niceness " , niceness ),
1323
1344
slog .Error (err ),
1324
1345
)
1325
1346
continue
@@ -1329,7 +1350,6 @@ func (a *agent) manageProcessPriorityLoop(ctx context.Context) {
1329
1350
slog .F ("name" , proc .Name ()),
1330
1351
slog .F ("pid" , proc .PID ),
1331
1352
slog .F ("niceness" , niceness ),
1332
- slog .F ("oom_score_adj" , oomScoreAdj ),
1333
1353
)
1334
1354
}
1335
1355
case <- ctx .Done ():
0 commit comments