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