blob: 2005899194720711593aafae443ef890fab26412 [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 Kasten05f9e4f2012-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"
24//#define LOG_NDEBUG 0
25
26#include <sys/atomics.h>
27#include <time.h>
28#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070029#include <utils/Trace.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070030#include <system/audio.h>
31#ifdef FAST_MIXER_STATISTICS
32#include <cpustats/CentralTendencyStatistics.h>
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070033#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -070034#include <cpustats/ThreadCpuUsage.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070035#endif
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070036#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070037#include "AudioMixer.h"
38#include "FastMixer.h"
39
40#define FAST_HOT_IDLE_NS 1000000L // 1 ms: time to sleep while hot idling
41#define FAST_DEFAULT_NS 999999999L // ~1 sec: default time to sleep
Glenn Kasteneb157162012-06-13 14:59:07 -070042#define MIN_WARMUP_CYCLES 2 // minimum number of loop cycles to wait for warmup
Glenn Kasten288ed212012-04-25 17:52:27 -070043#define MAX_WARMUP_CYCLES 10 // maximum number of loop cycles to wait for warmup
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070044
45namespace android {
46
47// Fast mixer thread
48bool FastMixer::threadLoop()
49{
50 static const FastMixerState initial;
51 const FastMixerState *previous = &initial, *current = &initial;
52 FastMixerState preIdle; // copy of state before we went into idle
53 struct timespec oldTs = {0, 0};
54 bool oldTsValid = false;
55 long slopNs = 0; // accumulated time we've woken up too early (> 0) or too late (< 0)
56 long sleepNs = -1; // -1: busy wait, 0: sched_yield, > 0: nanosleep
57 int fastTrackNames[FastMixerState::kMaxFastTracks]; // handles used by mixer to identify tracks
58 int generations[FastMixerState::kMaxFastTracks]; // last observed mFastTracks[i].mGeneration
59 unsigned i;
60 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
61 fastTrackNames[i] = -1;
62 generations[i] = 0;
63 }
64 NBAIO_Sink *outputSink = NULL;
65 int outputSinkGen = 0;
66 AudioMixer* mixer = NULL;
67 short *mixBuffer = NULL;
68 enum {UNDEFINED, MIXED, ZEROED} mixBufferState = UNDEFINED;
69 NBAIO_Format format = Format_Invalid;
70 unsigned sampleRate = 0;
71 int fastTracksGen = 0;
72 long periodNs = 0; // expected period; the time required to render one mix buffer
Glenn Kasten288ed212012-04-25 17:52:27 -070073 long underrunNs = 0; // underrun likely when write cycle is greater than this value
74 long overrunNs = 0; // overrun likely when write cycle is less than this value
Glenn Kasten972af222012-06-13 17:14:03 -070075 long forceNs = 0; // if overrun detected, force the write cycle to take this much time
Glenn Kasten288ed212012-04-25 17:52:27 -070076 long warmupNs = 0; // warmup complete when write cycle is greater than to this value
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070077 FastMixerDumpState dummyDumpState, *dumpState = &dummyDumpState;
78 bool ignoreNextOverrun = true; // used to ignore initial overrun and first after an underrun
79#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -070080 struct timespec oldLoad = {0, 0}; // previous value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
81 bool oldLoadValid = false; // whether oldLoad is valid
82 uint32_t bounds = 0;
83 bool full = false; // whether we have collected at least kSamplingN samples
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070084#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -070085 ThreadCpuUsage tcu; // for reading the current CPU clock frequency in kHz
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070086#endif
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070087#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070088 unsigned coldGen = 0; // last observed mColdGen
Glenn Kasten288ed212012-04-25 17:52:27 -070089 bool isWarm = false; // true means ready to mix, false means wait for warmup before mixing
90 struct timespec measuredWarmupTs = {0, 0}; // how long did it take for warmup to complete
91 uint32_t warmupCycles = 0; // counter of number of loop cycles required to warmup
Glenn Kastenfbae5da2012-05-21 09:17:20 -070092 NBAIO_Sink* teeSink = NULL; // if non-NULL, then duplicate write() to this non-blocking sink
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070093
94 for (;;) {
95
96 // either nanosleep, sched_yield, or busy wait
97 if (sleepNs >= 0) {
98 if (sleepNs > 0) {
99 ALOG_ASSERT(sleepNs < 1000000000);
100 const struct timespec req = {0, sleepNs};
101 nanosleep(&req, NULL);
102 } else {
103 sched_yield();
104 }
105 }
106 // default to long sleep for next cycle
107 sleepNs = FAST_DEFAULT_NS;
108
109 // poll for state change
110 const FastMixerState *next = mSQ.poll();
111 if (next == NULL) {
112 // continue to use the default initial state until a real state is available
113 ALOG_ASSERT(current == &initial && previous == &initial);
114 next = current;
115 }
116
117 FastMixerState::Command command = next->mCommand;
118 if (next != current) {
119
120 // As soon as possible of learning of a new dump area, start using it
121 dumpState = next->mDumpState != NULL ? next->mDumpState : &dummyDumpState;
Glenn Kastenfbae5da2012-05-21 09:17:20 -0700122 teeSink = next->mTeeSink;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700123
124 // We want to always have a valid reference to the previous (non-idle) state.
125 // However, the state queue only guarantees access to current and previous states.
126 // So when there is a transition from a non-idle state into an idle state, we make a
127 // copy of the last known non-idle state so it is still available on return from idle.
128 // The possible transitions are:
129 // non-idle -> non-idle update previous from current in-place
130 // non-idle -> idle update previous from copy of current
131 // idle -> idle don't update previous
132 // idle -> non-idle don't update previous
133 if (!(current->mCommand & FastMixerState::IDLE)) {
134 if (command & FastMixerState::IDLE) {
135 preIdle = *current;
136 current = &preIdle;
137 oldTsValid = false;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700138 oldLoadValid = false;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700139 ignoreNextOverrun = true;
140 }
141 previous = current;
142 }
143 current = next;
144 }
145#if !LOG_NDEBUG
146 next = NULL; // not referenced again
147#endif
148
149 dumpState->mCommand = command;
150
151 switch (command) {
152 case FastMixerState::INITIAL:
153 case FastMixerState::HOT_IDLE:
154 sleepNs = FAST_HOT_IDLE_NS;
155 continue;
156 case FastMixerState::COLD_IDLE:
157 // only perform a cold idle command once
Glenn Kasten21e8c502012-04-12 09:39:42 -0700158 // FIXME consider checking previous state and only perform if previous != COLD_IDLE
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700159 if (current->mColdGen != coldGen) {
160 int32_t *coldFutexAddr = current->mColdFutexAddr;
161 ALOG_ASSERT(coldFutexAddr != NULL);
162 int32_t old = android_atomic_dec(coldFutexAddr);
163 if (old <= 0) {
164 __futex_syscall4(coldFutexAddr, FUTEX_WAIT_PRIVATE, old - 1, NULL);
165 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700166 // This may be overly conservative; there could be times that the normal mixer
167 // requests such a brief cold idle that it doesn't require resetting this flag.
168 isWarm = false;
169 measuredWarmupTs.tv_sec = 0;
170 measuredWarmupTs.tv_nsec = 0;
171 warmupCycles = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700172 sleepNs = -1;
173 coldGen = current->mColdGen;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700174 bounds = 0;
175 full = false;
Glenn Kasten04a4ca42012-06-01 10:49:51 -0700176 oldTsValid = !clock_gettime(CLOCK_MONOTONIC, &oldTs);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700177 } else {
178 sleepNs = FAST_HOT_IDLE_NS;
179 }
180 continue;
181 case FastMixerState::EXIT:
182 delete mixer;
183 delete[] mixBuffer;
184 return false;
185 case FastMixerState::MIX:
186 case FastMixerState::WRITE:
187 case FastMixerState::MIX_WRITE:
188 break;
189 default:
190 LOG_FATAL("bad command %d", command);
191 }
192
193 // there is a non-idle state available to us; did the state change?
194 size_t frameCount = current->mFrameCount;
195 if (current != previous) {
196
197 // handle state change here, but since we want to diff the state,
198 // we're prepared for previous == &initial the first time through
199 unsigned previousTrackMask;
200
201 // check for change in output HAL configuration
202 NBAIO_Format previousFormat = format;
203 if (current->mOutputSinkGen != outputSinkGen) {
204 outputSink = current->mOutputSink;
205 outputSinkGen = current->mOutputSinkGen;
206 if (outputSink == NULL) {
207 format = Format_Invalid;
208 sampleRate = 0;
209 } else {
210 format = outputSink->format();
211 sampleRate = Format_sampleRate(format);
212 ALOG_ASSERT(Format_channelCount(format) == 2);
213 }
Glenn Kasten21e8c502012-04-12 09:39:42 -0700214 dumpState->mSampleRate = sampleRate;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700215 }
216
217 if ((format != previousFormat) || (frameCount != previous->mFrameCount)) {
218 // FIXME to avoid priority inversion, don't delete here
219 delete mixer;
220 mixer = NULL;
221 delete[] mixBuffer;
222 mixBuffer = NULL;
223 if (frameCount > 0 && sampleRate > 0) {
224 // FIXME new may block for unbounded time at internal mutex of the heap
225 // implementation; it would be better to have normal mixer allocate for us
226 // to avoid blocking here and to prevent possible priority inversion
227 mixer = new AudioMixer(frameCount, sampleRate, FastMixerState::kMaxFastTracks);
228 mixBuffer = new short[frameCount * 2];
229 periodNs = (frameCount * 1000000000LL) / sampleRate; // 1.00
230 underrunNs = (frameCount * 1750000000LL) / sampleRate; // 1.75
Glenn Kasten0d27c652012-08-07 10:38:59 -0700231 overrunNs = (frameCount * 500000000LL) / sampleRate; // 0.50
232 forceNs = (frameCount * 950000000LL) / sampleRate; // 0.95
Glenn Kasten288ed212012-04-25 17:52:27 -0700233 warmupNs = (frameCount * 500000000LL) / sampleRate; // 0.50
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700234 } else {
235 periodNs = 0;
236 underrunNs = 0;
237 overrunNs = 0;
Glenn Kasten972af222012-06-13 17:14:03 -0700238 forceNs = 0;
239 warmupNs = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700240 }
241 mixBufferState = UNDEFINED;
242#if !LOG_NDEBUG
243 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
244 fastTrackNames[i] = -1;
245 }
246#endif
247 // we need to reconfigure all active tracks
248 previousTrackMask = 0;
249 fastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten21e8c502012-04-12 09:39:42 -0700250 dumpState->mFrameCount = frameCount;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700251 } else {
252 previousTrackMask = previous->mTrackMask;
253 }
254
255 // check for change in active track set
256 unsigned currentTrackMask = current->mTrackMask;
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700257 dumpState->mTrackMask = currentTrackMask;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700258 if (current->mFastTracksGen != fastTracksGen) {
259 ALOG_ASSERT(mixBuffer != NULL);
260 int name;
261
262 // process removed tracks first to avoid running out of track names
263 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
264 while (removedTracks != 0) {
265 i = __builtin_ctz(removedTracks);
266 removedTracks &= ~(1 << i);
267 const FastTrack* fastTrack = &current->mFastTracks[i];
Glenn Kasten288ed212012-04-25 17:52:27 -0700268 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700269 if (mixer != NULL) {
270 name = fastTrackNames[i];
271 ALOG_ASSERT(name >= 0);
272 mixer->deleteTrackName(name);
273 }
274#if !LOG_NDEBUG
275 fastTrackNames[i] = -1;
276#endif
Glenn Kasten288ed212012-04-25 17:52:27 -0700277 // don't reset track dump state, since other side is ignoring it
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700278 generations[i] = fastTrack->mGeneration;
279 }
280
281 // now process added tracks
282 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
283 while (addedTracks != 0) {
284 i = __builtin_ctz(addedTracks);
285 addedTracks &= ~(1 << i);
286 const FastTrack* fastTrack = &current->mFastTracks[i];
287 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
288 ALOG_ASSERT(bufferProvider != NULL && fastTrackNames[i] == -1);
289 if (mixer != NULL) {
Jean-Michel Trivicff71372012-09-10 18:58:27 -0700290 // calling getTrackName with default channel mask and a random invalid
291 // sessionId (no effects here)
292 name = mixer->getTrackName(AUDIO_CHANNEL_OUT_STEREO, -555);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700293 ALOG_ASSERT(name >= 0);
294 fastTrackNames[i] = name;
295 mixer->setBufferProvider(name, bufferProvider);
296 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
297 (void *) mixBuffer);
298 // newly allocated track names default to full scale volume
Glenn Kasten21e8c502012-04-12 09:39:42 -0700299 if (fastTrack->mSampleRate != 0 && fastTrack->mSampleRate != sampleRate) {
300 mixer->setParameter(name, AudioMixer::RESAMPLE,
301 AudioMixer::SAMPLE_RATE, (void*) fastTrack->mSampleRate);
302 }
303 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
304 (void *) fastTrack->mChannelMask);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700305 mixer->enable(name);
306 }
307 generations[i] = fastTrack->mGeneration;
308 }
309
310 // finally process modified tracks; these use the same slot
311 // but may have a different buffer provider or volume provider
312 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
313 while (modifiedTracks != 0) {
314 i = __builtin_ctz(modifiedTracks);
315 modifiedTracks &= ~(1 << i);
316 const FastTrack* fastTrack = &current->mFastTracks[i];
317 if (fastTrack->mGeneration != generations[i]) {
318 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
319 ALOG_ASSERT(bufferProvider != NULL);
320 if (mixer != NULL) {
321 name = fastTrackNames[i];
322 ALOG_ASSERT(name >= 0);
323 mixer->setBufferProvider(name, bufferProvider);
324 if (fastTrack->mVolumeProvider == NULL) {
325 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0,
326 (void *)0x1000);
327 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1,
328 (void *)0x1000);
329 }
Glenn Kasten21e8c502012-04-12 09:39:42 -0700330 if (fastTrack->mSampleRate != 0 &&
331 fastTrack->mSampleRate != sampleRate) {
332 mixer->setParameter(name, AudioMixer::RESAMPLE,
333 AudioMixer::SAMPLE_RATE, (void*) fastTrack->mSampleRate);
334 } else {
335 mixer->setParameter(name, AudioMixer::RESAMPLE,
336 AudioMixer::REMOVE, NULL);
337 }
338 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
339 (void *) fastTrack->mChannelMask);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700340 // already enabled
341 }
342 generations[i] = fastTrack->mGeneration;
343 }
344 }
345
346 fastTracksGen = current->mFastTracksGen;
347
348 dumpState->mNumTracks = popcount(currentTrackMask);
349 }
350
351#if 1 // FIXME shouldn't need this
352 // only process state change once
353 previous = current;
354#endif
355 }
356
357 // do work using current state here
Glenn Kasten288ed212012-04-25 17:52:27 -0700358 if ((command & FastMixerState::MIX) && (mixer != NULL) && isWarm) {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700359 ALOG_ASSERT(mixBuffer != NULL);
Glenn Kasten288ed212012-04-25 17:52:27 -0700360 // for each track, update volume and check for underrun
361 unsigned currentTrackMask = current->mTrackMask;
362 while (currentTrackMask != 0) {
363 i = __builtin_ctz(currentTrackMask);
364 currentTrackMask &= ~(1 << i);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700365 const FastTrack* fastTrack = &current->mFastTracks[i];
366 int name = fastTrackNames[i];
367 ALOG_ASSERT(name >= 0);
368 if (fastTrack->mVolumeProvider != NULL) {
369 uint32_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
370 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0,
371 (void *)(vlr & 0xFFFF));
372 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1,
373 (void *)(vlr >> 16));
374 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700375 // FIXME The current implementation of framesReady() for fast tracks
376 // takes a tryLock, which can block
377 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
378 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
379 size_t framesReady = fastTrack->mBufferProvider->framesReady();
Glenn Kasten99c99d02012-05-14 16:37:13 -0700380#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER)
381 // I wish we had formatted trace names
382 char traceName[16];
383 strcpy(traceName, "framesReady");
384 traceName[11] = i + (i < 10 ? '0' : 'A' - 10);
385 traceName[12] = '\0';
386 ATRACE_INT(traceName, framesReady);
387#endif
Glenn Kasten288ed212012-04-25 17:52:27 -0700388 FastTrackDump *ftDump = &dumpState->mTracks[i];
Glenn Kasten09474df2012-05-10 14:48:07 -0700389 FastTrackUnderruns underruns = ftDump->mUnderruns;
Glenn Kasten288ed212012-04-25 17:52:27 -0700390 if (framesReady < frameCount) {
Glenn Kasten288ed212012-04-25 17:52:27 -0700391 if (framesReady == 0) {
Glenn Kasten09474df2012-05-10 14:48:07 -0700392 underruns.mBitFields.mEmpty++;
393 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
Glenn Kasten288ed212012-04-25 17:52:27 -0700394 mixer->disable(name);
395 } else {
396 // allow mixing partial buffer
Glenn Kasten09474df2012-05-10 14:48:07 -0700397 underruns.mBitFields.mPartial++;
398 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kasten288ed212012-04-25 17:52:27 -0700399 mixer->enable(name);
400 }
Glenn Kasten09474df2012-05-10 14:48:07 -0700401 } else {
402 underruns.mBitFields.mFull++;
403 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kasten288ed212012-04-25 17:52:27 -0700404 mixer->enable(name);
405 }
Glenn Kasten09474df2012-05-10 14:48:07 -0700406 ftDump->mUnderruns = underruns;
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700407 ftDump->mFramesReady = framesReady;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700408 }
John Grossman2c3b2da2012-08-02 17:08:54 -0700409
410 int64_t pts;
411 if (outputSink == NULL || (OK != outputSink->getNextWriteTimestamp(&pts)))
412 pts = AudioBufferProvider::kInvalidPTS;
413
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700414 // process() is CPU-bound
John Grossman2c3b2da2012-08-02 17:08:54 -0700415 mixer->process(pts);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700416 mixBufferState = MIXED;
417 } else if (mixBufferState == MIXED) {
418 mixBufferState = UNDEFINED;
419 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700420 bool attemptedWrite = false;
421 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700422 if ((command & FastMixerState::WRITE) && (outputSink != NULL) && (mixBuffer != NULL)) {
423 if (mixBufferState == UNDEFINED) {
424 memset(mixBuffer, 0, frameCount * 2 * sizeof(short));
425 mixBufferState = ZEROED;
426 }
Glenn Kastenfbae5da2012-05-21 09:17:20 -0700427 if (teeSink != NULL) {
428 (void) teeSink->write(mixBuffer, frameCount);
429 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700430 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
431 // but this code should be modified to handle both non-blocking and blocking sinks
432 dumpState->mWriteSequence++;
Glenn Kasten99c99d02012-05-14 16:37:13 -0700433#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER)
Glenn Kastend8e6fd32012-05-07 11:07:57 -0700434 Tracer::traceBegin(ATRACE_TAG, "write");
Glenn Kasten99c99d02012-05-14 16:37:13 -0700435#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700436 ssize_t framesWritten = outputSink->write(mixBuffer, frameCount);
Glenn Kasten99c99d02012-05-14 16:37:13 -0700437#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER)
Glenn Kastend8e6fd32012-05-07 11:07:57 -0700438 Tracer::traceEnd(ATRACE_TAG);
Glenn Kasten99c99d02012-05-14 16:37:13 -0700439#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700440 dumpState->mWriteSequence++;
441 if (framesWritten >= 0) {
Glenn Kasten288ed212012-04-25 17:52:27 -0700442 ALOG_ASSERT(framesWritten <= frameCount);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700443 dumpState->mFramesWritten += framesWritten;
Glenn Kasten288ed212012-04-25 17:52:27 -0700444 //if ((size_t) framesWritten == frameCount) {
445 // didFullWrite = true;
446 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700447 } else {
448 dumpState->mWriteErrors++;
449 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700450 attemptedWrite = true;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700451 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
452 }
453
454 // To be exactly periodic, compute the next sleep time based on current time.
455 // This code doesn't have long-term stability when the sink is non-blocking.
456 // FIXME To avoid drift, use the local audio clock or watch the sink's fill status.
457 struct timespec newTs;
458 int rc = clock_gettime(CLOCK_MONOTONIC, &newTs);
459 if (rc == 0) {
460 if (oldTsValid) {
461 time_t sec = newTs.tv_sec - oldTs.tv_sec;
462 long nsec = newTs.tv_nsec - oldTs.tv_nsec;
Glenn Kasten99ae06b2012-09-24 11:29:00 -0700463 ALOGE_IF(sec < 0 || (sec == 0 && nsec < 0),
464 "clock_gettime(CLOCK_MONOTONIC) failed: was %ld.%09ld but now %ld.%09ld",
465 oldTs.tv_sec, oldTs.tv_nsec, newTs.tv_sec, newTs.tv_nsec);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700466 if (nsec < 0) {
467 --sec;
468 nsec += 1000000000;
469 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700470 // To avoid an initial underrun on fast tracks after exiting standby,
471 // do not start pulling data from tracks and mixing until warmup is complete.
472 // Warmup is considered complete after the earlier of:
Glenn Kasteneb157162012-06-13 14:59:07 -0700473 // MIN_WARMUP_CYCLES write() attempts and last one blocks for at least warmupNs
Glenn Kasten288ed212012-04-25 17:52:27 -0700474 // MAX_WARMUP_CYCLES write() attempts.
475 // This is overly conservative, but to get better accuracy requires a new HAL API.
476 if (!isWarm && attemptedWrite) {
477 measuredWarmupTs.tv_sec += sec;
478 measuredWarmupTs.tv_nsec += nsec;
479 if (measuredWarmupTs.tv_nsec >= 1000000000) {
480 measuredWarmupTs.tv_sec++;
481 measuredWarmupTs.tv_nsec -= 1000000000;
482 }
483 ++warmupCycles;
Glenn Kasteneb157162012-06-13 14:59:07 -0700484 if ((nsec > warmupNs && warmupCycles >= MIN_WARMUP_CYCLES) ||
Glenn Kasten288ed212012-04-25 17:52:27 -0700485 (warmupCycles >= MAX_WARMUP_CYCLES)) {
486 isWarm = true;
487 dumpState->mMeasuredWarmupTs = measuredWarmupTs;
488 dumpState->mWarmupCycles = warmupCycles;
489 }
490 }
Glenn Kasten972af222012-06-13 17:14:03 -0700491 sleepNs = -1;
492 if (isWarm) {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700493 if (sec > 0 || nsec > underrunNs) {
Glenn Kasten99c99d02012-05-14 16:37:13 -0700494#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER)
Glenn Kastend8e6fd32012-05-07 11:07:57 -0700495 ScopedTrace st(ATRACE_TAG, "underrun");
Glenn Kasten99c99d02012-05-14 16:37:13 -0700496#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700497 // FIXME only log occasionally
498 ALOGV("underrun: time since last cycle %d.%03ld sec",
499 (int) sec, nsec / 1000000L);
500 dumpState->mUnderruns++;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700501 ignoreNextOverrun = true;
502 } else if (nsec < overrunNs) {
503 if (ignoreNextOverrun) {
504 ignoreNextOverrun = false;
505 } else {
506 // FIXME only log occasionally
507 ALOGV("overrun: time since last cycle %d.%03ld sec",
508 (int) sec, nsec / 1000000L);
509 dumpState->mOverruns++;
510 }
Glenn Kasten972af222012-06-13 17:14:03 -0700511 // This forces a minimum cycle time. It:
512 // - compensates for an audio HAL with jitter due to sample rate conversion
513 // - works with a variable buffer depth audio HAL that never pulls at a rate
514 // < than overrunNs per buffer.
515 // - recovers from overrun immediately after underrun
516 // It doesn't work with a non-blocking audio HAL.
517 sleepNs = forceNs - nsec;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700518 } else {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700519 ignoreNextOverrun = false;
520 }
Glenn Kasten972af222012-06-13 17:14:03 -0700521 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700522#ifdef FAST_MIXER_STATISTICS
Glenn Kasteneb157162012-06-13 14:59:07 -0700523 if (isWarm) {
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700524 // advance the FIFO queue bounds
525 size_t i = bounds & (FastMixerDumpState::kSamplingN - 1);
Glenn Kastene58ccce2012-05-11 15:19:24 -0700526 bounds = (bounds & 0xFFFF0000) | ((bounds + 1) & 0xFFFF);
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700527 if (full) {
528 bounds += 0x10000;
529 } else if (!(bounds & (FastMixerDumpState::kSamplingN - 1))) {
530 full = true;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700531 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700532 // compute the delta value of clock_gettime(CLOCK_MONOTONIC)
533 uint32_t monotonicNs = nsec;
534 if (sec > 0 && sec < 4) {
535 monotonicNs += sec * 1000000000;
536 }
537 // compute the raw CPU load = delta value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
538 uint32_t loadNs = 0;
539 struct timespec newLoad;
540 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &newLoad);
541 if (rc == 0) {
542 if (oldLoadValid) {
543 sec = newLoad.tv_sec - oldLoad.tv_sec;
544 nsec = newLoad.tv_nsec - oldLoad.tv_nsec;
545 if (nsec < 0) {
546 --sec;
547 nsec += 1000000000;
548 }
549 loadNs = nsec;
550 if (sec > 0 && sec < 4) {
551 loadNs += sec * 1000000000;
552 }
553 } else {
554 // first time through the loop
555 oldLoadValid = true;
556 }
557 oldLoad = newLoad;
558 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700559#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700560 // get the absolute value of CPU clock frequency in kHz
561 int cpuNum = sched_getcpu();
562 uint32_t kHz = tcu.getCpukHz(cpuNum);
Glenn Kastenc059bd42012-05-14 17:41:09 -0700563 kHz = (kHz << 4) | (cpuNum & 0xF);
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700564#endif
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700565 // save values in FIFO queues for dumpsys
566 // these stores #1, #2, #3 are not atomic with respect to each other,
567 // or with respect to store #4 below
568 dumpState->mMonotonicNs[i] = monotonicNs;
569 dumpState->mLoadNs[i] = loadNs;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700570#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700571 dumpState->mCpukHz[i] = kHz;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700572#endif
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700573 // this store #4 is not atomic with respect to stores #1, #2, #3 above, but
574 // the newest open and oldest closed halves are atomic with respect to each other
575 dumpState->mBounds = bounds;
Glenn Kasten99c99d02012-05-14 16:37:13 -0700576#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER)
577 ATRACE_INT("cycle_ms", monotonicNs / 1000000);
578 ATRACE_INT("load_us", loadNs / 1000);
579#endif
Glenn Kasteneb157162012-06-13 14:59:07 -0700580 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700581#endif
582 } else {
583 // first time through the loop
584 oldTsValid = true;
585 sleepNs = periodNs;
586 ignoreNextOverrun = true;
587 }
588 oldTs = newTs;
589 } else {
590 // monotonic clock is broken
591 oldTsValid = false;
592 sleepNs = periodNs;
593 }
594
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700595
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700596 } // for (;;)
597
598 // never return 'true'; Thread::_threadLoop() locks mutex which can result in priority inversion
599}
600
601FastMixerDumpState::FastMixerDumpState() :
602 mCommand(FastMixerState::INITIAL), mWriteSequence(0), mFramesWritten(0),
Glenn Kasten21e8c502012-04-12 09:39:42 -0700603 mNumTracks(0), mWriteErrors(0), mUnderruns(0), mOverruns(0),
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700604 mSampleRate(0), mFrameCount(0), /* mMeasuredWarmupTs({0, 0}), */ mWarmupCycles(0),
605 mTrackMask(0)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700606#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700607 , mBounds(0)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700608#endif
609{
Glenn Kasten288ed212012-04-25 17:52:27 -0700610 mMeasuredWarmupTs.tv_sec = 0;
611 mMeasuredWarmupTs.tv_nsec = 0;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700612 // sample arrays aren't accessed atomically with respect to the bounds,
613 // so clearing reduces chance for dumpsys to read random uninitialized samples
614 memset(&mMonotonicNs, 0, sizeof(mMonotonicNs));
615 memset(&mLoadNs, 0, sizeof(mLoadNs));
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700616#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700617 memset(&mCpukHz, 0, sizeof(mCpukHz));
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700618#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700619}
620
621FastMixerDumpState::~FastMixerDumpState()
622{
623}
624
Glenn Kastenba8a6162012-09-07 12:58:38 -0700625// helper function called by qsort()
626static int compare_uint32_t(const void *pa, const void *pb)
627{
628 uint32_t a = *(const uint32_t *)pa;
629 uint32_t b = *(const uint32_t *)pb;
630 if (a < b) {
631 return -1;
632 } else if (a > b) {
633 return 1;
634 } else {
635 return 0;
636 }
637}
638
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700639void FastMixerDumpState::dump(int fd)
640{
Glenn Kasten868c0ab2012-06-13 14:59:17 -0700641 if (mCommand == FastMixerState::INITIAL) {
642 fdprintf(fd, "FastMixer not initialized\n");
643 return;
644 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700645#define COMMAND_MAX 32
646 char string[COMMAND_MAX];
647 switch (mCommand) {
648 case FastMixerState::INITIAL:
649 strcpy(string, "INITIAL");
650 break;
651 case FastMixerState::HOT_IDLE:
652 strcpy(string, "HOT_IDLE");
653 break;
654 case FastMixerState::COLD_IDLE:
655 strcpy(string, "COLD_IDLE");
656 break;
657 case FastMixerState::EXIT:
658 strcpy(string, "EXIT");
659 break;
660 case FastMixerState::MIX:
661 strcpy(string, "MIX");
662 break;
663 case FastMixerState::WRITE:
664 strcpy(string, "WRITE");
665 break;
666 case FastMixerState::MIX_WRITE:
667 strcpy(string, "MIX_WRITE");
668 break;
669 default:
670 snprintf(string, COMMAND_MAX, "%d", mCommand);
671 break;
672 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700673 double measuredWarmupMs = (mMeasuredWarmupTs.tv_sec * 1000.0) +
Glenn Kasten288ed212012-04-25 17:52:27 -0700674 (mMeasuredWarmupTs.tv_nsec / 1000000.0);
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700675 double mixPeriodSec = (double) mFrameCount / (double) mSampleRate;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700676 fdprintf(fd, "FastMixer command=%s writeSequence=%u framesWritten=%u\n"
Glenn Kasten21e8c502012-04-12 09:39:42 -0700677 " numTracks=%u writeErrors=%u underruns=%u overruns=%u\n"
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700678 " sampleRate=%u frameCount=%u measuredWarmup=%.3g ms, warmupCycles=%u\n"
679 " mixPeriod=%.2f ms\n",
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700680 string, mWriteSequence, mFramesWritten,
Glenn Kasten21e8c502012-04-12 09:39:42 -0700681 mNumTracks, mWriteErrors, mUnderruns, mOverruns,
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700682 mSampleRate, mFrameCount, measuredWarmupMs, mWarmupCycles,
683 mixPeriodSec * 1e3);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700684#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700685 // find the interval of valid samples
686 uint32_t bounds = mBounds;
687 uint32_t newestOpen = bounds & 0xFFFF;
688 uint32_t oldestClosed = bounds >> 16;
689 uint32_t n = (newestOpen - oldestClosed) & 0xFFFF;
690 if (n > kSamplingN) {
691 ALOGE("too many samples %u", n);
692 n = kSamplingN;
693 }
694 // statistics for monotonic (wall clock) time, thread raw CPU load in time, CPU clock frequency,
695 // and adjusted CPU load in MHz normalized for CPU clock frequency
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700696 CentralTendencyStatistics wall, loadNs;
697#ifdef CPU_FREQUENCY_STATISTICS
698 CentralTendencyStatistics kHz, loadMHz;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700699 uint32_t previousCpukHz = 0;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700700#endif
Glenn Kastenba8a6162012-09-07 12:58:38 -0700701 // Assuming a normal distribution for cycle times, three standard deviations on either side of
702 // the mean account for 99.73% of the population. So if we take each tail to be 1/1000 of the
703 // sample set, we get 99.8% combined, or close to three standard deviations.
704 static const uint32_t kTailDenominator = 1000;
705 uint32_t *tail = n >= kTailDenominator ? new uint32_t[n] : NULL;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700706 // loop over all the samples
Glenn Kastenba8a6162012-09-07 12:58:38 -0700707 for (uint32_t j = 0; j < n; ++j) {
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700708 size_t i = oldestClosed++ & (kSamplingN - 1);
709 uint32_t wallNs = mMonotonicNs[i];
Glenn Kastenba8a6162012-09-07 12:58:38 -0700710 if (tail != NULL) {
711 tail[j] = wallNs;
712 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700713 wall.sample(wallNs);
714 uint32_t sampleLoadNs = mLoadNs[i];
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700715 loadNs.sample(sampleLoadNs);
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700716#ifdef CPU_FREQUENCY_STATISTICS
717 uint32_t sampleCpukHz = mCpukHz[i];
Glenn Kastenc059bd42012-05-14 17:41:09 -0700718 // skip bad kHz samples
719 if ((sampleCpukHz & ~0xF) != 0) {
720 kHz.sample(sampleCpukHz >> 4);
721 if (sampleCpukHz == previousCpukHz) {
722 double megacycles = (double) sampleLoadNs * (double) (sampleCpukHz >> 4) * 1e-12;
723 double adjMHz = megacycles / mixPeriodSec; // _not_ wallNs * 1e9
724 loadMHz.sample(adjMHz);
725 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700726 }
727 previousCpukHz = sampleCpukHz;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700728#endif
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700729 }
730 fdprintf(fd, "Simple moving statistics over last %.1f seconds:\n", wall.n() * mixPeriodSec);
731 fdprintf(fd, " wall clock time in ms per mix cycle:\n"
732 " mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
733 wall.mean()*1e-6, wall.minimum()*1e-6, wall.maximum()*1e-6, wall.stddev()*1e-6);
734 fdprintf(fd, " raw CPU load in us per mix cycle:\n"
735 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
736 loadNs.mean()*1e-3, loadNs.minimum()*1e-3, loadNs.maximum()*1e-3,
737 loadNs.stddev()*1e-3);
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700738#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700739 fdprintf(fd, " CPU clock frequency in MHz:\n"
740 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
741 kHz.mean()*1e-3, kHz.minimum()*1e-3, kHz.maximum()*1e-3, kHz.stddev()*1e-3);
742 fdprintf(fd, " adjusted CPU load in MHz (i.e. normalized for CPU clock frequency):\n"
743 " mean=%.1f min=%.1f max=%.1f stddev=%.1f\n",
744 loadMHz.mean(), loadMHz.minimum(), loadMHz.maximum(), loadMHz.stddev());
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700745#endif
Glenn Kastenba8a6162012-09-07 12:58:38 -0700746 if (tail != NULL) {
747 qsort(tail, n, sizeof(uint32_t), compare_uint32_t);
748 // assume same number of tail samples on each side, left and right
749 uint32_t count = n / kTailDenominator;
750 CentralTendencyStatistics left, right;
751 for (uint32_t i = 0; i < count; ++i) {
752 left.sample(tail[i]);
753 right.sample(tail[n - (i + 1)]);
754 }
755 fdprintf(fd, "Distribution of mix cycle times in ms for the tails (> ~3 stddev outliers):\n"
756 " left tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n"
757 " right tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
758 left.mean()*1e-6, left.minimum()*1e-6, left.maximum()*1e-6, left.stddev()*1e-6,
759 right.mean()*1e-6, right.minimum()*1e-6, right.maximum()*1e-6,
760 right.stddev()*1e-6);
761 delete[] tail;
762 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700763#endif
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700764 // The active track mask and track states are updated non-atomically.
765 // So if we relied on isActive to decide whether to display,
766 // then we might display an obsolete track or omit an active track.
767 // Instead we always display all tracks, with an indication
768 // of whether we think the track is active.
769 uint32_t trackMask = mTrackMask;
770 fdprintf(fd, "Fast tracks: kMaxFastTracks=%u activeMask=%#x\n",
771 FastMixerState::kMaxFastTracks, trackMask);
772 fdprintf(fd, "Index Active Full Partial Empty Recent Ready\n");
773 for (uint32_t i = 0; i < FastMixerState::kMaxFastTracks; ++i, trackMask >>= 1) {
774 bool isActive = trackMask & 1;
775 const FastTrackDump *ftDump = &mTracks[i];
776 const FastTrackUnderruns& underruns = ftDump->mUnderruns;
777 const char *mostRecent;
778 switch (underruns.mBitFields.mMostRecent) {
779 case UNDERRUN_FULL:
780 mostRecent = "full";
781 break;
782 case UNDERRUN_PARTIAL:
783 mostRecent = "partial";
784 break;
785 case UNDERRUN_EMPTY:
786 mostRecent = "empty";
787 break;
788 default:
789 mostRecent = "?";
790 break;
791 }
792 fdprintf(fd, "%5u %6s %4u %7u %5u %7s %5u\n", i, isActive ? "yes" : "no",
793 (underruns.mBitFields.mFull) & UNDERRUN_MASK,
794 (underruns.mBitFields.mPartial) & UNDERRUN_MASK,
795 (underruns.mBitFields.mEmpty) & UNDERRUN_MASK,
796 mostRecent, ftDump->mFramesReady);
797 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700798}
799
800} // namespace android