Skip to content

Commit

Permalink
refactor: make power management compatible with zramSwap
Browse files Browse the repository at this point in the history
Dynamically enable zramSwap if no swapDevices exist.
  • Loading branch information
flexiondotorg committed Aug 12, 2024
1 parent 655eac3 commit 983721d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
33 changes: 28 additions & 5 deletions nixos/_mixins/features/power-management/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@
let
isIntelCPU = config.hardware.cpu.intel.updateMicrocode;
isLaptop = hostname != "vader" && hostname != "phasma" && hostname != "revan";
GNOMEIsenabled = config.services.xserver.desktopManager.gnome.enable == true;
isThinkpad = hostname == "tanis" || hostname == "sidious"|| hostname == "shaa";
usePowerProfiles = config.services.xserver.desktopManager.gnome.enable || config.services.xserver.desktopManager.pantheon.enable;
in
lib.mkIf isInstall {
# Power Management strategy:
# - If a desktop environment is enabled the supports the power-profiles-daemon, then use the power profiles daemon.
# - Otherwise, use auto-cpufreq.
# - If zramSwap is enabled, then disable power management features that conflict with zram.
# - Always disable TLP and Powertop because they conflict with auto-cpufreq or agressively suspend USB devices
# - Disable USB autosuspend on desktop workstations
# - Enable thermald on Intel CPUs
# - Thinkpads have a battery threshold charging either via the GNOME extension or auto-cpufreq

# Disable USB autosuspend on desktop always on power workstations
boot.kernelParams = lib.optionals (!isLaptop) [
"usbcore.autosuspend=-1"
];

# Install Battery Threshold GNOME extensions for Thinkpads
environment.systemPackages = with pkgs; lib.optionals (isThinkpad && GNOMEIsenabled) [
environment.systemPackages = with pkgs; lib.optionals (isThinkpad && config.services.xserver.desktopManager.gnome.enable) [
gnomeExtensions.thinkpad-battery-threshold
];

powerManagement.powertop.enable = lib.mkDefault false;

programs = {
auto-cpufreq = {
enable = (!GNOMEIsenabled && isLaptop);
enable = (!usePowerProfiles && isLaptop);
settings = {
battery = {
governor = "powersave";
Expand All @@ -32,12 +49,18 @@ lib.mkIf isInstall {
};
};
};

services = {
# Only enable power-profiles-daemon if GNOME is enabled
power-profiles-daemon.enable = (GNOMEIsenabled && isLaptop);
# Only enable power-profiles-daemon if the desktop environment supports it
power-profiles-daemon.enable = usePowerProfiles;
# Only enable thermald on Intel CPUs
thermald.enable = isIntelCPU;
# Disable TLP because it conflicts with auto-cpufreq
tlp.enable = lib.mkForce false;
};

# Disable hiberate, hybrid-sleep and suspend-then-hibernate when zram swap is enabled.
systemd.targets.hibernate.enable = !config.zramSwap.enable;
systemd.targets.hybrid-sleep.enable = !config.zramSwap.enable;
systemd.targets.suspend-then-hibernate.enable = !config.zramSwap.enable;
}
15 changes: 8 additions & 7 deletions nixos/_mixins/features/zram/default.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{ pkgs, ... }:
{ config, lib, pkgs, ... }:
let
# Only enable zram swap if no swap devices are configured
usezramSwap = builtins.length config.swapDevices == 0;
in
{
# Keep zram swap (lz4) latency in check
boot.kernel.sysctl = {
boot.kernel.sysctl = lib.mkIf usezramSwap {
"vm.page-cluster" = 1;
};

# Enable Multi-Gen LRU:
# - https://docs.kernel.org/next/admin-guide/mm/multigen_lru.html
# - Inspired by: https://github.com/hakavlad/mg-lru-helper
systemd.services."mglru" = {
systemd.services."mglru" = lib.mkIf usezramSwap {
enable = true;
wantedBy = [ "basic.target" ];
script = ''
Expand All @@ -23,15 +27,12 @@
};
};

# Disable hiberate and hybrid-sleep when using zram.
systemd.targets.hibernate.enable = false;
systemd.targets.hybrid-sleep.enable = false;
# Enable zram
# - https://github.com/ecdye/zram-config/blob/main/README.md#performance
# - https://www.reddit.com/r/Fedora/comments/mzun99/new_zram_tuning_benchmarks/
# - https://linuxreviews.org/Zram
zramSwap = {
algorithm = "lz4";
enable = true;
enable = usezramSwap;
};
}
2 changes: 0 additions & 2 deletions nixos/phasma/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
];
kernelPackages = lib.mkForce pkgs.linuxPackages_xanmod_latest;
kernelParams = [
# Disable USB autosuspend on workstations
"usbcore.autosuspend=-1"
"video=DP-1:3440x1440@60"
"video=DP-2:1920x1080@60"
"video=HDMI-A-1:1920x1080@60"
Expand Down
2 changes: 0 additions & 2 deletions nixos/vader/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
];
kernelPackages = lib.mkForce pkgs.linuxPackages_xanmod_latest;
kernelParams = [
# Disable USB autosuspend on workstations
"usbcore.autosuspend=-1"
"video=DP-1:2560x2880@60"
"video=DP-2:2560x2880@60"
"video=DP-3:1920x1080@60"
Expand Down

0 comments on commit 983721d

Please sign in to comment.