Difference between revisions of "CPU frequency scaling"

From ArchWiki
Jump to: navigation, search
(even if this were in the scope of the article - which it isn't - there's zero references to back up the claimed statements)
 
(157 intermediate revisions by 46 users not shown)
Line 1: Line 1:
 
[[Category:Power management]]
 
[[Category:Power management]]
 
[[Category:CPU]]
 
[[Category:CPU]]
[[Category:Daemons and system services]]
+
[[ar:CPU frequency scaling]]
[[cs:CPU Frequency Scaling]]
+
[[cs:CPU frequency scaling]]
 
[[de:Cpufrequtils]]
 
[[de:Cpufrequtils]]
[[el:CPU Frequency Scaling]]
+
[[el:CPU frequency scaling]]
[[es:CPU Frequency Scaling]]
+
[[es:CPU frequency scaling]]
 
[[fr:Cpufreq]]
 
[[fr:Cpufreq]]
[[it:CPU Frequency Scaling]]
+
[[it:CPU frequency scaling]]
[[pt:CPU Frequency Scaling]]
+
[[ja:CPU 周波数スケーリング]]
[[ru:CPU Frequency Scaling]]
+
[[pt:CPU frequency scaling]]
[[sk:CPU Frequency Scaling]]
+
[[ru:CPU frequency scaling]]
[[tr:Işlemci_frekansını_ölçekleme]]
+
[[sk:CPU frequency scaling]]
[[zh-CN:CPU Frequency Scaling]]
+
[[tr:Işlemci frekansını ölçekleme]]
{{Article summary start}}
+
[[zh-cn:CPU frequency scaling]]
{{Article summary text|An overview of the popular userspace tools for the kernel [http://www.kernel.org/pub/linux/utils/kernel/cpufreq/cpufreq-info.html CPUfreq] subsystem.}}
+
{{Related articles start}}
{{Article summary heading|Related}}
+
{{Related|Power saving}}
{{Article summary wiki|Laptop Mode Tools|Laptop_Mode_Tools}}
+
{{Related|Laptop Mode Tools}}
{{Article summary wiki|pm-utils}}
+
{{Related|pm-utils}}
{{Article summary wiki|PHC}}
+
{{Related|PHC}}
{{Article summary end}}
+
{{Related articles end}}
  
{{Accuracy|This article needs cleanup as far as kernel cpufreq and userspace cpufrequtils and cpupower are concerned.}}
+
CPU frequency scaling enables the operating system to scale the CPU frequency up or down in order to save power. CPU frequencies can be scaled automatically depending on the system load, in response to ACPI events, or manually by userspace programs.
  
cpufreq refers to the kernel infrastructure that implements CPU frequency scaling. This technology enables the operating system to scale the CPU speed up or down in order to save power. CPU frequencies can be scaled automatically depending on the system load, in response to ACPI events, or manually by userspace programs.
+
CPU frequency scaling is implemented in the Linux kernel, the infrastructure is called ''cpufreq''. Since kernel 3.4 the necessary modules are loaded automatically and the recommended [[#Scaling governors|ondemand governor]] is enabled by default. However, userspace tools like [[#cpupower|cpupower]], [[acpid]], [[Laptop Mode Tools]], or GUI tools provided for your desktop environment, may still be used for advanced configuration.
  
Since kernel 3.4 the necessary modules are loaded automatically and the recommended [[CPU_Frequency_Scaling#Scaling_governors|ondemand governor]] is enabled by default. However, userspace applications like [[#cpupower|cpupower]], [[acpid]], [[laptop-mode-tools]], or GUI tools provided for your desktop environment, may still be used for advanced configuration.
+
== Userspace tools ==
  
==cpupower==
+
=== thermald ===
{{Pkg|cpupower}} is a set of userspace utilities designed to assist with ''CPU frequency scaling''. The package is not required to use scaling, but is highly recommended because it provides useful command-line utilities and a service to change the governor at boot (see [[#Daemon]] below).
+
  
{{Tip|Make sure you edit {{ic|/etc/conf.d/cpupower}} and set the governor/frequencies you desire before starting cpupower daemon}}
+
{{AUR|thermald}} is a Linux daemon used to prevent the overheating of platforms. This daemon monitors temperature and applies compensation using available cooling methods.
  
{{Note|Using kernel 3.5 or later additional features in cpupower do no longer work, because support for them has been removed from the kernel. Do not set the smp_scheduler or mc_scheduler values in the configuration file, or starting the daemon will fail.}}
+
By default, it monitors CPU temperature using available CPU digital temperature sensors and maintains CPU temperature under control, before HW takes aggressive correction action. If there is a skin temperature sensor in thermal sysfs, then it tries to keep skin temperature under 45C.
  
==Configuration==
+
=== i7z ===
Configuring CPU scaling is a 3-part process:
+
{{Pkg|i7z}} is an i7 (and now i3, i5) (CPU) reporting tool for Linux. It can be launched from a Terminal with the command {{ic|i7z}} or as GUI with {{ic|i7z-gui}}.
# Load appropriate CPU frequency driver
+
# Load desired scaling governor(s)
+
# Select a method to manage switching and tuning governor(s):
+
#* manually via {{ic|/sys}} interface or {{Pkg|cpupower}},
+
#* the cpupower [[#Daemon]],
+
#* [[Laptop Mode Tools]],
+
#* [[acpid]] events,
+
#* desktop enviroment tools/applets,
+
#* or some combination of the above.
+
  
=== CPU frequency driver ===
+
=== cpupower ===
{{Note|The CPU frequency driver for your CPU is automatically loaded since kernel 3.4, therefore loading it manually - as described in this section - should not be necessary anymore.}}
+
In order for frequency scaling to work properly, the operating system must first know the limits of the CPU(s).  To accomplish this, a kernel module is loaded that can read and manage the specifications of the CPU(s).  Note that these modules may need related features enabled in BIOS which may be labeled as: ''Speedstep'', ''Cool and Quiet'', ''PowerNow!'', or ''ACPI''.
+
  
If you have a 64-bit processor, you will very likely want either '''{{ic|acpi_cpufreq}}''' for Intel processors or '''{{ic|powernow-k8}}''' for AMD K8/K10 processors (Athlon 64, Opteron, and Phenom).  These modules are built for both 32 and 64-bit kernels so even if you run a 32-bit kernel on your 64-bit hardware they are probably still the ones you want.
+
{{Pkg|cpupower}} is a set of userspace utilities designed to assist with CPU frequency scaling. The package is not required to use scaling, but is highly recommended because it provides useful command-line utilities and a [[systemd]] service to change the governor at boot.
  
To see a full list of available drivers, run the following:
+
The configuration file for ''cpupower'' is located in {{ic|/etc/default/cpupower}}. This configuration file is read by a bash script in {{ic|/usr/lib/systemd/scripts/cpupower}} which is activated by ''systemd'' with {{ic|cpupower.service}}. You may want to enable {{ic|cpupower.service}} to start at boot.
  
$ ls /lib/modules/$(uname -r)/kernel/drivers/cpufreq/
+
== CPU frequency driver ==
  
The following table is a partial list of available frequency drivers (Adapted from kernel source file {{ic|<kernel source>/arch/x86/kernel/cpu/cpufreq/Kconfig}}).
+
{{Note|
{| border="1"
+
* The native CPU module is loaded automatically.
 +
* The {{ic|pstate}} power scaling driver is used automatically for modern Intel CPUs instead of the other drivers below. This driver takes priority over other drivers and is built-in as opposed to being a module. This driver is currently automatically used for Sandy Bridge and newer CPUs. If you encounter a problem while using this driver, add {{ic|intel_pstate=disable}} to your kernel line. You can use the same user space utilities with this driver, '''but cannot control it'''.
 +
* Even P State behavior mentioned above can be influenced with {{ic|/sys/devices/system/cpu/intel_pstate}}, e.g. Intel Turbo Boost can be deactivated with {{ic|# echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo}} for keeping CPU-Temperatures low.
 +
* Additional control for modern Intel CPUs is available with the [https://01.org/linux-thermal-daemon Linux Thermal Daemon] (available as {{AUR|thermald}}), which proactively controls thermal using P-states, T-states, and the Intel power clamp driver. thermald can also be used for older Intel CPUs. If the latest drivers are not available, then the daemon will revert to x86 model specific registers and the Linux ‘cpufreq subsystem’ to control system cooling.
 +
}}
 +
 
 +
''cpupower'' requires modules to know the limits of the native CPU:
 +
 
 +
{| class="wikitable"
 
!Module!!Description
 
!Module!!Description
 
|-
 
|-
| acpi_cpufreq || CPUFreq driver which utilizes the ACPI Processor Performance States. This driver also supports Intel Enhanced SpeedStep (previously supported by the deprecated speedstep-centrino module).
+
| intel_pstate || This driver implements a scaling driver with an internal governor for Intel Core (Sandy Bridge and newer) processors.
 
|-
 
|-
| speedstep-ich || CPUFreq driver for certain mobile Intel Pentium III (Coppermine), all mobile Intel Pentium III-M (Tualatin) and all mobile Intel Pentium 4 P4-M on systems which have an Intel ICH2, ICH3 or ICH4 southbridge.
+
| acpi-cpufreq  || CPUFreq driver which utilizes the ACPI Processor Performance States. This driver also supports the Intel Enhanced SpeedStep (previously supported by the deprecated speedstep-centrino module).
 
|-
 
|-
| speedstep-smi || CPUFreq driver for certain mobile Intel Pentium III (Coppermine), all mobile Intel Pentium III-M (Tualatin) on systems which have an Intel 440BX/ZX/MX southbridge.
+
| speedstep-lib || CPUFreq driver for Intel SpeedStep-enabled processors (mostly Atoms and older Pentiums (< 3))  
 
|-
 
|-
| powernow-k8  || CPUFreq driver for K8/K10 Athlon64/Opteron/Phenom processors.
+
| powernow-k8  || CPUFreq driver for K8/K10 Athlon 64/Opteron/Phenom processors. Since Linux 3.7 'acpi-cpufreq' will automatically be used for more modern CPUs from this family.
 
|-
 
|-
| powernow-k7   || CPUFreq driver for mobile AMD K7 mobile processors.  
+
| pcc-cpufreq   || This driver supports Processor Clocking Control interface by Hewlett-Packard and Microsoft Corporation which is useful on some ProLiant servers.
 
|-
 
|-
| cpufreq-nforce2 || CPUFreq driver for FSB changing on nVidia nForce2 platforms. (AMD K7, Socket A)
+
| p4-clockmod   || CPUFreq driver for Intel Pentium 4/Xeon/Celeron processors which lowers the CPU temperature by skipping clocks. (You probably want to use a SpeedStep driver instead.)
|-
+
| pcc-cpufreq  || This driver supports Processor Clocking Control interface by Hewlett-Packard and Microsoft Corporation which is useful on some Proliant servers.
+
|-
+
| p4_clockmod   || CPUFreq driver for Intel Pentium 4 / Xeon / Celeron processors.  When enabled it will lower CPU temperature by skipping clocks.<br>You probably want to use a Speedstep driver instead.
+
 
|}
 
|}
<br>
 
To load the CPU frequency driver manually:
 
# modprobe acpi_cpufreq
 
Note that if you attempt to load the wrong module you will get an error such as
 
FATAL: Error inserting acpi_cpufreq ([...]/acpi-cpufreq.ko): No such device
 
  
Once the appropriate cpufreq driver is loaded, detailed information about the CPU(s) can be displayed by running:
+
To see a full list of available modules, run:
{{hc
+
|$ cpupower frequency-info
+
|<nowiki>
+
analyzing CPU 0:
+
  driver: acpi-cpufreq
+
  CPUs which run at the same hardware frequency: 0 1 2 3
+
  CPUs which need to have their frequency coordinated by software: 0
+
  maximum transition latency: 10.0 us.
+
  hardware limits: 1.20 GHz - 2.40 GHz
+
  available frequency steps: 2.40 GHz, 2.40 GHz, 2.27 GHz, 2.13 GHz, 2.00 GHz, 1.87 GHz, 1.73 GHz, 1.60 GHz, 1.47 GHz, 1.33 GHz, 1.20 GHz
+
  available cpufreq governors: ondemand, performance
+
  current policy: frequency should be within 1.20 GHz and 2.40 GHz.
+
                  The governor "ondemand" may decide which speed to use
+
                  within this range.
+
  current CPU frequency is 1.20 GHz.
+
  boost state support:
+
    Supported: yes
+
    Active: yes
+
    25500 MHz max turbo 4 active cores
+
    25500 MHz max turbo 3 active cores
+
    25500 MHz max turbo 2 active cores
+
    25500 MHz max turbo 1 active cores
+
</nowiki>}}
+
  
To load the driver automatically at start-up, add a file specifying the appropriate driver to the {{ic|/etc/modules-load.d/}} directory. For example:
+
$ ls /usr/lib/modules/$(uname -r)/kernel/drivers/cpufreq/
  
{{hc|/etc/modules-load.d/cpufreq.conf|<nowiki>
+
Load the appropriate module (see [[Kernel modules]] for details). Once the appropriate cpufreq driver is loaded, detailed information about the CPU(s) can be displayed by running
# Load cpufreq driver
+
acpi_cpufreq</nowiki>}}
+
  
=== Scaling governors ===
+
$ cpupower frequency-info
  
Governors can be thought of as pre-configured power schemes for the CPU. Some of the governors must be loaded as kernel modules in order to be seen by user space programs. One may load as many governors as desired (only one will be active on a CPU at any given time).
+
=== Setting maximum and minimum frequencies ===
  
Available governors:
+
In rare cases, it may be necessary to manually set maximum and minimum frequencies.
  
;{{ic|cpufreq_ondemand}} ''(default and recommended)'': Dynamically switches between the CPU(s) available clock speeds based on system load
+
To set the maximum clock frequency (''clock_freq'' is a clock frequency with units: GHz, MHz):
;{{ic|cpufreq_performance}}: The performance governor runs the CPU(s) at maximum clock speed
+
;{{ic|cpufreq_conservative}}: Similar to {{ic|ondemand}}, but the CPU(s) clock speed switches gradually through all its available frequencies based on system load
+
;{{ic|cpufreq_powersave}}: Runs the CPU(s) at minimum speed
+
;{{ic|cpufreq_userspace}}: Manually configured clock speeds by user
+
  
For Desktops and most systems, the ondemand governor can provide the best compromise between heat emission, power consumption, performance, and manageability. Since it is the default and built into the kernel, loading the CPU frequency driver should be sufficient to activate it.
+
# cpupower frequency-set -u ''clock_freq''
For Laptops or other mobile systems, the conservative governor can possibly provide significant savings in power consumption.
+
  
The governors {{ic|ondemand}} and {{ic|performance}} are built into the kernel and do not need to be loaded as modules. If you want to use one of the other governors, you have to load them with {{ic|modprobe}}. For example:
+
To set the minimum clock frequency:
# modprobe cpufreq_powersave
+
# modprobe cpufreq_userspace
+
  
Or, add a file specifying the desired governor(s) to {{ic|/etc/modules-load.d/}} and reboot.
+
# cpupower frequency-set -d ''clock_freq''
  
{{hc|/etc/modules-load.d/cpufreq_gov.conf|<nowiki>
+
To set the CPU to run at a specified frequency:
# Load cpufreq governors
+
cpufreq_powersave
+
cpufreq_userspace</nowiki>}}
+
  
{{Note|Remember that if you are using a kernel older than 3.4 (e.g. {{pkg|linux-lts}}) the needed [[#CPU frequency driver]] should be added to the top of the list to ensure it is loaded at boot.}}
+
# cpupower frequency-set -f ''clock_freq''
  
To see which governors have been loaded and which frequencies are supported:
+
{{Note|
# cpupower frequency-info
+
* To adjust for only a single CPU core, append {{ic|-c ''core_number''}}.
 +
* The governor, maximum and minimum frequencies can be set in {{ic|/etc/default/cpupower}}.
 +
}}
  
Manually set the governor by running the {{ic|cpupower}} command (as root). For example:
+
== Scaling governors ==
# cpupower frequency-set -g performance
+
  
{{Note|These settings will not be preserved after a reboot/shutdown. See the [[#Daemon]] section below for configuring cpufreq governors at boot.}}
+
{{Note|The modern pstate driver only supports the performance and powersave governors and the performance governor [http://www.phoronix.com/scan.php?page&#61;news_item&px&#61;MTM3NDQ should give better power saving functionality than the old ondemand governor]. }}
  
Additional options such as upper and lower frequency bounds used by the governor can optionally be set by using the {{ic|-u}} and {{ic|-d}} options. For example, to set the second core's upper frequency bound as 2.00GHz and its lower bound as 1.00 GHz:
+
Governors (see table below) are power schemes for the CPU. Only one may be active at a time. For details, see the [https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt kernel documentation] in the kernel source.
# cpupower -c 1 frequency-set -g ondemand -u 2.00Ghz -d 1.00Ghz
+
  
To manually set a processor to a specific frequency the userspace governor is used.  For example, to set core 0 to 2.50Ghz and core 1 to 800Mhz:
+
{| class="wikitable"
# cpupower -c 0 frequency-set -f 2.50Ghz
+
!Governor!!Description
  # cpupower -c 1 frequency-set -f 800Mhz
+
|-
 +
| ondemand    || Dynamically switch between CPU(s) available if at 95% cpu load
 +
|-
 +
| performance || Run the cpu at max frequency
 +
|-
 +
| conservative || Dynamically switch between CPU(s) available if at 75% load
 +
|-
 +
| powersave    || Run the cpu at the minimum frequency
 +
|-
 +
| userspace    || Run the cpu at user specified frequencies
 +
|}
  
Run {{ic|cpupower --help}} or {{ic|man cpupower}} for more information.
+
Depending on the scaling driver, one of these governors will be loaded by default:
 +
* {{ic|ondemand}} for AMD and older Intel CPU.
 +
* {{ic|powersave}} for Intel Sandy Bridge and newer CPU.
  
For those who would like a GUI for setting governors or frequency there is [[trayfreq]], a GTK+ application that sits in the system tray.
+
To activate a particular governor, run:
  
The monitoring of CPU clock in real-time is accomplished by running:
+
  # cpupower frequency-set -g ''governor''
  $ watch grep \"cpu MHz\" /proc/cpuinfo
+
  
====Improving on-demand performance====
+
{{Note|
 +
* To adjust for only a single CPU core, append {{ic|-c ''core_number''}} to the command above.
 +
* Activating a governor requires that specific [[kernel module]] (named {{ic|cpufreq_''governor''}}) is loaded. As of kernel 3.4, these modules are loaded automatically.
 +
}}
  
With the out-of-the-box configuration, the ondemand governor will result in a slight but measurable and noticeable loss of performance. It will not clock up the CPU when it is at lower than 95% usage, and will sample the usage at the fastest possible frequency when at full clock speeds in order to clock back down as soon as possible.
+
Alternatively, you can activate a governor on every available CPU manually:
 +
# echo ''governor'' | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor >/dev/null
  
Tuning the governor for your needs can reduce the performance loss to the point where it is negligible/non-existent if you are willing to lose a lot of the power savings. If you just want to save power while idle, lowering the up_threshold to 11% and raising the sample_down_factor by an order of magnitude can accomplish this.
+
{{Tip|To monitor cpu speed in real time, run:
 +
$ watch grep \"cpu MHz\" /proc/cpuinfo
 +
}}
  
Tunables are available in {{ic|/sys/devices/system/cpu/cpufreq/ondemand/}} once the governor is loaded and selected and can be preserved at reboot using {{ic|/etc/tmpfiles.d}}. For example:
+
=== Tuning the ondemand governor ===
  
{{hc|/etc/tmpfiles.d/ondemand_threshold.conf|w /sys/devices/system/cpu/cpufreq/ondemand/up_threshold - - - - 25}}
+
See the [https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt kernel documentation] for details.
  
=====Changing the on-demand governor's threshold=====
+
==== Switching threshold ====
To change when the {{ic|ondemand}} governor switches to a higher multiplier, one can manipulate {{ic|/sys/devices/system/cpu/cpufreq/ondemand/up_threshold}}. Determine the current setting by issuing the following command as root:
+
# cat /sys/devices/system/cpu/cpufreq/ondemand/up_threshold
+
  
The value returned should be {{ic|95}}, the default setting as of kernel version 3.0. This means that the {{ic|ondemand}} governor currently increases the clock rate if a core reaches 95% utilization. This can be changed, for example:
+
To set the threshold for stepping up to another frequency:
  
  # echo -n 15 > /sys/devices/system/cpu/cpufreq/ondemand/up_threshold
+
  # echo -n ''percent'' > /sys/devices/system/cpu/cpufreq/<governor>/up_threshold
  
=====On-demand governor sampling_down_factor=====
+
To set the threshold for stepping down to another frequency:
{{ic|sampling_down_factor}} is another global ondemand global tunable.
+
  
Having {{ic|sampling_down_factor}} set to {{ic|1}} makes no changes from existing behavior, but having {{ic|sampling_down_factor}} set to a value greater than 1 (e.g. 100) causes it to act as a multiplier for the scheduling interval for re-evaluating load when the CPU is at its highest clock frequency due to high load. This improves performance by reducing the overhead of load evaluation and helping the CPU stay at its highest clock frequency when it is truly busy, rather than shifting back and forth in speed. This tunable has no effect on behavior at lower frequencies/lower CPU loads.
+
# echo -n ''percent'' > /sys/devices/system/cpu/cpufreq/<governor>/down_threshold
  
Read the value (default: 1):
+
==== Sampling rate ====
# cat /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
+
  
Set the value:
+
The sampling rate determines how frequently the governor checks to tune the CPU. {{ic|sampling_down_factor}} is a tunable that multiplies the sampling rate when the CPU is at its highest clock frequency thereby delaying load evaluation and improving performance. Allowed values for {{ic|sampling_down_factor}} are 1 to 100000. This tunable has no effect on behavior at lower CPU frequencies/loads.
  # echo -n 10 > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
+
  
{{Note|When using systemd with cpupower, the above commands can be added to {{ic|/usr/lib/systemd/scripts/cpupower}}, at the end of the script, right before {{ic|exit $fail}}.}}
+
To read the value (default = 1), run:
  
====Tuning conservative governor====
+
$ cat /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
  
While the conservative governor switches smoothly and utilizes all of the available frequencies, users may want to tune it further. Out-of-the-box it will clock the CPU up when load reaches 75% and clock down when load drops to 20%. As with the on-demand governor, decreasing the {{ic|up_threshold}} may improve performance and responsiveness.
+
To set the value, run:
  
Tunables are available in {{ic|/sys/devices/system/cpu/cpufreq/conservative/}}. Refer to the [[#Improving ondemand performance]] section for important advice regarding making this changes permanent, and potential daemon loading issues.
+
# echo -n ''value'' > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
  
=====Changing the conservative governor's thresholds=====
+
==== Make changes permanent ====
Determine the current {{ic|up_threshold}} setting by issuing the following command as root:
+
# cat /sys/devices/system/cpu/cpufreq/conservative/up_threshold
+
  
The value returned should be {{ic|75}}, the default setting as of kernel version 3.0. This means that the {{ic|conservative}} governor currently increases the clock rate to the next highest speed if a core reaches 75% utilization. The can be changed, for example:
+
To have changes persist on system reboot probably the easiest is to use systemd-tmpfiles. For example to set the sampling_down_factor on boot you could create or edit a {{ic|/etc/tmpfiles.d/10-cpu-sampling-down.conf}} file as follow
  
# echo -n 40 > /sys/devices/system/cpu/cpufreq/conservative/up_threshold
+
{{hc|/etc/tmpfiles.d/10-cpu-sampling-down.conf|<nowiki>
 +
w /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor - - - - 40
 +
</nowiki>}}
  
{{Note|The minimum value one can enter must be above the one in {{ic|down_threshold}}; entering a value under that results in the error, "bash: echo: write error: Invalid argument"}}
+
== Interaction with ACPI events ==
  
Similarly the {{ic|down_threshold}} value can be read and modified via {{ic|/sys/devices/system/cpu/cpufreq/conservative/down_threshold}}. The default value should be {{ic|20}} as of kernel version 3.0. This means that the {{ic|conservative}} governor decreases the clock rate to the next lowest speed if a core falls to 20% utilization, which is a sensible default.
+
Users may configure scaling governors to switch automatically based on different ACPI events such as connecting the AC adapter or closing a laptop lid. A quick example is given below, however it may be worth reading full article on [[acpid]].
 
+
While the down sampling rate can also be adjusted for the {{ic|conservative}} governor, increasing it may only help with occasional low usage CPU spikes during high usage applications, as the {{ic|down_threshold}} is a much more direct control for down scaling which does not exist on the {{ic|ondemand}} governor.
+
 
+
Be aware that setting {{ic|down_threshold}} too close to {{ic|up_threshold}} may result in constant CPU switching, which might be something desirable for certain users and not for others. Setting {{ic|down_threshold}} or {{ic|up_threshold}} too low may result in the CPU being clocked higher than it needs sacrificing power saving for performance, and setting {{ic|up_threshold}} too high may result in decreased performance, but reduced power consumption. Experiment to find the optimal values for your system and your needs.
+
 
+
==== Interaction with ACPI events ====
+
 
+
Users may configure scaling governors to switch automatically based on different ACPI events such as connecting the AC adapter or closing a laptop lid. A quick example is given below, however it may be worth reading full article on [[acpid]].
+
  
 
Events are defined in {{ic|/etc/acpi/handler.sh}}. If the {{Pkg|acpid}} package is installed, the file should already exist and be executable. For example, to change the scaling governor from {{ic|performance}} to {{ic|conservative}} when the AC adapter is disconnected and change it back if reconnected:
 
Events are defined in {{ic|/etc/acpi/handler.sh}}. If the {{Pkg|acpid}} package is installed, the file should already exist and be executable. For example, to change the scaling governor from {{ic|performance}} to {{ic|conservative}} when the AC adapter is disconnected and change it back if reconnected:
  
{{hc
+
{{hc|/etc/acpi/handler.sh|<nowiki>
|/etc/acpi/handler.sh
+
|<nowiki>
+
 
[...]
 
[...]
  
ac_adapter)
+
ac_adapter)
    case "$2" in
+
    case "$2" in
        AC*)
+
        AC*)
            case "$4" in
+
            case "$4" in
                00000000)
+
                00000000)
                    echo "conservative" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor     
+
                    echo "conservative" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor     
                    echo -n $minspeed >$setspeed
+
                    echo -n $minspeed >$setspeed
                    #/etc/laptop-mode/laptop-mode start
+
                    #/etc/laptop-mode/laptop-mode start
                ;;
+
                ;;
                00000001)
+
                00000001)
                    echo "performance" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
+
                    echo "performance" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
                    echo -n $maxspeed >$setspeed
+
                    echo -n $maxspeed >$setspeed
                    #/etc/laptop-mode/laptop-mode stop
+
                    #/etc/laptop-mode/laptop-mode stop
                ;;
+
                ;;
            esac
+
            esac
        ;;
+
        ;;
        *) logger "ACPI action undefined: $2" ;;
+
        *) logger "ACPI action undefined: $2" ;;
    esac
+
    esac
;;
+
;;
  
 
[...]
 
[...]
 
</nowiki>}}
 
</nowiki>}}
  
=== Daemon ===
+
== Privilege granting under GNOME ==
{{Pkg|cpupower}} includes a daemon which allows users to set the desired scaling governor and min/max clock speeds for all processor cores at boot-time.
+
  
Before starting the daemon, edit {{ic|/etc/conf.d/cpupower}} as root, selecting the desired governor and setting the min/max speed for your CPU(s).
+
{{Out of date|See the note below.}}
  
{{Note|The exact min/max values of the CPU(s) can be determined by running {{ic|cpupower frequency-info}}. However, these values are ''optional''. Users may omit them entirely by deleting or commenting out the min/max_freq lines; scaling will work automatically.}}
+
{{Note|systemd introduced logind which handles consolekit and policykit actions. The following code below does not work. With logind, simply edit in the file {{ic|/usr/share/polkit-1/actions/org.gnome.cpufreqselector.policy}} the <defaults> elements according to your needs and the polkit manual [http://www.freedesktop.org/software/polkit/docs/latest/polkit.8.html].}}
  
With the appropriate configuration, start the daemon with the following command:
+
[[GNOME]] has a nice applet to change the governor on the fly. To use it without the need to enter the root password, simply create following file:
# systemctl start cpupower
+
  
To start the daemon automatically at startup:
+
{{hc|/var/lib/polkit-1/localauthority/50-local.d/org.gnome.cpufreqselector.pkla|2=
# systemctl enable cpupower
+
[org.gnome.cpufreqselector]
 
+
Identity=unix-user:''user''
=== Privilege Granting Under GNOME ===
+
 
+
[[GNOME]] has a nice applet to change the governor on the fly.  To use it without the need to enter the root password, simply create {{ic|/var/lib/polkit-1/localauthority/50-local.d/org.gnome.cpufreqselector.pkla}} and populate it with the following:
+
 
+
{{bc|<nowiki>[org.gnome.cpufreqselector]
+
Identity=unix-user:USER
+
 
Action=org.gnome.cpufreqselector
 
Action=org.gnome.cpufreqselector
 
ResultAny=no
 
ResultAny=no
 
ResultInactive=no
 
ResultInactive=no
ResultActive=yes</nowiki>}}0
+
ResultActive=yes
 
+
}}
Where the word {{ic|USER}} is replaced with the username of interest.
+
  
The {{AUR|desktop-privileges}} package in the [[Arch User Repository|AUR]] contains a similar {{ic|.pkla}} file for authorizing all users of the {{ic|power}} [[Users and Groups|group]] to change the governor.
+
Where the word ''user'' is replaced with the username of interest.
  
===Laptop Mode Tools===
+
The {{AUR|desktop-privileges}} package in the [[AUR]] contains a similar {{ic|.pkla}} file for authorizing all users of the {{ic|power}} [[group]] to change the governor.
If you are already using or plan to use [[Laptop Mode Tools]] for other power saving solutions, then you may want to let it also manage CPU frequency scaling. To do so, you just have to insert the appropriate frequency driver to the {{ic|/etc/modules.d/}} directory. (see [[#CPU frequency driver]] above) and then go through the {{ic|/etc/laptop-mode/conf.d/cpufreq.conf}} file to define governors, frequencies and policies. You will not need to load other modules and daemons or to set up scaling governors and interaction with ACPI events. Please refer to [[Laptop Mode Tools]] for more details.
+
  
 
== Troubleshooting ==
 
== Troubleshooting ==
{{accuracy}}
+
 
 +
{{Accuracy}}
 +
 
 
* Some applications, like [[ntop]], do not respond well to automatic frequency scaling. In the case of ntop it can result in segmentation faults and lots of lost information as even the {{ic|on-demand}} governor cannot change the frequency quickly enough when a lot of packets suddenly arrive at the monitored network interface that cannot be handled by the current processor speed.
 
* Some applications, like [[ntop]], do not respond well to automatic frequency scaling. In the case of ntop it can result in segmentation faults and lots of lost information as even the {{ic|on-demand}} governor cannot change the frequency quickly enough when a lot of packets suddenly arrive at the monitored network interface that cannot be handled by the current processor speed.
  
* Some CPU's may suffer from poor performance with the default settings of the {{ic|on-demand}} governor (e.g. flash videos not playing smoothly or stuttering window animations). Instead of completely disabling frequency scaling to resolve these issues, the aggressiveness of frequency scaling can be increased by lowering the ''up_threshold'' [[sysctl]] variable for each CPU. See [[#Changing the on-demand governor's threshold]].
+
* Some CPU's may suffer from poor performance with the default settings of the {{ic|on-demand}} governor (e.g. flash videos not playing smoothly or stuttering window animations). Instead of completely disabling frequency scaling to resolve these issues, the aggressiveness of frequency scaling can be increased by lowering the ''up_threshold'' [[sysctl]] variable for each CPU. See [[#Switching_threshold|how to change the on-demand governor's threshold]].
  
 
* Sometimes the on-demand governor may not throttle to the maximum frequency but one step below. This can be solved by setting max_freq value slightly higher than the real maximum. For example, if frequency range of the CPU is from 2.00 GHz to 3.00 GHz, setting max_freq to 3.01 GHz can be a good idea.
 
* Sometimes the on-demand governor may not throttle to the maximum frequency but one step below. This can be solved by setting max_freq value slightly higher than the real maximum. For example, if frequency range of the CPU is from 2.00 GHz to 3.00 GHz, setting max_freq to 3.01 GHz can be a good idea.
Line 292: Line 238:
  
 
=== BIOS frequency limitation ===
 
=== BIOS frequency limitation ===
 +
 
Some CPU/BIOS configurations may have difficulties to scale to the maximum frequency or scale to higher frequencies at all. This is most likely caused by BIOS events telling the OS to limit the frequency resulting in {{ic|/sys/devices/system/cpu/cpu0/cpufreq/bios_limit}} set to a lower value.
 
Some CPU/BIOS configurations may have difficulties to scale to the maximum frequency or scale to higher frequencies at all. This is most likely caused by BIOS events telling the OS to limit the frequency resulting in {{ic|/sys/devices/system/cpu/cpu0/cpufreq/bios_limit}} set to a lower value.
  
Line 298: Line 245:
 
Reasons like that can be (assuming your machine's a notebook) that the battery is removed (or near death) so you're on AC-power only. In this case a weak AC-source might not supply enough electricity to fulfill extreme peak demands by the overall system and as there is no battery to assist this could lead to data loss, data corruption or in worst case even hardware damage!
 
Reasons like that can be (assuming your machine's a notebook) that the battery is removed (or near death) so you're on AC-power only. In this case a weak AC-source might not supply enough electricity to fulfill extreme peak demands by the overall system and as there is no battery to assist this could lead to data loss, data corruption or in worst case even hardware damage!
  
Not all BIOS'es limit the CPU-Frequency in this case, but for example most IBM/Lenove Thinkpads do. Refer to thinkwiki for more [http://www.thinkwiki.org/wiki/Problem_with_CPU_frequency_scaling thinkpad related info on this topic].
+
Not all BIOS'es limit the CPU-Frequency in this case, but for example most IBM/Lenovo Thinkpads do. Refer to thinkwiki for more [http://www.thinkwiki.org/wiki/Problem_with_CPU_frequency_scaling thinkpad related info on this topic].
  
 
If you checked there's not just an odd BIOS setting and you know what you're doing you can make the Kernel ignore these BIOS-limitations.
 
If you checked there's not just an odd BIOS setting and you know what you're doing you can make the Kernel ignore these BIOS-limitations.
Line 308: Line 255:
 
For trying this temporarily change the value in {{ic|/sys/module/processor/parameters/ignore_ppc}} from {{ic|0}} to {{ic|1}}.
 
For trying this temporarily change the value in {{ic|/sys/module/processor/parameters/ignore_ppc}} from {{ic|0}} to {{ic|1}}.
  
For setting it permanent refer to [[Kernel_modules#Configuration|Kernel modules]] or just read on.
+
For setting it permanent refer to [[Kernel_modules#Configuration|Kernel modules]]{{Broken section link}} or just read on.
 
Add {{ic|1=processor.ignore_ppc=1}} to your kernel boot line or create
 
Add {{ic|1=processor.ignore_ppc=1}} to your kernel boot line or create
 
{{hc|/etc/modprobe.d/ignore_ppc.conf|2=# If the frequency of your machine gets wrongly limited by BIOS, this should help
 
{{hc|/etc/modprobe.d/ignore_ppc.conf|2=# If the frequency of your machine gets wrongly limited by BIOS, this should help
 
options processor ignore_ppc=1}}
 
options processor ignore_ppc=1}}
 +
 +
== Common issues ==
 +
 +
=== High load ===
 +
 +
{{Expansion|What kernel version? Needs a link to an open bug report, all that I've been able to find with a quick search is [https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1413149].}}
 +
 +
There is a bug in the kernel module {{ic|rtsx_usb_ms}} which causes a constant load over 1.0. Test whether it makes a difference by temporarily removing it {{ic|rmmod rtsx_usb_ms}}
 +
 +
== See also ==
 +
 +
* [https://www.kernel.org/doc/Documentation/cpu-freq/index.txt Linux CPUFreq - kernel documentation]
 +
* [http://www.reddit.com/r/linux/comments/1hdogn/acpi_cpufreq_or_intel_pstates/ Comprehensive explanation of pstate]

Latest revision as of 20:32, 19 August 2016

CPU frequency scaling enables the operating system to scale the CPU frequency up or down in order to save power. CPU frequencies can be scaled automatically depending on the system load, in response to ACPI events, or manually by userspace programs.

CPU frequency scaling is implemented in the Linux kernel, the infrastructure is called cpufreq. Since kernel 3.4 the necessary modules are loaded automatically and the recommended ondemand governor is enabled by default. However, userspace tools like cpupower, acpid, Laptop Mode Tools, or GUI tools provided for your desktop environment, may still be used for advanced configuration.

Userspace tools

thermald

thermaldAUR is a Linux daemon used to prevent the overheating of platforms. This daemon monitors temperature and applies compensation using available cooling methods.

By default, it monitors CPU temperature using available CPU digital temperature sensors and maintains CPU temperature under control, before HW takes aggressive correction action. If there is a skin temperature sensor in thermal sysfs, then it tries to keep skin temperature under 45C.

i7z

i7z is an i7 (and now i3, i5) (CPU) reporting tool for Linux. It can be launched from a Terminal with the command i7z or as GUI with i7z-gui.

cpupower

cpupower is a set of userspace utilities designed to assist with CPU frequency scaling. The package is not required to use scaling, but is highly recommended because it provides useful command-line utilities and a systemd service to change the governor at boot.

The configuration file for cpupower is located in /etc/default/cpupower. This configuration file is read by a bash script in /usr/lib/systemd/scripts/cpupower which is activated by systemd with cpupower.service. You may want to enable cpupower.service to start at boot.

CPU frequency driver

Note:
  • The native CPU module is loaded automatically.
  • The pstate power scaling driver is used automatically for modern Intel CPUs instead of the other drivers below. This driver takes priority over other drivers and is built-in as opposed to being a module. This driver is currently automatically used for Sandy Bridge and newer CPUs. If you encounter a problem while using this driver, add intel_pstate=disable to your kernel line. You can use the same user space utilities with this driver, but cannot control it.
  • Even P State behavior mentioned above can be influenced with /sys/devices/system/cpu/intel_pstate, e.g. Intel Turbo Boost can be deactivated with # echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo for keeping CPU-Temperatures low.
  • Additional control for modern Intel CPUs is available with the Linux Thermal Daemon (available as thermaldAUR), which proactively controls thermal using P-states, T-states, and the Intel power clamp driver. thermald can also be used for older Intel CPUs. If the latest drivers are not available, then the daemon will revert to x86 model specific registers and the Linux ‘cpufreq subsystem’ to control system cooling.

cpupower requires modules to know the limits of the native CPU:

Module Description
intel_pstate This driver implements a scaling driver with an internal governor for Intel Core (Sandy Bridge and newer) processors.
acpi-cpufreq CPUFreq driver which utilizes the ACPI Processor Performance States. This driver also supports the Intel Enhanced SpeedStep (previously supported by the deprecated speedstep-centrino module).
speedstep-lib CPUFreq driver for Intel SpeedStep-enabled processors (mostly Atoms and older Pentiums (< 3))
powernow-k8 CPUFreq driver for K8/K10 Athlon 64/Opteron/Phenom processors. Since Linux 3.7 'acpi-cpufreq' will automatically be used for more modern CPUs from this family.
pcc-cpufreq This driver supports Processor Clocking Control interface by Hewlett-Packard and Microsoft Corporation which is useful on some ProLiant servers.
p4-clockmod CPUFreq driver for Intel Pentium 4/Xeon/Celeron processors which lowers the CPU temperature by skipping clocks. (You probably want to use a SpeedStep driver instead.)

To see a full list of available modules, run:

$ ls /usr/lib/modules/$(uname -r)/kernel/drivers/cpufreq/

Load the appropriate module (see Kernel modules for details). Once the appropriate cpufreq driver is loaded, detailed information about the CPU(s) can be displayed by running

$ cpupower frequency-info

Setting maximum and minimum frequencies

In rare cases, it may be necessary to manually set maximum and minimum frequencies.

To set the maximum clock frequency (clock_freq is a clock frequency with units: GHz, MHz):

# cpupower frequency-set -u clock_freq

To set the minimum clock frequency:

# cpupower frequency-set -d clock_freq

To set the CPU to run at a specified frequency:

# cpupower frequency-set -f clock_freq
Note:
  • To adjust for only a single CPU core, append -c core_number.
  • The governor, maximum and minimum frequencies can be set in /etc/default/cpupower.

Scaling governors

Note: The modern pstate driver only supports the performance and powersave governors and the performance governor should give better power saving functionality than the old ondemand governor.

Governors (see table below) are power schemes for the CPU. Only one may be active at a time. For details, see the kernel documentation in the kernel source.

Governor Description
ondemand Dynamically switch between CPU(s) available if at 95% cpu load
performance Run the cpu at max frequency
conservative Dynamically switch between CPU(s) available if at 75% load
powersave Run the cpu at the minimum frequency
userspace Run the cpu at user specified frequencies

Depending on the scaling driver, one of these governors will be loaded by default:

  • ondemand for AMD and older Intel CPU.
  • powersave for Intel Sandy Bridge and newer CPU.

To activate a particular governor, run:

# cpupower frequency-set -g governor
Note:
  • To adjust for only a single CPU core, append -c core_number to the command above.
  • Activating a governor requires that specific kernel module (named cpufreq_governor) is loaded. As of kernel 3.4, these modules are loaded automatically.

Alternatively, you can activate a governor on every available CPU manually:

# echo governor | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor >/dev/null
Tip: To monitor cpu speed in real time, run:
$ watch grep \"cpu MHz\" /proc/cpuinfo

Tuning the ondemand governor

See the kernel documentation for details.

Switching threshold

To set the threshold for stepping up to another frequency:

# echo -n percent > /sys/devices/system/cpu/cpufreq/<governor>/up_threshold

To set the threshold for stepping down to another frequency:

# echo -n percent > /sys/devices/system/cpu/cpufreq/<governor>/down_threshold

Sampling rate

The sampling rate determines how frequently the governor checks to tune the CPU. sampling_down_factor is a tunable that multiplies the sampling rate when the CPU is at its highest clock frequency thereby delaying load evaluation and improving performance. Allowed values for sampling_down_factor are 1 to 100000. This tunable has no effect on behavior at lower CPU frequencies/loads.

To read the value (default = 1), run:

$ cat /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor

To set the value, run:

# echo -n value > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor

Make changes permanent

To have changes persist on system reboot probably the easiest is to use systemd-tmpfiles. For example to set the sampling_down_factor on boot you could create or edit a /etc/tmpfiles.d/10-cpu-sampling-down.conf file as follow

/etc/tmpfiles.d/10-cpu-sampling-down.conf
w /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor - - - - 40

Interaction with ACPI events

Users may configure scaling governors to switch automatically based on different ACPI events such as connecting the AC adapter or closing a laptop lid. A quick example is given below, however it may be worth reading full article on acpid.

Events are defined in /etc/acpi/handler.sh. If the acpid package is installed, the file should already exist and be executable. For example, to change the scaling governor from performance to conservative when the AC adapter is disconnected and change it back if reconnected:

/etc/acpi/handler.sh
[...]

ac_adapter)
    case "$2" in
        AC*)
            case "$4" in
                00000000)
                    echo "conservative" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor    
                    echo -n $minspeed >$setspeed
                    #/etc/laptop-mode/laptop-mode start
                ;;
                00000001)
                    echo "performance" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
                    echo -n $maxspeed >$setspeed
                    #/etc/laptop-mode/laptop-mode stop
                ;;
            esac
        ;;
        *) logger "ACPI action undefined: $2" ;;
    esac
;;

[...]

Privilege granting under GNOME

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: See the note below. (Discuss in Talk:CPU frequency scaling#)
Note: systemd introduced logind which handles consolekit and policykit actions. The following code below does not work. With logind, simply edit in the file /usr/share/polkit-1/actions/org.gnome.cpufreqselector.policy the <defaults> elements according to your needs and the polkit manual [1].

GNOME has a nice applet to change the governor on the fly. To use it without the need to enter the root password, simply create following file:

/var/lib/polkit-1/localauthority/50-local.d/org.gnome.cpufreqselector.pkla
[org.gnome.cpufreqselector]
Identity=unix-user:user
Action=org.gnome.cpufreqselector
ResultAny=no
ResultInactive=no
ResultActive=yes

Where the word user is replaced with the username of interest.

The desktop-privilegesAUR package in the AUR contains a similar .pkla file for authorizing all users of the power group to change the governor.

Troubleshooting

Tango-inaccurate.pngThe factual accuracy of this article or section is disputed.Tango-inaccurate.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in Talk:CPU frequency scaling#)
  • Some applications, like ntop, do not respond well to automatic frequency scaling. In the case of ntop it can result in segmentation faults and lots of lost information as even the on-demand governor cannot change the frequency quickly enough when a lot of packets suddenly arrive at the monitored network interface that cannot be handled by the current processor speed.
  • Some CPU's may suffer from poor performance with the default settings of the on-demand governor (e.g. flash videos not playing smoothly or stuttering window animations). Instead of completely disabling frequency scaling to resolve these issues, the aggressiveness of frequency scaling can be increased by lowering the up_threshold sysctl variable for each CPU. See how to change the on-demand governor's threshold.
  • Sometimes the on-demand governor may not throttle to the maximum frequency but one step below. This can be solved by setting max_freq value slightly higher than the real maximum. For example, if frequency range of the CPU is from 2.00 GHz to 3.00 GHz, setting max_freq to 3.01 GHz can be a good idea.
  • Some combinations of ALSA drivers and sound chips may cause audio skipping as the governor changes between frequencies, switching back to a non-changing governor seems to stop the audio skipping.

BIOS frequency limitation

Some CPU/BIOS configurations may have difficulties to scale to the maximum frequency or scale to higher frequencies at all. This is most likely caused by BIOS events telling the OS to limit the frequency resulting in /sys/devices/system/cpu/cpu0/cpufreq/bios_limit set to a lower value.

Either you just made a specific Setting in the BIOS Setup Utility, (Frequency, Thermal Management, etc.) you can blame a buggy/outdated BIOS or the BIOS might have a serious reason for throttling the CPU on it's own.

Reasons like that can be (assuming your machine's a notebook) that the battery is removed (or near death) so you're on AC-power only. In this case a weak AC-source might not supply enough electricity to fulfill extreme peak demands by the overall system and as there is no battery to assist this could lead to data loss, data corruption or in worst case even hardware damage!

Not all BIOS'es limit the CPU-Frequency in this case, but for example most IBM/Lenovo Thinkpads do. Refer to thinkwiki for more thinkpad related info on this topic.

If you checked there's not just an odd BIOS setting and you know what you're doing you can make the Kernel ignore these BIOS-limitations.

Warning: Make sure you read and understood the section above. CPU frequency limitation is a safety feature of your BIOS and you should not need to work around it.

A special parameter has to be passed to the processor module.

For trying this temporarily change the value in /sys/module/processor/parameters/ignore_ppc from 0 to 1.

For setting it permanent refer to Kernel modules[broken link: invalid section] or just read on. Add processor.ignore_ppc=1 to your kernel boot line or create

/etc/modprobe.d/ignore_ppc.conf
# If the frequency of your machine gets wrongly limited by BIOS, this should help
options processor ignore_ppc=1

Common issues

High load

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: What kernel version? Needs a link to an open bug report, all that I've been able to find with a quick search is [2]. (Discuss in Talk:CPU frequency scaling#)

There is a bug in the kernel module rtsx_usb_ms which causes a constant load over 1.0. Test whether it makes a difference by temporarily removing it rmmod rtsx_usb_ms

See also