• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 err_integral;
49     float prev_err;
50 };
51 
52 // Return the target state of PID algorithm
53 size_t getTargetStateOfPID(const SensorInfo &sensor_info, const ThrottlingSeverity curr_severity);
54 
55 // A helper class for conducting thermal throttling
56 class ThermalThrottling {
57   public:
58     ThermalThrottling() = default;
59     ~ThermalThrottling() = default;
60     // Disallow copy and assign.
61     ThermalThrottling(const ThermalThrottling &) = delete;
62     void operator=(const ThermalThrottling &) = delete;
63 
64     // Clear throttling data
65     void clearThrottlingData(std::string_view sensor_name, const SensorInfo &sensor_info);
66     // Register map for throttling algo
67     bool registerThermalThrottling(
68             std::string_view sensor_name, const std::shared_ptr<ThrottlingInfo> &throttling_info,
69             const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map);
70     // Register map for throttling release algo
71     bool registerThrottlingReleaseToWatch(std::string_view sensor_name, std::string_view cdev_name,
72                                           const BindedCdevInfo &binded_cdev_info);
73     // Get throttling status map
GetThermalThrottlingStatusMap()74     const std::unordered_map<std::string, ThermalThrottlingStatus> &GetThermalThrottlingStatusMap()
75             const {
76         std::shared_lock<std::shared_mutex> _lock(thermal_throttling_status_map_mutex_);
77         return thermal_throttling_status_map_;
78     }
79     // update thermal throttling request for the specific sensor
80     void thermalThrottlingUpdate(
81             const Temperature_2_0 &temp, const SensorInfo &sensor_info,
82             const ThrottlingSeverity curr_severity, const std::chrono::milliseconds time_elapsed_ms,
83             const std::unordered_map<std::string, PowerStatus> &power_status_map,
84             const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map);
85 
86     // Compute the throttling target from all the sensors' request
87     void computeCoolingDevicesRequest(std::string_view sensor_name, const SensorInfo &sensor_info,
88                                       const ThrottlingSeverity curr_severity,
89                                       std::vector<std::string> *cooling_devices_to_update);
90 
91   private:
92     // PID algo - get the total power budget
93     float updatePowerBudget(const Temperature_2_0 &temp, const SensorInfo &sensor_info,
94                             std::chrono::milliseconds time_elapsed_ms,
95                             ThrottlingSeverity curr_severity);
96     // PID algo - map the target throttling state according to the power budget
97     bool updateCdevRequestByPower(
98             const Temperature_2_0 &temp, const SensorInfo &sensor_info,
99             const ThrottlingSeverity curr_severity, const std::chrono::milliseconds time_elapsed_ms,
100             const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map);
101     // Hard limit algo - assign the throttling state according to the severity
102     void updateCdevRequestBySeverity(std::string_view sensor_name, const SensorInfo &sensor_info,
103                                      ThrottlingSeverity curr_severity);
104     // Throttling release algo according to predefined power threshold
105     bool throttlingReleaseUpdate(
106             std::string_view sensor_name,
107             const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map,
108             const std::unordered_map<std::string, PowerStatus> &power_status_map,
109             const ThrottlingSeverity severity, const SensorInfo &sensor_info);
110 
111     mutable std::shared_mutex thermal_throttling_status_map_mutex_;
112     // Thermal throttling status from each sensor
113     std::unordered_map<std::string, ThermalThrottlingStatus> thermal_throttling_status_map_;
114     std::vector<std::string> cooling_devices_to_update;
115 };
116 
117 }  // namespace implementation
118 }  // namespace V2_0
119 }  // namespace thermal
120 }  // namespace hardware
121 }  // namespace android
122