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