blob: 5350e2c5438e231aea27ef36ab6cd07dd9f3e48f [file] [log] [blame]
Glenn Kasten97b5d0d2012-03-23 18:54:19 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Glenn Kastena3d26282012-11-30 07:57:43 -080017// <IMPORTANT_WARNING>
18// Design rules for threadLoop() are given in the comments at section "Fast mixer thread" of
19// StateQueue.h. In particular, avoid library and system calls except at well-known points.
20// The design rules are only for threadLoop(), and don't apply to FastMixerDumpState methods.
21// </IMPORTANT_WARNING>
22
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070023#define LOG_TAG "FastMixer"
Glenn Kasten7f5d3352013-02-15 23:55:04 +000024//#define LOG_NDEBUG 0
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070025
Alex Rayb3a83642012-11-30 19:42:28 -080026#define ATRACE_TAG ATRACE_TAG_AUDIO
Alex Ray371eb972012-11-30 11:11:54 -080027
Glenn Kasten153b9fe2013-07-15 11:23:36 -070028#include "Configuration.h"
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070029#include <sys/atomics.h>
30#include <time.h>
31#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070033#include <system/audio.h>
34#ifdef FAST_MIXER_STATISTICS
35#include <cpustats/CentralTendencyStatistics.h>
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070036#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -070037#include <cpustats/ThreadCpuUsage.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070038#endif
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070039#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070040#include "AudioMixer.h"
41#include "FastMixer.h"
42
43#define FAST_HOT_IDLE_NS 1000000L // 1 ms: time to sleep while hot idling
44#define FAST_DEFAULT_NS 999999999L // ~1 sec: default time to sleep
Glenn Kasteneb157162012-06-13 14:59:07 -070045#define MIN_WARMUP_CYCLES 2 // minimum number of loop cycles to wait for warmup
Glenn Kasten288ed212012-04-25 17:52:27 -070046#define MAX_WARMUP_CYCLES 10 // maximum number of loop cycles to wait for warmup
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070047
48namespace android {
49
50// Fast mixer thread
51bool FastMixer::threadLoop()
52{
53 static const FastMixerState initial;
54 const FastMixerState *previous = &initial, *current = &initial;
55 FastMixerState preIdle; // copy of state before we went into idle
56 struct timespec oldTs = {0, 0};
57 bool oldTsValid = false;
58 long slopNs = 0; // accumulated time we've woken up too early (> 0) or too late (< 0)
59 long sleepNs = -1; // -1: busy wait, 0: sched_yield, > 0: nanosleep
60 int fastTrackNames[FastMixerState::kMaxFastTracks]; // handles used by mixer to identify tracks
61 int generations[FastMixerState::kMaxFastTracks]; // last observed mFastTracks[i].mGeneration
62 unsigned i;
63 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
64 fastTrackNames[i] = -1;
65 generations[i] = 0;
66 }
67 NBAIO_Sink *outputSink = NULL;
68 int outputSinkGen = 0;
69 AudioMixer* mixer = NULL;
70 short *mixBuffer = NULL;
71 enum {UNDEFINED, MIXED, ZEROED} mixBufferState = UNDEFINED;
72 NBAIO_Format format = Format_Invalid;
73 unsigned sampleRate = 0;
74 int fastTracksGen = 0;
75 long periodNs = 0; // expected period; the time required to render one mix buffer
Glenn Kasten288ed212012-04-25 17:52:27 -070076 long underrunNs = 0; // underrun likely when write cycle is greater than this value
77 long overrunNs = 0; // overrun likely when write cycle is less than this value
Glenn Kasten972af222012-06-13 17:14:03 -070078 long forceNs = 0; // if overrun detected, force the write cycle to take this much time
Glenn Kasten288ed212012-04-25 17:52:27 -070079 long warmupNs = 0; // warmup complete when write cycle is greater than to this value
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070080 FastMixerDumpState dummyDumpState, *dumpState = &dummyDumpState;
81 bool ignoreNextOverrun = true; // used to ignore initial overrun and first after an underrun
82#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -070083 struct timespec oldLoad = {0, 0}; // previous value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
84 bool oldLoadValid = false; // whether oldLoad is valid
85 uint32_t bounds = 0;
Glenn Kasten4182c4e2013-07-15 14:45:07 -070086 bool full = false; // whether we have collected at least mSamplingN samples
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070087#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -070088 ThreadCpuUsage tcu; // for reading the current CPU clock frequency in kHz
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070089#endif
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070090#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070091 unsigned coldGen = 0; // last observed mColdGen
Glenn Kasten288ed212012-04-25 17:52:27 -070092 bool isWarm = false; // true means ready to mix, false means wait for warmup before mixing
93 struct timespec measuredWarmupTs = {0, 0}; // how long did it take for warmup to complete
94 uint32_t warmupCycles = 0; // counter of number of loop cycles required to warmup
Glenn Kastenfbae5da2012-05-21 09:17:20 -070095 NBAIO_Sink* teeSink = NULL; // if non-NULL, then duplicate write() to this non-blocking sink
Glenn Kasten9e58b552013-01-18 15:09:48 -080096 NBLog::Writer dummyLogWriter, *logWriter = &dummyLogWriter;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070097
98 for (;;) {
99
100 // either nanosleep, sched_yield, or busy wait
101 if (sleepNs >= 0) {
102 if (sleepNs > 0) {
103 ALOG_ASSERT(sleepNs < 1000000000);
104 const struct timespec req = {0, sleepNs};
105 nanosleep(&req, NULL);
106 } else {
107 sched_yield();
108 }
109 }
110 // default to long sleep for next cycle
111 sleepNs = FAST_DEFAULT_NS;
112
113 // poll for state change
114 const FastMixerState *next = mSQ.poll();
115 if (next == NULL) {
116 // continue to use the default initial state until a real state is available
117 ALOG_ASSERT(current == &initial && previous == &initial);
118 next = current;
119 }
120
121 FastMixerState::Command command = next->mCommand;
122 if (next != current) {
123
124 // As soon as possible of learning of a new dump area, start using it
125 dumpState = next->mDumpState != NULL ? next->mDumpState : &dummyDumpState;
Glenn Kastenfbae5da2012-05-21 09:17:20 -0700126 teeSink = next->mTeeSink;
Glenn Kasten9e58b552013-01-18 15:09:48 -0800127 logWriter = next->mNBLogWriter != NULL ? next->mNBLogWriter : &dummyLogWriter;
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800128 if (mixer != NULL) {
129 mixer->setLog(logWriter);
130 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700131
132 // We want to always have a valid reference to the previous (non-idle) state.
133 // However, the state queue only guarantees access to current and previous states.
134 // So when there is a transition from a non-idle state into an idle state, we make a
135 // copy of the last known non-idle state so it is still available on return from idle.
136 // The possible transitions are:
137 // non-idle -> non-idle update previous from current in-place
138 // non-idle -> idle update previous from copy of current
139 // idle -> idle don't update previous
140 // idle -> non-idle don't update previous
141 if (!(current->mCommand & FastMixerState::IDLE)) {
142 if (command & FastMixerState::IDLE) {
143 preIdle = *current;
144 current = &preIdle;
145 oldTsValid = false;
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700146#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700147 oldLoadValid = false;
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700148#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700149 ignoreNextOverrun = true;
150 }
151 previous = current;
152 }
153 current = next;
154 }
155#if !LOG_NDEBUG
156 next = NULL; // not referenced again
157#endif
158
159 dumpState->mCommand = command;
160
161 switch (command) {
162 case FastMixerState::INITIAL:
163 case FastMixerState::HOT_IDLE:
164 sleepNs = FAST_HOT_IDLE_NS;
165 continue;
166 case FastMixerState::COLD_IDLE:
167 // only perform a cold idle command once
Glenn Kasten21e8c502012-04-12 09:39:42 -0700168 // FIXME consider checking previous state and only perform if previous != COLD_IDLE
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700169 if (current->mColdGen != coldGen) {
170 int32_t *coldFutexAddr = current->mColdFutexAddr;
171 ALOG_ASSERT(coldFutexAddr != NULL);
172 int32_t old = android_atomic_dec(coldFutexAddr);
173 if (old <= 0) {
174 __futex_syscall4(coldFutexAddr, FUTEX_WAIT_PRIVATE, old - 1, NULL);
175 }
Glenn Kastena07f17c2013-04-23 12:39:37 -0700176 int policy = sched_getscheduler(0);
177 if (!(policy == SCHED_FIFO || policy == SCHED_RR)) {
178 ALOGE("did not receive expected priority boost");
179 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700180 // This may be overly conservative; there could be times that the normal mixer
181 // requests such a brief cold idle that it doesn't require resetting this flag.
182 isWarm = false;
183 measuredWarmupTs.tv_sec = 0;
184 measuredWarmupTs.tv_nsec = 0;
185 warmupCycles = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700186 sleepNs = -1;
187 coldGen = current->mColdGen;
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700188#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700189 bounds = 0;
190 full = false;
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700191#endif
Glenn Kasten04a4ca42012-06-01 10:49:51 -0700192 oldTsValid = !clock_gettime(CLOCK_MONOTONIC, &oldTs);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700193 } else {
194 sleepNs = FAST_HOT_IDLE_NS;
195 }
196 continue;
197 case FastMixerState::EXIT:
198 delete mixer;
199 delete[] mixBuffer;
200 return false;
201 case FastMixerState::MIX:
202 case FastMixerState::WRITE:
203 case FastMixerState::MIX_WRITE:
204 break;
205 default:
206 LOG_FATAL("bad command %d", command);
207 }
208
209 // there is a non-idle state available to us; did the state change?
210 size_t frameCount = current->mFrameCount;
211 if (current != previous) {
212
213 // handle state change here, but since we want to diff the state,
214 // we're prepared for previous == &initial the first time through
215 unsigned previousTrackMask;
216
217 // check for change in output HAL configuration
218 NBAIO_Format previousFormat = format;
219 if (current->mOutputSinkGen != outputSinkGen) {
220 outputSink = current->mOutputSink;
221 outputSinkGen = current->mOutputSinkGen;
222 if (outputSink == NULL) {
223 format = Format_Invalid;
224 sampleRate = 0;
225 } else {
226 format = outputSink->format();
227 sampleRate = Format_sampleRate(format);
228 ALOG_ASSERT(Format_channelCount(format) == 2);
229 }
Glenn Kasten21e8c502012-04-12 09:39:42 -0700230 dumpState->mSampleRate = sampleRate;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700231 }
232
233 if ((format != previousFormat) || (frameCount != previous->mFrameCount)) {
234 // FIXME to avoid priority inversion, don't delete here
235 delete mixer;
236 mixer = NULL;
237 delete[] mixBuffer;
238 mixBuffer = NULL;
239 if (frameCount > 0 && sampleRate > 0) {
240 // FIXME new may block for unbounded time at internal mutex of the heap
241 // implementation; it would be better to have normal mixer allocate for us
242 // to avoid blocking here and to prevent possible priority inversion
243 mixer = new AudioMixer(frameCount, sampleRate, FastMixerState::kMaxFastTracks);
244 mixBuffer = new short[frameCount * 2];
245 periodNs = (frameCount * 1000000000LL) / sampleRate; // 1.00
246 underrunNs = (frameCount * 1750000000LL) / sampleRate; // 1.75
Glenn Kasten0d27c652012-08-07 10:38:59 -0700247 overrunNs = (frameCount * 500000000LL) / sampleRate; // 0.50
248 forceNs = (frameCount * 950000000LL) / sampleRate; // 0.95
Glenn Kasten288ed212012-04-25 17:52:27 -0700249 warmupNs = (frameCount * 500000000LL) / sampleRate; // 0.50
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700250 } else {
251 periodNs = 0;
252 underrunNs = 0;
253 overrunNs = 0;
Glenn Kasten972af222012-06-13 17:14:03 -0700254 forceNs = 0;
255 warmupNs = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700256 }
257 mixBufferState = UNDEFINED;
258#if !LOG_NDEBUG
259 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
260 fastTrackNames[i] = -1;
261 }
262#endif
263 // we need to reconfigure all active tracks
264 previousTrackMask = 0;
265 fastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten21e8c502012-04-12 09:39:42 -0700266 dumpState->mFrameCount = frameCount;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700267 } else {
268 previousTrackMask = previous->mTrackMask;
269 }
270
271 // check for change in active track set
272 unsigned currentTrackMask = current->mTrackMask;
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700273 dumpState->mTrackMask = currentTrackMask;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700274 if (current->mFastTracksGen != fastTracksGen) {
275 ALOG_ASSERT(mixBuffer != NULL);
276 int name;
277
278 // process removed tracks first to avoid running out of track names
279 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
280 while (removedTracks != 0) {
281 i = __builtin_ctz(removedTracks);
282 removedTracks &= ~(1 << i);
283 const FastTrack* fastTrack = &current->mFastTracks[i];
Glenn Kasten288ed212012-04-25 17:52:27 -0700284 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700285 if (mixer != NULL) {
286 name = fastTrackNames[i];
287 ALOG_ASSERT(name >= 0);
288 mixer->deleteTrackName(name);
289 }
290#if !LOG_NDEBUG
291 fastTrackNames[i] = -1;
292#endif
Glenn Kasten288ed212012-04-25 17:52:27 -0700293 // don't reset track dump state, since other side is ignoring it
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700294 generations[i] = fastTrack->mGeneration;
295 }
296
297 // now process added tracks
298 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
299 while (addedTracks != 0) {
300 i = __builtin_ctz(addedTracks);
301 addedTracks &= ~(1 << i);
302 const FastTrack* fastTrack = &current->mFastTracks[i];
303 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
304 ALOG_ASSERT(bufferProvider != NULL && fastTrackNames[i] == -1);
305 if (mixer != NULL) {
Jean-Michel Trivife3156e2012-09-10 18:58:27 -0700306 // calling getTrackName with default channel mask and a random invalid
307 // sessionId (no effects here)
308 name = mixer->getTrackName(AUDIO_CHANNEL_OUT_STEREO, -555);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700309 ALOG_ASSERT(name >= 0);
310 fastTrackNames[i] = name;
311 mixer->setBufferProvider(name, bufferProvider);
312 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
313 (void *) mixBuffer);
314 // newly allocated track names default to full scale volume
Glenn Kasten21e8c502012-04-12 09:39:42 -0700315 if (fastTrack->mSampleRate != 0 && fastTrack->mSampleRate != sampleRate) {
316 mixer->setParameter(name, AudioMixer::RESAMPLE,
317 AudioMixer::SAMPLE_RATE, (void*) fastTrack->mSampleRate);
318 }
319 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
320 (void *) fastTrack->mChannelMask);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700321 mixer->enable(name);
322 }
323 generations[i] = fastTrack->mGeneration;
324 }
325
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800326 // finally process (potentially) modified tracks; these use the same slot
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700327 // but may have a different buffer provider or volume provider
328 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
329 while (modifiedTracks != 0) {
330 i = __builtin_ctz(modifiedTracks);
331 modifiedTracks &= ~(1 << i);
332 const FastTrack* fastTrack = &current->mFastTracks[i];
333 if (fastTrack->mGeneration != generations[i]) {
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800334 // this track was actually modified
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700335 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
336 ALOG_ASSERT(bufferProvider != NULL);
337 if (mixer != NULL) {
338 name = fastTrackNames[i];
339 ALOG_ASSERT(name >= 0);
340 mixer->setBufferProvider(name, bufferProvider);
341 if (fastTrack->mVolumeProvider == NULL) {
342 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0,
343 (void *)0x1000);
344 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1,
345 (void *)0x1000);
346 }
Glenn Kasten21e8c502012-04-12 09:39:42 -0700347 if (fastTrack->mSampleRate != 0 &&
348 fastTrack->mSampleRate != sampleRate) {
349 mixer->setParameter(name, AudioMixer::RESAMPLE,
350 AudioMixer::SAMPLE_RATE, (void*) fastTrack->mSampleRate);
351 } else {
352 mixer->setParameter(name, AudioMixer::RESAMPLE,
353 AudioMixer::REMOVE, NULL);
354 }
355 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
356 (void *) fastTrack->mChannelMask);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700357 // already enabled
358 }
359 generations[i] = fastTrack->mGeneration;
360 }
361 }
362
363 fastTracksGen = current->mFastTracksGen;
364
365 dumpState->mNumTracks = popcount(currentTrackMask);
366 }
367
368#if 1 // FIXME shouldn't need this
369 // only process state change once
370 previous = current;
371#endif
372 }
373
374 // do work using current state here
Glenn Kasten288ed212012-04-25 17:52:27 -0700375 if ((command & FastMixerState::MIX) && (mixer != NULL) && isWarm) {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700376 ALOG_ASSERT(mixBuffer != NULL);
Glenn Kasten288ed212012-04-25 17:52:27 -0700377 // for each track, update volume and check for underrun
378 unsigned currentTrackMask = current->mTrackMask;
379 while (currentTrackMask != 0) {
380 i = __builtin_ctz(currentTrackMask);
381 currentTrackMask &= ~(1 << i);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700382 const FastTrack* fastTrack = &current->mFastTracks[i];
383 int name = fastTrackNames[i];
384 ALOG_ASSERT(name >= 0);
385 if (fastTrack->mVolumeProvider != NULL) {
386 uint32_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
387 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0,
388 (void *)(vlr & 0xFFFF));
389 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1,
390 (void *)(vlr >> 16));
391 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700392 // FIXME The current implementation of framesReady() for fast tracks
393 // takes a tryLock, which can block
394 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
395 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
396 size_t framesReady = fastTrack->mBufferProvider->framesReady();
Alex Rayb3a83642012-11-30 19:42:28 -0800397 if (ATRACE_ENABLED()) {
398 // I wish we had formatted trace names
399 char traceName[16];
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800400 strcpy(traceName, "fRdy");
401 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
402 traceName[5] = '\0';
Alex Rayb3a83642012-11-30 19:42:28 -0800403 ATRACE_INT(traceName, framesReady);
404 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700405 FastTrackDump *ftDump = &dumpState->mTracks[i];
Glenn Kasten09474df2012-05-10 14:48:07 -0700406 FastTrackUnderruns underruns = ftDump->mUnderruns;
Glenn Kasten288ed212012-04-25 17:52:27 -0700407 if (framesReady < frameCount) {
Glenn Kasten288ed212012-04-25 17:52:27 -0700408 if (framesReady == 0) {
Glenn Kasten09474df2012-05-10 14:48:07 -0700409 underruns.mBitFields.mEmpty++;
410 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
Glenn Kasten288ed212012-04-25 17:52:27 -0700411 mixer->disable(name);
412 } else {
413 // allow mixing partial buffer
Glenn Kasten09474df2012-05-10 14:48:07 -0700414 underruns.mBitFields.mPartial++;
415 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kasten288ed212012-04-25 17:52:27 -0700416 mixer->enable(name);
417 }
Glenn Kasten09474df2012-05-10 14:48:07 -0700418 } else {
419 underruns.mBitFields.mFull++;
420 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kasten288ed212012-04-25 17:52:27 -0700421 mixer->enable(name);
422 }
Glenn Kasten09474df2012-05-10 14:48:07 -0700423 ftDump->mUnderruns = underruns;
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700424 ftDump->mFramesReady = framesReady;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700425 }
John Grossman2c3b2da2012-08-02 17:08:54 -0700426
427 int64_t pts;
428 if (outputSink == NULL || (OK != outputSink->getNextWriteTimestamp(&pts)))
429 pts = AudioBufferProvider::kInvalidPTS;
430
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700431 // process() is CPU-bound
John Grossman2c3b2da2012-08-02 17:08:54 -0700432 mixer->process(pts);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700433 mixBufferState = MIXED;
434 } else if (mixBufferState == MIXED) {
435 mixBufferState = UNDEFINED;
436 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700437 bool attemptedWrite = false;
438 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700439 if ((command & FastMixerState::WRITE) && (outputSink != NULL) && (mixBuffer != NULL)) {
440 if (mixBufferState == UNDEFINED) {
441 memset(mixBuffer, 0, frameCount * 2 * sizeof(short));
442 mixBufferState = ZEROED;
443 }
Glenn Kastenfbae5da2012-05-21 09:17:20 -0700444 if (teeSink != NULL) {
445 (void) teeSink->write(mixBuffer, frameCount);
446 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700447 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
448 // but this code should be modified to handle both non-blocking and blocking sinks
449 dumpState->mWriteSequence++;
Simon Wilson2d590962012-11-29 15:18:50 -0800450 ATRACE_BEGIN("write");
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700451 ssize_t framesWritten = outputSink->write(mixBuffer, frameCount);
Simon Wilson2d590962012-11-29 15:18:50 -0800452 ATRACE_END();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700453 dumpState->mWriteSequence++;
454 if (framesWritten >= 0) {
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800455 ALOG_ASSERT((size_t) framesWritten <= frameCount);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700456 dumpState->mFramesWritten += framesWritten;
Glenn Kasten288ed212012-04-25 17:52:27 -0700457 //if ((size_t) framesWritten == frameCount) {
458 // didFullWrite = true;
459 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700460 } else {
461 dumpState->mWriteErrors++;
462 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700463 attemptedWrite = true;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700464 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
465 }
466
467 // To be exactly periodic, compute the next sleep time based on current time.
468 // This code doesn't have long-term stability when the sink is non-blocking.
469 // FIXME To avoid drift, use the local audio clock or watch the sink's fill status.
470 struct timespec newTs;
471 int rc = clock_gettime(CLOCK_MONOTONIC, &newTs);
472 if (rc == 0) {
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800473 //logWriter->logTimestamp(newTs);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700474 if (oldTsValid) {
475 time_t sec = newTs.tv_sec - oldTs.tv_sec;
476 long nsec = newTs.tv_nsec - oldTs.tv_nsec;
Glenn Kasten80b32732012-09-24 11:29:00 -0700477 ALOGE_IF(sec < 0 || (sec == 0 && nsec < 0),
478 "clock_gettime(CLOCK_MONOTONIC) failed: was %ld.%09ld but now %ld.%09ld",
479 oldTs.tv_sec, oldTs.tv_nsec, newTs.tv_sec, newTs.tv_nsec);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700480 if (nsec < 0) {
481 --sec;
482 nsec += 1000000000;
483 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700484 // To avoid an initial underrun on fast tracks after exiting standby,
485 // do not start pulling data from tracks and mixing until warmup is complete.
486 // Warmup is considered complete after the earlier of:
Glenn Kasteneb157162012-06-13 14:59:07 -0700487 // MIN_WARMUP_CYCLES write() attempts and last one blocks for at least warmupNs
Glenn Kasten288ed212012-04-25 17:52:27 -0700488 // MAX_WARMUP_CYCLES write() attempts.
489 // This is overly conservative, but to get better accuracy requires a new HAL API.
490 if (!isWarm && attemptedWrite) {
491 measuredWarmupTs.tv_sec += sec;
492 measuredWarmupTs.tv_nsec += nsec;
493 if (measuredWarmupTs.tv_nsec >= 1000000000) {
494 measuredWarmupTs.tv_sec++;
495 measuredWarmupTs.tv_nsec -= 1000000000;
496 }
497 ++warmupCycles;
Glenn Kasteneb157162012-06-13 14:59:07 -0700498 if ((nsec > warmupNs && warmupCycles >= MIN_WARMUP_CYCLES) ||
Glenn Kasten288ed212012-04-25 17:52:27 -0700499 (warmupCycles >= MAX_WARMUP_CYCLES)) {
500 isWarm = true;
501 dumpState->mMeasuredWarmupTs = measuredWarmupTs;
502 dumpState->mWarmupCycles = warmupCycles;
503 }
504 }
Glenn Kasten972af222012-06-13 17:14:03 -0700505 sleepNs = -1;
Glenn Kasten3d198252013-07-10 17:20:54 -0700506 if (isWarm) {
507 if (sec > 0 || nsec > underrunNs) {
508 ATRACE_NAME("underrun");
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700509 // FIXME only log occasionally
Glenn Kasten3d198252013-07-10 17:20:54 -0700510 ALOGV("underrun: time since last cycle %d.%03ld sec",
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700511 (int) sec, nsec / 1000000L);
Glenn Kasten3d198252013-07-10 17:20:54 -0700512 dumpState->mUnderruns++;
513 ignoreNextOverrun = true;
514 } else if (nsec < overrunNs) {
515 if (ignoreNextOverrun) {
516 ignoreNextOverrun = false;
517 } else {
518 // FIXME only log occasionally
519 ALOGV("overrun: time since last cycle %d.%03ld sec",
520 (int) sec, nsec / 1000000L);
521 dumpState->mOverruns++;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700522 }
Glenn Kasten3d198252013-07-10 17:20:54 -0700523 // This forces a minimum cycle time. It:
524 // - compensates for an audio HAL with jitter due to sample rate conversion
525 // - works with a variable buffer depth audio HAL that never pulls at a
526 // rate < than overrunNs per buffer.
527 // - recovers from overrun immediately after underrun
528 // It doesn't work with a non-blocking audio HAL.
529 sleepNs = forceNs - nsec;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700530 } else {
Glenn Kasten3d198252013-07-10 17:20:54 -0700531 ignoreNextOverrun = false;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700532 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700533 }
Glenn Kasten3d198252013-07-10 17:20:54 -0700534#ifdef FAST_MIXER_STATISTICS
535 if (isWarm) {
536 // advance the FIFO queue bounds
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700537 size_t i = bounds & (dumpState->mSamplingN - 1);
Glenn Kasten3d198252013-07-10 17:20:54 -0700538 bounds = (bounds & 0xFFFF0000) | ((bounds + 1) & 0xFFFF);
539 if (full) {
540 bounds += 0x10000;
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700541 } else if (!(bounds & (dumpState->mSamplingN - 1))) {
Glenn Kasten3d198252013-07-10 17:20:54 -0700542 full = true;
543 }
544 // compute the delta value of clock_gettime(CLOCK_MONOTONIC)
545 uint32_t monotonicNs = nsec;
546 if (sec > 0 && sec < 4) {
547 monotonicNs += sec * 1000000000;
548 }
549 // compute raw CPU load = delta value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
550 uint32_t loadNs = 0;
551 struct timespec newLoad;
552 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &newLoad);
553 if (rc == 0) {
554 if (oldLoadValid) {
555 sec = newLoad.tv_sec - oldLoad.tv_sec;
556 nsec = newLoad.tv_nsec - oldLoad.tv_nsec;
557 if (nsec < 0) {
558 --sec;
559 nsec += 1000000000;
560 }
561 loadNs = nsec;
562 if (sec > 0 && sec < 4) {
563 loadNs += sec * 1000000000;
564 }
565 } else {
566 // first time through the loop
567 oldLoadValid = true;
568 }
569 oldLoad = newLoad;
570 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700571#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten3d198252013-07-10 17:20:54 -0700572 // get the absolute value of CPU clock frequency in kHz
573 int cpuNum = sched_getcpu();
574 uint32_t kHz = tcu.getCpukHz(cpuNum);
575 kHz = (kHz << 4) | (cpuNum & 0xF);
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700576#endif
Glenn Kasten3d198252013-07-10 17:20:54 -0700577 // save values in FIFO queues for dumpsys
578 // these stores #1, #2, #3 are not atomic with respect to each other,
579 // or with respect to store #4 below
580 dumpState->mMonotonicNs[i] = monotonicNs;
581 dumpState->mLoadNs[i] = loadNs;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700582#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten3d198252013-07-10 17:20:54 -0700583 dumpState->mCpukHz[i] = kHz;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700584#endif
Glenn Kasten3d198252013-07-10 17:20:54 -0700585 // this store #4 is not atomic with respect to stores #1, #2, #3 above, but
586 // the newest open & oldest closed halves are atomic with respect to each other
587 dumpState->mBounds = bounds;
588 ATRACE_INT("cycle_ms", monotonicNs / 1000000);
589 ATRACE_INT("load_us", loadNs / 1000);
590 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700591#endif
592 } else {
593 // first time through the loop
594 oldTsValid = true;
595 sleepNs = periodNs;
596 ignoreNextOverrun = true;
597 }
598 oldTs = newTs;
599 } else {
600 // monotonic clock is broken
601 oldTsValid = false;
602 sleepNs = periodNs;
603 }
604
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700605
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700606 } // for (;;)
607
608 // never return 'true'; Thread::_threadLoop() locks mutex which can result in priority inversion
609}
610
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700611FastMixerDumpState::FastMixerDumpState(
612#ifdef FAST_MIXER_STATISTICS
613 uint32_t samplingN
614#endif
615 ) :
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700616 mCommand(FastMixerState::INITIAL), mWriteSequence(0), mFramesWritten(0),
Glenn Kasten21e8c502012-04-12 09:39:42 -0700617 mNumTracks(0), mWriteErrors(0), mUnderruns(0), mOverruns(0),
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700618 mSampleRate(0), mFrameCount(0), /* mMeasuredWarmupTs({0, 0}), */ mWarmupCycles(0),
619 mTrackMask(0)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700620#ifdef FAST_MIXER_STATISTICS
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700621 , mSamplingN(0), mBounds(0)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700622#endif
623{
Glenn Kasten288ed212012-04-25 17:52:27 -0700624 mMeasuredWarmupTs.tv_sec = 0;
625 mMeasuredWarmupTs.tv_nsec = 0;
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700626#ifdef FAST_MIXER_STATISTICS
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700627 increaseSamplingN(samplingN);
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700628#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700629}
630
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700631#ifdef FAST_MIXER_STATISTICS
632void FastMixerDumpState::increaseSamplingN(uint32_t samplingN)
633{
634 if (samplingN <= mSamplingN || samplingN > kSamplingN || roundup(samplingN) != samplingN) {
635 return;
636 }
637 uint32_t additional = samplingN - mSamplingN;
638 // sample arrays aren't accessed atomically with respect to the bounds,
639 // so clearing reduces chance for dumpsys to read random uninitialized samples
640 memset(&mMonotonicNs[mSamplingN], 0, sizeof(mMonotonicNs[0]) * additional);
641 memset(&mLoadNs[mSamplingN], 0, sizeof(mLoadNs[0]) * additional);
642#ifdef CPU_FREQUENCY_STATISTICS
643 memset(&mCpukHz[mSamplingN], 0, sizeof(mCpukHz[0]) * additional);
644#endif
645 mSamplingN = samplingN;
646}
647#endif
648
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700649FastMixerDumpState::~FastMixerDumpState()
650{
651}
652
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700653// helper function called by qsort()
654static int compare_uint32_t(const void *pa, const void *pb)
655{
656 uint32_t a = *(const uint32_t *)pa;
657 uint32_t b = *(const uint32_t *)pb;
658 if (a < b) {
659 return -1;
660 } else if (a > b) {
661 return 1;
662 } else {
663 return 0;
664 }
665}
666
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700667void FastMixerDumpState::dump(int fd) const
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700668{
Glenn Kasten868c0ab2012-06-13 14:59:17 -0700669 if (mCommand == FastMixerState::INITIAL) {
670 fdprintf(fd, "FastMixer not initialized\n");
671 return;
672 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700673#define COMMAND_MAX 32
674 char string[COMMAND_MAX];
675 switch (mCommand) {
676 case FastMixerState::INITIAL:
677 strcpy(string, "INITIAL");
678 break;
679 case FastMixerState::HOT_IDLE:
680 strcpy(string, "HOT_IDLE");
681 break;
682 case FastMixerState::COLD_IDLE:
683 strcpy(string, "COLD_IDLE");
684 break;
685 case FastMixerState::EXIT:
686 strcpy(string, "EXIT");
687 break;
688 case FastMixerState::MIX:
689 strcpy(string, "MIX");
690 break;
691 case FastMixerState::WRITE:
692 strcpy(string, "WRITE");
693 break;
694 case FastMixerState::MIX_WRITE:
695 strcpy(string, "MIX_WRITE");
696 break;
697 default:
698 snprintf(string, COMMAND_MAX, "%d", mCommand);
699 break;
700 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700701 double measuredWarmupMs = (mMeasuredWarmupTs.tv_sec * 1000.0) +
Glenn Kasten288ed212012-04-25 17:52:27 -0700702 (mMeasuredWarmupTs.tv_nsec / 1000000.0);
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700703 double mixPeriodSec = (double) mFrameCount / (double) mSampleRate;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700704 fdprintf(fd, "FastMixer command=%s writeSequence=%u framesWritten=%u\n"
Glenn Kasten21e8c502012-04-12 09:39:42 -0700705 " numTracks=%u writeErrors=%u underruns=%u overruns=%u\n"
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700706 " sampleRate=%u frameCount=%u measuredWarmup=%.3g ms, warmupCycles=%u\n"
707 " mixPeriod=%.2f ms\n",
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700708 string, mWriteSequence, mFramesWritten,
Glenn Kasten21e8c502012-04-12 09:39:42 -0700709 mNumTracks, mWriteErrors, mUnderruns, mOverruns,
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700710 mSampleRate, mFrameCount, measuredWarmupMs, mWarmupCycles,
711 mixPeriodSec * 1e3);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700712#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700713 // find the interval of valid samples
714 uint32_t bounds = mBounds;
715 uint32_t newestOpen = bounds & 0xFFFF;
716 uint32_t oldestClosed = bounds >> 16;
717 uint32_t n = (newestOpen - oldestClosed) & 0xFFFF;
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700718 if (n > mSamplingN) {
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700719 ALOGE("too many samples %u", n);
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700720 n = mSamplingN;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700721 }
722 // statistics for monotonic (wall clock) time, thread raw CPU load in time, CPU clock frequency,
723 // and adjusted CPU load in MHz normalized for CPU clock frequency
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700724 CentralTendencyStatistics wall, loadNs;
725#ifdef CPU_FREQUENCY_STATISTICS
726 CentralTendencyStatistics kHz, loadMHz;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700727 uint32_t previousCpukHz = 0;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700728#endif
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700729 // Assuming a normal distribution for cycle times, three standard deviations on either side of
730 // the mean account for 99.73% of the population. So if we take each tail to be 1/1000 of the
731 // sample set, we get 99.8% combined, or close to three standard deviations.
732 static const uint32_t kTailDenominator = 1000;
733 uint32_t *tail = n >= kTailDenominator ? new uint32_t[n] : NULL;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700734 // loop over all the samples
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700735 for (uint32_t j = 0; j < n; ++j) {
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700736 size_t i = oldestClosed++ & (mSamplingN - 1);
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700737 uint32_t wallNs = mMonotonicNs[i];
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700738 if (tail != NULL) {
739 tail[j] = wallNs;
740 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700741 wall.sample(wallNs);
742 uint32_t sampleLoadNs = mLoadNs[i];
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700743 loadNs.sample(sampleLoadNs);
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700744#ifdef CPU_FREQUENCY_STATISTICS
745 uint32_t sampleCpukHz = mCpukHz[i];
Glenn Kastenc059bd42012-05-14 17:41:09 -0700746 // skip bad kHz samples
747 if ((sampleCpukHz & ~0xF) != 0) {
748 kHz.sample(sampleCpukHz >> 4);
749 if (sampleCpukHz == previousCpukHz) {
750 double megacycles = (double) sampleLoadNs * (double) (sampleCpukHz >> 4) * 1e-12;
751 double adjMHz = megacycles / mixPeriodSec; // _not_ wallNs * 1e9
752 loadMHz.sample(adjMHz);
753 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700754 }
755 previousCpukHz = sampleCpukHz;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700756#endif
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700757 }
758 fdprintf(fd, "Simple moving statistics over last %.1f seconds:\n", wall.n() * mixPeriodSec);
759 fdprintf(fd, " wall clock time in ms per mix cycle:\n"
760 " mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
761 wall.mean()*1e-6, wall.minimum()*1e-6, wall.maximum()*1e-6, wall.stddev()*1e-6);
762 fdprintf(fd, " raw CPU load in us per mix cycle:\n"
763 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
764 loadNs.mean()*1e-3, loadNs.minimum()*1e-3, loadNs.maximum()*1e-3,
765 loadNs.stddev()*1e-3);
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700766#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700767 fdprintf(fd, " CPU clock frequency in MHz:\n"
768 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
769 kHz.mean()*1e-3, kHz.minimum()*1e-3, kHz.maximum()*1e-3, kHz.stddev()*1e-3);
770 fdprintf(fd, " adjusted CPU load in MHz (i.e. normalized for CPU clock frequency):\n"
771 " mean=%.1f min=%.1f max=%.1f stddev=%.1f\n",
772 loadMHz.mean(), loadMHz.minimum(), loadMHz.maximum(), loadMHz.stddev());
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700773#endif
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700774 if (tail != NULL) {
775 qsort(tail, n, sizeof(uint32_t), compare_uint32_t);
776 // assume same number of tail samples on each side, left and right
777 uint32_t count = n / kTailDenominator;
778 CentralTendencyStatistics left, right;
779 for (uint32_t i = 0; i < count; ++i) {
780 left.sample(tail[i]);
781 right.sample(tail[n - (i + 1)]);
782 }
783 fdprintf(fd, "Distribution of mix cycle times in ms for the tails (> ~3 stddev outliers):\n"
784 " left tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n"
785 " right tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
786 left.mean()*1e-6, left.minimum()*1e-6, left.maximum()*1e-6, left.stddev()*1e-6,
787 right.mean()*1e-6, right.minimum()*1e-6, right.maximum()*1e-6,
788 right.stddev()*1e-6);
789 delete[] tail;
790 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700791#endif
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700792 // The active track mask and track states are updated non-atomically.
793 // So if we relied on isActive to decide whether to display,
794 // then we might display an obsolete track or omit an active track.
795 // Instead we always display all tracks, with an indication
796 // of whether we think the track is active.
797 uint32_t trackMask = mTrackMask;
798 fdprintf(fd, "Fast tracks: kMaxFastTracks=%u activeMask=%#x\n",
799 FastMixerState::kMaxFastTracks, trackMask);
800 fdprintf(fd, "Index Active Full Partial Empty Recent Ready\n");
801 for (uint32_t i = 0; i < FastMixerState::kMaxFastTracks; ++i, trackMask >>= 1) {
802 bool isActive = trackMask & 1;
803 const FastTrackDump *ftDump = &mTracks[i];
804 const FastTrackUnderruns& underruns = ftDump->mUnderruns;
805 const char *mostRecent;
806 switch (underruns.mBitFields.mMostRecent) {
807 case UNDERRUN_FULL:
808 mostRecent = "full";
809 break;
810 case UNDERRUN_PARTIAL:
811 mostRecent = "partial";
812 break;
813 case UNDERRUN_EMPTY:
814 mostRecent = "empty";
815 break;
816 default:
817 mostRecent = "?";
818 break;
819 }
820 fdprintf(fd, "%5u %6s %4u %7u %5u %7s %5u\n", i, isActive ? "yes" : "no",
821 (underruns.mBitFields.mFull) & UNDERRUN_MASK,
822 (underruns.mBitFields.mPartial) & UNDERRUN_MASK,
823 (underruns.mBitFields.mEmpty) & UNDERRUN_MASK,
824 mostRecent, ftDump->mFramesReady);
825 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700826}
827
828} // namespace android