blob: ad1a84f884fe58a00a75456ce9b69b6d2468eaf0 [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 Dhanjal7f7ab672024-07-18 22:43:04 +000020#include "com_android_internal_camera_flags.h"
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000021#include "android/set_abort_message.h"
Shuzhen Wang03fe6232023-02-05 12:41:15 -080022#include "utils/CameraServiceProxyWrapper.h"
Ravneetdbd5b242022-03-02 07:22:46 +000023
24namespace android {
25
Ravneet Dhanjal7f7ab672024-07-18 22:43:04 +000026namespace flags = com::android::internal::camera::flags;
27
Ravneetdbd5b242022-03-02 07:22:46 +000028bool 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
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000043 for (auto it = mTidMap.begin(); it != mTidMap.end(); it++) {
Ravneetdbd5b242022-03-02 07:22:46 +000044 uint32_t currentThreadId = it->first;
45
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000046 mTidMap[currentThreadId].cycles++;
Ravneetdbd5b242022-03-02 07:22:46 +000047
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000048 if (mTidMap[currentThreadId].cycles >= mMaxCycles) {
Ravneeta925d0d2023-04-25 01:30:23 +000049 std::string abortMessage = getAbortMessage(mTidMap[currentThreadId].functionName);
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000050 android_set_abort_message(abortMessage.c_str());
Ravneetce580c42022-09-13 04:38:15 +000051 ALOGW("CameraServiceWatchdog triggering abort for pid: %d tid: %d", getpid(),
52 currentThreadId);
Shuzhen Wang03fe6232023-02-05 12:41:15 -080053 mCameraServiceProxyWrapper->logClose(mCameraId, 0 /*latencyMs*/,
54 true /*deviceError*/);
Jayant Chowdhary620763f2022-05-27 05:37:14 +000055 // We use abort here so we can get a tombstone for better
56 // debugging.
Ravneet Dhanjal7f7ab672024-07-18 22:43:04 +000057 if (flags::enable_hal_abort_from_cameraservicewatchdog()) {
58 for (pid_t pid : mProviderPids) {
59 kill(pid, SIGABRT);
60 }
61 }
62
Jayant Chowdhary620763f2022-05-27 05:37:14 +000063 abort();
Ravneetdbd5b242022-03-02 07:22:46 +000064 }
65 }
66 }
67
68 return true;
69}
70
Ravneeta925d0d2023-04-25 01:30:23 +000071std::string CameraServiceWatchdog::getAbortMessage(const std::string& functionName) {
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000072 std::string res = "CameraServiceWatchdog triggering abort during "
Ravneeta925d0d2023-04-25 01:30:23 +000073 + functionName;
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000074 return res;
75}
76
Ravneetdbd5b242022-03-02 07:22:46 +000077void CameraServiceWatchdog::requestExit()
78{
79 Thread::requestExit();
80
81 AutoMutex _l(mWatchdogLock);
82
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +000083 mTidMap.clear();
Ravneetdbd5b242022-03-02 07:22:46 +000084
85 if (mPause) {
86 mPause = false;
87 mWatchdogCondition.signal();
88 }
89}
90
Ravneetaeb20dc2022-03-30 05:33:03 +000091void CameraServiceWatchdog::setEnabled(bool enable)
92{
93 AutoMutex _l(mEnabledLock);
94
95 if (enable) {
96 mEnabled = true;
97 } else {
98 mEnabled = false;
99 }
100}
101
Ravneetdbd5b242022-03-02 07:22:46 +0000102void CameraServiceWatchdog::stop(uint32_t tid)
103{
104 AutoMutex _l(mWatchdogLock);
105
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +0000106 mTidMap.erase(tid);
Ravneetdbd5b242022-03-02 07:22:46 +0000107
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +0000108 if (mTidMap.empty()) {
Ravneetdbd5b242022-03-02 07:22:46 +0000109 mPause = true;
110 }
111}
112
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +0000113void CameraServiceWatchdog::start(uint32_t tid, const char* functionName)
Ravneetdbd5b242022-03-02 07:22:46 +0000114{
115 AutoMutex _l(mWatchdogLock);
116
Ravneet Dhanjaledb2d0b2023-01-07 00:38:04 +0000117 MonitoredFunction monitoredFunction = {};
118 monitoredFunction.cycles = 0;
119 monitoredFunction.functionName = functionName;
120 mTidMap[tid] = monitoredFunction;
Ravneetdbd5b242022-03-02 07:22:46 +0000121
122 if (mPause) {
123 mPause = false;
124 mWatchdogCondition.signal();
125 }
126}
127
128} // namespace android