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