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