xref: /linux/Documentation/admin-guide/pm/cpuidle.rst (revision 1f2367a39f17bd553a75e179a747f9b257bc9478)
1.. |struct cpuidle_state| replace:: :c:type:`struct cpuidle_state <cpuidle_state>`
2.. |cpufreq| replace:: :doc:`CPU Performance Scaling <cpufreq>`
3
4========================
5CPU Idle Time Management
6========================
7
8::
9
10 Copyright (c) 2018 Intel Corp., Rafael J. Wysocki <rafael.j.wysocki@intel.com>
11
12Concepts
13========
14
15Modern processors are generally able to enter states in which the execution of
16a program is suspended and instructions belonging to it are not fetched from
17memory or executed.  Those states are the *idle* states of the processor.
18
19Since part of the processor hardware is not used in idle states, entering them
20generally allows power drawn by the processor to be reduced and, in consequence,
21it is an opportunity to save energy.
22
23CPU idle time management is an energy-efficiency feature concerned about using
24the idle states of processors for this purpose.
25
26Logical CPUs
27------------
28
29CPU idle time management operates on CPUs as seen by the *CPU scheduler* (that
30is the part of the kernel responsible for the distribution of computational
31work in the system).  In its view, CPUs are *logical* units.  That is, they need
32not be separate physical entities and may just be interfaces appearing to
33software as individual single-core processors.  In other words, a CPU is an
34entity which appears to be fetching instructions that belong to one sequence
35(program) from memory and executing them, but it need not work this way
36physically.  Generally, three different cases can be consider here.
37
38First, if the whole processor can only follow one sequence of instructions (one
39program) at a time, it is a CPU.  In that case, if the hardware is asked to
40enter an idle state, that applies to the processor as a whole.
41
42Second, if the processor is multi-core, each core in it is able to follow at
43least one program at a time.  The cores need not be entirely independent of each
44other (for example, they may share caches), but still most of the time they
45work physically in parallel with each other, so if each of them executes only
46one program, those programs run mostly independently of each other at the same
47time.  The entire cores are CPUs in that case and if the hardware is asked to
48enter an idle state, that applies to the core that asked for it in the first
49place, but it also may apply to a larger unit (say a "package" or a "cluster")
50that the core belongs to (in fact, it may apply to an entire hierarchy of larger
51units containing the core).  Namely, if all of the cores in the larger unit
52except for one have been put into idle states at the "core level" and the
53remaining core asks the processor to enter an idle state, that may trigger it
54to put the whole larger unit into an idle state which also will affect the
55other cores in that unit.
56
57Finally, each core in a multi-core processor may be able to follow more than one
58program in the same time frame (that is, each core may be able to fetch
59instructions from multiple locations in memory and execute them in the same time
60frame, but not necessarily entirely in parallel with each other).  In that case
61the cores present themselves to software as "bundles" each consisting of
62multiple individual single-core "processors", referred to as *hardware threads*
63(or hyper-threads specifically on Intel hardware), that each can follow one
64sequence of instructions.  Then, the hardware threads are CPUs from the CPU idle
65time management perspective and if the processor is asked to enter an idle state
66by one of them, the hardware thread (or CPU) that asked for it is stopped, but
67nothing more happens, unless all of the other hardware threads within the same
68core also have asked the processor to enter an idle state.  In that situation,
69the core may be put into an idle state individually or a larger unit containing
70it may be put into an idle state as a whole (if the other cores within the
71larger unit are in idle states already).
72
73Idle CPUs
74---------
75
76Logical CPUs, simply referred to as "CPUs" in what follows, are regarded as
77*idle* by the Linux kernel when there are no tasks to run on them except for the
78special "idle" task.
79
80Tasks are the CPU scheduler's representation of work.  Each task consists of a
81sequence of instructions to execute, or code, data to be manipulated while
82running that code, and some context information that needs to be loaded into the
83processor every time the task's code is run by a CPU.  The CPU scheduler
84distributes work by assigning tasks to run to the CPUs present in the system.
85
86Tasks can be in various states.  In particular, they are *runnable* if there are
87no specific conditions preventing their code from being run by a CPU as long as
88there is a CPU available for that (for example, they are not waiting for any
89events to occur or similar).  When a task becomes runnable, the CPU scheduler
90assigns it to one of the available CPUs to run and if there are no more runnable
91tasks assigned to it, the CPU will load the given task's context and run its
92code (from the instruction following the last one executed so far, possibly by
93another CPU).  [If there are multiple runnable tasks assigned to one CPU
94simultaneously, they will be subject to prioritization and time sharing in order
95to allow them to make some progress over time.]
96
97The special "idle" task becomes runnable if there are no other runnable tasks
98assigned to the given CPU and the CPU is then regarded as idle.  In other words,
99in Linux idle CPUs run the code of the "idle" task called *the idle loop*.  That
100code may cause the processor to be put into one of its idle states, if they are
101supported, in order to save energy, but if the processor does not support any
102idle states, or there is not enough time to spend in an idle state before the
103next wakeup event, or there are strict latency constraints preventing any of the
104available idle states from being used, the CPU will simply execute more or less
105useless instructions in a loop until it is assigned a new task to run.
106
107
108.. _idle-loop:
109
110The Idle Loop
111=============
112
113The idle loop code takes two major steps in every iteration of it.  First, it
114calls into a code module referred to as the *governor* that belongs to the CPU
115idle time management subsystem called ``CPUIdle`` to select an idle state for
116the CPU to ask the hardware to enter.  Second, it invokes another code module
117from the ``CPUIdle`` subsystem, called the *driver*, to actually ask the
118processor hardware to enter the idle state selected by the governor.
119
120The role of the governor is to find an idle state most suitable for the
121conditions at hand.  For this purpose, idle states that the hardware can be
122asked to enter by logical CPUs are represented in an abstract way independent of
123the platform or the processor architecture and organized in a one-dimensional
124(linear) array.  That array has to be prepared and supplied by the ``CPUIdle``
125driver matching the platform the kernel is running on at the initialization
126time.  This allows ``CPUIdle`` governors to be independent of the underlying
127hardware and to work with any platforms that the Linux kernel can run on.
128
129Each idle state present in that array is characterized by two parameters to be
130taken into account by the governor, the *target residency* and the (worst-case)
131*exit latency*.  The target residency is the minimum time the hardware must
132spend in the given state, including the time needed to enter it (which may be
133substantial), in order to save more energy than it would save by entering one of
134the shallower idle states instead.  [The "depth" of an idle state roughly
135corresponds to the power drawn by the processor in that state.]  The exit
136latency, in turn, is the maximum time it will take a CPU asking the processor
137hardware to enter an idle state to start executing the first instruction after a
138wakeup from that state.  Note that in general the exit latency also must cover
139the time needed to enter the given state in case the wakeup occurs when the
140hardware is entering it and it must be entered completely to be exited in an
141ordered manner.
142
143There are two types of information that can influence the governor's decisions.
144First of all, the governor knows the time until the closest timer event.  That
145time is known exactly, because the kernel programs timers and it knows exactly
146when they will trigger, and it is the maximum time the hardware that the given
147CPU depends on can spend in an idle state, including the time necessary to enter
148and exit it.  However, the CPU may be woken up by a non-timer event at any time
149(in particular, before the closest timer triggers) and it generally is not known
150when that may happen.  The governor can only see how much time the CPU actually
151was idle after it has been woken up (that time will be referred to as the *idle
152duration* from now on) and it can use that information somehow along with the
153time until the closest timer to estimate the idle duration in future.  How the
154governor uses that information depends on what algorithm is implemented by it
155and that is the primary reason for having more than one governor in the
156``CPUIdle`` subsystem.
157
158There are three ``CPUIdle`` governors available, ``menu``, `TEO <teo-gov_>`_
159and ``ladder``.  Which of them is used by default depends on the configuration
160of the kernel and in particular on whether or not the scheduler tick can be
161`stopped by the idle loop <idle-cpus-and-tick_>`_.  It is possible to change the
162governor at run time if the ``cpuidle_sysfs_switch`` command line parameter has
163been passed to the kernel, but that is not safe in general, so it should not be
164done on production systems (that may change in the future, though).  The name of
165the ``CPUIdle`` governor currently used by the kernel can be read from the
166:file:`current_governor_ro` (or :file:`current_governor` if
167``cpuidle_sysfs_switch`` is present in the kernel command line) file under
168:file:`/sys/devices/system/cpu/cpuidle/` in ``sysfs``.
169
170Which ``CPUIdle`` driver is used, on the other hand, usually depends on the
171platform the kernel is running on, but there are platforms with more than one
172matching driver.  For example, there are two drivers that can work with the
173majority of Intel platforms, ``intel_idle`` and ``acpi_idle``, one with
174hardcoded idle states information and the other able to read that information
175from the system's ACPI tables, respectively.  Still, even in those cases, the
176driver chosen at the system initialization time cannot be replaced later, so the
177decision on which one of them to use has to be made early (on Intel platforms
178the ``acpi_idle`` driver will be used if ``intel_idle`` is disabled for some
179reason or if it does not recognize the processor).  The name of the ``CPUIdle``
180driver currently used by the kernel can be read from the :file:`current_driver`
181file under :file:`/sys/devices/system/cpu/cpuidle/` in ``sysfs``.
182
183
184.. _idle-cpus-and-tick:
185
186Idle CPUs and The Scheduler Tick
187================================
188
189The scheduler tick is a timer that triggers periodically in order to implement
190the time sharing strategy of the CPU scheduler.  Of course, if there are
191multiple runnable tasks assigned to one CPU at the same time, the only way to
192allow them to make reasonable progress in a given time frame is to make them
193share the available CPU time.  Namely, in rough approximation, each task is
194given a slice of the CPU time to run its code, subject to the scheduling class,
195prioritization and so on and when that time slice is used up, the CPU should be
196switched over to running (the code of) another task.  The currently running task
197may not want to give the CPU away voluntarily, however, and the scheduler tick
198is there to make the switch happen regardless.  That is not the only role of the
199tick, but it is the primary reason for using it.
200
201The scheduler tick is problematic from the CPU idle time management perspective,
202because it triggers periodically and relatively often (depending on the kernel
203configuration, the length of the tick period is between 1 ms and 10 ms).
204Thus, if the tick is allowed to trigger on idle CPUs, it will not make sense
205for them to ask the hardware to enter idle states with target residencies above
206the tick period length.  Moreover, in that case the idle duration of any CPU
207will never exceed the tick period length and the energy used for entering and
208exiting idle states due to the tick wakeups on idle CPUs will be wasted.
209
210Fortunately, it is not really necessary to allow the tick to trigger on idle
211CPUs, because (by definition) they have no tasks to run except for the special
212"idle" one.  In other words, from the CPU scheduler perspective, the only user
213of the CPU time on them is the idle loop.  Since the time of an idle CPU need
214not be shared between multiple runnable tasks, the primary reason for using the
215tick goes away if the given CPU is idle.  Consequently, it is possible to stop
216the scheduler tick entirely on idle CPUs in principle, even though that may not
217always be worth the effort.
218
219Whether or not it makes sense to stop the scheduler tick in the idle loop
220depends on what is expected by the governor.  First, if there is another
221(non-tick) timer due to trigger within the tick range, stopping the tick clearly
222would be a waste of time, even though the timer hardware may not need to be
223reprogrammed in that case.  Second, if the governor is expecting a non-timer
224wakeup within the tick range, stopping the tick is not necessary and it may even
225be harmful.  Namely, in that case the governor will select an idle state with
226the target residency within the time until the expected wakeup, so that state is
227going to be relatively shallow.  The governor really cannot select a deep idle
228state then, as that would contradict its own expectation of a wakeup in short
229order.  Now, if the wakeup really occurs shortly, stopping the tick would be a
230waste of time and in this case the timer hardware would need to be reprogrammed,
231which is expensive.  On the other hand, if the tick is stopped and the wakeup
232does not occur any time soon, the hardware may spend indefinite amount of time
233in the shallow idle state selected by the governor, which will be a waste of
234energy.  Hence, if the governor is expecting a wakeup of any kind within the
235tick range, it is better to allow the tick trigger.  Otherwise, however, the
236governor will select a relatively deep idle state, so the tick should be stopped
237so that it does not wake up the CPU too early.
238
239In any case, the governor knows what it is expecting and the decision on whether
240or not to stop the scheduler tick belongs to it.  Still, if the tick has been
241stopped already (in one of the previous iterations of the loop), it is better
242to leave it as is and the governor needs to take that into account.
243
244The kernel can be configured to disable stopping the scheduler tick in the idle
245loop altogether.  That can be done through the build-time configuration of it
246(by unsetting the ``CONFIG_NO_HZ_IDLE`` configuration option) or by passing
247``nohz=off`` to it in the command line.  In both cases, as the stopping of the
248scheduler tick is disabled, the governor's decisions regarding it are simply
249ignored by the idle loop code and the tick is never stopped.
250
251The systems that run kernels configured to allow the scheduler tick to be
252stopped on idle CPUs are referred to as *tickless* systems and they are
253generally regarded as more energy-efficient than the systems running kernels in
254which the tick cannot be stopped.  If the given system is tickless, it will use
255the ``menu`` governor by default and if it is not tickless, the default
256``CPUIdle`` governor on it will be ``ladder``.
257
258
259.. _menu-gov:
260
261The ``menu`` Governor
262=====================
263
264The ``menu`` governor is the default ``CPUIdle`` governor for tickless systems.
265It is quite complex, but the basic principle of its design is straightforward.
266Namely, when invoked to select an idle state for a CPU (i.e. an idle state that
267the CPU will ask the processor hardware to enter), it attempts to predict the
268idle duration and uses the predicted value for idle state selection.
269
270It first obtains the time until the closest timer event with the assumption
271that the scheduler tick will be stopped.  That time, referred to as the *sleep
272length* in what follows, is the upper bound on the time before the next CPU
273wakeup.  It is used to determine the sleep length range, which in turn is needed
274to get the sleep length correction factor.
275
276The ``menu`` governor maintains two arrays of sleep length correction factors.
277One of them is used when tasks previously running on the given CPU are waiting
278for some I/O operations to complete and the other one is used when that is not
279the case.  Each array contains several correction factor values that correspond
280to different sleep length ranges organized so that each range represented in the
281array is approximately 10 times wider than the previous one.
282
283The correction factor for the given sleep length range (determined before
284selecting the idle state for the CPU) is updated after the CPU has been woken
285up and the closer the sleep length is to the observed idle duration, the closer
286to 1 the correction factor becomes (it must fall between 0 and 1 inclusive).
287The sleep length is multiplied by the correction factor for the range that it
288falls into to obtain the first approximation of the predicted idle duration.
289
290Next, the governor uses a simple pattern recognition algorithm to refine its
291idle duration prediction.  Namely, it saves the last 8 observed idle duration
292values and, when predicting the idle duration next time, it computes the average
293and variance of them.  If the variance is small (smaller than 400 square
294milliseconds) or it is small relative to the average (the average is greater
295that 6 times the standard deviation), the average is regarded as the "typical
296interval" value.  Otherwise, the longest of the saved observed idle duration
297values is discarded and the computation is repeated for the remaining ones.
298Again, if the variance of them is small (in the above sense), the average is
299taken as the "typical interval" value and so on, until either the "typical
300interval" is determined or too many data points are disregarded, in which case
301the "typical interval" is assumed to equal "infinity" (the maximum unsigned
302integer value).  The "typical interval" computed this way is compared with the
303sleep length multiplied by the correction factor and the minimum of the two is
304taken as the predicted idle duration.
305
306Then, the governor computes an extra latency limit to help "interactive"
307workloads.  It uses the observation that if the exit latency of the selected
308idle state is comparable with the predicted idle duration, the total time spent
309in that state probably will be very short and the amount of energy to save by
310entering it will be relatively small, so likely it is better to avoid the
311overhead related to entering that state and exiting it.  Thus selecting a
312shallower state is likely to be a better option then.   The first approximation
313of the extra latency limit is the predicted idle duration itself which
314additionally is divided by a value depending on the number of tasks that
315previously ran on the given CPU and now they are waiting for I/O operations to
316complete.  The result of that division is compared with the latency limit coming
317from the power management quality of service, or `PM QoS <cpu-pm-qos_>`_,
318framework and the minimum of the two is taken as the limit for the idle states'
319exit latency.
320
321Now, the governor is ready to walk the list of idle states and choose one of
322them.  For this purpose, it compares the target residency of each state with
323the predicted idle duration and the exit latency of it with the computed latency
324limit.  It selects the state with the target residency closest to the predicted
325idle duration, but still below it, and exit latency that does not exceed the
326limit.
327
328In the final step the governor may still need to refine the idle state selection
329if it has not decided to `stop the scheduler tick <idle-cpus-and-tick_>`_.  That
330happens if the idle duration predicted by it is less than the tick period and
331the tick has not been stopped already (in a previous iteration of the idle
332loop).  Then, the sleep length used in the previous computations may not reflect
333the real time until the closest timer event and if it really is greater than
334that time, the governor may need to select a shallower state with a suitable
335target residency.
336
337
338.. _teo-gov:
339
340The Timer Events Oriented (TEO) Governor
341========================================
342
343The timer events oriented (TEO) governor is an alternative ``CPUIdle`` governor
344for tickless systems.  It follows the same basic strategy as the ``menu`` `one
345<menu-gov_>`_: it always tries to find the deepest idle state suitable for the
346given conditions.  However, it applies a different approach to that problem.
347
348First, it does not use sleep length correction factors, but instead it attempts
349to correlate the observed idle duration values with the available idle states
350and use that information to pick up the idle state that is most likely to
351"match" the upcoming CPU idle interval.   Second, it does not take the tasks
352that were running on the given CPU in the past and are waiting on some I/O
353operations to complete now at all (there is no guarantee that they will run on
354the same CPU when they become runnable again) and the pattern detection code in
355it avoids taking timer wakeups into account.  It also only uses idle duration
356values less than the current time till the closest timer (with the scheduler
357tick excluded) for that purpose.
358
359Like in the ``menu`` governor `case <menu-gov_>`_, the first step is to obtain
360the *sleep length*, which is the time until the closest timer event with the
361assumption that the scheduler tick will be stopped (that also is the upper bound
362on the time until the next CPU wakeup).  That value is then used to preselect an
363idle state on the basis of three metrics maintained for each idle state provided
364by the ``CPUIdle`` driver: ``hits``, ``misses`` and ``early_hits``.
365
366The ``hits`` and ``misses`` metrics measure the likelihood that a given idle
367state will "match" the observed (post-wakeup) idle duration if it "matches" the
368sleep length.  They both are subject to decay (after a CPU wakeup) every time
369the target residency of the idle state corresponding to them is less than or
370equal to the sleep length and the target residency of the next idle state is
371greater than the sleep length (that is, when the idle state corresponding to
372them "matches" the sleep length).  The ``hits`` metric is increased if the
373former condition is satisfied and the target residency of the given idle state
374is less than or equal to the observed idle duration and the target residency of
375the next idle state is greater than the observed idle duration at the same time
376(that is, it is increased when the given idle state "matches" both the sleep
377length and the observed idle duration).  In turn, the ``misses`` metric is
378increased when the given idle state "matches" the sleep length only and the
379observed idle duration is too short for its target residency.
380
381The ``early_hits`` metric measures the likelihood that a given idle state will
382"match" the observed (post-wakeup) idle duration if it does not "match" the
383sleep length.  It is subject to decay on every CPU wakeup and it is increased
384when the idle state corresponding to it "matches" the observed (post-wakeup)
385idle duration and the target residency of the next idle state is less than or
386equal to the sleep length (i.e. the idle state "matching" the sleep length is
387deeper than the given one).
388
389The governor walks the list of idle states provided by the ``CPUIdle`` driver
390and finds the last (deepest) one with the target residency less than or equal
391to the sleep length.  Then, the ``hits`` and ``misses`` metrics of that idle
392state are compared with each other and it is preselected if the ``hits`` one is
393greater (which means that that idle state is likely to "match" the observed idle
394duration after CPU wakeup).  If the ``misses`` one is greater, the governor
395preselects the shallower idle state with the maximum ``early_hits`` metric
396(or if there are multiple shallower idle states with equal ``early_hits``
397metric which also is the maximum, the shallowest of them will be preselected).
398[If there is a wakeup latency constraint coming from the `PM QoS framework
399<cpu-pm-qos_>`_ which is hit before reaching the deepest idle state with the
400target residency within the sleep length, the deepest idle state with the exit
401latency within the constraint is preselected without consulting the ``hits``,
402``misses`` and ``early_hits`` metrics.]
403
404Next, the governor takes several idle duration values observed most recently
405into consideration and if at least a half of them are greater than or equal to
406the target residency of the preselected idle state, that idle state becomes the
407final candidate to ask for.  Otherwise, the average of the most recent idle
408duration values below the target residency of the preselected idle state is
409computed and the governor walks the idle states shallower than the preselected
410one and finds the deepest of them with the target residency within that average.
411That idle state is then taken as the final candidate to ask for.
412
413Still, at this point the governor may need to refine the idle state selection if
414it has not decided to `stop the scheduler tick <idle-cpus-and-tick_>`_.  That
415generally happens if the target residency of the idle state selected so far is
416less than the tick period and the tick has not been stopped already (in a
417previous iteration of the idle loop).  Then, like in the ``menu`` governor
418`case <menu-gov_>`_, the sleep length used in the previous computations may not
419reflect the real time until the closest timer event and if it really is greater
420than that time, a shallower state with a suitable target residency may need to
421be selected.
422
423
424.. _idle-states-representation:
425
426Representation of Idle States
427=============================
428
429For the CPU idle time management purposes all of the physical idle states
430supported by the processor have to be represented as a one-dimensional array of
431|struct cpuidle_state| objects each allowing an individual (logical) CPU to ask
432the processor hardware to enter an idle state of certain properties.  If there
433is a hierarchy of units in the processor, one |struct cpuidle_state| object can
434cover a combination of idle states supported by the units at different levels of
435the hierarchy.  In that case, the `target residency and exit latency parameters
436of it <idle-loop_>`_, must reflect the properties of the idle state at the
437deepest level (i.e. the idle state of the unit containing all of the other
438units).
439
440For example, take a processor with two cores in a larger unit referred to as
441a "module" and suppose that asking the hardware to enter a specific idle state
442(say "X") at the "core" level by one core will trigger the module to try to
443enter a specific idle state of its own (say "MX") if the other core is in idle
444state "X" already.  In other words, asking for idle state "X" at the "core"
445level gives the hardware a license to go as deep as to idle state "MX" at the
446"module" level, but there is no guarantee that this is going to happen (the core
447asking for idle state "X" may just end up in that state by itself instead).
448Then, the target residency of the |struct cpuidle_state| object representing
449idle state "X" must reflect the minimum time to spend in idle state "MX" of
450the module (including the time needed to enter it), because that is the minimum
451time the CPU needs to be idle to save any energy in case the hardware enters
452that state.  Analogously, the exit latency parameter of that object must cover
453the exit time of idle state "MX" of the module (and usually its entry time too),
454because that is the maximum delay between a wakeup signal and the time the CPU
455will start to execute the first new instruction (assuming that both cores in the
456module will always be ready to execute instructions as soon as the module
457becomes operational as a whole).
458
459There are processors without direct coordination between different levels of the
460hierarchy of units inside them, however.  In those cases asking for an idle
461state at the "core" level does not automatically affect the "module" level, for
462example, in any way and the ``CPUIdle`` driver is responsible for the entire
463handling of the hierarchy.  Then, the definition of the idle state objects is
464entirely up to the driver, but still the physical properties of the idle state
465that the processor hardware finally goes into must always follow the parameters
466used by the governor for idle state selection (for instance, the actual exit
467latency of that idle state must not exceed the exit latency parameter of the
468idle state object selected by the governor).
469
470In addition to the target residency and exit latency idle state parameters
471discussed above, the objects representing idle states each contain a few other
472parameters describing the idle state and a pointer to the function to run in
473order to ask the hardware to enter that state.  Also, for each
474|struct cpuidle_state| object, there is a corresponding
475:c:type:`struct cpuidle_state_usage <cpuidle_state_usage>` one containing usage
476statistics of the given idle state.  That information is exposed by the kernel
477via ``sysfs``.
478
479For each CPU in the system, there is a :file:`/sys/devices/system/cpu<N>/cpuidle/`
480directory in ``sysfs``, where the number ``<N>`` is assigned to the given
481CPU at the initialization time.  That directory contains a set of subdirectories
482called :file:`state0`, :file:`state1` and so on, up to the number of idle state
483objects defined for the given CPU minus one.  Each of these directories
484corresponds to one idle state object and the larger the number in its name, the
485deeper the (effective) idle state represented by it.  Each of them contains
486a number of files (attributes) representing the properties of the idle state
487object corresponding to it, as follows:
488
489``above``
490	Total number of times this idle state had been asked for, but the
491	observed idle duration was certainly too short to match its target
492	residency.
493
494``below``
495	Total number of times this idle state had been asked for, but cerainly
496	a deeper idle state would have been a better match for the observed idle
497	duration.
498
499``desc``
500	Description of the idle state.
501
502``disable``
503	Whether or not this idle state is disabled.
504
505``latency``
506	Exit latency of the idle state in microseconds.
507
508``name``
509	Name of the idle state.
510
511``power``
512	Power drawn by hardware in this idle state in milliwatts (if specified,
513	0 otherwise).
514
515``residency``
516	Target residency of the idle state in microseconds.
517
518``time``
519	Total time spent in this idle state by the given CPU (as measured by the
520	kernel) in microseconds.
521
522``usage``
523	Total number of times the hardware has been asked by the given CPU to
524	enter this idle state.
525
526The :file:`desc` and :file:`name` files both contain strings.  The difference
527between them is that the name is expected to be more concise, while the
528description may be longer and it may contain white space or special characters.
529The other files listed above contain integer numbers.
530
531The :file:`disable` attribute is the only writeable one.  If it contains 1, the
532given idle state is disabled for this particular CPU, which means that the
533governor will never select it for this particular CPU and the ``CPUIdle``
534driver will never ask the hardware to enter it for that CPU as a result.
535However, disabling an idle state for one CPU does not prevent it from being
536asked for by the other CPUs, so it must be disabled for all of them in order to
537never be asked for by any of them.  [Note that, due to the way the ``ladder``
538governor is implemented, disabling an idle state prevents that governor from
539selecting any idle states deeper than the disabled one too.]
540
541If the :file:`disable` attribute contains 0, the given idle state is enabled for
542this particular CPU, but it still may be disabled for some or all of the other
543CPUs in the system at the same time.  Writing 1 to it causes the idle state to
544be disabled for this particular CPU and writing 0 to it allows the governor to
545take it into consideration for the given CPU and the driver to ask for it,
546unless that state was disabled globally in the driver (in which case it cannot
547be used at all).
548
549The :file:`power` attribute is not defined very well, especially for idle state
550objects representing combinations of idle states at different levels of the
551hierarchy of units in the processor, and it generally is hard to obtain idle
552state power numbers for complex hardware, so :file:`power` often contains 0 (not
553available) and if it contains a nonzero number, that number may not be very
554accurate and it should not be relied on for anything meaningful.
555
556The number in the :file:`time` file generally may be greater than the total time
557really spent by the given CPU in the given idle state, because it is measured by
558the kernel and it may not cover the cases in which the hardware refused to enter
559this idle state and entered a shallower one instead of it (or even it did not
560enter any idle state at all).  The kernel can only measure the time span between
561asking the hardware to enter an idle state and the subsequent wakeup of the CPU
562and it cannot say what really happened in the meantime at the hardware level.
563Moreover, if the idle state object in question represents a combination of idle
564states at different levels of the hierarchy of units in the processor,
565the kernel can never say how deep the hardware went down the hierarchy in any
566particular case.  For these reasons, the only reliable way to find out how
567much time has been spent by the hardware in different idle states supported by
568it is to use idle state residency counters in the hardware, if available.
569
570
571.. _cpu-pm-qos:
572
573Power Management Quality of Service for CPUs
574============================================
575
576The power management quality of service (PM QoS) framework in the Linux kernel
577allows kernel code and user space processes to set constraints on various
578energy-efficiency features of the kernel to prevent performance from dropping
579below a required level.  The PM QoS constraints can be set globally, in
580predefined categories referred to as PM QoS classes, or against individual
581devices.
582
583CPU idle time management can be affected by PM QoS in two ways, through the
584global constraint in the ``PM_QOS_CPU_DMA_LATENCY`` class and through the
585resume latency constraints for individual CPUs.  Kernel code (e.g. device
586drivers) can set both of them with the help of special internal interfaces
587provided by the PM QoS framework.  User space can modify the former by opening
588the :file:`cpu_dma_latency` special device file under :file:`/dev/` and writing
589a binary value (interpreted as a signed 32-bit integer) to it.  In turn, the
590resume latency constraint for a CPU can be modified by user space by writing a
591string (representing a signed 32-bit integer) to the
592:file:`power/pm_qos_resume_latency_us` file under
593:file:`/sys/devices/system/cpu/cpu<N>/` in ``sysfs``, where the CPU number
594``<N>`` is allocated at the system initialization time.  Negative values
595will be rejected in both cases and, also in both cases, the written integer
596number will be interpreted as a requested PM QoS constraint in microseconds.
597
598The requested value is not automatically applied as a new constraint, however,
599as it may be less restrictive (greater in this particular case) than another
600constraint previously requested by someone else.  For this reason, the PM QoS
601framework maintains a list of requests that have been made so far in each
602global class and for each device, aggregates them and applies the effective
603(minimum in this particular case) value as the new constraint.
604
605In fact, opening the :file:`cpu_dma_latency` special device file causes a new
606PM QoS request to be created and added to the priority list of requests in the
607``PM_QOS_CPU_DMA_LATENCY`` class and the file descriptor coming from the
608"open" operation represents that request.  If that file descriptor is then
609used for writing, the number written to it will be associated with the PM QoS
610request represented by it as a new requested constraint value.  Next, the
611priority list mechanism will be used to determine the new effective value of
612the entire list of requests and that effective value will be set as a new
613constraint.  Thus setting a new requested constraint value will only change the
614real constraint if the effective "list" value is affected by it.  In particular,
615for the ``PM_QOS_CPU_DMA_LATENCY`` class it only affects the real constraint if
616it is the minimum of the requested constraints in the list.  The process holding
617a file descriptor obtained by opening the :file:`cpu_dma_latency` special device
618file controls the PM QoS request associated with that file descriptor, but it
619controls this particular PM QoS request only.
620
621Closing the :file:`cpu_dma_latency` special device file or, more precisely, the
622file descriptor obtained while opening it, causes the PM QoS request associated
623with that file descriptor to be removed from the ``PM_QOS_CPU_DMA_LATENCY``
624class priority list and destroyed.  If that happens, the priority list mechanism
625will be used, again, to determine the new effective value for the whole list
626and that value will become the new real constraint.
627
628In turn, for each CPU there is only one resume latency PM QoS request
629associated with the :file:`power/pm_qos_resume_latency_us` file under
630:file:`/sys/devices/system/cpu/cpu<N>/` in ``sysfs`` and writing to it causes
631this single PM QoS request to be updated regardless of which user space
632process does that.  In other words, this PM QoS request is shared by the entire
633user space, so access to the file associated with it needs to be arbitrated
634to avoid confusion.  [Arguably, the only legitimate use of this mechanism in
635practice is to pin a process to the CPU in question and let it use the
636``sysfs`` interface to control the resume latency constraint for it.]  It
637still only is a request, however.  It is a member of a priority list used to
638determine the effective value to be set as the resume latency constraint for the
639CPU in question every time the list of requests is updated this way or another
640(there may be other requests coming from kernel code in that list).
641
642CPU idle time governors are expected to regard the minimum of the global
643effective ``PM_QOS_CPU_DMA_LATENCY`` class constraint and the effective
644resume latency constraint for the given CPU as the upper limit for the exit
645latency of the idle states they can select for that CPU.  They should never
646select any idle states with exit latency beyond that limit.
647
648
649Idle States Control Via Kernel Command Line
650===========================================
651
652In addition to the ``sysfs`` interface allowing individual idle states to be
653`disabled for individual CPUs <idle-states-representation_>`_, there are kernel
654command line parameters affecting CPU idle time management.
655
656The ``cpuidle.off=1`` kernel command line option can be used to disable the
657CPU idle time management entirely.  It does not prevent the idle loop from
658running on idle CPUs, but it prevents the CPU idle time governors and drivers
659from being invoked.  If it is added to the kernel command line, the idle loop
660will ask the hardware to enter idle states on idle CPUs via the CPU architecture
661support code that is expected to provide a default mechanism for this purpose.
662That default mechanism usually is the least common denominator for all of the
663processors implementing the architecture (i.e. CPU instruction set) in question,
664however, so it is rather crude and not very energy-efficient.  For this reason,
665it is not recommended for production use.
666
667The ``cpuidle.governor=`` kernel command line switch allows the ``CPUIdle``
668governor to use to be specified.  It has to be appended with a string matching
669the name of an available governor (e.g. ``cpuidle.governor=menu``) and that
670governor will be used instead of the default one.  It is possible to force
671the ``menu`` governor to be used on the systems that use the ``ladder`` governor
672by default this way, for example.
673
674The other kernel command line parameters controlling CPU idle time management
675described below are only relevant for the *x86* architecture and some of
676them affect Intel processors only.
677
678The *x86* architecture support code recognizes three kernel command line
679options related to CPU idle time management: ``idle=poll``, ``idle=halt``,
680and ``idle=nomwait``.  The first two of them disable the ``acpi_idle`` and
681``intel_idle`` drivers altogether, which effectively causes the entire
682``CPUIdle`` subsystem to be disabled and makes the idle loop invoke the
683architecture support code to deal with idle CPUs.  How it does that depends on
684which of the two parameters is added to the kernel command line.  In the
685``idle=halt`` case, the architecture support code will use the ``HLT``
686instruction of the CPUs (which, as a rule, suspends the execution of the program
687and causes the hardware to attempt to enter the shallowest available idle state)
688for this purpose, and if ``idle=poll`` is used, idle CPUs will execute a
689more or less ``lightweight'' sequence of instructions in a tight loop.  [Note
690that using ``idle=poll`` is somewhat drastic in many cases, as preventing idle
691CPUs from saving almost any energy at all may not be the only effect of it.
692For example, on Intel hardware it effectively prevents CPUs from using
693P-states (see |cpufreq|) that require any number of CPUs in a package to be
694idle, so it very well may hurt single-thread computations performance as well as
695energy-efficiency.  Thus using it for performance reasons may not be a good idea
696at all.]
697
698The ``idle=nomwait`` option disables the ``intel_idle`` driver and causes
699``acpi_idle`` to be used (as long as all of the information needed by it is
700there in the system's ACPI tables), but it is not allowed to use the
701``MWAIT`` instruction of the CPUs to ask the hardware to enter idle states.
702
703In addition to the architecture-level kernel command line options affecting CPU
704idle time management, there are parameters affecting individual ``CPUIdle``
705drivers that can be passed to them via the kernel command line.  Specifically,
706the ``intel_idle.max_cstate=<n>`` and ``processor.max_cstate=<n>`` parameters,
707where ``<n>`` is an idle state index also used in the name of the given
708state's directory in ``sysfs`` (see
709`Representation of Idle States <idle-states-representation_>`_), causes the
710``intel_idle`` and ``acpi_idle`` drivers, respectively, to discard all of the
711idle states deeper than idle state ``<n>``.  In that case, they will never ask
712for any of those idle states or expose them to the governor.  [The behavior of
713the two drivers is different for ``<n>`` equal to ``0``.  Adding
714``intel_idle.max_cstate=0`` to the kernel command line disables the
715``intel_idle`` driver and allows ``acpi_idle`` to be used, whereas
716``processor.max_cstate=0`` is equivalent to ``processor.max_cstate=1``.
717Also, the ``acpi_idle`` driver is part of the ``processor`` kernel module that
718can be loaded separately and ``max_cstate=<n>`` can be passed to it as a module
719parameter when it is loaded.]
720