blob: 7c0e85dc2fd61325e905cbdbab3d7d3a30c201b4 [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>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070036
James Hawkinsabd73e62016-01-19 15:10:38 -080037#include "boot_event_record_store.h"
James Hawkins6f282992016-03-22 14:13:06 -070038#include "histogram_logger.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 Hawkins6f282992016-03-22 14:13:06 -070050 bootstat::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 Hawkinsc08e9962016-03-11 14:59:50 -0800210// Records several metrics related to the time it takes to boot the device,
211// including disambiguating boot time on encrypted or non-encrypted devices.
212void RecordBootComplete() {
213 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700214 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -0700215
James Hawkinsc08e9962016-03-11 14:59:50 -0800216 time_t uptime = bootstat::ParseUptime();
James Hawkins2d8b3e62016-04-14 14:13:20 -0700217 time_t current_time_utc = time(nullptr);
218
219 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
220 time_t last_boot_time_utc = record.second;
221 time_t time_since_last_boot = difftime(current_time_utc,
222 last_boot_time_utc);
223 boot_event_store.AddBootEventWithValue("time_since_last_boot",
224 time_since_last_boot);
225 }
226
227 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -0800228
James Hawkinsb9cf7712016-04-08 15:32:19 -0700229 // The boot_complete metric has two variants: boot_complete and
230 // ota_boot_complete. The latter signifies that the device is booting after
231 // a system update.
232 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkins4dded612016-07-28 11:50:23 -0700233 if (boot_complete_prefix.empty()) {
234 // The system is hosed because the build date property could not be read.
235 return;
236 }
James Hawkinsc08e9962016-03-11 14:59:50 -0800237
238 // post_decrypt_time_elapsed is only logged on encrypted devices.
239 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
240 // Log the amount of time elapsed until the device is decrypted, which
241 // includes the variable amount of time the user takes to enter the
242 // decryption password.
243 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
244
245 // Subtract the decryption time to normalize the boot cycle timing.
246 time_t boot_complete = uptime - record.second;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700247 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinsc08e9962016-03-11 14:59:50 -0800248 boot_complete);
249
250
251 } else {
James Hawkinsb9cf7712016-04-08 15:32:19 -0700252 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
James Hawkinsc08e9962016-03-11 14:59:50 -0800253 uptime);
254 }
255
256 // Record the total time from device startup to boot complete, regardless of
257 // encryption state.
James Hawkinsb9cf7712016-04-08 15:32:19 -0700258 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime);
James Hawkinsc08e9962016-03-11 14:59:50 -0800259}
260
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800261// Records the boot_reason metric by querying the ro.boot.bootreason system
262// property.
263void RecordBootReason() {
264 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
265 BootEventRecordStore boot_event_store;
266 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
267}
268
James Hawkins500d7152016-02-16 15:05:54 -0800269// Records two metrics related to the user resetting a device: the time at
270// which the device is reset, and the time since the user last reset the
271// device. The former is only set once per-factory reset.
272void RecordFactoryReset() {
273 BootEventRecordStore boot_event_store;
274 BootEventRecordStore::BootEventRecord record;
275
276 time_t current_time_utc = time(nullptr);
277
James Hawkins0660b302016-03-08 16:18:15 -0800278 if (current_time_utc < 0) {
279 // UMA does not display negative values in buckets, so convert to positive.
James Hawkinsfff95ba2016-03-29 16:13:49 -0700280 bootstat::LogHistogram(
281 "factory_reset_current_time_failure", std::abs(current_time_utc));
282
283 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
284 // is losing records somehow.
285 boot_event_store.AddBootEventWithValue(
286 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800287 return;
288 } else {
James Hawkinsfff95ba2016-03-29 16:13:49 -0700289 bootstat::LogHistogram("factory_reset_current_time", current_time_utc);
290
291 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
292 // is losing records somehow.
293 boot_event_store.AddBootEventWithValue(
294 "factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800295 }
296
James Hawkins500d7152016-02-16 15:05:54 -0800297 // The factory_reset boot event does not exist after the device is reset, so
298 // use this signal to mark the time of the factory reset.
299 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
300 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800301
302 // Don't log the time_since_factory_reset until some time has elapsed.
303 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800304 return;
305 }
306
307 // Calculate and record the difference in time between now and the
308 // factory_reset time.
309 time_t factory_reset_utc = record.second;
James Hawkins6f282992016-03-22 14:13:06 -0700310 bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700311
312 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
313 // is losing records somehow.
314 boot_event_store.AddBootEventWithValue(
315 "factory_reset_record_value", factory_reset_utc);
316
James Hawkins500d7152016-02-16 15:05:54 -0800317 time_t time_since_factory_reset = difftime(current_time_utc,
318 factory_reset_utc);
319 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
320 time_since_factory_reset);
321}
322
James Hawkinsabd73e62016-01-19 15:10:38 -0800323} // namespace
324
325int main(int argc, char **argv) {
326 android::base::InitLogging(argv);
327
328 const std::string cmd_line = GetCommandLine(argc, argv);
329 LOG(INFO) << "Service started: " << cmd_line;
330
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800331 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700332 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800333 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800334 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800335 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800336 static const struct option long_options[] = {
337 { "help", no_argument, NULL, 'h' },
338 { "log", no_argument, NULL, 'l' },
339 { "print", no_argument, NULL, 'p' },
340 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700341 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800342 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800343 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800344 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800345 { NULL, 0, NULL, 0 }
346 };
347
James Hawkinsc6275582016-03-22 10:47:44 -0700348 std::string boot_event;
349 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800350 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800351 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800352 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800353 // This case handles long options which have no single-character mapping.
354 case 0: {
355 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700356 if (option_name == value_str) {
357 // |optarg| is an external variable set by getopt representing
358 // the option argument.
359 value = optarg;
360 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800361 RecordBootComplete();
362 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800363 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800364 } else if (option_name == factory_reset_str) {
365 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800366 } else {
367 LOG(ERROR) << "Invalid option: " << option_name;
368 }
369 break;
370 }
371
James Hawkinsabd73e62016-01-19 15:10:38 -0800372 case 'h': {
373 ShowHelp(argv[0]);
374 break;
375 }
376
377 case 'l': {
378 LogBootEvents();
379 break;
380 }
381
382 case 'p': {
383 PrintBootEvents();
384 break;
385 }
386
387 case 'r': {
388 // |optarg| is an external variable set by getopt representing
389 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700390 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800391 break;
392 }
393
394 default: {
395 DCHECK_EQ(opt, '?');
396
397 // |optopt| is an external variable set by getopt representing
398 // the value of the invalid option.
399 LOG(ERROR) << "Invalid option: " << optopt;
400 ShowHelp(argv[0]);
401 return EXIT_FAILURE;
402 }
403 }
404 }
405
James Hawkinsc6275582016-03-22 10:47:44 -0700406 if (!boot_event.empty()) {
407 RecordBootEventFromCommandLine(boot_event, value);
408 }
409
James Hawkinsabd73e62016-01-19 15:10:38 -0800410 return 0;
411}