blob: 0ab4c98d4b4d1abf66688c7803390930185a3ed1 [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"
Mark Salyzynff2dcd92016-09-28 15:54:45 -070038#include "event_log_list_builder.h" /* ToDo: switch to liblog implementation */
James Hawkins6f282992016-03-22 14:13:06 -070039#include "histogram_logger.h"
James Hawkinsc08e9962016-03-11 14:59:50 -080040#include "uptime_parser.h"
James Hawkinsabd73e62016-01-19 15:10:38 -080041
42namespace {
43
James Hawkinsabd73e62016-01-19 15:10:38 -080044// Scans the boot event record store for record files and logs each boot event
45// via EventLog.
46void LogBootEvents() {
47 BootEventRecordStore boot_event_store;
48
49 auto events = boot_event_store.GetAllBootEvents();
50 for (auto i = events.cbegin(); i != events.cend(); ++i) {
James Hawkins6f282992016-03-22 14:13:06 -070051 bootstat::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080052 }
53}
54
James Hawkinsc6275582016-03-22 10:47:44 -070055// Records the named boot |event| to the record store. If |value| is non-empty
56// and is a proper string representation of an integer value, the converted
57// integer value is associated with the boot event.
58void RecordBootEventFromCommandLine(
59 const std::string& event, const std::string& value_str) {
60 BootEventRecordStore boot_event_store;
61 if (!value_str.empty()) {
62 int32_t value = 0;
James Hawkins4dded612016-07-28 11:50:23 -070063 if (android::base::ParseInt(value_str.c_str(), &value)) {
64 boot_event_store.AddBootEventWithValue(event, value);
65 }
James Hawkinsc6275582016-03-22 10:47:44 -070066 } else {
67 boot_event_store.AddBootEvent(event);
68 }
69}
70
James Hawkinsabd73e62016-01-19 15:10:38 -080071void PrintBootEvents() {
72 printf("Boot events:\n");
73 printf("------------\n");
74
75 BootEventRecordStore boot_event_store;
76 auto events = boot_event_store.GetAllBootEvents();
77 for (auto i = events.cbegin(); i != events.cend(); ++i) {
78 printf("%s\t%d\n", i->first.c_str(), i->second);
79 }
80}
81
82void ShowHelp(const char *cmd) {
83 fprintf(stderr, "Usage: %s [options]\n", cmd);
84 fprintf(stderr,
85 "options include:\n"
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080086 " -h, --help Show this help\n"
87 " -l, --log Log all metrics to logstorage\n"
88 " -p, --print Dump the boot event records to the console\n"
89 " -r, --record Record the timestamp of a named boot event\n"
James Hawkinsc6275582016-03-22 10:47:44 -070090 " --value Optional value to associate with the boot event\n"
James Hawkins53684ea2016-02-23 16:18:19 -080091 " --record_boot_reason Record the reason why the device booted\n"
92 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080093}
94
95// Constructs a readable, printable string from the givencommand line
96// arguments.
97std::string GetCommandLine(int argc, char **argv) {
98 std::string cmd;
99 for (int i = 0; i < argc; ++i) {
100 cmd += argv[i];
101 cmd += " ";
102 }
103
104 return cmd;
105}
106
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800107// Convenience wrapper over the property API that returns an
108// std::string.
109std::string GetProperty(const char* key) {
110 std::vector<char> temp(PROPERTY_VALUE_MAX);
111 const int len = property_get(key, &temp[0], nullptr);
112 if (len < 0) {
113 return "";
114 }
115 return std::string(&temp[0], len);
116}
117
James Hawkins6f74c0b2016-02-12 15:49:16 -0800118constexpr int32_t kUnknownBootReason = 1;
119
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800120// A mapping from boot reason string, as read from the ro.boot.bootreason
121// system property, to a unique integer ID. Viewers of log data dashboards for
122// the boot_reason metric may refer to this mapping to discern the histogram
123// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800124const std::map<std::string, int32_t> kBootReasonMap = {
125 {"unknown", kUnknownBootReason},
126 {"normal", 2},
127 {"recovery", 3},
128 {"reboot", 4},
129 {"PowerKey", 5},
130 {"hard_reset", 6},
131 {"kernel_panic", 7},
132 {"rpm_err", 8},
133 {"hw_reset", 9},
134 {"tz_err", 10},
135 {"adsp_err", 11},
136 {"modem_err", 12},
137 {"mba_err", 13},
138 {"Watchdog", 14},
139 {"Panic", 15},
140 {"power_key", 16},
141 {"power_on", 17},
142 {"Reboot", 18},
143 {"rtc", 19},
144 {"edl", 20},
James Hawkins45ead352016-03-08 16:42:07 -0800145 {"oem_pon1", 21},
146 {"oem_powerkey", 22},
147 {"oem_unknown_reset", 23},
148 {"srto: HWWDT reset SC", 24},
149 {"srto: HWWDT reset platform", 25},
150 {"srto: bootloader", 26},
151 {"srto: kernel panic", 27},
152 {"srto: kernel watchdog reset", 28},
153 {"srto: normal", 29},
154 {"srto: reboot", 30},
155 {"srto: reboot-bootloader", 31},
156 {"srto: security watchdog reset", 32},
157 {"srto: wakesrc", 33},
158 {"srto: watchdog", 34},
159 {"srto:1-1", 35},
160 {"srto:omap_hsmm", 36},
161 {"srto:phy0", 37},
162 {"srto:rtc0", 38},
163 {"srto:touchpad", 39},
164 {"watchdog", 40},
165 {"watchdogr", 41},
166 {"wdog_bark", 42},
167 {"wdog_bite", 43},
168 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800169};
170
171// Converts a string value representing the reason the system booted to an
172// integer representation. This is necessary for logging the boot_reason metric
173// via Tron, which does not accept non-integer buckets in histograms.
174int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800175 auto mapping = kBootReasonMap.find(boot_reason);
176 if (mapping != kBootReasonMap.end()) {
177 return mapping->second;
178 }
179
180 LOG(INFO) << "Unknown boot reason: " << boot_reason;
181 return kUnknownBootReason;
182}
183
James Hawkinsb9cf7712016-04-08 15:32:19 -0700184// Returns the appropriate metric key prefix for the boot_complete metric such
185// that boot metrics after a system update are labeled as ota_boot_complete;
186// otherwise, they are labeled as boot_complete. This method encapsulates the
187// bookkeeping required to track when a system update has occurred by storing
188// the UTC timestamp of the system build date and comparing against the current
189// system build date.
190std::string CalculateBootCompletePrefix() {
191 static const std::string kBuildDateKey = "build_date";
192 std::string boot_complete_prefix = "boot_complete";
193
194 std::string build_date_str = GetProperty("ro.build.date.utc");
James Hawkins4dded612016-07-28 11:50:23 -0700195 int32_t build_date;
196 if (!android::base::ParseInt(build_date_str.c_str(), &build_date)) {
197 return std::string();
198 }
James Hawkinsb9cf7712016-04-08 15:32:19 -0700199
200 BootEventRecordStore boot_event_store;
201 BootEventRecordStore::BootEventRecord record;
202 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record) ||
203 build_date != record.second) {
204 boot_complete_prefix = "ota_" + boot_complete_prefix;
205 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
206 }
207
208 return boot_complete_prefix;
209}
210
James Hawkinsc08e9962016-03-11 14:59:50 -0800211// Records several metrics related to the time it takes to boot the device,
212// including disambiguating boot time on encrypted or non-encrypted devices.
213void RecordBootComplete() {
214 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700215 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -0700216
James Hawkinsc08e9962016-03-11 14:59:50 -0800217 time_t uptime = bootstat::ParseUptime();
James Hawkins2d8b3e62016-04-14 14:13:20 -0700218 time_t current_time_utc = time(nullptr);
219
220 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
221 time_t last_boot_time_utc = record.second;
222 time_t time_since_last_boot = difftime(current_time_utc,
223 last_boot_time_utc);
224 boot_event_store.AddBootEventWithValue("time_since_last_boot",
225 time_since_last_boot);
226 }
227
228 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -0800229
James Hawkinsb9cf7712016-04-08 15:32:19 -0700230 // The boot_complete metric has two variants: boot_complete and
231 // ota_boot_complete. The latter signifies that the device is booting after
232 // a system update.
233 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkins4dded612016-07-28 11:50:23 -0700234 if (boot_complete_prefix.empty()) {
235 // The system is hosed because the build date property could not be read.
236 return;
237 }
James Hawkinsc08e9962016-03-11 14:59:50 -0800238
239 // post_decrypt_time_elapsed is only logged on encrypted devices.
240 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
241 // Log the amount of time elapsed until the device is decrypted, which
242 // includes the variable amount of time the user takes to enter the
243 // decryption password.
244 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
245
246 // Subtract the decryption time to normalize the boot cycle timing.
247 time_t boot_complete = uptime - record.second;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700248 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinsc08e9962016-03-11 14:59:50 -0800249 boot_complete);
250
251
252 } else {
James Hawkinsb9cf7712016-04-08 15:32:19 -0700253 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
James Hawkinsc08e9962016-03-11 14:59:50 -0800254 uptime);
255 }
256
257 // Record the total time from device startup to boot complete, regardless of
258 // encryption state.
James Hawkinsb9cf7712016-04-08 15:32:19 -0700259 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime);
James Hawkinsc08e9962016-03-11 14:59:50 -0800260}
261
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800262// Records the boot_reason metric by querying the ro.boot.bootreason system
263// property.
264void RecordBootReason() {
265 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
266 BootEventRecordStore boot_event_store;
267 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
268}
269
James Hawkins500d7152016-02-16 15:05:54 -0800270// Records two metrics related to the user resetting a device: the time at
271// which the device is reset, and the time since the user last reset the
272// device. The former is only set once per-factory reset.
273void RecordFactoryReset() {
274 BootEventRecordStore boot_event_store;
275 BootEventRecordStore::BootEventRecord record;
276
277 time_t current_time_utc = time(nullptr);
278
James Hawkins0660b302016-03-08 16:18:15 -0800279 if (current_time_utc < 0) {
280 // UMA does not display negative values in buckets, so convert to positive.
James Hawkinsfff95ba2016-03-29 16:13:49 -0700281 bootstat::LogHistogram(
282 "factory_reset_current_time_failure", std::abs(current_time_utc));
283
284 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
285 // is losing records somehow.
286 boot_event_store.AddBootEventWithValue(
287 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800288 return;
289 } else {
James Hawkinsfff95ba2016-03-29 16:13:49 -0700290 bootstat::LogHistogram("factory_reset_current_time", current_time_utc);
291
292 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
293 // is losing records somehow.
294 boot_event_store.AddBootEventWithValue(
295 "factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800296 }
297
James Hawkins500d7152016-02-16 15:05:54 -0800298 // The factory_reset boot event does not exist after the device is reset, so
299 // use this signal to mark the time of the factory reset.
300 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
301 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800302
303 // Don't log the time_since_factory_reset until some time has elapsed.
304 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800305 return;
306 }
307
308 // Calculate and record the difference in time between now and the
309 // factory_reset time.
310 time_t factory_reset_utc = record.second;
James Hawkins6f282992016-03-22 14:13:06 -0700311 bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700312
313 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
314 // is losing records somehow.
315 boot_event_store.AddBootEventWithValue(
316 "factory_reset_record_value", factory_reset_utc);
317
James Hawkins500d7152016-02-16 15:05:54 -0800318 time_t time_since_factory_reset = difftime(current_time_utc,
319 factory_reset_utc);
320 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
321 time_since_factory_reset);
322}
323
James Hawkinsabd73e62016-01-19 15:10:38 -0800324} // namespace
325
326int main(int argc, char **argv) {
327 android::base::InitLogging(argv);
328
329 const std::string cmd_line = GetCommandLine(argc, argv);
330 LOG(INFO) << "Service started: " << cmd_line;
331
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800332 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700333 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800334 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800335 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800336 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800337 static const struct option long_options[] = {
338 { "help", no_argument, NULL, 'h' },
339 { "log", no_argument, NULL, 'l' },
340 { "print", no_argument, NULL, 'p' },
341 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700342 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800343 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800344 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800345 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800346 { NULL, 0, NULL, 0 }
347 };
348
James Hawkinsc6275582016-03-22 10:47:44 -0700349 std::string boot_event;
350 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800351 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800352 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800353 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800354 // This case handles long options which have no single-character mapping.
355 case 0: {
356 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700357 if (option_name == value_str) {
358 // |optarg| is an external variable set by getopt representing
359 // the option argument.
360 value = optarg;
361 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800362 RecordBootComplete();
363 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800364 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800365 } else if (option_name == factory_reset_str) {
366 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800367 } else {
368 LOG(ERROR) << "Invalid option: " << option_name;
369 }
370 break;
371 }
372
James Hawkinsabd73e62016-01-19 15:10:38 -0800373 case 'h': {
374 ShowHelp(argv[0]);
375 break;
376 }
377
378 case 'l': {
379 LogBootEvents();
380 break;
381 }
382
383 case 'p': {
384 PrintBootEvents();
385 break;
386 }
387
388 case 'r': {
389 // |optarg| is an external variable set by getopt representing
390 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700391 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800392 break;
393 }
394
395 default: {
396 DCHECK_EQ(opt, '?');
397
398 // |optopt| is an external variable set by getopt representing
399 // the value of the invalid option.
400 LOG(ERROR) << "Invalid option: " << optopt;
401 ShowHelp(argv[0]);
402 return EXIT_FAILURE;
403 }
404 }
405 }
406
James Hawkinsc6275582016-03-22 10:47:44 -0700407 if (!boot_event.empty()) {
408 RecordBootEventFromCommandLine(boot_event, value);
409 }
410
James Hawkinsabd73e62016-01-19 15:10:38 -0800411 return 0;
412}