• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
21 
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   UnexpectedWifiScanResponse = 3,
40   UnexpectedWifiScanMonitorStateChange = 4,
41 
42   //! Must be last
43   NumCheckIds
44 };
45 
46 class SystemHealthMonitor : public NonCopyable {
47  public:
48   /**
49    * Configures if onCheckFailureImpl() should crash
50    *
51    * @param enable true if onCheckFailureImpl() should log the error and crash,
52    * false if onCheckFailureImpl() should only log the error
53    */
setFatalErrorOnCheckFailure(bool enable)54   inline void setFatalErrorOnCheckFailure(bool enable) {
55     mShouldCheckCrash = enable;
56   }
57 
58   /**
59    * Provides a runtime configurable way to call/skip FATAL_ERROR
60    * to prevent crashing on programming errors that are low visibility
61    * to users
62    *
63    * Also provides a counter to log the occurrence of each type of defined
64    * HealthCheckId
65    *
66    * @param condition Boolean expression which evaluates to false in the failure
67    *        case
68    * @param id predefined HealthCheckId used to record occurrence of each
69    * failure
70    */
check(bool condition,HealthCheckId id)71   static inline void check(bool condition, HealthCheckId id) {
72     if (!condition) {
73       SystemHealthMonitor::onFailure(id);
74     }
75   }
76 
77   /**
78    * Similar to check() but should be called when HealthCheck has already failed
79    *
80    * @param id predefined HealthCheckId used to record occurrence of each
81    * failure
82    */
83   static void onFailure(HealthCheckId id);
84 
85  private:
86   bool mShouldCheckCrash = false;
87 
88   /**
89    * Records how many times a check failed on a HealthCheckId
90    */
91   uint16_t mCheckIdOccurrenceCounter[asBaseType(HealthCheckId::NumCheckIds)];
92 
93   /**
94    * Implements the logic once check encountered a false condition
95    * This is needed to prevent the runtime overhead when calling a function
96    * when it is not necessary while also have the ability to modify object
97    * member
98    *
99    *  @param id which HealthCheckId that matches this failure
100    */
101   void onCheckFailureImpl(HealthCheckId id);
102 };
103 
104 }  // namespace chre
105 
106 #endif  // CHRE_CORE_SYSTEM_HEALTH_MONITOR_H_
107