CPU Frequency Scaling: Difference between revisions

From SlackWiki
Jump to navigation Jump to search
(Copied from /old)
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
The CPU frequency scaling feature present in modern computers allows lowering and increasing the processor speed dynamically in runtime so as to save energy while having the possibility of increasing the performance and power consumption if needed. Activating CPU frequency scaling under Slackware is very easy, and requires loading a minimum of two kernel modules and running a simple command. On the one hand, you need to load a specific CPU driver that will activate the frequency scaling feature in your platform. Second, you need to choose a CPU frequency scaling governor, a module that will decide how and when to change the internal processor speed. There are governors to always run at full speed, governors to always run at low speed, governors to increase and decrease the speed based on the CPU load, etc.
The CPU frequency scaling feature present in modern computers allows lowering and increasing the processor speed dynamically in runtime so as to save energy while having the possibility of increasing the performance and power consumption if needed. Activating CPU frequency scaling under Slackware is very easy, and requires loading a minimum of two kernel modules and running a simple command. On the one hand, you need to load a specific CPU driver that will activate the frequency scaling feature in your platform. Second, you need to choose a CPU frequency scaling governor, a module that will decide how and when to change the internal processor speed. There are governors to always run at full speed, governors to always run at low speed, governors to increase and decrease the speed based on the CPU load, etc.


It's usually desired to activate CPU frequency scaling as early as possible in the boot process, eventhough a few seconds running at full power are not that important. The easier way is, however, to enable it from the boot script <tt>/etc/rc.d/rc.local</tt>, which is executed right at the end of the boot process. Other approaches need more intrusive changes. You could load the required modules and run the commands from a modified init script of an initial RAM disk. You could do the same from a personal <tt>/etc/rc.d/rc.modules.local</tt>, or also from the already mentioned <tt>rc.local</tt>. It's up to you to decide when and where to load and activate the feature.


= Loading CPU specific drivers =


You first need to load one of the available CPU powersaving modules. They are included in the typical kernel module packages, and depend on the type of CPU you have. Every available module will be located in <tt>/lib/modules/$( uname -r )/kernel/arch/i386/kernel/cpu/cpufreq</tt>. For example, my CPU is a K8 processor and I use the <tt>powernow-k8</tt> module. Inside that directory you will see a lot of module files ending with <tt>.ko</tt>. To load them, use <tt>/sbin/modprobe</tt> followed by the name before the suffix, from any location. Example:
= Find available governors  =


  /sbin/modprobe powernow-k8
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors


If you're not sure about which driver you need, proceed by trial and error. Load one module with a command like the previous one and then check the last lines of <tt>dmesg</tt> with the command <tt>dmesg | tail</tt>. If you see a successful detection, that's the module you need. If not, unload the module with <tt>/sbin/modprobe -r</tt> (example: <tt>/sbin/modprobe -r powernow-k8</tt>) and try another one. This is an example of successful output as printed by <tt>dmesg | tail</tt>:


  powernow-k8: Found 1 Mobile AMD Sempron(tm) Processor 3000+ processors (version 2.00.00)
= Setting a governor =
  powernow-k8:    0 : fid 0xa (1800 MHz), vid 0x8
  powernow-k8:    1 : fid 0x8 (1600 MHz), vid 0xa
  powernow-k8:    2 : fid 0x0 (800 MHz), vid 0x13


= Loading a governor module =
edit ''/etc/default/cpufreq''


The second step is to load a governor module. The governor most people will want to load is the one named <em>ondemand</em>, that will always use the lowest frequency available to fullfill the processing requirements. If your computer is idle, it will put the processor in the lowest possible speed. If you perform a very CPU-intensive task, it will increase the frequency accordingly. It's also the governor that Intel recommends to use in its laptops. All the available governor modules are located in <tt>/lib/modules/$( uname -r )/kernel/drivers/cpufreq</tt>. To use a governor, the first step is to load its corresponding module as we did previously with the CPU module. Example:
= Applying the governor =


  /sbin/modprobe cpufreq_ondemand
''/etc/rc.d/rc.cpufreq'' should already be executable
make sure its running
''/etc.rc.d/rc.cpufreq restart''


Notice how the governor is called <em>ondemand</em>, yet its module is called <em>cpufreq_ondemand</em>.


= Setting the CPU scaling governor =
= Governor types =  


