|
| 1 | +# Store and restore /sys/devices/system/cpu/intel_pstate/max_perf_pct state. |
| 2 | +# |
| 3 | +# Background: My laptop has terrible thermal design and a very noisy fan. I |
| 4 | +# generally operate it with max_perf_pct=70 and would like to automate this. |
| 5 | +# This module adds a script called max_perf_pct that can be invoked to adjust |
| 6 | +# the setting (tip: bind to a keyboard shortcut). |
| 7 | + |
| 8 | +{ config, lib, pkgs, ... }: |
| 9 | + |
| 10 | +let |
| 11 | + stateFile = "/var/lib/max_perf_pct/state"; |
| 12 | +in |
| 13 | +{ |
| 14 | + environment.systemPackages = with pkgs; [ my.max_perf_pct ]; |
| 15 | + |
| 16 | + systemd.services.max_perf_pct = { |
| 17 | + description = "(Re)store max_perf_pct Sysfs Attribute"; |
| 18 | + wantedBy = [ "multi-user.target" ]; |
| 19 | + #path = [ "/run/wrappers" /* For sudo. Unneeded when run as root. */ ]; |
| 20 | + serviceConfig.ExecStart = pkgs.writeScript "max_perf_pct_start" '' |
| 21 | + #!${pkgs.bash}/bin/sh |
| 22 | + test -f "${stateFile}" || exit 0 |
| 23 | + saved_value=$(cat "${stateFile}" 2>/dev/null) |
| 24 | + if ! test "$saved_value" -eq "$saved_value" 2>/dev/null; then |
| 25 | + echo "error: cannot load state from ${stateFile} because it is empty or has non-numeric contents: $saved_value" |
| 26 | + else |
| 27 | + echo "Restoring state from ${stateFile}: $saved_value" |
| 28 | + ${pkgs.my.max_perf_pct}/bin/max_perf_pct "$saved_value" |
| 29 | + fi |
| 30 | + ''; |
| 31 | + serviceConfig.ExecStop = pkgs.writeScript "max_perf_pct_stop" '' |
| 32 | + #!${pkgs.bash}/bin/sh |
| 33 | + saved_value=$(${pkgs.my.max_perf_pct}/bin/max_perf_pct) |
| 34 | + echo "Saving state to ${stateFile}: $saved_value" |
| 35 | + echo "$saved_value" > "${stateFile}" |
| 36 | + ''; |
| 37 | + serviceConfig.SyslogIdentifier = "max_perf_pct"; |
| 38 | + serviceConfig.Type = "oneshot"; |
| 39 | + serviceConfig.ProtectSystem = "strict"; |
| 40 | + serviceConfig.RemainAfterExit = true; |
| 41 | + serviceConfig.StateDirectory = "max_perf_pct"; |
| 42 | + }; |
| 43 | +} |
0 commit comments