governors.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. 2.6 Interactive
  20. 3. The Governor Interface in the CPUfreq Core
  21. 1. What Is A CPUFreq Governor?
  22. ==============================
  23. Most cpufreq drivers (in fact, all except one, longrun) or even most
  24. cpu frequency scaling algorithms only offer the CPU to be set to one
  25. frequency. In order to offer dynamic frequency scaling, the cpufreq
  26. core must be able to tell these drivers of a "target frequency". So
  27. these specific drivers will be transformed to offer a "->target"
  28. call instead of the existing "->setpolicy" call. For "longrun", all
  29. stays the same, though.
  30. How to decide what frequency within the CPUfreq policy should be used?
  31. That's done using "cpufreq governors". Two are already in this patch
  32. -- they're the already existing "powersave" and "performance" which
  33. set the frequency statically to the lowest or highest frequency,
  34. respectively. At least two more such governors will be ready for
  35. addition in the near future, but likely many more as there are various
  36. different theories and models about dynamic frequency scaling
  37. around. Using such a generic interface as cpufreq offers to scaling
  38. governors, these can be tested extensively, and the best one can be
  39. selected for each specific use.
  40. Basically, it's the following flow graph:
  41. CPU can be set to switch independently | CPU can only be set
  42. within specific "limits" | to specific frequencies
  43. "CPUfreq policy"
  44. consists of frequency limits (policy->{min,max})
  45. and CPUfreq governor to be used
  46. / \
  47. / \
  48. / the cpufreq governor decides
  49. / (dynamically or statically)
  50. / what target_freq to set within
  51. / the limits of policy->{min,max}
  52. / \
  53. / \
  54. Using the ->setpolicy call, Using the ->target call,
  55. the limits and the the frequency closest
  56. "policy" is set. to target_freq is set.
  57. It is assured that it
  58. is within policy->{min,max}
  59. 2. Governors In the Linux Kernel
  60. ================================
  61. 2.1 Performance
  62. ---------------
  63. The CPUfreq governor "performance" sets the CPU statically to the
  64. highest frequency within the borders of scaling_min_freq and
  65. scaling_max_freq.
  66. 2.2 Powersave
  67. -------------
  68. The CPUfreq governor "powersave" sets the CPU statically to the
  69. lowest frequency within the borders of scaling_min_freq and
  70. scaling_max_freq.
  71. 2.3 Userspace
  72. -------------
  73. The CPUfreq governor "userspace" allows the user, or any userspace
  74. program running with UID "root", to set the CPU to a specific frequency
  75. by making a sysfs file "scaling_setspeed" available in the CPU-device
  76. directory.
  77. 2.4 Ondemand
  78. ------------
  79. The CPUfreq governor "ondemand" sets the CPU depending on the
  80. current usage. To do this the CPU must have the capability to
  81. switch the frequency very quickly. There are a number of sysfs file
  82. accessible parameters:
  83. sampling_rate: measured in uS (10^-6 seconds), this is how often you
  84. want the kernel to look at the CPU usage and to make decisions on
  85. what to do about the frequency. Typically this is set to values of
  86. around '10000' or more. It's default value is (cmp. with users-guide.txt):
  87. transition_latency * 1000
  88. Be aware that transition latency is in ns and sampling_rate is in us, so you
  89. get the same sysfs value by default.
  90. Sampling rate should always get adjusted considering the transition latency
  91. To set the sampling rate 750 times as high as the transition latency
  92. in the bash (as said, 1000 is default), do:
  93. echo `$(($(cat cpuinfo_transition_latency) * 750 / 1000)) \
  94. >ondemand/sampling_rate
  95. sampling_rate_min:
  96. The sampling rate is limited by the HW transition latency:
  97. transition_latency * 100
  98. Or by kernel restrictions:
  99. If CONFIG_NO_HZ is set, the limit is 10ms fixed.
  100. If CONFIG_NO_HZ is not set or nohz=off boot parameter is used, the
  101. limits depend on the CONFIG_HZ option:
  102. HZ=1000: min=20000us (20ms)
  103. HZ=250: min=80000us (80ms)
  104. HZ=100: min=200000us (200ms)
  105. The highest value of kernel and HW latency restrictions is shown and
  106. used as the minimum sampling rate.
  107. up_threshold: defines what the average CPU usage between the samplings
  108. of 'sampling_rate' needs to be for the kernel to make a decision on
  109. whether it should increase the frequency. For example when it is set
  110. to its default value of '95' it means that between the checking
  111. intervals the CPU needs to be on average more than 95% in use to then
  112. decide that the CPU frequency needs to be increased.
  113. ignore_nice_load: this parameter takes a value of '0' or '1'. When
  114. set to '0' (its default), all processes are counted towards the
  115. 'cpu utilisation' value. When set to '1', the processes that are
  116. run with a 'nice' value will not count (and thus be ignored) in the
  117. overall usage calculation. This is useful if you are running a CPU
  118. intensive calculation on your laptop that you do not care how long it
  119. takes to complete as you can 'nice' it and prevent it from taking part
  120. in the deciding process of whether to increase your CPU frequency.
  121. sampling_down_factor: this parameter controls the rate at which the
  122. kernel makes a decision on when to decrease the frequency while running
  123. at top speed. When set to 1 (the default) decisions to reevaluate load
  124. are made at the same interval regardless of current clock speed. But
  125. when set to greater than 1 (e.g. 100) it acts as a multiplier for the
  126. scheduling interval for reevaluating load when the CPU is at its top
  127. speed due to high load. This improves performance by reducing the overhead
  128. of load evaluation and helping the CPU stay at its top speed when truly
  129. busy, rather than shifting back and forth in speed. This tunable has no
  130. effect on behavior at lower speeds/lower CPU loads.
  131. 2.5 Conservative
  132. ----------------
  133. The CPUfreq governor "conservative", much like the "ondemand"
  134. governor, sets the CPU depending on the current usage. It differs in
  135. behaviour in that it gracefully increases and decreases the CPU speed
  136. rather than jumping to max speed the moment there is any load on the
  137. CPU. This behaviour more suitable in a battery powered environment.
  138. The governor is tweaked in the same manner as the "ondemand" governor
  139. through sysfs with the addition of:
  140. freq_step: this describes what percentage steps the cpu freq should be
  141. increased and decreased smoothly by. By default the cpu frequency will
  142. increase in 5% chunks of your maximum cpu frequency. You can change this
  143. value to anywhere between 0 and 100 where '0' will effectively lock your
  144. CPU at a speed regardless of its load whilst '100' will, in theory, make
  145. it behave identically to the "ondemand" governor.
  146. down_threshold: same as the 'up_threshold' found for the "ondemand"
  147. governor but for the opposite direction. For example when set to its
  148. default value of '20' it means that if the CPU usage needs to be below
  149. 20% between samples to have the frequency decreased.
  150. 2.6 Interactive
  151. ---------------
  152. The CPUfreq governor "interactive" is designed for latency-sensitive,
  153. interactive workloads. This governor sets the CPU speed depending on
  154. usage, similar to "ondemand" and "conservative" governors, but with a
  155. different set of configurable behaviors.
  156. The tuneable values for this governor are:
  157. target_loads: CPU load values used to adjust speed to influence the
  158. current CPU load toward that value. In general, the lower the target
  159. load, the more often the governor will raise CPU speeds to bring load
  160. below the target. The format is a single target load, optionally
  161. followed by pairs of CPU speeds and CPU loads to target at or above
  162. those speeds. Colons can be used between the speeds and associated
  163. target loads for readability. For example:
  164. 85 1000000:90 1700000:99
  165. targets CPU load 85% below speed 1GHz, 90% at or above 1GHz, until
  166. 1.7GHz and above, at which load 99% is targeted. If speeds are
  167. specified these must appear in ascending order. Higher target load
  168. values are typically specified for higher speeds, that is, target load
  169. values also usually appear in an ascending order. The default is
  170. target load 90% for all speeds.
  171. min_sample_time: The minimum amount of time to spend at the current
  172. frequency before ramping down. Default is 80000 uS.
  173. hispeed_freq: An intermediate "hi speed" at which to initially ramp
  174. when CPU load hits the value specified in go_hispeed_load. If load
  175. stays high for the amount of time specified in above_hispeed_delay,
  176. then speed may be bumped higher. Default is the maximum speed
  177. allowed by the policy at governor initialization time.
  178. go_hispeed_load: The CPU load at which to ramp to hispeed_freq.
  179. Default is 99%.
  180. above_hispeed_delay: When speed is at or above hispeed_freq, wait for
  181. this long before raising speed in response to continued high load.
  182. The format is a single delay value, optionally followed by pairs of
  183. CPU speeds and the delay to use at or above those speeds. Colons can
  184. be used between the speeds and associated delays for readability. For
  185. example:
  186. 80000 1300000:200000 1500000:40000
  187. uses delay 80000 uS until CPU speed 1.3 GHz, at which speed delay
  188. 200000 uS is used until speed 1.5 GHz, at which speed (and above)
  189. delay 40000 uS is used. If speeds are specified these must appear in
  190. ascending order. Default is 20000 uS.
  191. timer_rate: Sample rate for reevaluating CPU load when the CPU is not
  192. idle. A deferrable timer is used, such that the CPU will not be woken
  193. from idle to service this timer until something else needs to run.
  194. (The maximum time to allow deferring this timer when not running at
  195. minimum speed is configurable via timer_slack.) Default is 20000 uS.
  196. timer_slack: Maximum additional time to defer handling the governor
  197. sampling timer beyond timer_rate when running at speeds above the
  198. minimum. For platforms that consume additional power at idle when
  199. CPUs are running at speeds greater than minimum, this places an upper
  200. bound on how long the timer will be deferred prior to re-evaluating
  201. load and dropping speed. For example, if timer_rate is 20000uS and
  202. timer_slack is 10000uS then timers will be deferred for up to 30msec
  203. when not at lowest speed. A value of -1 means defer timers
  204. indefinitely at all speeds. Default is 80000 uS.
  205. boost: If non-zero, immediately boost speed of all CPUs to at least
  206. hispeed_freq until zero is written to this attribute. If zero, allow
  207. CPU speeds to drop below hispeed_freq according to load as usual.
  208. Default is zero.
  209. boostpulse: On each write, immediately boost speed of all CPUs to
  210. hispeed_freq for at least the period of time specified by
  211. boostpulse_duration, after which speeds are allowed to drop below
  212. hispeed_freq according to load as usual.
  213. boostpulse_duration: Length of time to hold CPU speed at hispeed_freq
  214. on a write to boostpulse, before allowing speed to drop according to
  215. load as usual. Default is 80000 uS.
  216. 3. The Governor Interface in the CPUfreq Core
  217. =============================================
  218. A new governor must register itself with the CPUfreq core using
  219. "cpufreq_register_governor". The struct cpufreq_governor, which has to
  220. be passed to that function, must contain the following values:
  221. governor->name - A unique name for this governor
  222. governor->governor - The governor callback function
  223. governor->owner - .THIS_MODULE for the governor module (if
  224. appropriate)
  225. The governor->governor callback is called with the current (or to-be-set)
  226. cpufreq_policy struct for that CPU, and an unsigned int event. The
  227. following events are currently defined:
  228. CPUFREQ_GOV_START: This governor shall start its duty for the CPU
  229. policy->cpu
  230. CPUFREQ_GOV_STOP: This governor shall end its duty for the CPU
  231. policy->cpu
  232. CPUFREQ_GOV_LIMITS: The limits for CPU policy->cpu have changed to
  233. policy->min and policy->max.
  234. If you need other "events" externally of your driver, _only_ use the
  235. cpufreq_governor_l(unsigned int cpu, unsigned int event) call to the
  236. CPUfreq core to ensure proper locking.
  237. The CPUfreq governor may call the CPU processor driver using one of
  238. these two functions:
  239. int cpufreq_driver_target(struct cpufreq_policy *policy,
  240. unsigned int target_freq,
  241. unsigned int relation);
  242. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  243. unsigned int target_freq,
  244. unsigned int relation);
  245. target_freq must be within policy->min and policy->max, of course.
  246. What's the difference between these two functions? When your governor
  247. still is in a direct code path of a call to governor->governor, the
  248. per-CPU cpufreq lock is still held in the cpufreq core, and there's
  249. no need to lock it again (in fact, this would cause a deadlock). So
  250. use __cpufreq_driver_target only in these cases. In all other cases
  251. (for example, when there's a "daemonized" function that wakes up
  252. every second), use cpufreq_driver_target to lock the cpufreq per-CPU
  253. lock before the command is passed to the cpufreq processor driver.