Loading a governor module doesn't mean it's going to be used immediately. In fact, you can load all governor modules if you want. To decide which governor will be used in a given moment, you need to set it at runtime. Each CPU can have its own governor. That is, if you have a dual core machine, you can put one of the cores in low power and other one in full power if you need it. However, the most usual case is to use the same governor in all CPUs. Each CPU has its own configuration file in the <tt>/sys</tt> filesystem. For example, the file for the first CPU is called <tt>/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor</tt>. For other CPUs, increase the digit in <tt>cpu0</tt>. You can set a governor name for all of them at the same time using a shell loop like the one in this example, that sets the <em>ondemand</em> governor for every CPU:
'''Performance'''


  for config_file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
The CPUfreq governor "performance" sets the CPU statically to the
      echo ondemand >$config_file
highest frequency within the borders of scaling_min_freq and
  done
scaling_max_freq.


To confirm you have successfully set the governor, see if it scales the CPU frequency. From an idle system, run <tt>grep '^cpu MHz' /proc/cpuinfo</tt>. You should see one or more lines like:


  cpu MHz        : 800.000
'''Powersave'''


Then, launch a CPU intensive task from a different terminal, like <tt>yes >/dev/null</tt> and then run the previous command again. You should now see a higher number (1800.000 in my case) in at least one of the output lines. If you see the same number wait 5 seconds and try again.
The CPUfreq governor "powersave" sets the CPU statically to the
lowest frequency within the borders of scaling_min_freq and
scaling_max_freq.


= Putting it all together =


This can be made as sophisticated as you decide, but let's suppose you opt for the simple approach of setting everything up from <tt>rc.local</tt>. You would add the following commands if your CPU needed the module <em>powernow-k8</em> and you wanted to set the <em>ondemand</em> governor:
'''Userspace'''
 
The CPUfreq governor "userspace" allows the user, or any userspace
program running with UID "root", to set the CPU to a specific frequency
by making a sysfs file "scaling_setspeed" available in the CPU-device
directory.
 
 
'''Ondemand'''
 
The CPUfreq governor "ondemand" sets the CPU frequency depending on the
current system load. Load estimation is triggered by the scheduler
through the update_util_data->func hook; when triggered, cpufreq checks
the CPU-usage statistics over the last period and the governor sets the
CPU accordingly.  The CPU must have the capability to switch the
frequency very quickly.
 
 
'''Conservative'''
 
The CPUfreq governor "conservative", much like the "ondemand"
governor, sets the CPU frequency depending on the current usage.  It
differs in behaviour in that it gracefully increases and decreases the
CPU speed rather than jumping to max speed the moment there is any load
on the CPU. This behaviour is more suitable in a battery powered
environment.


  /sbin/modprobe powernow-k8
  /sbin/modprobe cpufreq_ondemand
  for config_file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
      echo ondemand >$config_file
  done


[[Category:Tutorials]]
[[Category:Tutorials]]

Latest revision as of 13:31, 29 October 2018

The CPU frequency scaling feature present in modern computers allows lowering and increasing the processor speed dynamically in runtime so as to save energy while having the possibility of increasing the performance and power consumption if needed. Activating CPU frequency scaling under Slackware is very easy, and requires loading a minimum of two kernel modules and running a simple command. On the one hand, you need to load a specific CPU driver that will activate the frequency scaling feature in your platform. Second, you need to choose a CPU frequency scaling governor, a module that will decide how and when to change the internal processor speed. There are governors to always run at full speed, governors to always run at low speed, governors to increase and decrease the speed based on the CPU load, etc.


Find available governors

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors 


Setting a governor

edit /etc/default/cpufreq

Applying the governor

/etc/rc.d/rc.cpufreq should already be executable
make sure its running 
/etc.rc.d/rc.cpufreq restart


Governor types

Performance

The CPUfreq governor "performance" sets the CPU statically to the
highest frequency within the borders of scaling_min_freq and
scaling_max_freq.


Powersave

The CPUfreq governor "powersave" sets the CPU statically to the
lowest frequency within the borders of scaling_min_freq and
scaling_max_freq.


Userspace

The CPUfreq governor "userspace" allows the user, or any userspace
program running with UID "root", to set the CPU to a specific frequency
by making a sysfs file "scaling_setspeed" available in the CPU-device
directory.


Ondemand

The CPUfreq governor "ondemand" sets the CPU frequency depending on the
current system load. Load estimation is triggered by the scheduler
through the update_util_data->func hook; when triggered, cpufreq checks
the CPU-usage statistics over the last period and the governor sets the
CPU accordingly.  The CPU must have the capability to switch the
frequency very quickly.


Conservative

The CPUfreq governor "conservative", much like the "ondemand"
governor, sets the CPU frequency depending on the current usage.  It
differs in behaviour in that it gracefully increases and decreases the
CPU speed rather than jumping to max speed the moment there is any load
on the CPU. This behaviour is more suitable in a battery powered
environment.