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 #define ATRACE_TAG (ATRACE_TAG_THERMAL | ATRACE_TAG_HAL)
17
18 #include "thermal_throttling.h"
19
20 #include <android-base/file.h>
21 #include <android-base/logging.h>
22 #include <android-base/properties.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 #include <utils/Trace.h>
26
27 #include <iterator>
28 #include <set>
29 #include <sstream>
30 #include <thread>
31 #include <vector>
32
33 #include "power_files.h"
34 #include "thermal_info.h"
35
36 namespace aidl {
37 namespace android {
38 namespace hardware {
39 namespace thermal {
40 namespace implementation {
41 using ::android::base::StringPrintf;
42
43 // To find the next PID target state according to the current thermal severity
getTargetStateOfPID(const SensorInfo & sensor_info,const ThrottlingSeverity curr_severity)44 size_t getTargetStateOfPID(const SensorInfo &sensor_info, const ThrottlingSeverity curr_severity) {
45 size_t target_state = 0;
46
47 for (const auto &severity : ::ndk::enum_range<ThrottlingSeverity>()) {
48 size_t state = static_cast<size_t>(severity);
49 if (std::isnan(sensor_info.throttling_info->s_power[state])) {
50 continue;
51 }
52 target_state = state;
53 if (severity > curr_severity) {
54 break;
55 }
56 }
57 LOG(VERBOSE) << "PID target state = " << target_state;
58 return target_state;
59 }
60
parseProfileProperty(std::string_view sensor_name,const SensorInfo & sensor_info)61 void ThermalThrottling::parseProfileProperty(std::string_view sensor_name,
62 const SensorInfo &sensor_info) {
63 if (sensor_info.throttling_info == nullptr) {
64 return;
65 }
66
67 const std::string profile = ::android::base::GetProperty(
68 StringPrintf("vendor.thermal.%s.profile", sensor_name.data()), "");
69
70 if (profile.empty() || sensor_info.throttling_info->profile_map.count(profile)) {
71 if (profile != thermal_throttling_status_map_[sensor_name.data()].profile) {
72 LOG(INFO) << sensor_name.data() << ": throttling profile change to "
73 << ((profile.empty()) ? "default" : profile);
74 thermal_throttling_status_map_[sensor_name.data()].profile = profile;
75 }
76 } else {
77 LOG(ERROR) << sensor_name.data() << ": set profile to default because " << profile
78 << " is invalid";
79 thermal_throttling_status_map_[sensor_name.data()].profile = "";
80 }
81 }
82
clearThrottlingData(std::string_view sensor_name)83 void ThermalThrottling::clearThrottlingData(std::string_view sensor_name) {
84 if (!thermal_throttling_status_map_.count(sensor_name.data())) {
85 return;
86 }
87 std::unique_lock<std::shared_mutex> _lock(thermal_throttling_status_map_mutex_);
88
89 for (auto &pid_power_budget_pair :
90 thermal_throttling_status_map_.at(sensor_name.data()).pid_power_budget_map) {
91 pid_power_budget_pair.second = std::numeric_limits<int>::max();
92 }
93
94 for (auto &pid_cdev_request_pair :
95 thermal_throttling_status_map_.at(sensor_name.data()).pid_cdev_request_map) {
96 pid_cdev_request_pair.second = 0;
97 }
98
99 for (auto &hardlimit_cdev_request_pair :
100 thermal_throttling_status_map_.at(sensor_name.data()).hardlimit_cdev_request_map) {
101 hardlimit_cdev_request_pair.second = 0;
102 }
103
104 for (auto &throttling_release_pair :
105 thermal_throttling_status_map_.at(sensor_name.data()).throttling_release_map) {
106 throttling_release_pair.second = 0;
107 }
108
109 thermal_throttling_status_map_[sensor_name.data()].prev_err = NAN;
110 thermal_throttling_status_map_[sensor_name.data()].i_budget = NAN;
111 thermal_throttling_status_map_[sensor_name.data()].prev_target =
112 static_cast<size_t>(ThrottlingSeverity::NONE);
113 thermal_throttling_status_map_[sensor_name.data()].prev_power_budget = NAN;
114 thermal_throttling_status_map_[sensor_name.data()].tran_cycle = 0;
115
116 return;
117 }
118
registerThermalThrottling(std::string_view sensor_name,const std::shared_ptr<ThrottlingInfo> & throttling_info,const std::unordered_map<std::string,CdevInfo> & cooling_device_info_map)119 bool ThermalThrottling::registerThermalThrottling(
120 std::string_view sensor_name, const std::shared_ptr<ThrottlingInfo> &throttling_info,
121 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map) {
122 if (thermal_throttling_status_map_.count(sensor_name.data())) {
123 LOG(ERROR) << "Sensor " << sensor_name.data() << " throttling map has been registered";
124 return false;
125 }
126
127 if (throttling_info == nullptr) {
128 LOG(ERROR) << "Sensor " << sensor_name.data() << " has no throttling info";
129 return false;
130 }
131
132 thermal_throttling_status_map_[sensor_name.data()].prev_err = NAN;
133 thermal_throttling_status_map_[sensor_name.data()].i_budget = NAN;
134 thermal_throttling_status_map_[sensor_name.data()].prev_target =
135 static_cast<size_t>(ThrottlingSeverity::NONE);
136 thermal_throttling_status_map_[sensor_name.data()].prev_power_budget = NAN;
137 thermal_throttling_status_map_[sensor_name.data()].tran_cycle = 0;
138 thermal_throttling_status_map_[sensor_name.data()].profile = "";
139
140 for (auto &binded_cdev_pair : throttling_info->binded_cdev_info_map) {
141 if (!cooling_device_info_map.count(binded_cdev_pair.first)) {
142 LOG(ERROR) << "Could not find " << sensor_name.data() << "'s binded CDEV "
143 << binded_cdev_pair.first;
144 return false;
145 }
146 // Register PID throttling map
147 for (const auto &cdev_weight : binded_cdev_pair.second.cdev_weight_for_pid) {
148 if (!std::isnan(cdev_weight)) {
149 thermal_throttling_status_map_[sensor_name.data()]
150 .pid_power_budget_map[binded_cdev_pair.first] =
151 std::numeric_limits<int>::max();
152 thermal_throttling_status_map_[sensor_name.data()]
153 .pid_cdev_request_map[binded_cdev_pair.first] = 0;
154 thermal_throttling_status_map_[sensor_name.data()]
155 .cdev_status_map[binded_cdev_pair.first] = 0;
156 cdev_all_request_map_[binded_cdev_pair.first].insert(0);
157 break;
158 }
159 }
160 // Register hard limit throttling map
161 for (const auto &limit_info : binded_cdev_pair.second.limit_info) {
162 if (limit_info > 0) {
163 thermal_throttling_status_map_[sensor_name.data()]
164 .hardlimit_cdev_request_map[binded_cdev_pair.first] = 0;
165 thermal_throttling_status_map_[sensor_name.data()]
166 .cdev_status_map[binded_cdev_pair.first] = 0;
167 cdev_all_request_map_[binded_cdev_pair.first].insert(0);
168 break;
169 }
170 }
171 // Register throttling release map if power threshold exists
172 if (!binded_cdev_pair.second.power_rail.empty()) {
173 for (const auto &power_threshold : binded_cdev_pair.second.power_thresholds) {
174 if (!std::isnan(power_threshold)) {
175 thermal_throttling_status_map_[sensor_name.data()]
176 .throttling_release_map[binded_cdev_pair.first] = 0;
177 break;
178 }
179 }
180 }
181 }
182 return true;
183 }
184
185 // return power budget based on PID algo
updatePowerBudget(const Temperature & temp,const SensorInfo & sensor_info,const std::unordered_map<std::string,CdevInfo> & cooling_device_info_map,std::chrono::milliseconds time_elapsed_ms,ThrottlingSeverity curr_severity,const bool max_throttling,const std::vector<float> & sensor_predictions)186 float ThermalThrottling::updatePowerBudget(
187 const Temperature &temp, const SensorInfo &sensor_info,
188 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map,
189 std::chrono::milliseconds time_elapsed_ms, ThrottlingSeverity curr_severity,
190 const bool max_throttling, const std::vector<float> &sensor_predictions) {
191 float p = 0, d = 0;
192 float power_budget = std::numeric_limits<float>::max();
193 bool target_changed = false;
194 float budget_transient = 0.0;
195 auto &throttling_status = thermal_throttling_status_map_.at(temp.name);
196 std::string sensor_name = temp.name;
197
198 if (curr_severity == ThrottlingSeverity::NONE) {
199 return power_budget;
200 }
201
202 const auto target_state = getTargetStateOfPID(sensor_info, curr_severity);
203 if (throttling_status.prev_target != static_cast<size_t>(ThrottlingSeverity::NONE) &&
204 target_state != throttling_status.prev_target &&
205 sensor_info.throttling_info->tran_cycle > 0) {
206 throttling_status.tran_cycle = sensor_info.throttling_info->tran_cycle - 1;
207 target_changed = true;
208 }
209 throttling_status.prev_target = target_state;
210
211 // Compute PID
212 float target = sensor_info.hot_thresholds[target_state];
213 float err = target - temp.value;
214
215 if (max_throttling && err <= 0) {
216 return sensor_info.throttling_info->min_alloc_power[target_state];
217 }
218
219 p = err * (err < 0 ? sensor_info.throttling_info->k_po[target_state]
220 : sensor_info.throttling_info->k_pu[target_state]);
221
222 if (std::isnan(throttling_status.i_budget)) {
223 if (std::isnan(sensor_info.throttling_info->i_default_pct)) {
224 throttling_status.i_budget = sensor_info.throttling_info->i_default;
225 } else {
226 float default_i_budget = 0.0;
227 for (const auto &binded_cdev_info_pair :
228 sensor_info.throttling_info->binded_cdev_info_map) {
229 int max_cdev_vote;
230 const CdevInfo &cdev_info = cooling_device_info_map.at(binded_cdev_info_pair.first);
231 max_cdev_vote = getCdevMaxRequest(binded_cdev_info_pair.first, &max_cdev_vote);
232 default_i_budget += cdev_info.state2power[max_cdev_vote];
233 }
234 throttling_status.i_budget =
235 default_i_budget * sensor_info.throttling_info->i_default_pct / 100;
236 }
237 }
238
239 if (err < sensor_info.throttling_info->i_cutoff[target_state]) {
240 throttling_status.i_budget += err * sensor_info.throttling_info->k_i[target_state];
241 }
242
243 if (fabsf(throttling_status.i_budget) > sensor_info.throttling_info->i_max[target_state]) {
244 throttling_status.i_budget = sensor_info.throttling_info->i_max[target_state] *
245 (throttling_status.i_budget > 0 ? 1 : -1);
246 }
247
248 if (!std::isnan(throttling_status.prev_err) &&
249 time_elapsed_ms != std::chrono::milliseconds::zero()) {
250 d = sensor_info.throttling_info->k_d[target_state] * (err - throttling_status.prev_err) /
251 time_elapsed_ms.count();
252 }
253
254 // Compute Compensation
255 float compensation = 0;
256 if (sensor_info.predictor_info != nullptr &&
257 sensor_info.predictor_info->support_pid_compensation) {
258 const std::vector<float> &prediction_weights =
259 sensor_info.predictor_info->prediction_weights;
260 for (size_t i = 0; i < sensor_predictions.size(); ++i) {
261 float prediction_err = target - (sensor_predictions[i] * sensor_info.multiplier);
262 compensation += prediction_weights[i] * prediction_err;
263 }
264 // apply weight based on current severity level
265 compensation *= sensor_info.predictor_info->k_p_compensate[target_state];
266 }
267
268 throttling_status.prev_err = err;
269 // Calculate power budget
270 power_budget = sensor_info.throttling_info->s_power[target_state] + p +
271 throttling_status.i_budget + d + compensation;
272 if (power_budget < sensor_info.throttling_info->min_alloc_power[target_state]) {
273 power_budget = sensor_info.throttling_info->min_alloc_power[target_state];
274 }
275 if (power_budget > sensor_info.throttling_info->max_alloc_power[target_state]) {
276 power_budget = sensor_info.throttling_info->max_alloc_power[target_state];
277 }
278
279 if (target_changed) {
280 throttling_status.budget_transient = throttling_status.prev_power_budget - power_budget;
281 }
282
283 if (throttling_status.tran_cycle) {
284 budget_transient = throttling_status.budget_transient *
285 ((static_cast<float>(throttling_status.tran_cycle) /
286 static_cast<float>(sensor_info.throttling_info->tran_cycle)));
287 power_budget += budget_transient;
288 throttling_status.tran_cycle--;
289 }
290
291 LOG(INFO) << temp.name << " power_budget=" << power_budget << " err=" << err
292 << " s_power=" << sensor_info.throttling_info->s_power[target_state]
293 << " time_elapsed_ms=" << time_elapsed_ms.count() << " p=" << p
294 << " i=" << throttling_status.i_budget << " d=" << d
295 << " compensation=" << compensation << " budget transient=" << budget_transient
296 << " control target=" << target_state;
297
298 ATRACE_INT((sensor_name + std::string("-power_budget")).c_str(),
299 static_cast<int>(power_budget));
300 ATRACE_INT((sensor_name + std::string("-s_power")).c_str(),
301 static_cast<int>(sensor_info.throttling_info->s_power[target_state]));
302 ATRACE_INT((sensor_name + std::string("-time_elapsed_ms")).c_str(),
303 static_cast<int>(time_elapsed_ms.count()));
304 ATRACE_INT((sensor_name + std::string("-budget_transient")).c_str(),
305 static_cast<int>(budget_transient));
306 ATRACE_INT((sensor_name + std::string("-i")).c_str(),
307 static_cast<int>(throttling_status.i_budget));
308 ATRACE_INT((sensor_name + std::string("-target_state")).c_str(),
309 static_cast<int>(target_state));
310
311 ATRACE_INT((sensor_name + std::string("-err")).c_str(),
312 static_cast<int>(err / sensor_info.multiplier));
313 ATRACE_INT((sensor_name + std::string("-p")).c_str(), static_cast<int>(p));
314 ATRACE_INT((sensor_name + std::string("-d")).c_str(), static_cast<int>(d));
315 ATRACE_INT((sensor_name + std::string("-predict_compensation")).c_str(),
316 static_cast<int>(compensation));
317 ATRACE_INT((sensor_name + std::string("-temp")).c_str(),
318 static_cast<int>(temp.value / sensor_info.multiplier));
319
320 throttling_status.prev_power_budget = power_budget;
321
322 return power_budget;
323 }
324
computeExcludedPower(const SensorInfo & sensor_info,const ThrottlingSeverity curr_severity,const std::unordered_map<std::string,PowerStatus> & power_status_map,std::string * log_buf,std::string_view sensor_name)325 float ThermalThrottling::computeExcludedPower(
326 const SensorInfo &sensor_info, const ThrottlingSeverity curr_severity,
327 const std::unordered_map<std::string, PowerStatus> &power_status_map, std::string *log_buf,
328 std::string_view sensor_name) {
329 float excluded_power = 0.0;
330
331 for (const auto &excluded_power_info_pair :
332 sensor_info.throttling_info->excluded_power_info_map) {
333 const auto last_updated_avg_power =
334 power_status_map.at(excluded_power_info_pair.first).last_updated_avg_power;
335 if (!std::isnan(last_updated_avg_power)) {
336 excluded_power += last_updated_avg_power *
337 excluded_power_info_pair.second[static_cast<size_t>(curr_severity)];
338 log_buf->append(StringPrintf(
339 "(%s: %0.2f mW, cdev_weight: %f)", excluded_power_info_pair.first.c_str(),
340 last_updated_avg_power,
341 excluded_power_info_pair.second[static_cast<size_t>(curr_severity)]));
342
343 ATRACE_INT((std::string(sensor_name) + std::string("-") +
344 excluded_power_info_pair.first + std::string("-avg_power"))
345 .c_str(),
346 static_cast<int>(last_updated_avg_power));
347 }
348 }
349
350 ATRACE_INT((std::string(sensor_name) + std::string("-excluded_power")).c_str(),
351 static_cast<int>(excluded_power));
352 return excluded_power;
353 }
354
355 // Allocate power budget to binded cooling devices base on the real ODPM power data
allocatePowerToCdev(const Temperature & temp,const SensorInfo & sensor_info,const ThrottlingSeverity curr_severity,const std::chrono::milliseconds time_elapsed_ms,const std::unordered_map<std::string,PowerStatus> & power_status_map,const std::unordered_map<std::string,CdevInfo> & cooling_device_info_map,const bool max_throttling,const std::vector<float> & sensor_predictions)356 bool ThermalThrottling::allocatePowerToCdev(
357 const Temperature &temp, const SensorInfo &sensor_info,
358 const ThrottlingSeverity curr_severity, const std::chrono::milliseconds time_elapsed_ms,
359 const std::unordered_map<std::string, PowerStatus> &power_status_map,
360 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map,
361 const bool max_throttling, const std::vector<float> &sensor_predictions) {
362 float total_weight = 0;
363 float last_updated_avg_power = NAN;
364 float allocated_power = 0;
365 float allocated_weight = 0;
366 bool low_power_device_check = true;
367 bool is_budget_allocated = false;
368 bool power_data_invalid = false;
369 std::set<std::string> allocated_cdev;
370 std::string log_buf;
371
372 std::unique_lock<std::shared_mutex> _lock(thermal_throttling_status_map_mutex_);
373 auto total_power_budget =
374 updatePowerBudget(temp, sensor_info, cooling_device_info_map, time_elapsed_ms,
375 curr_severity, max_throttling, sensor_predictions);
376 const auto &profile = thermal_throttling_status_map_[temp.name].profile;
377
378 if (sensor_info.throttling_info->excluded_power_info_map.size()) {
379 total_power_budget -= computeExcludedPower(sensor_info, curr_severity, power_status_map,
380 &log_buf, temp.name);
381 total_power_budget = std::max(total_power_budget, 0.0f);
382 if (!log_buf.empty()) {
383 LOG(INFO) << temp.name << " power budget=" << total_power_budget << " after " << log_buf
384 << " is excluded";
385 }
386 }
387
388 // Compute total cdev weight
389 for (const auto &binded_cdev_info_pair :
390 (sensor_info.throttling_info->profile_map.count(profile)
391 ? sensor_info.throttling_info->profile_map.at(profile)
392 : sensor_info.throttling_info->binded_cdev_info_map)) {
393 const auto cdev_weight = binded_cdev_info_pair.second
394 .cdev_weight_for_pid[static_cast<size_t>(curr_severity)];
395 if (!binded_cdev_info_pair.second.enabled) {
396 continue;
397 } else if (std::isnan(cdev_weight) || cdev_weight == 0) {
398 allocated_cdev.insert(binded_cdev_info_pair.first);
399 continue;
400 }
401 total_weight += cdev_weight;
402 }
403
404 while (!is_budget_allocated) {
405 for (const auto &binded_cdev_info_pair :
406 (sensor_info.throttling_info->profile_map.count(profile)
407 ? sensor_info.throttling_info->profile_map.at(profile)
408 : sensor_info.throttling_info->binded_cdev_info_map)) {
409 float cdev_power_adjustment = 0;
410 const auto cdev_weight =
411 binded_cdev_info_pair.second
412 .cdev_weight_for_pid[static_cast<size_t>(curr_severity)];
413
414 if (allocated_cdev.count(binded_cdev_info_pair.first)) {
415 continue;
416 }
417
418 // Get the power data
419 if (!power_data_invalid) {
420 if (!binded_cdev_info_pair.second.power_rail.empty()) {
421 last_updated_avg_power =
422 power_status_map.at(binded_cdev_info_pair.second.power_rail)
423 .last_updated_avg_power;
424 if (std::isnan(last_updated_avg_power)) {
425 LOG(VERBOSE) << "power data is under collecting";
426 power_data_invalid = true;
427 break;
428 }
429
430 ATRACE_INT((temp.name + std::string("-") +
431 binded_cdev_info_pair.second.power_rail + std::string("-avg_power"))
432 .c_str(),
433 static_cast<int>(last_updated_avg_power));
434 } else {
435 power_data_invalid = true;
436 break;
437 }
438 if (binded_cdev_info_pair.second.throttling_with_power_link) {
439 return false;
440 }
441 }
442
443 auto cdev_power_budget = total_power_budget * (cdev_weight / total_weight);
444 cdev_power_adjustment = cdev_power_budget - last_updated_avg_power;
445
446 if (low_power_device_check) {
447 // Share the budget for the CDEV which power is lower than target
448 if (cdev_power_adjustment > 0 &&
449 thermal_throttling_status_map_[temp.name].pid_cdev_request_map.at(
450 binded_cdev_info_pair.first) == 0) {
451 allocated_power += last_updated_avg_power;
452 allocated_weight += cdev_weight;
453 allocated_cdev.insert(binded_cdev_info_pair.first);
454 if (!binded_cdev_info_pair.second.power_rail.empty()) {
455 log_buf.append(StringPrintf("(%s: %0.2f mW)",
456 binded_cdev_info_pair.second.power_rail.c_str(),
457 last_updated_avg_power));
458 }
459 LOG(VERBOSE) << temp.name << " binded " << binded_cdev_info_pair.first
460 << " has been already at min state 0";
461 }
462 } else {
463 const CdevInfo &cdev_info = cooling_device_info_map.at(binded_cdev_info_pair.first);
464 if (!binded_cdev_info_pair.second.power_rail.empty()) {
465 log_buf.append(StringPrintf("(%s: %0.2f mW)",
466 binded_cdev_info_pair.second.power_rail.c_str(),
467 last_updated_avg_power));
468 }
469 // Ignore the power distribution if the CDEV has no space to reduce power
470 if ((cdev_power_adjustment < 0 &&
471 thermal_throttling_status_map_[temp.name].pid_cdev_request_map.at(
472 binded_cdev_info_pair.first) == cdev_info.max_state)) {
473 LOG(VERBOSE) << temp.name << " binded " << binded_cdev_info_pair.first
474 << " has been already at max state " << cdev_info.max_state;
475 continue;
476 }
477
478 if (!binded_cdev_info_pair.second.enabled) {
479 cdev_power_budget = cdev_info.state2power[0];
480 } else if (!power_data_invalid && binded_cdev_info_pair.second.power_rail != "") {
481 auto cdev_curr_power_budget =
482 thermal_throttling_status_map_[temp.name].pid_power_budget_map.at(
483 binded_cdev_info_pair.first);
484
485 if (last_updated_avg_power > cdev_curr_power_budget) {
486 cdev_power_budget = cdev_curr_power_budget +=
487 (cdev_power_adjustment *
488 (cdev_curr_power_budget / last_updated_avg_power));
489 } else {
490 cdev_power_budget = cdev_curr_power_budget += cdev_power_adjustment;
491 }
492 } else {
493 cdev_power_budget = total_power_budget * (cdev_weight / total_weight);
494 }
495
496 if (!std::isnan(cdev_info.state2power[0]) &&
497 cdev_power_budget > cdev_info.state2power[0]) {
498 cdev_power_budget = cdev_info.state2power[0];
499 } else if (cdev_power_budget < 0) {
500 cdev_power_budget = 0;
501 }
502
503 int max_cdev_vote;
504 if (!getCdevMaxRequest(binded_cdev_info_pair.first, &max_cdev_vote)) {
505 return false;
506 }
507
508 const auto curr_cdev_vote =
509 thermal_throttling_status_map_[temp.name].pid_cdev_request_map.at(
510 binded_cdev_info_pair.first);
511
512 if (!max_throttling) {
513 if (binded_cdev_info_pair.second.max_release_step !=
514 std::numeric_limits<int>::max() &&
515 (power_data_invalid || cdev_power_adjustment > 0)) {
516 if (!power_data_invalid && curr_cdev_vote < max_cdev_vote) {
517 cdev_power_budget = cdev_info.state2power[curr_cdev_vote];
518 LOG(VERBOSE) << temp.name << "'s " << binded_cdev_info_pair.first
519 << " vote: " << curr_cdev_vote
520 << " is lower than max cdev vote: " << max_cdev_vote;
521 } else {
522 int target_release_step = binded_cdev_info_pair.second.max_release_step;
523 while ((curr_cdev_vote - target_release_step) >
524 binded_cdev_info_pair.second
525 .limit_info[static_cast<size_t>(
526 curr_severity)] &&
527 cdev_info.state2power[curr_cdev_vote - target_release_step] ==
528 cdev_info.state2power[curr_cdev_vote]) {
529 target_release_step += 1;
530 }
531 const auto target_state =
532 std::max(curr_cdev_vote - target_release_step, 0);
533
534 cdev_power_budget = std::min(cdev_power_budget,
535 cdev_info.state2power[target_state]);
536 }
537 }
538
539 if (binded_cdev_info_pair.second.max_throttle_step !=
540 std::numeric_limits<int>::max() &&
541 (power_data_invalid || cdev_power_adjustment < 0)) {
542 int target_throttle_step = binded_cdev_info_pair.second.max_throttle_step;
543 while ((curr_cdev_vote + target_throttle_step) <
544 binded_cdev_info_pair.second
545 .cdev_ceiling[static_cast<size_t>(curr_severity)] &&
546 cdev_info.state2power[curr_cdev_vote + target_throttle_step] ==
547 cdev_info.state2power[curr_cdev_vote]) {
548 target_throttle_step += 1;
549 }
550 const auto target_state =
551 std::min(curr_cdev_vote + target_throttle_step,
552 binded_cdev_info_pair.second
553 .cdev_ceiling[static_cast<size_t>(curr_severity)]);
554 cdev_power_budget =
555 std::max(cdev_power_budget, cdev_info.state2power[target_state]);
556 }
557 }
558
559 thermal_throttling_status_map_[temp.name].pid_power_budget_map.at(
560 binded_cdev_info_pair.first) = cdev_power_budget;
561 LOG(VERBOSE) << temp.name << " allocate "
562 << thermal_throttling_status_map_[temp.name].pid_power_budget_map.at(
563 binded_cdev_info_pair.first)
564 << "mW to " << binded_cdev_info_pair.first
565 << "(cdev_weight=" << cdev_weight << ")";
566 }
567 }
568
569 if (!power_data_invalid) {
570 total_power_budget -= allocated_power;
571 total_weight -= allocated_weight;
572 }
573 allocated_power = 0;
574 allocated_weight = 0;
575
576 if (low_power_device_check) {
577 low_power_device_check = false;
578 } else {
579 is_budget_allocated = true;
580 }
581 }
582 if (log_buf.size()) {
583 LOG(INFO) << temp.name << " binded power rails: " << log_buf;
584 }
585 return true;
586 }
587
updateCdevRequestByPower(std::string sensor_name,const std::unordered_map<std::string,CdevInfo> & cooling_device_info_map)588 void ThermalThrottling::updateCdevRequestByPower(
589 std::string sensor_name,
590 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map) {
591 size_t i;
592
593 std::unique_lock<std::shared_mutex> _lock(thermal_throttling_status_map_mutex_);
594 for (auto &pid_power_budget_pair :
595 thermal_throttling_status_map_[sensor_name.data()].pid_power_budget_map) {
596 const CdevInfo &cdev_info = cooling_device_info_map.at(pid_power_budget_pair.first);
597
598 for (i = 0; i < cdev_info.state2power.size() - 1; ++i) {
599 if (pid_power_budget_pair.second >= cdev_info.state2power[i]) {
600 break;
601 }
602 }
603 thermal_throttling_status_map_[sensor_name.data()].pid_cdev_request_map.at(
604 pid_power_budget_pair.first) = static_cast<int>(i);
605 }
606
607 return;
608 }
609
updateCdevRequestBySeverity(std::string_view sensor_name,const SensorInfo & sensor_info,ThrottlingSeverity curr_severity)610 void ThermalThrottling::updateCdevRequestBySeverity(std::string_view sensor_name,
611 const SensorInfo &sensor_info,
612 ThrottlingSeverity curr_severity) {
613 std::unique_lock<std::shared_mutex> _lock(thermal_throttling_status_map_mutex_);
614 const auto &profile = thermal_throttling_status_map_[sensor_name.data()].profile;
615
616 for (const auto &binded_cdev_info_pair :
617 (sensor_info.throttling_info->profile_map.count(profile)
618 ? sensor_info.throttling_info->profile_map.at(profile)
619 : sensor_info.throttling_info->binded_cdev_info_map)) {
620 if (!thermal_throttling_status_map_[sensor_name.data()].hardlimit_cdev_request_map.count(
621 binded_cdev_info_pair.first)) {
622 continue;
623 }
624 thermal_throttling_status_map_[sensor_name.data()].hardlimit_cdev_request_map.at(
625 binded_cdev_info_pair.first) =
626 (binded_cdev_info_pair.second.enabled)
627 ? binded_cdev_info_pair.second
628 .limit_info[static_cast<size_t>(curr_severity)]
629 : 0;
630 LOG(VERBOSE) << "Hard Limit: Sensor " << sensor_name.data() << " update cdev "
631 << binded_cdev_info_pair.first << " to "
632 << thermal_throttling_status_map_[sensor_name.data()]
633 .hardlimit_cdev_request_map.at(binded_cdev_info_pair.first);
634 }
635 }
636
throttlingReleaseUpdate(std::string_view sensor_name,const std::unordered_map<std::string,CdevInfo> & cooling_device_info_map,const std::unordered_map<std::string,PowerStatus> & power_status_map,const ThrottlingSeverity severity,const SensorInfo & sensor_info)637 bool ThermalThrottling::throttlingReleaseUpdate(
638 std::string_view sensor_name,
639 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map,
640 const std::unordered_map<std::string, PowerStatus> &power_status_map,
641 const ThrottlingSeverity severity, const SensorInfo &sensor_info) {
642 ATRACE_CALL();
643 std::unique_lock<std::shared_mutex> _lock(thermal_throttling_status_map_mutex_);
644 if (!thermal_throttling_status_map_.count(sensor_name.data())) {
645 return false;
646 }
647 auto &thermal_throttling_status = thermal_throttling_status_map_.at(sensor_name.data());
648 for (const auto &binded_cdev_info_pair : sensor_info.throttling_info->binded_cdev_info_map) {
649 float avg_power = -1;
650
651 if (!thermal_throttling_status.throttling_release_map.count(binded_cdev_info_pair.first) ||
652 !power_status_map.count(binded_cdev_info_pair.second.power_rail)) {
653 return false;
654 }
655
656 const auto max_state = cooling_device_info_map.at(binded_cdev_info_pair.first).max_state;
657
658 auto &release_step =
659 thermal_throttling_status.throttling_release_map.at(binded_cdev_info_pair.first);
660 avg_power =
661 power_status_map.at(binded_cdev_info_pair.second.power_rail).last_updated_avg_power;
662
663 if (std::isnan(avg_power) || avg_power < 0) {
664 release_step = binded_cdev_info_pair.second.throttling_with_power_link ? max_state : 0;
665 continue;
666 }
667
668 bool is_over_budget = true;
669 if (!binded_cdev_info_pair.second.high_power_check) {
670 if (avg_power <
671 binded_cdev_info_pair.second.power_thresholds[static_cast<int>(severity)]) {
672 is_over_budget = false;
673 }
674 } else {
675 if (avg_power >
676 binded_cdev_info_pair.second.power_thresholds[static_cast<int>(severity)]) {
677 is_over_budget = false;
678 }
679 }
680 LOG(INFO) << sensor_name.data() << "'s " << binded_cdev_info_pair.first
681 << " binded power rail " << binded_cdev_info_pair.second.power_rail
682 << ": power threshold = "
683 << binded_cdev_info_pair.second.power_thresholds[static_cast<int>(severity)]
684 << ", avg power = " << avg_power;
685 std::string atrace_prefix = ::android::base::StringPrintf(
686 "%s-%s", sensor_name.data(), binded_cdev_info_pair.second.power_rail.data());
687 ATRACE_INT(
688 (atrace_prefix + std::string("-power_threshold")).c_str(),
689 static_cast<int>(
690 binded_cdev_info_pair.second.power_thresholds[static_cast<int>(severity)]));
691 ATRACE_INT((atrace_prefix + std::string("-avg_power")).c_str(), avg_power);
692
693 switch (binded_cdev_info_pair.second.release_logic) {
694 case ReleaseLogic::INCREASE:
695 if (!is_over_budget) {
696 if (std::abs(release_step) < static_cast<int>(max_state)) {
697 release_step--;
698 }
699 } else {
700 release_step = 0;
701 }
702 break;
703 case ReleaseLogic::DECREASE:
704 if (!is_over_budget) {
705 if (release_step < static_cast<int>(max_state)) {
706 release_step++;
707 }
708 } else {
709 release_step = 0;
710 }
711 break;
712 case ReleaseLogic::STEPWISE:
713 if (!is_over_budget) {
714 if (release_step < static_cast<int>(max_state)) {
715 release_step++;
716 }
717 } else {
718 if (std::abs(release_step) < static_cast<int>(max_state)) {
719 release_step--;
720 }
721 }
722 break;
723 case ReleaseLogic::RELEASE_TO_FLOOR:
724 release_step = is_over_budget ? 0 : max_state;
725 break;
726 case ReleaseLogic::NONE:
727 default:
728 break;
729 }
730 }
731 return true;
732 }
733
thermalThrottlingUpdate(const Temperature & temp,const SensorInfo & sensor_info,const ThrottlingSeverity curr_severity,const std::chrono::milliseconds time_elapsed_ms,const std::unordered_map<std::string,PowerStatus> & power_status_map,const std::unordered_map<std::string,CdevInfo> & cooling_device_info_map,const bool max_throttling,const std::vector<float> & sensor_predictions)734 void ThermalThrottling::thermalThrottlingUpdate(
735 const Temperature &temp, const SensorInfo &sensor_info,
736 const ThrottlingSeverity curr_severity, const std::chrono::milliseconds time_elapsed_ms,
737 const std::unordered_map<std::string, PowerStatus> &power_status_map,
738 const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map,
739 const bool max_throttling, const std::vector<float> &sensor_predictions) {
740 if (!thermal_throttling_status_map_.count(temp.name)) {
741 return;
742 }
743
744 if (sensor_info.throttling_info->profile_map.size()) {
745 parseProfileProperty(temp.name.c_str(), sensor_info);
746 }
747
748 if (thermal_throttling_status_map_[temp.name].pid_power_budget_map.size()) {
749 if (!allocatePowerToCdev(temp, sensor_info, curr_severity, time_elapsed_ms,
750 power_status_map, cooling_device_info_map, max_throttling,
751 sensor_predictions)) {
752 LOG(ERROR) << "Sensor " << temp.name << " PID request cdev failed";
753 // Clear the CDEV request if the power budget is failed to be allocated
754 for (auto &pid_cdev_request_pair :
755 thermal_throttling_status_map_[temp.name].pid_cdev_request_map) {
756 pid_cdev_request_pair.second = 0;
757 }
758 }
759 updateCdevRequestByPower(temp.name, cooling_device_info_map);
760 }
761
762 if (thermal_throttling_status_map_[temp.name].hardlimit_cdev_request_map.size()) {
763 updateCdevRequestBySeverity(temp.name.c_str(), sensor_info, curr_severity);
764 }
765
766 if (thermal_throttling_status_map_[temp.name].throttling_release_map.size()) {
767 throttlingReleaseUpdate(temp.name.c_str(), cooling_device_info_map, power_status_map,
768 curr_severity, sensor_info);
769 }
770 }
771
computeCoolingDevicesRequest(std::string_view sensor_name,const SensorInfo & sensor_info,const ThrottlingSeverity curr_severity,std::vector<std::string> * cooling_devices_to_update,ThermalStatsHelper * thermal_stats_helper)772 void ThermalThrottling::computeCoolingDevicesRequest(
773 std::string_view sensor_name, const SensorInfo &sensor_info,
774 const ThrottlingSeverity curr_severity, std::vector<std::string> *cooling_devices_to_update,
775 ThermalStatsHelper *thermal_stats_helper) {
776 int release_step = 0;
777 std::unique_lock<std::shared_mutex> _lock(thermal_throttling_status_map_mutex_);
778
779 if (!thermal_throttling_status_map_.count(sensor_name.data())) {
780 return;
781 }
782
783 auto &thermal_throttling_status = thermal_throttling_status_map_.at(sensor_name.data());
784 const auto &cdev_release_map = thermal_throttling_status.throttling_release_map;
785
786 const auto &profile = thermal_throttling_status_map_[sensor_name.data()].profile;
787 const auto &binded_cdev_info_map =
788 sensor_info.throttling_info->profile_map.count(profile)
789 ? sensor_info.throttling_info->profile_map.at(profile)
790 : sensor_info.throttling_info->binded_cdev_info_map;
791
792 for (auto &cdev_request_pair : thermal_throttling_status.cdev_status_map) {
793 int pid_cdev_request = 0;
794 int hardlimit_cdev_request = 0;
795 const auto &cdev_name = cdev_request_pair.first;
796 const auto &binded_cdev_info = binded_cdev_info_map.at(cdev_name);
797 const auto cdev_ceiling = binded_cdev_info.cdev_ceiling[static_cast<size_t>(curr_severity)];
798 const auto cdev_floor =
799 binded_cdev_info.cdev_floor_with_power_link[static_cast<size_t>(curr_severity)];
800 release_step = 0;
801
802 if (thermal_throttling_status.pid_cdev_request_map.count(cdev_name)) {
803 pid_cdev_request = thermal_throttling_status.pid_cdev_request_map.at(cdev_name);
804 }
805
806 if (thermal_throttling_status.hardlimit_cdev_request_map.count(cdev_name)) {
807 hardlimit_cdev_request =
808 thermal_throttling_status.hardlimit_cdev_request_map.at(cdev_name);
809 }
810
811 if (cdev_release_map.count(cdev_name)) {
812 release_step = cdev_release_map.at(cdev_name);
813 }
814
815 LOG(VERBOSE) << sensor_name.data() << " binded cooling device " << cdev_name
816 << "'s pid_request=" << pid_cdev_request
817 << " hardlimit_cdev_request=" << hardlimit_cdev_request
818 << " release_step=" << release_step
819 << " cdev_floor_with_power_link=" << cdev_floor
820 << " cdev_ceiling=" << cdev_ceiling;
821 std::string atrace_prefix =
822 ::android::base::StringPrintf("%s-%s", sensor_name.data(), cdev_name.data());
823 ATRACE_INT((atrace_prefix + std::string("-pid_request")).c_str(), pid_cdev_request);
824 ATRACE_INT((atrace_prefix + std::string("-hardlimit_request")).c_str(),
825 hardlimit_cdev_request);
826 ATRACE_INT((atrace_prefix + std::string("-release_step")).c_str(), release_step);
827 ATRACE_INT((atrace_prefix + std::string("-cdev_floor")).c_str(), cdev_floor);
828 ATRACE_INT((atrace_prefix + std::string("-cdev_ceiling")).c_str(), cdev_ceiling);
829
830 auto request_state = std::max(pid_cdev_request, hardlimit_cdev_request);
831 if (release_step) {
832 if (release_step >= request_state) {
833 request_state = 0;
834 } else {
835 request_state = request_state - release_step;
836 }
837 // Only check the cdev_floor when release step is non zero
838 request_state = std::max(request_state, cdev_floor);
839 }
840 request_state = std::min(request_state, cdev_ceiling);
841 if (cdev_request_pair.second != request_state) {
842 ATRACE_INT((atrace_prefix + std::string("-final_request")).c_str(), request_state);
843 if (updateCdevMaxRequestAndNotifyIfChange(cdev_name, cdev_request_pair.second,
844 request_state)) {
845 cooling_devices_to_update->emplace_back(cdev_name);
846 }
847 cdev_request_pair.second = request_state;
848 // Update sensor cdev request time in state
849 thermal_stats_helper->updateSensorCdevRequestStats(sensor_name, cdev_name,
850 cdev_request_pair.second);
851 }
852 }
853 }
854
updateCdevMaxRequestAndNotifyIfChange(std::string_view cdev_name,int cur_request,int new_request)855 bool ThermalThrottling::updateCdevMaxRequestAndNotifyIfChange(std::string_view cdev_name,
856 int cur_request, int new_request) {
857 std::unique_lock<std::shared_mutex> _lock(cdev_all_request_map_mutex_);
858 auto &request_set = cdev_all_request_map_.at(cdev_name.data());
859 int cur_max_request = (*request_set.begin());
860 // Remove old cdev request and add the new one.
861 request_set.erase(request_set.find(cur_request));
862 request_set.insert(new_request);
863 // Check if there is any change in aggregated max cdev request.
864 int new_max_request = (*request_set.begin());
865 LOG(VERBOSE) << "For cooling device [" << cdev_name.data()
866 << "] cur_max_request is: " << cur_max_request
867 << " new_max_request is: " << new_max_request;
868 return new_max_request != cur_max_request;
869 }
870
getCdevMaxRequest(std::string_view cdev_name,int * max_state)871 bool ThermalThrottling::getCdevMaxRequest(std::string_view cdev_name, int *max_state) {
872 std::shared_lock<std::shared_mutex> _lock(cdev_all_request_map_mutex_);
873 if (!cdev_all_request_map_.count(cdev_name.data())) {
874 LOG(ERROR) << "Cooling device [" << cdev_name.data()
875 << "] not present in cooling device request map";
876 return false;
877 }
878 *max_state = *cdev_all_request_map_.at(cdev_name.data()).begin();
879 return true;
880 }
881
882 } // namespace implementation
883 } // namespace thermal
884 } // namespace hardware
885 } // namespace android
886 } // namespace aidl
887