Skip to content

Commit

Permalink
nixos/zram: add zramSwap.memoryMax option
Browse files Browse the repository at this point in the history
This allows capping the total amount of memory that will be used for
zram-swap, in addition to the percentage-based calculation, which is
useful when blanket-applying a configuration to many machines.

This is based off the strategy used by Fedora for their rollout of
zram-swap-by-default in Fedora 33
(https://fedoraproject.org/wiki/Changes/SwapOnZRAM), which caps the
maximum amount of memory used for zram at 4GiB.

In future it might be good to port this to the systemd zram-generator,
instead of using this separate infrastructure.
  • Loading branch information
lukegb committed Nov 25, 2020
1 parent db63be9 commit ad62155
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion nixos/modules/config/zram.nix
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ in
'';
};

memoryMax = mkOption {
default = null;
type = with types; nullOr int;
description = ''
Maximum total amount of memory (in bytes) that can be used by the zram
swap devices.
'';
};

priority = mkOption {
default = 5;
type = types.int;
Expand Down Expand Up @@ -146,7 +155,12 @@ in
# Calculate memory to use for zram
mem=$(${pkgs.gawk}/bin/awk '/MemTotal: / {
print int($2*${toString cfg.memoryPercent}/100.0/${toString devicesCount}*1024)
value=int($2*${toString cfg.memoryPercent}/100.0/${toString devicesCount}*1024);
${lib.optionalString (cfg.memoryMax != null) ''
memory_max=int(${toString cfg.memoryMax}/${toString devicesCount});
if (value > memory_max) { value = memory_max }
''}
print value
}' /proc/meminfo)
${pkgs.util-linux}/sbin/zramctl --size $mem --algorithm ${cfg.algorithm} /dev/${dev}
Expand Down

0 comments on commit ad62155

Please sign in to comment.