intel_powerclamp.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. =======================
  2. INTEL POWERCLAMP DRIVER
  3. =======================
  4. By: Arjan van de Ven <arjan@linux.intel.com>
  5. Jacob Pan <jacob.jun.pan@linux.intel.com>
  6. Contents:
  7. (*) Introduction
  8. - Goals and Objectives
  9. (*) Theory of Operation
  10. - Idle Injection
  11. - Calibration
  12. (*) Performance Analysis
  13. - Effectiveness and Limitations
  14. - Power vs Performance
  15. - Scalability
  16. - Calibration
  17. - Comparison with Alternative Techniques
  18. (*) Usage and Interfaces
  19. - Generic Thermal Layer (sysfs)
  20. - Kernel APIs (TBD)
  21. ============
  22. INTRODUCTION
  23. ============
  24. Consider the situation where a system’s power consumption must be
  25. reduced at runtime, due to power budget, thermal constraint, or noise
  26. level, and where active cooling is not preferred. Software managed
  27. passive power reduction must be performed to prevent the hardware
  28. actions that are designed for catastrophic scenarios.
  29. Currently, P-states, T-states (clock modulation), and CPU offlining
  30. are used for CPU throttling.
  31. On Intel CPUs, C-states provide effective power reduction, but so far
  32. they’re only used opportunistically, based on workload. With the
  33. development of intel_powerclamp driver, the method of synchronizing
  34. idle injection across all online CPU threads was introduced. The goal
  35. is to achieve forced and controllable C-state residency.
  36. Test/Analysis has been made in the areas of power, performance,
  37. scalability, and user experience. In many cases, clear advantage is
  38. shown over taking the CPU offline or modulating the CPU clock.
  39. ===================
  40. THEORY OF OPERATION
  41. ===================
  42. Idle Injection
  43. --------------
  44. On modern Intel processors (Nehalem or later), package level C-state
  45. residency is available in MSRs, thus also available to the kernel.
  46. These MSRs are:
  47. #define MSR_PKG_C2_RESIDENCY 0x60D
  48. #define MSR_PKG_C3_RESIDENCY 0x3F8
  49. #define MSR_PKG_C6_RESIDENCY 0x3F9
  50. #define MSR_PKG_C7_RESIDENCY 0x3FA
  51. If the kernel can also inject idle time to the system, then a
  52. closed-loop control system can be established that manages package
  53. level C-state. The intel_powerclamp driver is conceived as such a
  54. control system, where the target set point is a user-selected idle
  55. ratio (based on power reduction), and the error is the difference
  56. between the actual package level C-state residency ratio and the target idle
  57. ratio.
  58. Injection is controlled by high priority kernel threads, spawned for
  59. each online CPU.
  60. These kernel threads, with SCHED_FIFO class, are created to perform
  61. clamping actions of controlled duty ratio and duration. Each per-CPU
  62. thread synchronizes its idle time and duration, based on the rounding
  63. of jiffies, so accumulated errors can be prevented to avoid a jittery
  64. effect. Threads are also bound to the CPU such that they cannot be
  65. migrated, unless the CPU is taken offline. In this case, threads
  66. belong to the offlined CPUs will be terminated immediately.
  67. Running as SCHED_FIFO and relatively high priority, also allows such
  68. scheme to work for both preemptable and non-preemptable kernels.
  69. Alignment of idle time around jiffies ensures scalability for HZ
  70. values. This effect can be better visualized using a Perf timechart.
  71. The following diagram shows the behavior of kernel thread
  72. kidle_inject/cpu. During idle injection, it runs monitor/mwait idle
  73. for a given "duration", then relinquishes the CPU to other tasks,
  74. until the next time interval.
  75. The NOHZ schedule tick is disabled during idle time, but interrupts
  76. are not masked. Tests show that the extra wakeups from scheduler tick
  77. have a dramatic impact on the effectiveness of the powerclamp driver
  78. on large scale systems (Westmere system with 80 processors).
  79. CPU0
  80. ____________ ____________
  81. kidle_inject/0 | sleep | mwait | sleep |
  82. _________| |________| |_______
  83. duration
  84. CPU1
  85. ____________ ____________
  86. kidle_inject/1 | sleep | mwait | sleep |
  87. _________| |________| |_______
  88. ^
  89. |
  90. |
  91. roundup(jiffies, interval)
  92. Only one CPU is allowed to collect statistics and update global
  93. control parameters. This CPU is referred to as the controlling CPU in
  94. this document. The controlling CPU is elected at runtime, with a
  95. policy that favors BSP, taking into account the possibility of a CPU
  96. hot-plug.
  97. In terms of dynamics of the idle control system, package level idle
  98. time is considered largely as a non-causal system where its behavior
  99. cannot be based on the past or current input. Therefore, the
  100. intel_powerclamp driver attempts to enforce the desired idle time
  101. instantly as given input (target idle ratio). After injection,
  102. powerclamp moniors the actual idle for a given time window and adjust
  103. the next injection accordingly to avoid over/under correction.
  104. When used in a causal control system, such as a temperature control,
  105. it is up to the user of this driver to implement algorithms where
  106. past samples and outputs are included in the feedback. For example, a
  107. PID-based thermal controller can use the powerclamp driver to
  108. maintain a desired target temperature, based on integral and
  109. derivative gains of the past samples.
  110. Calibration
  111. -----------
  112. During scalability testing, it is observed that synchronized actions
  113. among CPUs become challenging as the number of cores grows. This is
  114. also true for the ability of a system to enter package level C-states.
  115. To make sure the intel_powerclamp driver scales well, online
  116. calibration is implemented. The goals for doing such a calibration
  117. are:
  118. a) determine the effective range of idle injection ratio
  119. b) determine the amount of compensation needed at each target ratio
  120. Compensation to each target ratio consists of two parts:
  121. a) steady state error compensation
  122. This is to offset the error occurring when the system can
  123. enter idle without extra wakeups (such as external interrupts).
  124. b) dynamic error compensation
  125. When an excessive amount of wakeups occurs during idle, an
  126. additional idle ratio can be added to quiet interrupts, by
  127. slowing down CPU activities.
  128. A debugfs file is provided for the user to examine compensation
  129. progress and results, such as on a Westmere system.
  130. [jacob@nex01 ~]$ cat
  131. /sys/kernel/debug/intel_powerclamp/powerclamp_calib
  132. controlling cpu: 0
  133. pct confidence steady dynamic (compensation)
  134. 0 0 0 0
  135. 1 1 0 0
  136. 2 1 1 0
  137. 3 3 1 0
  138. 4 3 1 0
  139. 5 3 1 0
  140. 6 3 1 0
  141. 7 3 1 0
  142. 8 3 1 0
  143. ...
  144. 30 3 2 0
  145. 31 3 2 0
  146. 32 3 1 0
  147. 33 3 2 0
  148. 34 3 1 0
  149. 35 3 2 0
  150. 36 3 1 0
  151. 37 3 2 0
  152. 38 3 1 0
  153. 39 3 2 0
  154. 40 3 3 0
  155. 41 3 1 0
  156. 42 3 2 0
  157. 43 3 1 0
  158. 44 3 1 0
  159. 45 3 2 0
  160. 46 3 3 0
  161. 47 3 0 0
  162. 48 3 2 0
  163. 49 3 3 0
  164. Calibration occurs during runtime. No offline method is available.
  165. Steady state compensation is used only when confidence levels of all
  166. adjacent ratios have reached satisfactory level. A confidence level
  167. is accumulated based on clean data collected at runtime. Data
  168. collected during a period without extra interrupts is considered
  169. clean.
  170. To compensate for excessive amounts of wakeup during idle, additional
  171. idle time is injected when such a condition is detected. Currently,
  172. we have a simple algorithm to double the injection ratio. A possible
  173. enhancement might be to throttle the offending IRQ, such as delaying
  174. EOI for level triggered interrupts. But it is a challenge to be
  175. non-intrusive to the scheduler or the IRQ core code.
  176. CPU Online/Offline
  177. ------------------
  178. Per-CPU kernel threads are started/stopped upon receiving
  179. notifications of CPU hotplug activities. The intel_powerclamp driver
  180. keeps track of clamping kernel threads, even after they are migrated
  181. to other CPUs, after a CPU offline event.
  182. =====================
  183. Performance Analysis
  184. =====================
  185. This section describes the general performance data collected on
  186. multiple systems, including Westmere (80P) and Ivy Bridge (4P, 8P).
  187. Effectiveness and Limitations
  188. -----------------------------
  189. The maximum range that idle injection is allowed is capped at 50
  190. percent. As mentioned earlier, since interrupts are allowed during
  191. forced idle time, excessive interrupts could result in less
  192. effectiveness. The extreme case would be doing a ping -f to generated
  193. flooded network interrupts without much CPU acknowledgement. In this
  194. case, little can be done from the idle injection threads. In most
  195. normal cases, such as scp a large file, applications can be throttled
  196. by the powerclamp driver, since slowing down the CPU also slows down
  197. network protocol processing, which in turn reduces interrupts.
  198. When control parameters change at runtime by the controlling CPU, it
  199. may take an additional period for the rest of the CPUs to catch up
  200. with the changes. During this time, idle injection is out of sync,
  201. thus not able to enter package C- states at the expected ratio. But
  202. this effect is minor, in that in most cases change to the target
  203. ratio is updated much less frequently than the idle injection
  204. frequency.
  205. Scalability
  206. -----------
  207. Tests also show a minor, but measurable, difference between the 4P/8P
  208. Ivy Bridge system and the 80P Westmere server under 50% idle ratio.
  209. More compensation is needed on Westmere for the same amount of
  210. target idle ratio. The compensation also increases as the idle ratio
  211. gets larger. The above reason constitutes the need for the
  212. calibration code.
  213. On the IVB 8P system, compared to an offline CPU, powerclamp can
  214. achieve up to 40% better performance per watt. (measured by a spin
  215. counter summed over per CPU counting threads spawned for all running
  216. CPUs).
  217. ====================
  218. Usage and Interfaces
  219. ====================
  220. The powerclamp driver is registered to the generic thermal layer as a
  221. cooling device. Currently, it’s not bound to any thermal zones.
  222. jacob@chromoly:/sys/class/thermal/cooling_device14$ grep . *
  223. cur_state:0
  224. max_state:50
  225. type:intel_powerclamp
  226. Example usage:
  227. - To inject 25% idle time
  228. $ sudo sh -c "echo 25 > /sys/class/thermal/cooling_device80/cur_state
  229. "
  230. If the system is not busy and has more than 25% idle time already,
  231. then the powerclamp driver will not start idle injection. Using Top
  232. will not show idle injection kernel threads.
  233. If the system is busy (spin test below) and has less than 25% natural
  234. idle time, powerclamp kernel threads will do idle injection, which
  235. appear running to the scheduler. But the overall system idle is still
  236. reflected. In this example, 24.1% idle is shown. This helps the
  237. system admin or user determine the cause of slowdown, when a
  238. powerclamp driver is in action.
  239. Tasks: 197 total, 1 running, 196 sleeping, 0 stopped, 0 zombie
  240. Cpu(s): 71.2%us, 4.7%sy, 0.0%ni, 24.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
  241. Mem: 3943228k total, 1689632k used, 2253596k free, 74960k buffers
  242. Swap: 4087804k total, 0k used, 4087804k free, 945336k cached
  243. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  244. 3352 jacob 20 0 262m 644 428 S 286 0.0 0:17.16 spin
  245. 3341 root -51 0 0 0 0 D 25 0.0 0:01.62 kidle_inject/0
  246. 3344 root -51 0 0 0 0 D 25 0.0 0:01.60 kidle_inject/3
  247. 3342 root -51 0 0 0 0 D 25 0.0 0:01.61 kidle_inject/1
  248. 3343 root -51 0 0 0 0 D 25 0.0 0:01.60 kidle_inject/2
  249. 2935 jacob 20 0 696m 125m 35m S 5 3.3 0:31.11 firefox
  250. 1546 root 20 0 158m 20m 6640 S 3 0.5 0:26.97 Xorg
  251. 2100 jacob 20 0 1223m 88m 30m S 3 2.3 0:23.68 compiz
  252. Tests have shown that by using the powerclamp driver as a cooling
  253. device, a PID based userspace thermal controller can manage to
  254. control CPU temperature effectively, when no other thermal influence
  255. is added. For example, a UltraBook user can compile the kernel under
  256. certain temperature (below most active trip points).