From 8924469660600193d5b253307670a5bbbe2742cb Mon Sep 17 00:00:00 2001 From: Nadia Santalla Date: Thu, 30 Jan 2025 13:43:39 +0100 Subject: [PATCH] feat: crocochrome: try to use choom to lower oom_score_adj, failing gracefully --- cmd/crocochrome/main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/crocochrome/main.go b/cmd/crocochrome/main.go index 634f440..fd83d6a 100644 --- a/cmd/crocochrome/main.go +++ b/cmd/crocochrome/main.go @@ -5,6 +5,8 @@ import ( "log/slog" "net/http" "os" + "os/exec" + "strconv" "github.com/grafana/crocochrome" crocohttp "github.com/grafana/crocochrome/http" @@ -18,6 +20,20 @@ func main() { Level: slog.LevelDebug, })) + const oomScore = -500 + if out, err := choom(oomScore); err != nil { + logger.Error( + "Error changing OOM score, assuming this is not a production environment and continuing anyway", + "err", err, + "choomOutput", string(out), + ) + } else { + logger.Info( + "Main process OOM score adjusted successfully", + "oomScore", oomScore, + ) + } + mux := http.NewServeMux() registry := prometheus.NewRegistry() @@ -56,3 +72,9 @@ func main() { logger.Error("Setting up HTTP listener", "err", err) } } + +// choom runs the choom helper (source included in this repo) to lower the current process OOM score. +func choom(score int) ([]byte, error) { + choom := exec.Command("choom", strconv.Itoa(os.Getpid()), strconv.Itoa(score)) + return choom.CombinedOutput() +}