blob: 01b0432a334c73a16562f24e3dd80027e83fb94e [file] [log] [blame]
Glenn Kasten22340022014-04-07 12:04:41 -07001/*
2 * Copyright (C) 2014 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 "FastThread"
18//#define LOG_NDEBUG 0
19
20#define ATRACE_TAG ATRACE_TAG_AUDIO
21
22#include "Configuration.h"
Elliott Hughese348c5b2014-05-21 18:47:50 -070023#include <linux/futex.h>
24#include <sys/syscall.h>
Eric Tanfefe3162018-09-07 10:09:11 -070025#include <audio_utils/clock.h>
Mathias Agopian05d19b02017-02-28 16:28:19 -080026#include <cutils/atomic.h>
Glenn Kasten22340022014-04-07 12:04:41 -070027#include <utils/Log.h>
Glenn Kasten22340022014-04-07 12:04:41 -070028#include <utils/Trace.h>
29#include "FastThread.h"
Glenn Kasten045ee7e2015-02-17 16:22:04 -080030#include "FastThreadDumpState.h"
Glenn Kasten3ab8d662017-04-03 14:35:09 -070031#include "TypedLogger.h"
Glenn Kasten22340022014-04-07 12:04:41 -070032
33#define FAST_DEFAULT_NS 999999999L // ~1 sec: default time to sleep
34#define FAST_HOT_IDLE_NS 1000000L // 1 ms: time to sleep while hot idling
Glenn Kastend2123e62015-01-29 10:02:44 -080035#define MIN_WARMUP_CYCLES 2 // minimum number of consecutive in-range loop cycles
36 // to wait for warmup
Glenn Kasten22340022014-04-07 12:04:41 -070037#define MAX_WARMUP_CYCLES 10 // maximum number of loop cycles to wait for warmup
38
39namespace android {
40
Glenn Kastenf9715e42016-07-13 14:02:03 -070041FastThread::FastThread(const char *cycleMs, const char *loadUs) : Thread(false /*canCallJava*/),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080042 // re-initialized to &sInitial by subclass constructor
43 mPrevious(NULL), mCurrent(NULL),
44 /* mOldTs({0, 0}), */
45 mOldTsValid(false),
46 mSleepNs(-1),
47 mPeriodNs(0),
48 mUnderrunNs(0),
49 mOverrunNs(0),
50 mForceNs(0),
51 mWarmupNsMin(0),
52 mWarmupNsMax(LONG_MAX),
53 // re-initialized to &mDummySubclassDumpState by subclass constructor
Glenn Kasten22340022014-04-07 12:04:41 -070054 mDummyDumpState(NULL),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080055 mDumpState(NULL),
56 mIgnoreNextOverrun(true),
Glenn Kasten214b4062015-03-02 14:15:47 -080057#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -080058 // mOldLoad
59 mOldLoadValid(false),
60 mBounds(0),
61 mFull(false),
62 // mTcu
Glenn Kasten22340022014-04-07 12:04:41 -070063#endif
Glenn Kastene4a7ce22015-03-03 11:23:17 -080064 mColdGen(0),
65 mIsWarm(false),
66 /* mMeasuredWarmupTs({0, 0}), */
67 mWarmupCycles(0),
68 mWarmupConsecutiveInRangeCycles(0),
Mikhail Naganov01d09d92018-09-18 12:38:57 -070069 mDummyNBLogWriter(new NBLog::Writer()),
70 mNBLogWriter(mDummyNBLogWriter.get()),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080071 mTimestampStatus(INVALID_OPERATION),
Glenn Kasten22340022014-04-07 12:04:41 -070072
Glenn Kastene4a7ce22015-03-03 11:23:17 -080073 mCommand(FastThreadState::INITIAL),
Glenn Kasten22340022014-04-07 12:04:41 -070074#if 0
75 frameCount(0),
76#endif
Glenn Kastene4a7ce22015-03-03 11:23:17 -080077 mAttemptedWrite(false)
Glenn Kastenf9715e42016-07-13 14:02:03 -070078 // mCycleMs(cycleMs)
79 // mLoadUs(loadUs)
Glenn Kasten22340022014-04-07 12:04:41 -070080{
Glenn Kastene4a7ce22015-03-03 11:23:17 -080081 mOldTs.tv_sec = 0;
82 mOldTs.tv_nsec = 0;
83 mMeasuredWarmupTs.tv_sec = 0;
84 mMeasuredWarmupTs.tv_nsec = 0;
Glenn Kastenf9715e42016-07-13 14:02:03 -070085 strlcpy(mCycleMs, cycleMs, sizeof(mCycleMs));
86 strlcpy(mLoadUs, loadUs, sizeof(mLoadUs));
Glenn Kasten22340022014-04-07 12:04:41 -070087}
88
89FastThread::~FastThread()
90{
91}
92
93bool FastThread::threadLoop()
94{
Glenn Kasten388d5712017-04-07 14:38:41 -070095 // LOGT now works even if tlNBLogWriter is nullptr, but we're considering changing that,
Glenn Kasteneef598c2017-04-03 14:41:13 -070096 // so this initialization permits a future change to remove the check for nullptr.
Mikhail Naganov01d09d92018-09-18 12:38:57 -070097 tlNBLogWriter = mDummyNBLogWriter.get();
Glenn Kasten22340022014-04-07 12:04:41 -070098 for (;;) {
99
100 // either nanosleep, sched_yield, or busy wait
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800101 if (mSleepNs >= 0) {
102 if (mSleepNs > 0) {
103 ALOG_ASSERT(mSleepNs < 1000000000);
104 const struct timespec req = {0, mSleepNs};
Glenn Kasten22340022014-04-07 12:04:41 -0700105 nanosleep(&req, NULL);
106 } else {
107 sched_yield();
108 }
109 }
110 // default to long sleep for next cycle
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800111 mSleepNs = FAST_DEFAULT_NS;
Glenn Kasten22340022014-04-07 12:04:41 -0700112
113 // poll for state change
114 const FastThreadState *next = poll();
115 if (next == NULL) {
116 // continue to use the default initial state until a real state is available
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800117 // FIXME &sInitial not available, should save address earlier
118 //ALOG_ASSERT(mCurrent == &sInitial && previous == &sInitial);
119 next = mCurrent;
Glenn Kasten22340022014-04-07 12:04:41 -0700120 }
121
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800122 mCommand = next->mCommand;
123 if (next != mCurrent) {
Glenn Kasten22340022014-04-07 12:04:41 -0700124
125 // As soon as possible of learning of a new dump area, start using it
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800126 mDumpState = next->mDumpState != NULL ? next->mDumpState : mDummyDumpState;
Mikhail Naganov01d09d92018-09-18 12:38:57 -0700127 mNBLogWriter = next->mNBLogWriter != NULL ?
128 next->mNBLogWriter : mDummyNBLogWriter.get();
Glenn Kasten3ab8d662017-04-03 14:35:09 -0700129 setNBLogWriter(mNBLogWriter); // FastMixer informs its AudioMixer, FastCapture ignores
Glenn Kasten388d5712017-04-07 14:38:41 -0700130 tlNBLogWriter = mNBLogWriter;
Glenn Kasten22340022014-04-07 12:04:41 -0700131
132 // We want to always have a valid reference to the previous (non-idle) state.
133 // However, the state queue only guarantees access to current and previous states.
134 // So when there is a transition from a non-idle state into an idle state, we make a
135 // copy of the last known non-idle state so it is still available on return from idle.
136 // The possible transitions are:
137 // non-idle -> non-idle update previous from current in-place
138 // non-idle -> idle update previous from copy of current
139 // idle -> idle don't update previous
140 // idle -> non-idle don't update previous
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800141 if (!(mCurrent->mCommand & FastThreadState::IDLE)) {
142 if (mCommand & FastThreadState::IDLE) {
Glenn Kasten22340022014-04-07 12:04:41 -0700143 onIdle();
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800144 mOldTsValid = false;
Glenn Kasten214b4062015-03-02 14:15:47 -0800145#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800146 mOldLoadValid = false;
Glenn Kasten22340022014-04-07 12:04:41 -0700147#endif
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800148 mIgnoreNextOverrun = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700149 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800150 mPrevious = mCurrent;
Glenn Kasten22340022014-04-07 12:04:41 -0700151 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800152 mCurrent = next;
Glenn Kasten22340022014-04-07 12:04:41 -0700153 }
154#if !LOG_NDEBUG
155 next = NULL; // not referenced again
156#endif
157
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800158 mDumpState->mCommand = mCommand;
Glenn Kasten22340022014-04-07 12:04:41 -0700159
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800160 // FIXME what does this comment mean?
Glenn Kasten22340022014-04-07 12:04:41 -0700161 // << current, previous, command, dumpState >>
162
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800163 switch (mCommand) {
Glenn Kasten22340022014-04-07 12:04:41 -0700164 case FastThreadState::INITIAL:
165 case FastThreadState::HOT_IDLE:
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800166 mSleepNs = FAST_HOT_IDLE_NS;
Glenn Kasten22340022014-04-07 12:04:41 -0700167 continue;
168 case FastThreadState::COLD_IDLE:
169 // only perform a cold idle command once
170 // FIXME consider checking previous state and only perform if previous != COLD_IDLE
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800171 if (mCurrent->mColdGen != mColdGen) {
172 int32_t *coldFutexAddr = mCurrent->mColdFutexAddr;
Glenn Kasten22340022014-04-07 12:04:41 -0700173 ALOG_ASSERT(coldFutexAddr != NULL);
174 int32_t old = android_atomic_dec(coldFutexAddr);
175 if (old <= 0) {
Elliott Hughese348c5b2014-05-21 18:47:50 -0700176 syscall(__NR_futex, coldFutexAddr, FUTEX_WAIT_PRIVATE, old - 1, NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700177 }
Glenn Kasten8255ba72016-08-23 13:54:23 -0700178 int policy = sched_getscheduler(0) & ~SCHED_RESET_ON_FORK;
Glenn Kasten22340022014-04-07 12:04:41 -0700179 if (!(policy == SCHED_FIFO || policy == SCHED_RR)) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -0800180 ALOGE("did not receive expected priority boost on time");
Glenn Kasten22340022014-04-07 12:04:41 -0700181 }
182 // This may be overly conservative; there could be times that the normal mixer
183 // requests such a brief cold idle that it doesn't require resetting this flag.
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800184 mIsWarm = false;
185 mMeasuredWarmupTs.tv_sec = 0;
186 mMeasuredWarmupTs.tv_nsec = 0;
187 mWarmupCycles = 0;
188 mWarmupConsecutiveInRangeCycles = 0;
189 mSleepNs = -1;
190 mColdGen = mCurrent->mColdGen;
Glenn Kasten214b4062015-03-02 14:15:47 -0800191#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800192 mBounds = 0;
193 mFull = false;
Glenn Kasten22340022014-04-07 12:04:41 -0700194#endif
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800195 mOldTsValid = !clock_gettime(CLOCK_MONOTONIC, &mOldTs);
196 mTimestampStatus = INVALID_OPERATION;
Glenn Kasten22340022014-04-07 12:04:41 -0700197 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800198 mSleepNs = FAST_HOT_IDLE_NS;
Glenn Kasten22340022014-04-07 12:04:41 -0700199 }
200 continue;
201 case FastThreadState::EXIT:
202 onExit();
203 return false;
204 default:
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800205 LOG_ALWAYS_FATAL_IF(!isSubClassCommand(mCommand));
Glenn Kasten22340022014-04-07 12:04:41 -0700206 break;
207 }
208
209 // there is a non-idle state available to us; did the state change?
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800210 if (mCurrent != mPrevious) {
Glenn Kasten22340022014-04-07 12:04:41 -0700211 onStateChange();
212#if 1 // FIXME shouldn't need this
213 // only process state change once
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800214 mPrevious = mCurrent;
Glenn Kasten22340022014-04-07 12:04:41 -0700215#endif
216 }
217
218 // do work using current state here
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800219 mAttemptedWrite = false;
Glenn Kasten22340022014-04-07 12:04:41 -0700220 onWork();
221
222 // To be exactly periodic, compute the next sleep time based on current time.
223 // This code doesn't have long-term stability when the sink is non-blocking.
224 // FIXME To avoid drift, use the local audio clock or watch the sink's fill status.
225 struct timespec newTs;
226 int rc = clock_gettime(CLOCK_MONOTONIC, &newTs);
227 if (rc == 0) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800228 if (mOldTsValid) {
229 time_t sec = newTs.tv_sec - mOldTs.tv_sec;
230 long nsec = newTs.tv_nsec - mOldTs.tv_nsec;
Glenn Kasten22340022014-04-07 12:04:41 -0700231 ALOGE_IF(sec < 0 || (sec == 0 && nsec < 0),
232 "clock_gettime(CLOCK_MONOTONIC) failed: was %ld.%09ld but now %ld.%09ld",
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800233 mOldTs.tv_sec, mOldTs.tv_nsec, newTs.tv_sec, newTs.tv_nsec);
Glenn Kasten22340022014-04-07 12:04:41 -0700234 if (nsec < 0) {
235 --sec;
236 nsec += 1000000000;
237 }
238 // To avoid an initial underrun on fast tracks after exiting standby,
239 // do not start pulling data from tracks and mixing until warmup is complete.
240 // Warmup is considered complete after the earlier of:
Glenn Kastend2123e62015-01-29 10:02:44 -0800241 // MIN_WARMUP_CYCLES consecutive in-range write() attempts,
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800242 // where "in-range" means mWarmupNsMin <= cycle time <= mWarmupNsMax
Glenn Kasten22340022014-04-07 12:04:41 -0700243 // MAX_WARMUP_CYCLES write() attempts.
244 // This is overly conservative, but to get better accuracy requires a new HAL API.
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800245 if (!mIsWarm && mAttemptedWrite) {
246 mMeasuredWarmupTs.tv_sec += sec;
247 mMeasuredWarmupTs.tv_nsec += nsec;
248 if (mMeasuredWarmupTs.tv_nsec >= 1000000000) {
249 mMeasuredWarmupTs.tv_sec++;
250 mMeasuredWarmupTs.tv_nsec -= 1000000000;
Glenn Kasten22340022014-04-07 12:04:41 -0700251 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800252 ++mWarmupCycles;
253 if (mWarmupNsMin <= nsec && nsec <= mWarmupNsMax) {
254 ALOGV("warmup cycle %d in range: %.03f ms", mWarmupCycles, nsec * 1e-9);
255 ++mWarmupConsecutiveInRangeCycles;
Glenn Kastend2123e62015-01-29 10:02:44 -0800256 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800257 ALOGV("warmup cycle %d out of range: %.03f ms", mWarmupCycles, nsec * 1e-9);
258 mWarmupConsecutiveInRangeCycles = 0;
Glenn Kastend2123e62015-01-29 10:02:44 -0800259 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800260 if ((mWarmupConsecutiveInRangeCycles >= MIN_WARMUP_CYCLES) ||
261 (mWarmupCycles >= MAX_WARMUP_CYCLES)) {
262 mIsWarm = true;
263 mDumpState->mMeasuredWarmupTs = mMeasuredWarmupTs;
264 mDumpState->mWarmupCycles = mWarmupCycles;
Eric Tanfefe3162018-09-07 10:09:11 -0700265 const double measuredWarmupMs = (mMeasuredWarmupTs.tv_sec * 1e3) +
266 (mMeasuredWarmupTs.tv_nsec * 1e-6);
267 LOG_WARMUP_TIME(measuredWarmupMs);
Glenn Kasten22340022014-04-07 12:04:41 -0700268 }
269 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800270 mSleepNs = -1;
271 if (mIsWarm) {
272 if (sec > 0 || nsec > mUnderrunNs) {
Glenn Kasten22340022014-04-07 12:04:41 -0700273 ATRACE_NAME("underrun");
274 // FIXME only log occasionally
275 ALOGV("underrun: time since last cycle %d.%03ld sec",
276 (int) sec, nsec / 1000000L);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800277 mDumpState->mUnderruns++;
Eric Tanfefe3162018-09-07 10:09:11 -0700278 LOG_UNDERRUN(audio_utils_ns_from_timespec(&newTs));
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800279 mIgnoreNextOverrun = true;
280 } else if (nsec < mOverrunNs) {
281 if (mIgnoreNextOverrun) {
282 mIgnoreNextOverrun = false;
Glenn Kasten22340022014-04-07 12:04:41 -0700283 } else {
284 // FIXME only log occasionally
285 ALOGV("overrun: time since last cycle %d.%03ld sec",
286 (int) sec, nsec / 1000000L);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800287 mDumpState->mOverruns++;
Eric Tanfefe3162018-09-07 10:09:11 -0700288 LOG_OVERRUN(audio_utils_ns_from_timespec(&newTs));
Glenn Kasten22340022014-04-07 12:04:41 -0700289 }
290 // This forces a minimum cycle time. It:
291 // - compensates for an audio HAL with jitter due to sample rate conversion
292 // - works with a variable buffer depth audio HAL that never pulls at a
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800293 // rate < than mOverrunNs per buffer.
Glenn Kasten22340022014-04-07 12:04:41 -0700294 // - recovers from overrun immediately after underrun
295 // It doesn't work with a non-blocking audio HAL.
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800296 mSleepNs = mForceNs - nsec;
Glenn Kasten22340022014-04-07 12:04:41 -0700297 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800298 mIgnoreNextOverrun = false;
Glenn Kasten22340022014-04-07 12:04:41 -0700299 }
300 }
Glenn Kasten214b4062015-03-02 14:15:47 -0800301#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800302 if (mIsWarm) {
Glenn Kasten22340022014-04-07 12:04:41 -0700303 // advance the FIFO queue bounds
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800304 size_t i = mBounds & (mDumpState->mSamplingN - 1);
305 mBounds = (mBounds & 0xFFFF0000) | ((mBounds + 1) & 0xFFFF);
306 if (mFull) {
Ivan Lozano77657142018-01-02 21:01:16 +0000307 //mBounds += 0x10000;
308 __builtin_add_overflow(mBounds, 0x10000, &mBounds);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800309 } else if (!(mBounds & (mDumpState->mSamplingN - 1))) {
310 mFull = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700311 }
312 // compute the delta value of clock_gettime(CLOCK_MONOTONIC)
313 uint32_t monotonicNs = nsec;
314 if (sec > 0 && sec < 4) {
315 monotonicNs += sec * 1000000000;
316 }
317 // compute raw CPU load = delta value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
318 uint32_t loadNs = 0;
319 struct timespec newLoad;
320 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &newLoad);
321 if (rc == 0) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800322 if (mOldLoadValid) {
323 sec = newLoad.tv_sec - mOldLoad.tv_sec;
324 nsec = newLoad.tv_nsec - mOldLoad.tv_nsec;
Glenn Kasten22340022014-04-07 12:04:41 -0700325 if (nsec < 0) {
326 --sec;
327 nsec += 1000000000;
328 }
329 loadNs = nsec;
330 if (sec > 0 && sec < 4) {
331 loadNs += sec * 1000000000;
332 }
333 } else {
334 // first time through the loop
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800335 mOldLoadValid = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700336 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800337 mOldLoad = newLoad;
Glenn Kasten22340022014-04-07 12:04:41 -0700338 }
339#ifdef CPU_FREQUENCY_STATISTICS
340 // get the absolute value of CPU clock frequency in kHz
341 int cpuNum = sched_getcpu();
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800342 uint32_t kHz = mTcu.getCpukHz(cpuNum);
Glenn Kasten22340022014-04-07 12:04:41 -0700343 kHz = (kHz << 4) | (cpuNum & 0xF);
344#endif
345 // save values in FIFO queues for dumpsys
346 // these stores #1, #2, #3 are not atomic with respect to each other,
347 // or with respect to store #4 below
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800348 mDumpState->mMonotonicNs[i] = monotonicNs;
Eric Tancf3d82c2018-09-04 15:44:45 -0700349 LOG_WORK_TIME(monotonicNs);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800350 mDumpState->mLoadNs[i] = loadNs;
Glenn Kasten22340022014-04-07 12:04:41 -0700351#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800352 mDumpState->mCpukHz[i] = kHz;
Glenn Kasten22340022014-04-07 12:04:41 -0700353#endif
354 // this store #4 is not atomic with respect to stores #1, #2, #3 above, but
355 // the newest open & oldest closed halves are atomic with respect to each other
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800356 mDumpState->mBounds = mBounds;
Glenn Kastenf9715e42016-07-13 14:02:03 -0700357 ATRACE_INT(mCycleMs, monotonicNs / 1000000);
358 ATRACE_INT(mLoadUs, loadNs / 1000);
Glenn Kasten22340022014-04-07 12:04:41 -0700359 }
360#endif
361 } else {
362 // first time through the loop
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800363 mOldTsValid = true;
364 mSleepNs = mPeriodNs;
365 mIgnoreNextOverrun = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700366 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800367 mOldTs = newTs;
Glenn Kasten22340022014-04-07 12:04:41 -0700368 } else {
369 // monotonic clock is broken
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800370 mOldTsValid = false;
371 mSleepNs = mPeriodNs;
Glenn Kasten22340022014-04-07 12:04:41 -0700372 }
373
374 } // for (;;)
375
376 // never return 'true'; Thread::_threadLoop() locks mutex which can result in priority inversion
377}
378
379} // namespace android