governors.txt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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): THIS INTERFACE IS DEPRECATED, DON'T USE IT.
  87. You can use wider ranges now and the general
  88. cpuinfo_transition_latency variable (cmp. with user-guide.txt) can be
  89. used to obtain exactly the same info:
  90. show_sampling_rate_min = transtition_latency * 500 / 1000
  91. show_sampling_rate_max = transtition_latency * 500000 / 1000
  92. (divided by 1000 is to illustrate that sampling rate is in us and
  93. transition latency is exported ns).
  94. up_threshold: defines what the average CPU usage between the samplings
  95. of 'sampling_rate' needs to be for the kernel to make a decision on
  96. whether it should increase the frequency. For example when it is set
  97. to its default value of '80' it means that between the checking
  98. intervals the CPU needs to be on average more than 80% in use to then
  99. decide that the CPU frequency needs to be increased.
  100. ignore_nice_load: this parameter takes a value of '0' or '1'. When
  101. set to '0' (its default), all processes are counted towards the
  102. 'cpu utilisation' value. When set to '1', the processes that are
  103. run with a 'nice' value will not count (and thus be ignored) in the
  104. overall usage calculation. This is useful if you are running a CPU
  105. intensive calculation on your laptop that you do not care how long it
  106. takes to complete as you can 'nice' it and prevent it from taking part
  107. in the deciding process of whether to increase your CPU frequency.
  108. 2.5 Conservative
  109. ----------------
  110. The CPUfreq governor "conservative", much like the "ondemand"
  111. governor, sets the CPU depending on the current usage. It differs in
  112. behaviour in that it gracefully increases and decreases the CPU speed
  113. rather than jumping to max speed the moment there is any load on the
  114. CPU. This behaviour more suitable in a battery powered environment.
  115. The governor is tweaked in the same manner as the "ondemand" governor
  116. through sysfs with the addition of:
  117. freq_step: this describes what percentage steps the cpu freq should be
  118. increased and decreased smoothly by. By default the cpu frequency will
  119. increase in 5% chunks of your maximum cpu frequency. You can change this
  120. value to anywhere between 0 and 100 where '0' will effectively lock your
  121. CPU at a speed regardless of its load whilst '100' will, in theory, make
  122. it behave identically to the "ondemand" governor.
  123. down_threshold: same as the 'up_threshold' found for the "ondemand"
  124. governor but for the opposite direction. For example when set to its
  125. default value of '20' it means that if the CPU usage needs to be below
  126. 20% between samples to have the frequency decreased.
  127. 3. The Governor Interface in the CPUfreq Core
  128. =============================================
  129. A new governor must register itself with the CPUfreq core using
  130. "cpufreq_register_governor". The struct cpufreq_governor, which has to
  131. be passed to that function, must contain the following values:
  132. governor->name - A unique name for this governor
  133. governor->governor - The governor callback function
  134. governor->owner - .THIS_MODULE for the governor module (if
  135. appropriate)
  136. The governor->governor callback is called with the current (or to-be-set)
  137. cpufreq_policy struct for that CPU, and an unsigned int event. The
  138. following events are currently defined:
  139. CPUFREQ_GOV_START: This governor shall start its duty for the CPU
  140. policy->cpu
  141. CPUFREQ_GOV_STOP: This governor shall end its duty for the CPU
  142. policy->cpu
  143. CPUFREQ_GOV_LIMITS: The limits for CPU policy->cpu have changed to
  144. policy->min and policy->max.
  145. If you need other "events" externally of your driver, _only_ use the
  146. cpufreq_governor_l(unsigned int cpu, unsigned int event) call to the
  147. CPUfreq core to ensure proper locking.
  148. The CPUfreq governor may call the CPU processor driver using one of
  149. these two functions:
  150. int cpufreq_driver_target(struct cpufreq_policy *policy,
  151. unsigned int target_freq,
  152. unsigned int relation);
  153. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  154. unsigned int target_freq,
  155. unsigned int relation);
  156. target_freq must be within policy->min and policy->max, of course.
  157. What's the difference between these two functions? When your governor
  158. still is in a direct code path of a call to governor->governor, the
  159. per-CPU cpufreq lock is still held in the cpufreq core, and there's
  160. no need to lock it again (in fact, this would cause a deadlock). So
  161. use __cpufreq_driver_target only in these cases. In all other cases
  162. (for example, when there's a "daemonized" function that wakes up
  163. every second), use cpufreq_driver_target to lock the cpufreq per-CPU
  164. lock before the command is passed to the cpufreq processor driver.