blob: cb32e9dccf57b7d4b097f04cc71eb16ce0d7281e [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 Kasten22340022014-04-07 12:04:41 -070033 FastThread();
34 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;
44 virtual void setLog(NBLog::Writer *logWriter __unused) { }
45 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
51 // FIXME these former local variables need comments and to be renamed to have an "m" prefix
52 const FastThreadState *previous;
53 const FastThreadState *current;
54 struct timespec oldTs;
55 bool oldTsValid;
56 long sleepNs; // -1: busy wait, 0: sched_yield, > 0: nanosleep
57 long periodNs; // expected period; the time required to render one mix buffer
58 long underrunNs; // underrun likely when write cycle is greater than this value
59 long overrunNs; // overrun likely when write cycle is less than this value
60 long forceNs; // if overrun detected, force the write cycle to take this much time
Glenn Kastend2123e62015-01-29 10:02:44 -080061 long warmupNsMin; // warmup complete when write cycle is greater than or equal to this value
62 long warmupNsMax; // and less than or equal to this value
Glenn Kasten22340022014-04-07 12:04:41 -070063 FastThreadDumpState *mDummyDumpState;
64 FastThreadDumpState *dumpState;
65 bool ignoreNextOverrun; // used to ignore initial overrun and first after an underrun
66#ifdef FAST_MIXER_STATISTICS
67 struct timespec oldLoad; // previous value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
68 bool oldLoadValid; // whether oldLoad is valid
69 uint32_t bounds;
70 bool full; // whether we have collected at least mSamplingN samples
71#ifdef CPU_FREQUENCY_STATISTICS
72 ThreadCpuUsage tcu; // for reading the current CPU clock frequency in kHz
73#endif
74#endif
75 unsigned coldGen; // last observed mColdGen
76 bool isWarm; // true means ready to mix, false means wait for warmup before mixing
77 struct timespec measuredWarmupTs; // how long did it take for warmup to complete
Glenn Kastend2123e62015-01-29 10:02:44 -080078 uint32_t warmupCycles; // counter of number of loop cycles during warmup phase
79 uint32_t warmupConsecutiveInRangeCycles; // number of consecutive cycles in range
Glenn Kasten22340022014-04-07 12:04:41 -070080 NBLog::Writer dummyLogWriter;
81 NBLog::Writer *logWriter;
82 status_t timestampStatus;
83
84 FastThreadState::Command command;
85#if 0
86 size_t frameCount;
87#endif
88 bool attemptedWrite;
Glenn Kasten4cd161b2014-03-18 16:25:04 -070089
90}; // class FastThread
91
92} // android
93
94#endif // ANDROID_AUDIO_FAST_THREAD_H