Advertisement
Guest User

Untitled

a guest
Sep 4th, 2011
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.26 KB | None | 0 0
  1. CPU frequency and voltage scaling code in the Linux(TM) kernel
  2.  
  3.  
  4. L i n u x C P U F r e q
  5.  
  6. C P U F r e q G o v e r n o r s
  7.  
  8. - information for users and developers -
  9.  
  10.  
  11. Dominik Brodowski <linux@brodo.de>
  12. some additions and corrections by Nico Golde <nico@ngolde.de>
  13.  
  14.  
  15.  
  16. Clock scaling allows you to change the clock speed of the CPUs on the
  17. fly. This is a nice method to save battery power, because the lower
  18. the clock speed, the less power the CPU consumes.
  19.  
  20.  
  21. Contents:
  22. ---------
  23. 1. What is a CPUFreq Governor?
  24.  
  25. 2. Governors In the Linux Kernel
  26. 2.1 Performance
  27. 2.2 Powersave
  28. 2.3 Userspace
  29. 2.4 Ondemand
  30. 2.5 Conservative
  31. 2.6 Interactive
  32. 2.7 MinMax
  33. 2.8 SmartassV2
  34.  
  35. 3. The Governor Interface in the CPUfreq Core
  36.  
  37.  
  38.  
  39. 1. What Is A CPUFreq Governor?
  40. ==============================
  41.  
  42. Most cpufreq drivers (in fact, all except one, longrun) or even most
  43. cpu frequency scaling algorithms only offer the CPU to be set to one
  44. frequency. In order to offer dynamic frequency scaling, the cpufreq
  45. core must be able to tell these drivers of a "target frequency". So
  46. these specific drivers will be transformed to offer a "->target"
  47. call instead of the existing "->setpolicy" call. For "longrun", all
  48. stays the same, though.
  49.  
  50. How to decide what frequency within the CPUfreq policy should be used?
  51. That's done using "cpufreq governors". Two are already in this patch
  52. -- they're the already existing "powersave" and "performance" which
  53. set the frequency statically to the lowest or highest frequency,
  54. respectively. At least two more such governors will be ready for
  55. addition in the near future, but likely many more as there are various
  56. different theories and models about dynamic frequency scaling
  57. around. Using such a generic interface as cpufreq offers to scaling
  58. governors, these can be tested extensively, and the best one can be
  59. selected for each specific use.
  60.  
  61. Basically, it's the following flow graph:
  62.  
  63. CPU can be set to switch independently | CPU can only be set
  64. within specific "limits" | to specific frequencies
  65.  
  66. "CPUfreq policy"
  67. consists of frequency limits (policy->{min,max})
  68. and CPUfreq governor to be used
  69. / \
  70. / \
  71. / the cpufreq governor decides
  72. / (dynamically or statically)
  73. / what target_freq to set within
  74. / the limits of policy->{min,max}
  75. / \
  76. / \
  77. Using the ->setpolicy call, Using the ->target call,
  78. the limits and the the frequency closest
  79. "policy" is set. to target_freq is set.
  80. It is assured that it
  81. is within policy->{min,max}
  82.  
  83.  
  84. 2. Governors In the Linux Kernel
  85. ================================
  86.  
  87. 2.1 Performance
  88. ---------------
  89.  
  90. The CPUfreq governor "performance" sets the CPU statically to the
  91. highest frequency within the borders of scaling_min_freq and
  92. scaling_max_freq.
  93.  
  94.  
  95. 2.2 Powersave
  96. -------------
  97.  
  98. The CPUfreq governor "powersave" sets the CPU statically to the
  99. lowest frequency within the borders of scaling_min_freq and
  100. scaling_max_freq.
  101.  
  102.  
  103. 2.3 Userspace
  104. -------------
  105.  
  106. The CPUfreq governor "userspace" allows the user, or any userspace
  107. program running with UID "root", to set the CPU to a specific frequency
  108. by making a sysfs file "scaling_setspeed" available in the CPU-device
  109. directory.
  110.  
  111.  
  112. 2.4 Ondemand
  113. ------------
  114.  
  115. The CPUfreq governor "ondemand" sets the CPU depending on the
  116. current usage. To do this the CPU must have the capability to
  117. switch the frequency very quickly. There are a number of sysfs file
  118. accessible parameters:
  119.  
  120. sampling_rate: measured in uS (10^-6 seconds), this is how often you
  121. want the kernel to look at the CPU usage and to make decisions on
  122. what to do about the frequency. Typically this is set to values of
  123. around '10000' or more.
  124.  
  125. show_sampling_rate_(min|max): the minimum and maximum sampling rates
  126. available that you may set 'sampling_rate' to.
  127.  
  128. up_threshold: defines what the average CPU usage between the samplings
  129. of 'sampling_rate' needs to be for the kernel to make a decision on
  130. whether it should increase the frequency. For example when it is set
  131. to its default value of '80' it means that between the checking
  132. intervals the CPU needs to be on average more than 80% in use to then
  133. decide that the CPU frequency needs to be increased.
  134.  
  135. ignore_nice_load: this parameter takes a value of '0' or '1'. When
  136. set to '0' (its default), all processes are counted towards the
  137. 'cpu utilisation' value. When set to '1', the processes that are
  138. run with a 'nice' value will not count (and thus be ignored) in the
  139. overall usage calculation. This is useful if you are running a CPU
  140. intensive calculation on your laptop that you do not care how long it
  141. takes to complete as you can 'nice' it and prevent it from taking part
  142. in the deciding process of whether to increase your CPU frequency.
  143.  
  144.  
  145. 2.5 Conservative
  146. ----------------
  147.  
  148. The CPUfreq governor "conservative", much like the "ondemand"
  149. governor, sets the CPU depending on the current usage. It differs in
  150. behaviour in that it gracefully increases and decreases the CPU speed
  151. rather than jumping to max speed the moment there is any load on the
  152. CPU. This behaviour more suitable in a battery powered environment.
  153. The governor is tweaked in the same manner as the "ondemand" governor
  154. through sysfs with the addition of:
  155.  
  156. freq_step: this describes what percentage steps the cpu freq should be
  157. increased and decreased smoothly by. By default the cpu frequency will
  158. increase in 5% chunks of your maximum cpu frequency. You can change this
  159. value to anywhere between 0 and 100 where '0' will effectively lock your
  160. CPU at a speed regardless of its load whilst '100' will, in theory, make
  161. it behave identically to the "ondemand" governor.
  162.  
  163. down_threshold: same as the 'up_threshold' found for the "ondemand"
  164. governor but for the opposite direction. For example when set to its
  165. default value of '20' it means that if the CPU usage needs to be below
  166. 20% between samples to have the frequency decreased.
  167.  
  168.  
  169. 2.6 Interactive
  170. ---------------
  171.  
  172. The CPUfreq governor "interactive" is designed for latency-sensitive,
  173. interactive workloads. This governor sets the CPU speed depending on
  174. usage, similar to "ondemand" and "conservative" governors. However,
  175. the governor is more aggressive about scaling the CPU speed up in
  176. response to CPU-intensive activity.
  177.  
  178. Sampling the CPU load every X ms can lead to under-powering the CPU
  179. for X ms, leading to dropped frames, stuttering UI, etc. Instead of
  180. sampling the cpu at a specified rate, the interactive governor will
  181. check whether to scale the cpu frequency up soon after coming out of
  182. idle. When the cpu comes out of idle, a timer is configured to fire
  183. within 1-2 ticks. If the cpu is very busy between exiting idle and
  184. when the timer fires then we assume the cpu is underpowered and ramp
  185. to MAX speed.
  186.  
  187. If the cpu was not sufficiently busy to immediately ramp to MAX speed,
  188. then governor evaluates the cpu load since the last speed adjustment,
  189. choosing th highest value between that longer-term load or the
  190. short-term load since idle exit to determine the cpu speed to ramp to.
  191.  
  192. There is only one tuneable value for this governor:
  193.  
  194. min_sample_time: The minimum amount of time to spend at the current
  195. frequency before ramping down. This is to ensure that the governor has
  196. seen enough historic cpu load data to determine the appropriate
  197. workload. Default is 80000 uS.
  198.  
  199.  
  200. 2.7 MinMax
  201. ---------------
  202.  
  203. The CPUfreq governor "maxmin" tries to minimize the frequency jumps by limiting
  204. the selected frequencies to only two frequencies: either the min or the max
  205. frequency of the current policy. The frequency is raised or lowered according
  206. to the current load and the 'up_threshold' and 'down_threshold'.
  207.  
  208. Its parameters and implementation are similar to that of conservative.
  209.  
  210. It does not have the freq_step parameter as it jumps right from min to max
  211. and vice-versa.
  212.  
  213. The sampling_down_factor, unlike conservative, will count the minimal number
  214. of samplings since the last time we saw the 'up_threshold' load on the CPU.
  215. Hence it is set to higher default and acts as a limiter not to do too many
  216. frequency jumps without hurting the performance.
  217.  
  218.  
  219. 2.8 SmartassV2
  220. ---------------
  221.  
  222. The CPUfreq governor "smartassV2", like other governors, aims to balance
  223. performance vs battery life by using low frequencies when load is low and
  224. ramping the frequency when necessary, fast enough to ensure responsiveness.
  225.  
  226. The implementation of the governor is roughtly based on the idea of interactive.
  227. The idle loop is used to track when the CPU has idle cycles. The idle loop will
  228. set a relatively high rate timer to sample the load when appropriate, the timer
  229. will measure the load since it was set and schedule a work queue task to do the
  230. actual frequency change when necessary.
  231.  
  232. The most important tunable is the "ideal" frequency: this governor will aim
  233. for this frequency, in the sense that it will ramp towards this frequency much
  234. more aggresively than beyond it - both when ramping up from below this frequency
  235. and when ramping down from above this frequency. Still, note, that when load is
  236. low enough the governor should choose the lowest available frequency regardless
  237. of the ideal frequency and similarly when load is consistently high enough the
  238. highest available frequency will be used.
  239.  
  240. Smartass also tracks the state of the screen, and when screen is off (a.k.a
  241. sleep or suspended in the terms of this governor) a different ideal frequency
  242. is used. This is the only difference between the screen on and screen off
  243. states. Proper tuning of the awake_ideal_freq and sleep_ideal_freq should
  244. allow both high responsiveness when screen is on and utilizing the low
  245. frequency range when load is low, especially when screen is off.
  246.  
  247. Finally, smartass is a highly customizable governor with almost everything
  248. tweakable through the sysfs. For a detailed explaination of each tunable,
  249. please see the inline comments at the begging of the code (smartass2.c).
  250.  
  251.  
  252.  
  253. 3. The Governor Interface in the CPUfreq Core
  254. =============================================
  255.  
  256. A new governor must register itself with the CPUfreq core using
  257. "cpufreq_register_governor". The struct cpufreq_governor, which has to
  258. be passed to that function, must contain the following values:
  259.  
  260. governor->name - A unique name for this governor
  261. governor->governor - The governor callback function
  262. governor->owner - .THIS_MODULE for the governor module (if
  263. appropriate)
  264.  
  265. The governor->governor callback is called with the current (or to-be-set)
  266. cpufreq_policy struct for that CPU, and an unsigned int event. The
  267. following events are currently defined:
  268.  
  269. CPUFREQ_GOV_START: This governor shall start its duty for the CPU
  270. policy->cpu
  271. CPUFREQ_GOV_STOP: This governor shall end its duty for the CPU
  272. policy->cpu
  273. CPUFREQ_GOV_LIMITS: The limits for CPU policy->cpu have changed to
  274. policy->min and policy->max.
  275.  
  276. If you need other "events" externally of your driver, _only_ use the
  277. cpufreq_governor_l(unsigned int cpu, unsigned int event) call to the
  278. CPUfreq core to ensure proper locking.
  279.  
  280.  
  281. The CPUfreq governor may call the CPU processor driver using one of
  282. these two functions:
  283.  
  284. int cpufreq_driver_target(struct cpufreq_policy *policy,
  285. unsigned int target_freq,
  286. unsigned int relation);
  287.  
  288. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  289. unsigned int target_freq,
  290. unsigned int relation);
  291.  
  292. target_freq must be within policy->min and policy->max, of course.
  293. What's the difference between these two functions? When your governor
  294. still is in a direct code path of a call to governor->governor, the
  295. per-CPU cpufreq lock is still held in the cpufreq core, and there's
  296. no need to lock it again (in fact, this would cause a deadlock). So
  297. use __cpufreq_driver_target only in these cases. In all other cases
  298. (for example, when there's a "daemonized" function that wakes up
  299. every second), use cpufreq_driver_target to lock the cpufreq per-CPU
  300. lock before the command is passed to the cpufreq processor driver.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement