FastThread.cpp revision 8255ba7b40774b2b8868482f1a302db3ae98e905
12234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten/*
22234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten * Copyright (C) 2014 The Android Open Source Project
32234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten *
42234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten * Licensed under the Apache License, Version 2.0 (the "License");
52234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten * you may not use this file except in compliance with the License.
62234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten * You may obtain a copy of the License at
72234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten *
82234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten *      http://www.apache.org/licenses/LICENSE-2.0
92234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten *
102234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten * Unless required by applicable law or agreed to in writing, software
112234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten * distributed under the License is distributed on an "AS IS" BASIS,
122234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten * See the License for the specific language governing permissions and
142234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten * limitations under the License.
152234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten */
162234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
172234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#define LOG_TAG "FastThread"
182234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten//#define LOG_NDEBUG 0
192234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
202234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#define ATRACE_TAG ATRACE_TAG_AUDIO
212234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
222234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#include "Configuration.h"
23e348c5b72ad889389c7c1c900c121f0fbee221b5Elliott Hughes#include <linux/futex.h>
24e348c5b72ad889389c7c1c900c121f0fbee221b5Elliott Hughes#include <sys/syscall.h>
252234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#include <utils/Log.h>
262234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#include <utils/Trace.h>
272234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#include "FastThread.h"
28045ee7ea3e42b8657869d81c917feed5fb0c2b4aGlenn Kasten#include "FastThreadDumpState.h"
292234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
302234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#define FAST_DEFAULT_NS    999999999L   // ~1 sec: default time to sleep
312234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#define FAST_HOT_IDLE_NS     1000000L   // 1 ms: time to sleep while hot idling
32d2123e631834a887c2d8600c3ac43dda02d47cb9Glenn Kasten#define MIN_WARMUP_CYCLES          2    // minimum number of consecutive in-range loop cycles
33d2123e631834a887c2d8600c3ac43dda02d47cb9Glenn Kasten                                        // to wait for warmup
342234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#define MAX_WARMUP_CYCLES         10    // maximum number of loop cycles to wait for warmup
352234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
362234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kastennamespace android {
372234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
38f9715e43ea73361321663514c44129c939c5db2fGlenn KastenFastThread::FastThread(const char *cycleMs, const char *loadUs) : Thread(false /*canCallJava*/),
39e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    // re-initialized to &sInitial by subclass constructor
40e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mPrevious(NULL), mCurrent(NULL),
41e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    /* mOldTs({0, 0}), */
42e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mOldTsValid(false),
43e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mSleepNs(-1),
44e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mPeriodNs(0),
45e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mUnderrunNs(0),
46e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mOverrunNs(0),
47e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mForceNs(0),
48e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mWarmupNsMin(0),
49e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mWarmupNsMax(LONG_MAX),
50e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    // re-initialized to &mDummySubclassDumpState by subclass constructor
512234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten    mDummyDumpState(NULL),
52e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mDumpState(NULL),
53e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mIgnoreNextOverrun(true),
54214b406c813e5baca3e4b5cdc1d986de35f09bbbGlenn Kasten#ifdef FAST_THREAD_STATISTICS
55e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    // mOldLoad
56e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mOldLoadValid(false),
57e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mBounds(0),
58e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mFull(false),
59e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    // mTcu
602234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#endif
61e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mColdGen(0),
62e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mIsWarm(false),
63e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    /* mMeasuredWarmupTs({0, 0}), */
64e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mWarmupCycles(0),
65e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mWarmupConsecutiveInRangeCycles(0),
66e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    // mDummyLogWriter
67e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mLogWriter(&mDummyLogWriter),
68e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mTimestampStatus(INVALID_OPERATION),
692234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
70e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mCommand(FastThreadState::INITIAL),
712234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#if 0
722234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten    frameCount(0),
732234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#endif
74e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mAttemptedWrite(false)
75f9715e43ea73361321663514c44129c939c5db2fGlenn Kasten    // mCycleMs(cycleMs)
76f9715e43ea73361321663514c44129c939c5db2fGlenn Kasten    // mLoadUs(loadUs)
772234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten{
78e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mOldTs.tv_sec = 0;
79e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mOldTs.tv_nsec = 0;
80e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mMeasuredWarmupTs.tv_sec = 0;
81e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten    mMeasuredWarmupTs.tv_nsec = 0;
82f9715e43ea73361321663514c44129c939c5db2fGlenn Kasten    strlcpy(mCycleMs, cycleMs, sizeof(mCycleMs));
83f9715e43ea73361321663514c44129c939c5db2fGlenn Kasten    strlcpy(mLoadUs, loadUs, sizeof(mLoadUs));
842234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten}
852234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
862234002b0710c8db73f82d397cb945cd541c6bbbGlenn KastenFastThread::~FastThread()
872234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten{
882234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten}
892234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
902234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kastenbool FastThread::threadLoop()
912234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten{
922234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten    for (;;) {
932234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
942234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        // either nanosleep, sched_yield, or busy wait
95e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten        if (mSleepNs >= 0) {
96e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            if (mSleepNs > 0) {
97e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                ALOG_ASSERT(mSleepNs < 1000000000);
98e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                const struct timespec req = {0, mSleepNs};
992234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                nanosleep(&req, NULL);
1002234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            } else {
1012234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                sched_yield();
1022234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            }
1032234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        }
1042234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        // default to long sleep for next cycle
105e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten        mSleepNs = FAST_DEFAULT_NS;
1062234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
1072234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        // poll for state change
1082234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        const FastThreadState *next = poll();
1092234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        if (next == NULL) {
1102234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            // continue to use the default initial state until a real state is available
111e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            // FIXME &sInitial not available, should save address earlier
112e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            //ALOG_ASSERT(mCurrent == &sInitial && previous == &sInitial);
113e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            next = mCurrent;
1142234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        }
1152234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
116e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten        mCommand = next->mCommand;
117e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten        if (next != mCurrent) {
1182234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
1192234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            // As soon as possible of learning of a new dump area, start using it
120e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            mDumpState = next->mDumpState != NULL ? next->mDumpState : mDummyDumpState;
121e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            mLogWriter = next->mNBLogWriter != NULL ? next->mNBLogWriter : &mDummyLogWriter;
122e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            setLog(mLogWriter);
1232234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
1242234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            // We want to always have a valid reference to the previous (non-idle) state.
1252234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            // However, the state queue only guarantees access to current and previous states.
1262234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            // So when there is a transition from a non-idle state into an idle state, we make a
1272234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            // copy of the last known non-idle state so it is still available on return from idle.
1282234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            // The possible transitions are:
1292234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            //  non-idle -> non-idle    update previous from current in-place
1302234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            //  non-idle -> idle        update previous from copy of current
1312234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            //  idle     -> idle        don't update previous
1322234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            //  idle     -> non-idle    don't update previous
133e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            if (!(mCurrent->mCommand & FastThreadState::IDLE)) {
134e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                if (mCommand & FastThreadState::IDLE) {
1352234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    onIdle();
136e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    mOldTsValid = false;
137214b406c813e5baca3e4b5cdc1d986de35f09bbbGlenn Kasten#ifdef FAST_THREAD_STATISTICS
138e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    mOldLoadValid = false;
1392234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#endif
140e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    mIgnoreNextOverrun = true;
1412234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                }
142e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mPrevious = mCurrent;
1432234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            }
144e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            mCurrent = next;
1452234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        }
1462234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#if !LOG_NDEBUG
1472234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        next = NULL;    // not referenced again
1482234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#endif
1492234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
150e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten        mDumpState->mCommand = mCommand;
1512234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
152e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten        // FIXME what does this comment mean?
1532234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        // << current, previous, command, dumpState >>
1542234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
155e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten        switch (mCommand) {
1562234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        case FastThreadState::INITIAL:
1572234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        case FastThreadState::HOT_IDLE:
158e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            mSleepNs = FAST_HOT_IDLE_NS;
1592234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            continue;
1602234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        case FastThreadState::COLD_IDLE:
1612234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            // only perform a cold idle command once
1622234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            // FIXME consider checking previous state and only perform if previous != COLD_IDLE
163e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            if (mCurrent->mColdGen != mColdGen) {
164e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                int32_t *coldFutexAddr = mCurrent->mColdFutexAddr;
1652234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                ALOG_ASSERT(coldFutexAddr != NULL);
1662234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                int32_t old = android_atomic_dec(coldFutexAddr);
1672234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                if (old <= 0) {
168e348c5b72ad889389c7c1c900c121f0fbee221b5Elliott Hughes                    syscall(__NR_futex, coldFutexAddr, FUTEX_WAIT_PRIVATE, old - 1, NULL);
1692234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                }
1708255ba7b40774b2b8868482f1a302db3ae98e905Glenn Kasten                int policy = sched_getscheduler(0) & ~SCHED_RESET_ON_FORK;
1712234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                if (!(policy == SCHED_FIFO || policy == SCHED_RR)) {
1722234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    ALOGE("did not receive expected priority boost");
1732234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                }
1742234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                // This may be overly conservative; there could be times that the normal mixer
1752234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                // requests such a brief cold idle that it doesn't require resetting this flag.
176e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mIsWarm = false;
177e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mMeasuredWarmupTs.tv_sec = 0;
178e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mMeasuredWarmupTs.tv_nsec = 0;
179e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mWarmupCycles = 0;
180e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mWarmupConsecutiveInRangeCycles = 0;
181e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mSleepNs = -1;
182e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mColdGen = mCurrent->mColdGen;
183214b406c813e5baca3e4b5cdc1d986de35f09bbbGlenn Kasten#ifdef FAST_THREAD_STATISTICS
184e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mBounds = 0;
185e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mFull = false;
1862234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#endif
187e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mOldTsValid = !clock_gettime(CLOCK_MONOTONIC, &mOldTs);
188e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mTimestampStatus = INVALID_OPERATION;
1892234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            } else {
190e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mSleepNs = FAST_HOT_IDLE_NS;
1912234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            }
1922234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            continue;
1932234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        case FastThreadState::EXIT:
1942234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            onExit();
1952234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            return false;
1962234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        default:
197e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            LOG_ALWAYS_FATAL_IF(!isSubClassCommand(mCommand));
1982234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            break;
1992234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        }
2002234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
2012234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        // there is a non-idle state available to us; did the state change?
202e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten        if (mCurrent != mPrevious) {
2032234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            onStateChange();
2042234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#if 1   // FIXME shouldn't need this
2052234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            // only process state change once
206e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            mPrevious = mCurrent;
2072234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#endif
2082234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        }
2092234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
2102234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        // do work using current state here
211e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten        mAttemptedWrite = false;
2122234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        onWork();
2132234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
2142234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        // To be exactly periodic, compute the next sleep time based on current time.
2152234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        // This code doesn't have long-term stability when the sink is non-blocking.
2162234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        // FIXME To avoid drift, use the local audio clock or watch the sink's fill status.
2172234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        struct timespec newTs;
2182234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        int rc = clock_gettime(CLOCK_MONOTONIC, &newTs);
2192234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        if (rc == 0) {
220e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            //mLogWriter->logTimestamp(newTs);
221e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            if (mOldTsValid) {
222e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                time_t sec = newTs.tv_sec - mOldTs.tv_sec;
223e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                long nsec = newTs.tv_nsec - mOldTs.tv_nsec;
2242234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                ALOGE_IF(sec < 0 || (sec == 0 && nsec < 0),
2252234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        "clock_gettime(CLOCK_MONOTONIC) failed: was %ld.%09ld but now %ld.%09ld",
226e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mOldTs.tv_sec, mOldTs.tv_nsec, newTs.tv_sec, newTs.tv_nsec);
2272234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                if (nsec < 0) {
2282234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    --sec;
2292234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    nsec += 1000000000;
2302234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                }
2312234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                // To avoid an initial underrun on fast tracks after exiting standby,
2322234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                // do not start pulling data from tracks and mixing until warmup is complete.
2332234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                // Warmup is considered complete after the earlier of:
234d2123e631834a887c2d8600c3ac43dda02d47cb9Glenn Kasten                //      MIN_WARMUP_CYCLES consecutive in-range write() attempts,
235e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                //          where "in-range" means mWarmupNsMin <= cycle time <= mWarmupNsMax
2362234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                //      MAX_WARMUP_CYCLES write() attempts.
2372234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                // This is overly conservative, but to get better accuracy requires a new HAL API.
238e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                if (!mIsWarm && mAttemptedWrite) {
239e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    mMeasuredWarmupTs.tv_sec += sec;
240e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    mMeasuredWarmupTs.tv_nsec += nsec;
241e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    if (mMeasuredWarmupTs.tv_nsec >= 1000000000) {
242e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mMeasuredWarmupTs.tv_sec++;
243e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mMeasuredWarmupTs.tv_nsec -= 1000000000;
2442234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    }
245e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    ++mWarmupCycles;
246e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    if (mWarmupNsMin <= nsec && nsec <= mWarmupNsMax) {
247e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        ALOGV("warmup cycle %d in range: %.03f ms", mWarmupCycles, nsec * 1e-9);
248e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        ++mWarmupConsecutiveInRangeCycles;
249d2123e631834a887c2d8600c3ac43dda02d47cb9Glenn Kasten                    } else {
250e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        ALOGV("warmup cycle %d out of range: %.03f ms", mWarmupCycles, nsec * 1e-9);
251e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mWarmupConsecutiveInRangeCycles = 0;
252d2123e631834a887c2d8600c3ac43dda02d47cb9Glenn Kasten                    }
253e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    if ((mWarmupConsecutiveInRangeCycles >= MIN_WARMUP_CYCLES) ||
254e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                            (mWarmupCycles >= MAX_WARMUP_CYCLES)) {
255e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mIsWarm = true;
256e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mDumpState->mMeasuredWarmupTs = mMeasuredWarmupTs;
257e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mDumpState->mWarmupCycles = mWarmupCycles;
2582234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    }
2592234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                }
260e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mSleepNs = -1;
261e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                if (mIsWarm) {
262e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    if (sec > 0 || nsec > mUnderrunNs) {
2632234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        ATRACE_NAME("underrun");
2642234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        // FIXME only log occasionally
2652234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        ALOGV("underrun: time since last cycle %d.%03ld sec",
2662234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                                (int) sec, nsec / 1000000L);
267e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mDumpState->mUnderruns++;
268e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mIgnoreNextOverrun = true;
269e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    } else if (nsec < mOverrunNs) {
270e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        if (mIgnoreNextOverrun) {
271e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                            mIgnoreNextOverrun = false;
2722234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        } else {
2732234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                            // FIXME only log occasionally
2742234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                            ALOGV("overrun: time since last cycle %d.%03ld sec",
2752234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                                    (int) sec, nsec / 1000000L);
276e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                            mDumpState->mOverruns++;
2772234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        }
2782234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        // This forces a minimum cycle time. It:
2792234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        //  - compensates for an audio HAL with jitter due to sample rate conversion
2802234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        //  - works with a variable buffer depth audio HAL that never pulls at a
281e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        //    rate < than mOverrunNs per buffer.
2822234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        //  - recovers from overrun immediately after underrun
2832234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        // It doesn't work with a non-blocking audio HAL.
284e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mSleepNs = mForceNs - nsec;
2852234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    } else {
286e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mIgnoreNextOverrun = false;
2872234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    }
2882234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                }
289214b406c813e5baca3e4b5cdc1d986de35f09bbbGlenn Kasten#ifdef FAST_THREAD_STATISTICS
290e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                if (mIsWarm) {
2912234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    // advance the FIFO queue bounds
292e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    size_t i = mBounds & (mDumpState->mSamplingN - 1);
293e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    mBounds = (mBounds & 0xFFFF0000) | ((mBounds + 1) & 0xFFFF);
294e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    if (mFull) {
295e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mBounds += 0x10000;
296e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    } else if (!(mBounds & (mDumpState->mSamplingN - 1))) {
297e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mFull = true;
2982234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    }
2992234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    // compute the delta value of clock_gettime(CLOCK_MONOTONIC)
3002234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    uint32_t monotonicNs = nsec;
3012234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    if (sec > 0 && sec < 4) {
3022234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        monotonicNs += sec * 1000000000;
3032234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    }
3042234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    // compute raw CPU load = delta value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
3052234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    uint32_t loadNs = 0;
3062234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    struct timespec newLoad;
3072234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &newLoad);
3082234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    if (rc == 0) {
309e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        if (mOldLoadValid) {
310e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                            sec = newLoad.tv_sec - mOldLoad.tv_sec;
311e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                            nsec = newLoad.tv_nsec - mOldLoad.tv_nsec;
3122234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                            if (nsec < 0) {
3132234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                                --sec;
3142234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                                nsec += 1000000000;
3152234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                            }
3162234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                            loadNs = nsec;
3172234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                            if (sec > 0 && sec < 4) {
3182234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                                loadNs += sec * 1000000000;
3192234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                            }
3202234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        } else {
3212234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                            // first time through the loop
322e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                            mOldLoadValid = true;
3232234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                        }
324e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                        mOldLoad = newLoad;
3252234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    }
3262234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#ifdef CPU_FREQUENCY_STATISTICS
3272234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    // get the absolute value of CPU clock frequency in kHz
3282234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    int cpuNum = sched_getcpu();
329e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    uint32_t kHz = mTcu.getCpukHz(cpuNum);
3302234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    kHz = (kHz << 4) | (cpuNum & 0xF);
3312234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#endif
3322234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    // save values in FIFO queues for dumpsys
3332234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    // these stores #1, #2, #3 are not atomic with respect to each other,
3342234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    // or with respect to store #4 below
335e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    mDumpState->mMonotonicNs[i] = monotonicNs;
336e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    mDumpState->mLoadNs[i] = loadNs;
3372234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#ifdef CPU_FREQUENCY_STATISTICS
338e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    mDumpState->mCpukHz[i] = kHz;
3392234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#endif
3402234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    // this store #4 is not atomic with respect to stores #1, #2, #3 above, but
3412234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                    // the newest open & oldest closed halves are atomic with respect to each other
342e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                    mDumpState->mBounds = mBounds;
343f9715e43ea73361321663514c44129c939c5db2fGlenn Kasten                    ATRACE_INT(mCycleMs, monotonicNs / 1000000);
344f9715e43ea73361321663514c44129c939c5db2fGlenn Kasten                    ATRACE_INT(mLoadUs, loadNs / 1000);
3452234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                }
3462234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten#endif
3472234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            } else {
3482234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten                // first time through the loop
349e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mOldTsValid = true;
350e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mSleepNs = mPeriodNs;
351e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten                mIgnoreNextOverrun = true;
3522234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            }
353e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            mOldTs = newTs;
3542234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        } else {
3552234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten            // monotonic clock is broken
356e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            mOldTsValid = false;
357e4a7ce250cb94a00aa2f76e5edca1c4479dc5401Glenn Kasten            mSleepNs = mPeriodNs;
3582234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten        }
3592234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
3602234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten    }   // for (;;)
3612234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
3622234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten    // never return 'true'; Thread::_threadLoop() locks mutex which can result in priority inversion
3632234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten}
3642234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten
3652234002b0710c8db73f82d397cb945cd541c6bbbGlenn Kasten}   // namespace android
366