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 #pragma once 18 19 #include <aidl/android/hardware/thermal/CoolingType.h> 20 #include <aidl/android/hardware/thermal/TemperatureType.h> 21 #include <aidl/android/hardware/thermal/ThrottlingSeverity.h> 22 #include <json/value.h> 23 24 #include <chrono> 25 #include <string> 26 #include <unordered_map> 27 #include <unordered_set> 28 #include <variant> 29 30 #include "virtualtemp_estimator/virtualtemp_estimator.h" 31 32 namespace aidl { 33 namespace android { 34 namespace hardware { 35 namespace thermal { 36 namespace implementation { 37 38 constexpr size_t kThrottlingSeverityCount = 39 std::distance(::ndk::enum_range<ThrottlingSeverity>().begin(), 40 ::ndk::enum_range<ThrottlingSeverity>().end()); 41 using ThrottlingArray = std::array<float, static_cast<size_t>(kThrottlingSeverityCount)>; 42 using CdevArray = std::array<int, static_cast<size_t>(kThrottlingSeverityCount)>; 43 constexpr std::chrono::milliseconds kMinPollIntervalMs = std::chrono::milliseconds(2000); 44 constexpr std::chrono::milliseconds kUeventPollTimeoutMs = std::chrono::milliseconds(300000); 45 // TODO(b/292044404): Add debug config to make them easily configurable 46 constexpr std::chrono::milliseconds kPowerLogIntervalMs = std::chrono::milliseconds(60000); 47 constexpr int kMaxPowerLogPerLine = 6; 48 // Max number of time_in_state buckets is 20 in atoms 49 // VendorSensorCoolingDeviceStats, VendorTempResidencyStats 50 constexpr int kMaxStatsResidencyCount = 20; 51 constexpr int kMaxStatsThresholdCount = kMaxStatsResidencyCount - 1; 52 53 enum class FormulaOption : uint32_t { 54 COUNT_THRESHOLD = 0, 55 WEIGHTED_AVG, 56 MAXIMUM, 57 MINIMUM, 58 USE_ML_MODEL, 59 USE_LINEAR_MODEL 60 }; 61 62 template <typename T> 63 struct ThresholdList { 64 std::optional<std::string> logging_name; 65 std::vector<T> thresholds; ThresholdListThresholdList66 explicit ThresholdList(std::optional<std::string> logging_name, std::vector<T> thresholds) 67 : logging_name(logging_name), thresholds(thresholds) {} 68 69 ThresholdList() = default; 70 ThresholdList(const ThresholdList &) = default; 71 ThresholdList &operator=(const ThresholdList &) = default; 72 ThresholdList(ThresholdList &&) = default; 73 ThresholdList &operator=(ThresholdList &&) = default; 74 ~ThresholdList() = default; 75 }; 76 77 template <typename T> 78 struct StatsInfo { 79 // if bool, record all or none depending on flag 80 // if set, check name present in set 81 std::variant<bool, std::unordered_set<std::string> > 82 record_by_default_threshold_all_or_name_set_; 83 // map name to list of thresholds 84 std::unordered_map<std::string, std::vector<ThresholdList<T> > > record_by_threshold; clearStatsInfo85 void clear() { 86 record_by_default_threshold_all_or_name_set_ = false; 87 record_by_threshold.clear(); 88 } 89 }; 90 91 struct StatsConfig { 92 StatsInfo<float> sensor_stats_info; 93 StatsInfo<int> cooling_device_request_info; clearStatsConfig94 void clear() { 95 sensor_stats_info.clear(); 96 cooling_device_request_info.clear(); 97 } 98 }; 99 100 struct TempRangeInfo { 101 int max_temp_threshold; 102 int min_temp_threshold; 103 }; 104 105 struct TempStuckInfo { 106 int min_polling_count; 107 std::chrono::milliseconds min_stuck_duration; 108 }; 109 110 struct AbnormalStatsInfo { 111 struct SensorsTempRangeInfo { 112 std::vector<std::string> sensors; 113 TempRangeInfo temp_range_info; 114 }; 115 struct SensorsTempStuckInfo { 116 std::vector<std::string> sensors; 117 TempStuckInfo temp_stuck_info; 118 }; 119 120 std::optional<TempRangeInfo> default_temp_range_info; 121 std::vector<SensorsTempRangeInfo> sensors_temp_range_infos; 122 std::optional<TempStuckInfo> default_temp_stuck_info; 123 std::vector<SensorsTempStuckInfo> sensors_temp_stuck_infos; 124 }; 125 126 enum class SensorFusionType : uint32_t { 127 SENSOR = 0, 128 ODPM, 129 CONSTANT, 130 }; 131 132 std::ostream &operator<<(std::ostream &os, const SensorFusionType &sensor_fusion_type); 133 134 struct VirtualSensorInfo { 135 std::vector<std::string> linked_sensors; 136 std::vector<SensorFusionType> linked_sensors_type; 137 std::vector<std::string> coefficients; 138 std::vector<SensorFusionType> coefficients_type; 139 140 float offset; 141 std::vector<std::string> trigger_sensors; 142 FormulaOption formula; 143 std::string vt_estimator_model_file; 144 std::unique_ptr<::thermal::vtestimator::VirtualTempEstimator> vt_estimator; 145 std::string backup_sensor; 146 }; 147 148 struct PredictorInfo { 149 std::string sensor; 150 bool support_pid_compensation; 151 std::vector<float> prediction_weights; 152 ThrottlingArray k_p_compensate; 153 }; 154 155 struct VirtualPowerRailInfo { 156 std::vector<std::string> linked_power_rails; 157 std::vector<float> coefficients; 158 float offset; 159 FormulaOption formula; 160 }; 161 162 // The method when the ODPM power is lower than threshold 163 enum class ReleaseLogic : uint32_t { 164 INCREASE = 0, // Increase throttling by step 165 DECREASE, // Decrease throttling by step 166 STEPWISE, // Support both increase and decrease logix 167 RELEASE_TO_FLOOR, // Release throttling to floor directly 168 NONE, 169 }; 170 171 struct BindedCdevInfo { 172 CdevArray limit_info; 173 ThrottlingArray power_thresholds; 174 ReleaseLogic release_logic; 175 ThrottlingArray cdev_weight_for_pid; 176 CdevArray cdev_ceiling; 177 int max_release_step; 178 int max_throttle_step; 179 CdevArray cdev_floor_with_power_link; 180 std::string power_rail; 181 // The flag for activate release logic when power is higher than power threshold 182 bool high_power_check; 183 // The flag for only triggering throttling until all power samples are collected 184 bool throttling_with_power_link; 185 bool enabled; 186 }; 187 188 // The map to store the CDEV throttling info for each profile 189 using ProfileMap = std::unordered_map<std::string, std::unordered_map<std::string, BindedCdevInfo>>; 190 191 struct ThrottlingInfo { 192 ThrottlingArray k_po; 193 ThrottlingArray k_pu; 194 ThrottlingArray k_i; 195 ThrottlingArray k_d; 196 ThrottlingArray i_max; 197 ThrottlingArray max_alloc_power; 198 ThrottlingArray min_alloc_power; 199 ThrottlingArray s_power; 200 ThrottlingArray i_cutoff; 201 float i_default; 202 float i_default_pct; 203 int tran_cycle; 204 std::unordered_map<std::string, ThrottlingArray> excluded_power_info_map; 205 std::unordered_map<std::string, BindedCdevInfo> binded_cdev_info_map; 206 ProfileMap profile_map; 207 }; 208 209 struct SensorInfo { 210 TemperatureType type; 211 ThrottlingArray hot_thresholds; 212 ThrottlingArray cold_thresholds; 213 ThrottlingArray hot_hysteresis; 214 ThrottlingArray cold_hysteresis; 215 std::string temp_path; 216 float vr_threshold; 217 float multiplier; 218 std::chrono::milliseconds polling_delay; 219 std::chrono::milliseconds passive_delay; 220 std::chrono::milliseconds time_resolution; 221 // The StepRatio value which is used for smoothing transient w/ the equation: 222 // Temp = CurrentTemp * StepRatio + LastTemp * (1 - StepRatio) 223 float step_ratio; 224 bool send_cb; 225 bool send_powerhint; 226 bool is_watch; 227 bool is_hidden; 228 std::unique_ptr<VirtualSensorInfo> virtual_sensor_info; 229 std::shared_ptr<ThrottlingInfo> throttling_info; 230 std::unique_ptr<PredictorInfo> predictor_info; 231 }; 232 233 struct CdevInfo { 234 CoolingType type; 235 std::string read_path; 236 std::string write_path; 237 std::vector<float> state2power; 238 int max_state; 239 }; 240 241 struct PowerRailInfo { 242 int power_sample_count; 243 std::chrono::milliseconds power_sample_delay; 244 std::unique_ptr<VirtualPowerRailInfo> virtual_power_rail_info; 245 }; 246 247 bool ParseThermalConfig(std::string_view config_path, Json::Value *config); 248 bool ParseSensorInfo(const Json::Value &config, 249 std::unordered_map<std::string, SensorInfo> *sensors_parsed); 250 bool ParseCoolingDevice(const Json::Value &config, 251 std::unordered_map<std::string, CdevInfo> *cooling_device_parsed); 252 bool ParsePowerRailInfo(const Json::Value &config, 253 std::unordered_map<std::string, PowerRailInfo> *power_rail_parsed); 254 bool ParseSensorStatsConfig(const Json::Value &config, 255 const std::unordered_map<std::string, SensorInfo> &sensor_info_map_, 256 StatsInfo<float> *sensor_stats_info_parsed, 257 AbnormalStatsInfo *abnormal_stats_info_parsed); 258 bool ParseCoolingDeviceStatsConfig( 259 const Json::Value &config, 260 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map_, 261 StatsInfo<int> *cooling_device_request_info_parsed); 262 } // namespace implementation 263 } // namespace thermal 264 } // namespace hardware 265 } // namespace android 266 } // namespace aidl 267