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