1 /* 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 #ifndef CHRE_CORE_SYSTEM_HEALTH_MONITOR_H_ 18 #define CHRE_CORE_SYSTEM_HEALTH_MONITOR_H_ 19 20 #include "chre/platform/assert.h" 21 #include "chre/platform/log.h" 22 #include "chre/util/enum.h" 23 #include "chre/util/non_copyable.h" 24 25 namespace chre { 26 27 /** 28 * Types of different health check id 29 * User should consider adding a new check id if current id does not describe 30 * the case accurately 31 * 32 * The goal of this enum class is to be granular enough to produce useful debug 33 * information and metric report 34 */ 35 enum class HealthCheckId : uint16_t { 36 WifiScanResponseTimeout = 0, 37 WifiConfigureScanMonitorTimeout = 1, 38 WifiRequestRangingTimeout = 2, 39 UnexpectedWifiPalCallback = 3, 40 41 //! Must be last 42 NumCheckIds 43 }; 44 45 class SystemHealthMonitor : public NonCopyable { 46 public: 47 /** 48 * Configures if onCheckFailureImpl() should crash 49 * 50 * @param enable true if onCheckFailureImpl() should log the error and crash, 51 * false if onCheckFailureImpl() should only log the error 52 */ setFatalErrorOnCheckFailure(bool enable)53 inline void setFatalErrorOnCheckFailure(bool enable) { 54 mShouldCheckCrash = enable; 55 } 56 57 /** 58 * Provides a runtime configurable way to call/skip FATAL_ERROR 59 * to prevent crashing on programming errors that are low visibility 60 * to users 61 * 62 * Also provides a counter to log the occurrence of each type of defined 63 * HealthCheckId 64 * 65 * @param condition Boolean expression which evaluates to false in the failure 66 * case 67 * @param id predefined HealthCheckId used to record occurrence of each 68 * failure 69 */ check(bool condition,HealthCheckId id)70 static inline void check(bool condition, HealthCheckId id) { 71 if (!condition) { 72 SystemHealthMonitor::onFailure(id); 73 } 74 } 75 76 /** 77 * Similar to check() but should be called when HealthCheck has already failed 78 * 79 * @param id predefined HealthCheckId used to record occurrence of each 80 * failure 81 */ 82 static void onFailure(HealthCheckId id); 83 84 private: 85 bool mShouldCheckCrash = false; 86 87 /** 88 * Records how many times a check failed on a HealthCheckId 89 */ 90 uint16_t mCheckIdOccurrenceCounter[asBaseType(HealthCheckId::NumCheckIds)]; 91 92 /** 93 * Implements the logic once check encountered a false condition 94 * This is needed to prevent the runtime overhead when calling a function 95 * when it is not necessary while also have the ability to modify object 96 * member 97 * 98 * @param id which HealthCheckId that matches this failure 99 */ 100 void onCheckFailureImpl(HealthCheckId id); 101 }; 102 103 } // namespace chre 104 105 #endif // CHRE_CORE_SYSTEM_HEALTH_MONITOR_H_ 106