summaryrefslogtreecommitdiff
path: root/thermal/utils/thermal_throttling.h
blob: d373dd3660c97df0e53173e3271af1339c5f11df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <android/hardware/thermal/2.0/IThermal.h>

#include <queue>
#include <shared_mutex>
#include <string>
#include <unordered_map>
#include <unordered_set>

#include "power_files.h"
#include "thermal_info.h"

namespace android {
namespace hardware {
namespace thermal {
namespace V2_0 {
namespace implementation {

using ::android::hardware::hidl_vec;
using ::android::hardware::thermal::V2_0::IThermal;
using Temperature_2_0 = ::android::hardware::thermal::V2_0::Temperature;
using ::android::hardware::thermal::V2_0::TemperatureThreshold;
using ::android::hardware::thermal::V2_0::ThrottlingSeverity;

struct ThermalThrottlingStatus {
    std::unordered_map<std::string, int> pid_power_budget_map;
    std::unordered_map<std::string, int> pid_cdev_request_map;
    std::unordered_map<std::string, int> hardlimit_cdev_request_map;
    std::unordered_map<std::string, int> throttling_release_map;
    std::unordered_map<std::string, int> cdev_status_map;
    float prev_err;
    float i_budget;
    float prev_target;
    float prev_power_budget;
    float budget_transient;
    int tran_cycle;
};

// Return the control temp target of PID algorithm
size_t getTargetStateOfPID(const SensorInfo &sensor_info, const ThrottlingSeverity curr_severity);

// A helper class for conducting thermal throttling
class ThermalThrottling {
  public:
    ThermalThrottling() = default;
    ~ThermalThrottling() = default;
    // Disallow copy and assign.
    ThermalThrottling(const ThermalThrottling &) = delete;
    void operator=(const ThermalThrottling &) = delete;

    // Clear throttling data
    void clearThrottlingData(std::string_view sensor_name, const SensorInfo &sensor_info);
    // Register map for throttling algo
    bool registerThermalThrottling(
            std::string_view sensor_name, const std::shared_ptr<ThrottlingInfo> &throttling_info,
            const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map);
    // Register map for throttling release algo
    bool registerThrottlingReleaseToWatch(std::string_view sensor_name, std::string_view cdev_name,
                                          const BindedCdevInfo &binded_cdev_info);
    // Get throttling status map
    const std::unordered_map<std::string, ThermalThrottlingStatus> &GetThermalThrottlingStatusMap()
            const {
        std::shared_lock<std::shared_mutex> _lock(thermal_throttling_status_map_mutex_);
        return thermal_throttling_status_map_;
    }
    // Update thermal throttling request for the specific sensor
    void thermalThrottlingUpdate(
            const Temperature_2_0 &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);

    // Compute the throttling target from all the sensors' request
    void computeCoolingDevicesRequest(std::string_view sensor_name, const SensorInfo &sensor_info,
                                      const ThrottlingSeverity curr_severity,
                                      std::vector<std::string> *cooling_devices_to_update);

  private:
    // PID algo - get the total power budget
    float updatePowerBudget(const Temperature_2_0 &temp, const SensorInfo &sensor_info,
                            std::chrono::milliseconds time_elapsed_ms,
                            ThrottlingSeverity curr_severity);

    // PID algo - return the power number from excluded power rail list
    float computeExcludedPower(const SensorInfo &sensor_info,
                               const ThrottlingSeverity curr_severity,
                               const std::unordered_map<std::string, PowerStatus> &power_status_map,
                               std::string *log_buf);

    // PID algo - allocate the power to target CDEV according to the ODPM
    bool allocatePowerToCdev(
            const Temperature_2_0 &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);
    // PID algo - map the target throttling state according to the power budget
    void updateCdevRequestByPower(
            std::string sensor_name,
            const std::unordered_map<std::string, CdevInfo> &cooling_device_info_map);
    // Hard limit algo - assign the throttling state according to the severity
    void updateCdevRequestBySeverity(std::string_view sensor_name, const SensorInfo &sensor_info,
                                     ThrottlingSeverity curr_severity);
    // Throttling release algo - decide release step according to the predefined power threshold,
    // return false if the throttling release is not registered in thermal config
    bool 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);

    mutable std::shared_mutex thermal_throttling_status_map_mutex_;
    // Thermal throttling status from each sensor
    std::unordered_map<std::string, ThermalThrottlingStatus> thermal_throttling_status_map_;
    std::vector<std::string> cooling_devices_to_update;
};

}  // namespace implementation
}  // namespace V2_0
}  // namespace thermal
}  // namespace hardware
}  // namespace android