Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zramSwap: by default disable zSwap and set sysctls #351002

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tamara-schmitz
Copy link
Contributor

I am adding two improvements to the zramSwap module. The module can configure a NixOS machine such that it creates a zram block device which is then mounted as swap. This is useful in situations where it makes sense to avoid storing swap onto a physical storage device. The kernel starts to swap anonymous memory pages (like application data) when a lot of memory is either requested by applications and / or when files from physical storage are accessed frequently while application memory is never touched. By creating this setup, anonymous pages can be compressed in memory instead of forcing the kernel to try other memory freeing techniques like compaction and reclaiming. Applications will pretty much always waste memory for example by allocating a variable on the stack when launching and initializing but never touching it again until the program exits. Also most program data is highly compressible.

These changes are taken and / or inspired by the Archwiki page on Zram and the discussions linked in this Pop_OS! PR.

So here are the main new options I added and enabled by default:

  • Add a kernel parameter that disables zswap: the idea of zswap is to keep memory pages that are intended to be swapped out to a physical disk and try compressing them in RAM instead. This is non-sense when your swap is also just compressed in RAM.
  • Set sysctls to trigger swapping and hence compression much earlier: these are tunables and the value you set is an opinion and not absolute. Their defaults however are assuming swap on a physical storage device.
    • vm.swappiness = 150 getting file data even from a fast SSD is gonna be much slower than decompressing some pages, so let's swap more frequently
    • vm.watermark_boost_factor = 0 watermark boosting kicks off kswapd when there is memory fragmentation and someone needs a continous area. Even when using a swapfile this seems to maybe cause longer pauses as seen here. But because swapping is gonna lead to more allocations this seems counterproductive. Let reclaim do its thing instead.
    • vm.watermark_scale_factor = 125 again let's kick off kswapd earlier rather than wait for memory starved situations as swapping to zram will increase pressure more.
    • vm.page-cluster = 0 prefetching multiple pages from swap seems silly when we are in RAM anyway

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.11 Release Notes (or backporting 23.11 and 24.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Oct 24, 2024
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Oct 24, 2024
nixos/modules/config/zram.nix Outdated Show resolved Hide resolved
nixos/modules/config/zram.nix Outdated Show resolved Hide resolved
@tamara-schmitz
Copy link
Contributor Author

I updated the commit to add to the zramSwap description. I also set the optimized sysctls to be disabled by default.

@Aleksanaa Aleksanaa requested a review from K900 December 6, 2024 13:46
nixos/modules/config/zram.nix Outdated Show resolved Hide resolved
@tamara-schmitz tamara-schmitz force-pushed the master branch 2 times, most recently from cf08ca5 to 3f2bace Compare December 9, 2024 18:47
This option when enabled, adds the `zswap.enabled=0` kernel
parameter when you also enable the zramSwap module.

Defaults to `true`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to have these

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that it is off by default, I guess. If someone does later add a zswap option in a different module, there ought to be a warning as @K900 suggested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The option search already shows the default value of options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be okay with "When zramSwap is enabled, this defaults to true." ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the disableZswap option, which should be removed entirely.

nixos/modules/config/zram.nix Outdated Show resolved Hide resolved
[zswap](https://www.kernel.org/doc/html/latest/admin-guide/mm/zswap.html).
Zswap applies more suitable heuristics in this case when using disk swap.

Zswap can be enabled using `boot.kernelParams = [ "zswap.enabled=1" ];`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is a good time to introduce a zswap module then? We could then direct users to the right options, or even add a warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense to do this in a second PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I really don't like is that we have information in the zram docs that's not very relevant to zram, and we could have automation take care of this instead of relying on users to read things (which they never do).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants