blob: 1c1bd246bb4e774f55f9f1f44362c1ffa28eee37 [file] [log] [blame]
Ravneetdbd5b242022-03-02 07:22:46 +00001/*
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"
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000020#include "android/set_abort_message.h"
Shuzhen Wang03fe6232023-02-05 12:41:15 -080021#include "utils/CameraServiceProxyWrapper.h"
Ravneetdbd5b242022-03-02 07:22:46 +000022
23namespace android {
24
25bool 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
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000040 for (auto it = mTidMap.begin(); it != mTidMap.end(); it++) {
Ravneetdbd5b242022-03-02 07:22:46 +000041 uint32_t currentThreadId = it->first;
42
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000043 mTidMap[currentThreadId].cycles++;
Ravneetdbd5b242022-03-02 07:22:46 +000044
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000045 if (mTidMap[currentThreadId].cycles >= mMaxCycles) {
Ravneeta925d0d2023-04-25 01:30:23 +000046 std::string abortMessage = getAbortMessage(mTidMap[currentThreadId].functionName);
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000047 android_set_abort_message(abortMessage.c_str());
Ravneetce580c42022-09-13 04:38:15 +000048 ALOGW("CameraServiceWatchdog triggering abort for pid: %d tid: %d", getpid(),
49 currentThreadId);
Shuzhen Wang03fe6232023-02-05 12:41:15 -080050 mCameraServiceProxyWrapper->logClose(mCameraId, 0 /*latencyMs*/,
51 true /*deviceError*/);
Jayant Chowdhary620763f2022-05-27 05:37:14 +000052 // We use abort here so we can get a tombstone for better
53 // debugging.
54 abort();
Ravneetdbd5b242022-03-02 07:22:46 +000055 }
56 }
57 }
58
59 return true;
60}
61
Ravneeta925d0d2023-04-25 01:30:23 +000062std::string CameraServiceWatchdog::getAbortMessage(const std::string& functionName) {
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000063 std::string res = "CameraServiceWatchdog triggering abort during "
Ravneeta925d0d2023-04-25 01:30:23 +000064 + functionName;
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000065 return res;
66}
67
Ravneetdbd5b242022-03-02 07:22:46 +000068void CameraServiceWatchdog::requestExit()
69{
70 Thread::requestExit();
71
72 AutoMutex _l(mWatchdogLock);
73
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000074 mTidMap.clear();
Ravneetdbd5b242022-03-02 07:22:46 +000075
76 if (mPause) {
77 mPause = false;
78 mWatchdogCondition.signal();
79 }
80}
81
Ravneetaeb20dc2022-03-30 05:33:03 +000082void CameraServiceWatchdog::setEnabled(bool enable)
83{
84 AutoMutex _l(mEnabledLock);
85
86 if (enable) {
87 mEnabled = true;
88 } else {
89 mEnabled = false;
90 }
91}
92
Ravneetdbd5b242022-03-02 07:22:46 +000093void CameraServiceWatchdog::stop(uint32_t tid)
94{
95 AutoMutex _l(mWatchdogLock);
96
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000097 mTidMap.erase(tid);
Ravneetdbd5b242022-03-02 07:22:46 +000098
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000099 if (mTidMap.empty()) {
Ravneetdbd5b242022-03-02 07:22:46 +0000100 mPause = true;
101 }
102}
103
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +0000104void CameraServiceWatchdog::start(uint32_t tid, const char* functionName)
Ravneetdbd5b242022-03-02 07:22:46 +0000105{
106 AutoMutex _l(mWatchdogLock);
107
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +0000108 MonitoredFunction monitoredFunction = {};
109 monitoredFunction.cycles = 0;
110 monitoredFunction.functionName = functionName;
111 mTidMap[tid] = monitoredFunction;
Ravneetdbd5b242022-03-02 07:22:46 +0000112
113 if (mPause) {
114 mPause = false;
115 mWatchdogCondition.signal();
116 }
117}
118
119} // namespace android