blob: 2678cbffbbae78ab51ecf21937c6d5d2d063c0df [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 <time.h>
30#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070031#include <utils/Trace.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070032#include <system/audio.h>
33#ifdef FAST_MIXER_STATISTICS
34#include <cpustats/CentralTendencyStatistics.h>
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070035#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -070036#include <cpustats/ThreadCpuUsage.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070037#endif
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070038#endif
Andy Hung1258c1a2014-05-23 21:22:17 -070039#include <audio_utils/format.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070040#include "AudioMixer.h"
41#include "FastMixer.h"
42
Glenn Kasten7fc97ba2013-07-16 17:18:58 -070043#define FCC_2 2 // fixed channel count assumption
44
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070045namespace android {
46
Glenn Kasten22340022014-04-07 12:04:41 -070047/*static*/ const FastMixerState FastMixer::initial;
48
49FastMixer::FastMixer() : FastThread(),
50 slopNs(0),
51 // fastTrackNames
52 // generations
53 outputSink(NULL),
54 outputSinkGen(0),
55 mixer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070056 mSinkBuffer(NULL),
57 mSinkBufferSize(0),
Andy Hung9a592762014-07-21 21:56:01 -070058 mSinkChannelCount(FCC_2),
Andy Hung45d68d32014-05-23 21:13:31 -070059 mMixerBuffer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070060 mMixerBufferSize(0),
61 mMixerBufferFormat(AUDIO_FORMAT_PCM_16_BIT),
Andy Hung45d68d32014-05-23 21:13:31 -070062 mMixerBufferState(UNDEFINED),
Glenn Kasten22340022014-04-07 12:04:41 -070063 format(Format_Invalid),
64 sampleRate(0),
65 fastTracksGen(0),
66 totalNativeFramesWritten(0),
67 // timestamp
68 nativeFramesWrittenButNotPresented(0) // the = 0 is to silence the compiler
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070069{
Glenn Kasten22340022014-04-07 12:04:41 -070070 // FIXME pass initial as parameter to base class constructor, and make it static local
71 previous = &initial;
72 current = &initial;
73
74 mDummyDumpState = &dummyDumpState;
Andy Hung9a592762014-07-21 21:56:01 -070075 // TODO: Add channel mask to NBAIO_Format.
76 // We assume that the channel mask must be a valid positional channel mask.
77 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
Glenn Kasten22340022014-04-07 12:04:41 -070078
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070079 unsigned i;
80 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
81 fastTrackNames[i] = -1;
82 generations[i] = 0;
83 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070084#ifdef FAST_MIXER_STATISTICS
Glenn Kasten22340022014-04-07 12:04:41 -070085 oldLoad.tv_sec = 0;
86 oldLoad.tv_nsec = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070087#endif
Glenn Kasten22340022014-04-07 12:04:41 -070088}
89
90FastMixer::~FastMixer()
91{
92}
93
94FastMixerStateQueue* FastMixer::sq()
95{
96 return &mSQ;
97}
98
99const FastThreadState *FastMixer::poll()
100{
101 return mSQ.poll();
102}
103
104void FastMixer::setLog(NBLog::Writer *logWriter)
105{
106 if (mixer != NULL) {
107 mixer->setLog(logWriter);
108 }
109}
110
111void FastMixer::onIdle()
112{
113 preIdle = *(const FastMixerState *)current;
114 current = &preIdle;
115}
116
117void FastMixer::onExit()
118{
119 delete mixer;
Andy Hung1258c1a2014-05-23 21:22:17 -0700120 free(mMixerBuffer);
121 free(mSinkBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700122}
123
124bool FastMixer::isSubClassCommand(FastThreadState::Command command)
125{
126 switch ((FastMixerState::Command) command) {
127 case FastMixerState::MIX:
128 case FastMixerState::WRITE:
129 case FastMixerState::MIX_WRITE:
130 return true;
131 default:
132 return false;
133 }
134}
135
136void FastMixer::onStateChange()
137{
138 const FastMixerState * const current = (const FastMixerState *) this->current;
139 const FastMixerState * const previous = (const FastMixerState *) this->previous;
140 FastMixerDumpState * const dumpState = (FastMixerDumpState *) this->dumpState;
141 const size_t frameCount = current->mFrameCount;
142
143 // handle state change here, but since we want to diff the state,
144 // we're prepared for previous == &initial the first time through
145 unsigned previousTrackMask;
146
147 // check for change in output HAL configuration
148 NBAIO_Format previousFormat = format;
149 if (current->mOutputSinkGen != outputSinkGen) {
150 outputSink = current->mOutputSink;
151 outputSinkGen = current->mOutputSinkGen;
152 if (outputSink == NULL) {
153 format = Format_Invalid;
154 sampleRate = 0;
Andy Hung9a592762014-07-21 21:56:01 -0700155 mSinkChannelCount = 0;
156 mSinkChannelMask = AUDIO_CHANNEL_NONE;
Glenn Kasten22340022014-04-07 12:04:41 -0700157 } else {
158 format = outputSink->format();
159 sampleRate = Format_sampleRate(format);
Andy Hung9a592762014-07-21 21:56:01 -0700160 mSinkChannelCount = Format_channelCount(format);
161 LOG_ALWAYS_FATAL_IF(mSinkChannelCount > AudioMixer::MAX_NUM_CHANNELS);
162
163 // TODO: Add channel mask to NBAIO_Format
164 // We assume that the channel mask must be a valid positional channel mask.
165 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700166 }
167 dumpState->mSampleRate = sampleRate;
168 }
169
170 if ((!Format_isEqual(format, previousFormat)) || (frameCount != previous->mFrameCount)) {
171 // FIXME to avoid priority inversion, don't delete here
172 delete mixer;
173 mixer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700174 free(mMixerBuffer);
Andy Hung45d68d32014-05-23 21:13:31 -0700175 mMixerBuffer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700176 free(mSinkBuffer);
177 mSinkBuffer = NULL;
Glenn Kasten22340022014-04-07 12:04:41 -0700178 if (frameCount > 0 && sampleRate > 0) {
179 // FIXME new may block for unbounded time at internal mutex of the heap
180 // implementation; it would be better to have normal mixer allocate for us
181 // to avoid blocking here and to prevent possible priority inversion
182 mixer = new AudioMixer(frameCount, sampleRate, FastMixerState::kMaxFastTracks);
Andy Hung9a592762014-07-21 21:56:01 -0700183 const size_t mixerFrameSize = mSinkChannelCount
184 * audio_bytes_per_sample(mMixerBufferFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700185 mMixerBufferSize = mixerFrameSize * frameCount;
186 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
Andy Hung9a592762014-07-21 21:56:01 -0700187 const size_t sinkFrameSize = mSinkChannelCount
188 * audio_bytes_per_sample(format.mFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700189 if (sinkFrameSize > mixerFrameSize) { // need a sink buffer
190 mSinkBufferSize = sinkFrameSize * frameCount;
191 (void)posix_memalign(&mSinkBuffer, 32, mSinkBufferSize);
192 }
Glenn Kasten22340022014-04-07 12:04:41 -0700193 periodNs = (frameCount * 1000000000LL) / sampleRate; // 1.00
194 underrunNs = (frameCount * 1750000000LL) / sampleRate; // 1.75
195 overrunNs = (frameCount * 500000000LL) / sampleRate; // 0.50
196 forceNs = (frameCount * 950000000LL) / sampleRate; // 0.95
197 warmupNs = (frameCount * 500000000LL) / sampleRate; // 0.50
198 } else {
199 periodNs = 0;
200 underrunNs = 0;
201 overrunNs = 0;
202 forceNs = 0;
203 warmupNs = 0;
204 }
Andy Hung45d68d32014-05-23 21:13:31 -0700205 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700206#if !LOG_NDEBUG
207 for (unsigned i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
208 fastTrackNames[i] = -1;
209 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700210#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700211 // we need to reconfigure all active tracks
212 previousTrackMask = 0;
213 fastTracksGen = current->mFastTracksGen - 1;
214 dumpState->mFrameCount = frameCount;
215 } else {
216 previousTrackMask = previous->mTrackMask;
217 }
Glenn Kasten732845c2013-08-23 09:26:31 -0700218
Glenn Kasten22340022014-04-07 12:04:41 -0700219 // check for change in active track set
220 const unsigned currentTrackMask = current->mTrackMask;
221 dumpState->mTrackMask = currentTrackMask;
222 if (current->mFastTracksGen != fastTracksGen) {
Andy Hung45d68d32014-05-23 21:13:31 -0700223 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700224 int name;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700225
Glenn Kasten22340022014-04-07 12:04:41 -0700226 // process removed tracks first to avoid running out of track names
227 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
228 while (removedTracks != 0) {
229 int i = __builtin_ctz(removedTracks);
230 removedTracks &= ~(1 << i);
231 const FastTrack* fastTrack = &current->mFastTracks[i];
232 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800233 if (mixer != NULL) {
Glenn Kasten22340022014-04-07 12:04:41 -0700234 name = fastTrackNames[i];
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700235 ALOG_ASSERT(name >= 0);
Glenn Kasten22340022014-04-07 12:04:41 -0700236 mixer->deleteTrackName(name);
237 }
238#if !LOG_NDEBUG
239 fastTrackNames[i] = -1;
240#endif
241 // don't reset track dump state, since other side is ignoring it
242 generations[i] = fastTrack->mGeneration;
243 }
244
245 // now process added tracks
246 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
247 while (addedTracks != 0) {
248 int i = __builtin_ctz(addedTracks);
249 addedTracks &= ~(1 << i);
250 const FastTrack* fastTrack = &current->mFastTracks[i];
251 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
252 ALOG_ASSERT(bufferProvider != NULL && fastTrackNames[i] == -1);
253 if (mixer != NULL) {
Andy Hunge8a1ced2014-05-09 15:02:21 -0700254 name = mixer->getTrackName(fastTrack->mChannelMask,
255 fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
Glenn Kasten22340022014-04-07 12:04:41 -0700256 ALOG_ASSERT(name >= 0);
257 fastTrackNames[i] = name;
258 mixer->setBufferProvider(name, bufferProvider);
259 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
Andy Hung9a592762014-07-21 21:56:01 -0700260 (void *)mMixerBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700261 // newly allocated track names default to full scale volume
Andy Hung1258c1a2014-05-23 21:22:17 -0700262 mixer->setParameter(
263 name,
264 AudioMixer::TRACK,
265 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700266 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
267 (void *)(uintptr_t)fastTrack->mFormat);
Andy Hung9a592762014-07-21 21:56:01 -0700268 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
269 (void *)(uintptr_t)fastTrack->mChannelMask);
270 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
271 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kasten22340022014-04-07 12:04:41 -0700272 mixer->enable(name);
273 }
274 generations[i] = fastTrack->mGeneration;
275 }
276
277 // finally process (potentially) modified tracks; these use the same slot
278 // but may have a different buffer provider or volume provider
279 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
280 while (modifiedTracks != 0) {
281 int i = __builtin_ctz(modifiedTracks);
282 modifiedTracks &= ~(1 << i);
283 const FastTrack* fastTrack = &current->mFastTracks[i];
284 if (fastTrack->mGeneration != generations[i]) {
285 // this track was actually modified
286 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
287 ALOG_ASSERT(bufferProvider != NULL);
288 if (mixer != NULL) {
289 name = fastTrackNames[i];
290 ALOG_ASSERT(name >= 0);
291 mixer->setBufferProvider(name, bufferProvider);
292 if (fastTrack->mVolumeProvider == NULL) {
Andy Hung6be49402014-05-30 10:42:03 -0700293 float f = AudioMixer::UNITY_GAIN_FLOAT;
294 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &f);
295 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &f);
Glenn Kasten288ed212012-04-25 17:52:27 -0700296 }
Glenn Kasten22340022014-04-07 12:04:41 -0700297 mixer->setParameter(name, AudioMixer::RESAMPLE,
298 AudioMixer::REMOVE, NULL);
Andy Hung1258c1a2014-05-23 21:22:17 -0700299 mixer->setParameter(
300 name,
301 AudioMixer::TRACK,
302 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700303 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
304 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kasten22340022014-04-07 12:04:41 -0700305 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700306 (void *)(uintptr_t)fastTrack->mChannelMask);
307 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
308 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kasten22340022014-04-07 12:04:41 -0700309 // already enabled
310 }
311 generations[i] = fastTrack->mGeneration;
312 }
313 }
314
315 fastTracksGen = current->mFastTracksGen;
316
317 dumpState->mNumTracks = popcount(currentTrackMask);
318 }
319}
320
321void FastMixer::onWork()
322{
323 const FastMixerState * const current = (const FastMixerState *) this->current;
324 FastMixerDumpState * const dumpState = (FastMixerDumpState *) this->dumpState;
325 const FastMixerState::Command command = this->command;
326 const size_t frameCount = current->mFrameCount;
327
328 if ((command & FastMixerState::MIX) && (mixer != NULL) && isWarm) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700329 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700330 // for each track, update volume and check for underrun
331 unsigned currentTrackMask = current->mTrackMask;
332 while (currentTrackMask != 0) {
333 int i = __builtin_ctz(currentTrackMask);
334 currentTrackMask &= ~(1 << i);
335 const FastTrack* fastTrack = &current->mFastTracks[i];
336
337 // Refresh the per-track timestamp
338 if (timestampStatus == NO_ERROR) {
339 uint32_t trackFramesWrittenButNotPresented =
340 nativeFramesWrittenButNotPresented;
341 uint32_t trackFramesWritten = fastTrack->mBufferProvider->framesReleased();
342 // Can't provide an AudioTimestamp before first frame presented,
343 // or during the brief 32-bit wraparound window
344 if (trackFramesWritten >= trackFramesWrittenButNotPresented) {
345 AudioTimestamp perTrackTimestamp;
346 perTrackTimestamp.mPosition =
347 trackFramesWritten - trackFramesWrittenButNotPresented;
348 perTrackTimestamp.mTime = timestamp.mTime;
349 fastTrack->mBufferProvider->onTimestamp(perTrackTimestamp);
350 }
351 }
352
353 int name = fastTrackNames[i];
354 ALOG_ASSERT(name >= 0);
355 if (fastTrack->mVolumeProvider != NULL) {
Glenn Kastenc56f3422014-03-21 17:53:17 -0700356 gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
Andy Hung6be49402014-05-30 10:42:03 -0700357 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
358 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
359
360 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &vlf);
361 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &vrf);
Glenn Kasten22340022014-04-07 12:04:41 -0700362 }
363 // FIXME The current implementation of framesReady() for fast tracks
364 // takes a tryLock, which can block
365 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
366 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
367 size_t framesReady = fastTrack->mBufferProvider->framesReady();
368 if (ATRACE_ENABLED()) {
369 // I wish we had formatted trace names
370 char traceName[16];
371 strcpy(traceName, "fRdy");
372 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
373 traceName[5] = '\0';
374 ATRACE_INT(traceName, framesReady);
375 }
376 FastTrackDump *ftDump = &dumpState->mTracks[i];
377 FastTrackUnderruns underruns = ftDump->mUnderruns;
378 if (framesReady < frameCount) {
379 if (framesReady == 0) {
380 underruns.mBitFields.mEmpty++;
381 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
382 mixer->disable(name);
Glenn Kasten09474df2012-05-10 14:48:07 -0700383 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700384 // allow mixing partial buffer
385 underruns.mBitFields.mPartial++;
386 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kasten288ed212012-04-25 17:52:27 -0700387 mixer->enable(name);
388 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700389 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700390 underruns.mBitFields.mFull++;
391 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
392 mixer->enable(name);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700393 }
Glenn Kasten22340022014-04-07 12:04:41 -0700394 ftDump->mUnderruns = underruns;
395 ftDump->mFramesReady = framesReady;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700396 }
397
Glenn Kasten22340022014-04-07 12:04:41 -0700398 int64_t pts;
399 if (outputSink == NULL || (OK != outputSink->getNextWriteTimestamp(&pts))) {
400 pts = AudioBufferProvider::kInvalidPTS;
401 }
402
403 // process() is CPU-bound
404 mixer->process(pts);
Andy Hung45d68d32014-05-23 21:13:31 -0700405 mMixerBufferState = MIXED;
406 } else if (mMixerBufferState == MIXED) {
407 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700408 }
409 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Andy Hung45d68d32014-05-23 21:13:31 -0700410 if ((command & FastMixerState::WRITE) && (outputSink != NULL) && (mMixerBuffer != NULL)) {
411 if (mMixerBufferState == UNDEFINED) {
Andy Hung1258c1a2014-05-23 21:22:17 -0700412 memset(mMixerBuffer, 0, mMixerBufferSize);
Andy Hung45d68d32014-05-23 21:13:31 -0700413 mMixerBufferState = ZEROED;
Glenn Kasten22340022014-04-07 12:04:41 -0700414 }
Andy Hung1258c1a2014-05-23 21:22:17 -0700415 void *buffer = mSinkBuffer != NULL ? mSinkBuffer : mMixerBuffer;
416 if (format.mFormat != mMixerBufferFormat) { // sink format not the same as mixer format
417 memcpy_by_audio_format(buffer, format.mFormat, mMixerBuffer, mMixerBufferFormat,
418 frameCount * Format_channelCount(format));
419 }
Glenn Kasten22340022014-04-07 12:04:41 -0700420 // if non-NULL, then duplicate write() to this non-blocking sink
421 NBAIO_Sink* teeSink;
422 if ((teeSink = current->mTeeSink) != NULL) {
Glenn Kasten329f6512014-08-28 16:23:16 -0700423 (void) teeSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700424 }
425 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
426 // but this code should be modified to handle both non-blocking and blocking sinks
427 dumpState->mWriteSequence++;
428 ATRACE_BEGIN("write");
Andy Hung1258c1a2014-05-23 21:22:17 -0700429 ssize_t framesWritten = outputSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700430 ATRACE_END();
431 dumpState->mWriteSequence++;
432 if (framesWritten >= 0) {
433 ALOG_ASSERT((size_t) framesWritten <= frameCount);
434 totalNativeFramesWritten += framesWritten;
435 dumpState->mFramesWritten = totalNativeFramesWritten;
436 //if ((size_t) framesWritten == frameCount) {
437 // didFullWrite = true;
438 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700439 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700440 dumpState->mWriteErrors++;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700441 }
Glenn Kasten22340022014-04-07 12:04:41 -0700442 attemptedWrite = true;
443 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700444
Glenn Kasten22340022014-04-07 12:04:41 -0700445 timestampStatus = outputSink->getTimestamp(timestamp);
446 if (timestampStatus == NO_ERROR) {
447 uint32_t totalNativeFramesPresented = timestamp.mPosition;
448 if (totalNativeFramesPresented <= totalNativeFramesWritten) {
449 nativeFramesWrittenButNotPresented =
450 totalNativeFramesWritten - totalNativeFramesPresented;
451 } else {
452 // HAL reported that more frames were presented than were written
453 timestampStatus = INVALID_OPERATION;
454 }
455 }
456 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700457}
458
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700459FastMixerDumpState::FastMixerDumpState(
460#ifdef FAST_MIXER_STATISTICS
461 uint32_t samplingN
462#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700463 ) : FastThreadDumpState(),
464 mWriteSequence(0), mFramesWritten(0),
465 mNumTracks(0), mWriteErrors(0),
466 mSampleRate(0), mFrameCount(0),
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700467 mTrackMask(0)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700468{
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700469#ifdef FAST_MIXER_STATISTICS
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700470 increaseSamplingN(samplingN);
Glenn Kasten153b9fe2013-07-15 11:23:36 -0700471#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700472}
473
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700474#ifdef FAST_MIXER_STATISTICS
475void FastMixerDumpState::increaseSamplingN(uint32_t samplingN)
476{
477 if (samplingN <= mSamplingN || samplingN > kSamplingN || roundup(samplingN) != samplingN) {
478 return;
479 }
480 uint32_t additional = samplingN - mSamplingN;
481 // sample arrays aren't accessed atomically with respect to the bounds,
482 // so clearing reduces chance for dumpsys to read random uninitialized samples
483 memset(&mMonotonicNs[mSamplingN], 0, sizeof(mMonotonicNs[0]) * additional);
484 memset(&mLoadNs[mSamplingN], 0, sizeof(mLoadNs[0]) * additional);
485#ifdef CPU_FREQUENCY_STATISTICS
486 memset(&mCpukHz[mSamplingN], 0, sizeof(mCpukHz[0]) * additional);
487#endif
488 mSamplingN = samplingN;
489}
490#endif
491
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700492FastMixerDumpState::~FastMixerDumpState()
493{
494}
495
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700496// helper function called by qsort()
497static int compare_uint32_t(const void *pa, const void *pb)
498{
499 uint32_t a = *(const uint32_t *)pa;
500 uint32_t b = *(const uint32_t *)pb;
501 if (a < b) {
502 return -1;
503 } else if (a > b) {
504 return 1;
505 } else {
506 return 0;
507 }
508}
509
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700510void FastMixerDumpState::dump(int fd) const
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700511{
Glenn Kasten868c0ab2012-06-13 14:59:17 -0700512 if (mCommand == FastMixerState::INITIAL) {
Elliott Hughes87cebad2014-05-22 10:14:43 -0700513 dprintf(fd, " FastMixer not initialized\n");
Glenn Kasten868c0ab2012-06-13 14:59:17 -0700514 return;
515 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700516#define COMMAND_MAX 32
517 char string[COMMAND_MAX];
518 switch (mCommand) {
519 case FastMixerState::INITIAL:
520 strcpy(string, "INITIAL");
521 break;
522 case FastMixerState::HOT_IDLE:
523 strcpy(string, "HOT_IDLE");
524 break;
525 case FastMixerState::COLD_IDLE:
526 strcpy(string, "COLD_IDLE");
527 break;
528 case FastMixerState::EXIT:
529 strcpy(string, "EXIT");
530 break;
531 case FastMixerState::MIX:
532 strcpy(string, "MIX");
533 break;
534 case FastMixerState::WRITE:
535 strcpy(string, "WRITE");
536 break;
537 case FastMixerState::MIX_WRITE:
538 strcpy(string, "MIX_WRITE");
539 break;
540 default:
541 snprintf(string, COMMAND_MAX, "%d", mCommand);
542 break;
543 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700544 double measuredWarmupMs = (mMeasuredWarmupTs.tv_sec * 1000.0) +
Glenn Kasten288ed212012-04-25 17:52:27 -0700545 (mMeasuredWarmupTs.tv_nsec / 1000000.0);
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700546 double mixPeriodSec = (double) mFrameCount / (double) mSampleRate;
Elliott Hughes87cebad2014-05-22 10:14:43 -0700547 dprintf(fd, " FastMixer command=%s writeSequence=%u framesWritten=%u\n"
548 " numTracks=%u writeErrors=%u underruns=%u overruns=%u\n"
549 " sampleRate=%u frameCount=%zu measuredWarmup=%.3g ms, warmupCycles=%u\n"
550 " mixPeriod=%.2f ms\n",
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700551 string, mWriteSequence, mFramesWritten,
Glenn Kasten21e8c502012-04-12 09:39:42 -0700552 mNumTracks, mWriteErrors, mUnderruns, mOverruns,
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700553 mSampleRate, mFrameCount, measuredWarmupMs, mWarmupCycles,
554 mixPeriodSec * 1e3);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700555#ifdef FAST_MIXER_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700556 // find the interval of valid samples
557 uint32_t bounds = mBounds;
558 uint32_t newestOpen = bounds & 0xFFFF;
559 uint32_t oldestClosed = bounds >> 16;
560 uint32_t n = (newestOpen - oldestClosed) & 0xFFFF;
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700561 if (n > mSamplingN) {
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700562 ALOGE("too many samples %u", n);
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700563 n = mSamplingN;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700564 }
565 // statistics for monotonic (wall clock) time, thread raw CPU load in time, CPU clock frequency,
566 // and adjusted CPU load in MHz normalized for CPU clock frequency
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700567 CentralTendencyStatistics wall, loadNs;
568#ifdef CPU_FREQUENCY_STATISTICS
569 CentralTendencyStatistics kHz, loadMHz;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700570 uint32_t previousCpukHz = 0;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700571#endif
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700572 // Assuming a normal distribution for cycle times, three standard deviations on either side of
573 // the mean account for 99.73% of the population. So if we take each tail to be 1/1000 of the
574 // sample set, we get 99.8% combined, or close to three standard deviations.
575 static const uint32_t kTailDenominator = 1000;
576 uint32_t *tail = n >= kTailDenominator ? new uint32_t[n] : NULL;
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700577 // loop over all the samples
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700578 for (uint32_t j = 0; j < n; ++j) {
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700579 size_t i = oldestClosed++ & (mSamplingN - 1);
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700580 uint32_t wallNs = mMonotonicNs[i];
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700581 if (tail != NULL) {
582 tail[j] = wallNs;
583 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700584 wall.sample(wallNs);
585 uint32_t sampleLoadNs = mLoadNs[i];
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700586 loadNs.sample(sampleLoadNs);
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700587#ifdef CPU_FREQUENCY_STATISTICS
588 uint32_t sampleCpukHz = mCpukHz[i];
Glenn Kastenc059bd42012-05-14 17:41:09 -0700589 // skip bad kHz samples
590 if ((sampleCpukHz & ~0xF) != 0) {
591 kHz.sample(sampleCpukHz >> 4);
592 if (sampleCpukHz == previousCpukHz) {
593 double megacycles = (double) sampleLoadNs * (double) (sampleCpukHz >> 4) * 1e-12;
594 double adjMHz = megacycles / mixPeriodSec; // _not_ wallNs * 1e9
595 loadMHz.sample(adjMHz);
596 }
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700597 }
598 previousCpukHz = sampleCpukHz;
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700599#endif
Glenn Kasten42d45cf2012-05-02 10:34:47 -0700600 }
Marco Nelissenb2208842014-02-07 14:00:50 -0800601 if (n) {
Elliott Hughes87cebad2014-05-22 10:14:43 -0700602 dprintf(fd, " Simple moving statistics over last %.1f seconds:\n",
603 wall.n() * mixPeriodSec);
604 dprintf(fd, " wall clock time in ms per mix cycle:\n"
605 " mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
606 wall.mean()*1e-6, wall.minimum()*1e-6, wall.maximum()*1e-6,
607 wall.stddev()*1e-6);
608 dprintf(fd, " raw CPU load in us per mix cycle:\n"
609 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
610 loadNs.mean()*1e-3, loadNs.minimum()*1e-3, loadNs.maximum()*1e-3,
611 loadNs.stddev()*1e-3);
Marco Nelissenb2208842014-02-07 14:00:50 -0800612 } else {
Elliott Hughes87cebad2014-05-22 10:14:43 -0700613 dprintf(fd, " No FastMixer statistics available currently\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800614 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700615#ifdef CPU_FREQUENCY_STATISTICS
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700616 dprintf(fd, " CPU clock frequency in MHz:\n"
617 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
618 kHz.mean()*1e-3, kHz.minimum()*1e-3, kHz.maximum()*1e-3, kHz.stddev()*1e-3);
619 dprintf(fd, " adjusted CPU load in MHz (i.e. normalized for CPU clock frequency):\n"
620 " mean=%.1f min=%.1f max=%.1f stddev=%.1f\n",
621 loadMHz.mean(), loadMHz.minimum(), loadMHz.maximum(), loadMHz.stddev());
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700622#endif
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700623 if (tail != NULL) {
624 qsort(tail, n, sizeof(uint32_t), compare_uint32_t);
625 // assume same number of tail samples on each side, left and right
626 uint32_t count = n / kTailDenominator;
627 CentralTendencyStatistics left, right;
628 for (uint32_t i = 0; i < count; ++i) {
629 left.sample(tail[i]);
630 right.sample(tail[n - (i + 1)]);
631 }
Elliott Hughes87cebad2014-05-22 10:14:43 -0700632 dprintf(fd, " Distribution of mix cycle times in ms for the tails (> ~3 stddev outliers):\n"
633 " left tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n"
634 " right tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700635 left.mean()*1e-6, left.minimum()*1e-6, left.maximum()*1e-6, left.stddev()*1e-6,
636 right.mean()*1e-6, right.minimum()*1e-6, right.maximum()*1e-6,
637 right.stddev()*1e-6);
Glenn Kasten1ab212cf2012-09-07 12:58:38 -0700638 delete[] tail;
639 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700640#endif
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700641 // The active track mask and track states are updated non-atomically.
642 // So if we relied on isActive to decide whether to display,
643 // then we might display an obsolete track or omit an active track.
644 // Instead we always display all tracks, with an indication
645 // of whether we think the track is active.
646 uint32_t trackMask = mTrackMask;
Elliott Hughes87cebad2014-05-22 10:14:43 -0700647 dprintf(fd, " Fast tracks: kMaxFastTracks=%u activeMask=%#x\n",
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700648 FastMixerState::kMaxFastTracks, trackMask);
Elliott Hughes87cebad2014-05-22 10:14:43 -0700649 dprintf(fd, " Index Active Full Partial Empty Recent Ready\n");
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700650 for (uint32_t i = 0; i < FastMixerState::kMaxFastTracks; ++i, trackMask >>= 1) {
651 bool isActive = trackMask & 1;
652 const FastTrackDump *ftDump = &mTracks[i];
653 const FastTrackUnderruns& underruns = ftDump->mUnderruns;
654 const char *mostRecent;
655 switch (underruns.mBitFields.mMostRecent) {
656 case UNDERRUN_FULL:
657 mostRecent = "full";
658 break;
659 case UNDERRUN_PARTIAL:
660 mostRecent = "partial";
661 break;
662 case UNDERRUN_EMPTY:
663 mostRecent = "empty";
664 break;
665 default:
666 mostRecent = "?";
667 break;
668 }
Elliott Hughes87cebad2014-05-22 10:14:43 -0700669 dprintf(fd, " %5u %6s %4u %7u %5u %7s %5zu\n", i, isActive ? "yes" : "no",
Glenn Kasten1295bb4d2012-05-31 07:43:43 -0700670 (underruns.mBitFields.mFull) & UNDERRUN_MASK,
671 (underruns.mBitFields.mPartial) & UNDERRUN_MASK,
672 (underruns.mBitFields.mEmpty) & UNDERRUN_MASK,
673 mostRecent, ftDump->mFramesReady);
674 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700675}
676
677} // namespace android