Skip to content

Commit

Permalink
cpufreq: add option for setting the cpu max and min frequencies
Browse files Browse the repository at this point in the history
This adds a NixOS option for setting the CPU max and min frequencies
with `cpufreq`.  The two options that have been added are:

- `powerManagement.cpufreq.max`
- `powerManagement.cpufreq.min`

(cherry picked from commit b0f10d2)
(cherry picked from commit 46ecec8)
  • Loading branch information
cdepillabout committed Jan 6, 2019
1 parent 64e1f4b commit 0232f5c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
2 changes: 2 additions & 0 deletions nixos/modules/services/hardware/tlp.nix
Expand Up @@ -56,6 +56,8 @@ in

powerManagement.scsiLinkPolicy = null;
powerManagement.cpuFreqGovernor = null;
powerManagement.cpufreq.max = null;
powerManagement.cpufreq.min = null;

systemd.sockets."systemd-rfkill".enable = false;

Expand Down
72 changes: 54 additions & 18 deletions nixos/modules/tasks/cpu-freq.nix
Expand Up @@ -10,43 +10,79 @@ in
{
###### interface

options = {
options.powerManagement = {

powerManagement.cpuFreqGovernor = mkOption {
cpuFreqGovernor = mkOption {
type = types.nullOr types.str;
default = null;
example = "ondemand";
description = ''
Configure the governor used to regulate the frequence of the
available CPUs. By default, the kernel configures the
performance governor.
performance governor, although this may be overwritten in your
hardware-configuration.nix file.
Often used values: "ondemand", "powersave", "performance"
'';
};

cpufreq = {

max = mkOption {
type = types.nullOr types.ints.unsigned;
default = null;
example = 2200000;
description = ''
The maximum frequency the CPU will use. Defaults to the maximum possible.
'';
};

min = mkOption {
type = types.nullOr types.ints.unsigned;
default = null;
example = 800000;
description = ''
The minimum frequency the CPU will use.
'';
};
};

};


###### implementation

config = mkIf (!config.boot.isContainer && config.powerManagement.cpuFreqGovernor != null) {
config =
let
governorEnable = cfg.cpuFreqGovernor != null;
maxEnable = cfg.cpufreq.max != null;
minEnable = cfg.cpufreq.min != null;
enable =
!config.boot.isContainer &&
(governorEnable || maxEnable || minEnable);
in
mkIf enable {

boot.kernelModules = [ "cpufreq_${cfg.cpuFreqGovernor}" ];
boot.kernelModules = optional governorEnable "cpufreq_${cfg.cpuFreqGovernor}";

environment.systemPackages = [ cpupower ];
environment.systemPackages = [ cpupower ];

systemd.services.cpufreq = {
description = "CPU Frequency Governor Setup";
after = [ "systemd-modules-load.service" ];
wantedBy = [ "multi-user.target" ];
path = [ cpupower pkgs.kmod ];
unitConfig.ConditionVirtualization = false;
serviceConfig = {
Type = "oneshot";
RemainAfterExit = "yes";
ExecStart = "${cpupower}/bin/cpupower frequency-set -g ${cfg.cpuFreqGovernor}";
SuccessExitStatus = "0 237";
systemd.services.cpufreq = {
description = "CPU Frequency Setup";
after = [ "systemd-modules-load.service" ];
wantedBy = [ "multi-user.target" ];
path = [ cpupower pkgs.kmod ];
unitConfig.ConditionVirtualization = false;
serviceConfig = {
Type = "oneshot";
RemainAfterExit = "yes";
ExecStart = "${cpupower}/bin/cpupower frequency-set " +
optionalString governorEnable "--governor ${cfg.cpuFreqGovernor} " +
optionalString maxEnable "--max ${toString cfg.cpufreq.max} " +
optionalString minEnable "--min ${toString cfg.cpufreq.min} ";
SuccessExitStatus = "0 237";
};
};
};

};
}

0 comments on commit 0232f5c

Please sign in to comment.