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" |
| 20 | |
| 21 | namespace android { |
| 22 | |
| 23 | bool CameraServiceWatchdog::threadLoop() |
| 24 | { |
| 25 | { |
| 26 | AutoMutex _l(mWatchdogLock); |
| 27 | |
| 28 | while (mPause) { |
| 29 | mWatchdogCondition.wait(mWatchdogLock); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | std::this_thread::sleep_for(std::chrono::milliseconds(mCycleLengthMs)); |
| 34 | |
| 35 | { |
| 36 | AutoMutex _l(mWatchdogLock); |
| 37 | |
| 38 | for (auto it = tidToCycleCounterMap.begin(); it != tidToCycleCounterMap.end(); it++) { |
| 39 | uint32_t currentThreadId = it->first; |
| 40 | |
| 41 | tidToCycleCounterMap[currentThreadId]++; |
| 42 | |
| 43 | if (tidToCycleCounterMap[currentThreadId] >= mMaxCycles) { |
Jayant Chowdhary | 620763f | 2022-05-27 05:37:14 +0000 | [diff] [blame] | 44 | ALOGW("CameraServiceWatchdog triggering abort for pid: %d", getpid()); |
| 45 | // We use abort here so we can get a tombstone for better |
| 46 | // debugging. |
| 47 | abort(); |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | void CameraServiceWatchdog::requestExit() |
| 56 | { |
| 57 | Thread::requestExit(); |
| 58 | |
| 59 | AutoMutex _l(mWatchdogLock); |
| 60 | |
| 61 | tidToCycleCounterMap.clear(); |
| 62 | |
| 63 | if (mPause) { |
| 64 | mPause = false; |
| 65 | mWatchdogCondition.signal(); |
| 66 | } |
| 67 | } |
| 68 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 69 | void CameraServiceWatchdog::setEnabled(bool enable) |
| 70 | { |
| 71 | AutoMutex _l(mEnabledLock); |
| 72 | |
| 73 | if (enable) { |
| 74 | mEnabled = true; |
| 75 | } else { |
| 76 | mEnabled = false; |
| 77 | } |
| 78 | } |
| 79 | |
Ravneet | dbd5b24 | 2022-03-02 07:22:46 +0000 | [diff] [blame] | 80 | void CameraServiceWatchdog::stop(uint32_t tid) |
| 81 | { |
| 82 | AutoMutex _l(mWatchdogLock); |
| 83 | |
| 84 | tidToCycleCounterMap.erase(tid); |
| 85 | |
| 86 | if (tidToCycleCounterMap.empty()) { |
| 87 | mPause = true; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void CameraServiceWatchdog::start(uint32_t tid) |
| 92 | { |
| 93 | AutoMutex _l(mWatchdogLock); |
| 94 | |
| 95 | tidToCycleCounterMap[tid] = 0; |
| 96 | |
| 97 | if (mPause) { |
| 98 | mPause = false; |
| 99 | mWatchdogCondition.signal(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | } // namespace android |