Sunday, August 25, 2013

Changing CPU frequency

Changing CPU frequency in Linux


Note: To see my entire list of tutorials click here.

There are many scenarios where you would like to change the CPU frequency. It may be for reducing the CPU power consumption, or to get the best performance. Its recommended to choose a particular frequency for testing your tools and benchmarks. You may set the frequency minimum to reduce the heat. The default policy is called "OnDemand" which changes CPU frequency according to the need. You can set the frequency or mode for each CPU core.

Step 1: Install the required packages


The below command is for debian based system. For RPM based system use yum
sudo apt-get install cpufrequtils


Step 2: The current CPU frequency and mode


You can view the current cpu frequency and mode for each core by using the following command

cpufreq-info

Just to see the frequency you can use

more /proc/cpuinfo | grep MHz 

Step 3: Supported modes and frequencies


We can either set a particular frequency or a particular frequency mode.

Lets first see the list of supported CPU frequency.
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies

#sample output: 2401000 2400000 1600000 800000 

To see the available modes use
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors

#sample output: conservative ondemand userspace powersave performance

Available governors/modes

  • Performance: Use the highest possible CPU frequency
  • Powersave: Use the lowest possible CPU frequency
  • Userspace: exports the available frequency information to the user level (through the /sys file system) and permits user-space control of the CPU frequency
  • Ondemand: Set frequency based on the user demand
  • Conservative: Like the ondemand but increases frequency step by step

Step 4: Loading the CPU driver


The driver for CPU depends on your CPU, here is a short list

  • acpi-cpufreq: CPUFreq driver which utilizes the ACPI Processor Performance States. This driver also supports Intel Enhanced SpeedStep.
  • speedstep-lib: CPUFreq drive for Intel speedstep enabled processors (mostly atoms and older pentiums (< 3))


  • powernow-k8: CPUFreq driver for K8/K10 Athlon64/Opteron/Phenom processors. Deprecated since linux 3.7 - Use acpi_cpufreq.


  • 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


you can use the following command to find the driver
ls /lib/modules/$(uname -r)/kernel/drivers/cpufreq/
Now load the module using the following in my case (acpi-cpufreq)
sudo modprobe acpi-cpufreq

Step 5: Setting minimum and maximum cpu frequency

If you want to set a frequency mode rather than a particular frequency skip this step To set maximum cpu frequency for core 0
echo 2401000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
To set minimum cpu frequency for core 0
echo 800000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
Just repeat this step for cpu1 etc to set the same on all cores. Verify the setting using step 2

Step 6: Setting cpu frequency modes

To set the cpu frequency use the following
sudo sh -c 'echo "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor' 
Verify the setting using step 2 Just repeat this step for cpu1 etc to set the same on all cores.

Step 6: Result

You have changed the cpu frequency