blob: 84dc4d2935bce9380f8bc8402e15fa98300c2c9c [file] [log] [blame]
Glenn Kasten4cd161b2014-03-18 16:25:04 -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
Andy Hungf0859f32023-05-25 16:28:04 -070017#pragma once
Glenn Kasten4cd161b2014-03-18 16:25:04 -070018
Glenn Kasten22340022014-04-07 12:04:41 -070019#include "Configuration.h"
20#ifdef CPU_FREQUENCY_STATISTICS
21#include <cpustats/ThreadCpuUsage.h>
22#endif
Glenn Kasten4cd161b2014-03-18 16:25:04 -070023#include <utils/Thread.h>
Glenn Kasten22340022014-04-07 12:04:41 -070024#include "FastThreadState.h"
Glenn Kasten4cd161b2014-03-18 16:25:04 -070025
26namespace android {
27
28// FastThread is the common abstract base class of FastMixer and FastCapture
29class FastThread : public Thread {
30
31public:
Glenn Kastenf9715e42016-07-13 14:02:03 -070032 FastThread(const char *cycleMs, const char *loadUs);
Glenn Kasten22340022014-04-07 12:04:41 -070033
34private:
35 // implement Thread::threadLoop()
Andy Hungf0859f32023-05-25 16:28:04 -070036 bool threadLoop() override;
Glenn Kasten4cd161b2014-03-18 16:25:04 -070037
38protected:
Glenn Kasten22340022014-04-07 12:04:41 -070039 // callouts to subclass in same lexical order as they were in original FastMixer.cpp
40 // FIXME need comments
41 virtual const FastThreadState *poll() = 0;
Glenn Kasten3ab8d662017-04-03 14:35:09 -070042 virtual void setNBLogWriter(NBLog::Writer *logWriter __unused) { }
Glenn Kasten22340022014-04-07 12:04:41 -070043 virtual void onIdle() = 0;
44 virtual void onExit() = 0;
45 virtual bool isSubClassCommand(FastThreadState::Command command) = 0;
46 virtual void onStateChange() = 0;
47 virtual void onWork() = 0;
48
Glenn Kastene4a7ce22015-03-03 11:23:17 -080049 // FIXME these former local variables need comments
50 const FastThreadState* mPrevious;
51 const FastThreadState* mCurrent;
52 struct timespec mOldTs;
53 bool mOldTsValid;
54 long mSleepNs; // -1: busy wait, 0: sched_yield, > 0: nanosleep
55 long mPeriodNs; // expected period; the time required to render one mix buffer
56 long mUnderrunNs; // underrun likely when write cycle is greater than this value
57 long mOverrunNs; // overrun likely when write cycle is less than this value
58 long mForceNs; // if overrun detected,
59 // force the write cycle to take this much time
60 long mWarmupNsMin; // warmup complete when write cycle is greater than or equal to
61 // this value
62 long mWarmupNsMax; // and less than or equal to this value
63 FastThreadDumpState* mDummyDumpState;
64 FastThreadDumpState* mDumpState;
65 bool mIgnoreNextOverrun; // used to ignore initial overrun and first after an
66 // underrun
Glenn Kasten214b4062015-03-02 14:15:47 -080067#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -080068 struct timespec mOldLoad; // previous value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
69 bool mOldLoadValid; // whether oldLoad is valid
70 uint32_t mBounds;
71 bool mFull; // whether we have collected at least mSamplingN samples
Glenn Kasten22340022014-04-07 12:04:41 -070072#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -080073 ThreadCpuUsage mTcu; // for reading the current CPU clock frequency in kHz
Glenn Kasten22340022014-04-07 12:04:41 -070074#endif
75#endif
Glenn Kastene4a7ce22015-03-03 11:23:17 -080076 unsigned mColdGen; // last observed mColdGen
77 bool mIsWarm; // true means ready to mix,
78 // false means wait for warmup before mixing
Mikhail Naganov01d09d92018-09-18 12:38:57 -070079 struct timespec mMeasuredWarmupTs; // how long did it take for warmup to complete
80 uint32_t mWarmupCycles; // counter of number of loop cycles during warmup phase
81 uint32_t mWarmupConsecutiveInRangeCycles; // number of consecutive cycles in range
Andy Hunga37aaa22018-09-18 18:15:37 -070082 const sp<NBLog::Writer> mDummyNBLogWriter{new NBLog::Writer()};
Mikhail Naganov01d09d92018-09-18 12:38:57 -070083 status_t mTimestampStatus;
Glenn Kasten22340022014-04-07 12:04:41 -070084
Glenn Kastene4a7ce22015-03-03 11:23:17 -080085 FastThreadState::Command mCommand;
86 bool mAttemptedWrite;
Glenn Kasten4cd161b2014-03-18 16:25:04 -070087
Glenn Kastenf9715e42016-07-13 14:02:03 -070088 char mCycleMs[16]; // cycle_ms + suffix
89 char mLoadUs[16]; // load_us + suffix
90
Glenn Kasten4cd161b2014-03-18 16:25:04 -070091}; // class FastThread
92
Andy Hung71ba4b32022-10-06 12:09:49 -070093} // namespace android