blob: 8a8939350b025fff9d64de90927db0662da12791 [file] [log] [blame]
James Hawkinsabd73e62016-01-19 15:10:38 -08001/*
2 * Copyright (C) 2016 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// The bootstat command provides options to persist boot events with the current
18// timestamp, dump the persisted events, and log all events to EventLog to be
19// uploaded to Android log storage via Tron.
20
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080021#include <getopt.h>
Mark Salyzynb304f6d2017-08-04 13:35:51 -070022#include <sys/klog.h>
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -070023#include <unistd.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070024
James Hawkinse78ea772017-03-24 11:43:02 -070025#include <chrono>
James Hawkins0660b302016-03-08 16:18:15 -080026#include <cmath>
James Hawkinsabd73e62016-01-19 15:10:38 -080027#include <cstddef>
28#include <cstdio>
James Hawkins500d7152016-02-16 15:05:54 -080029#include <ctime>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080030#include <map>
James Hawkinsabd73e62016-01-19 15:10:38 -080031#include <memory>
Mark Salyzyn25900dd2018-03-16 09:05:59 -070032#include <regex>
James Hawkinsabd73e62016-01-19 15:10:38 -080033#include <string>
Mark Salyzyn853bb802018-03-16 08:44:56 -070034#include <utility>
James Hawkinsbe46fd12017-02-02 16:21:25 -080035#include <vector>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070036
James Hawkinse78ea772017-03-24 11:43:02 -070037#include <android-base/chrono_utils.h>
Mark Salyzynb304f6d2017-08-04 13:35:51 -070038#include <android-base/file.h>
James Hawkinseabe08b2016-01-19 16:54:35 -080039#include <android-base/logging.h>
James Hawkins4dded612016-07-28 11:50:23 -070040#include <android-base/parseint.h>
James Hawkinsbe46fd12017-02-02 16:21:25 -080041#include <android-base/strings.h>
James Hawkinse78ea772017-03-24 11:43:02 -070042#include <android/log.h>
Mark Salyzynb304f6d2017-08-04 13:35:51 -070043#include <cutils/android_reboot.h>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080044#include <cutils/properties.h>
Mark Salyzynb304f6d2017-08-04 13:35:51 -070045#include <log/logcat.h>
James Hawkins9aec9262017-01-31 11:42:24 -080046#include <metricslogger/metrics_logger.h>
Tej Singh4eacd382018-01-25 17:59:57 -080047#include <statslog.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070048
James Hawkinsabd73e62016-01-19 15:10:38 -080049#include "boot_event_record_store.h"
James Hawkinsabd73e62016-01-19 15:10:38 -080050
51namespace {
52
James Hawkinsabd73e62016-01-19 15:10:38 -080053// Scans the boot event record store for record files and logs each boot event
54// via EventLog.
55void LogBootEvents() {
56 BootEventRecordStore boot_event_store;
57
58 auto events = boot_event_store.GetAllBootEvents();
59 for (auto i = events.cbegin(); i != events.cend(); ++i) {
James Hawkins9aec9262017-01-31 11:42:24 -080060 android::metricslogger::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080061 }
62}
63
James Hawkinsc6275582016-03-22 10:47:44 -070064// Records the named boot |event| to the record store. If |value| is non-empty
65// and is a proper string representation of an integer value, the converted
66// integer value is associated with the boot event.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -070067void RecordBootEventFromCommandLine(const std::string& event, const std::string& value_str) {
James Hawkinsc6275582016-03-22 10:47:44 -070068 BootEventRecordStore boot_event_store;
69 if (!value_str.empty()) {
70 int32_t value = 0;
Elliott Hughesda46b392016-10-11 17:09:00 -070071 if (android::base::ParseInt(value_str, &value)) {
James Hawkins4dded612016-07-28 11:50:23 -070072 boot_event_store.AddBootEventWithValue(event, value);
73 }
James Hawkinsc6275582016-03-22 10:47:44 -070074 } else {
75 boot_event_store.AddBootEvent(event);
76 }
77}
78
James Hawkinsabd73e62016-01-19 15:10:38 -080079void PrintBootEvents() {
80 printf("Boot events:\n");
81 printf("------------\n");
82
83 BootEventRecordStore boot_event_store;
84 auto events = boot_event_store.GetAllBootEvents();
85 for (auto i = events.cbegin(); i != events.cend(); ++i) {
86 printf("%s\t%d\n", i->first.c_str(), i->second);
87 }
88}
89
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -070090void ShowHelp(const char* cmd) {
James Hawkinsabd73e62016-01-19 15:10:38 -080091 fprintf(stderr, "Usage: %s [options]\n", cmd);
92 fprintf(stderr,
93 "options include:\n"
Yongqin Liu78b2b942017-07-07 13:26:49 +080094 " -h, --help Show this help\n"
95 " -l, --log Log all metrics to logstorage\n"
96 " -p, --print Dump the boot event records to the console\n"
97 " -r, --record Record the timestamp of a named boot event\n"
98 " --value Optional value to associate with the boot event\n"
99 " --record_boot_complete Record metrics related to the time for the device boot\n"
100 " --record_boot_reason Record the reason why the device booted\n"
James Hawkins53684ea2016-02-23 16:18:19 -0800101 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -0800102}
103
104// Constructs a readable, printable string from the givencommand line
105// arguments.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700106std::string GetCommandLine(int argc, char** argv) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800107 std::string cmd;
108 for (int i = 0; i < argc; ++i) {
109 cmd += argv[i];
110 cmd += " ";
111 }
112
113 return cmd;
114}
115
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800116// Convenience wrapper over the property API that returns an
117// std::string.
118std::string GetProperty(const char* key) {
119 std::vector<char> temp(PROPERTY_VALUE_MAX);
120 const int len = property_get(key, &temp[0], nullptr);
121 if (len < 0) {
122 return "";
123 }
124 return std::string(&temp[0], len);
125}
126
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700127void SetProperty(const char* key, const std::string& val) {
128 property_set(key, val.c_str());
129}
130
131void SetProperty(const char* key, const char* val) {
132 property_set(key, val);
133}
134
James Hawkins25f71222017-10-10 16:37:05 -0700135constexpr int32_t kEmptyBootReason = 0;
James Hawkins6f74c0b2016-02-12 15:49:16 -0800136constexpr int32_t kUnknownBootReason = 1;
137
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800138// A mapping from boot reason string, as read from the ro.boot.bootreason
139// system property, to a unique integer ID. Viewers of log data dashboards for
140// the boot_reason metric may refer to this mapping to discern the histogram
141// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800142const std::map<std::string, int32_t> kBootReasonMap = {
James Hawkins25f71222017-10-10 16:37:05 -0700143 {"empty", kEmptyBootReason},
Mark Salyzyn2b820532018-03-16 08:53:34 -0700144 {"__BOOTSTAT_UNKNOWN__", kUnknownBootReason},
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700145 {"normal", 2},
146 {"recovery", 3},
147 {"reboot", 4},
148 {"PowerKey", 5},
149 {"hard_reset", 6},
150 {"kernel_panic", 7},
151 {"rpm_err", 8},
152 {"hw_reset", 9},
153 {"tz_err", 10},
154 {"adsp_err", 11},
155 {"modem_err", 12},
156 {"mba_err", 13},
157 {"Watchdog", 14},
158 {"Panic", 15},
159 {"power_key", 16},
160 {"power_on", 17},
161 {"Reboot", 18},
162 {"rtc", 19},
163 {"edl", 20},
164 {"oem_pon1", 21},
165 {"oem_powerkey", 22},
166 {"oem_unknown_reset", 23},
167 {"srto: HWWDT reset SC", 24},
168 {"srto: HWWDT reset platform", 25},
169 {"srto: bootloader", 26},
170 {"srto: kernel panic", 27},
171 {"srto: kernel watchdog reset", 28},
172 {"srto: normal", 29},
173 {"srto: reboot", 30},
174 {"srto: reboot-bootloader", 31},
175 {"srto: security watchdog reset", 32},
176 {"srto: wakesrc", 33},
177 {"srto: watchdog", 34},
178 {"srto:1-1", 35},
179 {"srto:omap_hsmm", 36},
180 {"srto:phy0", 37},
181 {"srto:rtc0", 38},
182 {"srto:touchpad", 39},
183 {"watchdog", 40},
184 {"watchdogr", 41},
185 {"wdog_bark", 42},
186 {"wdog_bite", 43},
187 {"wdog_reset", 44},
188 {"shutdown,", 45}, // Trailing comma is intentional.
189 {"shutdown,userrequested", 46},
190 {"reboot,bootloader", 47},
191 {"reboot,cold", 48},
192 {"reboot,recovery", 49},
193 {"thermal_shutdown", 50},
194 {"s3_wakeup", 51},
195 {"kernel_panic,sysrq", 52},
196 {"kernel_panic,NULL", 53},
Mark Salyzyn853bb802018-03-16 08:44:56 -0700197 {"kernel_panic,null", 53},
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700198 {"kernel_panic,BUG", 54},
Mark Salyzyn853bb802018-03-16 08:44:56 -0700199 {"kernel_panic,bug", 54},
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700200 {"bootloader", 55},
201 {"cold", 56},
202 {"hard", 57},
203 {"warm", 58},
Mark Salyzyn2b820532018-03-16 08:53:34 -0700204 // {"recovery", 59}, // Duplicate of enum 3 above. Immediate reuse possible.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700205 {"thermal-shutdown", 60},
206 {"shutdown,thermal", 61},
207 {"shutdown,battery", 62},
208 {"reboot,ota", 63},
209 {"reboot,factory_reset", 64},
210 {"reboot,", 65},
211 {"reboot,shell", 66},
212 {"reboot,adb", 67},
Mark Salyzyn9033bf52017-09-21 11:30:29 -0700213 {"reboot,userrequested", 68},
Mark Salyzyn161b8622017-09-26 08:26:12 -0700214 {"shutdown,container", 69}, // Host OS asking Android Container to shutdown
Mark Salyzyn243fa292017-10-11 09:02:04 -0700215 {"cold,powerkey", 70},
216 {"warm,s3_wakeup", 71},
217 {"hard,hw_reset", 72},
218 {"shutdown,suspend", 73}, // Suspend to RAM
219 {"shutdown,hibernate", 74}, // Suspend to DISK
James Hawkins34073b52017-10-17 15:53:27 -0700220 {"power_on_key", 75},
221 {"reboot_by_key", 76},
222 {"wdt_by_pass_pwk", 77},
223 {"reboot_longkey", 78},
224 {"powerkey", 79},
225 {"usb", 80},
226 {"wdt", 81},
227 {"tool_by_pass_pwk", 82},
228 {"2sec_reboot", 83},
229 {"reboot,by_key", 84},
230 {"reboot,longkey", 85},
Mark Salyzyncabbe4f2017-10-23 13:52:39 -0700231 {"reboot,2sec", 86},
Mark Salyzync89f9da2017-10-24 15:35:34 -0700232 {"shutdown,thermal,battery", 87},
Mark Salyzyn72a8ea32017-10-25 09:23:19 -0700233 {"reboot,its_just_so_hard", 88}, // produced by boot_reason_test
234 {"reboot,Its Just So Hard", 89}, // produced by boot_reason_test
Mark Salyzyn2b820532018-03-16 08:53:34 -0700235 // {"usb", 90}, // Duplicate of enum 80 above. Immediate reuse possible.
James Hawkins74b17582017-11-20 14:13:41 -0800236 {"charge", 91},
237 {"oem_tz_crash", 92},
238 {"uvlo", 93},
239 {"oem_ps_hold", 94},
240 {"abnormal_reset", 95},
241 {"oemerr_unknown", 96},
242 {"reboot_fastboot_mode", 97},
James Hawkins5f85f832017-11-29 14:30:06 -0800243 {"watchdog_apps_bite", 98},
244 {"xpu_err", 99},
245 {"power_on_usb", 100},
James Hawkinsf4444f02017-11-30 15:01:40 -0800246 {"watchdog_rpm", 101},
247 {"watchdog_nonsec", 102},
248 {"watchdog_apps_bark", 103},
249 {"reboot_dmverity_corrupted", 104},
James Hawkins00433a22017-12-04 14:20:21 -0800250 {"reboot_smpl", 105},
251 {"watchdog_sdi_apps_reset", 106},
252 {"smpl", 107},
253 {"oem_modem_failed_to_powerup", 108},
James Hawkinse2c27242017-12-18 13:40:27 -0800254 {"reboot_normal", 109},
255 {"oem_lpass_cfg", 110},
256 {"oem_xpu_ns_error", 111},
257 {"power_key_press", 112},
258 {"hardware_reset", 113},
259 {"reboot_by_powerkey", 114},
260 {"reboot_verity", 115},
261 {"oem_rpm_undef_error", 116},
262 {"oem_crash_on_the_lk", 117},
263 {"oem_rpm_reset", 118},
264 {"oem_lpass_cfg", 119},
265 {"oem_xpu_ns_error", 120},
266 {"factory_cable", 121},
267 {"oem_ar6320_failed_to_powerup", 122},
268 {"watchdog_rpm_bite", 123},
269 {"power_on_cable", 124},
270 {"reboot_unknown", 125},
271 {"wireless_charger", 126},
272 {"0x776655ff", 127},
273 {"oem_thermal_bite_reset", 128},
274 {"charger", 129},
275 {"pon1", 130},
276 {"unknown", 131},
277 {"reboot_rtc", 132},
278 {"cold_boot", 133},
279 {"hard_rst", 134},
James Hawkinsb607dae2018-01-05 14:42:55 -0800280 {"power-on", 135},
281 {"oem_adsp_resetting_the_soc", 136},
282 {"kpdpwr", 137},
283 {"oem_modem_timeout_waiting", 138},
284 {"usb_chg", 139},
285 {"warm_reset_0x02", 140},
286 {"warm_reset_0x80", 141},
287 {"pon_reason_0xb0", 142},
288 {"reboot_download", 143},
James Hawkins79a4ee22018-01-26 14:31:04 -0800289 {"reboot_recovery_mode", 144},
290 {"oem_sdi_err_fatal", 145},
291 {"pmic_watchdog", 146},
292 {"software_master", 147},
Mark Salyzyn8aa36c62018-03-16 11:00:14 -0700293 {"cold,charger", 148},
294 {"cold,rtc", 149},
Mark Salyzyn39cc3e72018-03-19 15:16:29 -0700295 {"cold,rtc,2sec", 150},
296 {"reboot,tool", 151},
297 {"reboot,wdt", 152},
298 {"reboot,unknown", 153},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800299};
300
301// Converts a string value representing the reason the system booted to an
302// integer representation. This is necessary for logging the boot_reason metric
303// via Tron, which does not accept non-integer buckets in histograms.
304int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800305 auto mapping = kBootReasonMap.find(boot_reason);
306 if (mapping != kBootReasonMap.end()) {
307 return mapping->second;
308 }
309
James Hawkins25f71222017-10-10 16:37:05 -0700310 if (boot_reason.empty()) {
311 return kEmptyBootReason;
312 }
313
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800314 LOG(INFO) << "Unknown boot reason: " << boot_reason;
315 return kUnknownBootReason;
316}
317
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700318// Canonical list of supported primary reboot reasons.
319const std::vector<const std::string> knownReasons = {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700320 // clang-format off
321 // kernel
322 "watchdog",
323 "kernel_panic",
324 // strong
325 "recovery", // Should not happen from ro.boot.bootreason
326 "bootloader", // Should not happen from ro.boot.bootreason
327 // blunt
328 "cold",
329 "hard",
330 "warm",
Mark Salyzyn62909822017-10-09 09:27:16 -0700331 // super blunt
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700332 "shutdown", // Can not happen from ro.boot.bootreason
333 "reboot", // Default catch-all for anything unknown
334 // clang-format on
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700335};
336
337// Returns true if the supplied reason prefix is considered detailed enough.
338bool isStrongRebootReason(const std::string& r) {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700339 for (auto& s : knownReasons) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700340 if (s == "cold") break;
341 // Prefix defined as terminated by a nul or comma (,).
Elliott Hughes579e6822017-12-20 09:41:00 -0800342 if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700343 return true;
344 }
345 }
346 return false;
347}
348
349// Returns true if the supplied reason prefix is associated with the kernel.
350bool isKernelRebootReason(const std::string& r) {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700351 for (auto& s : knownReasons) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700352 if (s == "recovery") break;
353 // Prefix defined as terminated by a nul or comma (,).
Elliott Hughes579e6822017-12-20 09:41:00 -0800354 if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700355 return true;
356 }
357 }
358 return false;
359}
360
361// Returns true if the supplied reason prefix is considered known.
362bool isKnownRebootReason(const std::string& r) {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700363 for (auto& s : knownReasons) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700364 // Prefix defined as terminated by a nul or comma (,).
Elliott Hughes579e6822017-12-20 09:41:00 -0800365 if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700366 return true;
367 }
368 }
369 return false;
370}
371
372// If the reboot reason should be improved, report true if is too blunt.
373bool isBluntRebootReason(const std::string& r) {
374 if (isStrongRebootReason(r)) return false;
375
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700376 if (!isKnownRebootReason(r)) return true; // Can not support unknown as detail
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700377
378 size_t pos = 0;
379 while ((pos = r.find(',', pos)) != std::string::npos) {
380 ++pos;
381 std::string next(r.substr(pos));
382 if (next.length() == 0) break;
383 if (next[0] == ',') continue;
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700384 if (!isKnownRebootReason(next)) return false; // Unknown subreason is good.
385 if (isStrongRebootReason(next)) return false; // eg: reboot,reboot
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700386 }
387 return true;
388}
389
Mark Salyzyn64610892017-09-18 10:41:14 -0700390bool readPstoreConsole(std::string& console) {
391 if (android::base::ReadFileToString("/sys/fs/pstore/console-ramoops-0", &console)) {
392 return true;
393 }
394 return android::base::ReadFileToString("/sys/fs/pstore/console-ramoops", &console);
395}
396
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700397// Implement a variant of std::string::rfind that is resilient to errors in
398// the data stream being inspected.
399class pstoreConsole {
400 private:
401 const size_t kBitErrorRate = 8; // number of bits per error
402 const std::string& console;
403
404 // Number of bits that differ between the two arguments l and r.
405 // Returns zero if the values for l and r are identical.
406 size_t numError(uint8_t l, uint8_t r) const { return std::bitset<8>(l ^ r).count(); }
407
408 // A string comparison function, reports the number of errors discovered
409 // in the match to a maximum of the bitLength / kBitErrorRate, at that
410 // point returning npos to indicate match is too poor.
411 //
412 // Since called in rfind which works backwards, expect cache locality will
413 // help if we check in reverse here as well for performance.
414 //
415 // Assumption: l (from console.c_str() + pos) is long enough to house
416 // _r.length(), checked in rfind caller below.
417 //
418 size_t numError(size_t pos, const std::string& _r) const {
419 const char* l = console.c_str() + pos;
420 const char* r = _r.c_str();
421 size_t n = _r.length();
422 const uint8_t* le = reinterpret_cast<const uint8_t*>(l) + n;
423 const uint8_t* re = reinterpret_cast<const uint8_t*>(r) + n;
424 size_t count = 0;
425 n = 0;
426 do {
427 // individual character bit error rate > threshold + slop
428 size_t num = numError(*--le, *--re);
429 if (num > ((8 + kBitErrorRate) / kBitErrorRate)) return std::string::npos;
430 // total bit error rate > threshold + slop
431 count += num;
432 ++n;
433 if (count > ((n * 8 + kBitErrorRate - (n > 2)) / kBitErrorRate)) {
434 return std::string::npos;
435 }
436 } while (le != reinterpret_cast<const uint8_t*>(l));
437 return count;
438 }
439
440 public:
441 explicit pstoreConsole(const std::string& console) : console(console) {}
442 // scope of argument must be equal to or greater than scope of pstoreConsole
443 explicit pstoreConsole(const std::string&& console) = delete;
444 explicit pstoreConsole(std::string&& console) = delete;
445
446 // Our implementation of rfind, use exact match first, then resort to fuzzy.
447 size_t rfind(const std::string& needle) const {
448 size_t pos = console.rfind(needle); // exact match?
449 if (pos != std::string::npos) return pos;
450
451 // Check to make sure needle fits in console string.
452 pos = console.length();
453 if (needle.length() > pos) return std::string::npos;
454 pos -= needle.length();
455 // fuzzy match to maximum kBitErrorRate
Ivan Lozano44d3cac2017-11-07 13:13:55 -0800456 for (;;) {
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700457 if (numError(pos, needle) != std::string::npos) return pos;
Ivan Lozano44d3cac2017-11-07 13:13:55 -0800458 if (pos == 0) break;
459 --pos;
460 }
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700461 return std::string::npos;
462 }
463
464 // Our implementation of find, use only fuzzy match.
465 size_t find(const std::string& needle, size_t start = 0) const {
466 // Check to make sure needle fits in console string.
467 if (needle.length() > console.length()) return std::string::npos;
468 const size_t last_pos = console.length() - needle.length();
469 // fuzzy match to maximum kBitErrorRate
470 for (size_t pos = start; pos <= last_pos; ++pos) {
471 if (numError(pos, needle) != std::string::npos) return pos;
472 }
473 return std::string::npos;
474 }
Mark Salyzyn39cc3e72018-03-19 15:16:29 -0700475
476 operator const std::string&() const { return console; }
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700477};
478
479// If bit error match to needle, correct it.
480// Return true if any corrections were discovered and applied.
Mark Salyzyn1e7d1c72018-03-16 08:57:20 -0700481bool correctForBitError(std::string& reason, const std::string& needle) {
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700482 bool corrected = false;
483 if (reason.length() < needle.length()) return corrected;
484 const pstoreConsole console(reason);
485 const size_t last_pos = reason.length() - needle.length();
486 for (size_t pos = 0; pos <= last_pos; pos += needle.length()) {
487 pos = console.find(needle, pos);
488 if (pos == std::string::npos) break;
489
490 // exact match has no malice
491 if (needle == reason.substr(pos, needle.length())) continue;
492
493 corrected = true;
494 reason = reason.substr(0, pos) + needle + reason.substr(pos + needle.length());
495 }
496 return corrected;
497}
498
Mark Salyzyn1e7d1c72018-03-16 08:57:20 -0700499// If bit error match to needle, correct it.
500// Return true if any corrections were discovered and applied.
501// Try again if we can replace underline with spaces.
502bool correctForBitErrorOrUnderline(std::string& reason, const std::string& needle) {
503 bool corrected = correctForBitError(reason, needle);
504 std::string _needle(needle);
505 std::transform(_needle.begin(), _needle.end(), _needle.begin(),
506 [](char c) { return (c == '_') ? ' ' : c; });
507 if (needle != _needle) {
508 corrected |= correctForBitError(reason, _needle);
509 }
510 return corrected;
511}
512
Mark Salyzyn39cc3e72018-03-19 15:16:29 -0700513// Converts a string value representing the reason the system booted to a
514// string complying with Android system standard reason.
515void transformReason(std::string& reason) {
516 std::transform(reason.begin(), reason.end(), reason.begin(), ::tolower);
517 std::transform(reason.begin(), reason.end(), reason.begin(),
518 [](char c) { return ::isblank(c) ? '_' : c; });
519 std::transform(reason.begin(), reason.end(), reason.begin(),
520 [](char c) { return ::isprint(c) ? c : '?'; });
521}
522
523// Pull out and correct quoted (') subreason, pos just beyond first quote.
524// Check subreasons for reboot,<subreason> and kernel_panic,sysrq,<subreason>
525std::string getSubreason(const std::string& content, size_t pos) {
526 static constexpr size_t max_reason_length = 256;
527
528 std::string subReason(content.substr(pos, max_reason_length));
529 // Correct against any known strings that Bit Error Match
530 for (const auto& s : knownReasons) {
531 correctForBitErrorOrUnderline(subReason, s);
532 }
533 for (const auto& m : kBootReasonMap) {
534 if (m.first.length() <= strlen("cold")) continue; // too short?
535 if (correctForBitErrorOrUnderline(subReason, m.first + "'")) continue;
536 if (m.first.length() <= strlen("reboot,cold")) continue; // short?
537 if (android::base::StartsWith(m.first, "reboot,")) {
538 correctForBitErrorOrUnderline(subReason, m.first.substr(strlen("reboot,")) + "'");
539 } else if (android::base::StartsWith(m.first, "kernel_panic,sysrq,")) {
540 correctForBitErrorOrUnderline(subReason, m.first.substr(strlen("kernel_panic,sysrq,")) + "'");
541 }
542 }
543 for (pos = 0; pos < subReason.length(); ++pos) {
544 char c = subReason[pos];
545 // #, &, %, / are common single bit error for ' that we can block
546 if (!::isprint(c) || (c == '\'') || (c == '#') || (c == '&') || (c == '%') || (c == '/')) {
547 subReason.erase(pos);
548 break;
549 }
550 }
551 transformReason(subReason);
552 return subReason;
553}
554
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700555bool addKernelPanicSubReason(const pstoreConsole& console, std::string& ret) {
Mark Salyzyn64610892017-09-18 10:41:14 -0700556 // Check for kernel panic types to refine information
Mark Salyzyn853bb802018-03-16 08:44:56 -0700557 if ((console.rfind("SysRq : Trigger a crash") != std::string::npos) ||
558 (console.rfind("PC is at sysrq_handle_crash+") != std::string::npos)) {
Mark Salyzyn64610892017-09-18 10:41:14 -0700559 ret = "kernel_panic,sysrq";
Mark Salyzyn39cc3e72018-03-19 15:16:29 -0700560 // Invented for Android to allow daemons that specifically trigger sysrq
561 // to communicate more accurate boot subreasons via last console messages.
562 static constexpr char sysrqSubreason[] = "SysRq : Trigger a crash : '";
563 auto pos = console.rfind(sysrqSubreason);
564 if (pos != std::string::npos) {
565 ret += "," + getSubreason(console, pos + strlen(sysrqSubreason));
566 }
Mark Salyzyn64610892017-09-18 10:41:14 -0700567 return true;
568 }
569 if (console.rfind("Unable to handle kernel NULL pointer dereference at virtual address") !=
570 std::string::npos) {
Mark Salyzyn853bb802018-03-16 08:44:56 -0700571 ret = "kernel_panic,null";
Mark Salyzyn64610892017-09-18 10:41:14 -0700572 return true;
573 }
574 if (console.rfind("Kernel BUG at ") != std::string::npos) {
Mark Salyzyn853bb802018-03-16 08:44:56 -0700575 ret = "kernel_panic,bug";
Mark Salyzyn64610892017-09-18 10:41:14 -0700576 return true;
577 }
578 return false;
579}
580
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700581bool addKernelPanicSubReason(const std::string& content, std::string& ret) {
582 return addKernelPanicSubReason(pstoreConsole(content), ret);
583}
584
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700585const char system_reboot_reason_property[] = "sys.boot.reason";
586const char last_reboot_reason_property[] = LAST_REBOOT_REASON_PROPERTY;
587const char bootloader_reboot_reason_property[] = "ro.boot.bootreason";
588
589// Scrub, Sanitize, Standardize and Enhance the boot reason string supplied.
590std::string BootReasonStrToReason(const std::string& boot_reason) {
591 std::string ret(GetProperty(system_reboot_reason_property));
592 std::string reason(boot_reason);
593 // If sys.boot.reason == ro.boot.bootreason, let's re-evaluate
594 if (reason == ret) ret = "";
595
Mark Salyzyn88d692c2017-09-20 08:37:46 -0700596 transformReason(reason);
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700597
598 // Is the current system boot reason sys.boot.reason valid?
599 if (!isKnownRebootReason(ret)) ret = "";
600
601 if (ret == "") {
602 // Is the bootloader boot reason ro.boot.bootreason known?
603 std::vector<std::string> words(android::base::Split(reason, ",_-"));
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700604 for (auto& s : knownReasons) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700605 std::string blunt;
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700606 for (auto& r : words) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700607 if (r == s) {
608 if (isBluntRebootReason(s)) {
609 blunt = s;
610 } else {
611 ret = s;
612 break;
613 }
614 }
615 }
616 if (ret == "") ret = blunt;
617 if (ret != "") break;
618 }
619 }
620
621 if (ret == "") {
622 // A series of checks to take some officially unsupported reasons
623 // reported by the bootloader and find some logical and canonical
624 // sense. In an ideal world, we would require those bootloaders
Mark Salyzyn25900dd2018-03-16 09:05:59 -0700625 // to behave and follow our CTS standards.
626 //
627 // first member is the output
628 // second member is an unanchored regex for an alias
629 //
Mark Salyzyn28193282018-03-16 09:05:59 -0700630 // If output has a prefix of <bang> '!', we do not use it as a
631 // match needle (and drop the <bang> prefix when landing in output),
632 // otherwise look for it as well. This helps keep the scale of the
Mark Salyzyn25900dd2018-03-16 09:05:59 -0700633 // following table smaller.
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700634 static const std::vector<std::pair<const std::string, const std::string>> aliasReasons = {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700635 {"watchdog", "wdog"},
Mark Salyzyn25900dd2018-03-16 09:05:59 -0700636 {"cold,powerkey", "powerkey|power_key|PowerKey"},
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700637 {"kernel_panic", "panic"},
638 {"shutdown,thermal", "thermal"},
639 {"warm,s3_wakeup", "s3_wakeup"},
640 {"hard,hw_reset", "hw_reset"},
Mark Salyzyn8aa36c62018-03-16 11:00:14 -0700641 {"cold,charger", "usb"},
642 {"cold,rtc", "rtc"},
Mark Salyzyncabbe4f2017-10-23 13:52:39 -0700643 {"reboot,2sec", "2sec_reboot"},
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700644 {"bootloader", ""},
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700645 };
646
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700647 for (auto& s : aliasReasons) {
Mark Salyzyn28193282018-03-16 09:05:59 -0700648 size_t firstHasNot = s.first[0] == '!';
649 if (!firstHasNot && (reason.find(s.first) != std::string::npos)) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700650 ret = s.first;
651 break;
652 }
Mark Salyzyn25900dd2018-03-16 09:05:59 -0700653 if (s.second.size() && std::regex_search(reason, std::regex(s.second))) {
Mark Salyzyn28193282018-03-16 09:05:59 -0700654 ret = s.first.substr(firstHasNot);
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700655 break;
656 }
657 }
658 }
659
660 // If watchdog is the reason, see if there is a security angle?
661 if (ret == "watchdog") {
662 if (reason.find("sec") != std::string::npos) {
663 ret += ",security";
664 }
665 }
666
Mark Salyzyn64610892017-09-18 10:41:14 -0700667 if (ret == "kernel_panic") {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700668 // Check to see if last klog has some refinement hints.
669 std::string content;
Mark Salyzyn64610892017-09-18 10:41:14 -0700670 if (readPstoreConsole(content)) {
671 addKernelPanicSubReason(content, ret);
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700672 }
Mark Salyzyn64610892017-09-18 10:41:14 -0700673 } else if (isBluntRebootReason(ret)) {
674 // Check the other available reason resources if the reason is still blunt.
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700675
Mark Salyzyn64610892017-09-18 10:41:14 -0700676 // Check to see if last klog has some refinement hints.
677 std::string content;
678 if (readPstoreConsole(content)) {
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700679 const pstoreConsole console(content);
Mark Salyzyn64610892017-09-18 10:41:14 -0700680 // The toybox reboot command used directly (unlikely)? But also
681 // catches init's response to Android's more controlled reboot command.
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700682 if (console.rfind("reboot: Power down") != std::string::npos) {
Mark Salyzyn64610892017-09-18 10:41:14 -0700683 ret = "shutdown"; // Still too blunt, but more accurate.
684 // ToDo: init should record the shutdown reason to kernel messages ala:
685 // init: shutdown system with command 'last_reboot_reason'
686 // so that if pstore has persistence we can get some details
687 // that could be missing in last_reboot_reason_property.
688 }
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700689
Mark Salyzyn64610892017-09-18 10:41:14 -0700690 static const char cmd[] = "reboot: Restarting system with command '";
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700691 size_t pos = console.rfind(cmd);
Mark Salyzyn64610892017-09-18 10:41:14 -0700692 if (pos != std::string::npos) {
Mark Salyzyn39cc3e72018-03-19 15:16:29 -0700693 std::string subReason(getSubreason(content, pos + strlen(cmd)));
Mark Salyzyn64610892017-09-18 10:41:14 -0700694 if (subReason != "") { // Will not land "reboot" as that is too blunt.
695 if (isKernelRebootReason(subReason)) {
696 ret = "reboot," + subReason; // User space can't talk kernel reasons.
Mark Salyzyndafced92017-09-20 08:37:46 -0700697 } else if (isKnownRebootReason(subReason)) {
Mark Salyzyn64610892017-09-18 10:41:14 -0700698 ret = subReason;
Mark Salyzyndafced92017-09-20 08:37:46 -0700699 } else {
700 ret = "reboot," + subReason; // legitimize unknown reasons
Mark Salyzyn64610892017-09-18 10:41:14 -0700701 }
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700702 }
703 }
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700704
Mark Salyzyn64610892017-09-18 10:41:14 -0700705 // Check for kernel panics, allowed to override reboot command.
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700706 if (!addKernelPanicSubReason(console, ret) &&
Mark Salyzyn64610892017-09-18 10:41:14 -0700707 // check for long-press power down
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700708 ((console.rfind("Power held for ") != std::string::npos) ||
709 (console.rfind("charger: [") != std::string::npos))) {
Mark Salyzyn64610892017-09-18 10:41:14 -0700710 ret = "cold";
711 }
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700712 }
713
714 // The following battery test should migrate to a default system health HAL
715
716 // Let us not worry if the reboot command was issued, for the cases of
717 // reboot -p, reboot <no reason>, reboot cold, reboot warm and reboot hard.
718 // Same for bootloader and ro.boot.bootreasons of this set, but a dead
719 // battery could conceivably lead to these, so worthy of override.
720 if (isBluntRebootReason(ret)) {
721 // Heuristic to determine if shutdown possibly because of a dead battery?
722 // Really a hail-mary pass to find it in last klog content ...
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700723 static const int battery_dead_threshold = 2; // percent
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700724 static const char battery[] = "healthd: battery l=";
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700725 const pstoreConsole console(content);
726 size_t pos = console.rfind(battery); // last one
Mark Salyzyna16e4372017-09-20 08:36:12 -0700727 std::string digits;
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700728 if (pos != std::string::npos) {
Mark Salyzyn747c0e62017-09-20 08:37:46 -0700729 digits = content.substr(pos + strlen(battery), strlen("100 "));
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700730 // correct common errors
Mark Salyzyn1e7d1c72018-03-16 08:57:20 -0700731 correctForBitError(digits, "100 ");
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700732 if (digits[0] == '!') digits[0] = '1';
733 if (digits[1] == '!') digits[1] = '1';
Mark Salyzyna16e4372017-09-20 08:36:12 -0700734 }
Mark Salyzyn747c0e62017-09-20 08:37:46 -0700735 const char* endptr = digits.c_str();
736 unsigned level = 0;
737 while (::isdigit(*endptr)) {
738 level *= 10;
739 level += *endptr++ - '0';
740 // make sure no leading zeros, except zero itself, and range check.
741 if ((level == 0) || (level > 100)) break;
742 }
Mark Salyzyn293cb3b2017-09-20 08:37:46 -0700743 // example bit error rate issues for 10%
744 // 'l=10 ' no bits in error
745 // 'l=00 ' single bit error (fails above)
746 // 'l=1 ' single bit error
747 // 'l=0 ' double bit error
748 // There are others, not typically critical because of 2%
749 // battery_dead_threshold. KISS check, make sure second
750 // character after digit sequence is not a space.
751 if ((level <= 100) && (endptr != digits.c_str()) && (endptr[0] == ' ') && (endptr[1] != ' ')) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700752 LOG(INFO) << "Battery level at shutdown " << level << "%";
753 if (level <= battery_dead_threshold) {
754 ret = "shutdown,battery";
755 }
Mark Salyzyna16e4372017-09-20 08:36:12 -0700756 } else { // Most likely
757 digits = ""; // reset digits
758
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700759 // Content buffer no longer will have console data. Beware if more
760 // checks added below, that depend on parsing console content.
761 content = "";
762
763 LOG(DEBUG) << "Can not find last low battery in last console messages";
764 android_logcat_context ctx = create_android_logcat();
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700765 FILE* fp = android_logcat_popen(&ctx, "logcat -b kernel -v brief -d");
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700766 if (fp != nullptr) {
767 android::base::ReadFdToString(fileno(fp), &content);
768 }
769 android_logcat_pclose(&ctx, fp);
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700770 static const char logcat_battery[] = "W/healthd ( 0): battery l=";
771 const char* match = logcat_battery;
772
773 if (content == "") {
774 // Service logd.klog not running, go to smaller buffer in the kernel.
775 int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
776 if (rc > 0) {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700777 ssize_t len = rc + 1024; // 1K Margin should it grow between calls.
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700778 std::unique_ptr<char[]> buf(new char[len]);
779 rc = klogctl(KLOG_READ_ALL, buf.get(), len);
780 if (rc < len) {
781 len = rc + 1;
782 }
783 buf[--len] = '\0';
784 content = buf.get();
785 }
786 match = battery;
787 }
788
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700789 pos = content.find(match); // The first one it finds.
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700790 if (pos != std::string::npos) {
Mark Salyzyn747c0e62017-09-20 08:37:46 -0700791 digits = content.substr(pos + strlen(match), strlen("100 "));
Mark Salyzyna16e4372017-09-20 08:36:12 -0700792 }
Mark Salyzyn747c0e62017-09-20 08:37:46 -0700793 endptr = digits.c_str();
794 level = 0;
795 while (::isdigit(*endptr)) {
796 level *= 10;
797 level += *endptr++ - '0';
798 // make sure no leading zeros, except zero itself, and range check.
799 if ((level == 0) || (level > 100)) break;
800 }
Mark Salyzyna16e4372017-09-20 08:36:12 -0700801 if ((level <= 100) && (endptr != digits.c_str()) && (*endptr == ' ')) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700802 LOG(INFO) << "Battery level at startup " << level << "%";
803 if (level <= battery_dead_threshold) {
804 ret = "shutdown,battery";
805 }
806 } else {
807 LOG(DEBUG) << "Can not find first battery level in dmesg or logcat";
808 }
809 }
810 }
811
812 // Is there a controlled shutdown hint in last_reboot_reason_property?
813 if (isBluntRebootReason(ret)) {
814 // Content buffer no longer will have console data. Beware if more
815 // checks added below, that depend on parsing console content.
816 content = GetProperty(last_reboot_reason_property);
Mark Salyzyn88d692c2017-09-20 08:37:46 -0700817 transformReason(content);
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700818
Mark Salyzyn62909822017-10-09 09:27:16 -0700819 // Anything in last is better than 'super-blunt' reboot or shutdown.
820 if ((ret == "") || (ret == "reboot") || (ret == "shutdown") || !isBluntRebootReason(content)) {
821 ret = content;
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700822 }
823 }
824
825 // Other System Health HAL reasons?
826
827 // ToDo: /proc/sys/kernel/boot_reason needs a HAL interface to
828 // possibly offer hardware-specific clues from the PMIC.
829 }
830
831 // If unknown left over from above, make it "reboot,<boot_reason>"
832 if (ret == "") {
833 ret = "reboot";
834 if (android::base::StartsWith(reason, "reboot")) {
835 reason = reason.substr(strlen("reboot"));
Mark Salyzyn0af71a52017-10-05 13:58:04 -0700836 while ((reason[0] == ',') || (reason[0] == '_')) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700837 reason = reason.substr(1);
838 }
839 }
840 if (reason != "") {
841 ret += ",";
842 ret += reason;
843 }
844 }
845
846 LOG(INFO) << "Canonical boot reason: " << ret;
847 if (isKernelRebootReason(ret) && (GetProperty(last_reboot_reason_property) != "")) {
848 // Rewrite as it must be old news, kernel reasons trump user space.
849 SetProperty(last_reboot_reason_property, ret);
850 }
851 return ret;
852}
853
James Hawkinsb9cf7712016-04-08 15:32:19 -0700854// Returns the appropriate metric key prefix for the boot_complete metric such
855// that boot metrics after a system update are labeled as ota_boot_complete;
856// otherwise, they are labeled as boot_complete. This method encapsulates the
857// bookkeeping required to track when a system update has occurred by storing
858// the UTC timestamp of the system build date and comparing against the current
859// system build date.
860std::string CalculateBootCompletePrefix() {
861 static const std::string kBuildDateKey = "build_date";
862 std::string boot_complete_prefix = "boot_complete";
863
864 std::string build_date_str = GetProperty("ro.build.date.utc");
James Hawkins4dded612016-07-28 11:50:23 -0700865 int32_t build_date;
Elliott Hughesda46b392016-10-11 17:09:00 -0700866 if (!android::base::ParseInt(build_date_str, &build_date)) {
James Hawkins4dded612016-07-28 11:50:23 -0700867 return std::string();
868 }
James Hawkinsb9cf7712016-04-08 15:32:19 -0700869
870 BootEventRecordStore boot_event_store;
871 BootEventRecordStore::BootEventRecord record;
James Hawkins0bc4ad42017-05-30 15:03:15 -0700872 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record)) {
873 boot_complete_prefix = "factory_reset_" + boot_complete_prefix;
874 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700875 LOG(INFO) << "Canonical boot reason: reboot,factory_reset";
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700876 SetProperty(system_reboot_reason_property, "reboot,factory_reset");
James Hawkins0bc4ad42017-05-30 15:03:15 -0700877 } else if (build_date != record.second) {
James Hawkinsb9cf7712016-04-08 15:32:19 -0700878 boot_complete_prefix = "ota_" + boot_complete_prefix;
879 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700880 LOG(INFO) << "Canonical boot reason: reboot,ota";
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700881 SetProperty(system_reboot_reason_property, "reboot,ota");
James Hawkinsb9cf7712016-04-08 15:32:19 -0700882 }
883
884 return boot_complete_prefix;
885}
886
James Hawkinsef0a0902017-01-06 14:38:23 -0800887// Records the value of a given ro.boottime.init property in milliseconds.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700888void RecordInitBootTimeProp(BootEventRecordStore* boot_event_store, const char* property) {
James Hawkinsef0a0902017-01-06 14:38:23 -0800889 std::string value = GetProperty(property);
890
James Hawkins27c05222017-01-26 11:55:44 -0800891 int32_t time_in_ms;
892 if (android::base::ParseInt(value, &time_in_ms)) {
James Hawkinsef0a0902017-01-06 14:38:23 -0800893 boot_event_store->AddBootEventWithValue(property, time_in_ms);
894 }
895}
896
James Hawkins1bfcaec2017-05-19 14:27:27 -0700897// A map from bootloader timing stage to the time that stage took during boot.
898typedef std::map<std::string, int32_t> BootloaderTimingMap;
899
900// Returns a mapping from bootloader stage names to the time those stages
901// took to boot.
902const BootloaderTimingMap GetBootLoaderTimings() {
903 BootloaderTimingMap timings;
904
905 // |ro.boot.boottime| is of the form 'stage1:time1,...,stageN:timeN',
906 // where timeN is in milliseconds.
James Hawkinsbe46fd12017-02-02 16:21:25 -0800907 std::string value = GetProperty("ro.boot.boottime");
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800908 if (value.empty()) {
909 // ro.boot.boottime is not reported on all devices.
James Hawkins1bfcaec2017-05-19 14:27:27 -0700910 return BootloaderTimingMap();
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800911 }
James Hawkinsbe46fd12017-02-02 16:21:25 -0800912
913 auto stages = android::base::Split(value, ",");
James Hawkins1bfcaec2017-05-19 14:27:27 -0700914 for (const auto& stageTiming : stages) {
James Hawkinsbe46fd12017-02-02 16:21:25 -0800915 // |stageTiming| is of the form 'stage:time'.
916 auto stageTimingValues = android::base::Split(stageTiming, ":");
James Hawkins0bc4ad42017-05-30 15:03:15 -0700917 DCHECK_EQ(2U, stageTimingValues.size());
James Hawkinsbe46fd12017-02-02 16:21:25 -0800918
919 std::string stageName = stageTimingValues[0];
920 int32_t time_ms;
921 if (android::base::ParseInt(stageTimingValues[1], &time_ms)) {
James Hawkins1bfcaec2017-05-19 14:27:27 -0700922 timings[stageName] = time_ms;
James Hawkinsbe46fd12017-02-02 16:21:25 -0800923 }
924 }
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800925
James Hawkins1bfcaec2017-05-19 14:27:27 -0700926 return timings;
927}
928
Tej Singh4eacd382018-01-25 17:59:57 -0800929// Returns the total bootloader boot time from the ro.boot.boottime system property.
930int32_t GetBootloaderTime(const BootloaderTimingMap& bootloader_timings) {
931 int32_t total_time = 0;
932 for (const auto& timing : bootloader_timings) {
933 total_time += timing.second;
934 }
935
936 return total_time;
937}
938
James Hawkins1bfcaec2017-05-19 14:27:27 -0700939// Parses and records the set of bootloader stages and associated boot times
940// from the ro.boot.boottime system property.
941void RecordBootloaderTimings(BootEventRecordStore* boot_event_store,
942 const BootloaderTimingMap& bootloader_timings) {
943 int32_t total_time = 0;
944 for (const auto& timing : bootloader_timings) {
945 total_time += timing.second;
946 boot_event_store->AddBootEventWithValue("boottime.bootloader." + timing.first, timing.second);
947 }
948
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800949 boot_event_store->AddBootEventWithValue("boottime.bootloader.total", total_time);
James Hawkinsbe46fd12017-02-02 16:21:25 -0800950}
951
Tej Singh4eacd382018-01-25 17:59:57 -0800952// Returns the closest estimation to the absolute device boot time, i.e.,
James Hawkins1bfcaec2017-05-19 14:27:27 -0700953// from power on to boot_complete, including bootloader times.
Tej Singh4eacd382018-01-25 17:59:57 -0800954std::chrono::milliseconds GetAbsoluteBootTime(const BootloaderTimingMap& bootloader_timings,
955 std::chrono::milliseconds uptime) {
James Hawkins1bfcaec2017-05-19 14:27:27 -0700956 int32_t bootloader_time_ms = 0;
957
958 for (const auto& timing : bootloader_timings) {
959 if (timing.first.compare("SW") != 0) {
960 bootloader_time_ms += timing.second;
961 }
962 }
963
964 auto bootloader_duration = std::chrono::milliseconds(bootloader_time_ms);
Tej Singh4eacd382018-01-25 17:59:57 -0800965 return bootloader_duration + uptime;
966}
967
968// Records the closest estimation to the absolute device boot time in seconds.
969// i.e. from power on to boot_complete, including bootloader times.
970void RecordAbsoluteBootTime(BootEventRecordStore* boot_event_store,
971 std::chrono::milliseconds absolute_total) {
972 auto absolute_total_sec = std::chrono::duration_cast<std::chrono::seconds>(absolute_total);
973 boot_event_store->AddBootEventWithValue("absolute_boot_time", absolute_total_sec.count());
974}
975
976// Logs the total boot time and reason to statsd.
977void LogBootInfoToStatsd(std::chrono::milliseconds end_time,
978 std::chrono::milliseconds total_duration, int32_t bootloader_duration_ms,
979 double time_since_last_boot_sec) {
980 const std::string reason(GetProperty(bootloader_reboot_reason_property));
981
982 if (reason.empty()) {
983 android::util::stats_write(android::util::BOOT_SEQUENCE_REPORTED, "<EMPTY>", "<EMPTY>",
984 end_time.count(), total_duration.count(),
985 (int64_t)bootloader_duration_ms,
986 (int64_t)time_since_last_boot_sec * 1000);
987 return;
988 }
989
Tej Singhfe3e7622018-02-06 15:57:38 -0800990 const std::string system_reason(GetProperty(system_reboot_reason_property));
Tej Singh4eacd382018-01-25 17:59:57 -0800991 android::util::stats_write(android::util::BOOT_SEQUENCE_REPORTED, reason.c_str(),
992 system_reason.c_str(), end_time.count(), total_duration.count(),
993 (int64_t)bootloader_duration_ms,
994 (int64_t)time_since_last_boot_sec * 1000);
James Hawkins1bfcaec2017-05-19 14:27:27 -0700995}
996
Tej Singhfe3e7622018-02-06 15:57:38 -0800997void SetSystemBootReason() {
998 const std::string bootloader_boot_reason(GetProperty(bootloader_reboot_reason_property));
999 const std::string system_boot_reason(BootReasonStrToReason(bootloader_boot_reason));
1000 // Record the scrubbed system_boot_reason to the property
1001 SetProperty(system_reboot_reason_property, system_boot_reason);
1002}
1003
James Hawkinsc08e9962016-03-11 14:59:50 -08001004// Records several metrics related to the time it takes to boot the device,
1005// including disambiguating boot time on encrypted or non-encrypted devices.
1006void RecordBootComplete() {
1007 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -07001008 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -07001009
James Hawkins1bfcaec2017-05-19 14:27:27 -07001010 auto time_since_epoch = android::base::boot_clock::now().time_since_epoch();
1011 auto uptime = std::chrono::duration_cast<std::chrono::seconds>(time_since_epoch);
James Hawkins2d8b3e62016-04-14 14:13:20 -07001012 time_t current_time_utc = time(nullptr);
Tej Singh4eacd382018-01-25 17:59:57 -08001013 time_t time_since_last_boot = 0;
James Hawkins2d8b3e62016-04-14 14:13:20 -07001014
1015 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
1016 time_t last_boot_time_utc = record.second;
Tej Singh4eacd382018-01-25 17:59:57 -08001017 time_since_last_boot = difftime(current_time_utc, last_boot_time_utc);
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -07001018 boot_event_store.AddBootEventWithValue("time_since_last_boot", time_since_last_boot);
James Hawkins2d8b3e62016-04-14 14:13:20 -07001019 }
1020
1021 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -08001022
James Hawkinsb9cf7712016-04-08 15:32:19 -07001023 // The boot_complete metric has two variants: boot_complete and
1024 // ota_boot_complete. The latter signifies that the device is booting after
1025 // a system update.
1026 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkins4dded612016-07-28 11:50:23 -07001027 if (boot_complete_prefix.empty()) {
1028 // The system is hosed because the build date property could not be read.
1029 return;
1030 }
James Hawkinsc08e9962016-03-11 14:59:50 -08001031
1032 // post_decrypt_time_elapsed is only logged on encrypted devices.
1033 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
1034 // Log the amount of time elapsed until the device is decrypted, which
1035 // includes the variable amount of time the user takes to enter the
1036 // decryption password.
James Hawkinse78ea772017-03-24 11:43:02 -07001037 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -08001038
1039 // Subtract the decryption time to normalize the boot cycle timing.
James Hawkinse78ea772017-03-24 11:43:02 -07001040 std::chrono::seconds boot_complete = std::chrono::seconds(uptime.count() - record.second);
James Hawkinsb9cf7712016-04-08 15:32:19 -07001041 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinse78ea772017-03-24 11:43:02 -07001042 boot_complete.count());
James Hawkinsc08e9962016-03-11 14:59:50 -08001043 } else {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -07001044 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption", uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -08001045 }
1046
1047 // Record the total time from device startup to boot complete, regardless of
1048 // encryption state.
James Hawkinse78ea772017-03-24 11:43:02 -07001049 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime.count());
James Hawkinsef0a0902017-01-06 14:38:23 -08001050
1051 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init");
1052 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux");
1053 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait");
James Hawkinsbe46fd12017-02-02 16:21:25 -08001054
James Hawkins1bfcaec2017-05-19 14:27:27 -07001055 const BootloaderTimingMap bootloader_timings = GetBootLoaderTimings();
Tej Singh4eacd382018-01-25 17:59:57 -08001056 int32_t bootloader_boot_duration = GetBootloaderTime(bootloader_timings);
James Hawkins1bfcaec2017-05-19 14:27:27 -07001057 RecordBootloaderTimings(&boot_event_store, bootloader_timings);
1058
1059 auto uptime_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_since_epoch);
Tej Singh4eacd382018-01-25 17:59:57 -08001060 auto absolute_boot_time = GetAbsoluteBootTime(bootloader_timings, uptime_ms);
1061 RecordAbsoluteBootTime(&boot_event_store, absolute_boot_time);
1062
1063 auto boot_end_time_point = std::chrono::system_clock::now().time_since_epoch();
1064 auto boot_end_time = std::chrono::duration_cast<std::chrono::milliseconds>(boot_end_time_point);
1065
1066 LogBootInfoToStatsd(boot_end_time, absolute_boot_time, bootloader_boot_duration,
1067 time_since_last_boot);
James Hawkinsc08e9962016-03-11 14:59:50 -08001068}
1069
James Hawkinsa4a1a4a2016-02-09 15:32:38 -08001070// Records the boot_reason metric by querying the ro.boot.bootreason system
1071// property.
1072void RecordBootReason() {
Mark Salyzynb304f6d2017-08-04 13:35:51 -07001073 const std::string reason(GetProperty(bootloader_reboot_reason_property));
James Hawkins25f71222017-10-10 16:37:05 -07001074
1075 if (reason.empty()) {
1076 // Log an empty boot reason value as '<EMPTY>' to ensure the value is intentional
1077 // (and not corruption anywhere else in the reporting pipeline).
1078 android::metricslogger::LogMultiAction(android::metricslogger::ACTION_BOOT,
1079 android::metricslogger::FIELD_PLATFORM_REASON, "<EMPTY>");
1080 } else {
1081 android::metricslogger::LogMultiAction(android::metricslogger::ACTION_BOOT,
1082 android::metricslogger::FIELD_PLATFORM_REASON, reason);
1083 }
Mark Salyzynb304f6d2017-08-04 13:35:51 -07001084
1085 // Log the raw bootloader_boot_reason property value.
1086 int32_t boot_reason = BootReasonStrToEnum(reason);
James Hawkinsa4a1a4a2016-02-09 15:32:38 -08001087 BootEventRecordStore boot_event_store;
1088 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
Mark Salyzynb304f6d2017-08-04 13:35:51 -07001089
1090 // Log the scrubbed system_boot_reason.
Tej Singhfe3e7622018-02-06 15:57:38 -08001091 const std::string system_reason(GetProperty(system_reboot_reason_property));
Mark Salyzynb304f6d2017-08-04 13:35:51 -07001092 int32_t system_boot_reason = BootReasonStrToEnum(system_reason);
1093 boot_event_store.AddBootEventWithValue("system_boot_reason", system_boot_reason);
1094
Mark Salyzynb304f6d2017-08-04 13:35:51 -07001095 if (reason == "") {
1096 SetProperty(bootloader_reboot_reason_property, system_reason);
1097 }
James Hawkinsa4a1a4a2016-02-09 15:32:38 -08001098}
1099
James Hawkins500d7152016-02-16 15:05:54 -08001100// Records two metrics related to the user resetting a device: the time at
1101// which the device is reset, and the time since the user last reset the
1102// device. The former is only set once per-factory reset.
1103void RecordFactoryReset() {
1104 BootEventRecordStore boot_event_store;
1105 BootEventRecordStore::BootEventRecord record;
1106
1107 time_t current_time_utc = time(nullptr);
1108
James Hawkins0660b302016-03-08 16:18:15 -08001109 if (current_time_utc < 0) {
1110 // UMA does not display negative values in buckets, so convert to positive.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -07001111 android::metricslogger::LogHistogram("factory_reset_current_time_failure",
1112 std::abs(current_time_utc));
James Hawkinsfff95ba2016-03-29 16:13:49 -07001113
James Hawkins9aec9262017-01-31 11:42:24 -08001114 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -07001115 // is losing records somehow.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -07001116 boot_event_store.AddBootEventWithValue("factory_reset_current_time_failure",
1117 std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -08001118 return;
1119 } else {
James Hawkins9aec9262017-01-31 11:42:24 -08001120 android::metricslogger::LogHistogram("factory_reset_current_time", current_time_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -07001121
James Hawkins9aec9262017-01-31 11:42:24 -08001122 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -07001123 // is losing records somehow.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -07001124 boot_event_store.AddBootEventWithValue("factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -08001125 }
1126
James Hawkins500d7152016-02-16 15:05:54 -08001127 // The factory_reset boot event does not exist after the device is reset, so
1128 // use this signal to mark the time of the factory reset.
1129 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
1130 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -08001131
1132 // Don't log the time_since_factory_reset until some time has elapsed.
1133 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -08001134 return;
1135 }
1136
1137 // Calculate and record the difference in time between now and the
1138 // factory_reset time.
1139 time_t factory_reset_utc = record.second;
James Hawkins9aec9262017-01-31 11:42:24 -08001140 android::metricslogger::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -07001141
James Hawkins9aec9262017-01-31 11:42:24 -08001142 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -07001143 // is losing records somehow.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -07001144 boot_event_store.AddBootEventWithValue("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -07001145
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -07001146 time_t time_since_factory_reset = difftime(current_time_utc, factory_reset_utc);
1147 boot_event_store.AddBootEventWithValue("time_since_factory_reset", time_since_factory_reset);
James Hawkins500d7152016-02-16 15:05:54 -08001148}
1149
James Hawkinsabd73e62016-01-19 15:10:38 -08001150} // namespace
1151
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -07001152int main(int argc, char** argv) {
James Hawkinsabd73e62016-01-19 15:10:38 -08001153 android::base::InitLogging(argv);
1154
1155 const std::string cmd_line = GetCommandLine(argc, argv);
1156 LOG(INFO) << "Service started: " << cmd_line;
1157
James Hawkinsa4a1a4a2016-02-09 15:32:38 -08001158 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -07001159 static const char value_str[] = "value";
Tej Singhfe3e7622018-02-06 15:57:38 -08001160 static const char system_boot_reason_str[] = "set_system_boot_reason";
James Hawkinsc08e9962016-03-11 14:59:50 -08001161 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -08001162 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -08001163 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -08001164 static const struct option long_options[] = {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -07001165 // clang-format off
Tej Singhfe3e7622018-02-06 15:57:38 -08001166 { "help", no_argument, NULL, 'h' },
1167 { "log", no_argument, NULL, 'l' },
1168 { "print", no_argument, NULL, 'p' },
1169 { "record", required_argument, NULL, 'r' },
1170 { value_str, required_argument, NULL, 0 },
1171 { system_boot_reason_str, no_argument, NULL, 0 },
1172 { boot_complete_str, no_argument, NULL, 0 },
1173 { boot_reason_str, no_argument, NULL, 0 },
1174 { factory_reset_str, no_argument, NULL, 0 },
1175 { NULL, 0, NULL, 0 }
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -07001176 // clang-format on
James Hawkinsa4a1a4a2016-02-09 15:32:38 -08001177 };
1178
James Hawkinsc6275582016-03-22 10:47:44 -07001179 std::string boot_event;
1180 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -08001181 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -08001182 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -08001183 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -08001184 // This case handles long options which have no single-character mapping.
1185 case 0: {
1186 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -07001187 if (option_name == value_str) {
1188 // |optarg| is an external variable set by getopt representing
1189 // the option argument.
1190 value = optarg;
Tej Singhfe3e7622018-02-06 15:57:38 -08001191 } else if (option_name == system_boot_reason_str) {
1192 SetSystemBootReason();
James Hawkinsc6275582016-03-22 10:47:44 -07001193 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -08001194 RecordBootComplete();
1195 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -08001196 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -08001197 } else if (option_name == factory_reset_str) {
1198 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -08001199 } else {
1200 LOG(ERROR) << "Invalid option: " << option_name;
1201 }
1202 break;
1203 }
1204
James Hawkinsabd73e62016-01-19 15:10:38 -08001205 case 'h': {
1206 ShowHelp(argv[0]);
1207 break;
1208 }
1209
1210 case 'l': {
1211 LogBootEvents();
1212 break;
1213 }
1214
1215 case 'p': {
1216 PrintBootEvents();
1217 break;
1218 }
1219
1220 case 'r': {
1221 // |optarg| is an external variable set by getopt representing
1222 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -07001223 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -08001224 break;
1225 }
1226
1227 default: {
1228 DCHECK_EQ(opt, '?');
1229
1230 // |optopt| is an external variable set by getopt representing
1231 // the value of the invalid option.
1232 LOG(ERROR) << "Invalid option: " << optopt;
1233 ShowHelp(argv[0]);
1234 return EXIT_FAILURE;
1235 }
1236 }
1237 }
1238
James Hawkinsc6275582016-03-22 10:47:44 -07001239 if (!boot_event.empty()) {
1240 RecordBootEventFromCommandLine(boot_event, value);
1241 }
1242
James Hawkinsabd73e62016-01-19 15:10:38 -08001243 return 0;
1244}