blob: 322add3274aa173b1a11b9ef1b90fe4a42edc41e [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 Hawkins26f40c02017-02-06 10:46:54 -080024#include <chrono>
James Hawkins0660b302016-03-08 16:18:15 -080025#include <cmath>
James Hawkinsabd73e62016-01-19 15:10:38 -080026#include <cstddef>
27#include <cstdio>
James Hawkins500d7152016-02-16 15:05:54 -080028#include <ctime>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080029#include <map>
James Hawkinsabd73e62016-01-19 15:10:38 -080030#include <memory>
31#include <string>
James Hawkinsbe46fd12017-02-02 16:21:25 -080032#include <vector>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070033
34#include <android/log.h>
James Hawkins26f40c02017-02-06 10:46:54 -080035#include <android-base/chrono_utils.h>
James Hawkinseabe08b2016-01-19 16:54:35 -080036#include <android-base/logging.h>
James Hawkins4dded612016-07-28 11:50:23 -070037#include <android-base/parseint.h>
James Hawkinsbe46fd12017-02-02 16:21:25 -080038#include <android-base/strings.h>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080039#include <cutils/properties.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070040
James Hawkinsabd73e62016-01-19 15:10:38 -080041#include "boot_event_record_store.h"
James Hawkins6f282992016-03-22 14:13:06 -070042#include "histogram_logger.h"
James Hawkinsabd73e62016-01-19 15:10:38 -080043
44namespace {
45
James Hawkinsabd73e62016-01-19 15:10:38 -080046// Scans the boot event record store for record files and logs each boot event
47// via EventLog.
48void LogBootEvents() {
49 BootEventRecordStore boot_event_store;
50
51 auto events = boot_event_store.GetAllBootEvents();
52 for (auto i = events.cbegin(); i != events.cend(); ++i) {
James Hawkins6f282992016-03-22 14:13:06 -070053 bootstat::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080054 }
55}
56
James Hawkinsc6275582016-03-22 10:47:44 -070057// Records the named boot |event| to the record store. If |value| is non-empty
58// and is a proper string representation of an integer value, the converted
59// integer value is associated with the boot event.
60void RecordBootEventFromCommandLine(
61 const std::string& event, const std::string& value_str) {
62 BootEventRecordStore boot_event_store;
63 if (!value_str.empty()) {
64 int32_t value = 0;
Elliott Hughesda46b392016-10-11 17:09:00 -070065 if (android::base::ParseInt(value_str, &value)) {
James Hawkins4dded612016-07-28 11:50:23 -070066 boot_event_store.AddBootEventWithValue(event, value);
67 }
James Hawkinsc6275582016-03-22 10:47:44 -070068 } else {
69 boot_event_store.AddBootEvent(event);
70 }
71}
72
James Hawkinsabd73e62016-01-19 15:10:38 -080073void PrintBootEvents() {
74 printf("Boot events:\n");
75 printf("------------\n");
76
77 BootEventRecordStore boot_event_store;
78 auto events = boot_event_store.GetAllBootEvents();
79 for (auto i = events.cbegin(); i != events.cend(); ++i) {
80 printf("%s\t%d\n", i->first.c_str(), i->second);
81 }
82}
83
84void ShowHelp(const char *cmd) {
85 fprintf(stderr, "Usage: %s [options]\n", cmd);
86 fprintf(stderr,
87 "options include:\n"
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080088 " -h, --help Show this help\n"
89 " -l, --log Log all metrics to logstorage\n"
90 " -p, --print Dump the boot event records to the console\n"
91 " -r, --record Record the timestamp of a named boot event\n"
James Hawkinsc6275582016-03-22 10:47:44 -070092 " --value Optional value to associate with the boot event\n"
James Hawkins53684ea2016-02-23 16:18:19 -080093 " --record_boot_reason Record the reason why the device booted\n"
94 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080095}
96
97// Constructs a readable, printable string from the givencommand line
98// arguments.
99std::string GetCommandLine(int argc, char **argv) {
100 std::string cmd;
101 for (int i = 0; i < argc; ++i) {
102 cmd += argv[i];
103 cmd += " ";
104 }
105
106 return cmd;
107}
108
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800109// Convenience wrapper over the property API that returns an
110// std::string.
111std::string GetProperty(const char* key) {
112 std::vector<char> temp(PROPERTY_VALUE_MAX);
113 const int len = property_get(key, &temp[0], nullptr);
114 if (len < 0) {
115 return "";
116 }
117 return std::string(&temp[0], len);
118}
119
James Hawkins6f74c0b2016-02-12 15:49:16 -0800120constexpr int32_t kUnknownBootReason = 1;
121
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800122// A mapping from boot reason string, as read from the ro.boot.bootreason
123// system property, to a unique integer ID. Viewers of log data dashboards for
124// the boot_reason metric may refer to this mapping to discern the histogram
125// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800126const std::map<std::string, int32_t> kBootReasonMap = {
127 {"unknown", kUnknownBootReason},
128 {"normal", 2},
129 {"recovery", 3},
130 {"reboot", 4},
131 {"PowerKey", 5},
132 {"hard_reset", 6},
133 {"kernel_panic", 7},
134 {"rpm_err", 8},
135 {"hw_reset", 9},
136 {"tz_err", 10},
137 {"adsp_err", 11},
138 {"modem_err", 12},
139 {"mba_err", 13},
140 {"Watchdog", 14},
141 {"Panic", 15},
142 {"power_key", 16},
143 {"power_on", 17},
144 {"Reboot", 18},
145 {"rtc", 19},
146 {"edl", 20},
James Hawkins45ead352016-03-08 16:42:07 -0800147 {"oem_pon1", 21},
148 {"oem_powerkey", 22},
149 {"oem_unknown_reset", 23},
150 {"srto: HWWDT reset SC", 24},
151 {"srto: HWWDT reset platform", 25},
152 {"srto: bootloader", 26},
153 {"srto: kernel panic", 27},
154 {"srto: kernel watchdog reset", 28},
155 {"srto: normal", 29},
156 {"srto: reboot", 30},
157 {"srto: reboot-bootloader", 31},
158 {"srto: security watchdog reset", 32},
159 {"srto: wakesrc", 33},
160 {"srto: watchdog", 34},
161 {"srto:1-1", 35},
162 {"srto:omap_hsmm", 36},
163 {"srto:phy0", 37},
164 {"srto:rtc0", 38},
165 {"srto:touchpad", 39},
166 {"watchdog", 40},
167 {"watchdogr", 41},
168 {"wdog_bark", 42},
169 {"wdog_bite", 43},
170 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800171};
172
173// Converts a string value representing the reason the system booted to an
174// integer representation. This is necessary for logging the boot_reason metric
175// via Tron, which does not accept non-integer buckets in histograms.
176int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800177 auto mapping = kBootReasonMap.find(boot_reason);
178 if (mapping != kBootReasonMap.end()) {
179 return mapping->second;
180 }
181
182 LOG(INFO) << "Unknown boot reason: " << boot_reason;
183 return kUnknownBootReason;
184}
185
James Hawkinsb9cf7712016-04-08 15:32:19 -0700186// Returns the appropriate metric key prefix for the boot_complete metric such
187// that boot metrics after a system update are labeled as ota_boot_complete;
188// otherwise, they are labeled as boot_complete. This method encapsulates the
189// bookkeeping required to track when a system update has occurred by storing
190// the UTC timestamp of the system build date and comparing against the current
191// system build date.
192std::string CalculateBootCompletePrefix() {
193 static const std::string kBuildDateKey = "build_date";
194 std::string boot_complete_prefix = "boot_complete";
195
196 std::string build_date_str = GetProperty("ro.build.date.utc");
James Hawkins4dded612016-07-28 11:50:23 -0700197 int32_t build_date;
Elliott Hughesda46b392016-10-11 17:09:00 -0700198 if (!android::base::ParseInt(build_date_str, &build_date)) {
James Hawkins4dded612016-07-28 11:50:23 -0700199 return std::string();
200 }
James Hawkinsb9cf7712016-04-08 15:32:19 -0700201
202 BootEventRecordStore boot_event_store;
203 BootEventRecordStore::BootEventRecord record;
204 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record) ||
205 build_date != record.second) {
206 boot_complete_prefix = "ota_" + boot_complete_prefix;
207 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
208 }
209
210 return boot_complete_prefix;
211}
212
James Hawkinsef0a0902017-01-06 14:38:23 -0800213// Records the value of a given ro.boottime.init property in milliseconds.
214void RecordInitBootTimeProp(
215 BootEventRecordStore* boot_event_store, const char* property) {
216 std::string value = GetProperty(property);
217
James Hawkins27c05222017-01-26 11:55:44 -0800218 int32_t time_in_ms;
219 if (android::base::ParseInt(value, &time_in_ms)) {
James Hawkinsef0a0902017-01-06 14:38:23 -0800220 boot_event_store->AddBootEventWithValue(property, time_in_ms);
221 }
222}
223
James Hawkinsbe46fd12017-02-02 16:21:25 -0800224// Parses and records the set of bootloader stages and associated boot times
225// from the ro.boot.boottime system property.
226void RecordBootloaderTimings(BootEventRecordStore* boot_event_store) {
227 // |ro.boot.boottime| is of the form 'stage1:time1,...,stageN:timeN'.
228 std::string value = GetProperty("ro.boot.boottime");
229
230 auto stages = android::base::Split(value, ",");
231 for (auto const &stageTiming : stages) {
232 // |stageTiming| is of the form 'stage:time'.
233 auto stageTimingValues = android::base::Split(stageTiming, ":");
234 DCHECK_EQ(2, stageTimingValues.size());
235
236 std::string stageName = stageTimingValues[0];
237 int32_t time_ms;
238 if (android::base::ParseInt(stageTimingValues[1], &time_ms)) {
239 boot_event_store->AddBootEventWithValue(
240 "boottime.bootloader." + stageName, time_ms);
241 }
242 }
243}
244
James Hawkinsc08e9962016-03-11 14:59:50 -0800245// Records several metrics related to the time it takes to boot the device,
246// including disambiguating boot time on encrypted or non-encrypted devices.
247void RecordBootComplete() {
248 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700249 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -0700250
James Hawkins26f40c02017-02-06 10:46:54 -0800251 auto uptime = std::chrono::duration_cast<std::chrono::seconds>(
252 android::base::boot_clock::now().time_since_epoch());
James Hawkins2d8b3e62016-04-14 14:13:20 -0700253 time_t current_time_utc = time(nullptr);
254
255 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
256 time_t last_boot_time_utc = record.second;
257 time_t time_since_last_boot = difftime(current_time_utc,
258 last_boot_time_utc);
259 boot_event_store.AddBootEventWithValue("time_since_last_boot",
260 time_since_last_boot);
261 }
262
263 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -0800264
James Hawkinsb9cf7712016-04-08 15:32:19 -0700265 // The boot_complete metric has two variants: boot_complete and
266 // ota_boot_complete. The latter signifies that the device is booting after
267 // a system update.
268 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkins4dded612016-07-28 11:50:23 -0700269 if (boot_complete_prefix.empty()) {
270 // The system is hosed because the build date property could not be read.
271 return;
272 }
James Hawkinsc08e9962016-03-11 14:59:50 -0800273
274 // post_decrypt_time_elapsed is only logged on encrypted devices.
275 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
276 // Log the amount of time elapsed until the device is decrypted, which
277 // includes the variable amount of time the user takes to enter the
278 // decryption password.
James Hawkins26f40c02017-02-06 10:46:54 -0800279 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800280
281 // Subtract the decryption time to normalize the boot cycle timing.
James Hawkins26f40c02017-02-06 10:46:54 -0800282 std::chrono::seconds boot_complete = std::chrono::seconds(uptime.count() - record.second);
James Hawkinsb9cf7712016-04-08 15:32:19 -0700283 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkins26f40c02017-02-06 10:46:54 -0800284 boot_complete.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800285
286
287 } else {
James Hawkinsb9cf7712016-04-08 15:32:19 -0700288 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
James Hawkins26f40c02017-02-06 10:46:54 -0800289 uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800290 }
291
292 // Record the total time from device startup to boot complete, regardless of
293 // encryption state.
James Hawkins26f40c02017-02-06 10:46:54 -0800294 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime.count());
James Hawkinsef0a0902017-01-06 14:38:23 -0800295
296 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init");
297 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux");
298 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait");
James Hawkinsbe46fd12017-02-02 16:21:25 -0800299
300 RecordBootloaderTimings(&boot_event_store);
James Hawkinsc08e9962016-03-11 14:59:50 -0800301}
302
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800303// Records the boot_reason metric by querying the ro.boot.bootreason system
304// property.
305void RecordBootReason() {
306 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
307 BootEventRecordStore boot_event_store;
308 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
309}
310
James Hawkins500d7152016-02-16 15:05:54 -0800311// Records two metrics related to the user resetting a device: the time at
312// which the device is reset, and the time since the user last reset the
313// device. The former is only set once per-factory reset.
314void RecordFactoryReset() {
315 BootEventRecordStore boot_event_store;
316 BootEventRecordStore::BootEventRecord record;
317
318 time_t current_time_utc = time(nullptr);
319
James Hawkins0660b302016-03-08 16:18:15 -0800320 if (current_time_utc < 0) {
321 // UMA does not display negative values in buckets, so convert to positive.
James Hawkinsfff95ba2016-03-29 16:13:49 -0700322 bootstat::LogHistogram(
323 "factory_reset_current_time_failure", std::abs(current_time_utc));
324
325 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
326 // is losing records somehow.
327 boot_event_store.AddBootEventWithValue(
328 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800329 return;
330 } else {
James Hawkinsfff95ba2016-03-29 16:13:49 -0700331 bootstat::LogHistogram("factory_reset_current_time", current_time_utc);
332
333 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
334 // is losing records somehow.
335 boot_event_store.AddBootEventWithValue(
336 "factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800337 }
338
James Hawkins500d7152016-02-16 15:05:54 -0800339 // The factory_reset boot event does not exist after the device is reset, so
340 // use this signal to mark the time of the factory reset.
341 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
342 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800343
344 // Don't log the time_since_factory_reset until some time has elapsed.
345 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800346 return;
347 }
348
349 // Calculate and record the difference in time between now and the
350 // factory_reset time.
351 time_t factory_reset_utc = record.second;
James Hawkins6f282992016-03-22 14:13:06 -0700352 bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700353
354 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
355 // is losing records somehow.
356 boot_event_store.AddBootEventWithValue(
357 "factory_reset_record_value", factory_reset_utc);
358
James Hawkins500d7152016-02-16 15:05:54 -0800359 time_t time_since_factory_reset = difftime(current_time_utc,
360 factory_reset_utc);
361 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
362 time_since_factory_reset);
363}
364
James Hawkinsabd73e62016-01-19 15:10:38 -0800365} // namespace
366
367int main(int argc, char **argv) {
368 android::base::InitLogging(argv);
369
370 const std::string cmd_line = GetCommandLine(argc, argv);
371 LOG(INFO) << "Service started: " << cmd_line;
372
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800373 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700374 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800375 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800376 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800377 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800378 static const struct option long_options[] = {
379 { "help", no_argument, NULL, 'h' },
380 { "log", no_argument, NULL, 'l' },
381 { "print", no_argument, NULL, 'p' },
382 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700383 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800384 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800385 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800386 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800387 { NULL, 0, NULL, 0 }
388 };
389
James Hawkinsc6275582016-03-22 10:47:44 -0700390 std::string boot_event;
391 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800392 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800393 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800394 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800395 // This case handles long options which have no single-character mapping.
396 case 0: {
397 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700398 if (option_name == value_str) {
399 // |optarg| is an external variable set by getopt representing
400 // the option argument.
401 value = optarg;
402 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800403 RecordBootComplete();
404 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800405 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800406 } else if (option_name == factory_reset_str) {
407 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800408 } else {
409 LOG(ERROR) << "Invalid option: " << option_name;
410 }
411 break;
412 }
413
James Hawkinsabd73e62016-01-19 15:10:38 -0800414 case 'h': {
415 ShowHelp(argv[0]);
416 break;
417 }
418
419 case 'l': {
420 LogBootEvents();
421 break;
422 }
423
424 case 'p': {
425 PrintBootEvents();
426 break;
427 }
428
429 case 'r': {
430 // |optarg| is an external variable set by getopt representing
431 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700432 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800433 break;
434 }
435
436 default: {
437 DCHECK_EQ(opt, '?');
438
439 // |optopt| is an external variable set by getopt representing
440 // the value of the invalid option.
441 LOG(ERROR) << "Invalid option: " << optopt;
442 ShowHelp(argv[0]);
443 return EXIT_FAILURE;
444 }
445 }
446 }
447
James Hawkinsc6275582016-03-22 10:47:44 -0700448 if (!boot_event.empty()) {
449 RecordBootEventFromCommandLine(boot_event, value);
450 }
451
James Hawkinsabd73e62016-01-19 15:10:38 -0800452 return 0;
453}