governors.txt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. CPU frequency and voltage scaling code in the Linux(TM) kernel
  2. L i n u x C P U F r e q
  3. C P U F r e q G o v e r n o r s
  4. - information for users and developers -
  5. Dominik Brodowski <linux@brodo.de>
  6. some additions and corrections by Nico Golde <nico@ngolde.de>
  7. Clock scaling allows you to change the clock speed of the CPUs on the
  8. fly. This is a nice method to save battery power, because the lower
  9. the clock speed, the less power the CPU consumes.
  10. Contents:
  11. ---------
  12. 1. What is a CPUFreq Governor?
  13. 2. Governors In the Linux Kernel
  14. 2.1 Performance
  15. 2.2 Powersave
  16. 2.3 Userspace
  17. 2.4 Ondemand
  18. 2.5 Conservative
  19. 3. The Governor Interface in the CPUfreq Core
  20. 1. What Is A CPUFreq Governor?
  21. ==============================
  22. Most cpufreq drivers (in fact, all except one, longrun) or even most
  23. cpu frequency scaling algorithms only offer the CPU to be set to one
  24. frequency. In order to offer dynamic frequency scaling, the cpufreq
  25. core must be able to tell these drivers of a "target frequency". So
  26. these specific drivers will be transformed to offer a "->target"
  27. call instead of the existing "->setpolicy" call. For "longrun", all
  28. stays the same, though.
  29. How to decide what frequency within the CPUfreq policy should be used?
  30. That's done using "cpufreq governors". Two are already in this patch
  31. -- they're the already existing "powersave" and "performance" which
  32. set the frequency statically to the lowest or highest frequency,
  33. respectively. At least two more such governors will be ready for
  34. addition in the near future, but likely many more as there are various
  35. different theories and models about dynamic frequency scaling
  36. around. Using such a generic interface as cpufreq offers to scaling
  37. governors, these can be tested extensively, and the best one can be
  38. selected for each specific use.
  39. Basically, it's the following flow graph:
  40. CPU can be set to switch independently | CPU can only be set
  41. within specific "limits" | to specific frequencies
  42. "CPUfreq policy"
  43. consists of frequency limits (policy->{min,max})
  44. and CPUfreq governor to be used
  45. / \
  46. / \
  47. / the cpufreq governor decides
  48. / (dynamically or statically)
  49. / what target_freq to set within
  50. / the limits of policy->{min,max}
  51. / \
  52. / \
  53. Using the ->setpolicy call, Using the ->target call,
  54. the limits and the the frequency closest
  55. "policy" is set. to target_freq is set.
  56. It is assured that it
  57. is within policy->{min,max}
  58. 2. Governors In the Linux Kernel
  59. ================================
  60. 2.1 Performance
  61. ---------------
  62. The CPUfreq governor "performance" sets the CPU statically to the
  63. highest frequency within the borders of scaling_min_freq and
  64. scaling_max_freq.
  65. 2.2 Powersave
  66. -------------
  67. The CPUfreq governor "powersave" sets the CPU statically to the
  68. lowest frequency within the borders of scaling_min_freq and
  69. scaling_max_freq.
  70. 2.3 Userspace
  71. -------------
  72. The CPUfreq governor "userspace" allows the user, or any userspace
  73. program running with UID "root", to set the CPU to a specific frequency
  74. by making a sysfs file "scaling_setspeed" available in the CPU-device
  75. directory.
  76. 2.4 Ondemand
  77. ------------
  78. The CPUfreq governor "ondemand" sets the CPU depending on the
  79. current usage. To do this the CPU must have the capability to
  80. switch the frequency very quickly. There are a number of sysfs file
  81. accessible parameters:
  82. sampling_rate: measured in uS (10^-6 seconds), this is how often you
  83. want the kernel to look at the CPU usage and to make decisions on
  84. what to do about the frequency. Typically this is set to values of
  85. around '10000' or more.
  86. show_sampling_rate_(min|max): the minimum and maximum sampling rates
  87. available that you may set 'sampling_rate' to.
  88. up_threshold: defines what the average CPU usaged between the samplings
  89. of 'sampling_rate' needs to be for the kernel to make a decision on
  90. whether it should increase the frequency. For example when it is set
  91. to its default value of '80' it means that between the checking
  92. intervals the CPU needs to be on average more than 80% in use to then
  93. decide that the CPU frequency needs to be increased.
  94. sampling_down_factor: this parameter controls the rate that the CPU
  95. makes a decision on when to decrease the frequency. When set to its
  96. default value of '5' it means that at 1/5 the sampling_rate the kernel
  97. makes a decision to lower the frequency. Five "lower rate" decisions
  98. have to be made in a row before the CPU frequency is actually lower.
  99. If set to '1' then the frequency decreases as quickly as it increases,
  100. if set to '2' it decreases at half the rate of the increase.
  101. ignore_nice_load: this parameter takes a value of '0' or '1'. When
  102. set to '0' (its default), all processes are counted towards the
  103. 'cpu utilisation' value. When set to '1', the processes that are
  104. run with a 'nice' value will not count (and thus be ignored) in the
  105. overall usage calculation. This is useful if you are running a CPU
  106. intensive calculation on your laptop that you do not care how long it
  107. takes to complete as you can 'nice' it and prevent it from taking part
  108. in the deciding process of whether to increase your CPU frequency.
  109. 2.5 Conservative
  110. ----------------
  111. The CPUfreq governor "conservative", much like the "ondemand"
  112. governor, sets the CPU depending on the current usage. It differs in
  113. behaviour in that it gracefully increases and decreases the CPU speed
  114. rather than jumping to max speed the moment there is any load on the
  115. CPU. This behaviour more suitable in a battery powered environment.
  116. The governor is tweaked in the same manner as the "ondemand" governor
  117. through sysfs with the addition of:
  118. freq_step: this describes what percentage steps the cpu freq should be
  119. increased and decreased smoothly by. By default the cpu frequency will
  120. increase in 5% chunks of your maximum cpu frequency. You can change this
  121. value to anywhere between 0 and 100 where '0' will effectively lock your
  122. CPU at a speed regardless of its load whilst '100' will, in theory, make
  123. it behave identically to the "ondemand" governor.
  124. down_threshold: same as the 'up_threshold' found for the "ondemand"
  125. governor but for the opposite direction. For example when set to its
  126. default value of '20' it means that if the CPU usage needs to be below
  127. 20% between samples to have the frequency decreased.
  128. 3. The Governor Interface in the CPUfreq Core
  129. =============================================
  130. A new governor must register itself with the CPUfreq core using
  131. "cpufreq_register_governor". The struct cpufreq_governor, which has to
  132. be passed to that function, must contain the following values:
  133. governor->name - A unique name for this governor
  134. governor->governor - The governor callback function
  135. governor->owner - .THIS_MODULE for the governor module (if
  136. appropriate)
  137. The governor->governor callback is called with the current (or to-be-set)
  138. cpufreq_policy struct for that CPU, and an unsigned int event. The
  139. following events are currently defined:
  140. CPUFREQ_GOV_START: This governor shall start its duty for the CPU
  141. policy->cpu
  142. CPUFREQ_GOV_STOP: This governor shall end its duty for the CPU
  143. policy->cpu
  144. CPUFREQ_GOV_LIMITS: The limits for CPU policy->cpu have changed to
  145. policy->min and policy->max.
  146. If you need other "events" externally of your driver, _only_ use the
  147. cpufreq_governor_l(unsigned int cpu, unsigned int event) call to the
  148. CPUfreq core to ensure proper locking.
  149. The CPUfreq governor may call the CPU processor driver using one of
  150. these two functions:
  151. int cpufreq_driver_target(struct cpufreq_policy *policy,
  152. unsigned int target_freq,
  153. unsigned int relation);
  154. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  155. unsigned int target_freq,
  156. unsigned int relation);
  157. target_freq must be within policy->min and policy->max, of course.
  158. What's the difference between these two functions? When your governor
  159. still is in a direct code path of a call to governor->governor, the
  160. per-CPU cpufreq lock is still held in the cpufreq core, and there's
  161. no need to lock it again (in fact, this would cause a deadlock). So
  162. use __cpufreq_driver_target only in these cases. In all other cases
  163. (for example, when there's a "daemonized" function that wakes up
  164. every second), use cpufreq_driver_target to lock the cpufreq per-CPU
  165. lock before the command is passed to the cpufreq processor driver.