governors.txt 8.0 KB

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