Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 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" |
Ravneet Dhanjal | edb2d0b | 2023-01-07 00:38:04 +0000 | [diff] [blame] | 20 | #include "android/set_abort_message.h" |
Shuzhen Wang | 03fe623 | 2023-02-05 12:41:15 -0800 | [diff] [blame] | 21 | #include "utils/CameraServiceProxyWrapper.h" |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | |
| 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 | |
Ravneet Dhanjal | edb2d0b | 2023-01-07 00:38:04 +0000 | [diff] [blame] | 40 | for (auto it = mTidMap.begin(); it != mTidMap.end(); it++) { |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 41 | uint32_t currentThreadId = it->first; |
| 42 | |
Ravneet Dhanjal | edb2d0b | 2023-01-07 00:38:04 +0000 | [diff] [blame] | 43 | mTidMap[currentThreadId].cycles++; |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 44 | |
Ravneet Dhanjal | edb2d0b | 2023-01-07 00:38:04 +0000 | [diff] [blame] | 45 | if (mTidMap[currentThreadId].cycles >= mMaxCycles) { |
| 46 | std::string abortMessage = getAbortMessage(getpid(), currentThreadId, |
| 47 | mTidMap[currentThreadId].functionName); |
| 48 | android_set_abort_message(abortMessage.c_str()); |
Ravneet | ce580c4 | 2022-09-13 04:38:15 +0000 | [diff] [blame] | 49 | ALOGW("CameraServiceWatchdog triggering abort for pid: %d tid: %d", getpid(), |
| 50 | currentThreadId); |
Shuzhen Wang | 03fe623 | 2023-02-05 12:41:15 -0800 | [diff] [blame] | 51 | mCameraServiceProxyWrapper->logClose(mCameraId, 0 /*latencyMs*/, |
| 52 | true /*deviceError*/); |
Jayant Chowdhary | 620763f | 2022-05-27 05:37:14 +0000 | [diff] [blame] | 53 | // We use abort here so we can get a tombstone for better |
| 54 | // debugging. |
| 55 | abort(); |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
Ravneet Dhanjal | edb2d0b | 2023-01-07 00:38:04 +0000 | [diff] [blame] | 63 | std::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 | |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 70 | void CameraServiceWatchdog::requestExit() |
| 71 | { |
| 72 | Thread::requestExit(); |
| 73 | |
| 74 | AutoMutex _l(mWatchdogLock); |
| 75 | |
Ravneet Dhanjal | edb2d0b | 2023-01-07 00:38:04 +0000 | [diff] [blame] | 76 | mTidMap.clear(); |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 77 | |
| 78 | if (mPause) { |
| 79 | mPause = false; |
| 80 | mWatchdogCondition.signal(); |
| 81 | } |
| 82 | } |
| 83 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 84 | void CameraServiceWatchdog::setEnabled(bool enable) |
| 85 | { |
| 86 | AutoMutex _l(mEnabledLock); |
| 87 | |
| 88 | if (enable) { |
| 89 | mEnabled = true; |
| 90 | } else { |
| 91 | mEnabled = false; |
| 92 | } |
| 93 | } |
| 94 | |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 95 | void CameraServiceWatchdog::stop(uint32_t tid) |
| 96 | { |
| 97 | AutoMutex _l(mWatchdogLock); |
| 98 | |
Ravneet Dhanjal | edb2d0b | 2023-01-07 00:38:04 +0000 | [diff] [blame] | 99 | mTidMap.erase(tid); |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 100 | |
Ravneet Dhanjal | edb2d0b | 2023-01-07 00:38:04 +0000 | [diff] [blame] | 101 | if (mTidMap.empty()) { |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 102 | mPause = true; |
| 103 | } |
| 104 | } |
| 105 | |
Ravneet Dhanjal | edb2d0b | 2023-01-07 00:38:04 +0000 | [diff] [blame] | 106 | void CameraServiceWatchdog::start(uint32_t tid, const char* functionName) |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 107 | { |
| 108 | AutoMutex _l(mWatchdogLock); |
| 109 | |
Ravneet Dhanjal | edb2d0b | 2023-01-07 00:38:04 +0000 | [diff] [blame] | 110 | MonitoredFunction monitoredFunction = {}; |
| 111 | monitoredFunction.cycles = 0; |
| 112 | monitoredFunction.functionName = functionName; |
| 113 | mTidMap[tid] = monitoredFunction; |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 114 | |
| 115 | if (mPause) { |
| 116 | mPause = false; |
| 117 | mWatchdogCondition.signal(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | } // namespace android |