FastThread.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #ifndef ANDROID_AUDIO_FAST_THREAD_H
  17. #define ANDROID_AUDIO_FAST_THREAD_H
  18. #include "Configuration.h"
  19. #ifdef CPU_FREQUENCY_STATISTICS
  20. #include <cpustats/ThreadCpuUsage.h>
  21. #endif
  22. #include <utils/Thread.h>
  23. #include "FastThreadState.h"
  24. namespace android {
  25. // FastThread is the common abstract base class of FastMixer and FastCapture
  26. class FastThread : public Thread {
  27. public:
  28. FastThread(const char *cycleMs, const char *loadUs);
  29. virtual ~FastThread();
  30. private:
  31. // implement Thread::threadLoop()
  32. virtual bool threadLoop();
  33. protected:
  34. // callouts to subclass in same lexical order as they were in original FastMixer.cpp
  35. // FIXME need comments
  36. virtual const FastThreadState *poll() = 0;
  37. virtual void setNBLogWriter(NBLog::Writer *logWriter __unused) { }
  38. virtual void onIdle() = 0;
  39. virtual void onExit() = 0;
  40. virtual bool isSubClassCommand(FastThreadState::Command command) = 0;
  41. virtual void onStateChange() = 0;
  42. virtual void onWork() = 0;
  43. // FIXME these former local variables need comments
  44. const FastThreadState* mPrevious;
  45. const FastThreadState* mCurrent;
  46. struct timespec mOldTs;
  47. bool mOldTsValid;
  48. long mSleepNs; // -1: busy wait, 0: sched_yield, > 0: nanosleep
  49. long mPeriodNs; // expected period; the time required to render one mix buffer
  50. long mUnderrunNs; // underrun likely when write cycle is greater than this value
  51. long mOverrunNs; // overrun likely when write cycle is less than this value
  52. long mForceNs; // if overrun detected,
  53. // force the write cycle to take this much time
  54. long mWarmupNsMin; // warmup complete when write cycle is greater than or equal to
  55. // this value
  56. long mWarmupNsMax; // and less than or equal to this value
  57. FastThreadDumpState* mDummyDumpState;
  58. FastThreadDumpState* mDumpState;
  59. bool mIgnoreNextOverrun; // used to ignore initial overrun and first after an
  60. // underrun
  61. #ifdef FAST_THREAD_STATISTICS
  62. struct timespec mOldLoad; // previous value of clock_gettime(CLOCK_THREAD_CPUTIME_ID)
  63. bool mOldLoadValid; // whether oldLoad is valid
  64. uint32_t mBounds;
  65. bool mFull; // whether we have collected at least mSamplingN samples
  66. #ifdef CPU_FREQUENCY_STATISTICS
  67. ThreadCpuUsage mTcu; // for reading the current CPU clock frequency in kHz
  68. #endif
  69. #endif
  70. unsigned mColdGen; // last observed mColdGen
  71. bool mIsWarm; // true means ready to mix,
  72. // false means wait for warmup before mixing
  73. struct timespec mMeasuredWarmupTs; // how long did it take for warmup to complete
  74. uint32_t mWarmupCycles; // counter of number of loop cycles during warmup phase
  75. uint32_t mWarmupConsecutiveInRangeCycles; // number of consecutive cycles in range
  76. const sp<NBLog::Writer> mDummyNBLogWriter{new NBLog::Writer()};
  77. status_t mTimestampStatus;
  78. FastThreadState::Command mCommand;
  79. bool mAttemptedWrite;
  80. char mCycleMs[16]; // cycle_ms + suffix
  81. char mLoadUs[16]; // load_us + suffix
  82. }; // class FastThread
  83. } // android
  84. #endif // ANDROID_AUDIO_FAST_THREAD_H