Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 22 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 25 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 28 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <android-base/file.h> |
| 32 | #include <android-base/logging.h> |
| 33 | #include <android-base/properties.h> |
| 34 | #include <android-base/stringprintf.h> |
| 35 | #include <android-base/strings.h> |
| 36 | #include <hidl/HidlTransportSupport.h> |
| 37 | |
| 38 | #include "thermalConfig.h" |
| 39 | #include "thermalUtils.h" |
| 40 | |
| 41 | namespace android { |
| 42 | namespace hardware { |
| 43 | namespace thermal { |
| 44 | namespace V2_0 { |
| 45 | namespace implementation { |
| 46 | |
| 47 | ThermalUtils::ThermalUtils(const ueventCB &inp_cb): |
| 48 | cfg(), |
| 49 | cmnInst(), |
| 50 | monitor(std::bind(&ThermalUtils::ueventParse, this, |
| 51 | std::placeholders::_1, |
| 52 | std::placeholders::_2)), |
| 53 | cb(inp_cb) |
| 54 | { |
| 55 | int ret = 0; |
| 56 | std::vector<struct therm_sensor> sensorList; |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 57 | std::vector<struct target_therm_cfg> therm_cfg = cfg.fetchConfig(); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 58 | |
| 59 | is_sensor_init = false; |
| 60 | is_cdev_init = false; |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 61 | ret = cmnInst.initThermalZones(therm_cfg); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 62 | if (ret > 0) { |
| 63 | is_sensor_init = true; |
| 64 | sensorList = cmnInst.fetch_sensor_list(); |
| 65 | std::lock_guard<std::mutex> _lock(sens_cb_mutex); |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 66 | for (struct therm_sensor sens: sensorList) { |
| 67 | thermalConfig[sens.sensor_name] = sens; |
| 68 | cmnInst.read_temperature(sens); |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 69 | cmnInst.estimateSeverity(sens); |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 70 | cmnInst.initThreshold(sens); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 71 | } |
| 72 | monitor.start(); |
| 73 | } |
| 74 | ret = cmnInst.initCdev(); |
| 75 | if (ret > 0) { |
| 76 | is_cdev_init = true; |
| 77 | cdevList = cmnInst.fetch_cdev_list(); |
| 78 | } |
| 79 | } |
| 80 | |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 81 | void ThermalUtils::Notify(struct therm_sensor& sens) |
| 82 | { |
| 83 | int severity = cmnInst.estimateSeverity(sens); |
| 84 | if (severity != -1) { |
| 85 | LOG(INFO) << "sensor: " << sens.sensor_name <<" temperature: " |
| 86 | << sens.t.value << " old: " << |
| 87 | (int)sens.lastThrottleStatus << " new: " << |
| 88 | (int)sens.t.throttlingStatus << std::endl; |
| 89 | cb(sens.t); |
| 90 | cmnInst.initThreshold(sens); |
| 91 | } |
| 92 | } |
| 93 | |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 94 | void ThermalUtils::ueventParse(std::string sensor_name, int temp) |
| 95 | { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 96 | LOG(INFO) << "uevent triggered for sensor: " << sensor_name |
| 97 | << std::endl; |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 98 | if (thermalConfig.find(sensor_name) == thermalConfig.end()) { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 99 | LOG(DEBUG) << "sensor is not monitored:" << sensor_name |
| 100 | << std::endl; |
| 101 | return; |
| 102 | } |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 103 | std::lock_guard<std::mutex> _lock(sens_cb_mutex); |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 104 | struct therm_sensor& sens = thermalConfig[sensor_name]; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 105 | sens.t.value = (float)temp / (float)sens.mulFactor; |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 106 | return Notify(sens); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 109 | int ThermalUtils::readTemperatures(hidl_vec<Temperature_1_0>& temp) |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 110 | { |
| 111 | std::unordered_map<std::string, struct therm_sensor>::iterator it; |
| 112 | int ret = 0, idx = 0; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 113 | std::vector<Temperature_1_0> _temp_v; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 114 | |
| 115 | if (!is_sensor_init) |
| 116 | return 0; |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 117 | std::lock_guard<std::mutex> _lock(sens_cb_mutex); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 118 | for (it = thermalConfig.begin(); it != thermalConfig.end(); |
| 119 | it++, idx++) { |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 120 | struct therm_sensor& sens = it->second; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 121 | Temperature_1_0 _temp; |
| 122 | |
| 123 | /* v1 supports only CPU, GPU, Battery and SKIN */ |
| 124 | if (sens.t.type > TemperatureType::SKIN) |
| 125 | continue; |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 126 | ret = cmnInst.read_temperature(sens); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 127 | if (ret < 0) |
| 128 | return ret; |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 129 | Notify(sens); |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 130 | _temp.currentValue = sens.t.value; |
| 131 | _temp.name = sens.t.name; |
| 132 | _temp.type = (TemperatureType_1_0)sens.t.type; |
| 133 | _temp.throttlingThreshold = sens.thresh.hotThrottlingThresholds[ |
Ram Chandrasekar | f61c614 | 2020-08-31 15:05:42 -0700 | [diff] [blame] | 134 | (size_t)ThrottlingSeverity::SEVERE]; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 135 | _temp.shutdownThreshold = sens.thresh.hotThrottlingThresholds[ |
Ram Chandrasekar | f61c614 | 2020-08-31 15:05:42 -0700 | [diff] [blame] | 136 | (size_t)ThrottlingSeverity::SHUTDOWN]; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 137 | _temp.vrThrottlingThreshold = sens.thresh.vrThrottlingThreshold; |
| 138 | _temp_v.push_back(_temp); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 141 | temp = _temp_v; |
| 142 | return temp.size(); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int ThermalUtils::readTemperatures(bool filterType, TemperatureType type, |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 146 | hidl_vec<Temperature>& temp) |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 147 | { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 148 | std::unordered_map<std::string, struct therm_sensor>::iterator it; |
| 149 | int ret = 0; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 150 | std::vector<Temperature> _temp; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 151 | |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 152 | std::lock_guard<std::mutex> _lock(sens_cb_mutex); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 153 | for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) { |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 154 | struct therm_sensor& sens = it->second; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 155 | |
| 156 | if (filterType && sens.t.type != type) |
| 157 | continue; |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 158 | ret = cmnInst.read_temperature(sens); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 159 | if (ret < 0) |
| 160 | return ret; |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 161 | Notify(sens); |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 162 | _temp.push_back(sens.t); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 163 | } |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 164 | |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 165 | temp = _temp; |
| 166 | return temp.size(); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | int ThermalUtils::readTemperatureThreshold(bool filterType, TemperatureType type, |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 170 | hidl_vec<TemperatureThreshold>& thresh) |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 171 | { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 172 | std::unordered_map<std::string, struct therm_sensor>::iterator it; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 173 | std::vector<TemperatureThreshold> _thresh; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 174 | |
| 175 | for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) { |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 176 | struct therm_sensor& sens = it->second; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 177 | |
| 178 | if (filterType && sens.t.type != type) |
| 179 | continue; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 180 | _thresh.push_back(sens.thresh); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 181 | } |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 182 | |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 183 | thresh = _thresh; |
| 184 | return thresh.size(); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | int ThermalUtils::readCdevStates(bool filterType, cdevType type, |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 188 | hidl_vec<CoolingDevice>& cdev_out) |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 189 | { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 190 | int ret = 0; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 191 | std::vector<CoolingDevice> _cdev; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 192 | |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 193 | for (struct therm_cdev cdev: cdevList) { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 194 | |
| 195 | if (filterType && cdev.c.type != type) |
| 196 | continue; |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 197 | ret = cmnInst.read_cdev_state(cdev); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 198 | if (ret < 0) |
| 199 | return ret; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 200 | _cdev.push_back(cdev.c); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 201 | } |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 202 | |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 203 | cdev_out = _cdev; |
| 204 | |
| 205 | return cdev_out.size(); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Ram Chandrasekar | 72110b5 | 2020-08-06 14:39:40 -0700 | [diff] [blame] | 208 | int ThermalUtils::fetchCpuUsages(hidl_vec<CpuUsage>& cpu_usages) |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 209 | { |
| 210 | return cmnInst.get_cpu_usages(cpu_usages); |
| 211 | } |
| 212 | |
| 213 | } // namespace implementation |
| 214 | } // namespace V2_0 |
| 215 | } // namespace thermal |
| 216 | } // namespace hardware |
| 217 | } // namespace android |