governors.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/target_index"
  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/target_index 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_COMMON is set, the limit is 10ms fixed.
  100. If CONFIG_NO_HZ_COMMON 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. powersave_bias: this parameter takes a value between 0 to 1000. It
  132. defines the percentage (times 10) value of the target frequency that
  133. will be shaved off of the target. For example, when set to 100 -- 10%,
  134. when ondemand governor would have targeted 1000 MHz, it will target
  135. 1000 MHz - (10% of 1000 MHz) = 900 MHz instead. This is set to 0
  136. (disabled) by default.
  137. When AMD frequency sensitivity powersave bias driver --
  138. drivers/cpufreq/amd_freq_sensitivity.c is loaded, this parameter
  139. defines the workload frequency sensitivity threshold in which a lower
  140. frequency is chosen instead of ondemand governor's original target.
  141. The frequency sensitivity is a hardware reported (on AMD Family 16h
  142. Processors and above) value between 0 to 100% that tells software how
  143. the performance of the workload running on a CPU will change when
  144. frequency changes. A workload with sensitivity of 0% (memory/IO-bound)
  145. will not perform any better on higher core frequency, whereas a
  146. workload with sensitivity of 100% (CPU-bound) will perform better
  147. higher the frequency. When the driver is loaded, this is set to 400
  148. by default -- for CPUs running workloads with sensitivity value below
  149. 40%, a lower frequency is chosen. Unloading the driver or writing 0
  150. will disable this feature.
  151. 2.5 Conservative
  152. ----------------
  153. The CPUfreq governor "conservative", much like the "ondemand"
  154. governor, sets the CPU depending on the current usage. It differs in
  155. behaviour in that it gracefully increases and decreases the CPU speed
  156. rather than jumping to max speed the moment there is any load on the
  157. CPU. This behaviour more suitable in a battery powered environment.
  158. The governor is tweaked in the same manner as the "ondemand" governor
  159. through sysfs with the addition of:
  160. freq_step: this describes what percentage steps the cpu freq should be
  161. increased and decreased smoothly by. By default the cpu frequency will
  162. increase in 5% chunks of your maximum cpu frequency. You can change this
  163. value to anywhere between 0 and 100 where '0' will effectively lock your
  164. CPU at a speed regardless of its load whilst '100' will, in theory, make
  165. it behave identically to the "ondemand" governor.
  166. down_threshold: same as the 'up_threshold' found for the "ondemand"
  167. governor but for the opposite direction. For example when set to its
  168. default value of '20' it means that if the CPU usage needs to be below
  169. 20% between samples to have the frequency decreased.
  170. sampling_down_factor: similar functionality as in "ondemand" governor.
  171. But in "conservative", it controls the rate at which the kernel makes
  172. a decision on when to decrease the frequency while running in any
  173. speed. Load for frequency increase is still evaluated every
  174. sampling rate.
  175. 2.6 Interactive
  176. ---------------
  177. The CPUfreq governor "interactive" is designed for latency-sensitive,
  178. interactive workloads. This governor sets the CPU speed depending on
  179. usage, similar to "ondemand" and "conservative" governors, but with a
  180. different set of configurable behaviors.
  181. The tuneable values for this governor are:
  182. target_loads: CPU load values used to adjust speed to influence the
  183. current CPU load toward that value. In general, the lower the target
  184. load, the more often the governor will raise CPU speeds to bring load
  185. below the target. The format is a single target load, optionally
  186. followed by pairs of CPU speeds and CPU loads to target at or above
  187. those speeds. Colons can be used between the speeds and associated
  188. target loads for readability. For example:
  189. 85 1000000:90 1700000:99
  190. targets CPU load 85% below speed 1GHz, 90% at or above 1GHz, until
  191. 1.7GHz and above, at which load 99% is targeted. If speeds are
  192. specified these must appear in ascending order. Higher target load
  193. values are typically specified for higher speeds, that is, target load
  194. values also usually appear in an ascending order. The default is
  195. target load 90% for all speeds.
  196. min_sample_time: The minimum amount of time to spend at the current
  197. frequency before ramping down. Default is 80000 uS.
  198. hispeed_freq: An intermediate "hi speed" at which to initially ramp
  199. when CPU load hits the value specified in go_hispeed_load. If load
  200. stays high for the amount of time specified in above_hispeed_delay,
  201. then speed may be bumped higher. Default is the maximum speed
  202. allowed by the policy at governor initialization time.
  203. go_hispeed_load: The CPU load at which to ramp to hispeed_freq.
  204. Default is 99%.
  205. above_hispeed_delay: When speed is at or above hispeed_freq, wait for
  206. this long before raising speed in response to continued high load.
  207. The format is a single delay value, optionally followed by pairs of
  208. CPU speeds and the delay to use at or above those speeds. Colons can
  209. be used between the speeds and associated delays for readability. For
  210. example:
  211. 80000 1300000:200000 1500000:40000
  212. uses delay 80000 uS until CPU speed 1.3 GHz, at which speed delay
  213. 200000 uS is used until speed 1.5 GHz, at which speed (and above)
  214. delay 40000 uS is used. If speeds are specified these must appear in
  215. ascending order. Default is 20000 uS.
  216. timer_rate: Sample rate for reevaluating CPU load when the CPU is not
  217. idle. A deferrable timer is used, such that the CPU will not be woken
  218. from idle to service this timer until something else needs to run.
  219. (The maximum time to allow deferring this timer when not running at
  220. minimum speed is configurable via timer_slack.) Default is 20000 uS.
  221. timer_slack: Maximum additional time to defer handling the governor
  222. sampling timer beyond timer_rate when running at speeds above the
  223. minimum. For platforms that consume additional power at idle when
  224. CPUs are running at speeds greater than minimum, this places an upper
  225. bound on how long the timer will be deferred prior to re-evaluating
  226. load and dropping speed. For example, if timer_rate is 20000uS and
  227. timer_slack is 10000uS then timers will be deferred for up to 30msec
  228. when not at lowest speed. A value of -1 means defer timers
  229. indefinitely at all speeds. Default is 80000 uS.
  230. boost: If non-zero, immediately boost speed of all CPUs to at least
  231. hispeed_freq until zero is written to this attribute. If zero, allow
  232. CPU speeds to drop below hispeed_freq according to load as usual.
  233. Default is zero.
  234. boostpulse: On each write, immediately boost speed of all CPUs to
  235. hispeed_freq for at least the period of time specified by
  236. boostpulse_duration, after which speeds are allowed to drop below
  237. hispeed_freq according to load as usual.
  238. boostpulse_duration: Length of time to hold CPU speed at hispeed_freq
  239. on a write to boostpulse, before allowing speed to drop according to
  240. load as usual. Default is 80000 uS.
  241. 3. The Governor Interface in the CPUfreq Core
  242. =============================================
  243. A new governor must register itself with the CPUfreq core using
  244. "cpufreq_register_governor". The struct cpufreq_governor, which has to
  245. be passed to that function, must contain the following values:
  246. governor->name - A unique name for this governor
  247. governor->governor - The governor callback function
  248. governor->owner - .THIS_MODULE for the governor module (if
  249. appropriate)
  250. The governor->governor callback is called with the current (or to-be-set)
  251. cpufreq_policy struct for that CPU, and an unsigned int event. The
  252. following events are currently defined:
  253. CPUFREQ_GOV_START: This governor shall start its duty for the CPU
  254. policy->cpu
  255. CPUFREQ_GOV_STOP: This governor shall end its duty for the CPU
  256. policy->cpu
  257. CPUFREQ_GOV_LIMITS: The limits for CPU policy->cpu have changed to
  258. policy->min and policy->max.
  259. If you need other "events" externally of your driver, _only_ use the
  260. cpufreq_governor_l(unsigned int cpu, unsigned int event) call to the
  261. CPUfreq core to ensure proper locking.
  262. The CPUfreq governor may call the CPU processor driver using one of
  263. these two functions:
  264. int cpufreq_driver_target(struct cpufreq_policy *policy,
  265. unsigned int target_freq,
  266. unsigned int relation);
  267. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  268. unsigned int target_freq,
  269. unsigned int relation);
  270. target_freq must be within policy->min and policy->max, of course.
  271. What's the difference between these two functions? When your governor
  272. still is in a direct code path of a call to governor->governor, the
  273. per-CPU cpufreq lock is still held in the cpufreq core, and there's
  274. no need to lock it again (in fact, this would cause a deadlock). So
  275. use __cpufreq_driver_target only in these cases. In all other cases
  276. (for example, when there's a "daemonized" function that wakes up
  277. every second), use cpufreq_driver_target to lock the cpufreq per-CPU
  278. lock before the command is passed to the cpufreq processor driver.