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