Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 1 | page.title=Audio Latency |
| 2 | @jd:body |
| 3 | |
| 4 | <!-- |
Clay Murphy | 768b82a | 2013-11-12 11:32:41 -0800 | [diff] [blame] | 5 | Copyright 2013 The Android Open Source Project |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 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>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 Murphy | c28f237 | 2013-09-25 16:13:40 -0700 | [diff] [blame] | 29 | compatibility, see <em>Section 5.5 Audio Latency</em> |
Clay Murphy | 714cd07 | 2014-12-01 13:07:52 -0800 | [diff] [blame] | 30 | in the <a href="{@docRoot}compatibility/index.html">Android CDD</a>. |
Clay Murphy | c28f237 | 2013-09-25 16:13:40 -0700 | [diff] [blame] | 31 | See <a href="latency_design.html">Design For Reduced Latency</a> for an |
| 32 | understanding of Android's audio latency-reduction efforts. |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 33 | </p> |
| 34 | |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 35 | <p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 36 | This page focuses on the contributors to output latency, |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 37 | but a similar discussion applies to input latency. |
| 38 | </p> |
| 39 | <p> |
Clay Murphy | c28f237 | 2013-09-25 16:13:40 -0700 | [diff] [blame] | 40 | Assuming the analog circuitry does not contribute significantly, then the major |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 41 | surface-level contributors to audio latency are the following: |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 42 | </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 Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 56 | underrun or overrun is heard as a "click" or "pop." To compensate, the |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 57 | system designer then increases buffer sizes or buffer counts. |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 58 | This has the desired result of eliminating the underruns or overruns, but it also |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 59 | has the undesired side effect of increasing latency. |
| 60 | </p> |
| 61 | |
| 62 | <p> |
Clay Murphy | c28f237 | 2013-09-25 16:13:40 -0700 | [diff] [blame] | 63 | A better approach is to understand the causes of the |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 64 | underruns and overruns, and then correct those. This eliminates the |
Glenn Kasten | ff257d4 | 2014-11-10 16:29:03 -0800 | [diff] [blame] | 65 | audible artifacts and may permit even smaller or fewer buffers |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 66 | and thus reduce latency. |
| 67 | </p> |
| 68 | |
| 69 | <p> |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 70 | In our experience, the most common causes of underruns and overruns include: |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 71 | </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 Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 78 | <li>power management</li> |
| 79 | <li>security kernels</li> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 80 | </ul> |
| 81 | |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 82 | <h3 id="linuxCfs">Linux CFS and SCHED_FIFO scheduling</h3> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 83 | <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 Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 95 | this can result in an underrun or overrun. |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 96 | </p> |
| 97 | |
| 98 | <p> |
| 99 | The obvious solution is to avoid CFS for high-performance audio |
Clay Murphy | c28f237 | 2013-09-25 16:13:40 -0700 | [diff] [blame] | 100 | threads. Beginning with Android 4.1, such threads now use the |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 101 | <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 Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 105 | <h3 id="schedFifo">SCHED_FIFO priorities</h3> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 106 | <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 Murphy | c28f237 | 2013-09-25 16:13:40 -0700 | [diff] [blame] | 113 | and priorities 4 to 99 for higher priority threads. We recommend |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 114 | 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 Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 116 | 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 Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 118 | </p> |
| 119 | |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 120 | <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, |
| 126 | with higher priorities assigned to threads of shorter periods, not based on perceived "importance". |
| 127 | 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 Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 135 | <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 Murphy | c28f237 | 2013-09-25 16:13:40 -0700 | [diff] [blame] | 138 | thread actually runs on a CPU. The shorter the latency the better, and |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 139 | 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 Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 146 | <h3 id="interrupts">Interrupts</h3> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 147 | <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 Murphy | c28f237 | 2013-09-25 16:13:40 -0700 | [diff] [blame] | 150 | audio direct memory access (DMA) completion interrupts. Design interrupt handlers |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 151 | to finish quickly and defer lengthy work to a thread (preferably |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 152 | 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 Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 163 | <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 Kasten | e158b8e | 2015-02-06 09:48:11 -0800 | [diff] [blame^] | 169 | and <a href="http://en.wikipedia.org/wiki/Computer_cooling">computer cooling</a> |
| 170 | are similar, but seek to measure and control heat to avoid damage due to excess heat. |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 171 | 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" — 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> |