FastMixer.cpp revision 1295bb4dcff7b29c75cd23746816df12a871d72c
1/* 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 17#define LOG_TAG "FastMixer" 18//#define LOG_NDEBUG 0 19 20#include <sys/atomics.h> 21#include <time.h> 22#include <utils/Log.h> 23#include <utils/Trace.h> 24#include <system/audio.h> 25#ifdef FAST_MIXER_STATISTICS 26#include <cpustats/CentralTendencyStatistics.h> 27#include <cpustats/ThreadCpuUsage.h> 28#endif 29#include "AudioMixer.h" 30#include "FastMixer.h" 31 32#define FAST_HOT_IDLE_NS 1000000L // 1 ms: time to sleep while hot idling 33#define FAST_DEFAULT_NS 999999999L // ~1 sec: default time to sleep 34#define MAX_WARMUP_CYCLES 10 // maximum number of loop cycles to wait for warmup 35 36namespace android { 37 38// Fast mixer thread 39bool FastMixer::threadLoop() 40{ 41 static const FastMixerState initial; 42 const FastMixerState *previous = &initial, *current = &initial; 43 FastMixerState preIdle; // copy of state before we went into idle 44 struct timespec oldTs = {0, 0}; 45 bool oldTsValid = false; 46 long slopNs = 0; // accumulated time we've woken up too early (> 0) or too late (< 0) 47 long sleepNs = -1; // -1: busy wait, 0: sched_yield, > 0: nanosleep 48 int fastTrackNames[FastMixerState::kMaxFastTracks]; // handles used by mixer to identify tracks 49 int generations[FastMixerState::kMaxFastTracks]; // last observed mFastTracks[i].mGeneration 50 unsigned i; 51 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) { 52 fastTrackNames[i] = -1; 53 generations[i] = 0; 54 } 55 NBAIO_Sink *outputSink = NULL; 56 int outputSinkGen = 0; 57 AudioMixer* mixer = NULL; 58 short *mixBuffer = NULL; 59 enum {UNDEFINED, MIXED, ZEROED} mixBufferState = UNDEFINED; 60 NBAIO_Format format = Format_Invalid; 61 unsigned sampleRate = 0; 62 int fastTracksGen = 0; 63 long periodNs = 0; // expected period; the time required to render one mix buffer 64 long underrunNs = 0; // underrun likely when write cycle is greater than this value 65 long overrunNs = 0; // overrun likely when write cycle is less than this value 66 long warmupNs = 0; // warmup complete when write cycle is greater than to this value 67 FastMixerDumpState dummyDumpState, *dumpState = &dummyDumpState; 68 bool ignoreNextOverrun = true; // used to ignore initial overrun and first after an underrun 69#ifdef FAST_MIXER_STATISTICS 70 struct timespec oldLoad = {0, 0}; // previous value of clock_gettime(CLOCK_THREAD_CPUTIME_ID) 71 bool oldLoadValid = false; // whether oldLoad is valid 72 uint32_t bounds = 0; 73 bool full = false; // whether we have collected at least kSamplingN samples 74 ThreadCpuUsage tcu; // for reading the current CPU clock frequency in kHz 75#endif 76 unsigned coldGen = 0; // last observed mColdGen 77 bool isWarm = false; // true means ready to mix, false means wait for warmup before mixing 78 struct timespec measuredWarmupTs = {0, 0}; // how long did it take for warmup to complete 79 uint32_t warmupCycles = 0; // counter of number of loop cycles required to warmup 80 NBAIO_Sink* teeSink = NULL; // if non-NULL, then duplicate write() to this non-blocking sink 81 82 for (;;) { 83 84 // either nanosleep, sched_yield, or busy wait 85 if (sleepNs >= 0) { 86 if (sleepNs > 0) { 87 ALOG_ASSERT(sleepNs < 1000000000); 88 const struct timespec req = {0, sleepNs}; 89 nanosleep(&req, NULL); 90 } else { 91 sched_yield(); 92 } 93 } 94 // default to long sleep for next cycle 95 sleepNs = FAST_DEFAULT_NS; 96 97 // poll for state change 98 const FastMixerState *next = mSQ.poll(); 99 if (next == NULL) { 100 // continue to use the default initial state until a real state is available 101 ALOG_ASSERT(current == &initial && previous == &initial); 102 next = current; 103 } 104 105 FastMixerState::Command command = next->mCommand; 106 if (next != current) { 107 108 // As soon as possible of learning of a new dump area, start using it 109 dumpState = next->mDumpState != NULL ? next->mDumpState : &dummyDumpState; 110 teeSink = next->mTeeSink; 111 112 // We want to always have a valid reference to the previous (non-idle) state. 113 // However, the state queue only guarantees access to current and previous states. 114 // So when there is a transition from a non-idle state into an idle state, we make a 115 // copy of the last known non-idle state so it is still available on return from idle. 116 // The possible transitions are: 117 // non-idle -> non-idle update previous from current in-place 118 // non-idle -> idle update previous from copy of current 119 // idle -> idle don't update previous 120 // idle -> non-idle don't update previous 121 if (!(current->mCommand & FastMixerState::IDLE)) { 122 if (command & FastMixerState::IDLE) { 123 preIdle = *current; 124 current = &preIdle; 125 oldTsValid = false; 126 oldLoadValid = false; 127 ignoreNextOverrun = true; 128 } 129 previous = current; 130 } 131 current = next; 132 } 133#if !LOG_NDEBUG 134 next = NULL; // not referenced again 135#endif 136 137 dumpState->mCommand = command; 138 139 switch (command) { 140 case FastMixerState::INITIAL: 141 case FastMixerState::HOT_IDLE: 142 sleepNs = FAST_HOT_IDLE_NS; 143 continue; 144 case FastMixerState::COLD_IDLE: 145 // only perform a cold idle command once 146 // FIXME consider checking previous state and only perform if previous != COLD_IDLE 147 if (current->mColdGen != coldGen) { 148 int32_t *coldFutexAddr = current->mColdFutexAddr; 149 ALOG_ASSERT(coldFutexAddr != NULL); 150 int32_t old = android_atomic_dec(coldFutexAddr); 151 if (old <= 0) { 152 __futex_syscall4(coldFutexAddr, FUTEX_WAIT_PRIVATE, old - 1, NULL); 153 } 154 // This may be overly conservative; there could be times that the normal mixer 155 // requests such a brief cold idle that it doesn't require resetting this flag. 156 isWarm = false; 157 measuredWarmupTs.tv_sec = 0; 158 measuredWarmupTs.tv_nsec = 0; 159 warmupCycles = 0; 160 sleepNs = -1; 161 coldGen = current->mColdGen; 162 bounds = 0; 163 full = false; 164 oldTsValid = !clock_gettime(CLOCK_MONOTONIC, &oldTs); 165 } else { 166 sleepNs = FAST_HOT_IDLE_NS; 167 } 168 continue; 169 case FastMixerState::EXIT: 170 delete mixer; 171 delete[] mixBuffer; 172 return false; 173 case FastMixerState::MIX: 174 case FastMixerState::WRITE: 175 case FastMixerState::MIX_WRITE: 176 break; 177 default: 178 LOG_FATAL("bad command %d", command); 179 } 180 181 // there is a non-idle state available to us; did the state change? 182 size_t frameCount = current->mFrameCount; 183 if (current != previous) { 184 185 // handle state change here, but since we want to diff the state, 186 // we're prepared for previous == &initial the first time through 187 unsigned previousTrackMask; 188 189 // check for change in output HAL configuration 190 NBAIO_Format previousFormat = format; 191 if (current->mOutputSinkGen != outputSinkGen) { 192 outputSink = current->mOutputSink; 193 outputSinkGen = current->mOutputSinkGen; 194 if (outputSink == NULL) { 195 format = Format_Invalid; 196 sampleRate = 0; 197 } else { 198 format = outputSink->format(); 199 sampleRate = Format_sampleRate(format); 200 ALOG_ASSERT(Format_channelCount(format) == 2); 201 } 202 dumpState->mSampleRate = sampleRate; 203 } 204 205 if ((format != previousFormat) || (frameCount != previous->mFrameCount)) { 206 // FIXME to avoid priority inversion, don't delete here 207 delete mixer; 208 mixer = NULL; 209 delete[] mixBuffer; 210 mixBuffer = NULL; 211 if (frameCount > 0 && sampleRate > 0) { 212 // FIXME new may block for unbounded time at internal mutex of the heap 213 // implementation; it would be better to have normal mixer allocate for us 214 // to avoid blocking here and to prevent possible priority inversion 215 mixer = new AudioMixer(frameCount, sampleRate, FastMixerState::kMaxFastTracks); 216 mixBuffer = new short[frameCount * 2]; 217 periodNs = (frameCount * 1000000000LL) / sampleRate; // 1.00 218 underrunNs = (frameCount * 1750000000LL) / sampleRate; // 1.75 219 overrunNs = (frameCount * 250000000LL) / sampleRate; // 0.25 220 warmupNs = (frameCount * 500000000LL) / sampleRate; // 0.50 221 } else { 222 periodNs = 0; 223 underrunNs = 0; 224 overrunNs = 0; 225 } 226 mixBufferState = UNDEFINED; 227#if !LOG_NDEBUG 228 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) { 229 fastTrackNames[i] = -1; 230 } 231#endif 232 // we need to reconfigure all active tracks 233 previousTrackMask = 0; 234 fastTracksGen = current->mFastTracksGen - 1; 235 dumpState->mFrameCount = frameCount; 236 } else { 237 previousTrackMask = previous->mTrackMask; 238 } 239 240 // check for change in active track set 241 unsigned currentTrackMask = current->mTrackMask; 242 dumpState->mTrackMask = currentTrackMask; 243 if (current->mFastTracksGen != fastTracksGen) { 244 ALOG_ASSERT(mixBuffer != NULL); 245 int name; 246 247 // process removed tracks first to avoid running out of track names 248 unsigned removedTracks = previousTrackMask & ~currentTrackMask; 249 while (removedTracks != 0) { 250 i = __builtin_ctz(removedTracks); 251 removedTracks &= ~(1 << i); 252 const FastTrack* fastTrack = ¤t->mFastTracks[i]; 253 ALOG_ASSERT(fastTrack->mBufferProvider == NULL); 254 if (mixer != NULL) { 255 name = fastTrackNames[i]; 256 ALOG_ASSERT(name >= 0); 257 mixer->deleteTrackName(name); 258 } 259#if !LOG_NDEBUG 260 fastTrackNames[i] = -1; 261#endif 262 // don't reset track dump state, since other side is ignoring it 263 generations[i] = fastTrack->mGeneration; 264 } 265 266 // now process added tracks 267 unsigned addedTracks = currentTrackMask & ~previousTrackMask; 268 while (addedTracks != 0) { 269 i = __builtin_ctz(addedTracks); 270 addedTracks &= ~(1 << i); 271 const FastTrack* fastTrack = ¤t->mFastTracks[i]; 272 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider; 273 ALOG_ASSERT(bufferProvider != NULL && fastTrackNames[i] == -1); 274 if (mixer != NULL) { 275 // calling getTrackName with default channel mask 276 name = mixer->getTrackName(AUDIO_CHANNEL_OUT_STEREO); 277 ALOG_ASSERT(name >= 0); 278 fastTrackNames[i] = name; 279 mixer->setBufferProvider(name, bufferProvider); 280 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER, 281 (void *) mixBuffer); 282 // newly allocated track names default to full scale volume 283 if (fastTrack->mSampleRate != 0 && fastTrack->mSampleRate != sampleRate) { 284 mixer->setParameter(name, AudioMixer::RESAMPLE, 285 AudioMixer::SAMPLE_RATE, (void*) fastTrack->mSampleRate); 286 } 287 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK, 288 (void *) fastTrack->mChannelMask); 289 mixer->enable(name); 290 } 291 generations[i] = fastTrack->mGeneration; 292 } 293 294 // finally process modified tracks; these use the same slot 295 // but may have a different buffer provider or volume provider 296 unsigned modifiedTracks = currentTrackMask & previousTrackMask; 297 while (modifiedTracks != 0) { 298 i = __builtin_ctz(modifiedTracks); 299 modifiedTracks &= ~(1 << i); 300 const FastTrack* fastTrack = ¤t->mFastTracks[i]; 301 if (fastTrack->mGeneration != generations[i]) { 302 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider; 303 ALOG_ASSERT(bufferProvider != NULL); 304 if (mixer != NULL) { 305 name = fastTrackNames[i]; 306 ALOG_ASSERT(name >= 0); 307 mixer->setBufferProvider(name, bufferProvider); 308 if (fastTrack->mVolumeProvider == NULL) { 309 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, 310 (void *)0x1000); 311 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, 312 (void *)0x1000); 313 } 314 if (fastTrack->mSampleRate != 0 && 315 fastTrack->mSampleRate != sampleRate) { 316 mixer->setParameter(name, AudioMixer::RESAMPLE, 317 AudioMixer::SAMPLE_RATE, (void*) fastTrack->mSampleRate); 318 } else { 319 mixer->setParameter(name, AudioMixer::RESAMPLE, 320 AudioMixer::REMOVE, NULL); 321 } 322 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK, 323 (void *) fastTrack->mChannelMask); 324 // already enabled 325 } 326 generations[i] = fastTrack->mGeneration; 327 } 328 } 329 330 fastTracksGen = current->mFastTracksGen; 331 332 dumpState->mNumTracks = popcount(currentTrackMask); 333 } 334 335#if 1 // FIXME shouldn't need this 336 // only process state change once 337 previous = current; 338#endif 339 } 340 341 // do work using current state here 342 if ((command & FastMixerState::MIX) && (mixer != NULL) && isWarm) { 343 ALOG_ASSERT(mixBuffer != NULL); 344 // for each track, update volume and check for underrun 345 unsigned currentTrackMask = current->mTrackMask; 346 while (currentTrackMask != 0) { 347 i = __builtin_ctz(currentTrackMask); 348 currentTrackMask &= ~(1 << i); 349 const FastTrack* fastTrack = ¤t->mFastTracks[i]; 350 int name = fastTrackNames[i]; 351 ALOG_ASSERT(name >= 0); 352 if (fastTrack->mVolumeProvider != NULL) { 353 uint32_t vlr = fastTrack->mVolumeProvider->getVolumeLR(); 354 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, 355 (void *)(vlr & 0xFFFF)); 356 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, 357 (void *)(vlr >> 16)); 358 } 359 // FIXME The current implementation of framesReady() for fast tracks 360 // takes a tryLock, which can block 361 // up to 1 ms. If enough active tracks all blocked in sequence, this would result 362 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO. 363 size_t framesReady = fastTrack->mBufferProvider->framesReady(); 364#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER) 365 // I wish we had formatted trace names 366 char traceName[16]; 367 strcpy(traceName, "framesReady"); 368 traceName[11] = i + (i < 10 ? '0' : 'A' - 10); 369 traceName[12] = '\0'; 370 ATRACE_INT(traceName, framesReady); 371#endif 372 FastTrackDump *ftDump = &dumpState->mTracks[i]; 373 FastTrackUnderruns underruns = ftDump->mUnderruns; 374 if (framesReady < frameCount) { 375 if (framesReady == 0) { 376 underruns.mBitFields.mEmpty++; 377 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY; 378 mixer->disable(name); 379 } else { 380 // allow mixing partial buffer 381 underruns.mBitFields.mPartial++; 382 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL; 383 mixer->enable(name); 384 } 385 } else { 386 underruns.mBitFields.mFull++; 387 underruns.mBitFields.mMostRecent = UNDERRUN_FULL; 388 mixer->enable(name); 389 } 390 ftDump->mUnderruns = underruns; 391 ftDump->mFramesReady = framesReady; 392 } 393 // process() is CPU-bound 394 mixer->process(AudioBufferProvider::kInvalidPTS); 395 mixBufferState = MIXED; 396 } else if (mixBufferState == MIXED) { 397 mixBufferState = UNDEFINED; 398 } 399 bool attemptedWrite = false; 400 //bool didFullWrite = false; // dumpsys could display a count of partial writes 401 if ((command & FastMixerState::WRITE) && (outputSink != NULL) && (mixBuffer != NULL)) { 402 if (mixBufferState == UNDEFINED) { 403 memset(mixBuffer, 0, frameCount * 2 * sizeof(short)); 404 mixBufferState = ZEROED; 405 } 406 if (teeSink != NULL) { 407 (void) teeSink->write(mixBuffer, frameCount); 408 } 409 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink, 410 // but this code should be modified to handle both non-blocking and blocking sinks 411 dumpState->mWriteSequence++; 412#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER) 413 Tracer::traceBegin(ATRACE_TAG, "write"); 414#endif 415 ssize_t framesWritten = outputSink->write(mixBuffer, frameCount); 416#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER) 417 Tracer::traceEnd(ATRACE_TAG); 418#endif 419 dumpState->mWriteSequence++; 420 if (framesWritten >= 0) { 421 ALOG_ASSERT(framesWritten <= frameCount); 422 dumpState->mFramesWritten += framesWritten; 423 //if ((size_t) framesWritten == frameCount) { 424 // didFullWrite = true; 425 //} 426 } else { 427 dumpState->mWriteErrors++; 428 } 429 attemptedWrite = true; 430 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump 431 } 432 433 // To be exactly periodic, compute the next sleep time based on current time. 434 // This code doesn't have long-term stability when the sink is non-blocking. 435 // FIXME To avoid drift, use the local audio clock or watch the sink's fill status. 436 struct timespec newTs; 437 int rc = clock_gettime(CLOCK_MONOTONIC, &newTs); 438 if (rc == 0) { 439 if (oldTsValid) { 440 time_t sec = newTs.tv_sec - oldTs.tv_sec; 441 long nsec = newTs.tv_nsec - oldTs.tv_nsec; 442 if (nsec < 0) { 443 --sec; 444 nsec += 1000000000; 445 } 446 // To avoid an initial underrun on fast tracks after exiting standby, 447 // do not start pulling data from tracks and mixing until warmup is complete. 448 // Warmup is considered complete after the earlier of: 449 // first successful single write() that blocks for more than warmupNs 450 // MAX_WARMUP_CYCLES write() attempts. 451 // This is overly conservative, but to get better accuracy requires a new HAL API. 452 if (!isWarm && attemptedWrite) { 453 measuredWarmupTs.tv_sec += sec; 454 measuredWarmupTs.tv_nsec += nsec; 455 if (measuredWarmupTs.tv_nsec >= 1000000000) { 456 measuredWarmupTs.tv_sec++; 457 measuredWarmupTs.tv_nsec -= 1000000000; 458 } 459 ++warmupCycles; 460 if ((attemptedWrite && nsec > warmupNs) || 461 (warmupCycles >= MAX_WARMUP_CYCLES)) { 462 isWarm = true; 463 dumpState->mMeasuredWarmupTs = measuredWarmupTs; 464 dumpState->mWarmupCycles = warmupCycles; 465 } 466 } 467 if (sec > 0 || nsec > underrunNs) { 468#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER) 469 ScopedTrace st(ATRACE_TAG, "underrun"); 470#endif 471 // FIXME only log occasionally 472 ALOGV("underrun: time since last cycle %d.%03ld sec", 473 (int) sec, nsec / 1000000L); 474 dumpState->mUnderruns++; 475 sleepNs = -1; 476 ignoreNextOverrun = true; 477 } else if (nsec < overrunNs) { 478 if (ignoreNextOverrun) { 479 ignoreNextOverrun = false; 480 } else { 481 // FIXME only log occasionally 482 ALOGV("overrun: time since last cycle %d.%03ld sec", 483 (int) sec, nsec / 1000000L); 484 dumpState->mOverruns++; 485 } 486 sleepNs = periodNs - overrunNs; 487 } else { 488 sleepNs = -1; 489 ignoreNextOverrun = false; 490 } 491#ifdef FAST_MIXER_STATISTICS 492 // advance the FIFO queue bounds 493 size_t i = bounds & (FastMixerDumpState::kSamplingN - 1); 494 bounds = (bounds & 0xFFFF0000) | ((bounds + 1) & 0xFFFF); 495 if (full) { 496 bounds += 0x10000; 497 } else if (!(bounds & (FastMixerDumpState::kSamplingN - 1))) { 498 full = true; 499 } 500 // compute the delta value of clock_gettime(CLOCK_MONOTONIC) 501 uint32_t monotonicNs = nsec; 502 if (sec > 0 && sec < 4) { 503 monotonicNs += sec * 1000000000; 504 } 505 // compute the raw CPU load = delta value of clock_gettime(CLOCK_THREAD_CPUTIME_ID) 506 uint32_t loadNs = 0; 507 struct timespec newLoad; 508 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &newLoad); 509 if (rc == 0) { 510 if (oldLoadValid) { 511 sec = newLoad.tv_sec - oldLoad.tv_sec; 512 nsec = newLoad.tv_nsec - oldLoad.tv_nsec; 513 if (nsec < 0) { 514 --sec; 515 nsec += 1000000000; 516 } 517 loadNs = nsec; 518 if (sec > 0 && sec < 4) { 519 loadNs += sec * 1000000000; 520 } 521 } else { 522 // first time through the loop 523 oldLoadValid = true; 524 } 525 oldLoad = newLoad; 526 } 527 // get the absolute value of CPU clock frequency in kHz 528 int cpuNum = sched_getcpu(); 529 uint32_t kHz = tcu.getCpukHz(cpuNum); 530 kHz = (kHz << 4) | (cpuNum & 0xF); 531 // save values in FIFO queues for dumpsys 532 // these stores #1, #2, #3 are not atomic with respect to each other, 533 // or with respect to store #4 below 534 dumpState->mMonotonicNs[i] = monotonicNs; 535 dumpState->mLoadNs[i] = loadNs; 536 dumpState->mCpukHz[i] = kHz; 537 // this store #4 is not atomic with respect to stores #1, #2, #3 above, but 538 // the newest open and oldest closed halves are atomic with respect to each other 539 dumpState->mBounds = bounds; 540#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER) 541 ATRACE_INT("cycle_ms", monotonicNs / 1000000); 542 ATRACE_INT("load_us", loadNs / 1000); 543#endif 544#endif 545 } else { 546 // first time through the loop 547 oldTsValid = true; 548 sleepNs = periodNs; 549 ignoreNextOverrun = true; 550 } 551 oldTs = newTs; 552 } else { 553 // monotonic clock is broken 554 oldTsValid = false; 555 sleepNs = periodNs; 556 } 557 558 559 } // for (;;) 560 561 // never return 'true'; Thread::_threadLoop() locks mutex which can result in priority inversion 562} 563 564FastMixerDumpState::FastMixerDumpState() : 565 mCommand(FastMixerState::INITIAL), mWriteSequence(0), mFramesWritten(0), 566 mNumTracks(0), mWriteErrors(0), mUnderruns(0), mOverruns(0), 567 mSampleRate(0), mFrameCount(0), /* mMeasuredWarmupTs({0, 0}), */ mWarmupCycles(0), 568 mTrackMask(0) 569#ifdef FAST_MIXER_STATISTICS 570 , mBounds(0) 571#endif 572{ 573 mMeasuredWarmupTs.tv_sec = 0; 574 mMeasuredWarmupTs.tv_nsec = 0; 575 // sample arrays aren't accessed atomically with respect to the bounds, 576 // so clearing reduces chance for dumpsys to read random uninitialized samples 577 memset(&mMonotonicNs, 0, sizeof(mMonotonicNs)); 578 memset(&mLoadNs, 0, sizeof(mLoadNs)); 579 memset(&mCpukHz, 0, sizeof(mCpukHz)); 580} 581 582FastMixerDumpState::~FastMixerDumpState() 583{ 584} 585 586void FastMixerDumpState::dump(int fd) 587{ 588#define COMMAND_MAX 32 589 char string[COMMAND_MAX]; 590 switch (mCommand) { 591 case FastMixerState::INITIAL: 592 strcpy(string, "INITIAL"); 593 break; 594 case FastMixerState::HOT_IDLE: 595 strcpy(string, "HOT_IDLE"); 596 break; 597 case FastMixerState::COLD_IDLE: 598 strcpy(string, "COLD_IDLE"); 599 break; 600 case FastMixerState::EXIT: 601 strcpy(string, "EXIT"); 602 break; 603 case FastMixerState::MIX: 604 strcpy(string, "MIX"); 605 break; 606 case FastMixerState::WRITE: 607 strcpy(string, "WRITE"); 608 break; 609 case FastMixerState::MIX_WRITE: 610 strcpy(string, "MIX_WRITE"); 611 break; 612 default: 613 snprintf(string, COMMAND_MAX, "%d", mCommand); 614 break; 615 } 616 double measuredWarmupMs = (mMeasuredWarmupTs.tv_sec * 1000.0) + 617 (mMeasuredWarmupTs.tv_nsec / 1000000.0); 618 double mixPeriodSec = (double) mFrameCount / (double) mSampleRate; 619 fdprintf(fd, "FastMixer command=%s writeSequence=%u framesWritten=%u\n" 620 " numTracks=%u writeErrors=%u underruns=%u overruns=%u\n" 621 " sampleRate=%u frameCount=%u measuredWarmup=%.3g ms, warmupCycles=%u\n" 622 " mixPeriod=%.2f ms\n", 623 string, mWriteSequence, mFramesWritten, 624 mNumTracks, mWriteErrors, mUnderruns, mOverruns, 625 mSampleRate, mFrameCount, measuredWarmupMs, mWarmupCycles, 626 mixPeriodSec * 1e3); 627#ifdef FAST_MIXER_STATISTICS 628 // find the interval of valid samples 629 uint32_t bounds = mBounds; 630 uint32_t newestOpen = bounds & 0xFFFF; 631 uint32_t oldestClosed = bounds >> 16; 632 uint32_t n = (newestOpen - oldestClosed) & 0xFFFF; 633 if (n > kSamplingN) { 634 ALOGE("too many samples %u", n); 635 n = kSamplingN; 636 } 637 // statistics for monotonic (wall clock) time, thread raw CPU load in time, CPU clock frequency, 638 // and adjusted CPU load in MHz normalized for CPU clock frequency 639 CentralTendencyStatistics wall, loadNs, kHz, loadMHz; 640 // only compute adjusted CPU load in Hz if current CPU number and CPU clock frequency are stable 641 bool valid = false; 642 uint32_t previousCpukHz = 0; 643 // loop over all the samples 644 for (; n > 0; --n) { 645 size_t i = oldestClosed++ & (kSamplingN - 1); 646 uint32_t wallNs = mMonotonicNs[i]; 647 wall.sample(wallNs); 648 uint32_t sampleLoadNs = mLoadNs[i]; 649 uint32_t sampleCpukHz = mCpukHz[i]; 650 loadNs.sample(sampleLoadNs); 651 // skip bad kHz samples 652 if ((sampleCpukHz & ~0xF) != 0) { 653 kHz.sample(sampleCpukHz >> 4); 654 if (sampleCpukHz == previousCpukHz) { 655 double megacycles = (double) sampleLoadNs * (double) (sampleCpukHz >> 4) * 1e-12; 656 double adjMHz = megacycles / mixPeriodSec; // _not_ wallNs * 1e9 657 loadMHz.sample(adjMHz); 658 } 659 } 660 previousCpukHz = sampleCpukHz; 661 } 662 fdprintf(fd, "Simple moving statistics over last %.1f seconds:\n", wall.n() * mixPeriodSec); 663 fdprintf(fd, " wall clock time in ms per mix cycle:\n" 664 " mean=%.2f min=%.2f max=%.2f stddev=%.2f\n", 665 wall.mean()*1e-6, wall.minimum()*1e-6, wall.maximum()*1e-6, wall.stddev()*1e-6); 666 fdprintf(fd, " raw CPU load in us per mix cycle:\n" 667 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n", 668 loadNs.mean()*1e-3, loadNs.minimum()*1e-3, loadNs.maximum()*1e-3, 669 loadNs.stddev()*1e-3); 670 fdprintf(fd, " CPU clock frequency in MHz:\n" 671 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n", 672 kHz.mean()*1e-3, kHz.minimum()*1e-3, kHz.maximum()*1e-3, kHz.stddev()*1e-3); 673 fdprintf(fd, " adjusted CPU load in MHz (i.e. normalized for CPU clock frequency):\n" 674 " mean=%.1f min=%.1f max=%.1f stddev=%.1f\n", 675 loadMHz.mean(), loadMHz.minimum(), loadMHz.maximum(), loadMHz.stddev()); 676#endif 677 // The active track mask and track states are updated non-atomically. 678 // So if we relied on isActive to decide whether to display, 679 // then we might display an obsolete track or omit an active track. 680 // Instead we always display all tracks, with an indication 681 // of whether we think the track is active. 682 uint32_t trackMask = mTrackMask; 683 fdprintf(fd, "Fast tracks: kMaxFastTracks=%u activeMask=%#x\n", 684 FastMixerState::kMaxFastTracks, trackMask); 685 fdprintf(fd, "Index Active Full Partial Empty Recent Ready\n"); 686 for (uint32_t i = 0; i < FastMixerState::kMaxFastTracks; ++i, trackMask >>= 1) { 687 bool isActive = trackMask & 1; 688 const FastTrackDump *ftDump = &mTracks[i]; 689 const FastTrackUnderruns& underruns = ftDump->mUnderruns; 690 const char *mostRecent; 691 switch (underruns.mBitFields.mMostRecent) { 692 case UNDERRUN_FULL: 693 mostRecent = "full"; 694 break; 695 case UNDERRUN_PARTIAL: 696 mostRecent = "partial"; 697 break; 698 case UNDERRUN_EMPTY: 699 mostRecent = "empty"; 700 break; 701 default: 702 mostRecent = "?"; 703 break; 704 } 705 fdprintf(fd, "%5u %6s %4u %7u %5u %7s %5u\n", i, isActive ? "yes" : "no", 706 (underruns.mBitFields.mFull) & UNDERRUN_MASK, 707 (underruns.mBitFields.mPartial) & UNDERRUN_MASK, 708 (underruns.mBitFields.mEmpty) & UNDERRUN_MASK, 709 mostRecent, ftDump->mFramesReady); 710 } 711} 712 713} // namespace android 714