blob: 9d381e537f6faabdfeab0c88d89eab18e45822d4 [file] [log] [blame]
Robert Ly35f2fda2013-01-29 16:27:05 -08001page.title=Audio Latency
2@jd:body
3
4<!--
Clay Murphy768b82a2013-11-12 11:32:41 -08005 Copyright 2013 The Android Open Source Project
Robert Ly35f2fda2013-01-29 16:27:05 -08006
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>Audio latency is the time delay as an audio signal passes through a system.
28 For a complete description of audio latency for the purposes of Android
Clay Murphyc28f2372013-09-25 16:13:40 -070029 compatibility, see <em>Section 5.5 Audio Latency</em>
Clay Murphy714cd072014-12-01 13:07:52 -080030 in the <a href="{@docRoot}compatibility/index.html">Android CDD</a>.
Clay Murphyc28f2372013-09-25 16:13:40 -070031 See <a href="latency_design.html">Design For Reduced Latency</a> for an
32 understanding of Android's audio latency-reduction efforts.
Robert Ly35f2fda2013-01-29 16:27:05 -080033</p>
34
Robert Ly35f2fda2013-01-29 16:27:05 -080035<p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -070036 This page focuses on the contributors to output latency,
Robert Ly35f2fda2013-01-29 16:27:05 -080037 but a similar discussion applies to input latency.
38</p>
39<p>
Clay Murphyc28f2372013-09-25 16:13:40 -070040 Assuming the analog circuitry does not contribute significantly, then the major
Glenn Kasten978bec82014-12-23 15:15:20 -080041 surface-level contributors to audio latency are the following:
Robert Ly35f2fda2013-01-29 16:27:05 -080042</p>
43
44<ul>
45 <li>Application</li>
46 <li>Total number of buffers in pipeline</li>
47 <li>Size of each buffer, in frames</li>
48 <li>Additional latency after the app processor, such as from a DSP</li>
49</ul>
50
51<p>
52 As accurate as the above list of contributors may be, it is also misleading.
53 The reason is that buffer count and buffer size are more of an
54 <em>effect</em> than a <em>cause</em>. What usually happens is that
55 a given buffer scheme is implemented and tested, but during testing, an audio
Glenn Kasten978bec82014-12-23 15:15:20 -080056 underrun or overrun is heard as a "click" or "pop." To compensate, the
Robert Ly35f2fda2013-01-29 16:27:05 -080057 system designer then increases buffer sizes or buffer counts.
Glenn Kasten978bec82014-12-23 15:15:20 -080058 This has the desired result of eliminating the underruns or overruns, but it also
Robert Ly35f2fda2013-01-29 16:27:05 -080059 has the undesired side effect of increasing latency.
60</p>
61
62<p>
Clay Murphyc28f2372013-09-25 16:13:40 -070063 A better approach is to understand the causes of the
Glenn Kasten978bec82014-12-23 15:15:20 -080064 underruns and overruns, and then correct those. This eliminates the
Glenn Kastenff257d42014-11-10 16:29:03 -080065 audible artifacts and may permit even smaller or fewer buffers
Robert Ly35f2fda2013-01-29 16:27:05 -080066 and thus reduce latency.
67</p>
68
69<p>
Glenn Kasten978bec82014-12-23 15:15:20 -080070 In our experience, the most common causes of underruns and overruns include:
Robert Ly35f2fda2013-01-29 16:27:05 -080071</p>
72<ul>
73 <li>Linux CFS (Completely Fair Scheduler)</li>
74 <li>high-priority threads with SCHED_FIFO scheduling</li>
75 <li>long scheduling latency</li>
76 <li>long-running interrupt handlers</li>
77 <li>long interrupt disable time</li>
Glenn Kasten978bec82014-12-23 15:15:20 -080078 <li>power management</li>
79 <li>security kernels</li>
Robert Ly35f2fda2013-01-29 16:27:05 -080080</ul>
81
Glenn Kasten978bec82014-12-23 15:15:20 -080082<h3 id="linuxCfs">Linux CFS and SCHED_FIFO scheduling</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -080083<p>
84 The Linux CFS is designed to be fair to competing workloads sharing a common CPU
85 resource. This fairness is represented by a per-thread <em>nice</em> parameter.
86 The nice value ranges from -19 (least nice, or most CPU time allocated)
87 to 20 (nicest, or least CPU time allocated). In general, all threads with a given
88 nice value receive approximately equal CPU time and threads with a
89 numerically lower nice value should expect to
90 receive more CPU time. However, CFS is "fair" only over relatively long
91 periods of observation. Over short-term observation windows,
92 CFS may allocate the CPU resource in unexpected ways. For example, it
93 may take the CPU away from a thread with numerically low niceness
94 onto a thread with a numerically high niceness. In the case of audio,
Glenn Kasten978bec82014-12-23 15:15:20 -080095 this can result in an underrun or overrun.
Robert Ly35f2fda2013-01-29 16:27:05 -080096</p>
97
98<p>
99 The obvious solution is to avoid CFS for high-performance audio
Clay Murphyc28f2372013-09-25 16:13:40 -0700100 threads. Beginning with Android 4.1, such threads now use the
Robert Ly35f2fda2013-01-29 16:27:05 -0800101 <code>SCHED_FIFO</code> scheduling policy rather than the <code>SCHED_NORMAL</code> (also called
102 <code>SCHED_OTHER</code>) scheduling policy implemented by CFS.
103</p>
104
Glenn Kasten978bec82014-12-23 15:15:20 -0800105<h3 id="schedFifo">SCHED_FIFO priorities</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800106<p>
107 Though the high-performance audio threads now use <code>SCHED_FIFO</code>, they
108 are still susceptible to other higher priority <code>SCHED_FIFO</code> threads.
109 These are typically kernel worker threads, but there may also be a few
110 non-audio user threads with policy <code>SCHED_FIFO</code>. The available <code>SCHED_FIFO</code>
111 priorities range from 1 to 99. The audio threads run at priority
112 2 or 3. This leaves priority 1 available for lower priority threads,
Clay Murphyc28f2372013-09-25 16:13:40 -0700113 and priorities 4 to 99 for higher priority threads. We recommend
Robert Ly35f2fda2013-01-29 16:27:05 -0800114 you use priority 1 whenever possible, and reserve priorities 4 to 99 for
115 those threads that are guaranteed to complete within a bounded amount
Glenn Kasten978bec82014-12-23 15:15:20 -0800116 of time, execute with a period shorter than the period of audio threads,
117 and are known to not interfere with scheduling of audio threads.
Robert Ly35f2fda2013-01-29 16:27:05 -0800118</p>
119
Glenn Kasten978bec82014-12-23 15:15:20 -0800120<h3 id="rms">Rate-monotonic scheduling</h3>
121<p>
122 For more information on the theory of assignment of fixed priorities,
123 see the Wikipedia article
124 <a href="http://en.wikipedia.org/wiki/Rate-monotonic_scheduling">Rate-monotonic scheduling</a> (RMS).
125 A key point is that fixed priorities should be allocated strictly based on period,
Glenn Kastene064b462015-02-11 08:53:52 -0800126 with higher priorities assigned to threads of shorter periods, not based on perceived "importance."
Glenn Kasten978bec82014-12-23 15:15:20 -0800127 Non-periodic threads may be modeled as periodic threads, using the maximum frequency of execution
128 and maximum computation per execution. If a non-periodic thread cannot be modeled as
129 a periodic thread (for example it could execute with unbounded frequency or unbounded computation
130 per execution), then it should not be assigned a fixed priority as that would be incompatible
131 with the scheduling of true periodic threads.
132</p>
133
134<h3 id="schedLatency">Scheduling latency</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800135<p>
136 Scheduling latency is the time between when a thread becomes
137 ready to run, and when the resulting context switch completes so that the
Clay Murphyc28f2372013-09-25 16:13:40 -0700138 thread actually runs on a CPU. The shorter the latency the better, and
Robert Ly35f2fda2013-01-29 16:27:05 -0800139 anything over two milliseconds causes problems for audio. Long scheduling
140 latency is most likely to occur during mode transitions, such as
141 bringing up or shutting down a CPU, switching between a security kernel
142 and the normal kernel, switching from full power to low-power mode,
143 or adjusting the CPU clock frequency and voltage.
144</p>
145
Glenn Kasten978bec82014-12-23 15:15:20 -0800146<h3 id="interrupts">Interrupts</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800147<p>
148 In many designs, CPU 0 services all external interrupts. So a
149 long-running interrupt handler may delay other interrupts, in particular
Clay Murphyc28f2372013-09-25 16:13:40 -0700150 audio direct memory access (DMA) completion interrupts. Design interrupt handlers
Glenn Kasten978bec82014-12-23 15:15:20 -0800151 to finish quickly and defer lengthy work to a thread (preferably
Robert Ly35f2fda2013-01-29 16:27:05 -0800152 a CFS thread or <code>SCHED_FIFO</code> thread of priority 1).
153</p>
154
155<p>
156 Equivalently, disabling interrupts on CPU 0 for a long period
157 has the same result of delaying the servicing of audio interrupts.
158 Long interrupt disable times typically happen while waiting for a kernel
159 <i>spin lock</i>. Review these spin locks to ensure that
160 they are bounded.
161</p>
162
Glenn Kasten978bec82014-12-23 15:15:20 -0800163<h3 id="power">Power, performance, and thermal management</h3>
164<p>
165 <a href="http://en.wikipedia.org/wiki/Power_management">Power management</a>
166 is a broad term that encompasses efforts to monitor
167 and reduce power consumption while optimizing performance.
168 <a href="http://en.wikipedia.org/wiki/Thermal_management_of_electronic_devices_and_systems">Thermal management</a>
Glenn Kastene158b8e2015-02-06 09:48:11 -0800169 and <a href="http://en.wikipedia.org/wiki/Computer_cooling">computer cooling</a>
Clay Murphyc9ea0002015-02-17 14:57:14 -0800170 are similar but seek to measure and control heat to avoid damage due to excess heat.
Glenn Kasten978bec82014-12-23 15:15:20 -0800171 In the Linux kernel, the CPU
172 <a href="http://en.wikipedia.org/wiki/Governor_%28device%29">governor</a>
173 is responsible for low-level policy, while user mode configures high-level policy.
174 Techniques used include:
175</p>
176
177<ul>
178 <li>dynamic voltage scaling</li>
179 <li>dynamic frequency scaling</li>
180 <li>dynamic core enabling</li>
181 <li>cluster switching</li>
182 <li>power gating</li>
183 <li>hotplug (hotswap)</li>
184 <li>various sleep modes (halt, stop, idle, suspend, etc.)</li>
185 <li>process migration</li>
186 <li><a href="http://en.wikipedia.org/wiki/Processor_affinity">processor affinity</a></li>
187</ul>
188
189<p>
190 Some management operations can result in "work stoppages" or
191 times during which there is no useful work performed by the application processor.
192 These work stoppages can interfere with audio, so such management should be designed
193 for an acceptable worst-case work stoppage while audio is active.
194 Of course, when thermal runaway is imminent, avoiding permanent damage
195 is more important than audio!
196</p>
197
198<h3 id="security">Security kernels</h3>
199<p>
200 A <a href="http://en.wikipedia.org/wiki/Security_kernel">security kernel</a> for
201 <a href="http://en.wikipedia.org/wiki/Digital_rights_management">Digital rights management</a>
202 (DRM) may run on the same application processor core(s) as those used
203 for the main operating system kernel and application code. Any time
204 during which a security kernel operation is active on a core is effectively a
205 stoppage of ordinary work that would normally run on that core.
206 In particular, this may include audio work. By its nature, the internal
207 behavior of a security kernel is inscrutable from higher-level layers, and thus
208 any performance anomalies caused by a security kernel are especially
209 pernicious. For example, security kernel operations do not typically appear in
210 context switch traces. We call this "dark time" &mdash; time that elapses
211 yet cannot be observed. Security kernels should be designed for an
212 acceptable worst-case work stoppage while audio is active.
213</p>