governors.txt 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. It's default value is (cmp. with users-guide.txt):
  86. transition_latency * 1000
  87. The lowest value you can set is:
  88. transition_latency * 100 or it may get restricted to a value where it
  89. makes not sense for the kernel anymore to poll that often which depends
  90. on your HZ config variable (HZ=1000: max=20000us, HZ=250: max=5000).
  91. Be aware that transition latency is in ns and sampling_rate is in us, so you
  92. get the same sysfs value by default.
  93. Sampling rate should always get adjusted considering the transition latency
  94. To set the sampling rate 750 times as high as the transition latency
  95. in the bash (as said, 1000 is default), do:
  96. echo `$(($(cat cpuinfo_transition_latency) * 750 / 1000)) \
  97. >ondemand/sampling_rate
  98. show_sampling_rate_(min|max): THIS INTERFACE IS DEPRECATED, DON'T USE IT.
  99. You can use wider ranges now and the general
  100. cpuinfo_transition_latency variable (cmp. with user-guide.txt) can be
  101. used to obtain exactly the same info:
  102. show_sampling_rate_min = transtition_latency * 500 / 1000
  103. show_sampling_rate_max = transtition_latency * 500000 / 1000
  104. (divided by 1000 is to illustrate that sampling rate is in us and
  105. transition latency is exported ns).
  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 '80' it means that between the checking
  110. intervals the CPU needs to be on average more than 80% 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. 2.5 Conservative
  121. ----------------
  122. The CPUfreq governor "conservative", much like the "ondemand"
  123. governor, sets the CPU depending on the current usage. It differs in
  124. behaviour in that it gracefully increases and decreases the CPU speed
  125. rather than jumping to max speed the moment there is any load on the
  126. CPU. This behaviour more suitable in a battery powered environment.
  127. The governor is tweaked in the same manner as the "ondemand" governor
  128. through sysfs with the addition of:
  129. freq_step: this describes what percentage steps the cpu freq should be
  130. increased and decreased smoothly by. By default the cpu frequency will
  131. increase in 5% chunks of your maximum cpu frequency. You can change this
  132. value to anywhere between 0 and 100 where '0' will effectively lock your
  133. CPU at a speed regardless of its load whilst '100' will, in theory, make
  134. it behave identically to the "ondemand" governor.
  135. down_threshold: same as the 'up_threshold' found for the "ondemand"
  136. governor but for the opposite direction. For example when set to its
  137. default value of '20' it means that if the CPU usage needs to be below
  138. 20% between samples to have the frequency decreased.
  139. 3. The Governor Interface in the CPUfreq Core
  140. =============================================
  141. A new governor must register itself with the CPUfreq core using
  142. "cpufreq_register_governor". The struct cpufreq_governor, which has to
  143. be passed to that function, must contain the following values:
  144. governor->name - A unique name for this governor
  145. governor->governor - The governor callback function
  146. governor->owner - .THIS_MODULE for the governor module (if
  147. appropriate)
  148. The governor->governor callback is called with the current (or to-be-set)
  149. cpufreq_policy struct for that CPU, and an unsigned int event. The
  150. following events are currently defined:
  151. CPUFREQ_GOV_START: This governor shall start its duty for the CPU
  152. policy->cpu
  153. CPUFREQ_GOV_STOP: This governor shall end its duty for the CPU
  154. policy->cpu
  155. CPUFREQ_GOV_LIMITS: The limits for CPU policy->cpu have changed to
  156. policy->min and policy->max.
  157. If you need other "events" externally of your driver, _only_ use the
  158. cpufreq_governor_l(unsigned int cpu, unsigned int event) call to the
  159. CPUfreq core to ensure proper locking.
  160. The CPUfreq governor may call the CPU processor driver using one of
  161. these two functions:
  162. int cpufreq_driver_target(struct cpufreq_policy *policy,
  163. unsigned int target_freq,
  164. unsigned int relation);
  165. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  166. unsigned int target_freq,
  167. unsigned int relation);
  168. target_freq must be within policy->min and policy->max, of course.
  169. What's the difference between these two functions? When your governor
  170. still is in a direct code path of a call to governor->governor, the
  171. per-CPU cpufreq lock is still held in the cpufreq core, and there's
  172. no need to lock it again (in fact, this would cause a deadlock). So
  173. use __cpufreq_driver_target only in these cases. In all other cases
  174. (for example, when there's a "daemonized" function that wakes up
  175. every second), use cpufreq_driver_target to lock the cpufreq per-CPU
  176. lock before the command is passed to the cpufreq processor driver.