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 #define LOG_TAG "mitigation-logger"
17
18 #include <android-base/chrono_utils.h>
19 #include <android-base/file.h>
20 #include <android-base/logging.h>
21 #include <android-base/parseint.h>
22 #include <android-base/properties.h>
23 #include <android-base/strings.h>
24 #include <battery_mitigation/MitigationThermalManager.h>
25 #include <errno.h>
26 #include <sys/time.h>
27
28 #include <chrono>
29 #include <cmath>
30 #include <ctime>
31 #include <iomanip>
32 #include <iostream>
33 #include <string>
34
35 #define NUM_OF_SAMPLES 20
36 #define CAPTURE_INTERVAL_S 2 /* 2 seconds between new capture */
37
38 namespace android {
39 namespace hardware {
40 namespace google {
41 namespace pixel {
42
43 using android::hardware::thermal::V1_0::ThermalStatus;
44 using android::hardware::thermal::V1_0::ThermalStatusCode;
45
getInstance()46 MitigationThermalManager &MitigationThermalManager::getInstance() {
47 static MitigationThermalManager mitigationThermalManager;
48 return mitigationThermalManager;
49 }
50
remove()51 void MitigationThermalManager::remove() {
52 if (!thermal) {
53 return;
54 }
55 if (callback) {
56 ThermalStatus returnStatus;
57 auto ret = thermal->unregisterThermalChangedCallback(
58 callback, [&returnStatus](ThermalStatus status) { returnStatus = status; });
59 if (!ret.isOk() || returnStatus.code != ThermalStatusCode::SUCCESS) {
60 LOG(ERROR) << "Failed to release thermal callback!";
61 }
62 }
63 if (deathRecipient) {
64 auto ret = thermal->unlinkToDeath(deathRecipient);
65 if (!ret.isOk()) {
66 LOG(ERROR) << "Failed to release thermal death notification!";
67 }
68 }
69 }
70
MitigationThermalManager()71 MitigationThermalManager::MitigationThermalManager() {
72 if (!connectThermalHal()) {
73 remove();
74 }
75 }
76
~MitigationThermalManager()77 MitigationThermalManager::~MitigationThermalManager() {
78 remove();
79 }
80
updateConfig(const struct MitigationConfig::Config & cfg)81 void MitigationThermalManager::updateConfig(const struct MitigationConfig::Config &cfg) {
82 kLogFilePath = std::string(cfg.LogFilePath);
83 kSystemPath = cfg.SystemPath;
84 kSystemName = cfg.SystemName;
85 kFilteredZones = cfg.FilteredZones;
86 kTimestampFormat = cfg.TimestampFormat;
87 }
88
connectThermalHal()89 bool MitigationThermalManager::connectThermalHal() {
90 thermal = IThermal::getService();
91 if (thermal) {
92 lastCapturedTime = ::android::base::boot_clock::now();
93 registerCallback();
94 return true;
95 } else {
96 LOG(ERROR) << "Cannot get IThermal service!";
97 }
98 return false;
99 }
100
isMitigationTemperature(const Temperature & temperature)101 bool MitigationThermalManager::isMitigationTemperature(const Temperature &temperature) {
102 if (std::find(kFilteredZones.begin(), kFilteredZones.end(), temperature.name) !=
103 kFilteredZones.end()) {
104 return true;
105 }
106 return false;
107 }
108
thermalCb(const Temperature & temperature)109 void MitigationThermalManager::thermalCb(const Temperature &temperature) {
110 if ((temperature.throttlingStatus == ThrottlingSeverity::NONE) ||
111 (!isMitigationTemperature(temperature))) {
112 return;
113 }
114 auto currentTime = ::android::base::boot_clock::now();
115 auto delta =
116 std::chrono::duration_cast<std::chrono::seconds>(currentTime - lastCapturedTime);
117 if (delta.count() < CAPTURE_INTERVAL_S) {
118 /* Do not log if delta is within 2 seconds */
119 return;
120 }
121 int flag = O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_APPEND | O_TRUNC;
122 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(kLogFilePath.c_str(), flag, 0644)));
123 lastCapturedTime = currentTime;
124 std::stringstream oss;
125 oss << temperature.name << " triggered at " << temperature.value << std::endl << std::flush;
126 android::base::WriteStringToFd(oss.str(), fd);
127 fsync(fd);
128
129 for (int i = 0; i < NUM_OF_SAMPLES; i++) {
130 auto now = std::chrono::system_clock::now();
131 auto time_sec = std::chrono::system_clock::to_time_t(now);
132 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) -
133 std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
134 struct tm now_tm;
135 localtime_r(&time_sec, &now_tm);
136 oss << std::put_time(&now_tm, kTimestampFormat.c_str()) << "." << std::setw(3)
137 << std::setfill('0') << ms.count() << std::endl
138 << std::flush;
139 android::base::WriteStringToFd(oss.str(), fd);
140 fsync(fd);
141 oss.str("");
142 /* log System info */
143 for (int j = 0; j < kSystemName.size(); j++) {
144 std::string value;
145 bool result = android::base::ReadFileToString(kSystemPath[j], &value);
146 if (!result) {
147 LOG(ERROR) << "Could not read: " << kSystemName[j];
148 }
149 android::base::WriteStringToFd(kSystemName[j] + ":" + value, fd);
150 }
151 }
152 fsync(fd);
153 }
154
registerCallback()155 void MitigationThermalManager::registerCallback() {
156 if (!thermal) {
157 LOG(ERROR) << "Cannot register thermal callback!";
158 return;
159 }
160 ThermalStatus returnStatus;
161 // Create thermal death recipient object.
162 if (deathRecipient == nullptr) {
163 deathRecipient = new MitigationThermalManager::ThermalDeathRecipient();
164 }
165 // Create thermal callback recipient object.
166 if (callback == nullptr) {
167 callback = new MitigationThermalManager::ThermalCallback(
168 [this](const Temperature &temperature) {
169 std::lock_guard api_lock(mutex_);
170 thermalCb(temperature);
171 });
172 }
173 // Register thermal callback SKIN to thermal hal to cover all. Cannot register twice.
174 auto ret_callback = thermal->registerThermalChangedCallback(
175 callback, false, TemperatureType::SKIN,
176 [&returnStatus](ThermalStatus status) { return returnStatus = status; });
177 if (!ret_callback.isOk() || returnStatus.code != ThermalStatusCode::SUCCESS) {
178 LOG(ERROR) << "Failed to register thermal callback!";
179 }
180 // Register thermal death notification to thermal hal.
181 auto retLink = thermal->linkToDeath(deathRecipient, 0);
182 if (!retLink.isOk()) {
183 LOG(ERROR) << "Failed to register thermal death notification!";
184 }
185 }
186
187 } // namespace pixel
188 } // namespace google
189 } // namespace hardware
190 } // namespace android
191