1 /** 2 * Copyright (c) 2020, 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 #pragma once 18 19 #include "IoOveruseMonitor.h" 20 #include "ThreadPriorityController.h" 21 #include "WatchdogPerfService.h" 22 #include "WatchdogProcessService.h" 23 #include "WatchdogServiceHelper.h" 24 25 #include <aidl/android/automotive/watchdog/internal/BnCarWatchdog.h> 26 #include <aidl/android/automotive/watchdog/internal/ComponentType.h> 27 #include <aidl/android/automotive/watchdog/internal/ICarWatchdogMonitor.h> 28 #include <aidl/android/automotive/watchdog/internal/ICarWatchdogServiceForSystem.h> 29 #include <aidl/android/automotive/watchdog/internal/PowerCycle.h> 30 #include <aidl/android/automotive/watchdog/internal/ProcessIdentifier.h> 31 #include <aidl/android/automotive/watchdog/internal/ResourceOveruseConfiguration.h> 32 #include <aidl/android/automotive/watchdog/internal/StateType.h> 33 #include <aidl/android/automotive/watchdog/internal/UserPackageIoUsageStats.h> 34 #include <aidl/android/automotive/watchdog/internal/UserState.h> 35 #include <android/binder_auto_utils.h> 36 #include <gtest/gtest_prod.h> 37 #include <utils/Errors.h> 38 #include <utils/RefBase.h> 39 #include <utils/String16.h> 40 #include <utils/Vector.h> 41 42 namespace android { 43 namespace automotive { 44 namespace watchdog { 45 46 // Forward declaration for testing use only. 47 namespace internal { 48 49 class WatchdogInternalHandlerPeer; 50 51 } // namespace internal 52 53 class WatchdogInternalHandlerInterface : 54 public aidl::android::automotive::watchdog::internal::BnCarWatchdog { 55 public: 56 virtual void terminate() = 0; 57 }; 58 59 class WatchdogInternalHandler final : public WatchdogInternalHandlerInterface { 60 public: WatchdogInternalHandler(const android::sp<WatchdogServiceHelperInterface> & watchdogServiceHelper,const android::sp<WatchdogProcessServiceInterface> & watchdogProcessService,const android::sp<WatchdogPerfServiceInterface> & watchdogPerfService,const android::sp<IoOveruseMonitorInterface> & ioOveruseMonitor)61 WatchdogInternalHandler( 62 const android::sp<WatchdogServiceHelperInterface>& watchdogServiceHelper, 63 const android::sp<WatchdogProcessServiceInterface>& watchdogProcessService, 64 const android::sp<WatchdogPerfServiceInterface>& watchdogPerfService, 65 const android::sp<IoOveruseMonitorInterface>& ioOveruseMonitor) : 66 mWatchdogServiceHelper(watchdogServiceHelper), 67 mWatchdogProcessService(watchdogProcessService), 68 mWatchdogPerfService(watchdogPerfService), 69 mIoOveruseMonitor(ioOveruseMonitor), 70 mThreadPriorityController(std::make_unique<ThreadPriorityController>()) {} ~WatchdogInternalHandler()71 ~WatchdogInternalHandler() { terminate(); } 72 73 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; 74 ndk::ScopedAStatus registerCarWatchdogService( 75 const std::shared_ptr< 76 aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& 77 service) override; 78 ndk::ScopedAStatus unregisterCarWatchdogService( 79 const std::shared_ptr< 80 aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& 81 service) override; 82 ndk::ScopedAStatus registerMonitor( 83 const std::shared_ptr< 84 aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor>& monitor) 85 override; 86 ndk::ScopedAStatus unregisterMonitor( 87 const std::shared_ptr< 88 aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor>& monitor) 89 override; 90 ndk::ScopedAStatus tellCarWatchdogServiceAlive( 91 const std::shared_ptr< 92 aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& 93 service, 94 const std::vector<aidl::android::automotive::watchdog::internal::ProcessIdentifier>& 95 clientsNotResponding, 96 int32_t sessionId) override; 97 ndk::ScopedAStatus tellDumpFinished( 98 const std::shared_ptr< 99 aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor>& monitor, 100 const aidl::android::automotive::watchdog::internal::ProcessIdentifier& 101 processIdentifier) override; 102 ndk::ScopedAStatus notifySystemStateChange( 103 aidl::android::automotive::watchdog::internal::StateType type, int32_t arg1, 104 int32_t arg2) override; 105 ndk::ScopedAStatus updateResourceOveruseConfigurations( 106 const std::vector< 107 aidl::android::automotive::watchdog::internal::ResourceOveruseConfiguration>& 108 configs) override; 109 ndk::ScopedAStatus getResourceOveruseConfigurations( 110 std::vector< 111 aidl::android::automotive::watchdog::internal::ResourceOveruseConfiguration>* 112 configs) override; 113 ndk::ScopedAStatus controlProcessHealthCheck(bool enable) override; 114 ndk::ScopedAStatus setThreadPriority(int pid, int tid, int uid, int policy, 115 int priority) override; 116 ndk::ScopedAStatus getThreadPriority( 117 int pid, int tid, int uid, 118 aidl::android::automotive::watchdog::internal::ThreadPolicyWithPriority* 119 threadPolicyWithPriority) override; 120 ndk::ScopedAStatus onAidlVhalPidFetched(int pid) override; 121 ndk::ScopedAStatus onTodayIoUsageStatsFetched( 122 const std::vector< 123 aidl::android::automotive::watchdog::internal::UserPackageIoUsageStats>& 124 userPackageIoUsageStats) override; 125 terminate()126 void terminate() override { 127 mWatchdogServiceHelper.clear(); 128 mWatchdogProcessService.clear(); 129 mWatchdogPerfService.clear(); 130 mIoOveruseMonitor.clear(); 131 } 132 133 private: 134 status_t dumpServices(int fd); 135 status_t dumpProto(int fd); 136 status_t dumpHelpText(const int fd, const std::string& errorMsg); 137 void checkAndRegisterIoOveruseMonitor(); 138 ndk::ScopedAStatus handlePowerCycleChange( 139 aidl::android::automotive::watchdog::internal::PowerCycle powerCycle); 140 ndk::ScopedAStatus handleUserStateChange( 141 userid_t userId, 142 const aidl::android::automotive::watchdog::internal::UserState& userState); 143 void setThreadPriorityController(std::unique_ptr<ThreadPriorityControllerInterface> controller); 144 145 android::sp<WatchdogServiceHelperInterface> mWatchdogServiceHelper; 146 android::sp<WatchdogProcessServiceInterface> mWatchdogProcessService; 147 android::sp<WatchdogPerfServiceInterface> mWatchdogPerfService; 148 android::sp<IoOveruseMonitorInterface> mIoOveruseMonitor; 149 std::unique_ptr<ThreadPriorityControllerInterface> mThreadPriorityController; 150 151 // For unit tests. 152 friend class internal::WatchdogInternalHandlerPeer; 153 FRIEND_TEST(WatchdogInternalHandlerTest, TestTerminate); 154 }; 155 156 } // namespace watchdog 157 } // namespace automotive 158 } // namespace android 159