forked from NixOS/nixpkgs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nixos/systemd-oomd: Add a new module + test
- Loading branch information
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,57 @@ | ||
{ config, lib, ... }: let | ||
|
||
cfg = config.systemd.oomd; | ||
|
||
in { | ||
options.systemd.oomd = { | ||
enable = lib.mkEnableOption "the systemd-oomd OOM killer" // { default = true; }; | ||
|
||
# Fedora enables the first and third option by default. See the 10-oomd-* files here: | ||
# https://src.fedoraproject.org/rpms/systemd/tree/acb90c49c42276b06375a66c73673ac351025597 | ||
enableRootSlice = lib.mkEnableOption "oomd on the root slice (-.slice)"; | ||
enableSystemSlice = lib.mkEnableOption "oomd on the system slice (system.slice)"; | ||
enableUserServices = lib.mkEnableOption "oomd on all user services ([email protected])"; | ||
|
||
extraConfig = lib.mkOption { | ||
type = with lib.types; attrsOf (oneOf [ str int bool ]); | ||
default = {}; | ||
example = lib.literalExpression ''{ DefaultMemoryPressureDurationSec = "20s"; }''; | ||
description = '' | ||
Extra config options for systemd-oomd. See man oomd.conf | ||
for available options. | ||
''; | ||
}; | ||
}; | ||
|
||
config = lib.mkIf cfg.enable { | ||
systemd.additionalUpstreamSystemUnits = [ | ||
"systemd-oomd.service" | ||
"systemd-oomd.socket" | ||
]; | ||
systemd.services.systemd-oomd.wantedBy = [ "multi-user.target" ]; | ||
|
||
environment.etc."systemd/oomd.conf".text = lib.generators.toINI {} { | ||
OOM = cfg.extraConfig; | ||
}; | ||
|
||
systemd.oomd.extraConfig.DefaultMemoryPressureDurationSec = lib.mkDefault "20s"; # Fedora default | ||
|
||
users.users.systemd-oom = { | ||
description = "systemd-oomd service user"; | ||
group = "systemd-oom"; | ||
isSystemUser = true; | ||
}; | ||
users.groups.systemd-oom = { }; | ||
|
||
systemd.slices."-".sliceConfig = lib.mkIf cfg.enableRootSlice { | ||
ManagedOOMSwap = "kill"; | ||
}; | ||
systemd.slices."system".sliceConfig = lib.mkIf cfg.enableSystemSlice { | ||
ManagedOOMSwap = "kill"; | ||
}; | ||
systemd.services."user@".serviceConfig = lib.mkIf cfg.enableUserServices { | ||
ManagedOOMMemoryPressure = "kill"; | ||
ManagedOOMMemoryPressureLimit = "50%"; | ||
}; | ||
}; | ||
} |
This file contains 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
This file contains 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,37 @@ | ||
import ./make-test-python.nix ({ pkgs, ... }: | ||
|
||
{ | ||
name = "systemd-oomd"; | ||
|
||
nodes.machine = { pkgs, ... }: { | ||
systemd.oomd.extraConfig.DefaultMemoryPressureDurationSec = "1s"; # makes the test faster | ||
# Kill cgroups when more than 1% pressure is encountered | ||
systemd.slices."-".sliceConfig = { | ||
ManagedOOMMemoryPressure = "kill"; | ||
ManagedOOMMemoryPressureLimit = "1%"; | ||
}; | ||
# A service to bring the system under memory pressure | ||
systemd.services.testservice = { | ||
serviceConfig.ExecStart = "${pkgs.coreutils}/bin/tail /dev/zero"; | ||
}; | ||
# Do not kill the backdoor | ||
systemd.services.backdoor.serviceConfig.ManagedOOMMemoryPressure = "auto"; | ||
|
||
virtualisation.memorySize = 1024; | ||
}; | ||
|
||
testScript = '' | ||
# Start the system | ||
machine.wait_for_unit("multi-user.target") | ||
machine.succeed("oomctl") | ||
# Bring the system into memory pressure | ||
machine.succeed("echo 0 > /proc/sys/vm/panic_on_oom") # NixOS tests kill the VM when the OOM killer is invoked - override this | ||
machine.succeed("systemctl start testservice") | ||
# Wait for oomd to kill something | ||
# Matches these lines: | ||
# systemd-oomd[508]: Killed /system.slice/systemd-udevd.service due to memory pressure for / being 3.26% > 1.00% for > 1s with reclaim activity | ||
machine.wait_until_succeeds("journalctl -b | grep -q 'due to memory pressure for'") | ||
''; | ||
}) |