• 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 <aidl/android/hardware/thermal/IThermal.h>
20 
21 #include <array>
22 #include <chrono>
23 #include <map>
24 #include <mutex>
25 #include <shared_mutex>
26 #include <string>
27 #include <string_view>
28 #include <thread>
29 #include <unordered_map>
30 #include <vector>
31 
32 #include "utils/power_files.h"
33 #include "utils/powerhal_helper.h"
34 #include "utils/thermal_files.h"
35 #include "utils/thermal_info.h"
36 #include "utils/thermal_stats_helper.h"
37 #include "utils/thermal_throttling.h"
38 #include "utils/thermal_watcher.h"
39 
40 namespace aidl {
41 namespace android {
42 namespace hardware {
43 namespace thermal {
44 namespace implementation {
45 
46 using ::android::sp;
47 
48 using NotificationCallback = std::function<void(const Temperature &t)>;
49 
50 // Get thermal_zone type
51 bool getThermalZoneTypeById(int tz_id, std::string *);
52 
53 struct ThermalSample {
54     float temp;
55     boot_clock::time_point timestamp;
56 };
57 
58 struct EmulSetting {
59     float emul_temp;
60     int emul_severity;
61     bool pending_update;
62 };
63 
64 struct SensorStatus {
65     ThrottlingSeverity severity;
66     ThrottlingSeverity prev_hot_severity;
67     ThrottlingSeverity prev_cold_severity;
68     boot_clock::time_point last_update_time;
69     ThermalSample thermal_cached;
70     std::unique_ptr<EmulSetting> emul_setting;
71 };
72 
73 class ThermalHelper {
74   public:
75     virtual ~ThermalHelper() = default;
76     virtual bool fillCurrentTemperatures(bool filterType, bool filterCallback, TemperatureType type,
77                                          std::vector<Temperature> *temperatures) = 0;
78     virtual bool fillTemperatureThresholds(bool filterType, TemperatureType type,
79                                            std::vector<TemperatureThreshold> *thresholds) const = 0;
80     virtual bool fillCurrentCoolingDevices(bool filterType, CoolingType type,
81                                            std::vector<CoolingDevice> *coolingdevices) const = 0;
82     virtual bool emulTemp(std::string_view target_sensor, const float temp) = 0;
83     virtual bool emulSeverity(std::string_view target_sensor, const int severity) = 0;
84     virtual bool emulClear(std::string_view target_sensor) = 0;
85     virtual bool isInitializedOk() const = 0;
86     virtual bool readTemperature(
87             std::string_view sensor_name, Temperature *out,
88             std::pair<ThrottlingSeverity, ThrottlingSeverity> *throtting_status = nullptr,
89             const bool force_sysfs = false) = 0;
90     virtual bool readTemperatureThreshold(std::string_view sensor_name,
91                                           TemperatureThreshold *out) const = 0;
92     virtual bool readCoolingDevice(std::string_view cooling_device, CoolingDevice *out) const = 0;
93     virtual const std::unordered_map<std::string, SensorInfo> &GetSensorInfoMap() const = 0;
94     virtual const std::unordered_map<std::string, CdevInfo> &GetCdevInfoMap() const = 0;
95     virtual const std::unordered_map<std::string, SensorStatus> &GetSensorStatusMap() const = 0;
96     virtual const std::unordered_map<std::string, ThermalThrottlingStatus> &
97     GetThermalThrottlingStatusMap() const = 0;
98     virtual const std::unordered_map<std::string, PowerRailInfo> &GetPowerRailInfoMap() const = 0;
99     virtual const std::unordered_map<std::string, PowerStatus> &GetPowerStatusMap() const = 0;
100     virtual const std::unordered_map<std::string, SensorTempStats> GetSensorTempStatsSnapshot() = 0;
101     virtual const std::unordered_map<std::string,
102                                      std::unordered_map<std::string, ThermalStats<int>>>
103     GetSensorCoolingDeviceRequestStatsSnapshot() = 0;
104     virtual bool isAidlPowerHalExist() = 0;
105     virtual bool isPowerHalConnected() = 0;
106     virtual bool isPowerHalExtConnected() = 0;
107 };
108 
109 class ThermalHelperImpl : public ThermalHelper {
110   public:
111     explicit ThermalHelperImpl(const NotificationCallback &cb);
112     ~ThermalHelperImpl() override = default;
113 
114     bool fillCurrentTemperatures(bool filterType, bool filterCallback, TemperatureType type,
115                                  std::vector<Temperature> *temperatures) override;
116     bool fillTemperatureThresholds(bool filterType, TemperatureType type,
117                                    std::vector<TemperatureThreshold> *thresholds) const override;
118     bool fillCurrentCoolingDevices(bool filterType, CoolingType type,
119                                    std::vector<CoolingDevice> *coolingdevices) const override;
120     bool emulTemp(std::string_view target_sensor, const float temp) override;
121     bool emulSeverity(std::string_view target_sensor, const int severity) override;
122     bool emulClear(std::string_view target_sensor) override;
123 
124     // Disallow copy and assign.
125     ThermalHelperImpl(const ThermalHelperImpl &) = delete;
126     void operator=(const ThermalHelperImpl &) = delete;
127 
isInitializedOk()128     bool isInitializedOk() const override { return is_initialized_; }
129 
130     // Read the temperature of a single sensor.
131     bool readTemperature(
132             std::string_view sensor_name, Temperature *out,
133             std::pair<ThrottlingSeverity, ThrottlingSeverity> *throtting_status = nullptr,
134             const bool force_sysfs = false) override;
135 
136     bool readTemperatureThreshold(std::string_view sensor_name,
137                                   TemperatureThreshold *out) const override;
138     // Read the value of a single cooling device.
139     bool readCoolingDevice(std::string_view cooling_device, CoolingDevice *out) const override;
140     // Get SensorInfo Map
GetSensorInfoMap()141     const std::unordered_map<std::string, SensorInfo> &GetSensorInfoMap() const override {
142         return sensor_info_map_;
143     }
144     // Get CdevInfo Map
GetCdevInfoMap()145     const std::unordered_map<std::string, CdevInfo> &GetCdevInfoMap() const override {
146         return cooling_device_info_map_;
147     }
148     // Get SensorStatus Map
GetSensorStatusMap()149     const std::unordered_map<std::string, SensorStatus> &GetSensorStatusMap() const override {
150         std::shared_lock<std::shared_mutex> _lock(sensor_status_map_mutex_);
151         return sensor_status_map_;
152     }
153     // Get ThermalThrottling Map
GetThermalThrottlingStatusMap()154     const std::unordered_map<std::string, ThermalThrottlingStatus> &GetThermalThrottlingStatusMap()
155             const override {
156         return thermal_throttling_.GetThermalThrottlingStatusMap();
157     }
158     // Get PowerRailInfo Map
GetPowerRailInfoMap()159     const std::unordered_map<std::string, PowerRailInfo> &GetPowerRailInfoMap() const override {
160         return power_files_.GetPowerRailInfoMap();
161     }
162 
163     // Get PowerStatus Map
GetPowerStatusMap()164     const std::unordered_map<std::string, PowerStatus> &GetPowerStatusMap() const override {
165         return power_files_.GetPowerStatusMap();
166     }
167 
168     // Get Thermal Stats Sensor Map
GetSensorTempStatsSnapshot()169     const std::unordered_map<std::string, SensorTempStats> GetSensorTempStatsSnapshot() override {
170         return thermal_stats_helper_.GetSensorTempStatsSnapshot();
171     }
172     // Get Thermal Stats Sensor, Binded Cdev State Request Map
173     const std::unordered_map<std::string, std::unordered_map<std::string, ThermalStats<int>>>
GetSensorCoolingDeviceRequestStatsSnapshot()174     GetSensorCoolingDeviceRequestStatsSnapshot() override {
175         return thermal_stats_helper_.GetSensorCoolingDeviceRequestStatsSnapshot();
176     }
177 
isAidlPowerHalExist()178     bool isAidlPowerHalExist() override { return power_hal_service_.isAidlPowerHalExist(); }
isPowerHalConnected()179     bool isPowerHalConnected() override { return power_hal_service_.isPowerHalConnected(); }
isPowerHalExtConnected()180     bool isPowerHalExtConnected() override { return power_hal_service_.isPowerHalExtConnected(); }
181 
182   private:
183     bool initializeSensorMap(const std::unordered_map<std::string, std::string> &path_map);
184     bool initializeCoolingDevices(const std::unordered_map<std::string, std::string> &path_map);
185     bool isSubSensorValid(std::string_view sensor_data, const SensorFusionType sensor_fusion_type);
186     void setMinTimeout(SensorInfo *sensor_info);
187     void initializeTrip(const std::unordered_map<std::string, std::string> &path_map,
188                         std::set<std::string> *monitored_sensors, bool thermal_genl_enabled);
189     void clearAllThrottling();
190     // For thermal_watcher_'s polling thread, return the sleep interval
191     std::chrono::milliseconds thermalWatcherCallbackFunc(
192             const std::set<std::string> &uevent_sensors);
193     // Return hot and cold severity status as std::pair
194     std::pair<ThrottlingSeverity, ThrottlingSeverity> getSeverityFromThresholds(
195             const ThrottlingArray &hot_thresholds, const ThrottlingArray &cold_thresholds,
196             const ThrottlingArray &hot_hysteresis, const ThrottlingArray &cold_hysteresis,
197             ThrottlingSeverity prev_hot_severity, ThrottlingSeverity prev_cold_severity,
198             float value) const;
199     // Read sensor data according to the type
200     bool readDataByType(std::string_view sensor_data, float *reading_value,
201                         const SensorFusionType type, const bool force_no_cache,
202                         std::map<std::string, float> *sensor_log_map);
203     // Read temperature data according to thermal sensor's info
204     bool readThermalSensor(std::string_view sensor_name, float *temp, const bool force_sysfs,
205                            std::map<std::string, float> *sensor_log_map);
206     void updateCoolingDevices(const std::vector<std::string> &cooling_devices_to_update);
207     // Check the max CDEV state for cdev_ceiling
208     void maxCoolingRequestCheck(
209             std::unordered_map<std::string, BindedCdevInfo> *binded_cdev_info_map);
210     sp<ThermalWatcher> thermal_watcher_;
211     PowerFiles power_files_;
212     ThermalFiles thermal_sensors_;
213     ThermalFiles cooling_devices_;
214     ThermalThrottling thermal_throttling_;
215     bool is_initialized_;
216     const NotificationCallback cb_;
217     std::unordered_map<std::string, CdevInfo> cooling_device_info_map_;
218     std::unordered_map<std::string, SensorInfo> sensor_info_map_;
219     std::unordered_map<std::string, std::unordered_map<ThrottlingSeverity, ThrottlingSeverity>>
220             supported_powerhint_map_;
221     PowerHalService power_hal_service_;
222     ThermalStatsHelper thermal_stats_helper_;
223     mutable std::shared_mutex sensor_status_map_mutex_;
224     std::unordered_map<std::string, SensorStatus> sensor_status_map_;
225 };
226 
227 }  // namespace implementation
228 }  // namespace thermal
229 }  // namespace hardware
230 }  // namespace android
231 }  // namespace aidl
232