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 <android/hardware/thermal/2.0/IThermal.h> 20 21 #include <queue> 22 #include <shared_mutex> 23 #include <string> 24 #include <unordered_map> 25 #include <unordered_set> 26 27 #include "power_files.h" 28 #include "thermal_info.h" 29 30 namespace android { 31 namespace hardware { 32 namespace thermal { 33 namespace V2_0 { 34 namespace implementation { 35 36 using ::android::hardware::hidl_vec; 37 using ::android::hardware::thermal::V2_0::IThermal; 38 using Temperature_2_0 = ::android::hardware::thermal::V2_0::Temperature; 39 using ::android::hardware::thermal::V2_0::TemperatureThreshold; 40 using ::android::hardware::thermal::V2_0::ThrottlingSeverity; 41 42 struct ThermalThrottlingStatus { 43 std::unordered_map<std::string, int> pid_power_budget_map; 44 std::unordered_map<std::string, int> pid_cdev_request_map; 45 std::unordered_map<std::string, int> hardlimit_cdev_request_map; 46 std::unordered_map<std::string, int> throttling_release_map; 47 std::unordered_map<std::string, int> cdev_status_map; 48 float prev_err; 49 float i_budget; 50 float prev_target; 51 float prev_power_budget; 52 float budget_transient; 53 int tran_cycle; 54 }; 55 56 // Return the control temp target of PID algorithm 57 size_t getTargetStateOfPID(const SensorInfo &sensor_info, const ThrottlingSeverity curr_severity); 58 59 // A helper class for conducting thermal throttling 60 class ThermalThrottling { 61 public: 62 ThermalThrottling() = default; 63 ~ThermalThrottling() = default; 64 // Disallow copy and assign. 65 ThermalThrottling(const ThermalThrottling &) = delete; 66 void operator=(const ThermalThrottling &) = delete; 67 68 // Clear throttling data 69 void clearThrottlingData(std::string_view sensor_name, const SensorInfo &sensor_info); 70 // Register map for throttling algo 71 bool registerThermalThrottling( 72 std::string_view sensor_name, const std::shared_ptr<ThrottlingInfo> &throttling_info, 73 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map); 74 // Register map for throttling release algo 75 bool registerThrottlingReleaseToWatch(std::string_view sensor_name, std::string_view cdev_name, 76 const BindedCdevInfo &binded_cdev_info); 77 // Get throttling status map GetThermalThrottlingStatusMap()78 const std::unordered_map<std::string, ThermalThrottlingStatus> &GetThermalThrottlingStatusMap() 79 const { 80 std::shared_lock<std::shared_mutex> _lock(thermal_throttling_status_map_mutex_); 81 return thermal_throttling_status_map_; 82 } 83 // Update thermal throttling request for the specific sensor 84 void thermalThrottlingUpdate( 85 const Temperature_2_0 &temp, const SensorInfo &sensor_info, 86 const ThrottlingSeverity curr_severity, const std::chrono::milliseconds time_elapsed_ms, 87 const std::unordered_map<std::string, PowerStatus> &power_status_map, 88 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map); 89 90 // Compute the throttling target from all the sensors' request 91 void computeCoolingDevicesRequest(std::string_view sensor_name, const SensorInfo &sensor_info, 92 const ThrottlingSeverity curr_severity, 93 std::vector<std::string> *cooling_devices_to_update); 94 95 private: 96 // PID algo - get the total power budget 97 float updatePowerBudget(const Temperature_2_0 &temp, const SensorInfo &sensor_info, 98 std::chrono::milliseconds time_elapsed_ms, 99 ThrottlingSeverity curr_severity); 100 101 // PID algo - return the power number from excluded power rail list 102 float computeExcludedPower(const SensorInfo &sensor_info, 103 const ThrottlingSeverity curr_severity, 104 const std::unordered_map<std::string, PowerStatus> &power_status_map, 105 std::string *log_buf); 106 107 // PID algo - allocate the power to target CDEV according to the ODPM 108 bool allocatePowerToCdev( 109 const Temperature_2_0 &temp, const SensorInfo &sensor_info, 110 const ThrottlingSeverity curr_severity, const std::chrono::milliseconds time_elapsed_ms, 111 const std::unordered_map<std::string, PowerStatus> &power_status_map, 112 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map); 113 // PID algo - map the target throttling state according to the power budget 114 void updateCdevRequestByPower( 115 std::string sensor_name, 116 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map); 117 // Hard limit algo - assign the throttling state according to the severity 118 void updateCdevRequestBySeverity(std::string_view sensor_name, const SensorInfo &sensor_info, 119 ThrottlingSeverity curr_severity); 120 // Throttling release algo - decide release step according to the predefined power threshold, 121 // return false if the throttling release is not registered in thermal config 122 bool throttlingReleaseUpdate( 123 std::string_view sensor_name, 124 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map, 125 const std::unordered_map<std::string, PowerStatus> &power_status_map, 126 const ThrottlingSeverity severity, const SensorInfo &sensor_info); 127 128 mutable std::shared_mutex thermal_throttling_status_map_mutex_; 129 // Thermal throttling status from each sensor 130 std::unordered_map<std::string, ThermalThrottlingStatus> thermal_throttling_status_map_; 131 std::vector<std::string> cooling_devices_to_update; 132 }; 133 134 } // namespace implementation 135 } // namespace V2_0 136 } // namespace thermal 137 } // namespace hardware 138 } // namespace android 139