• 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 EmulTemp {
59     float temp;
60     int severity;
61 };
62 
63 struct OverrideStatus {
64     std::unique_ptr<EmulTemp> emul_temp;
65     bool max_throttling;
66     bool pending_update;
67 };
68 
69 struct SensorStatus {
70     ThrottlingSeverity severity;
71     ThrottlingSeverity prev_hot_severity;
72     ThrottlingSeverity prev_cold_severity;
73     boot_clock::time_point last_update_time;
74     ThermalSample thermal_cached;
75     OverrideStatus override_status;
76 };
77 
78 class ThermalHelper {
79   public:
80     virtual ~ThermalHelper() = default;
81     virtual bool fillCurrentTemperatures(bool filterType, bool filterCallback, TemperatureType type,
82                                          std::vector<Temperature> *temperatures) = 0;
83     virtual bool fillTemperatureThresholds(bool filterType, TemperatureType type,
84                                            std::vector<TemperatureThreshold> *thresholds) const = 0;
85     virtual bool fillCurrentCoolingDevices(bool filterType, CoolingType type,
86                                            std::vector<CoolingDevice> *coolingdevices) const = 0;
87     virtual bool emulTemp(std::string_view target_sensor, const float temp,
88                           const bool max_throttling) = 0;
89     virtual bool emulSeverity(std::string_view target_sensor, const int severity,
90                               const bool max_throttling) = 0;
91     virtual bool emulClear(std::string_view target_sensor) = 0;
92     virtual bool isInitializedOk() const = 0;
93     virtual bool readTemperature(
94             std::string_view sensor_name, Temperature *out,
95             std::pair<ThrottlingSeverity, ThrottlingSeverity> *throtting_status = nullptr,
96             const bool force_sysfs = false) = 0;
97     virtual bool readTemperatureThreshold(std::string_view sensor_name,
98                                           TemperatureThreshold *out) const = 0;
99     virtual bool readCoolingDevice(std::string_view cooling_device, CoolingDevice *out) const = 0;
100     virtual void dumpVtEstimatorStatus(std::string_view senor_name,
101                                        std::ostringstream *dump_buf) const = 0;
102     virtual const std::unordered_map<std::string, SensorInfo> &GetSensorInfoMap() const = 0;
103     virtual const std::unordered_map<std::string, CdevInfo> &GetCdevInfoMap() const = 0;
104     virtual const std::unordered_map<std::string, SensorStatus> &GetSensorStatusMap() const = 0;
105     virtual const std::unordered_map<std::string, ThermalThrottlingStatus> &
106     GetThermalThrottlingStatusMap() const = 0;
107     virtual const std::unordered_map<std::string, PowerRailInfo> &GetPowerRailInfoMap() const = 0;
108     virtual const std::unordered_map<std::string, PowerStatus> &GetPowerStatusMap() const = 0;
109     virtual const std::unordered_map<std::string, SensorTempStats> GetSensorTempStatsSnapshot() = 0;
110     virtual const std::unordered_map<std::string,
111                                      std::unordered_map<std::string, ThermalStats<int>>>
112     GetSensorCoolingDeviceRequestStatsSnapshot() = 0;
113     virtual bool isAidlPowerHalExist() = 0;
114     virtual bool isPowerHalConnected() = 0;
115     virtual bool isPowerHalExtConnected() = 0;
116     virtual void dumpTraces(std::string_view target_sensor) = 0;
117 };
118 
119 class ThermalHelperImpl : public ThermalHelper {
120   public:
121     explicit ThermalHelperImpl(const NotificationCallback &cb);
122     ~ThermalHelperImpl() override = default;
123 
124     bool fillCurrentTemperatures(bool filterType, bool filterCallback, TemperatureType type,
125                                  std::vector<Temperature> *temperatures) override;
126     bool fillTemperatureThresholds(bool filterType, TemperatureType type,
127                                    std::vector<TemperatureThreshold> *thresholds) const override;
128     bool fillCurrentCoolingDevices(bool filterType, CoolingType type,
129                                    std::vector<CoolingDevice> *coolingdevices) const override;
130     bool emulTemp(std::string_view target_sensor, const float temp,
131                   const bool max_throttling) override;
132     bool emulSeverity(std::string_view target_sensor, const int severity,
133                       const bool max_throttling) override;
134     bool emulClear(std::string_view target_sensor) override;
135     void dumpTraces(std::string_view target_sensor) override;
136 
137     // Disallow copy and assign.
138     ThermalHelperImpl(const ThermalHelperImpl &) = delete;
139     void operator=(const ThermalHelperImpl &) = delete;
140 
isInitializedOk()141     bool isInitializedOk() const override { return is_initialized_; }
142 
143     // Read the temperature of a single sensor.
144     bool readTemperature(
145             std::string_view sensor_name, Temperature *out,
146             std::pair<ThrottlingSeverity, ThrottlingSeverity> *throtting_status = nullptr,
147             const bool force_sysfs = false) override;
148 
149     bool readTemperatureThreshold(std::string_view sensor_name,
150                                   TemperatureThreshold *out) const override;
151     // Read the value of a single cooling device.
152     bool readCoolingDevice(std::string_view cooling_device, CoolingDevice *out) const override;
153     // Dump VtEstimator status
154     void dumpVtEstimatorStatus(std::string_view sensor_name,
155                                std::ostringstream *dump_buf) const override;
156     // Get SensorInfo Map
GetSensorInfoMap()157     const std::unordered_map<std::string, SensorInfo> &GetSensorInfoMap() const override {
158         return sensor_info_map_;
159     }
160     // Get CdevInfo Map
GetCdevInfoMap()161     const std::unordered_map<std::string, CdevInfo> &GetCdevInfoMap() const override {
162         return cooling_device_info_map_;
163     }
164     // Get SensorStatus Map
GetSensorStatusMap()165     const std::unordered_map<std::string, SensorStatus> &GetSensorStatusMap() const override {
166         std::shared_lock<std::shared_mutex> _lock(sensor_status_map_mutex_);
167         return sensor_status_map_;
168     }
169     // Get ThermalThrottling Map
GetThermalThrottlingStatusMap()170     const std::unordered_map<std::string, ThermalThrottlingStatus> &GetThermalThrottlingStatusMap()
171             const override {
172         return thermal_throttling_.GetThermalThrottlingStatusMap();
173     }
174     // Get PowerRailInfo Map
GetPowerRailInfoMap()175     const std::unordered_map<std::string, PowerRailInfo> &GetPowerRailInfoMap() const override {
176         return power_files_.GetPowerRailInfoMap();
177     }
178 
179     // Get PowerStatus Map
GetPowerStatusMap()180     const std::unordered_map<std::string, PowerStatus> &GetPowerStatusMap() const override {
181         return power_files_.GetPowerStatusMap();
182     }
183 
184     // Get Thermal Stats Sensor Map
GetSensorTempStatsSnapshot()185     const std::unordered_map<std::string, SensorTempStats> GetSensorTempStatsSnapshot() override {
186         return thermal_stats_helper_.GetSensorTempStatsSnapshot();
187     }
188     // Get Thermal Stats Sensor, Binded Cdev State Request Map
189     const std::unordered_map<std::string, std::unordered_map<std::string, ThermalStats<int>>>
GetSensorCoolingDeviceRequestStatsSnapshot()190     GetSensorCoolingDeviceRequestStatsSnapshot() override {
191         return thermal_stats_helper_.GetSensorCoolingDeviceRequestStatsSnapshot();
192     }
193 
isAidlPowerHalExist()194     bool isAidlPowerHalExist() override { return power_hal_service_.isAidlPowerHalExist(); }
isPowerHalConnected()195     bool isPowerHalConnected() override { return power_hal_service_.isPowerHalConnected(); }
isPowerHalExtConnected()196     bool isPowerHalExtConnected() override { return power_hal_service_.isPowerHalExtConnected(); }
197 
198   private:
199     bool initializeSensorMap(const std::unordered_map<std::string, std::string> &path_map);
200     bool initializeCoolingDevices(const std::unordered_map<std::string, std::string> &path_map);
201     bool isSubSensorValid(std::string_view sensor_data, const SensorFusionType sensor_fusion_type);
202     void setMinTimeout(SensorInfo *sensor_info);
203     void initializeTrip(const std::unordered_map<std::string, std::string> &path_map,
204                         std::set<std::string> *monitored_sensors, bool thermal_genl_enabled);
205     void clearAllThrottling();
206     // For thermal_watcher_'s polling thread, return the sleep interval
207     std::chrono::milliseconds thermalWatcherCallbackFunc(
208             const std::set<std::string> &uevent_sensors);
209     // Return hot and cold severity status as std::pair
210     std::pair<ThrottlingSeverity, ThrottlingSeverity> getSeverityFromThresholds(
211             const ThrottlingArray &hot_thresholds, const ThrottlingArray &cold_thresholds,
212             const ThrottlingArray &hot_hysteresis, const ThrottlingArray &cold_hysteresis,
213             ThrottlingSeverity prev_hot_severity, ThrottlingSeverity prev_cold_severity,
214             float value) const;
215     // Read sensor data according to the type
216     bool readDataByType(std::string_view sensor_data, float *reading_value,
217                         const SensorFusionType type, const bool force_no_cache,
218                         std::map<std::string, float> *sensor_log_map);
219     bool readThermalSensor(std::string_view sensor_name, float *temp, const bool force_sysfs,
220                            std::map<std::string, float> *sensor_log_map);
221     bool runVirtualTempEstimator(std::string_view sensor_name,
222                                  std::map<std::string, float> *sensor_log_map,
223                                  const bool force_no_cache, std::vector<float> *outputs);
224     size_t getPredictionMaxWindowMs(std::string_view sensor_name);
225     float readPredictionAfterTimeMs(std::string_view sensor_name, const size_t time_ms);
226     bool readTemperaturePredictions(std::string_view sensor_name, std::vector<float> *predictions);
227     void updateCoolingDevices(const std::vector<std::string> &cooling_devices_to_update);
228     // Check the max CDEV state for cdev_ceiling
229     void maxCoolingRequestCheck(
230             std::unordered_map<std::string, BindedCdevInfo> *binded_cdev_info_map);
231     void checkUpdateSensorForEmul(std::string_view target_sensor, const bool max_throttling);
232     sp<ThermalWatcher> thermal_watcher_;
233     PowerFiles power_files_;
234     ThermalFiles thermal_sensors_;
235     ThermalFiles cooling_devices_;
236     ThermalThrottling thermal_throttling_;
237     bool is_initialized_;
238     const NotificationCallback cb_;
239     std::unordered_map<std::string, CdevInfo> cooling_device_info_map_;
240     std::unordered_map<std::string, SensorInfo> sensor_info_map_;
241     std::unordered_map<std::string, std::unordered_map<ThrottlingSeverity, ThrottlingSeverity>>
242             supported_powerhint_map_;
243     PowerHalService power_hal_service_;
244     ThermalStatsHelper thermal_stats_helper_;
245     mutable std::shared_mutex sensor_status_map_mutex_;
246     std::unordered_map<std::string, SensorStatus> sensor_status_map_;
247 };
248 
249 }  // namespace implementation
250 }  // namespace thermal
251 }  // namespace hardware
252 }  // namespace android
253 }  // namespace aidl
254