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 #define LOG_TAG "pixelstats: BrownoutDetected"
18
19 #include <aidl/android/frameworks/stats/IStats.h>
20 #include <android-base/file.h>
21 #include <android-base/parseint.h>
22 #include <android-base/properties.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 #include <android/binder_manager.h>
26 #include <hardware/google/pixel/pixelstats/pixelatoms.pb.h>
27 #include <pixelstats/BrownoutDetectedReporter.h>
28 #include <time.h>
29 #include <utils/Log.h>
30
31 #include <map>
32 #include <regex>
33
34 namespace android {
35 namespace hardware {
36 namespace google {
37 namespace pixel {
38
39 using aidl::android::frameworks::stats::IStats;
40 using aidl::android::frameworks::stats::VendorAtom;
41 using aidl::android::frameworks::stats::VendorAtomValue;
42 using android::base::ReadFileToString;
43 using android::hardware::google::pixel::PixelAtoms::BrownoutDetected;
44
45 #define READING_IDX 2
46 #define KEY_IDX 0
47 #define DEFAULT_BATTERY_TEMP 9999999
48 #define DEFAULT_BATTERY_SOC 100
49 #define DEFAULT_BATTERY_VOLT 5000000
50 #define ONE_SECOND_IN_US 1000000
51
52 const std::regex kTimestampPattern("^\\S+\\s[0-9]+:[0-9]+:[0-9]+\\S+$");
53 const std::regex kIrqPattern("^(\\S+)\\striggered\\sat\\s\\S+$");
54 const std::regex kOdpmPattern("^CH\\d+\\[(\\S+)\\],\\s(\\d+)$");
55 const std::regex kDvfsPattern("^([A-Z1-9]+):(\\d+)$");
56 const std::regex kFgPattern("^(voltage_now):(\\d+)$");
57 const std::regex kBatteryTempPattern("^(battery):(\\d+)$");
58 const std::regex kBatteryCyclePattern("^(battery_cycle):(\\d+)$");
59 const std::regex kBatterySocPattern("^(soc):(\\d+)$");
60 const std::regex kAlreadyUpdatedPattern("^(LASTMEAL_UPDATED)$");
61
62 const std::map<std::string, int> kBrownoutReason = {{"uvlo,pmic,if", BrownoutDetected::UVLO_IF},
63 {"ocp,pmic,if", BrownoutDetected::OCP_IF},
64 {"ocp2,pmic,if", BrownoutDetected::OCP2_IF},
65 {"uvlo,pmic,main", BrownoutDetected::UVLO_MAIN},
66 {"uvlo,pmic,sub", BrownoutDetected::UVLO_SUB},
67 {"ocp,buck1m", BrownoutDetected::OCP_B1M},
68 {"ocp,buck2m", BrownoutDetected::OCP_B2M},
69 {"ocp,buck3m", BrownoutDetected::OCP_B3M},
70 {"ocp,buck4m", BrownoutDetected::OCP_B4M},
71 {"ocp,buck5m", BrownoutDetected::OCP_B5M},
72 {"ocp,buck6m", BrownoutDetected::OCP_B6M},
73 {"ocp,buck7m", BrownoutDetected::OCP_B7M},
74 {"ocp,buck8m", BrownoutDetected::OCP_B8M},
75 {"ocp,buck9m", BrownoutDetected::OCP_B9M},
76 {"ocp,buck10m", BrownoutDetected::OCP_B10M},
77 {"ocp,buck1s", BrownoutDetected::OCP_B1S},
78 {"ocp,buck2s", BrownoutDetected::OCP_B2S},
79 {"ocp,buck3s", BrownoutDetected::OCP_B3S},
80 {"ocp,buck4s", BrownoutDetected::OCP_B4S},
81 {"ocp,buck5s", BrownoutDetected::OCP_B5S},
82 {"ocp,buck6s", BrownoutDetected::OCP_B6S},
83 {"ocp,buck7s", BrownoutDetected::OCP_B7S},
84 {"ocp,buck8s", BrownoutDetected::OCP_B8S},
85 {"ocp,buck9s", BrownoutDetected::OCP_B9S},
86 {"ocp,buck10s", BrownoutDetected::OCP_B10S},
87 {"ocp,buckas", BrownoutDetected::OCP_BAS},
88 {"ocp,buckbs", BrownoutDetected::OCP_BBS},
89 {"ocp,buckcs", BrownoutDetected::OCP_BCS},
90 {"ocp,buckds", BrownoutDetected::OCP_BDS}};
91
updateIfFound(std::string line,std::regex pattern,int * current_value,Update flag)92 bool BrownoutDetectedReporter::updateIfFound(std::string line, std::regex pattern,
93 int *current_value, Update flag) {
94 bool found = false;
95 std::smatch pattern_match;
96 if (std::regex_match(line, pattern_match, pattern)) {
97 if (pattern_match.size() < (READING_IDX + 1)) {
98 return found;
99 }
100 found = true;
101 int reading = std::stoi(pattern_match[READING_IDX].str());
102 if (flag == kUpdateMax) {
103 if (*current_value < reading) {
104 *current_value = reading;
105 }
106 } else {
107 if (*current_value > reading) {
108 *current_value = reading;
109 }
110 }
111 }
112 return found;
113 }
114
setAtomFieldValue(std::vector<VendorAtomValue> * values,int offset,int content)115 void BrownoutDetectedReporter::setAtomFieldValue(std::vector<VendorAtomValue> *values, int offset,
116 int content) {
117 std::vector<VendorAtomValue> &val = *values;
118 if (offset - kVendorAtomOffset < val.size()) {
119 val[offset - kVendorAtomOffset].set<VendorAtomValue::intValue>(content);
120 }
121 }
122
uploadData(const std::shared_ptr<IStats> & stats_client,const struct BrownoutDetectedInfo max_value)123 void BrownoutDetectedReporter::uploadData(const std::shared_ptr<IStats> &stats_client,
124 const struct BrownoutDetectedInfo max_value) {
125 // Load values array
126 VendorAtomValue tmp;
127 std::vector<VendorAtomValue> values(47);
128 setAtomFieldValue(&values, BrownoutDetected::kTriggeredIrqFieldNumber,
129 max_value.triggered_irq_);
130 setAtomFieldValue(&values, BrownoutDetected::kTriggeredTimestampFieldNumber,
131 max_value.triggered_timestamp_);
132 setAtomFieldValue(&values, BrownoutDetected::kBatteryTempFieldNumber, max_value.battery_temp_);
133 setAtomFieldValue(&values, BrownoutDetected::kBatterySocFieldNumber,
134 100 - max_value.battery_soc_);
135 setAtomFieldValue(&values, BrownoutDetected::kBatteryCycleFieldNumber,
136 max_value.battery_cycle_);
137 setAtomFieldValue(&values, BrownoutDetected::kVoltageNowFieldNumber, max_value.voltage_now_);
138
139 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel01FieldNumber,
140 max_value.odpm_value_[0]);
141 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel02FieldNumber,
142 max_value.odpm_value_[1]);
143 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel03FieldNumber,
144 max_value.odpm_value_[2]);
145 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel04FieldNumber,
146 max_value.odpm_value_[3]);
147 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel05FieldNumber,
148 max_value.odpm_value_[4]);
149 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel06FieldNumber,
150 max_value.odpm_value_[5]);
151 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel07FieldNumber,
152 max_value.odpm_value_[6]);
153 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel08FieldNumber,
154 max_value.odpm_value_[7]);
155 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel09FieldNumber,
156 max_value.odpm_value_[8]);
157 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel10FieldNumber,
158 max_value.odpm_value_[9]);
159 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel11FieldNumber,
160 max_value.odpm_value_[10]);
161 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel12FieldNumber,
162 max_value.odpm_value_[11]);
163 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel13FieldNumber,
164 max_value.odpm_value_[12]);
165 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel14FieldNumber,
166 max_value.odpm_value_[13]);
167 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel15FieldNumber,
168 max_value.odpm_value_[14]);
169 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel16FieldNumber,
170 max_value.odpm_value_[15]);
171 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel17FieldNumber,
172 max_value.odpm_value_[16]);
173 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel18FieldNumber,
174 max_value.odpm_value_[17]);
175 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel19FieldNumber,
176 max_value.odpm_value_[18]);
177 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel20FieldNumber,
178 max_value.odpm_value_[19]);
179 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel21FieldNumber,
180 max_value.odpm_value_[20]);
181 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel22FieldNumber,
182 max_value.odpm_value_[21]);
183 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel23FieldNumber,
184 max_value.odpm_value_[22]);
185 setAtomFieldValue(&values, BrownoutDetected::kOdpmChannel24FieldNumber,
186 max_value.odpm_value_[23]);
187
188 setAtomFieldValue(&values, BrownoutDetected::kDvfsChannel1FieldNumber,
189 max_value.dvfs_value_[0]);
190 setAtomFieldValue(&values, BrownoutDetected::kDvfsChannel2FieldNumber,
191 max_value.dvfs_value_[1]);
192 setAtomFieldValue(&values, BrownoutDetected::kDvfsChannel3FieldNumber,
193 max_value.dvfs_value_[2]);
194 setAtomFieldValue(&values, BrownoutDetected::kDvfsChannel4FieldNumber,
195 max_value.dvfs_value_[3]);
196 setAtomFieldValue(&values, BrownoutDetected::kDvfsChannel5FieldNumber,
197 max_value.dvfs_value_[4]);
198 setAtomFieldValue(&values, BrownoutDetected::kDvfsChannel6FieldNumber,
199 max_value.dvfs_value_[5]);
200 setAtomFieldValue(&values, BrownoutDetected::kBrownoutReasonFieldNumber,
201 max_value.brownout_reason_);
202
203 setAtomFieldValue(&values, BrownoutDetected::kMaxCurrentFieldNumber, max_value.max_curr_);
204 setAtomFieldValue(&values, BrownoutDetected::kEvtCntUvlo1FieldNumber, max_value.evt_cnt_uvlo1_);
205 setAtomFieldValue(&values, BrownoutDetected::kEvtCntUvlo2FieldNumber, max_value.evt_cnt_uvlo2_);
206 setAtomFieldValue(&values, BrownoutDetected::kEvtCntOilo1FieldNumber, max_value.evt_cnt_oilo1_);
207 setAtomFieldValue(&values, BrownoutDetected::kEvtCntOilo2FieldNumber, max_value.evt_cnt_oilo2_);
208 setAtomFieldValue(&values, BrownoutDetected::kVimonVbattFieldNumber, max_value.vimon_vbatt_);
209 setAtomFieldValue(&values, BrownoutDetected::kVimonIbattFieldNumber, max_value.vimon_ibatt_);
210
211 setAtomFieldValue(&values, BrownoutDetected::kMitigationMethod0FieldNumber,
212 max_value.mitigation_method_0_);
213 setAtomFieldValue(&values, BrownoutDetected::kMitigationMethod0CountFieldNumber,
214 max_value.mitigation_method_0_count_);
215 setAtomFieldValue(&values, BrownoutDetected::kMitigationMethod0TimeUsFieldNumber,
216 max_value.mitigation_method_0_time_us_);
217
218 // Send vendor atom to IStats HAL
219 VendorAtom event = {.reverseDomainName = "",
220 .atomId = PixelAtoms::Atom::kBrownoutDetected,
221 .values = std::move(values)};
222 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
223 if (!ret.isOk())
224 ALOGE("Unable to report ChargeStats to Stats service");
225 }
226
parseTimestamp(std::string timestamp)227 long BrownoutDetectedReporter::parseTimestamp(std::string timestamp) {
228 struct tm triggeredTimestamp = {};
229 std::string timestampFormat = "%Y-%m-%d %H:%M:%S";
230 if (strptime(timestamp.substr(0, 19).c_str(), timestampFormat.c_str(), &triggeredTimestamp)) {
231 auto logFileTime = std::chrono::system_clock::from_time_t(mktime(&triggeredTimestamp));
232 return logFileTime.time_since_epoch().count() / ONE_SECOND_IN_US;
233 }
234 return 0;
235 }
236
brownoutReasonCheck(const std::string & brownoutReasonProp)237 int BrownoutDetectedReporter::brownoutReasonCheck(const std::string &brownoutReasonProp) {
238 std::string reason = android::base::GetProperty(brownoutReasonProp.c_str(), "");
239 if (reason.empty()) {
240 // Brownout not found
241 return -1;
242 }
243 auto key = kBrownoutReason.find(reason);
244 if (key == kBrownoutReason.end()) {
245 return -1;
246 }
247 return key->second;
248 }
249
parseIRQ(const std::string & element)250 int parseIRQ(const std::string &element) {
251 int idx = atoi(element.c_str());
252 if (idx == SMPL_WARN) {
253 return BrownoutDetected::SMPL_WARN;
254 } else if (idx == UVLO1) {
255 return BrownoutDetected::UVLO1;
256 } else if (idx == UVLO2) {
257 return BrownoutDetected::UVLO2;
258 } else if (idx == BATOILO) {
259 return BrownoutDetected::BATOILO;
260 } else if (idx == BATOILO2) {
261 return BrownoutDetected::BATOILO2;
262 }
263 return -1;
264 }
265
logBrownoutCsv(const std::shared_ptr<IStats> & stats_client,const std::string & CsvFilePath,const std::string & brownoutReasonProp)266 void BrownoutDetectedReporter::logBrownoutCsv(const std::shared_ptr<IStats> &stats_client,
267 const std::string &CsvFilePath,
268 const std::string &brownoutReasonProp) {
269 std::string csvFile;
270 if (!android::base::ReadFileToString(CsvFilePath, &csvFile)) {
271 return;
272 }
273 std::istringstream content(csvFile);
274 std::string line;
275 struct BrownoutDetectedInfo max_value = {};
276 max_value.voltage_now_ = DEFAULT_BATTERY_VOLT;
277 max_value.battery_soc_ = DEFAULT_BATTERY_SOC;
278 max_value.battery_temp_ = DEFAULT_BATTERY_TEMP;
279 std::smatch pattern_match;
280 max_value.brownout_reason_ = brownoutReasonCheck(brownoutReasonProp);
281 if (max_value.brownout_reason_ < 0) {
282 return;
283 }
284 bool isAlreadyUpdated = false;
285 std::vector<std::vector<std::string>> rows;
286 int row_num = 0;
287 while (std::getline(content, line)) {
288 if (std::regex_match(line, pattern_match, kAlreadyUpdatedPattern)) {
289 isAlreadyUpdated = true;
290 break;
291 }
292 row_num++;
293 if (row_num == 1) {
294 continue;
295 }
296 std::vector<std::string> row;
297 std::stringstream ss(line);
298 std::string field;
299 while (getline(ss, field, ',')) {
300 row.push_back(field);
301 }
302
303 max_value.triggered_timestamp_ = parseTimestamp(row[TIMESTAMP_IDX].c_str());
304 max_value.triggered_irq_ = parseIRQ(row[IRQ_IDX]);
305 max_value.battery_soc_ = atoi(row[SOC_IDX].c_str());
306 max_value.battery_temp_ = atoi(row[TEMP_IDX].c_str());
307 max_value.battery_cycle_ = atoi(row[CYCLE_IDX].c_str());
308 max_value.voltage_now_ = atoi(row[VOLTAGE_IDX].c_str());
309 for (int i = 0; i < DVFS_MAX_IDX; i++) {
310 max_value.dvfs_value_[i] = atoi(row[i + DVFS_CHANNEL_0].c_str());
311 }
312 for (int i = 0; i < ODPM_MAX_IDX; i++) {
313 max_value.odpm_value_[i] = atoi(row[i + ODPM_CHANNEL_0].c_str());
314 }
315 if (row.size() > MAX_CURR) {
316 max_value.evt_cnt_oilo1_ = atoi(row[EVT_CNT_IDX_OILO1].c_str());
317 max_value.evt_cnt_oilo2_ = atoi(row[EVT_CNT_IDX_OILO2].c_str());
318 max_value.evt_cnt_uvlo1_ = atoi(row[EVT_CNT_IDX_UVLO1].c_str());
319 max_value.evt_cnt_uvlo2_ = atoi(row[EVT_CNT_IDX_UVLO2].c_str());
320 max_value.max_curr_ = atoi(row[MAX_CURR].c_str());
321 }
322 if (row.size() > IDX_VIMON_I) {
323 max_value.vimon_vbatt_ = atoi(row[IDX_VIMON_V].c_str());
324 max_value.vimon_ibatt_ = atoi(row[IDX_VIMON_I].c_str());
325 }
326 }
327 if (!isAlreadyUpdated && max_value.battery_temp_ != DEFAULT_BATTERY_TEMP) {
328 std::string file_content = "LASTMEAL_UPDATED\n" + csvFile;
329 android::base::WriteStringToFile(file_content, CsvFilePath);
330 uploadData(stats_client, max_value);
331 }
332 }
333
logBrownout(const std::shared_ptr<IStats> & stats_client,const std::string & logFilePath,const std::string & brownoutReasonProp)334 void BrownoutDetectedReporter::logBrownout(const std::shared_ptr<IStats> &stats_client,
335 const std::string &logFilePath,
336 const std::string &brownoutReasonProp) {
337 std::string logFile;
338 if (!android::base::ReadFileToString(logFilePath, &logFile)) {
339 return;
340 }
341 std::istringstream content(logFile);
342 std::string line;
343 struct BrownoutDetectedInfo max_value = {};
344 max_value.voltage_now_ = DEFAULT_BATTERY_VOLT;
345 max_value.battery_soc_ = DEFAULT_BATTERY_SOC;
346 max_value.battery_temp_ = DEFAULT_BATTERY_TEMP;
347 std::smatch pattern_match;
348 int odpm_index = 0, dvfs_index = 0;
349 max_value.brownout_reason_ = brownoutReasonCheck(brownoutReasonProp);
350 if (max_value.brownout_reason_ < 0) {
351 return;
352 }
353 bool isAlreadyUpdated = false;
354 while (std::getline(content, line)) {
355 if (std::regex_match(line, pattern_match, kAlreadyUpdatedPattern)) {
356 isAlreadyUpdated = true;
357 break;
358 }
359 if (std::regex_match(line, pattern_match, kIrqPattern)) {
360 if (pattern_match.size() < (KEY_IDX + 1)) {
361 return;
362 }
363 std::ssub_match irq = pattern_match[KEY_IDX];
364 if (irq.str().find("batoilo") != std::string::npos) {
365 max_value.triggered_irq_ = BrownoutDetected::BATOILO;
366 continue;
367 }
368 if (irq.str().find("vdroop1") != std::string::npos) {
369 max_value.triggered_irq_ = BrownoutDetected::UVLO1;
370 continue;
371 }
372 if (irq.str().find("vdroop2") != std::string::npos) {
373 max_value.triggered_irq_ = BrownoutDetected::UVLO2;
374 continue;
375 }
376 if (irq.str().find("smpl_gm") != std::string::npos) {
377 max_value.triggered_irq_ = BrownoutDetected::SMPL_WARN;
378 continue;
379 }
380 continue;
381 }
382 if (std::regex_match(line, pattern_match, kTimestampPattern)) {
383 max_value.triggered_timestamp_ = parseTimestamp(line.c_str());
384 continue;
385 }
386 if (updateIfFound(line, kBatterySocPattern, &max_value.battery_soc_, kUpdateMin)) {
387 continue;
388 }
389 if (updateIfFound(line, kBatteryTempPattern, &max_value.battery_temp_, kUpdateMin)) {
390 continue;
391 }
392 if (updateIfFound(line, kBatteryCyclePattern, &max_value.battery_cycle_, kUpdateMax)) {
393 continue;
394 }
395 if (updateIfFound(line, kFgPattern, &max_value.voltage_now_, kUpdateMin)) {
396 continue;
397 }
398 if (updateIfFound(line, kDvfsPattern, &max_value.dvfs_value_[dvfs_index], kUpdateMax)) {
399 dvfs_index++;
400 // Discarding previous value and update with new DVFS value
401 if (dvfs_index == DVFS_MAX_IDX) {
402 dvfs_index = 0;
403 }
404 continue;
405 }
406 if (updateIfFound(line, kOdpmPattern, &max_value.odpm_value_[odpm_index], kUpdateMax)) {
407 odpm_index++;
408 // Discarding previous value and update with new ODPM value
409 if (odpm_index == ODPM_MAX_IDX) {
410 odpm_index = 0;
411 }
412 continue;
413 }
414 }
415 if (!isAlreadyUpdated && max_value.battery_temp_ != DEFAULT_BATTERY_TEMP) {
416 std::string file_content = "LASTMEAL_UPDATED\n" + logFile;
417 android::base::WriteStringToFile(file_content, logFilePath);
418 uploadData(stats_client, max_value);
419 }
420 }
421
422 } // namespace pixel
423 } // namespace google
424 } // namespace hardware
425 } // namespace android
426