blob: e802ea38fd0b2befa49af8ca015adc05f15a6d8c [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) {
46 std::string abortMessage = getAbortMessage(getpid(), currentThreadId,
47 mTidMap[currentThreadId].functionName);
48 android_set_abort_message(abortMessage.c_str());
Ravneetce580c42022-09-13 04:38:15 +000049 ALOGW("CameraServiceWatchdog triggering abort for pid: %d tid: %d", getpid(),
50 currentThreadId);
Shuzhen Wang03fe6232023-02-05 12:41:15 -080051 mCameraServiceProxyWrapper->logClose(mCameraId, 0 /*latencyMs*/,
52 true /*deviceError*/);
Jayant Chowdhary620763f2022-05-27 05:37:14 +000053 // We use abort here so we can get a tombstone for better
54 // debugging.
55 abort();
Ravneetdbd5b242022-03-02 07:22:46 +000056 }
57 }
58 }
59
60 return true;
61}
62
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000063std::string CameraServiceWatchdog::getAbortMessage(int pid, int tid, const char* functionName) {
64 std::string res = "CameraServiceWatchdog triggering abort during "
65 + std::string(functionName) + " | pid: " + std::to_string(pid)
66 + " tid: " + std::to_string(tid);
67 return res;
68}
69
Ravneetdbd5b242022-03-02 07:22:46 +000070void CameraServiceWatchdog::requestExit()
71{
72 Thread::requestExit();
73
74 AutoMutex _l(mWatchdogLock);
75
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000076 mTidMap.clear();
Ravneetdbd5b242022-03-02 07:22:46 +000077
78 if (mPause) {
79 mPause = false;
80 mWatchdogCondition.signal();
81 }
82}
83
Ravneetaeb20dc2022-03-30 05:33:03 +000084void CameraServiceWatchdog::setEnabled(bool enable)
85{
86 AutoMutex _l(mEnabledLock);
87
88 if (enable) {
89 mEnabled = true;
90 } else {
91 mEnabled = false;
92 }
93}
94
Ravneetdbd5b242022-03-02 07:22:46 +000095void CameraServiceWatchdog::stop(uint32_t tid)
96{
97 AutoMutex _l(mWatchdogLock);
98
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000099 mTidMap.erase(tid);
Ravneetdbd5b242022-03-02 07:22:46 +0000100
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +0000101 if (mTidMap.empty()) {
Ravneetdbd5b242022-03-02 07:22:46 +0000102 mPause = true;
103 }
104}
105
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +0000106void CameraServiceWatchdog::start(uint32_t tid, const char* functionName)
Ravneetdbd5b242022-03-02 07:22:46 +0000107{
108 AutoMutex _l(mWatchdogLock);
109
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +0000110 MonitoredFunction monitoredFunction = {};
111 monitoredFunction.cycles = 0;
112 monitoredFunction.functionName = functionName;
113 mTidMap[tid] = monitoredFunction;
Ravneetdbd5b242022-03-02 07:22:46 +0000114
115 if (mPause) {
116 mPause = false;
117 mWatchdogCondition.signal();
118 }
119}
120
121} // namespace android