governors.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 (except the intel_pstate and 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/target_index"
  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/target_index 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. It's default value is (cmp. with users-guide.txt):
  86. transition_latency * 1000
  87. Be aware that transition latency is in ns and sampling_rate is in us, so you
  88. get the same sysfs value by default.
  89. Sampling rate should always get adjusted considering the transition latency
  90. To set the sampling rate 750 times as high as the transition latency
  91. in the bash (as said, 1000 is default), do:
  92. echo `$(($(cat cpuinfo_transition_latency) * 750 / 1000)) \
  93. >ondemand/sampling_rate
  94. sampling_rate_min:
  95. The sampling rate is limited by the HW transition latency:
  96. transition_latency * 100
  97. Or by kernel restrictions:
  98. If CONFIG_NO_HZ_COMMON is set, the limit is 10ms fixed.
  99. If CONFIG_NO_HZ_COMMON is not set or nohz=off boot parameter is used, the
  100. limits depend on the CONFIG_HZ option:
  101. HZ=1000: min=20000us (20ms)
  102. HZ=250: min=80000us (80ms)
  103. HZ=100: min=200000us (200ms)
  104. The highest value of kernel and HW latency restrictions is shown and
  105. used as the minimum sampling rate.
  106. up_threshold: defines what the average CPU usage between the samplings
  107. of 'sampling_rate' needs to be for the kernel to make a decision on
  108. whether it should increase the frequency. For example when it is set
  109. to its default value of '95' it means that between the checking
  110. intervals the CPU needs to be on average more than 95% in use to then
  111. decide that the CPU frequency needs to be increased.
  112. ignore_nice_load: this parameter takes a value of '0' or '1'. When
  113. set to '0' (its default), all processes are counted towards the
  114. 'cpu utilisation' value. When set to '1', the processes that are
  115. run with a 'nice' value will not count (and thus be ignored) in the
  116. overall usage calculation. This is useful if you are running a CPU
  117. intensive calculation on your laptop that you do not care how long it
  118. takes to complete as you can 'nice' it and prevent it from taking part
  119. in the deciding process of whether to increase your CPU frequency.
  120. sampling_down_factor: this parameter controls the rate at which the
  121. kernel makes a decision on when to decrease the frequency while running
  122. at top speed. When set to 1 (the default) decisions to reevaluate load
  123. are made at the same interval regardless of current clock speed. But
  124. when set to greater than 1 (e.g. 100) it acts as a multiplier for the
  125. scheduling interval for reevaluating load when the CPU is at its top
  126. speed due to high load. This improves performance by reducing the overhead
  127. of load evaluation and helping the CPU stay at its top speed when truly
  128. busy, rather than shifting back and forth in speed. This tunable has no
  129. effect on behavior at lower speeds/lower CPU loads.
  130. powersave_bias: this parameter takes a value between 0 to 1000. It
  131. defines the percentage (times 10) value of the target frequency that
  132. will be shaved off of the target. For example, when set to 100 -- 10%,
  133. when ondemand governor would have targeted 1000 MHz, it will target
  134. 1000 MHz - (10% of 1000 MHz) = 900 MHz instead. This is set to 0
  135. (disabled) by default.
  136. When AMD frequency sensitivity powersave bias driver --
  137. drivers/cpufreq/amd_freq_sensitivity.c is loaded, this parameter
  138. defines the workload frequency sensitivity threshold in which a lower
  139. frequency is chosen instead of ondemand governor's original target.
  140. The frequency sensitivity is a hardware reported (on AMD Family 16h
  141. Processors and above) value between 0 to 100% that tells software how
  142. the performance of the workload running on a CPU will change when
  143. frequency changes. A workload with sensitivity of 0% (memory/IO-bound)
  144. will not perform any better on higher core frequency, whereas a
  145. workload with sensitivity of 100% (CPU-bound) will perform better
  146. higher the frequency. When the driver is loaded, this is set to 400
  147. by default -- for CPUs running workloads with sensitivity value below
  148. 40%, a lower frequency is chosen. Unloading the driver or writing 0
  149. will disable this feature.
  150. 2.5 Conservative
  151. ----------------
  152. The CPUfreq governor "conservative", much like the "ondemand"
  153. governor, sets the CPU depending on the current usage. It differs in
  154. behaviour in that it gracefully increases and decreases the CPU speed
  155. rather than jumping to max speed the moment there is any load on the
  156. CPU. This behaviour more suitable in a battery powered environment.
  157. The governor is tweaked in the same manner as the "ondemand" governor
  158. through sysfs with the addition of:
  159. freq_step: this describes what percentage steps the cpu freq should be
  160. increased and decreased smoothly by. By default the cpu frequency will
  161. increase in 5% chunks of your maximum cpu frequency. You can change this
  162. value to anywhere between 0 and 100 where '0' will effectively lock your
  163. CPU at a speed regardless of its load whilst '100' will, in theory, make
  164. it behave identically to the "ondemand" governor.
  165. down_threshold: same as the 'up_threshold' found for the "ondemand"
  166. governor but for the opposite direction. For example when set to its
  167. default value of '20' it means that if the CPU usage needs to be below
  168. 20% between samples to have the frequency decreased.
  169. sampling_down_factor: similar functionality as in "ondemand" governor.
  170. But in "conservative", it controls the rate at which the kernel makes
  171. a decision on when to decrease the frequency while running in any
  172. speed. Load for frequency increase is still evaluated every
  173. sampling rate.
  174. 3. The Governor Interface in the CPUfreq Core
  175. =============================================
  176. A new governor must register itself with the CPUfreq core using
  177. "cpufreq_register_governor". The struct cpufreq_governor, which has to
  178. be passed to that function, must contain the following values:
  179. governor->name - A unique name for this governor
  180. governor->governor - The governor callback function
  181. governor->owner - .THIS_MODULE for the governor module (if
  182. appropriate)
  183. The governor->governor callback is called with the current (or to-be-set)
  184. cpufreq_policy struct for that CPU, and an unsigned int event. The
  185. following events are currently defined:
  186. CPUFREQ_GOV_START: This governor shall start its duty for the CPU
  187. policy->cpu
  188. CPUFREQ_GOV_STOP: This governor shall end its duty for the CPU
  189. policy->cpu
  190. CPUFREQ_GOV_LIMITS: The limits for CPU policy->cpu have changed to
  191. policy->min and policy->max.
  192. If you need other "events" externally of your driver, _only_ use the
  193. cpufreq_governor_l(unsigned int cpu, unsigned int event) call to the
  194. CPUfreq core to ensure proper locking.
  195. The CPUfreq governor may call the CPU processor driver using one of
  196. these two functions:
  197. int cpufreq_driver_target(struct cpufreq_policy *policy,
  198. unsigned int target_freq,
  199. unsigned int relation);
  200. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  201. unsigned int target_freq,
  202. unsigned int relation);
  203. target_freq must be within policy->min and policy->max, of course.
  204. What's the difference between these two functions? When your governor
  205. still is in a direct code path of a call to governor->governor, the
  206. per-CPU cpufreq lock is still held in the cpufreq core, and there's
  207. no need to lock it again (in fact, this would cause a deadlock). So
  208. use __cpufreq_driver_target only in these cases. In all other cases
  209. (for example, when there's a "daemonized" function that wakes up
  210. every second), use cpufreq_driver_target to lock the cpufreq per-CPU
  211. lock before the command is passed to the cpufreq processor driver.