blob: 3f6b20676885bfd3842bb18b63112b61fe656a74 [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
17#ifndef ANDROID_AUDIO_FAST_THREAD_H
18#define ANDROID_AUDIO_FAST_THREAD_H
19
Glenn Kasten22340022014-04-07 12:04:41 -070020#include "Configuration.h"
21#ifdef CPU_FREQUENCY_STATISTICS
22#include <cpustats/ThreadCpuUsage.h>
23#endif
Glenn Kasten4cd161b2014-03-18 16:25:04 -070024#include <utils/Thread.h>
Glenn Kasten22340022014-04-07 12:04:41 -070025#include "FastThreadState.h"
Glenn Kasten4cd161b2014-03-18 16:25:04 -070026
27namespace android {
28
29// FastThread is the common abstract base class of FastMixer and FastCapture
30class FastThread : public Thread {
31
32public:
Glenn Kastenf9715e42016-07-13 14:02:03 -070033 FastThread(const char *cycleMs, const char *loadUs);
Glenn Kasten22340022014-04-07 12:04:41 -070034 virtual ~FastThread();
35
36private:
37 // implement Thread::threadLoop()
38 virtual bool threadLoop();
Glenn Kasten4cd161b2014-03-18 16:25:04 -070039
40protected:
Glenn Kasten22340022014-04-07 12:04:41 -070041 // callouts to subclass in same lexical order as they were in original FastMixer.cpp
42 // FIXME need comments
43 virtual const FastThreadState *poll() = 0;
Glenn Kasten3ab8d662017-04-03 14:35:09 -070044 virtual void setNBLogWriter(NBLog::Writer *logWriter __unused) { }
Glenn Kasten22340022014-04-07 12:04:41 -070045 virtual void onIdle() = 0;
46 virtual void onExit() = 0;
47 virtual bool isSubClassCommand(FastThreadState::Command command) = 0;
48 virtual void onStateChange() = 0;
49 virtual void onWork() = 0;
50
Glenn Kastene4a7ce22015-03-03 11:23:17 -080051 // FIXME these former local variables need comments
52 const FastThreadState* mPrevious;
53 const FastThreadState* mCurrent;
54 struct timespec mOldTs;
55 bool mOldTsValid;
56 long mSleepNs; // -1: busy wait, 0: sched_yield, > 0: nanosleep
57 long mPeriodNs; // expected period; the time required to render one mix buffer
58 long mUnderrunNs; // underrun likely when write cycle is greater than this value
59 long mOverrunNs; // overrun likely when write cycle is less than this value
60 long mForceNs; // if overrun detected,
61 // force the write cycle to take this much time
62 long mWarmupNsMin; // warmup complete when write cycle is greater than or equal to
63 // this value
64 long mWarmupNsMax; // and less than or equal to this value
65 FastThreadDumpState* mDummyDumpState;
66 FastThreadDumpState* mDumpState;
67 bool mIgnoreNextOverrun; // used to ignore initial overrun and first after an
68 // underrun
Glenn Kasten214b4062015-03-02 14:15:47 -080069#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -080070 struct timespec mOldLoad; // previous value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
71 bool mOldLoadValid; // whether oldLoad is valid
72 uint32_t mBounds;
73 bool mFull; // whether we have collected at least mSamplingN samples
Glenn Kasten22340022014-04-07 12:04:41 -070074#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -080075 ThreadCpuUsage mTcu; // for reading the current CPU clock frequency in kHz
Glenn Kasten22340022014-04-07 12:04:41 -070076#endif
77#endif
Glenn Kastene4a7ce22015-03-03 11:23:17 -080078 unsigned mColdGen; // last observed mColdGen
79 bool mIsWarm; // true means ready to mix,
80 // false means wait for warmup before mixing
Mikhail Naganov01d09d92018-09-18 12:38:57 -070081 struct timespec mMeasuredWarmupTs; // how long did it take for warmup to complete
82 uint32_t mWarmupCycles; // counter of number of loop cycles during warmup phase
83 uint32_t mWarmupConsecutiveInRangeCycles; // number of consecutive cycles in range
Andy Hunga37aaa22018-09-18 18:15:37 -070084 const sp<NBLog::Writer> mDummyNBLogWriter{new NBLog::Writer()};
Mikhail Naganov01d09d92018-09-18 12:38:57 -070085 status_t mTimestampStatus;
Glenn Kasten22340022014-04-07 12:04:41 -070086
Glenn Kastene4a7ce22015-03-03 11:23:17 -080087 FastThreadState::Command mCommand;
88 bool mAttemptedWrite;
Glenn Kasten4cd161b2014-03-18 16:25:04 -070089
Glenn Kastenf9715e42016-07-13 14:02:03 -070090 char mCycleMs[16]; // cycle_ms + suffix
91 char mLoadUs[16]; // load_us + suffix
92
Glenn Kasten4cd161b2014-03-18 16:25:04 -070093}; // class FastThread
94
95} // android
96
97#endif // ANDROID_AUDIO_FAST_THREAD_H