blob: 74497d1a696860865d1fef834d3e50793a8e7b08 [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"
20
21namespace android {
22
23bool 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) {
Ravneetce580c42022-09-13 04:38:15 +000044 ALOGW("CameraServiceWatchdog triggering abort for pid: %d tid: %d", getpid(),
45 currentThreadId);
Jayant Chowdhary620763f2022-05-27 05:37:14 +000046 // We use abort here so we can get a tombstone for better
47 // debugging.
48 abort();
Ravneetdbd5b242022-03-02 07:22:46 +000049 }
50 }
51 }
52
53 return true;
54}
55
56void CameraServiceWatchdog::requestExit()
57{
58 Thread::requestExit();
59
60 AutoMutex _l(mWatchdogLock);
61
62 tidToCycleCounterMap.clear();
63
64 if (mPause) {
65 mPause = false;
66 mWatchdogCondition.signal();
67 }
68}
69
Ravneetaeb20dc2022-03-30 05:33:03 +000070void CameraServiceWatchdog::setEnabled(bool enable)
71{
72 AutoMutex _l(mEnabledLock);
73
74 if (enable) {
75 mEnabled = true;
76 } else {
77 mEnabled = false;
78 }
79}
80
Ravneetdbd5b242022-03-02 07:22:46 +000081void CameraServiceWatchdog::stop(uint32_t tid)
82{
83 AutoMutex _l(mWatchdogLock);
84
85 tidToCycleCounterMap.erase(tid);
86
87 if (tidToCycleCounterMap.empty()) {
88 mPause = true;
89 }
90}
91
92void CameraServiceWatchdog::start(uint32_t tid)
93{
94 AutoMutex _l(mWatchdogLock);
95
96 tidToCycleCounterMap[tid] = 0;
97
98 if (mPause) {
99 mPause = false;
100 mWatchdogCondition.signal();
101 }
102}
103
104} // namespace android