blob: 661787347324036a20ef0ef6f44462ad74096614 [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/**
18 * The CameraService watchdog is used to help detect bad states in the
19 * Camera HAL. The threadloop uses cycle counters, assigned to each calling
20 * thread, to monitor the elapsing time and kills the process when the
21 * expected duration has exceeded.
22 * Notes on multi-threaded behaviors:
23 * - The threadloop is blocked/paused when there are no calls being
Ravneetaeb20dc2022-03-30 05:33:03 +000024 * monitored (when the TID cycle to counter map is empty).
Ravneetdbd5b242022-03-02 07:22:46 +000025 * - The start and stop functions handle simultaneous call monitoring
26 * and single call monitoring differently. See function documentation for
27 * more details.
Ravneetaeb20dc2022-03-30 05:33:03 +000028 * To disable/enable:
29 * - adb shell cmd media.camera set-cameraservice-watchdog [0/1]
Ravneetdbd5b242022-03-02 07:22:46 +000030 */
Jayant Chowdhary620763f2022-05-27 05:37:14 +000031#pragma once
Ravneetdbd5b242022-03-02 07:22:46 +000032#include <chrono>
33#include <thread>
34#include <time.h>
Shuzhen Wang03fe6232023-02-05 12:41:15 -080035#include <utils/String8.h>
Ravneetdbd5b242022-03-02 07:22:46 +000036#include <utils/Thread.h>
37#include <utils/Log.h>
38#include <unordered_map>
39
Shuzhen Wang03fe6232023-02-05 12:41:15 -080040#include "utils/CameraServiceProxyWrapper.h"
41
Ravneetdbd5b242022-03-02 07:22:46 +000042// Used to wrap the call of interest in start and stop calls
43#define WATCH(toMonitor) watchThread([&]() { return toMonitor;}, gettid())
44#define WATCH_CUSTOM_TIMER(toMonitor, cycles, cycleLength) \
45 watchThread([&]() { return toMonitor;}, gettid(), cycles, cycleLength);
46
47// Default cycles and cycle length values used to calculate permitted elapsed time
48const static size_t kMaxCycles = 100;
49const static uint32_t kCycleLengthMs = 100;
50
51namespace android {
52
53class CameraServiceWatchdog : public Thread {
54
55public:
Shuzhen Wang03fe6232023-02-05 12:41:15 -080056 explicit CameraServiceWatchdog(const String8 &cameraId,
57 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper) :
58 mCameraId(cameraId), mPause(true), mMaxCycles(kMaxCycles),
59 mCycleLengthMs(kCycleLengthMs), mEnabled(true),
60 mCameraServiceProxyWrapper(cameraServiceProxyWrapper) {};
Ravneetdbd5b242022-03-02 07:22:46 +000061
Shuzhen Wang03fe6232023-02-05 12:41:15 -080062 explicit CameraServiceWatchdog (const String8 &cameraId, size_t maxCycles,
63 uint32_t cycleLengthMs, bool enabled,
64 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper) :
65 mCameraId(cameraId), mPause(true), mMaxCycles(maxCycles),
66 mCycleLengthMs(cycleLengthMs), mEnabled(enabled),
67 mCameraServiceProxyWrapper(cameraServiceProxyWrapper) {};
Ravneetdbd5b242022-03-02 07:22:46 +000068
69 virtual ~CameraServiceWatchdog() {};
70
71 virtual void requestExit();
72
Ravneetaeb20dc2022-03-30 05:33:03 +000073 /** Enables/disables the watchdog */
74 void setEnabled(bool enable);
75
Ravneetdbd5b242022-03-02 07:22:46 +000076 /** Used to wrap monitored calls in start and stop functions using custom timer values */
77 template<typename T>
78 auto watchThread(T func, uint32_t tid, uint32_t cycles, uint32_t cycleLength) {
Jayant Chowdhary620763f2022-05-27 05:37:14 +000079 decltype(func()) res;
Ravneetdbd5b242022-03-02 07:22:46 +000080
81 if (cycles != mMaxCycles || cycleLength != mCycleLengthMs) {
82 // Create another instance of the watchdog to prevent disruption
83 // of timer for current monitored calls
Ravneetaeb20dc2022-03-30 05:33:03 +000084
85 // Lock for mEnabled
86 mEnabledLock.lock();
Shuzhen Wang03fe6232023-02-05 12:41:15 -080087 sp<CameraServiceWatchdog> tempWatchdog = new CameraServiceWatchdog(
88 mCameraId, cycles, cycleLength, mEnabled, mCameraServiceProxyWrapper);
Ravneetaeb20dc2022-03-30 05:33:03 +000089 mEnabledLock.unlock();
90
Austin Borger7b129542022-06-09 13:23:06 -070091 status_t status = tempWatchdog->run("CameraServiceWatchdog");
92 if (status != OK) {
93 ALOGE("Unable to watch thread: %s (%d)", strerror(-status), status);
94 res = watchThread(func, tid);
95 return res;
96 }
97
Ravneetdbd5b242022-03-02 07:22:46 +000098 res = tempWatchdog->watchThread(func, tid);
99 tempWatchdog->requestExit();
100 tempWatchdog.clear();
101 } else {
102 // If custom timer values are equivalent to set class timer values, use
103 // current thread
104 res = watchThread(func, tid);
105 }
106
107 return res;
108 }
109
110 /** Used to wrap monitored calls in start and stop functions using class timer values */
111 template<typename T>
112 auto watchThread(T func, uint32_t tid) {
Jayant Chowdhary620763f2022-05-27 05:37:14 +0000113 decltype(func()) res;
Ravneetaeb20dc2022-03-30 05:33:03 +0000114 AutoMutex _l(mEnabledLock);
115
Ravneetaeb20dc2022-03-30 05:33:03 +0000116 if (mEnabled) {
117 start(tid);
118 res = func();
119 stop(tid);
120 } else {
121 res = func();
122 }
Ravneetdbd5b242022-03-02 07:22:46 +0000123
124 return res;
125 }
126
127private:
128
129 /**
130 * Start adds a cycle counter for the calling thread. When threadloop is blocked/paused,
131 * start() unblocks and starts the watchdog
132 */
133 void start(uint32_t tid);
134
135 /**
136 * If there are no calls left to be monitored, stop blocks/pauses threadloop
137 * otherwise stop() erases the cycle counter to end watchdog for the calling thread
138 */
139 void stop(uint32_t tid);
140
141 virtual bool threadLoop();
142
Ravneetaeb20dc2022-03-30 05:33:03 +0000143 Mutex mWatchdogLock; // Lock for condition variable
144 Mutex mEnabledLock; // Lock for enabled status
145 Condition mWatchdogCondition; // Condition variable for stop/start
Shuzhen Wang03fe6232023-02-05 12:41:15 -0800146 String8 mCameraId; // Camera Id the watchdog belongs to
Ravneetaeb20dc2022-03-30 05:33:03 +0000147 bool mPause; // True if tid map is empty
148 uint32_t mMaxCycles; // Max cycles
149 uint32_t mCycleLengthMs; // Length of time elapsed per cycle
150 bool mEnabled; // True if watchdog is enabled
Ravneetdbd5b242022-03-02 07:22:46 +0000151
Shuzhen Wang03fe6232023-02-05 12:41:15 -0800152 std::shared_ptr<CameraServiceProxyWrapper> mCameraServiceProxyWrapper;
153
Ravneetdbd5b242022-03-02 07:22:46 +0000154 std::unordered_map<uint32_t, uint32_t> tidToCycleCounterMap; // Thread Id to cycle counter map
155};
156
157} // namespace android