1 /*
2 * Copyright (C) 2022 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 "CameraServiceWatchdog"
18
19 #include "CameraServiceWatchdog.h"
20 #include "android/set_abort_message.h"
21 #include "utils/CameraServiceProxyWrapper.h"
22
23 namespace android {
24
threadLoop()25 bool CameraServiceWatchdog::threadLoop()
26 {
27 {
28 AutoMutex _l(mWatchdogLock);
29
30 while (mPause) {
31 mWatchdogCondition.wait(mWatchdogLock);
32 }
33 }
34
35 std::this_thread::sleep_for(std::chrono::milliseconds(mCycleLengthMs));
36
37 {
38 AutoMutex _l(mWatchdogLock);
39
40 for (auto it = mTidMap.begin(); it != mTidMap.end(); it++) {
41 uint32_t currentThreadId = it->first;
42
43 mTidMap[currentThreadId].cycles++;
44
45 if (mTidMap[currentThreadId].cycles >= mMaxCycles) {
46 std::string abortMessage = getAbortMessage(mTidMap[currentThreadId].functionName);
47 android_set_abort_message(abortMessage.c_str());
48 ALOGW("CameraServiceWatchdog triggering abort for pid: %d tid: %d", getpid(),
49 currentThreadId);
50 mCameraServiceProxyWrapper->logClose(mCameraId, 0 /*latencyMs*/,
51 true /*deviceError*/);
52 // We use abort here so we can get a tombstone for better
53 // debugging.
54 abort();
55 }
56 }
57 }
58
59 return true;
60 }
61
getAbortMessage(const std::string & functionName)62 std::string CameraServiceWatchdog::getAbortMessage(const std::string& functionName) {
63 std::string res = "CameraServiceWatchdog triggering abort during "
64 + functionName;
65 return res;
66 }
67
requestExit()68 void CameraServiceWatchdog::requestExit()
69 {
70 Thread::requestExit();
71
72 AutoMutex _l(mWatchdogLock);
73
74 mTidMap.clear();
75
76 if (mPause) {
77 mPause = false;
78 mWatchdogCondition.signal();
79 }
80 }
81
setEnabled(bool enable)82 void CameraServiceWatchdog::setEnabled(bool enable)
83 {
84 AutoMutex _l(mEnabledLock);
85
86 if (enable) {
87 mEnabled = true;
88 } else {
89 mEnabled = false;
90 }
91 }
92
stop(uint32_t tid)93 void CameraServiceWatchdog::stop(uint32_t tid)
94 {
95 AutoMutex _l(mWatchdogLock);
96
97 mTidMap.erase(tid);
98
99 if (mTidMap.empty()) {
100 mPause = true;
101 }
102 }
103
start(uint32_t tid,const char * functionName)104 void CameraServiceWatchdog::start(uint32_t tid, const char* functionName)
105 {
106 AutoMutex _l(mWatchdogLock);
107
108 MonitoredFunction monitoredFunction = {};
109 monitoredFunction.cycles = 0;
110 monitoredFunction.functionName = functionName;
111 mTidMap[tid] = monitoredFunction;
112
113 if (mPause) {
114 mPause = false;
115 mWatchdogCondition.signal();
116 }
117 }
118
119 } // namespace android
120