blob: 71a5a3993e3f027fd7d2e2fe6219f87be7a7febe [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>
James Hawkins0660b302016-03-08 16:18:15 -080023#include <cmath>
James Hawkinsabd73e62016-01-19 15:10:38 -080024#include <cstddef>
25#include <cstdio>
James Hawkins500d7152016-02-16 15:05:54 -080026#include <ctime>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080027#include <map>
James Hawkinsabd73e62016-01-19 15:10:38 -080028#include <memory>
29#include <string>
James Hawkinseabe08b2016-01-19 16:54:35 -080030#include <android-base/logging.h>
James Hawkins4dded612016-07-28 11:50:23 -070031#include <android-base/parseint.h>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080032#include <cutils/properties.h>
James Hawkinsabd73e62016-01-19 15:10:38 -080033#include <log/log.h>
34#include "boot_event_record_store.h"
35#include "event_log_list_builder.h"
James Hawkins6f282992016-03-22 14:13:06 -070036#include "histogram_logger.h"
James Hawkinsc08e9962016-03-11 14:59:50 -080037#include "uptime_parser.h"
James Hawkinsabd73e62016-01-19 15:10:38 -080038
39namespace {
40
James Hawkinsabd73e62016-01-19 15:10:38 -080041// Scans the boot event record store for record files and logs each boot event
42// via EventLog.
43void LogBootEvents() {
44 BootEventRecordStore boot_event_store;
45
46 auto events = boot_event_store.GetAllBootEvents();
47 for (auto i = events.cbegin(); i != events.cend(); ++i) {
James Hawkins6f282992016-03-22 14:13:06 -070048 bootstat::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080049 }
50}
51
James Hawkinsc6275582016-03-22 10:47:44 -070052// Records the named boot |event| to the record store. If |value| is non-empty
53// and is a proper string representation of an integer value, the converted
54// integer value is associated with the boot event.
55void RecordBootEventFromCommandLine(
56 const std::string& event, const std::string& value_str) {
57 BootEventRecordStore boot_event_store;
58 if (!value_str.empty()) {
59 int32_t value = 0;
James Hawkins4dded612016-07-28 11:50:23 -070060 if (android::base::ParseInt(value_str.c_str(), &value)) {
61 boot_event_store.AddBootEventWithValue(event, value);
62 }
James Hawkinsc6275582016-03-22 10:47:44 -070063 } else {
64 boot_event_store.AddBootEvent(event);
65 }
66}
67
James Hawkinsabd73e62016-01-19 15:10:38 -080068void PrintBootEvents() {
69 printf("Boot events:\n");
70 printf("------------\n");
71
72 BootEventRecordStore boot_event_store;
73 auto events = boot_event_store.GetAllBootEvents();
74 for (auto i = events.cbegin(); i != events.cend(); ++i) {
75 printf("%s\t%d\n", i->first.c_str(), i->second);
76 }
77}
78
79void ShowHelp(const char *cmd) {
80 fprintf(stderr, "Usage: %s [options]\n", cmd);
81 fprintf(stderr,
82 "options include:\n"
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080083 " -h, --help Show this help\n"
84 " -l, --log Log all metrics to logstorage\n"
85 " -p, --print Dump the boot event records to the console\n"
86 " -r, --record Record the timestamp of a named boot event\n"
James Hawkinsc6275582016-03-22 10:47:44 -070087 " --value Optional value to associate with the boot event\n"
James Hawkins53684ea2016-02-23 16:18:19 -080088 " --record_boot_reason Record the reason why the device booted\n"
89 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080090}
91
92// Constructs a readable, printable string from the givencommand line
93// arguments.
94std::string GetCommandLine(int argc, char **argv) {
95 std::string cmd;
96 for (int i = 0; i < argc; ++i) {
97 cmd += argv[i];
98 cmd += " ";
99 }
100
101 return cmd;
102}
103
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800104// Convenience wrapper over the property API that returns an
105// std::string.
106std::string GetProperty(const char* key) {
107 std::vector<char> temp(PROPERTY_VALUE_MAX);
108 const int len = property_get(key, &temp[0], nullptr);
109 if (len < 0) {
110 return "";
111 }
112 return std::string(&temp[0], len);
113}
114
James Hawkins6f74c0b2016-02-12 15:49:16 -0800115constexpr int32_t kUnknownBootReason = 1;
116
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800117// A mapping from boot reason string, as read from the ro.boot.bootreason
118// system property, to a unique integer ID. Viewers of log data dashboards for
119// the boot_reason metric may refer to this mapping to discern the histogram
120// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800121const std::map<std::string, int32_t> kBootReasonMap = {
122 {"unknown", kUnknownBootReason},
123 {"normal", 2},
124 {"recovery", 3},
125 {"reboot", 4},
126 {"PowerKey", 5},
127 {"hard_reset", 6},
128 {"kernel_panic", 7},
129 {"rpm_err", 8},
130 {"hw_reset", 9},
131 {"tz_err", 10},
132 {"adsp_err", 11},
133 {"modem_err", 12},
134 {"mba_err", 13},
135 {"Watchdog", 14},
136 {"Panic", 15},
137 {"power_key", 16},
138 {"power_on", 17},
139 {"Reboot", 18},
140 {"rtc", 19},
141 {"edl", 20},
James Hawkins45ead352016-03-08 16:42:07 -0800142 {"oem_pon1", 21},
143 {"oem_powerkey", 22},
144 {"oem_unknown_reset", 23},
145 {"srto: HWWDT reset SC", 24},
146 {"srto: HWWDT reset platform", 25},
147 {"srto: bootloader", 26},
148 {"srto: kernel panic", 27},
149 {"srto: kernel watchdog reset", 28},
150 {"srto: normal", 29},
151 {"srto: reboot", 30},
152 {"srto: reboot-bootloader", 31},
153 {"srto: security watchdog reset", 32},
154 {"srto: wakesrc", 33},
155 {"srto: watchdog", 34},
156 {"srto:1-1", 35},
157 {"srto:omap_hsmm", 36},
158 {"srto:phy0", 37},
159 {"srto:rtc0", 38},
160 {"srto:touchpad", 39},
161 {"watchdog", 40},
162 {"watchdogr", 41},
163 {"wdog_bark", 42},
164 {"wdog_bite", 43},
165 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800166};
167
168// Converts a string value representing the reason the system booted to an
169// integer representation. This is necessary for logging the boot_reason metric
170// via Tron, which does not accept non-integer buckets in histograms.
171int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800172 auto mapping = kBootReasonMap.find(boot_reason);
173 if (mapping != kBootReasonMap.end()) {
174 return mapping->second;
175 }
176
177 LOG(INFO) << "Unknown boot reason: " << boot_reason;
178 return kUnknownBootReason;
179}
180
James Hawkinsb9cf7712016-04-08 15:32:19 -0700181// Returns the appropriate metric key prefix for the boot_complete metric such
182// that boot metrics after a system update are labeled as ota_boot_complete;
183// otherwise, they are labeled as boot_complete. This method encapsulates the
184// bookkeeping required to track when a system update has occurred by storing
185// the UTC timestamp of the system build date and comparing against the current
186// system build date.
187std::string CalculateBootCompletePrefix() {
188 static const std::string kBuildDateKey = "build_date";
189 std::string boot_complete_prefix = "boot_complete";
190
191 std::string build_date_str = GetProperty("ro.build.date.utc");
James Hawkins4dded612016-07-28 11:50:23 -0700192 int32_t build_date;
193 if (!android::base::ParseInt(build_date_str.c_str(), &build_date)) {
194 return std::string();
195 }
James Hawkinsb9cf7712016-04-08 15:32:19 -0700196
197 BootEventRecordStore boot_event_store;
198 BootEventRecordStore::BootEventRecord record;
199 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record) ||
200 build_date != record.second) {
201 boot_complete_prefix = "ota_" + boot_complete_prefix;
202 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
203 }
204
205 return boot_complete_prefix;
206}
207
James Hawkinsc08e9962016-03-11 14:59:50 -0800208// Records several metrics related to the time it takes to boot the device,
209// including disambiguating boot time on encrypted or non-encrypted devices.
210void RecordBootComplete() {
211 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700212 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -0700213
James Hawkinsc08e9962016-03-11 14:59:50 -0800214 time_t uptime = bootstat::ParseUptime();
James Hawkins2d8b3e62016-04-14 14:13:20 -0700215 time_t current_time_utc = time(nullptr);
216
217 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
218 time_t last_boot_time_utc = record.second;
219 time_t time_since_last_boot = difftime(current_time_utc,
220 last_boot_time_utc);
221 boot_event_store.AddBootEventWithValue("time_since_last_boot",
222 time_since_last_boot);
223 }
224
225 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -0800226
James Hawkinsb9cf7712016-04-08 15:32:19 -0700227 // The boot_complete metric has two variants: boot_complete and
228 // ota_boot_complete. The latter signifies that the device is booting after
229 // a system update.
230 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkins4dded612016-07-28 11:50:23 -0700231 if (boot_complete_prefix.empty()) {
232 // The system is hosed because the build date property could not be read.
233 return;
234 }
James Hawkinsc08e9962016-03-11 14:59:50 -0800235
236 // post_decrypt_time_elapsed is only logged on encrypted devices.
237 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
238 // Log the amount of time elapsed until the device is decrypted, which
239 // includes the variable amount of time the user takes to enter the
240 // decryption password.
241 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
242
243 // Subtract the decryption time to normalize the boot cycle timing.
244 time_t boot_complete = uptime - record.second;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700245 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinsc08e9962016-03-11 14:59:50 -0800246 boot_complete);
247
248
249 } else {
James Hawkinsb9cf7712016-04-08 15:32:19 -0700250 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
James Hawkinsc08e9962016-03-11 14:59:50 -0800251 uptime);
252 }
253
254 // Record the total time from device startup to boot complete, regardless of
255 // encryption state.
James Hawkinsb9cf7712016-04-08 15:32:19 -0700256 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime);
James Hawkinsc08e9962016-03-11 14:59:50 -0800257}
258
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800259// Records the boot_reason metric by querying the ro.boot.bootreason system
260// property.
261void RecordBootReason() {
262 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
263 BootEventRecordStore boot_event_store;
264 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
265}
266
James Hawkins500d7152016-02-16 15:05:54 -0800267// Records two metrics related to the user resetting a device: the time at
268// which the device is reset, and the time since the user last reset the
269// device. The former is only set once per-factory reset.
270void RecordFactoryReset() {
271 BootEventRecordStore boot_event_store;
272 BootEventRecordStore::BootEventRecord record;
273
274 time_t current_time_utc = time(nullptr);
275
James Hawkins0660b302016-03-08 16:18:15 -0800276 if (current_time_utc < 0) {
277 // UMA does not display negative values in buckets, so convert to positive.
James Hawkinsfff95ba2016-03-29 16:13:49 -0700278 bootstat::LogHistogram(
279 "factory_reset_current_time_failure", std::abs(current_time_utc));
280
281 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
282 // is losing records somehow.
283 boot_event_store.AddBootEventWithValue(
284 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800285 return;
286 } else {
James Hawkinsfff95ba2016-03-29 16:13:49 -0700287 bootstat::LogHistogram("factory_reset_current_time", current_time_utc);
288
289 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
290 // is losing records somehow.
291 boot_event_store.AddBootEventWithValue(
292 "factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800293 }
294
James Hawkins500d7152016-02-16 15:05:54 -0800295 // The factory_reset boot event does not exist after the device is reset, so
296 // use this signal to mark the time of the factory reset.
297 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
298 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800299
300 // Don't log the time_since_factory_reset until some time has elapsed.
301 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800302 return;
303 }
304
305 // Calculate and record the difference in time between now and the
306 // factory_reset time.
307 time_t factory_reset_utc = record.second;
James Hawkins6f282992016-03-22 14:13:06 -0700308 bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700309
310 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
311 // is losing records somehow.
312 boot_event_store.AddBootEventWithValue(
313 "factory_reset_record_value", factory_reset_utc);
314
James Hawkins500d7152016-02-16 15:05:54 -0800315 time_t time_since_factory_reset = difftime(current_time_utc,
316 factory_reset_utc);
317 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
318 time_since_factory_reset);
319}
320
James Hawkinsabd73e62016-01-19 15:10:38 -0800321} // namespace
322
323int main(int argc, char **argv) {
324 android::base::InitLogging(argv);
325
326 const std::string cmd_line = GetCommandLine(argc, argv);
327 LOG(INFO) << "Service started: " << cmd_line;
328
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800329 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700330 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800331 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800332 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800333 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800334 static const struct option long_options[] = {
335 { "help", no_argument, NULL, 'h' },
336 { "log", no_argument, NULL, 'l' },
337 { "print", no_argument, NULL, 'p' },
338 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700339 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800340 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800341 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800342 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800343 { NULL, 0, NULL, 0 }
344 };
345
James Hawkinsc6275582016-03-22 10:47:44 -0700346 std::string boot_event;
347 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800348 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800349 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800350 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800351 // This case handles long options which have no single-character mapping.
352 case 0: {
353 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700354 if (option_name == value_str) {
355 // |optarg| is an external variable set by getopt representing
356 // the option argument.
357 value = optarg;
358 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800359 RecordBootComplete();
360 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800361 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800362 } else if (option_name == factory_reset_str) {
363 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800364 } else {
365 LOG(ERROR) << "Invalid option: " << option_name;
366 }
367 break;
368 }
369
James Hawkinsabd73e62016-01-19 15:10:38 -0800370 case 'h': {
371 ShowHelp(argv[0]);
372 break;
373 }
374
375 case 'l': {
376 LogBootEvents();
377 break;
378 }
379
380 case 'p': {
381 PrintBootEvents();
382 break;
383 }
384
385 case 'r': {
386 // |optarg| is an external variable set by getopt representing
387 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700388 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800389 break;
390 }
391
392 default: {
393 DCHECK_EQ(opt, '?');
394
395 // |optopt| is an external variable set by getopt representing
396 // the value of the invalid option.
397 LOG(ERROR) << "Invalid option: " << optopt;
398 ShowHelp(argv[0]);
399 return EXIT_FAILURE;
400 }
401 }
402 }
403
James Hawkinsc6275582016-03-22 10:47:44 -0700404 if (!boot_event.empty()) {
405 RecordBootEventFromCommandLine(boot_event, value);
406 }
407
James Hawkinsabd73e62016-01-19 15:10:38 -0800408 return 0;
409}