Bug 1839454 - Don't try and call gcparam in the browser when running JS reftests...
[gecko.git] / docs / performance / power_profiling_overview.md
blobbb8f511fe2ba4700e8f992422f19d455b88db2ba
1 # Power profiling
3 This article covers important background information about power
4 profiling, with an emphasis on Intel processors used in desktop and
5 laptop machines. It serves as a starting point for anybody doing power
6 profiling for the first time.
8 ## Basic physics concepts
10 In physics, *[power](https://en.wikipedia.org/wiki/Power_%28physics%29)*
11 is the rate of doing
12 *[work](https://en.wikipedia.org/wiki/Work_%28physics%29 "Work (physics)")*.
13 It is equivalent to an amount of
14 *[energy](https://en.wikipedia.org/wiki/Energy_%28physics%29 "Energy (physics)"){.mw-redirect}*
15 consumed per unit time. In SI units, energy is measured in Joules, and
16 power is measured in Watts, which is equivalent to Joules per second.
18 Although power is an instantaneous concept, in practice measurements of
19 it are determined in a non-instantaneous fashion, i.e. by dividing an
20 energy amount by a non-infinitesimal time period. Strictly speaking,
21 such a computation gives the *average power* but this is often referred
22 to as just the *power* when context makes it clear.
24 In the context of computing, a fully-charged mobile device battery (as
25 found in a laptop or smartphone) holds a certain amount of energy, and
26 the speed at which that stored energy is depleted depends on the power
27 consumption of the mobile device. That in turn depends on the software
28 running on the device. Web browsers are popular applications and can be
29 power-intensive, and therefore can significantly affect battery life. As
30 a result, it is worth optimizing (i.e. reducing) the power consumption
31 caused by Firefox and Firefox OS.
33 ## Intel processor basics
35 ### Processor layout
37 The following diagram (from the [Intel Power Governor
38 documentation)](https://software.intel.com/en-us/articles/intel-power-governor)
39 shows how machines using recent Intel processors are constructed.
41 ![](img/power-planes.jpg)
43 The important points are as follows.
45 -   The processor has one or more *packages*. These are part of the
46     actual processor that you buy from Intel. Client processors (e.g.
47     Core i3/i5/i7) have one package. Server processors (e.g. Xeon)
48     typically have two or more packages.
49 -   Each package contains multiple *cores*.
50 -   Each core typically has
51     [hyper-threading](https://en.wikipedia.org/wiki/Hyper-threading),
52     which means it contains two logical *CPUs*.
53 -   The part of the package outside the cores is called the [*uncore* or
54     *system agent*](https://en.wikipedia.org/wiki/Uncore)*.* It includes
55     various components including the L3 cache, memory controller, and,
56     for processors that have one, the integrated GPU.
57 -   RAM is separate from the processor.
59 ### C-states
61 Intel processors have aggressive power-saving features. The first is the
62 ability to switch frequently (thousands of times per second) between
63 active and idle states, and there are actually several different kinds
64 of idle states. These different states are called
65 *[C-states](https://en.wikipedia.org/wiki/Advanced_Configuration_and_Power_Interface#Processor_states).*
66 C0 is the active/busy state, where instructions are being executed. The
67 other states have higher numbers and reflect increasing deeper idle
68 states. The deeper an idle state is, the less power it uses, but the
69 longer it takes to wake up from.
71 Note: the [ACPI
72 standard](https://en.wikipedia.org/wiki/Advanced_Configuration_and_Power_Interface)
73 specifies four states, C0, C1, C2 and C3. Intel maps these to
74 processor-specific states such as C0, C1, C2, C6 and C7. and many tools
75 report C-states using the latter names. The exact relationship is
76 confusing, and chapter 13 of the [Intel optimization
77 manual](http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-optimization-manual.html)
78 has more details. The important thing is that C0 is always the active
79 state, and for the idle states a higher number always means less power
80 consumption.
82 The other thing to note about C-states is that they apply both to cores
83 and the entire package --- i.e. if all cores are idle then the entire
84 package can also become idle, which reduces power consumption even
85 further.
87 The fraction of time that a package or core spends in an idle C-state is
88 called the *C-state residency*. This is a misleading term --- the active
89 state, C0, is also a C-state --- but one that is nonetheless common.
91 Intel processors have model-specific registers (MSRs) containing
92 measurements of how much time is spent in different C-states, and tools
93 such as [powermetrics](powermetrics.md)
94 (Mac), powertop and
95 [turbostat](turbostat.md) (Linux) can
96 expose this information.
98 A *wakeup* occurs when a core or package transitions from an idle state
99 to the active state. This happens when the OS schedules a process to run
100 due to some kind of event. Common causes of wakeups include scheduled
101 timers going off and blocked I/O system calls receiving data.
102 Maintaining C-state residency is crucial to keep power consumption low,
103 and so reducing wakeup frequency is one of the best ways to reduce power
104 consumption.
106 One consequence of the existence of C-states is that observations made
107 during power profiling --- even more than with other kinds of profiling
108 --- can disturb what is being observed. For example, the Gecko Profiler
109 takes samples at 1000Hz using a timer. Each of these samples can trigger
110 a wakeup, which consumes power and obscures Firefox's natural wakeup
111 patterns. For this reason, integrating power measurements into the Gecko
112 Profiler is unlikely to be useful, and other power profiling tools
113 typically use much lower sampling rates (e.g. 1Hz.)
115 ### P-states
117 Intel processors also support multiple *P-states*. P0 is the state where
118 the processor is operating at maximum frequency and voltage, and
119 higher-numbered P-states operate at a lower frequency and voltage to
120 reduce power consumption. Processors can have dozens of P-states, but
121 the transitions are controlled by the hardware and OS and so P-states
122 are of less interest to application developers than C-states.
124 ## Power and power-related measurements
126 There are several kinds of power and power-related measurements. Some
127 are global (whole-system) and some are per-process. The following
128 sections list them from best to worst.
130 ### Power measurements
132 The best measurements are measured in joules and/or watts, and are taken
133 by measuring the actual hardware in some fashion. These are global
134 (whole-system) measurements that are affected by running programs but
135 also by other things such as (for laptops) how bright the monitor
136 backlight is.
138 -   Devices such as ammeters give the best results, but these can be
139     expensive and difficult to set up.
140 -   A cruder technique that works with mobile machines and devices is to
141     run a program for a long time and simply time how long it takes for
142     the battery to drain. The long measurement times required are a
143     disadvantage, though.
145 ### Power estimates
147 The next best measurements come from recent (Sandy Bridge and later)
148 Intel processors that implement the *RAPL* (Running Average Power Limit)
149 interface that provides MSRs containing energy consumption estimates for
150 up to four *power planes* or *domains* of a machine, as seen in the
151 diagram above.
153 -   PKG: The entire package.
154     -   PP0: The cores.
155     -   PP1: An uncore device, usually the GPU (not available on all
156         processor models.)
157 -   DRAM: main memory (not available on all processor models.)
159 The following relationship holds: PP0 + PP1 \<= PKG. DRAM is independent
160 of the other three domains.
162 These values are computed using a power model that uses
163 processor-internal counts as inputs, and they have been
164 [verified](http://www.computer.org/csdl/proceedings/ispass/2013/5776/00/06557170.pdf)
165 as being fairly accurate. They are also updated frequently, at
166 approximately 1,000 Hz, though the variability in their update latency
167 means that they are probably only accurate at lower frequencies, e.g. up
168 to 20 Hz or so. See section 14.9 of Volume 3 of the [Intel Software
169 Developer's
170 Manual](http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html)
171 for more details about RAPL.
173 Tools that can take RAPL readings include the following.
175 -   `tools/power/rapl`: all planes; Linux and Mac.
176 -   [Intel Power
177     Gadget](intel_power_gadget.md): PKG and
178     PP0 planes; Windows, Mac and Linux.
179 -   [powermetrics](powermetrics.md): PKG
180     plane; Mac.
181 -   [perf](perf.md): all planes; Linux.
182 -   [turbostat](turbostat.md): PKG, PP0 and
183     PP1 planes; Linux.
185 Of these,
186 [tools/power/rapl](tools_power_rapl.md) is
187 generally the easiest and best to use because it reads all power planes,
188 it's a command line utility, and it doesn't measure anything else.
190 ### Proxy measurements
192 The next best measurements are proxy measurements, i.e. measurements of
193 things that affect power consumption such as CPU activity, GPU activity,
194 wakeup frequency, C-state residency, disk activity, and network
195 activity. Some of these are measured on a global basis, and some can be
196 measured on a per-process basis. Some can also be measured via
197 instrumentation within Firefox itself.
199 The correlation between each proxy measure and power consumption is hard
200 to know and can vary greatly. When used carefully, however, they can
201 still be useful. This is because they can often be measured in a more
202 fine-grained fashion than power measurements and estimates, which is
203 vital for gaining insight into how a program can reduce power
204 consumption.
206 Most profiling tools provide at least some proxy measurements.
208 ### Hybrid proxy measurements
210 These are combinations of proxy measurements. The combinations are
211 semi-arbitrary, they amplify the unreliability of proxy measurements,
212 and unlike non-hybrid proxy measurements, they don't have a clear
213 physical meaning. Avoid them.
215 The most notable example of a hybrid proxy measurement is the ["Energy
216 Impact" used by OS X's Activity
217 [Monitor](activity_monitor_and_top.md#What-does-Energy-Impact-measure).
219 ## Ways to user power-related measurements
221 ### Low-context measurements
223 Most power-related measurements are global or per-process. Such
224 low-context measurements are typically good for understand *if* power
225 consumption is good or bad, but in the latter case they often don't
226 provide much insight into why the problem is occurring, which part of
227 the code is at fault, or how it can be fixed. Nonetheless, they can
228 still help improve understanding of a problem by using *differential
229 profiling*.
231 -   Compare browsers to see if Firefox is doing better or worse than
232     another browser on a particular workload.
233 -   Compare different versions of Firefox to see if Firefox has improved
234     or worsened over time on a particular workload. This can identify
235     specific changes that caused regressions, for example.
236 -   Compare different configurations of Firefox to see if a particular
237     feature is affecting things.
238 -   Compare different workloads. This can be particularly useful if the
239     workloads only vary slightly. For example, it can be useful to
240     gradually remove elements from a web page and see how the
241     power-related measurements change. Even just switching a tab from
242     the foreground to the background can make a difference.
244 ### High-context measurements
246 A few power-related measurements can be obtained in a high-context
247 fashion, e.g. with stack traces that clearly pinpoint specific parts of
248 the code as being responsible.
250 -   Standard performance profiling tools that measure CPU usage or
251     proxies of CPU usage (such as instruction counts) typically provide
252     high-context measurements. This is useful because high CPU usage
253     typically causes high power consumption.
254 -   Some tools can provide high-context wakeup measurements:
255     [dtrace](dtrace.md) (on Mac) and
256     [perf](perf.md) (on Linux.)
257 -   Source-level instrumentation, such as [TimerFirings
258     logging](timerfirings_logging.md), can
259     identify which timers are firing frequently.
261 ## Power profiling how-to
263 This section aims to put together all the above information and provide
264 a set of strategies for finding, diagnosing and fixing cases of high
265 power consumption.
267 -   First of all, all measurements are best done on a quiet machine that
268     is running little other than the program of interest. Global
269     measurements in particular can be completely skewed and unreliable
270     if this is not the case.
271 -   Find or confirm a test case where Firefox's power consumption is
272     high. "High" can most easily be gauged by comparing against other
273     browsers. Use power measurements or estimates (e.g. via
274     [tools/power/rapl](tools_power_rapl.md),
275     or `mach power` on Mac, or [Intel Power
276     Gadget](intel_power_gadget.md) on
277     Windows) for the comparisons. Avoid lower-quality measurements,
278     especially Activity Monitor's "Energy Impact".
279 -   Try using differential profiling to narrow down the cause.
280     -   Try turning hardware acceleration on or off; e10s on or off;
281         Flash on or off.
282     -   Try putting the relevant tab in the foreground vs. in the
283         background.
284     -   If the problem manifests on a particular website, try saving a
285         local copy of the site and then manually removing HTML elements
286         to see if a particular page feature is causing the problem
287 -   Many power problems are caused by either high CPU usage or high
288     wakeup frequency. Use one of the low-context tools to determine if
289     this is the case (e.g. on Mac use
290     [powermetrics](powermetrics.md).) If
291     so, follow that up by using a tool that gives high-context
292     measurements, which hopefully will identify the cause of the
293     problem.
294     -   For high CPU usage, many profilers can be used: Firefox's dev
295         tools profiler, the Gecko Profiler, or generic performance
296         profilers.
297     -   For high wakeup counts, use
298         [dtrace](dtrace.md) or
299         [perf](perf.md) or [TimerFirings logging](timerfirings_logging.md).
300 -   On Mac workloads that use graphics, Activity Monitor's "Energy"
301     tab can tell you if the high-performance GPU is being used, which
302     uses more power than the integrated GPU.
303 -   If neither CPU usage nor wakeup frequency identifies the problem,
304     more ingenuity may be needed. Looking at other measurements (C-state
305     residency, GPU usage, etc.) may be helpful.
306 -   Animations are sometimes the cause of high power consumption. The
307     [animation
308     inspector](/devtools-user/page_inspector/how_to/work_with_animations/index.rst#animation-inspector)
309     in the Firefox Devtools can identify them. Alternatively, [here is
310     an
311     explanation](https://bugzilla.mozilla.org/show_bug.cgi?id=1190721#c10)
312     of how one developer diagnosed two animation-related problems the
313     hard way (which required genuine platform expertise).
314 -   The approximate cause of power problems often isn't that hard to
315     find. Fixing them is often the hard part. Good luck.
316 -   If you do fix a problem by improving a proxy measurement, you should
317     verify that it also improves a power measurement or estimate. That
318     way you know the fix had a genuine effect.
320 ## Further reading
322 Chapter 13 of the [Intel optimization
323 manual](http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-optimization-manual.html)
324 has many details about optimizing for power consumption. Section 13.5
325 ("Tuning Software for Intelligent Power Consumption") in particular is
326 worth reading.