blob: c85667e8605a1f73136487fb40e0e0122f46acd9 [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>
James Hawkinsbe46fd12017-02-02 16:21:25 -080031#include <vector>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070032
33#include <android/log.h>
James Hawkinseabe08b2016-01-19 16:54:35 -080034#include <android-base/logging.h>
James Hawkins4dded612016-07-28 11:50:23 -070035#include <android-base/parseint.h>
James Hawkinsbe46fd12017-02-02 16:21:25 -080036#include <android-base/strings.h>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080037#include <cutils/properties.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070038
James Hawkinsabd73e62016-01-19 15:10:38 -080039#include "boot_event_record_store.h"
James Hawkins6f282992016-03-22 14:13:06 -070040#include "histogram_logger.h"
James Hawkinsc08e9962016-03-11 14:59:50 -080041#include "uptime_parser.h"
James Hawkinsabd73e62016-01-19 15:10:38 -080042
43namespace {
44
James Hawkinsabd73e62016-01-19 15:10:38 -080045// Scans the boot event record store for record files and logs each boot event
46// via EventLog.
47void LogBootEvents() {
48 BootEventRecordStore boot_event_store;
49
50 auto events = boot_event_store.GetAllBootEvents();
51 for (auto i = events.cbegin(); i != events.cend(); ++i) {
James Hawkins6f282992016-03-22 14:13:06 -070052 bootstat::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080053 }
54}
55
James Hawkinsc6275582016-03-22 10:47:44 -070056// Records the named boot |event| to the record store. If |value| is non-empty
57// and is a proper string representation of an integer value, the converted
58// integer value is associated with the boot event.
59void RecordBootEventFromCommandLine(
60 const std::string& event, const std::string& value_str) {
61 BootEventRecordStore boot_event_store;
62 if (!value_str.empty()) {
63 int32_t value = 0;
Elliott Hughesda46b392016-10-11 17:09:00 -070064 if (android::base::ParseInt(value_str, &value)) {
James Hawkins4dded612016-07-28 11:50:23 -070065 boot_event_store.AddBootEventWithValue(event, value);
66 }
James Hawkinsc6275582016-03-22 10:47:44 -070067 } else {
68 boot_event_store.AddBootEvent(event);
69 }
70}
71
James Hawkinsabd73e62016-01-19 15:10:38 -080072void PrintBootEvents() {
73 printf("Boot events:\n");
74 printf("------------\n");
75
76 BootEventRecordStore boot_event_store;
77 auto events = boot_event_store.GetAllBootEvents();
78 for (auto i = events.cbegin(); i != events.cend(); ++i) {
79 printf("%s\t%d\n", i->first.c_str(), i->second);
80 }
81}
82
83void ShowHelp(const char *cmd) {
84 fprintf(stderr, "Usage: %s [options]\n", cmd);
85 fprintf(stderr,
86 "options include:\n"
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080087 " -h, --help Show this help\n"
88 " -l, --log Log all metrics to logstorage\n"
89 " -p, --print Dump the boot event records to the console\n"
90 " -r, --record Record the timestamp of a named boot event\n"
James Hawkinsc6275582016-03-22 10:47:44 -070091 " --value Optional value to associate with the boot event\n"
James Hawkins53684ea2016-02-23 16:18:19 -080092 " --record_boot_reason Record the reason why the device booted\n"
93 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080094}
95
96// Constructs a readable, printable string from the givencommand line
97// arguments.
98std::string GetCommandLine(int argc, char **argv) {
99 std::string cmd;
100 for (int i = 0; i < argc; ++i) {
101 cmd += argv[i];
102 cmd += " ";
103 }
104
105 return cmd;
106}
107
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800108// Convenience wrapper over the property API that returns an
109// std::string.
110std::string GetProperty(const char* key) {
111 std::vector<char> temp(PROPERTY_VALUE_MAX);
112 const int len = property_get(key, &temp[0], nullptr);
113 if (len < 0) {
114 return "";
115 }
116 return std::string(&temp[0], len);
117}
118
James Hawkins6f74c0b2016-02-12 15:49:16 -0800119constexpr int32_t kUnknownBootReason = 1;
120
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800121// A mapping from boot reason string, as read from the ro.boot.bootreason
122// system property, to a unique integer ID. Viewers of log data dashboards for
123// the boot_reason metric may refer to this mapping to discern the histogram
124// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800125const std::map<std::string, int32_t> kBootReasonMap = {
126 {"unknown", kUnknownBootReason},
127 {"normal", 2},
128 {"recovery", 3},
129 {"reboot", 4},
130 {"PowerKey", 5},
131 {"hard_reset", 6},
132 {"kernel_panic", 7},
133 {"rpm_err", 8},
134 {"hw_reset", 9},
135 {"tz_err", 10},
136 {"adsp_err", 11},
137 {"modem_err", 12},
138 {"mba_err", 13},
139 {"Watchdog", 14},
140 {"Panic", 15},
141 {"power_key", 16},
142 {"power_on", 17},
143 {"Reboot", 18},
144 {"rtc", 19},
145 {"edl", 20},
James Hawkins45ead352016-03-08 16:42:07 -0800146 {"oem_pon1", 21},
147 {"oem_powerkey", 22},
148 {"oem_unknown_reset", 23},
149 {"srto: HWWDT reset SC", 24},
150 {"srto: HWWDT reset platform", 25},
151 {"srto: bootloader", 26},
152 {"srto: kernel panic", 27},
153 {"srto: kernel watchdog reset", 28},
154 {"srto: normal", 29},
155 {"srto: reboot", 30},
156 {"srto: reboot-bootloader", 31},
157 {"srto: security watchdog reset", 32},
158 {"srto: wakesrc", 33},
159 {"srto: watchdog", 34},
160 {"srto:1-1", 35},
161 {"srto:omap_hsmm", 36},
162 {"srto:phy0", 37},
163 {"srto:rtc0", 38},
164 {"srto:touchpad", 39},
165 {"watchdog", 40},
166 {"watchdogr", 41},
167 {"wdog_bark", 42},
168 {"wdog_bite", 43},
169 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800170};
171
172// Converts a string value representing the reason the system booted to an
173// integer representation. This is necessary for logging the boot_reason metric
174// via Tron, which does not accept non-integer buckets in histograms.
175int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800176 auto mapping = kBootReasonMap.find(boot_reason);
177 if (mapping != kBootReasonMap.end()) {
178 return mapping->second;
179 }
180
181 LOG(INFO) << "Unknown boot reason: " << boot_reason;
182 return kUnknownBootReason;
183}
184
James Hawkinsb9cf7712016-04-08 15:32:19 -0700185// Returns the appropriate metric key prefix for the boot_complete metric such
186// that boot metrics after a system update are labeled as ota_boot_complete;
187// otherwise, they are labeled as boot_complete. This method encapsulates the
188// bookkeeping required to track when a system update has occurred by storing
189// the UTC timestamp of the system build date and comparing against the current
190// system build date.
191std::string CalculateBootCompletePrefix() {
192 static const std::string kBuildDateKey = "build_date";
193 std::string boot_complete_prefix = "boot_complete";
194
195 std::string build_date_str = GetProperty("ro.build.date.utc");
James Hawkins4dded612016-07-28 11:50:23 -0700196 int32_t build_date;
Elliott Hughesda46b392016-10-11 17:09:00 -0700197 if (!android::base::ParseInt(build_date_str, &build_date)) {
James Hawkins4dded612016-07-28 11:50:23 -0700198 return std::string();
199 }
James Hawkinsb9cf7712016-04-08 15:32:19 -0700200
201 BootEventRecordStore boot_event_store;
202 BootEventRecordStore::BootEventRecord record;
203 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record) ||
204 build_date != record.second) {
205 boot_complete_prefix = "ota_" + boot_complete_prefix;
206 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
207 }
208
209 return boot_complete_prefix;
210}
211
James Hawkinsef0a0902017-01-06 14:38:23 -0800212// Records the value of a given ro.boottime.init property in milliseconds.
213void RecordInitBootTimeProp(
214 BootEventRecordStore* boot_event_store, const char* property) {
215 std::string value = GetProperty(property);
216
James Hawkins27c05222017-01-26 11:55:44 -0800217 int32_t time_in_ms;
218 if (android::base::ParseInt(value, &time_in_ms)) {
James Hawkinsef0a0902017-01-06 14:38:23 -0800219 boot_event_store->AddBootEventWithValue(property, time_in_ms);
220 }
221}
222
James Hawkinsbe46fd12017-02-02 16:21:25 -0800223// Parses and records the set of bootloader stages and associated boot times
224// from the ro.boot.boottime system property.
225void RecordBootloaderTimings(BootEventRecordStore* boot_event_store) {
226 // |ro.boot.boottime| is of the form 'stage1:time1,...,stageN:timeN'.
227 std::string value = GetProperty("ro.boot.boottime");
228
229 auto stages = android::base::Split(value, ",");
230 for (auto const &stageTiming : stages) {
231 // |stageTiming| is of the form 'stage:time'.
232 auto stageTimingValues = android::base::Split(stageTiming, ":");
233 DCHECK_EQ(2, stageTimingValues.size());
234
235 std::string stageName = stageTimingValues[0];
236 int32_t time_ms;
237 if (android::base::ParseInt(stageTimingValues[1], &time_ms)) {
238 boot_event_store->AddBootEventWithValue(
239 "boottime.bootloader." + stageName, time_ms);
240 }
241 }
242}
243
James Hawkinsc08e9962016-03-11 14:59:50 -0800244// Records several metrics related to the time it takes to boot the device,
245// including disambiguating boot time on encrypted or non-encrypted devices.
246void RecordBootComplete() {
247 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700248 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -0700249
James Hawkinsc08e9962016-03-11 14:59:50 -0800250 time_t uptime = bootstat::ParseUptime();
James Hawkins2d8b3e62016-04-14 14:13:20 -0700251 time_t current_time_utc = time(nullptr);
252
253 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
254 time_t last_boot_time_utc = record.second;
255 time_t time_since_last_boot = difftime(current_time_utc,
256 last_boot_time_utc);
257 boot_event_store.AddBootEventWithValue("time_since_last_boot",
258 time_since_last_boot);
259 }
260
261 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -0800262
James Hawkinsb9cf7712016-04-08 15:32:19 -0700263 // The boot_complete metric has two variants: boot_complete and
264 // ota_boot_complete. The latter signifies that the device is booting after
265 // a system update.
266 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkins4dded612016-07-28 11:50:23 -0700267 if (boot_complete_prefix.empty()) {
268 // The system is hosed because the build date property could not be read.
269 return;
270 }
James Hawkinsc08e9962016-03-11 14:59:50 -0800271
272 // post_decrypt_time_elapsed is only logged on encrypted devices.
273 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
274 // Log the amount of time elapsed until the device is decrypted, which
275 // includes the variable amount of time the user takes to enter the
276 // decryption password.
277 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
278
279 // Subtract the decryption time to normalize the boot cycle timing.
280 time_t boot_complete = uptime - record.second;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700281 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinsc08e9962016-03-11 14:59:50 -0800282 boot_complete);
283
284
285 } else {
James Hawkinsb9cf7712016-04-08 15:32:19 -0700286 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
James Hawkinsc08e9962016-03-11 14:59:50 -0800287 uptime);
288 }
289
290 // Record the total time from device startup to boot complete, regardless of
291 // encryption state.
James Hawkinsb9cf7712016-04-08 15:32:19 -0700292 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime);
James Hawkinsef0a0902017-01-06 14:38:23 -0800293
294 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init");
295 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux");
296 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait");
James Hawkinsbe46fd12017-02-02 16:21:25 -0800297
298 RecordBootloaderTimings(&boot_event_store);
James Hawkinsc08e9962016-03-11 14:59:50 -0800299}
300
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800301// Records the boot_reason metric by querying the ro.boot.bootreason system
302// property.
303void RecordBootReason() {
304 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
305 BootEventRecordStore boot_event_store;
306 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
307}
308
James Hawkins500d7152016-02-16 15:05:54 -0800309// Records two metrics related to the user resetting a device: the time at
310// which the device is reset, and the time since the user last reset the
311// device. The former is only set once per-factory reset.
312void RecordFactoryReset() {
313 BootEventRecordStore boot_event_store;
314 BootEventRecordStore::BootEventRecord record;
315
316 time_t current_time_utc = time(nullptr);
317
James Hawkins0660b302016-03-08 16:18:15 -0800318 if (current_time_utc < 0) {
319 // UMA does not display negative values in buckets, so convert to positive.
James Hawkinsfff95ba2016-03-29 16:13:49 -0700320 bootstat::LogHistogram(
321 "factory_reset_current_time_failure", std::abs(current_time_utc));
322
323 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
324 // is losing records somehow.
325 boot_event_store.AddBootEventWithValue(
326 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800327 return;
328 } else {
James Hawkinsfff95ba2016-03-29 16:13:49 -0700329 bootstat::LogHistogram("factory_reset_current_time", current_time_utc);
330
331 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
332 // is losing records somehow.
333 boot_event_store.AddBootEventWithValue(
334 "factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800335 }
336
James Hawkins500d7152016-02-16 15:05:54 -0800337 // The factory_reset boot event does not exist after the device is reset, so
338 // use this signal to mark the time of the factory reset.
339 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
340 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800341
342 // Don't log the time_since_factory_reset until some time has elapsed.
343 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800344 return;
345 }
346
347 // Calculate and record the difference in time between now and the
348 // factory_reset time.
349 time_t factory_reset_utc = record.second;
James Hawkins6f282992016-03-22 14:13:06 -0700350 bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700351
352 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
353 // is losing records somehow.
354 boot_event_store.AddBootEventWithValue(
355 "factory_reset_record_value", factory_reset_utc);
356
James Hawkins500d7152016-02-16 15:05:54 -0800357 time_t time_since_factory_reset = difftime(current_time_utc,
358 factory_reset_utc);
359 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
360 time_since_factory_reset);
361}
362
James Hawkinsabd73e62016-01-19 15:10:38 -0800363} // namespace
364
365int main(int argc, char **argv) {
366 android::base::InitLogging(argv);
367
368 const std::string cmd_line = GetCommandLine(argc, argv);
369 LOG(INFO) << "Service started: " << cmd_line;
370
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800371 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700372 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800373 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800374 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800375 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800376 static const struct option long_options[] = {
377 { "help", no_argument, NULL, 'h' },
378 { "log", no_argument, NULL, 'l' },
379 { "print", no_argument, NULL, 'p' },
380 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700381 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800382 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800383 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800384 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800385 { NULL, 0, NULL, 0 }
386 };
387
James Hawkinsc6275582016-03-22 10:47:44 -0700388 std::string boot_event;
389 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800390 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800391 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800392 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800393 // This case handles long options which have no single-character mapping.
394 case 0: {
395 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700396 if (option_name == value_str) {
397 // |optarg| is an external variable set by getopt representing
398 // the option argument.
399 value = optarg;
400 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800401 RecordBootComplete();
402 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800403 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800404 } else if (option_name == factory_reset_str) {
405 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800406 } else {
407 LOG(ERROR) << "Invalid option: " << option_name;
408 }
409 break;
410 }
411
James Hawkinsabd73e62016-01-19 15:10:38 -0800412 case 'h': {
413 ShowHelp(argv[0]);
414 break;
415 }
416
417 case 'l': {
418 LogBootEvents();
419 break;
420 }
421
422 case 'p': {
423 PrintBootEvents();
424 break;
425 }
426
427 case 'r': {
428 // |optarg| is an external variable set by getopt representing
429 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700430 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800431 break;
432 }
433
434 default: {
435 DCHECK_EQ(opt, '?');
436
437 // |optopt| is an external variable set by getopt representing
438 // the value of the invalid option.
439 LOG(ERROR) << "Invalid option: " << optopt;
440 ShowHelp(argv[0]);
441 return EXIT_FAILURE;
442 }
443 }
444 }
445
James Hawkinsc6275582016-03-22 10:47:44 -0700446 if (!boot_event.empty()) {
447 RecordBootEventFromCommandLine(boot_event, value);
448 }
449
James Hawkinsabd73e62016-01-19 15:10:38 -0800450 return 0;
451}