1 /*
2  * Copyright (C) 2018 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 #define LOG_TAG "TimeCheck"
18 
19 #include <utils/Log.h>
20 #include <mediautils/TimeCheck.h>
21 #include <mediautils/EventLog.h>
22 #include "debuggerd/handler.h"
23 
24 namespace android {
25 
26 // Audio HAL server pids vector used to generate audio HAL processes tombstone
27 // when audioserver watchdog triggers.
28 // We use a lockless storage to avoid potential deadlocks in the context of watchdog
29 // trigger.
30 // Protection again simultaneous writes is not needed given one update takes place
31 // during AudioFlinger construction and other comes necessarily later once the IAudioFlinger
32 // interface is available.
33 // The use of an atomic index just guaranties that current vector is fully initialized
34 // when read.
35 /* static */
accessAudioHalPids(std::vector<pid_t> * pids,bool update)36 void TimeCheck::accessAudioHalPids(std::vector<pid_t>* pids, bool update) {
37     static constexpr int kNumAudioHalPidsVectors = 3;
38     static std::vector<pid_t> audioHalPids[kNumAudioHalPidsVectors];
39     static std::atomic<int> curAudioHalPids = 0;
40 
41     if (update) {
42         audioHalPids[(curAudioHalPids + 1) % kNumAudioHalPidsVectors] = *pids;
43         curAudioHalPids++;
44     } else {
45         *pids = audioHalPids[curAudioHalPids];
46     }
47 }
48 
49 /* static */
setAudioHalPids(const std::vector<pid_t> & pids)50 void TimeCheck::setAudioHalPids(const std::vector<pid_t>& pids) {
51     accessAudioHalPids(&(const_cast<std::vector<pid_t>&>(pids)), true);
52 }
53 
54 /* static */
getAudioHalPids()55 std::vector<pid_t> TimeCheck::getAudioHalPids() {
56     std::vector<pid_t> pids;
57     accessAudioHalPids(&pids, false);
58     return pids;
59 }
60 
61 /* static */
getTimeCheckThread()62 sp<TimeCheck::TimeCheckThread> TimeCheck::getTimeCheckThread()
63 {
64     static sp<TimeCheck::TimeCheckThread> sTimeCheckThread = new TimeCheck::TimeCheckThread();
65     return sTimeCheckThread;
66 }
67 
TimeCheck(const char * tag,uint32_t timeoutMs)68 TimeCheck::TimeCheck(const char *tag, uint32_t timeoutMs)
69     : mEndTimeNs(getTimeCheckThread()->startMonitoring(tag, timeoutMs))
70 {
71 }
72 
~TimeCheck()73 TimeCheck::~TimeCheck() {
74     getTimeCheckThread()->stopMonitoring(mEndTimeNs);
75 }
76 
~TimeCheckThread()77 TimeCheck::TimeCheckThread::~TimeCheckThread()
78 {
79     AutoMutex _l(mMutex);
80     requestExit();
81     mMonitorRequests.clear();
82     mCond.signal();
83 }
84 
startMonitoring(const char * tag,uint32_t timeoutMs)85 nsecs_t TimeCheck::TimeCheckThread::startMonitoring(const char *tag, uint32_t timeoutMs) {
86     Mutex::Autolock _l(mMutex);
87     nsecs_t endTimeNs = systemTime() + milliseconds(timeoutMs);
88     for (; mMonitorRequests.indexOfKey(endTimeNs) >= 0; ++endTimeNs);
89     mMonitorRequests.add(endTimeNs, tag);
90     mCond.signal();
91     return endTimeNs;
92 }
93 
stopMonitoring(nsecs_t endTimeNs)94 void TimeCheck::TimeCheckThread::stopMonitoring(nsecs_t endTimeNs) {
95     Mutex::Autolock _l(mMutex);
96     mMonitorRequests.removeItem(endTimeNs);
97     mCond.signal();
98 }
99 
threadLoop()100 bool TimeCheck::TimeCheckThread::threadLoop()
101 {
102     status_t status = TIMED_OUT;
103     {
104         AutoMutex _l(mMutex);
105 
106         if (exitPending()) {
107             return false;
108         }
109 
110         nsecs_t endTimeNs = INT64_MAX;
111         const char *tag = "<unspecified>";
112         // KeyedVector mMonitorRequests is ordered so take first entry as next timeout
113         if (mMonitorRequests.size() != 0) {
114             endTimeNs = mMonitorRequests.keyAt(0);
115             tag = mMonitorRequests.valueAt(0);
116         }
117 
118         const nsecs_t waitTimeNs = endTimeNs - systemTime();
119         if (waitTimeNs > 0) {
120             status = mCond.waitRelative(mMutex, waitTimeNs);
121         }
122         if (status != NO_ERROR) {
123             // Generate audio HAL processes tombstones and allow time to complete
124             // before forcing restart
125             std::vector<pid_t> pids = getAudioHalPids();
126             if (pids.size() != 0) {
127                 for (const auto& pid : pids) {
128                     ALOGI("requesting tombstone for pid: %d", pid);
129                     sigqueue(pid, DEBUGGER_SIGNAL, {.sival_int = 0});
130                 }
131                 sleep(1);
132             } else {
133                 ALOGI("No HAL process pid available, skipping tombstones");
134             }
135             LOG_EVENT_STRING(LOGTAG_AUDIO_BINDER_TIMEOUT, tag);
136             LOG_ALWAYS_FATAL("TimeCheck timeout for %s", tag);
137         }
138     }
139     return true;
140 }
141 
142 }; // namespace android
143