xref: /aosp_15_r20/frameworks/av/services/audioflinger/afutils/AudioWatchdog.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*ec779b8eSAndroid Build Coastguard Worker  *
4*ec779b8eSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*ec779b8eSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*ec779b8eSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*ec779b8eSAndroid Build Coastguard Worker  *
8*ec779b8eSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*ec779b8eSAndroid Build Coastguard Worker  *
10*ec779b8eSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*ec779b8eSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*ec779b8eSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ec779b8eSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*ec779b8eSAndroid Build Coastguard Worker  * limitations under the License.
15*ec779b8eSAndroid Build Coastguard Worker  */
16*ec779b8eSAndroid Build Coastguard Worker 
17*ec779b8eSAndroid Build Coastguard Worker // The watchdog thread runs periodically.  It has two functions:
18*ec779b8eSAndroid Build Coastguard Worker //   (a) verify that adequate CPU time is available, and log
19*ec779b8eSAndroid Build Coastguard Worker //       as soon as possible when there appears to be a CPU shortage
20*ec779b8eSAndroid Build Coastguard Worker //   (b) monitor the other threads [not yet implemented]
21*ec779b8eSAndroid Build Coastguard Worker 
22*ec779b8eSAndroid Build Coastguard Worker #pragma once
23*ec779b8eSAndroid Build Coastguard Worker 
24*ec779b8eSAndroid Build Coastguard Worker #include <mutex>
25*ec779b8eSAndroid Build Coastguard Worker #include <time.h>
26*ec779b8eSAndroid Build Coastguard Worker #include <utils/Thread.h>
27*ec779b8eSAndroid Build Coastguard Worker 
28*ec779b8eSAndroid Build Coastguard Worker namespace android {
29*ec779b8eSAndroid Build Coastguard Worker 
30*ec779b8eSAndroid Build Coastguard Worker // Keeps a cache of AudioWatchdog statistics that can be logged by dumpsys.
31*ec779b8eSAndroid Build Coastguard Worker // The usual caveats about atomicity of information apply.
32*ec779b8eSAndroid Build Coastguard Worker struct AudioWatchdogDump {
33*ec779b8eSAndroid Build Coastguard Worker     uint32_t mUnderruns = 0;    // total number of underruns
34*ec779b8eSAndroid Build Coastguard Worker     uint32_t mLogs = 0;         // total number of log messages
35*ec779b8eSAndroid Build Coastguard Worker     time_t   mMostRecent = 0;   // time of most recent log
36*ec779b8eSAndroid Build Coastguard Worker     void     dump(int fd);  // should only be called on a stable copy, not the original
37*ec779b8eSAndroid Build Coastguard Worker };
38*ec779b8eSAndroid Build Coastguard Worker 
39*ec779b8eSAndroid Build Coastguard Worker class AudioWatchdog : public Thread {
40*ec779b8eSAndroid Build Coastguard Worker 
41*ec779b8eSAndroid Build Coastguard Worker public:
Thread(false)42*ec779b8eSAndroid Build Coastguard Worker     explicit AudioWatchdog(unsigned periodMs = 50) : Thread(false /*canCallJava*/),
43*ec779b8eSAndroid Build Coastguard Worker             mPeriodNs(periodMs * 1000000), mMaxCycleNs(mPeriodNs * 2)
44*ec779b8eSAndroid Build Coastguard Worker         {
45*ec779b8eSAndroid Build Coastguard Worker             // force an immediate log on first underrun
46*ec779b8eSAndroid Build Coastguard Worker             mLogTs.tv_sec = MIN_TIME_BETWEEN_LOGS_SEC;
47*ec779b8eSAndroid Build Coastguard Worker             mLogTs.tv_nsec = 0;
48*ec779b8eSAndroid Build Coastguard Worker         }
49*ec779b8eSAndroid Build Coastguard Worker 
50*ec779b8eSAndroid Build Coastguard Worker      // Do not call Thread::requestExitAndWait() without first calling requestExit().
51*ec779b8eSAndroid Build Coastguard Worker     // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough.
52*ec779b8eSAndroid Build Coastguard Worker     void            requestExit() override;
53*ec779b8eSAndroid Build Coastguard Worker 
54*ec779b8eSAndroid Build Coastguard Worker     // FIXME merge API and implementation with AudioTrackThread
55*ec779b8eSAndroid Build Coastguard Worker     void            pause();   // suspend thread from execution at next loop boundary
56*ec779b8eSAndroid Build Coastguard Worker     void            resume();  // allow thread to execute, if not requested to exit
57*ec779b8eSAndroid Build Coastguard Worker 
58*ec779b8eSAndroid Build Coastguard Worker     // Where to store the dump, or NULL to not update
59*ec779b8eSAndroid Build Coastguard Worker     void            setDump(AudioWatchdogDump* dump);
60*ec779b8eSAndroid Build Coastguard Worker 
61*ec779b8eSAndroid Build Coastguard Worker private:
62*ec779b8eSAndroid Build Coastguard Worker     bool            threadLoop() override;
63*ec779b8eSAndroid Build Coastguard Worker 
64*ec779b8eSAndroid Build Coastguard Worker     static constexpr int32_t MIN_TIME_BETWEEN_LOGS_SEC = 60;
65*ec779b8eSAndroid Build Coastguard Worker     const uint32_t  mPeriodNs;       // nominal period
66*ec779b8eSAndroid Build Coastguard Worker     const uint32_t  mMaxCycleNs;     // maximum allowed time of one cycle before declaring underrun
67*ec779b8eSAndroid Build Coastguard Worker 
68*ec779b8eSAndroid Build Coastguard Worker     mutable std::mutex mLock;      // Thread::mLock is private
69*ec779b8eSAndroid Build Coastguard Worker     std::condition_variable mCond; // Thread::mThreadExitedCondition is private
70*ec779b8eSAndroid Build Coastguard Worker     bool            mPaused GUARDED_BY(mLock) = false; // whether thread is currently paused
71*ec779b8eSAndroid Build Coastguard Worker     bool            mOldTsValid GUARDED_BY(mLock) = false;  // whether mOldTs is valid
72*ec779b8eSAndroid Build Coastguard Worker     struct timespec mOldTs GUARDED_BY(mLock);          // monotonic time when threadLoop last ran
73*ec779b8eSAndroid Build Coastguard Worker     struct timespec mLogTs GUARDED_BY(mLock);          // time since last log (ctor init).
74*ec779b8eSAndroid Build Coastguard Worker     uint32_t        mUnderruns GUARDED_BY(mLock) = 0;  // total number of underruns
75*ec779b8eSAndroid Build Coastguard Worker     uint32_t        mLogs GUARDED_BY(mLock) = 0;       // total number of logs
76*ec779b8eSAndroid Build Coastguard Worker 
77*ec779b8eSAndroid Build Coastguard Worker     // where to store the dump, always non-NULL
78*ec779b8eSAndroid Build Coastguard Worker     AudioWatchdogDump*  mDump GUARDED_BY(mLock) = &mDummyDump;
79*ec779b8eSAndroid Build Coastguard Worker     AudioWatchdogDump   mDummyDump; // default area for dump in case setDump() is not called
80*ec779b8eSAndroid Build Coastguard Worker };
81*ec779b8eSAndroid Build Coastguard Worker 
82*ec779b8eSAndroid Build Coastguard Worker }   // namespace android
83*ec779b8eSAndroid Build Coastguard Worker 
84