blob: 595239ca8bf2a263397150621db1b518a1e44848 [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>
James Hawkinsabd73e62016-01-19 15:10:38 -080022#include <unistd.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070023
James Hawkins0660b302016-03-08 16:18:15 -080024#include <cmath>
James Hawkinsabd73e62016-01-19 15:10:38 -080025#include <cstddef>
26#include <cstdio>
James Hawkins500d7152016-02-16 15:05:54 -080027#include <ctime>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080028#include <map>
James Hawkinsabd73e62016-01-19 15:10:38 -080029#include <memory>
30#include <string>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070031
32#include <android/log.h>
James Hawkinseabe08b2016-01-19 16:54:35 -080033#include <android-base/logging.h>
James Hawkins4dded612016-07-28 11:50:23 -070034#include <android-base/parseint.h>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080035#include <cutils/properties.h>
James Hawkins9aec9262017-01-31 11:42:24 -080036#include <metricslogger/metrics_logger.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070037
James Hawkinsabd73e62016-01-19 15:10:38 -080038#include "boot_event_record_store.h"
James Hawkinsc08e9962016-03-11 14:59:50 -080039#include "uptime_parser.h"
James Hawkinsabd73e62016-01-19 15:10:38 -080040
41namespace {
42
James Hawkinsabd73e62016-01-19 15:10:38 -080043// Scans the boot event record store for record files and logs each boot event
44// via EventLog.
45void LogBootEvents() {
46 BootEventRecordStore boot_event_store;
47
48 auto events = boot_event_store.GetAllBootEvents();
49 for (auto i = events.cbegin(); i != events.cend(); ++i) {
James Hawkins9aec9262017-01-31 11:42:24 -080050 android::metricslogger::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080051 }
52}
53
James Hawkinsc6275582016-03-22 10:47:44 -070054// Records the named boot |event| to the record store. If |value| is non-empty
55// and is a proper string representation of an integer value, the converted
56// integer value is associated with the boot event.
57void RecordBootEventFromCommandLine(
58 const std::string& event, const std::string& value_str) {
59 BootEventRecordStore boot_event_store;
60 if (!value_str.empty()) {
61 int32_t value = 0;
Elliott Hughesda46b392016-10-11 17:09:00 -070062 if (android::base::ParseInt(value_str, &value)) {
James Hawkins4dded612016-07-28 11:50:23 -070063 boot_event_store.AddBootEventWithValue(event, value);
64 }
James Hawkinsc6275582016-03-22 10:47:44 -070065 } else {
66 boot_event_store.AddBootEvent(event);
67 }
68}
69
James Hawkinsabd73e62016-01-19 15:10:38 -080070void PrintBootEvents() {
71 printf("Boot events:\n");
72 printf("------------\n");
73
74 BootEventRecordStore boot_event_store;
75 auto events = boot_event_store.GetAllBootEvents();
76 for (auto i = events.cbegin(); i != events.cend(); ++i) {
77 printf("%s\t%d\n", i->first.c_str(), i->second);
78 }
79}
80
81void ShowHelp(const char *cmd) {
82 fprintf(stderr, "Usage: %s [options]\n", cmd);
83 fprintf(stderr,
84 "options include:\n"
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080085 " -h, --help Show this help\n"
86 " -l, --log Log all metrics to logstorage\n"
87 " -p, --print Dump the boot event records to the console\n"
88 " -r, --record Record the timestamp of a named boot event\n"
James Hawkinsc6275582016-03-22 10:47:44 -070089 " --value Optional value to associate with the boot event\n"
James Hawkins53684ea2016-02-23 16:18:19 -080090 " --record_boot_reason Record the reason why the device booted\n"
91 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080092}
93
94// Constructs a readable, printable string from the givencommand line
95// arguments.
96std::string GetCommandLine(int argc, char **argv) {
97 std::string cmd;
98 for (int i = 0; i < argc; ++i) {
99 cmd += argv[i];
100 cmd += " ";
101 }
102
103 return cmd;
104}
105
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800106// Convenience wrapper over the property API that returns an
107// std::string.
108std::string GetProperty(const char* key) {
109 std::vector<char> temp(PROPERTY_VALUE_MAX);
110 const int len = property_get(key, &temp[0], nullptr);
111 if (len < 0) {
112 return "";
113 }
114 return std::string(&temp[0], len);
115}
116
James Hawkins6f74c0b2016-02-12 15:49:16 -0800117constexpr int32_t kUnknownBootReason = 1;
118
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800119// A mapping from boot reason string, as read from the ro.boot.bootreason
120// system property, to a unique integer ID. Viewers of log data dashboards for
121// the boot_reason metric may refer to this mapping to discern the histogram
122// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800123const std::map<std::string, int32_t> kBootReasonMap = {
124 {"unknown", kUnknownBootReason},
125 {"normal", 2},
126 {"recovery", 3},
127 {"reboot", 4},
128 {"PowerKey", 5},
129 {"hard_reset", 6},
130 {"kernel_panic", 7},
131 {"rpm_err", 8},
132 {"hw_reset", 9},
133 {"tz_err", 10},
134 {"adsp_err", 11},
135 {"modem_err", 12},
136 {"mba_err", 13},
137 {"Watchdog", 14},
138 {"Panic", 15},
139 {"power_key", 16},
140 {"power_on", 17},
141 {"Reboot", 18},
142 {"rtc", 19},
143 {"edl", 20},
James Hawkins45ead352016-03-08 16:42:07 -0800144 {"oem_pon1", 21},
145 {"oem_powerkey", 22},
146 {"oem_unknown_reset", 23},
147 {"srto: HWWDT reset SC", 24},
148 {"srto: HWWDT reset platform", 25},
149 {"srto: bootloader", 26},
150 {"srto: kernel panic", 27},
151 {"srto: kernel watchdog reset", 28},
152 {"srto: normal", 29},
153 {"srto: reboot", 30},
154 {"srto: reboot-bootloader", 31},
155 {"srto: security watchdog reset", 32},
156 {"srto: wakesrc", 33},
157 {"srto: watchdog", 34},
158 {"srto:1-1", 35},
159 {"srto:omap_hsmm", 36},
160 {"srto:phy0", 37},
161 {"srto:rtc0", 38},
162 {"srto:touchpad", 39},
163 {"watchdog", 40},
164 {"watchdogr", 41},
165 {"wdog_bark", 42},
166 {"wdog_bite", 43},
167 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800168};
169
170// Converts a string value representing the reason the system booted to an
171// integer representation. This is necessary for logging the boot_reason metric
172// via Tron, which does not accept non-integer buckets in histograms.
173int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800174 auto mapping = kBootReasonMap.find(boot_reason);
175 if (mapping != kBootReasonMap.end()) {
176 return mapping->second;
177 }
178
179 LOG(INFO) << "Unknown boot reason: " << boot_reason;
180 return kUnknownBootReason;
181}
182
James Hawkinsb9cf7712016-04-08 15:32:19 -0700183// Returns the appropriate metric key prefix for the boot_complete metric such
184// that boot metrics after a system update are labeled as ota_boot_complete;
185// otherwise, they are labeled as boot_complete. This method encapsulates the
186// bookkeeping required to track when a system update has occurred by storing
187// the UTC timestamp of the system build date and comparing against the current
188// system build date.
189std::string CalculateBootCompletePrefix() {
190 static const std::string kBuildDateKey = "build_date";
191 std::string boot_complete_prefix = "boot_complete";
192
193 std::string build_date_str = GetProperty("ro.build.date.utc");
James Hawkins4dded612016-07-28 11:50:23 -0700194 int32_t build_date;
Elliott Hughesda46b392016-10-11 17:09:00 -0700195 if (!android::base::ParseInt(build_date_str, &build_date)) {
James Hawkins4dded612016-07-28 11:50:23 -0700196 return std::string();
197 }
James Hawkinsb9cf7712016-04-08 15:32:19 -0700198
199 BootEventRecordStore boot_event_store;
200 BootEventRecordStore::BootEventRecord record;
201 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record) ||
202 build_date != record.second) {
203 boot_complete_prefix = "ota_" + boot_complete_prefix;
204 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
205 }
206
207 return boot_complete_prefix;
208}
209
James Hawkinsef0a0902017-01-06 14:38:23 -0800210// Records the value of a given ro.boottime.init property in milliseconds.
211void RecordInitBootTimeProp(
212 BootEventRecordStore* boot_event_store, const char* property) {
213 std::string value = GetProperty(property);
214
James Hawkins27c05222017-01-26 11:55:44 -0800215 int32_t time_in_ms;
216 if (android::base::ParseInt(value, &time_in_ms)) {
James Hawkinsef0a0902017-01-06 14:38:23 -0800217 boot_event_store->AddBootEventWithValue(property, time_in_ms);
218 }
219}
220
James Hawkinsc08e9962016-03-11 14:59:50 -0800221// Records several metrics related to the time it takes to boot the device,
222// including disambiguating boot time on encrypted or non-encrypted devices.
223void RecordBootComplete() {
224 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700225 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -0700226
James Hawkinsc08e9962016-03-11 14:59:50 -0800227 time_t uptime = bootstat::ParseUptime();
James Hawkins2d8b3e62016-04-14 14:13:20 -0700228 time_t current_time_utc = time(nullptr);
229
230 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
231 time_t last_boot_time_utc = record.second;
232 time_t time_since_last_boot = difftime(current_time_utc,
233 last_boot_time_utc);
234 boot_event_store.AddBootEventWithValue("time_since_last_boot",
235 time_since_last_boot);
236 }
237
238 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -0800239
James Hawkinsb9cf7712016-04-08 15:32:19 -0700240 // The boot_complete metric has two variants: boot_complete and
241 // ota_boot_complete. The latter signifies that the device is booting after
242 // a system update.
243 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkins4dded612016-07-28 11:50:23 -0700244 if (boot_complete_prefix.empty()) {
245 // The system is hosed because the build date property could not be read.
246 return;
247 }
James Hawkinsc08e9962016-03-11 14:59:50 -0800248
249 // post_decrypt_time_elapsed is only logged on encrypted devices.
250 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
251 // Log the amount of time elapsed until the device is decrypted, which
252 // includes the variable amount of time the user takes to enter the
253 // decryption password.
254 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
255
256 // Subtract the decryption time to normalize the boot cycle timing.
257 time_t boot_complete = uptime - record.second;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700258 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinsc08e9962016-03-11 14:59:50 -0800259 boot_complete);
260
261
262 } else {
James Hawkinsb9cf7712016-04-08 15:32:19 -0700263 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
James Hawkinsc08e9962016-03-11 14:59:50 -0800264 uptime);
265 }
266
267 // Record the total time from device startup to boot complete, regardless of
268 // encryption state.
James Hawkinsb9cf7712016-04-08 15:32:19 -0700269 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime);
James Hawkinsef0a0902017-01-06 14:38:23 -0800270
271 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init");
272 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux");
273 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait");
James Hawkinsc08e9962016-03-11 14:59:50 -0800274}
275
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800276// Records the boot_reason metric by querying the ro.boot.bootreason system
277// property.
278void RecordBootReason() {
279 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
280 BootEventRecordStore boot_event_store;
281 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
282}
283
James Hawkins500d7152016-02-16 15:05:54 -0800284// Records two metrics related to the user resetting a device: the time at
285// which the device is reset, and the time since the user last reset the
286// device. The former is only set once per-factory reset.
287void RecordFactoryReset() {
288 BootEventRecordStore boot_event_store;
289 BootEventRecordStore::BootEventRecord record;
290
291 time_t current_time_utc = time(nullptr);
292
James Hawkins0660b302016-03-08 16:18:15 -0800293 if (current_time_utc < 0) {
294 // UMA does not display negative values in buckets, so convert to positive.
James Hawkins9aec9262017-01-31 11:42:24 -0800295 android::metricslogger::LogHistogram(
James Hawkinsfff95ba2016-03-29 16:13:49 -0700296 "factory_reset_current_time_failure", std::abs(current_time_utc));
297
James Hawkins9aec9262017-01-31 11:42:24 -0800298 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700299 // is losing records somehow.
300 boot_event_store.AddBootEventWithValue(
301 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800302 return;
303 } else {
James Hawkins9aec9262017-01-31 11:42:24 -0800304 android::metricslogger::LogHistogram("factory_reset_current_time", current_time_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700305
James Hawkins9aec9262017-01-31 11:42:24 -0800306 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700307 // is losing records somehow.
308 boot_event_store.AddBootEventWithValue(
309 "factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800310 }
311
James Hawkins500d7152016-02-16 15:05:54 -0800312 // The factory_reset boot event does not exist after the device is reset, so
313 // use this signal to mark the time of the factory reset.
314 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
315 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800316
317 // Don't log the time_since_factory_reset until some time has elapsed.
318 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800319 return;
320 }
321
322 // Calculate and record the difference in time between now and the
323 // factory_reset time.
324 time_t factory_reset_utc = record.second;
James Hawkins9aec9262017-01-31 11:42:24 -0800325 android::metricslogger::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700326
James Hawkins9aec9262017-01-31 11:42:24 -0800327 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700328 // is losing records somehow.
329 boot_event_store.AddBootEventWithValue(
330 "factory_reset_record_value", factory_reset_utc);
331
James Hawkins500d7152016-02-16 15:05:54 -0800332 time_t time_since_factory_reset = difftime(current_time_utc,
333 factory_reset_utc);
334 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
335 time_since_factory_reset);
336}
337
James Hawkinsabd73e62016-01-19 15:10:38 -0800338} // namespace
339
340int main(int argc, char **argv) {
341 android::base::InitLogging(argv);
342
343 const std::string cmd_line = GetCommandLine(argc, argv);
344 LOG(INFO) << "Service started: " << cmd_line;
345
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800346 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700347 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800348 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800349 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800350 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800351 static const struct option long_options[] = {
352 { "help", no_argument, NULL, 'h' },
353 { "log", no_argument, NULL, 'l' },
354 { "print", no_argument, NULL, 'p' },
355 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700356 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800357 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800358 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800359 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800360 { NULL, 0, NULL, 0 }
361 };
362
James Hawkinsc6275582016-03-22 10:47:44 -0700363 std::string boot_event;
364 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800365 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800366 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800367 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800368 // This case handles long options which have no single-character mapping.
369 case 0: {
370 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700371 if (option_name == value_str) {
372 // |optarg| is an external variable set by getopt representing
373 // the option argument.
374 value = optarg;
375 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800376 RecordBootComplete();
377 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800378 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800379 } else if (option_name == factory_reset_str) {
380 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800381 } else {
382 LOG(ERROR) << "Invalid option: " << option_name;
383 }
384 break;
385 }
386
James Hawkinsabd73e62016-01-19 15:10:38 -0800387 case 'h': {
388 ShowHelp(argv[0]);
389 break;
390 }
391
392 case 'l': {
393 LogBootEvents();
394 break;
395 }
396
397 case 'p': {
398 PrintBootEvents();
399 break;
400 }
401
402 case 'r': {
403 // |optarg| is an external variable set by getopt representing
404 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700405 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800406 break;
407 }
408
409 default: {
410 DCHECK_EQ(opt, '?');
411
412 // |optopt| is an external variable set by getopt representing
413 // the value of the invalid option.
414 LOG(ERROR) << "Invalid option: " << optopt;
415 ShowHelp(argv[0]);
416 return EXIT_FAILURE;
417 }
418 }
419 }
420
James Hawkinsc6275582016-03-22 10:47:44 -0700421 if (!boot_event.empty()) {
422 RecordBootEventFromCommandLine(boot_event, value);
423 }
424
James Hawkinsabd73e62016-01-19 15:10:38 -0800425 return 0;
426}