| Glenn Kasten | 4cd161b | 2014-03-18 16:25:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 17 | #pragma once |
| Glenn Kasten | 4cd161b | 2014-03-18 16:25:04 -0700 | [diff] [blame] | 18 | |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 19 | #include "Configuration.h" |
| 20 | #ifdef CPU_FREQUENCY_STATISTICS |
| 21 | #include <cpustats/ThreadCpuUsage.h> |
| 22 | #endif |
| Glenn Kasten | 4cd161b | 2014-03-18 16:25:04 -0700 | [diff] [blame] | 23 | #include <utils/Thread.h> |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 24 | #include "FastThreadState.h" |
| Glenn Kasten | 4cd161b | 2014-03-18 16:25:04 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | // FastThread is the common abstract base class of FastMixer and FastCapture |
| 29 | class FastThread : public Thread { |
| 30 | |
| 31 | public: |
| Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 32 | FastThread(const char *cycleMs, const char *loadUs); |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 33 | |
| 34 | private: |
| 35 | // implement Thread::threadLoop() |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 36 | bool threadLoop() override; |
| Glenn Kasten | 4cd161b | 2014-03-18 16:25:04 -0700 | [diff] [blame] | 37 | |
| 38 | protected: |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 39 | // 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 Kasten | 3ab8d66 | 2017-04-03 14:35:09 -0700 | [diff] [blame] | 42 | virtual void setNBLogWriter(NBLog::Writer *logWriter __unused) { } |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 43 | 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 Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 49 | // FIXME these former local variables need comments |
| Andy Hung | 0530237 | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 50 | const FastThreadState* mPrevious = nullptr; |
| 51 | const FastThreadState* mCurrent = nullptr; |
| 52 | struct timespec mOldTs{}; |
| 53 | bool mOldTsValid = false; |
| 54 | int64_t mSleepNs = -1; // -1: busy wait, 0: sched_yield, > 0: nanosleep |
| 55 | int64_t mPeriodNs = 0; // expected period; the time required to |
| 56 | // render one mix buffer |
| 57 | int64_t mUnderrunNs = 0; // underrun likely when write cycle |
| 58 | // is greater than this value |
| 59 | int64_t mOverrunNs = 0; // overrun likely when write cycle is less than this value |
| 60 | int64_t mForceNs = 0; // if overrun detected, |
| 61 | // force the write cycle to take this much time |
| 62 | int64_t mWarmupNsMin = 0; // warmup complete when write cycle is greater |
| 63 | // than or equal to this value |
| 64 | int64_t mWarmupNsMax = INT64_MAX; // and less than or equal to this value |
| 65 | FastThreadDumpState* mDummyDumpState = nullptr; |
| 66 | FastThreadDumpState* mDumpState = nullptr; |
| 67 | bool mIgnoreNextOverrun = true; // used to ignore initial overrun |
| 68 | // and first after an underrun |
| Glenn Kasten | 214b406 | 2015-03-02 14:15:47 -0800 | [diff] [blame] | 69 | #ifdef FAST_THREAD_STATISTICS |
| Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 70 | struct timespec mOldLoad; // previous value of clock_gettime(CLOCK_THREAD_CPUTIME_ID) |
| Andy Hung | 0530237 | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 71 | bool mOldLoadValid = false; // whether oldLoad is valid |
| 72 | uint32_t mBounds = 0; |
| 73 | bool mFull = false; // whether we have collected at least mSamplingN samples |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 74 | #ifdef CPU_FREQUENCY_STATISTICS |
| Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 75 | ThreadCpuUsage mTcu; // for reading the current CPU clock frequency in kHz |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 76 | #endif |
| 77 | #endif |
| Andy Hung | 0530237 | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 78 | unsigned mColdGen = 0; // last observed mColdGen |
| 79 | bool mIsWarm = false; // true means ready to mix, |
| Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 80 | // false means wait for warmup before mixing |
| Andy Hung | 0530237 | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 81 | struct timespec mMeasuredWarmupTs{}; // how long did it take for warmup to complete |
| 82 | uint32_t mWarmupCycles = 0; // counter of number of loop cycles during warmup phase |
| 83 | uint32_t mWarmupConsecutiveInRangeCycles = 0; // number of consecutive cycles in range |
| Andy Hung | a37aaa2 | 2018-09-18 18:15:37 -0700 | [diff] [blame] | 84 | const sp<NBLog::Writer> mDummyNBLogWriter{new NBLog::Writer()}; |
| Andy Hung | 0530237 | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 85 | status_t mTimestampStatus = INVALID_OPERATION; |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 86 | |
| Andy Hung | 0530237 | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 87 | FastThreadState::Command mCommand = FastThreadState::INITIAL; |
| 88 | bool mAttemptedWrite = false; |
| Glenn Kasten | 4cd161b | 2014-03-18 16:25:04 -0700 | [diff] [blame] | 89 | |
| Andy Hung | 0530237 | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 90 | // init in constructor |
| Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 91 | char mCycleMs[16]; // cycle_ms + suffix |
| 92 | char mLoadUs[16]; // load_us + suffix |
| 93 | |
| Glenn Kasten | 4cd161b | 2014-03-18 16:25:04 -0700 | [diff] [blame] | 94 | }; // class FastThread |
| 95 | |
| Andy Hung | 71ba4b3 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 96 | } // namespace android |