From 9eb32b0429de786281a9f7c71b1f3880835d4410 Mon Sep 17 00:00:00 2001 From: Alex Wu Date: Thu, 2 Jan 2025 15:49:01 -0800 Subject: [PATCH] Change container workload's default OOM Score This makes the container workload a very likely candidate for the OOMKiller when the CS VM is experiencing out of memory issues. --- launcher/container_runner.go | 13 +++++++++++++ launcher/launcher/main.go | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/launcher/container_runner.go b/launcher/container_runner.go index c279d90a..ba79e4d3 100644 --- a/launcher/container_runner.go +++ b/launcher/container_runner.go @@ -75,6 +75,9 @@ const ( defaultRefreshJitter = 0.1 ) +// Default OOM score for a CS container. +const defaultOOMScore = 1000 + // NewRunner returns a runner. func NewRunner(ctx context.Context, cdClient *containerd.Client, token oauth2.Token, launchSpec spec.LaunchSpec, mdsClient *metadata.Client, tpm io.ReadWriteCloser, logger logging.Logger, serialConsole *os.File) (*ContainerRunner, error) { image, err := initImage(ctx, cdClient, launchSpec, token) @@ -163,6 +166,7 @@ func NewRunner(ctx context.Context, cdClient *containerd.Client, token oauth2.To oci.WithHostNamespace(specs.NetworkNamespace), oci.WithEnv([]string{fmt.Sprintf("HOSTNAME=%s", hostname)}), withRlimits(rlimits), + withOOMScoreAdj(defaultOOMScore), } if launchSpec.DevShmSize != 0 { specOpts = append(specOpts, oci.WithDevShmSize(launchSpec.DevShmSize)) @@ -186,6 +190,7 @@ func NewRunner(ctx context.Context, cdClient *containerd.Client, token oauth2.To if err != nil { return nil, &RetryableError{err} } + // Container process Args length should be strictly longer than the Cmd // override length set by the operator, as we want the Entrypoint filed // to be mandatory for the image. @@ -743,3 +748,11 @@ func withRlimits(rlimits []specs.POSIXRlimit) oci.SpecOpts { return nil } } + +// Set the container process's OOM score. +func withOOMScoreAdj(oomScore int) oci.SpecOpts { + return func(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error { + s.Process.OOMScoreAdj = &oomScore + return nil + } +} diff --git a/launcher/launcher/main.go b/launcher/launcher/main.go index 1d0eca12..e749f712 100644 --- a/launcher/launcher/main.go +++ b/launcher/launcher/main.go @@ -75,7 +75,7 @@ func main() { logger, err = logging.NewLogger(ctx) if err != nil { - log.Default().Printf("failed to initialize logging") + log.Default().Printf("failed to initialize logging: %v", err) exitCode = failRC log.Default().Printf("%s, exit code: %d (%s)\n", exitMessage, exitCode, rcMessage[exitCode]) return