summaryrefslogtreecommitdiff
path: root/thermal/utils/thermal_throttling.h
blob: cac7f8dc8ed8a3444e42334fed1295b64ec99a74 (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
139
140
141
142
143
144
/*
 * 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 <aidl/android/hardware/thermal/Temperature.h>

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

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

namespace aidl {
namespace android {
namespace hardware {
namespace thermal {
namespace implementation {

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;
    std::string profile;
};

// 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);
    // 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 &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 = false);

    // 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,
                                      ThermalStatsHelper *thermal_stats_helper);
    // Get the aggregated (from all sensor) max request for a cooling device
    bool getCdevMaxRequest(std::string_view cdev_name, int *max_state);

  private:
    // Check if the thermal throttling profile need to be switched
    void parseProfileProperty(std::string_view sensor_name, const SensorInfo &sensor_info);
    // PID algo - get the total power budget
    float updatePowerBudget(const Temperature &temp, const SensorInfo &sensor_info,
                            std::chrono::milliseconds time_elapsed_ms,
                            ThrottlingSeverity curr_severity, const bool max_throttling);

    // 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, std::string_view sensor_name);

    // PID algo - allocate the power to target CDEV according to the ODPM
    bool 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);
    // 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);
    // Update the cooling device request set for new request and notify the caller if there is
    // change in max_request for the cooling device.
    bool updateCdevMaxRequestAndNotifyIfChange(std::string_view cdev_name, int cur_request,
                                               int new_request);
    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::shared_mutex cdev_all_request_map_mutex_;
    // Set of all request for a cooling device from each sensor
    std::unordered_map<std::string, std::multiset<int, std::greater<int>>> cdev_all_request_map_;
};

}  // namespace implementation
}  // namespace thermal
}  // namespace hardware
}  // namespace android
}  // namespace aidl