blob: ad9f4f2d49414fd5e0b8e3f1d24dc8c1074439f0 [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
Glenn Kasten7fc97ba2013-07-16 17:18:58 -070048#define FCC_2 2 // fixed channel count assumption
49
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070050namespace android {
51
52// Fast mixer thread
53bool FastMixer::threadLoop()
54{
55 static const FastMixerState initial;
56 const FastMixerState *previous = &initial, *current = &initial;
57 FastMixerState preIdle; // copy of state before we went into idle
58 struct timespec oldTs = {0, 0};
59 bool oldTsValid = false;
60 long slopNs = 0; // accumulated time we've woken up too early (> 0) or too late (< 0)
61 long sleepNs = -1; // -1: busy wait, 0: sched_yield, > 0: nanosleep
62 int fastTrackNames[FastMixerState::kMaxFastTracks]; // handles used by mixer to identify tracks
63 int generations[FastMixerState::kMaxFastTracks]; // last observed mFastTracks[i].mGeneration
64 unsigned i;
65 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
66 fastTrackNames[i] = -1;
67 generations[i] = 0;
68 }
69 NBAIO_Sink *outputSink = NULL;
70 int outputSinkGen = 0;
71 AudioMixer* mixer = NULL;
72 short *mixBuffer = NULL;
73 enum {UNDEFINED, MIXED, ZEROED} mixBufferState = UNDEFINED;
74 NBAIO_Format format = Format_Invalid;
75 unsigned sampleRate = 0;
76 int fastTracksGen = 0;
77 long periodNs = 0; // expected period; the time required to render one mix buffer
Glenn Kasten288ed212012-04-25 17:52:27 -070078 long underrunNs = 0; // underrun likely when write cycle is greater than this value
79 long overrunNs = 0; // overrun likely when write cycle is less than this value
Glenn Kasten972af222012-06-13 17:14:03 -070080 long forceNs = 0; // if overrun detected, force the write cycle to take this much time
Glenn Kasten288ed212012-04-25 17:52:27 -070081 long warmupNs = 0; // warmup complete when write cycle is greater than to this value
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070082 FastMixerDumpState dummyDumpState, *dumpState = &dummyDumpState;
83 bool ignoreNextOverrun = true; // used to ignore initial overrun and first after an underrun
84#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -070085 struct timespec oldLoad = {0, 0}; // previous value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
86 bool oldLoadValid = false; // whether oldLoad is valid
87 uint32_t bounds = 0;
Glenn Kasten4182c4e2013-07-15 14:45:07 -070088 bool full = false; // whether we have collected at least mSamplingN samples
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070089#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -070090 ThreadCpuUsage tcu; // for reading the current CPU clock frequency in kHz
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070091#endif
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070092#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070093 unsigned coldGen = 0; // last observed mColdGen
Glenn Kasten288ed212012-04-25 17:52:27 -070094 bool isWarm = false; // true means ready to mix, false means wait for warmup before mixing
95 struct timespec measuredWarmupTs = {0, 0}; // how long did it take for warmup to complete
96 uint32_t warmupCycles = 0; // counter of number of loop cycles required to warmup
Glenn Kastenfbae5da2012-05-21 09:17:20 -070097 NBAIO_Sink* teeSink = NULL; // if non-NULL, then duplicate write() to this non-blocking sink
Glenn Kasten9e58b552013-01-18 15:09:48 -080098 NBLog::Writer dummyLogWriter, *logWriter = &dummyLogWriter;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070099
100 for (;;) {
101
102 // either nanosleep, sched_yield, or busy wait
103 if (sleepNs >= 0) {
104 if (sleepNs > 0) {
105 ALOG_ASSERT(sleepNs < 1000000000);
106 const struct timespec req = {0, sleepNs};
107 nanosleep(&req, NULL);
108 } else {
109 sched_yield();
110 }
111 }
112 // default to long sleep for next cycle
113 sleepNs = FAST_DEFAULT_NS;
114
115 // poll for state change
116 const FastMixerState *next = mSQ.poll();
117 if (next == NULL) {
118 // continue to use the default initial state until a real state is available
119 ALOG_ASSERT(current == &initial && previous == &initial);
120 next = current;
121 }
122
123 FastMixerState::Command command = next->mCommand;
124 if (next != current) {
125
126 // As soon as possible of learning of a new dump area, start using it
127 dumpState = next->mDumpState != NULL ? next->mDumpState : &dummyDumpState;
Glenn Kastenfbae5da2012-05-21 09:17:20 -0700128 teeSink = next->mTeeSink;
Glenn Kasten9e58b552013-01-18 15:09:48 -0800129 logWriter = next->mNBLogWriter != NULL ? next->mNBLogWriter : &dummyLogWriter;
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800130 if (mixer != NULL) {
131 mixer->setLog(logWriter);
132 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700133
134 // We want to always have a valid reference to the previous (non-idle) state.
135 // However, the state queue only guarantees access to current and previous states.
136 // So when there is a transition from a non-idle state into an idle state, we make a
137 // copy of the last known non-idle state so it is still available on return from idle.
138 // The possible transitions are:
139 // non-idle -> non-idle update previous from current in-place
140 // non-idle -> idle update previous from copy of current
141 // idle -> idle don't update previous
142 // idle -> non-idle don't update previous
143 if (!(current->mCommand & FastMixerState::IDLE)) {
144 if (command & FastMixerState::IDLE) {
145 preIdle = *current;
146 current = &preIdle;
147 oldTsValid = false;
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700148#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700149 oldLoadValid = false;
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700150#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700151 ignoreNextOverrun = true;
152 }
153 previous = current;
154 }
155 current = next;
156 }
157#if !LOG_NDEBUG
158 next = NULL; // not referenced again
159#endif
160
161 dumpState->mCommand = command;
162
163 switch (command) {
164 case FastMixerState::INITIAL:
165 case FastMixerState::HOT_IDLE:
166 sleepNs = FAST_HOT_IDLE_NS;
167 continue;
168 case FastMixerState::COLD_IDLE:
169 // only perform a cold idle command once
Glenn Kasten21e8c502012-04-12 09:39:42 -0700170 // FIXME consider checking previous state and only perform if previous != COLD_IDLE
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700171 if (current->mColdGen != coldGen) {
172 int32_t *coldFutexAddr = current->mColdFutexAddr;
173 ALOG_ASSERT(coldFutexAddr != NULL);
174 int32_t old = android_atomic_dec(coldFutexAddr);
175 if (old <= 0) {
176 __futex_syscall4(coldFutexAddr, FUTEX_WAIT_PRIVATE, old - 1, NULL);
177 }
Glenn Kastena07f17c2013-04-23 12:39:37 -0700178 int policy = sched_getscheduler(0);
179 if (!(policy == SCHED_FIFO || policy == SCHED_RR)) {
180 ALOGE("did not receive expected priority boost");
181 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700182 // This may be overly conservative; there could be times that the normal mixer
183 // requests such a brief cold idle that it doesn't require resetting this flag.
184 isWarm = false;
185 measuredWarmupTs.tv_sec = 0;
186 measuredWarmupTs.tv_nsec = 0;
187 warmupCycles = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700188 sleepNs = -1;
189 coldGen = current->mColdGen;
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700190#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700191 bounds = 0;
192 full = false;
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700193#endif
Glenn Kasten04a4ca42012-06-01 10:49:51 -0700194 oldTsValid = !clock_gettime(CLOCK_MONOTONIC, &oldTs);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700195 } else {
196 sleepNs = FAST_HOT_IDLE_NS;
197 }
198 continue;
199 case FastMixerState::EXIT:
200 delete mixer;
201 delete[] mixBuffer;
202 return false;
203 case FastMixerState::MIX:
204 case FastMixerState::WRITE:
205 case FastMixerState::MIX_WRITE:
206 break;
207 default:
208 LOG_FATAL("bad command %d", command);
209 }
210
211 // there is a non-idle state available to us; did the state change?
212 size_t frameCount = current->mFrameCount;
213 if (current != previous) {
214
215 // handle state change here, but since we want to diff the state,
216 // we're prepared for previous == &initial the first time through
217 unsigned previousTrackMask;
218
219 // check for change in output HAL configuration
220 NBAIO_Format previousFormat = format;
221 if (current->mOutputSinkGen != outputSinkGen) {
222 outputSink = current->mOutputSink;
223 outputSinkGen = current->mOutputSinkGen;
224 if (outputSink == NULL) {
225 format = Format_Invalid;
226 sampleRate = 0;
227 } else {
228 format = outputSink->format();
229 sampleRate = Format_sampleRate(format);
Glenn Kasten7fc97ba2013-07-16 17:18:58 -0700230 ALOG_ASSERT(Format_channelCount(format) == FCC_2);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700231 }
Glenn Kasten21e8c502012-04-12 09:39:42 -0700232 dumpState->mSampleRate = sampleRate;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700233 }
234
235 if ((format != previousFormat) || (frameCount != previous->mFrameCount)) {
236 // FIXME to avoid priority inversion, don't delete here
237 delete mixer;
238 mixer = NULL;
239 delete[] mixBuffer;
240 mixBuffer = NULL;
241 if (frameCount > 0 && sampleRate > 0) {
242 // FIXME new may block for unbounded time at internal mutex of the heap
243 // implementation; it would be better to have normal mixer allocate for us
244 // to avoid blocking here and to prevent possible priority inversion
245 mixer = new AudioMixer(frameCount, sampleRate, FastMixerState::kMaxFastTracks);
Glenn Kasten7fc97ba2013-07-16 17:18:58 -0700246 mixBuffer = new short[frameCount * FCC_2];
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700247 periodNs = (frameCount * 1000000000LL) / sampleRate; // 1.00
248 underrunNs = (frameCount * 1750000000LL) / sampleRate; // 1.75
Glenn Kasten0d27c652012-08-07 10:38:59 -0700249 overrunNs = (frameCount * 500000000LL) / sampleRate; // 0.50
250 forceNs = (frameCount * 950000000LL) / sampleRate; // 0.95
Glenn Kasten288ed212012-04-25 17:52:27 -0700251 warmupNs = (frameCount * 500000000LL) / sampleRate; // 0.50
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700252 } else {
253 periodNs = 0;
254 underrunNs = 0;
255 overrunNs = 0;
Glenn Kasten972af222012-06-13 17:14:03 -0700256 forceNs = 0;
257 warmupNs = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700258 }
259 mixBufferState = UNDEFINED;
260#if !LOG_NDEBUG
261 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
262 fastTrackNames[i] = -1;
263 }
264#endif
265 // we need to reconfigure all active tracks
266 previousTrackMask = 0;
267 fastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten21e8c502012-04-12 09:39:42 -0700268 dumpState->mFrameCount = frameCount;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700269 } else {
270 previousTrackMask = previous->mTrackMask;
271 }
272
273 // check for change in active track set
274 unsigned currentTrackMask = current->mTrackMask;
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700275 dumpState->mTrackMask = currentTrackMask;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700276 if (current->mFastTracksGen != fastTracksGen) {
277 ALOG_ASSERT(mixBuffer != NULL);
278 int name;
279
280 // process removed tracks first to avoid running out of track names
281 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
282 while (removedTracks != 0) {
283 i = __builtin_ctz(removedTracks);
284 removedTracks &= ~(1 << i);
285 const FastTrack* fastTrack = &current->mFastTracks[i];
Glenn Kasten288ed212012-04-25 17:52:27 -0700286 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700287 if (mixer != NULL) {
288 name = fastTrackNames[i];
289 ALOG_ASSERT(name >= 0);
290 mixer->deleteTrackName(name);
291 }
292#if !LOG_NDEBUG
293 fastTrackNames[i] = -1;
294#endif
Glenn Kasten288ed212012-04-25 17:52:27 -0700295 // don't reset track dump state, since other side is ignoring it
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700296 generations[i] = fastTrack->mGeneration;
297 }
298
299 // now process added tracks
300 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
301 while (addedTracks != 0) {
302 i = __builtin_ctz(addedTracks);
303 addedTracks &= ~(1 << i);
304 const FastTrack* fastTrack = &current->mFastTracks[i];
305 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
306 ALOG_ASSERT(bufferProvider != NULL && fastTrackNames[i] == -1);
307 if (mixer != NULL) {
Jean-Michel Trivife3156e2012-09-10 18:58:27 -0700308 // calling getTrackName with default channel mask and a random invalid
309 // sessionId (no effects here)
310 name = mixer->getTrackName(AUDIO_CHANNEL_OUT_STEREO, -555);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700311 ALOG_ASSERT(name >= 0);
312 fastTrackNames[i] = name;
313 mixer->setBufferProvider(name, bufferProvider);
314 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
315 (void *) mixBuffer);
316 // newly allocated track names default to full scale volume
Glenn Kasten21e8c502012-04-12 09:39:42 -0700317 if (fastTrack->mSampleRate != 0 && fastTrack->mSampleRate != sampleRate) {
318 mixer->setParameter(name, AudioMixer::RESAMPLE,
319 AudioMixer::SAMPLE_RATE, (void*) fastTrack->mSampleRate);
320 }
321 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
322 (void *) fastTrack->mChannelMask);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700323 mixer->enable(name);
324 }
325 generations[i] = fastTrack->mGeneration;
326 }
327
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800328 // finally process (potentially) modified tracks; these use the same slot
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700329 // but may have a different buffer provider or volume provider
330 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
331 while (modifiedTracks != 0) {
332 i = __builtin_ctz(modifiedTracks);
333 modifiedTracks &= ~(1 << i);
334 const FastTrack* fastTrack = &current->mFastTracks[i];
335 if (fastTrack->mGeneration != generations[i]) {
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800336 // this track was actually modified
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700337 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
338 ALOG_ASSERT(bufferProvider != NULL);
339 if (mixer != NULL) {
340 name = fastTrackNames[i];
341 ALOG_ASSERT(name >= 0);
342 mixer->setBufferProvider(name, bufferProvider);
343 if (fastTrack->mVolumeProvider == NULL) {
344 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0,
345 (void *)0x1000);
346 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1,
347 (void *)0x1000);
348 }
Glenn Kasten21e8c502012-04-12 09:39:42 -0700349 if (fastTrack->mSampleRate != 0 &&
350 fastTrack->mSampleRate != sampleRate) {
351 mixer->setParameter(name, AudioMixer::RESAMPLE,
352 AudioMixer::SAMPLE_RATE, (void*) fastTrack->mSampleRate);
353 } else {
354 mixer->setParameter(name, AudioMixer::RESAMPLE,
355 AudioMixer::REMOVE, NULL);
356 }
357 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
358 (void *) fastTrack->mChannelMask);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700359 // already enabled
360 }
361 generations[i] = fastTrack->mGeneration;
362 }
363 }
364
365 fastTracksGen = current->mFastTracksGen;
366
367 dumpState->mNumTracks = popcount(currentTrackMask);
368 }
369
370#if 1 // FIXME shouldn't need this
371 // only process state change once
372 previous = current;
373#endif
374 }
375
376 // do work using current state here
Glenn Kasten288ed212012-04-25 17:52:27 -0700377 if ((command & FastMixerState::MIX) && (mixer != NULL) && isWarm) {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700378 ALOG_ASSERT(mixBuffer != NULL);
Glenn Kasten288ed212012-04-25 17:52:27 -0700379 // for each track, update volume and check for underrun
380 unsigned currentTrackMask = current->mTrackMask;
381 while (currentTrackMask != 0) {
382 i = __builtin_ctz(currentTrackMask);
383 currentTrackMask &= ~(1 << i);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700384 const FastTrack* fastTrack = &current->mFastTracks[i];
385 int name = fastTrackNames[i];
386 ALOG_ASSERT(name >= 0);
387 if (fastTrack->mVolumeProvider != NULL) {
388 uint32_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
389 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0,
390 (void *)(vlr & 0xFFFF));
391 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1,
392 (void *)(vlr >> 16));
393 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700394 // FIXME The current implementation of framesReady() for fast tracks
395 // takes a tryLock, which can block
396 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
397 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
398 size_t framesReady = fastTrack->mBufferProvider->framesReady();
Alex Rayb3a83642012-11-30 19:42:28 -0800399 if (ATRACE_ENABLED()) {
400 // I wish we had formatted trace names
401 char traceName[16];
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800402 strcpy(traceName, "fRdy");
403 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
404 traceName[5] = '\0';
Alex Rayb3a83642012-11-30 19:42:28 -0800405 ATRACE_INT(traceName, framesReady);
406 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700407 FastTrackDump *ftDump = &dumpState->mTracks[i];
Glenn Kasten09474df2012-05-10 14:48:07 -0700408 FastTrackUnderruns underruns = ftDump->mUnderruns;
Glenn Kasten288ed212012-04-25 17:52:27 -0700409 if (framesReady < frameCount) {
Glenn Kasten288ed212012-04-25 17:52:27 -0700410 if (framesReady == 0) {
Glenn Kasten09474df2012-05-10 14:48:07 -0700411 underruns.mBitFields.mEmpty++;
412 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
Glenn Kasten288ed212012-04-25 17:52:27 -0700413 mixer->disable(name);
414 } else {
415 // allow mixing partial buffer
Glenn Kasten09474df2012-05-10 14:48:07 -0700416 underruns.mBitFields.mPartial++;
417 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kasten288ed212012-04-25 17:52:27 -0700418 mixer->enable(name);
419 }
Glenn Kasten09474df2012-05-10 14:48:07 -0700420 } else {
421 underruns.mBitFields.mFull++;
422 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kasten288ed212012-04-25 17:52:27 -0700423 mixer->enable(name);
424 }
Glenn Kasten09474df2012-05-10 14:48:07 -0700425 ftDump->mUnderruns = underruns;
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700426 ftDump->mFramesReady = framesReady;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700427 }
John Grossman2c3b2da2012-08-02 17:08:54 -0700428
429 int64_t pts;
430 if (outputSink == NULL || (OK != outputSink->getNextWriteTimestamp(&pts)))
431 pts = AudioBufferProvider::kInvalidPTS;
432
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700433 // process() is CPU-bound
John Grossman2c3b2da2012-08-02 17:08:54 -0700434 mixer->process(pts);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700435 mixBufferState = MIXED;
436 } else if (mixBufferState == MIXED) {
437 mixBufferState = UNDEFINED;
438 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700439 bool attemptedWrite = false;
440 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700441 if ((command & FastMixerState::WRITE) && (outputSink != NULL) && (mixBuffer != NULL)) {
442 if (mixBufferState == UNDEFINED) {
Glenn Kasten7fc97ba2013-07-16 17:18:58 -0700443 memset(mixBuffer, 0, frameCount * FCC_2 * sizeof(short));
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700444 mixBufferState = ZEROED;
445 }
Glenn Kastenfbae5da2012-05-21 09:17:20 -0700446 if (teeSink != NULL) {
447 (void) teeSink->write(mixBuffer, frameCount);
448 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700449 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
450 // but this code should be modified to handle both non-blocking and blocking sinks
451 dumpState->mWriteSequence++;
Simon Wilson2d590962012-11-29 15:18:50 -0800452 ATRACE_BEGIN("write");
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700453 ssize_t framesWritten = outputSink->write(mixBuffer, frameCount);
Simon Wilson2d590962012-11-29 15:18:50 -0800454 ATRACE_END();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700455 dumpState->mWriteSequence++;
456 if (framesWritten >= 0) {
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800457 ALOG_ASSERT((size_t) framesWritten <= frameCount);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700458 dumpState->mFramesWritten += framesWritten;
Glenn Kasten288ed212012-04-25 17:52:27 -0700459 //if ((size_t) framesWritten == frameCount) {
460 // didFullWrite = true;
461 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700462 } else {
463 dumpState->mWriteErrors++;
464 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700465 attemptedWrite = true;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700466 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
467 }
468
469 // To be exactly periodic, compute the next sleep time based on current time.
470 // This code doesn't have long-term stability when the sink is non-blocking.
471 // FIXME To avoid drift, use the local audio clock or watch the sink's fill status.
472 struct timespec newTs;
473 int rc = clock_gettime(CLOCK_MONOTONIC, &newTs);
474 if (rc == 0) {
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800475 //logWriter->logTimestamp(newTs);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700476 if (oldTsValid) {
477 time_t sec = newTs.tv_sec - oldTs.tv_sec;
478 long nsec = newTs.tv_nsec - oldTs.tv_nsec;
Glenn Kasten80b32732012-09-24 11:29:00 -0700479 ALOGE_IF(sec < 0 || (sec == 0 && nsec < 0),
480 "clock_gettime(CLOCK_MONOTONIC) failed: was %ld.%09ld but now %ld.%09ld",
481 oldTs.tv_sec, oldTs.tv_nsec, newTs.tv_sec, newTs.tv_nsec);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700482 if (nsec < 0) {
483 --sec;
484 nsec += 1000000000;
485 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700486 // To avoid an initial underrun on fast tracks after exiting standby,
487 // do not start pulling data from tracks and mixing until warmup is complete.
488 // Warmup is considered complete after the earlier of:
Glenn Kasteneb157162012-06-13 14:59:07 -0700489 // MIN_WARMUP_CYCLES write() attempts and last one blocks for at least warmupNs
Glenn Kasten288ed212012-04-25 17:52:27 -0700490 // MAX_WARMUP_CYCLES write() attempts.
491 // This is overly conservative, but to get better accuracy requires a new HAL API.
492 if (!isWarm && attemptedWrite) {
493 measuredWarmupTs.tv_sec += sec;
494 measuredWarmupTs.tv_nsec += nsec;
495 if (measuredWarmupTs.tv_nsec >= 1000000000) {
496 measuredWarmupTs.tv_sec++;
497 measuredWarmupTs.tv_nsec -= 1000000000;
498 }
499 ++warmupCycles;
Glenn Kasteneb157162012-06-13 14:59:07 -0700500 if ((nsec > warmupNs && warmupCycles >= MIN_WARMUP_CYCLES) ||
Glenn Kasten288ed212012-04-25 17:52:27 -0700501 (warmupCycles >= MAX_WARMUP_CYCLES)) {
502 isWarm = true;
503 dumpState->mMeasuredWarmupTs = measuredWarmupTs;
504 dumpState->mWarmupCycles = warmupCycles;
505 }
506 }
Glenn Kasten972af222012-06-13 17:14:03 -0700507 sleepNs = -1;
Glenn Kasten3d198252013-07-10 17:20:54 -0700508 if (isWarm) {
509 if (sec > 0 || nsec > underrunNs) {
510 ATRACE_NAME("underrun");
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700511 // FIXME only log occasionally
Glenn Kasten3d198252013-07-10 17:20:54 -0700512 ALOGV("underrun: time since last cycle %d.%03ld sec",
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700513 (int) sec, nsec / 1000000L);
Glenn Kasten3d198252013-07-10 17:20:54 -0700514 dumpState->mUnderruns++;
515 ignoreNextOverrun = true;
516 } else if (nsec < overrunNs) {
517 if (ignoreNextOverrun) {
518 ignoreNextOverrun = false;
519 } else {
520 // FIXME only log occasionally
521 ALOGV("overrun: time since last cycle %d.%03ld sec",
522 (int) sec, nsec / 1000000L);
523 dumpState->mOverruns++;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700524 }
Glenn Kasten3d198252013-07-10 17:20:54 -0700525 // This forces a minimum cycle time. It:
526 // - compensates for an audio HAL with jitter due to sample rate conversion
527 // - works with a variable buffer depth audio HAL that never pulls at a
528 // rate < than overrunNs per buffer.
529 // - recovers from overrun immediately after underrun
530 // It doesn't work with a non-blocking audio HAL.
531 sleepNs = forceNs - nsec;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700532 } else {
Glenn Kasten3d198252013-07-10 17:20:54 -0700533 ignoreNextOverrun = false;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700534 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700535 }
Glenn Kasten3d198252013-07-10 17:20:54 -0700536#ifdef FAST_MIXER_STATISTICS
537 if (isWarm) {
538 // advance the FIFO queue bounds
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700539 size_t i = bounds & (dumpState->mSamplingN - 1);
Glenn Kasten3d198252013-07-10 17:20:54 -0700540 bounds = (bounds & 0xFFFF0000) | ((bounds + 1) & 0xFFFF);
541 if (full) {
542 bounds += 0x10000;
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700543 } else if (!(bounds & (dumpState->mSamplingN - 1))) {
Glenn Kasten3d198252013-07-10 17:20:54 -0700544 full = true;
545 }
546 // compute the delta value of clock_gettime(CLOCK_MONOTONIC)
547 uint32_t monotonicNs = nsec;
548 if (sec > 0 && sec < 4) {
549 monotonicNs += sec * 1000000000;
550 }
551 // compute raw CPU load = delta value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
552 uint32_t loadNs = 0;
553 struct timespec newLoad;
554 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &newLoad);
555 if (rc == 0) {
556 if (oldLoadValid) {
557 sec = newLoad.tv_sec - oldLoad.tv_sec;
558 nsec = newLoad.tv_nsec - oldLoad.tv_nsec;
559 if (nsec < 0) {
560 --sec;
561 nsec += 1000000000;
562 }
563 loadNs = nsec;
564 if (sec > 0 && sec < 4) {
565 loadNs += sec * 1000000000;
566 }
567 } else {
568 // first time through the loop
569 oldLoadValid = true;
570 }
571 oldLoad = newLoad;
572 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700573#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten3d198252013-07-10 17:20:54 -0700574 // get the absolute value of CPU clock frequency in kHz
575 int cpuNum = sched_getcpu();
576 uint32_t kHz = tcu.getCpukHz(cpuNum);
577 kHz = (kHz << 4) | (cpuNum & 0xF);
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700578#endif
Glenn Kasten3d198252013-07-10 17:20:54 -0700579 // save values in FIFO queues for dumpsys
580 // these stores #1, #2, #3 are not atomic with respect to each other,
581 // or with respect to store #4 below
582 dumpState->mMonotonicNs[i] = monotonicNs;
583 dumpState->mLoadNs[i] = loadNs;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700584#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten3d198252013-07-10 17:20:54 -0700585 dumpState->mCpukHz[i] = kHz;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700586#endif
Glenn Kasten3d198252013-07-10 17:20:54 -0700587 // this store #4 is not atomic with respect to stores #1, #2, #3 above, but
588 // the newest open & oldest closed halves are atomic with respect to each other
589 dumpState->mBounds = bounds;
590 ATRACE_INT("cycle_ms", monotonicNs / 1000000);
591 ATRACE_INT("load_us", loadNs / 1000);
592 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700593#endif
594 } else {
595 // first time through the loop
596 oldTsValid = true;
597 sleepNs = periodNs;
598 ignoreNextOverrun = true;
599 }
600 oldTs = newTs;
601 } else {
602 // monotonic clock is broken
603 oldTsValid = false;
604 sleepNs = periodNs;
605 }
606
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700607
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700608 } // for (;;)
609
610 // never return 'true'; Thread::_threadLoop() locks mutex which can result in priority inversion
611}
612
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700613FastMixerDumpState::FastMixerDumpState(
614#ifdef FAST_MIXER_STATISTICS
615 uint32_t samplingN
616#endif
617 ) :
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700618 mCommand(FastMixerState::INITIAL), mWriteSequence(0), mFramesWritten(0),
Glenn Kasten21e8c502012-04-12 09:39:42 -0700619 mNumTracks(0), mWriteErrors(0), mUnderruns(0), mOverruns(0),
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700620 mSampleRate(0), mFrameCount(0), /* mMeasuredWarmupTs({0, 0}), */ mWarmupCycles(0),
621 mTrackMask(0)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700622#ifdef FAST_MIXER_STATISTICS
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700623 , mSamplingN(0), mBounds(0)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700624#endif
625{
Glenn Kasten288ed212012-04-25 17:52:27 -0700626 mMeasuredWarmupTs.tv_sec = 0;
627 mMeasuredWarmupTs.tv_nsec = 0;
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700628#ifdef FAST_MIXER_STATISTICS
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700629 increaseSamplingN(samplingN);
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700630#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700631}
632
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700633#ifdef FAST_MIXER_STATISTICS
634void FastMixerDumpState::increaseSamplingN(uint32_t samplingN)
635{
636 if (samplingN <= mSamplingN || samplingN > kSamplingN || roundup(samplingN) != samplingN) {
637 return;
638 }
639 uint32_t additional = samplingN - mSamplingN;
640 // sample arrays aren't accessed atomically with respect to the bounds,
641 // so clearing reduces chance for dumpsys to read random uninitialized samples
642 memset(&mMonotonicNs[mSamplingN], 0, sizeof(mMonotonicNs[0]) * additional);
643 memset(&mLoadNs[mSamplingN], 0, sizeof(mLoadNs[0]) * additional);
644#ifdef CPU_FREQUENCY_STATISTICS
645 memset(&mCpukHz[mSamplingN], 0, sizeof(mCpukHz[0]) * additional);
646#endif
647 mSamplingN = samplingN;
648}
649#endif
650
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700651FastMixerDumpState::~FastMixerDumpState()
652{
653}
654
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700655// helper function called by qsort()
656static int compare_uint32_t(const void *pa, const void *pb)
657{
658 uint32_t a = *(const uint32_t *)pa;
659 uint32_t b = *(const uint32_t *)pb;
660 if (a < b) {
661 return -1;
662 } else if (a > b) {
663 return 1;
664 } else {
665 return 0;
666 }
667}
668
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700669void FastMixerDumpState::dump(int fd) const
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700670{
Glenn Kasten868c0ab2012-06-13 14:59:17 -0700671 if (mCommand == FastMixerState::INITIAL) {
672 fdprintf(fd, "FastMixer not initialized\n");
673 return;
674 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700675#define COMMAND_MAX 32
676 char string[COMMAND_MAX];
677 switch (mCommand) {
678 case FastMixerState::INITIAL:
679 strcpy(string, "INITIAL");
680 break;
681 case FastMixerState::HOT_IDLE:
682 strcpy(string, "HOT_IDLE");
683 break;
684 case FastMixerState::COLD_IDLE:
685 strcpy(string, "COLD_IDLE");
686 break;
687 case FastMixerState::EXIT:
688 strcpy(string, "EXIT");
689 break;
690 case FastMixerState::MIX:
691 strcpy(string, "MIX");
692 break;
693 case FastMixerState::WRITE:
694 strcpy(string, "WRITE");
695 break;
696 case FastMixerState::MIX_WRITE:
697 strcpy(string, "MIX_WRITE");
698 break;
699 default:
700 snprintf(string, COMMAND_MAX, "%d", mCommand);
701 break;
702 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700703 double measuredWarmupMs = (mMeasuredWarmupTs.tv_sec * 1000.0) +
Glenn Kasten288ed212012-04-25 17:52:27 -0700704 (mMeasuredWarmupTs.tv_nsec / 1000000.0);
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700705 double mixPeriodSec = (double) mFrameCount / (double) mSampleRate;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700706 fdprintf(fd, "FastMixer command=%s writeSequence=%u framesWritten=%u\n"
Glenn Kasten21e8c502012-04-12 09:39:42 -0700707 " numTracks=%u writeErrors=%u underruns=%u overruns=%u\n"
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700708 " sampleRate=%u frameCount=%u measuredWarmup=%.3g ms, warmupCycles=%u\n"
709 " mixPeriod=%.2f ms\n",
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700710 string, mWriteSequence, mFramesWritten,
Glenn Kasten21e8c502012-04-12 09:39:42 -0700711 mNumTracks, mWriteErrors, mUnderruns, mOverruns,
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700712 mSampleRate, mFrameCount, measuredWarmupMs, mWarmupCycles,
713 mixPeriodSec * 1e3);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700714#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700715 // find the interval of valid samples
716 uint32_t bounds = mBounds;
717 uint32_t newestOpen = bounds & 0xFFFF;
718 uint32_t oldestClosed = bounds >> 16;
719 uint32_t n = (newestOpen - oldestClosed) & 0xFFFF;
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700720 if (n > mSamplingN) {
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700721 ALOGE("too many samples %u", n);
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700722 n = mSamplingN;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700723 }
724 // statistics for monotonic (wall clock) time, thread raw CPU load in time, CPU clock frequency,
725 // and adjusted CPU load in MHz normalized for CPU clock frequency
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700726 CentralTendencyStatistics wall, loadNs;
727#ifdef CPU_FREQUENCY_STATISTICS
728 CentralTendencyStatistics kHz, loadMHz;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700729 uint32_t previousCpukHz = 0;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700730#endif
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700731 // Assuming a normal distribution for cycle times, three standard deviations on either side of
732 // the mean account for 99.73% of the population. So if we take each tail to be 1/1000 of the
733 // sample set, we get 99.8% combined, or close to three standard deviations.
734 static const uint32_t kTailDenominator = 1000;
735 uint32_t *tail = n >= kTailDenominator ? new uint32_t[n] : NULL;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700736 // loop over all the samples
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700737 for (uint32_t j = 0; j < n; ++j) {
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700738 size_t i = oldestClosed++ & (mSamplingN - 1);
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700739 uint32_t wallNs = mMonotonicNs[i];
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700740 if (tail != NULL) {
741 tail[j] = wallNs;
742 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700743 wall.sample(wallNs);
744 uint32_t sampleLoadNs = mLoadNs[i];
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700745 loadNs.sample(sampleLoadNs);
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700746#ifdef CPU_FREQUENCY_STATISTICS
747 uint32_t sampleCpukHz = mCpukHz[i];
Glenn Kastenc059bd42012-05-14 17:41:09 -0700748 // skip bad kHz samples
749 if ((sampleCpukHz & ~0xF) != 0) {
750 kHz.sample(sampleCpukHz >> 4);
751 if (sampleCpukHz == previousCpukHz) {
752 double megacycles = (double) sampleLoadNs * (double) (sampleCpukHz >> 4) * 1e-12;
753 double adjMHz = megacycles / mixPeriodSec; // _not_ wallNs * 1e9
754 loadMHz.sample(adjMHz);
755 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700756 }
757 previousCpukHz = sampleCpukHz;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700758#endif
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700759 }
760 fdprintf(fd, "Simple moving statistics over last %.1f seconds:\n", wall.n() * mixPeriodSec);
761 fdprintf(fd, " wall clock time in ms per mix cycle:\n"
762 " mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
763 wall.mean()*1e-6, wall.minimum()*1e-6, wall.maximum()*1e-6, wall.stddev()*1e-6);
764 fdprintf(fd, " raw CPU load in us per mix cycle:\n"
765 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
766 loadNs.mean()*1e-3, loadNs.minimum()*1e-3, loadNs.maximum()*1e-3,
767 loadNs.stddev()*1e-3);
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700768#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700769 fdprintf(fd, " CPU clock frequency in MHz:\n"
770 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
771 kHz.mean()*1e-3, kHz.minimum()*1e-3, kHz.maximum()*1e-3, kHz.stddev()*1e-3);
772 fdprintf(fd, " adjusted CPU load in MHz (i.e. normalized for CPU clock frequency):\n"
773 " mean=%.1f min=%.1f max=%.1f stddev=%.1f\n",
774 loadMHz.mean(), loadMHz.minimum(), loadMHz.maximum(), loadMHz.stddev());
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700775#endif
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700776 if (tail != NULL) {
777 qsort(tail, n, sizeof(uint32_t), compare_uint32_t);
778 // assume same number of tail samples on each side, left and right
779 uint32_t count = n / kTailDenominator;
780 CentralTendencyStatistics left, right;
781 for (uint32_t i = 0; i < count; ++i) {
782 left.sample(tail[i]);
783 right.sample(tail[n - (i + 1)]);
784 }
785 fdprintf(fd, "Distribution of mix cycle times in ms for the tails (> ~3 stddev outliers):\n"
786 " left tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n"
787 " right tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
788 left.mean()*1e-6, left.minimum()*1e-6, left.maximum()*1e-6, left.stddev()*1e-6,
789 right.mean()*1e-6, right.minimum()*1e-6, right.maximum()*1e-6,
790 right.stddev()*1e-6);
791 delete[] tail;
792 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700793#endif
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700794 // The active track mask and track states are updated non-atomically.
795 // So if we relied on isActive to decide whether to display,
796 // then we might display an obsolete track or omit an active track.
797 // Instead we always display all tracks, with an indication
798 // of whether we think the track is active.
799 uint32_t trackMask = mTrackMask;
800 fdprintf(fd, "Fast tracks: kMaxFastTracks=%u activeMask=%#x\n",
801 FastMixerState::kMaxFastTracks, trackMask);
802 fdprintf(fd, "Index Active Full Partial Empty Recent Ready\n");
803 for (uint32_t i = 0; i < FastMixerState::kMaxFastTracks; ++i, trackMask >>= 1) {
804 bool isActive = trackMask & 1;
805 const FastTrackDump *ftDump = &mTracks[i];
806 const FastTrackUnderruns& underruns = ftDump->mUnderruns;
807 const char *mostRecent;
808 switch (underruns.mBitFields.mMostRecent) {
809 case UNDERRUN_FULL:
810 mostRecent = "full";
811 break;
812 case UNDERRUN_PARTIAL:
813 mostRecent = "partial";
814 break;
815 case UNDERRUN_EMPTY:
816 mostRecent = "empty";
817 break;
818 default:
819 mostRecent = "?";
820 break;
821 }
822 fdprintf(fd, "%5u %6s %4u %7u %5u %7s %5u\n", i, isActive ? "yes" : "no",
823 (underruns.mBitFields.mFull) & UNDERRUN_MASK,
824 (underruns.mBitFields.mPartial) & UNDERRUN_MASK,
825 (underruns.mBitFields.mEmpty) & UNDERRUN_MASK,
826 mostRecent, ftDump->mFramesReady);
827 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700828}
829
830} // namespace android