blob: fb7cd71d629c35c18d0292f93284360a458d3b34 [file] [log] [blame]
Glenn Kasten32517852015-03-30 11:57:01 -07001page.title=Contributors to Audio Latency
2@jd:body
3
4<!--
5 Copyright 2013 The Android Open Source Project
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18-->
19<div id="qv-wrapper">
20 <div id="qv">
21 <h2>In this document</h2>
22 <ol id="auto-toc">
23 </ol>
24 </div>
25</div>
26
27<p>
28 This page focuses on the contributors to output latency,
29 but a similar discussion applies to input latency.
30</p>
31<p>
32 Assuming the analog circuitry does not contribute significantly, then the major
33 surface-level contributors to audio latency are the following:
34</p>
35
36<ul>
37 <li>Application</li>
38 <li>Total number of buffers in pipeline</li>
39 <li>Size of each buffer, in frames</li>
40 <li>Additional latency after the app processor, such as from a DSP</li>
41</ul>
42
43<p>
44 As accurate as the above list of contributors may be, it is also misleading.
45 The reason is that buffer count and buffer size are more of an
46 <em>effect</em> than a <em>cause</em>. What usually happens is that
47 a given buffer scheme is implemented and tested, but during testing, an audio
48 underrun or overrun is heard as a "click" or "pop." To compensate, the
49 system designer then increases buffer sizes or buffer counts.
50 This has the desired result of eliminating the underruns or overruns, but it also
51 has the undesired side effect of increasing latency.
52</p>
53
54<p>
55 A better approach is to understand the causes of the
56 underruns and overruns, and then correct those. This eliminates the
57 audible artifacts and may permit even smaller or fewer buffers
58 and thus reduce latency.
59</p>
60
61<p>
62 In our experience, the most common causes of underruns and overruns include:
63</p>
64<ul>
65 <li>Linux CFS (Completely Fair Scheduler)</li>
66 <li>high-priority threads with SCHED_FIFO scheduling</li>
67 <li>long scheduling latency</li>
68 <li>long-running interrupt handlers</li>
69 <li>long interrupt disable time</li>
70 <li>power management</li>
71 <li>security kernels</li>
72</ul>
73
74<h3 id="linuxCfs">Linux CFS and SCHED_FIFO scheduling</h3>
75<p>
76 The Linux CFS is designed to be fair to competing workloads sharing a common CPU
77 resource. This fairness is represented by a per-thread <em>nice</em> parameter.
78 The nice value ranges from -19 (least nice, or most CPU time allocated)
79 to 20 (nicest, or least CPU time allocated). In general, all threads with a given
80 nice value receive approximately equal CPU time and threads with a
81 numerically lower nice value should expect to
82 receive more CPU time. However, CFS is "fair" only over relatively long
83 periods of observation. Over short-term observation windows,
84 CFS may allocate the CPU resource in unexpected ways. For example, it
85 may take the CPU away from a thread with numerically low niceness
86 onto a thread with a numerically high niceness. In the case of audio,
87 this can result in an underrun or overrun.
88</p>
89
90<p>
91 The obvious solution is to avoid CFS for high-performance audio
92 threads. Beginning with Android 4.1, such threads now use the
93 <code>SCHED_FIFO</code> scheduling policy rather than the <code>SCHED_NORMAL</code> (also called
94 <code>SCHED_OTHER</code>) scheduling policy implemented by CFS.
95</p>
96
97<h3 id="schedFifo">SCHED_FIFO priorities</h3>
98<p>
99 Though the high-performance audio threads now use <code>SCHED_FIFO</code>, they
100 are still susceptible to other higher priority <code>SCHED_FIFO</code> threads.
101 These are typically kernel worker threads, but there may also be a few
102 non-audio user threads with policy <code>SCHED_FIFO</code>. The available <code>SCHED_FIFO</code>
103 priorities range from 1 to 99. The audio threads run at priority
104 2 or 3. This leaves priority 1 available for lower priority threads,
105 and priorities 4 to 99 for higher priority threads. We recommend
106 you use priority 1 whenever possible, and reserve priorities 4 to 99 for
107 those threads that are guaranteed to complete within a bounded amount
108 of time, execute with a period shorter than the period of audio threads,
109 and are known to not interfere with scheduling of audio threads.
110</p>
111
112<h3 id="rms">Rate-monotonic scheduling</h3>
113<p>
114 For more information on the theory of assignment of fixed priorities,
115 see the Wikipedia article
116 <a href="http://en.wikipedia.org/wiki/Rate-monotonic_scheduling">Rate-monotonic scheduling</a> (RMS).
117 A key point is that fixed priorities should be allocated strictly based on period,
118 with higher priorities assigned to threads of shorter periods, not based on perceived "importance."
119 Non-periodic threads may be modeled as periodic threads, using the maximum frequency of execution
120 and maximum computation per execution. If a non-periodic thread cannot be modeled as
121 a periodic thread (for example it could execute with unbounded frequency or unbounded computation
122 per execution), then it should not be assigned a fixed priority as that would be incompatible
123 with the scheduling of true periodic threads.
124</p>
125
126<h3 id="schedLatency">Scheduling latency</h3>
127<p>
128 Scheduling latency is the time between when a thread becomes
129 ready to run and when the resulting context switch completes so that the
130 thread actually runs on a CPU. The shorter the latency the better, and
131 anything over two milliseconds causes problems for audio. Long scheduling
132 latency is most likely to occur during mode transitions, such as
133 bringing up or shutting down a CPU, switching between a security kernel
134 and the normal kernel, switching from full power to low-power mode,
135 or adjusting the CPU clock frequency and voltage.
136</p>
137
138<h3 id="interrupts">Interrupts</h3>
139<p>
140 In many designs, CPU 0 services all external interrupts. So a
141 long-running interrupt handler may delay other interrupts, in particular
142 audio direct memory access (DMA) completion interrupts. Design interrupt handlers
143 to finish quickly and defer lengthy work to a thread (preferably
144 a CFS thread or <code>SCHED_FIFO</code> thread of priority 1).
145</p>
146
147<p>
148 Equivalently, disabling interrupts on CPU 0 for a long period
149 has the same result of delaying the servicing of audio interrupts.
150 Long interrupt disable times typically happen while waiting for a kernel
151 <i>spin lock</i>. Review these spin locks to ensure they are bounded.
152</p>
153
154<h3 id="power">Power, performance, and thermal management</h3>
155<p>
156 <a href="http://en.wikipedia.org/wiki/Power_management">Power management</a>
157 is a broad term that encompasses efforts to monitor
158 and reduce power consumption while optimizing performance.
159 <a href="http://en.wikipedia.org/wiki/Thermal_management_of_electronic_devices_and_systems">Thermal management</a>
160 and <a href="http://en.wikipedia.org/wiki/Computer_cooling">computer cooling</a>
161 are similar but seek to measure and control heat to avoid damage due to excess heat.
162 In the Linux kernel, the CPU
163 <a href="http://en.wikipedia.org/wiki/Governor_%28device%29">governor</a>
164 is responsible for low-level policy, while user mode configures high-level policy.
165 Techniques used include:
166</p>
167
168<ul>
169 <li>dynamic voltage scaling</li>
170 <li>dynamic frequency scaling</li>
171 <li>dynamic core enabling</li>
172 <li>cluster switching</li>
173 <li>power gating</li>
174 <li>hotplug (hotswap)</li>
175 <li>various sleep modes (halt, stop, idle, suspend, etc.)</li>
176 <li>process migration</li>
177 <li><a href="http://en.wikipedia.org/wiki/Processor_affinity">processor affinity</a></li>
178</ul>
179
180<p>
181 Some management operations can result in "work stoppages" or
182 times during which there is no useful work performed by the application processor.
183 These work stoppages can interfere with audio, so such management should be designed
184 for an acceptable worst-case work stoppage while audio is active.
185 Of course, when thermal runaway is imminent, avoiding permanent damage
186 is more important than audio!
187</p>
188
189<h3 id="security">Security kernels</h3>
190<p>
191 A <a href="http://en.wikipedia.org/wiki/Security_kernel">security kernel</a> for
192 <a href="http://en.wikipedia.org/wiki/Digital_rights_management">Digital rights management</a>
193 (DRM) may run on the same application processor core(s) as those used
194 for the main operating system kernel and application code. Any time
195 during which a security kernel operation is active on a core is effectively a
196 stoppage of ordinary work that would normally run on that core.
197 In particular, this may include audio work. By its nature, the internal
198 behavior of a security kernel is inscrutable from higher-level layers, and thus
199 any performance anomalies caused by a security kernel are especially
200 pernicious. For example, security kernel operations do not typically appear in
201 context switch traces. We call this "dark time" &mdash; time that elapses
202 yet cannot be observed. Security kernels should be designed for an
203 acceptable worst-case work stoppage while audio is active.
204</p>