blob: de6ac9e868fc45b4c46f2acf21a1f522da3588bb [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
Ravneet Dhanjal3c782742023-01-07 00:38:04 +000043#define WATCH(toMonitor) watchThread([&]() { return toMonitor;}, gettid(), __FUNCTION__)
Ravneetdbd5b242022-03-02 07:22:46 +000044#define WATCH_CUSTOM_TIMER(toMonitor, cycles, cycleLength) \
Ravneet Dhanjal3c782742023-01-07 00:38:04 +000045 watchThread([&]() { return toMonitor;}, gettid(), __FUNCTION__, cycles, cycleLength);
Ravneetdbd5b242022-03-02 07:22:46 +000046
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
Ravneet Dhanjal3c782742023-01-07 00:38:04 +000055struct MonitoredFunction {
56 uint32_t cycles;
57 std::string functionName;
58};
59
Ravneetdbd5b242022-03-02 07:22:46 +000060public:
Shuzhen Wang03fe6232023-02-05 12:41:15 -080061 explicit CameraServiceWatchdog(const String8 &cameraId,
62 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper) :
63 mCameraId(cameraId), mPause(true), mMaxCycles(kMaxCycles),
64 mCycleLengthMs(kCycleLengthMs), mEnabled(true),
65 mCameraServiceProxyWrapper(cameraServiceProxyWrapper) {};
Ravneetdbd5b242022-03-02 07:22:46 +000066
Shuzhen Wang03fe6232023-02-05 12:41:15 -080067 explicit CameraServiceWatchdog (const String8 &cameraId, size_t maxCycles,
68 uint32_t cycleLengthMs, bool enabled,
69 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper) :
70 mCameraId(cameraId), mPause(true), mMaxCycles(maxCycles),
71 mCycleLengthMs(cycleLengthMs), mEnabled(enabled),
72 mCameraServiceProxyWrapper(cameraServiceProxyWrapper) {};
Ravneetdbd5b242022-03-02 07:22:46 +000073
74 virtual ~CameraServiceWatchdog() {};
75
76 virtual void requestExit();
77
Ravneetaeb20dc2022-03-30 05:33:03 +000078 /** Enables/disables the watchdog */
79 void setEnabled(bool enable);
80
Ravneetdbd5b242022-03-02 07:22:46 +000081 /** Used to wrap monitored calls in start and stop functions using custom timer values */
82 template<typename T>
Ravneet Dhanjal3c782742023-01-07 00:38:04 +000083 auto watchThread(T func, uint32_t tid, const char* functionName, uint32_t cycles,
84 uint32_t cycleLength) {
Jayant Chowdhary620763f2022-05-27 05:37:14 +000085 decltype(func()) res;
Ravneetdbd5b242022-03-02 07:22:46 +000086
87 if (cycles != mMaxCycles || cycleLength != mCycleLengthMs) {
88 // Create another instance of the watchdog to prevent disruption
89 // of timer for current monitored calls
Ravneetaeb20dc2022-03-30 05:33:03 +000090
91 // Lock for mEnabled
92 mEnabledLock.lock();
Shuzhen Wang03fe6232023-02-05 12:41:15 -080093 sp<CameraServiceWatchdog> tempWatchdog = new CameraServiceWatchdog(
94 mCameraId, cycles, cycleLength, mEnabled, mCameraServiceProxyWrapper);
Ravneetaeb20dc2022-03-30 05:33:03 +000095 mEnabledLock.unlock();
96
Austin Borger7b129542022-06-09 13:23:06 -070097 status_t status = tempWatchdog->run("CameraServiceWatchdog");
98 if (status != OK) {
99 ALOGE("Unable to watch thread: %s (%d)", strerror(-status), status);
Ravneet Dhanjal3c782742023-01-07 00:38:04 +0000100 res = watchThread(func, tid, functionName);
Austin Borger7b129542022-06-09 13:23:06 -0700101 return res;
102 }
103
Ravneet Dhanjal3c782742023-01-07 00:38:04 +0000104 res = tempWatchdog->watchThread(func, tid, functionName);
Ravneetdbd5b242022-03-02 07:22:46 +0000105 tempWatchdog->requestExit();
106 tempWatchdog.clear();
107 } else {
108 // If custom timer values are equivalent to set class timer values, use
109 // current thread
Ravneet Dhanjal3c782742023-01-07 00:38:04 +0000110 res = watchThread(func, tid, functionName);
Ravneetdbd5b242022-03-02 07:22:46 +0000111 }
112
113 return res;
114 }
115
116 /** Used to wrap monitored calls in start and stop functions using class timer values */
117 template<typename T>
Ravneet Dhanjal3c782742023-01-07 00:38:04 +0000118 auto watchThread(T func, uint32_t tid, const char* functionName) {
Jayant Chowdhary620763f2022-05-27 05:37:14 +0000119 decltype(func()) res;
Ravneetaeb20dc2022-03-30 05:33:03 +0000120 AutoMutex _l(mEnabledLock);
121
Ravneetaeb20dc2022-03-30 05:33:03 +0000122 if (mEnabled) {
Ravneet Dhanjal3c782742023-01-07 00:38:04 +0000123 start(tid, functionName);
Ravneetaeb20dc2022-03-30 05:33:03 +0000124 res = func();
125 stop(tid);
126 } else {
127 res = func();
128 }
Ravneetdbd5b242022-03-02 07:22:46 +0000129
130 return res;
131 }
132
133private:
134
135 /**
136 * Start adds a cycle counter for the calling thread. When threadloop is blocked/paused,
137 * start() unblocks and starts the watchdog
138 */
Ravneet Dhanjal3c782742023-01-07 00:38:04 +0000139 void start(uint32_t tid, const char* functionName);
Ravneetdbd5b242022-03-02 07:22:46 +0000140
141 /**
142 * If there are no calls left to be monitored, stop blocks/pauses threadloop
143 * otherwise stop() erases the cycle counter to end watchdog for the calling thread
144 */
145 void stop(uint32_t tid);
146
Ravneet62455912023-04-25 01:30:23 +0000147 std::string getAbortMessage(const std::string& functionName);
Ravneet Dhanjal3c782742023-01-07 00:38:04 +0000148
Ravneetdbd5b242022-03-02 07:22:46 +0000149 virtual bool threadLoop();
150
Ravneetaeb20dc2022-03-30 05:33:03 +0000151 Mutex mWatchdogLock; // Lock for condition variable
152 Mutex mEnabledLock; // Lock for enabled status
153 Condition mWatchdogCondition; // Condition variable for stop/start
Shuzhen Wang03fe6232023-02-05 12:41:15 -0800154 String8 mCameraId; // Camera Id the watchdog belongs to
Ravneetaeb20dc2022-03-30 05:33:03 +0000155 bool mPause; // True if tid map is empty
156 uint32_t mMaxCycles; // Max cycles
157 uint32_t mCycleLengthMs; // Length of time elapsed per cycle
158 bool mEnabled; // True if watchdog is enabled
Ravneetdbd5b242022-03-02 07:22:46 +0000159
Shuzhen Wang03fe6232023-02-05 12:41:15 -0800160 std::shared_ptr<CameraServiceProxyWrapper> mCameraServiceProxyWrapper;
161
Ravneet Dhanjal3c782742023-01-07 00:38:04 +0000162 std::unordered_map<uint32_t, MonitoredFunction> mTidMap; // Thread Id to MonitoredFunction type
163 // which retrieves the num of cycles
164 // and name of the function
Ravneetdbd5b242022-03-02 07:22:46 +0000165};
166
167} // namespace android