1 /* 2 * Copyright (C) 2018 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 <array> 20 #include <chrono> 21 #include <mutex> 22 #include <shared_mutex> 23 #include <string> 24 #include <string_view> 25 #include <thread> 26 #include <unordered_map> 27 #include <vector> 28 29 #include "utils/power_files.h" 30 #include "utils/powerhal_helper.h" 31 #include "utils/thermal_files.h" 32 #include "utils/thermal_info.h" 33 #include "utils/thermal_throttling.h" 34 #include "utils/thermal_watcher.h" 35 36 namespace android { 37 namespace hardware { 38 namespace thermal { 39 namespace V2_0 { 40 namespace implementation { 41 42 using ::android::hardware::hidl_vec; 43 using ::android::hardware::thermal::V1_0::CpuUsage; 44 using ::android::hardware::thermal::V2_0::CoolingType; 45 using ::android::hardware::thermal::V2_0::IThermal; 46 using CoolingDevice_1_0 = ::android::hardware::thermal::V1_0::CoolingDevice; 47 using CoolingDevice_2_0 = ::android::hardware::thermal::V2_0::CoolingDevice; 48 using Temperature_1_0 = ::android::hardware::thermal::V1_0::Temperature; 49 using Temperature_2_0 = ::android::hardware::thermal::V2_0::Temperature; 50 using TemperatureType_1_0 = ::android::hardware::thermal::V1_0::TemperatureType; 51 using TemperatureType_2_0 = ::android::hardware::thermal::V2_0::TemperatureType; 52 using ::android::hardware::thermal::V2_0::TemperatureThreshold; 53 using ::android::hardware::thermal::V2_0::ThrottlingSeverity; 54 55 using NotificationCallback = std::function<void(const Temperature_2_0 &t)>; 56 using NotificationTime = std::chrono::time_point<std::chrono::steady_clock>; 57 58 // Get thermal_zone type 59 bool getThermalZoneTypeById(int tz_id, std::string *); 60 61 struct ThermalSample { 62 float temp; 63 boot_clock::time_point timestamp; 64 }; 65 66 struct SensorStatus { 67 ThrottlingSeverity severity; 68 ThrottlingSeverity prev_hot_severity; 69 ThrottlingSeverity prev_cold_severity; 70 ThrottlingSeverity prev_hint_severity; 71 boot_clock::time_point last_update_time; 72 ThermalSample thermal_cached; 73 }; 74 75 class ThermalHelper { 76 public: 77 explicit ThermalHelper(const NotificationCallback &cb); 78 ~ThermalHelper() = default; 79 80 bool fillTemperatures(hidl_vec<Temperature_1_0> *temperatures); 81 bool fillCurrentTemperatures(bool filterType, bool filterCallback, TemperatureType_2_0 type, 82 hidl_vec<Temperature_2_0> *temperatures); 83 bool fillTemperatureThresholds(bool filterType, TemperatureType_2_0 type, 84 hidl_vec<TemperatureThreshold> *thresholds) const; 85 bool fillCurrentCoolingDevices(bool filterType, CoolingType type, 86 hidl_vec<CoolingDevice_2_0> *coolingdevices) const; 87 bool fillCpuUsages(hidl_vec<CpuUsage> *cpu_usages) const; 88 89 // Dissallow copy and assign. 90 ThermalHelper(const ThermalHelper &) = delete; 91 void operator=(const ThermalHelper &) = delete; 92 isInitializedOk()93 bool isInitializedOk() const { return is_initialized_; } 94 95 // Read the temperature of a single sensor. 96 bool readTemperature(std::string_view sensor_name, Temperature_1_0 *out); 97 bool readTemperature( 98 std::string_view sensor_name, Temperature_2_0 *out, 99 std::pair<ThrottlingSeverity, ThrottlingSeverity> *throtting_status = nullptr, 100 const bool force_sysfs = false); 101 bool readTemperatureThreshold(std::string_view sensor_name, TemperatureThreshold *out) const; 102 // Read the value of a single cooling device. 103 bool readCoolingDevice(std::string_view cooling_device, CoolingDevice_2_0 *out) const; 104 // Get SensorInfo Map GetSensorInfoMap()105 const std::unordered_map<std::string, SensorInfo> &GetSensorInfoMap() const { 106 return sensor_info_map_; 107 } 108 // Get CdevInfo Map GetCdevInfoMap()109 const std::unordered_map<std::string, CdevInfo> &GetCdevInfoMap() const { 110 return cooling_device_info_map_; 111 } 112 // Get SensorStatus Map GetSensorStatusMap()113 const std::unordered_map<std::string, SensorStatus> &GetSensorStatusMap() const { 114 std::shared_lock<std::shared_mutex> _lock(sensor_status_map_mutex_); 115 return sensor_status_map_; 116 } 117 // Get ThermalThrottling Map GetThermalThrottlingStatusMap()118 const std::unordered_map<std::string, ThermalThrottlingStatus> &GetThermalThrottlingStatusMap() 119 const { 120 return thermal_throttling_.GetThermalThrottlingStatusMap(); 121 } 122 // Get PowerRailInfo Map GetPowerRailInfoMap()123 const std::unordered_map<std::string, PowerRailInfo> &GetPowerRailInfoMap() const { 124 return power_files_.GetPowerRailInfoMap(); 125 } 126 127 // Get PowerStatus Map GetPowerStatusMap()128 const std::unordered_map<std::string, PowerStatus> &GetPowerStatusMap() const { 129 return power_files_.GetPowerStatusMap(); 130 } 131 void sendPowerExtHint(const Temperature_2_0 &t); isAidlPowerHalExist()132 bool isAidlPowerHalExist() { return power_hal_service_.isAidlPowerHalExist(); } isPowerHalConnected()133 bool isPowerHalConnected() { return power_hal_service_.isPowerHalConnected(); } isPowerHalExtConnected()134 bool isPowerHalExtConnected() { return power_hal_service_.isPowerHalExtConnected(); } 135 136 private: 137 bool initializeSensorMap(const std::unordered_map<std::string, std::string> &path_map); 138 bool initializeCoolingDevices(const std::unordered_map<std::string, std::string> &path_map); 139 void setMinTimeout(SensorInfo *sensor_info); 140 void initializeTrip(const std::unordered_map<std::string, std::string> &path_map, 141 std::set<std::string> *monitored_sensors, bool thermal_genl_enabled); 142 143 // For thermal_watcher_'s polling thread, return the sleep interval 144 std::chrono::milliseconds thermalWatcherCallbackFunc( 145 const std::set<std::string> &uevent_sensors); 146 // Return hot and cold severity status as std::pair 147 std::pair<ThrottlingSeverity, ThrottlingSeverity> getSeverityFromThresholds( 148 const ThrottlingArray &hot_thresholds, const ThrottlingArray &cold_thresholds, 149 const ThrottlingArray &hot_hysteresis, const ThrottlingArray &cold_hysteresis, 150 ThrottlingSeverity prev_hot_severity, ThrottlingSeverity prev_cold_severity, 151 float value) const; 152 // Read temperature data according to thermal sensor's info 153 bool readThermalSensor(std::string_view sensor_name, float *temp, const bool force_sysfs); 154 bool connectToPowerHal(); 155 void updateSupportedPowerHints(); 156 void updateCoolingDevices(const std::vector<std::string> &cooling_devices_to_update); 157 sp<ThermalWatcher> thermal_watcher_; 158 PowerFiles power_files_; 159 ThermalFiles thermal_sensors_; 160 ThermalFiles cooling_devices_; 161 ThermalThrottling thermal_throttling_; 162 bool is_initialized_; 163 const NotificationCallback cb_; 164 std::unordered_map<std::string, CdevInfo> cooling_device_info_map_; 165 std::unordered_map<std::string, SensorInfo> sensor_info_map_; 166 std::unordered_map<std::string, std::map<ThrottlingSeverity, ThrottlingSeverity>> 167 supported_powerhint_map_; 168 PowerHalService power_hal_service_; 169 170 mutable std::shared_mutex sensor_status_map_mutex_; 171 std::unordered_map<std::string, SensorStatus> sensor_status_map_; 172 }; 173 174 } // namespace implementation 175 } // namespace V2_0 176 } // namespace thermal 177 } // namespace hardware 178 } // namespace android 179