blob: e80064ab98e21caeb294b77f1e99df5e91331fe1 [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"
Shuzhen Wang03fe6232023-02-05 12:41:15 -080020#include "utils/CameraServiceProxyWrapper.h"
Ravneetdbd5b242022-03-02 07:22:46 +000021
22namespace android {
23
24bool CameraServiceWatchdog::threadLoop()
25{
26 {
27 AutoMutex _l(mWatchdogLock);
28
29 while (mPause) {
30 mWatchdogCondition.wait(mWatchdogLock);
31 }
32 }
33
34 std::this_thread::sleep_for(std::chrono::milliseconds(mCycleLengthMs));
35
36 {
37 AutoMutex _l(mWatchdogLock);
38
39 for (auto it = tidToCycleCounterMap.begin(); it != tidToCycleCounterMap.end(); it++) {
40 uint32_t currentThreadId = it->first;
41
42 tidToCycleCounterMap[currentThreadId]++;
43
44 if (tidToCycleCounterMap[currentThreadId] >= mMaxCycles) {
Ravneetce580c42022-09-13 04:38:15 +000045 ALOGW("CameraServiceWatchdog triggering abort for pid: %d tid: %d", getpid(),
46 currentThreadId);
Shuzhen Wang03fe6232023-02-05 12:41:15 -080047 mCameraServiceProxyWrapper->logClose(mCameraId, 0 /*latencyMs*/,
48 true /*deviceError*/);
Jayant Chowdhary620763f2022-05-27 05:37:14 +000049 // We use abort here so we can get a tombstone for better
50 // debugging.
51 abort();
Ravneetdbd5b242022-03-02 07:22:46 +000052 }
53 }
54 }
55
56 return true;
57}
58
59void CameraServiceWatchdog::requestExit()
60{
61 Thread::requestExit();
62
63 AutoMutex _l(mWatchdogLock);
64
65 tidToCycleCounterMap.clear();
66
67 if (mPause) {
68 mPause = false;
69 mWatchdogCondition.signal();
70 }
71}
72
Ravneetaeb20dc2022-03-30 05:33:03 +000073void CameraServiceWatchdog::setEnabled(bool enable)
74{
75 AutoMutex _l(mEnabledLock);
76
77 if (enable) {
78 mEnabled = true;
79 } else {
80 mEnabled = false;
81 }
82}
83
Ravneetdbd5b242022-03-02 07:22:46 +000084void CameraServiceWatchdog::stop(uint32_t tid)
85{
86 AutoMutex _l(mWatchdogLock);
87
88 tidToCycleCounterMap.erase(tid);
89
90 if (tidToCycleCounterMap.empty()) {
91 mPause = true;
92 }
93}
94
95void CameraServiceWatchdog::start(uint32_t tid)
96{
97 AutoMutex _l(mWatchdogLock);
98
99 tidToCycleCounterMap[tid] = 0;
100
101 if (mPause) {
102 mPause = false;
103 mWatchdogCondition.signal();
104 }
105}
106
107} // namespace android