blob: 26e0ffc5346ff44efde3667da4bcd05c232bc4cb [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 Hawkins10f54be2016-02-09 15:32:38 -080021#include <getopt.h>
James Hawkinsabd73e62016-01-19 15:10:38 -080022#include <unistd.h>
James Hawkins6b930bf2016-03-08 16:18:15 -080023#include <cmath>
James Hawkinsabd73e62016-01-19 15:10:38 -080024#include <cstddef>
25#include <cstdio>
James Hawkins35349142016-02-16 15:05:54 -080026#include <ctime>
James Hawkins10f54be2016-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 Hawkinsf85554e2016-07-28 11:50:23 -070031#include <android-base/parseint.h>
James Hawkins10f54be2016-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 Hawkinse8e8cf32016-03-22 14:13:06 -070036#include "histogram_logger.h"
James Hawkinseef069a2016-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 Hawkinse8e8cf32016-03-22 14:13:06 -070048 bootstat::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080049 }
50}
51
52void PrintBootEvents() {
53 printf("Boot events:\n");
54 printf("------------\n");
55
56 BootEventRecordStore boot_event_store;
57 auto events = boot_event_store.GetAllBootEvents();
58 for (auto i = events.cbegin(); i != events.cend(); ++i) {
59 printf("%s\t%d\n", i->first.c_str(), i->second);
60 }
61}
62
63void ShowHelp(const char *cmd) {
64 fprintf(stderr, "Usage: %s [options]\n", cmd);
65 fprintf(stderr,
66 "options include:\n"
James Hawkins10f54be2016-02-09 15:32:38 -080067 " -h, --help Show this help\n"
68 " -l, --log Log all metrics to logstorage\n"
69 " -p, --print Dump the boot event records to the console\n"
70 " -r, --record Record the timestamp of a named boot event\n"
James Hawkinsc0188da2016-02-23 16:18:19 -080071 " --record_boot_reason Record the reason why the device booted\n"
72 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080073}
74
75// Constructs a readable, printable string from the givencommand line
76// arguments.
77std::string GetCommandLine(int argc, char **argv) {
78 std::string cmd;
79 for (int i = 0; i < argc; ++i) {
80 cmd += argv[i];
81 cmd += " ";
82 }
83
84 return cmd;
85}
86
James Hawkins10f54be2016-02-09 15:32:38 -080087// Convenience wrapper over the property API that returns an
88// std::string.
89std::string GetProperty(const char* key) {
90 std::vector<char> temp(PROPERTY_VALUE_MAX);
91 const int len = property_get(key, &temp[0], nullptr);
92 if (len < 0) {
93 return "";
94 }
95 return std::string(&temp[0], len);
96}
97
James Hawkinsee79e732016-02-12 15:49:16 -080098constexpr int32_t kUnknownBootReason = 1;
99
James Hawkins10f54be2016-02-09 15:32:38 -0800100// A mapping from boot reason string, as read from the ro.boot.bootreason
101// system property, to a unique integer ID. Viewers of log data dashboards for
102// the boot_reason metric may refer to this mapping to discern the histogram
103// values.
James Hawkinsee79e732016-02-12 15:49:16 -0800104const std::map<std::string, int32_t> kBootReasonMap = {
105 {"unknown", kUnknownBootReason},
106 {"normal", 2},
107 {"recovery", 3},
108 {"reboot", 4},
109 {"PowerKey", 5},
110 {"hard_reset", 6},
111 {"kernel_panic", 7},
112 {"rpm_err", 8},
113 {"hw_reset", 9},
114 {"tz_err", 10},
115 {"adsp_err", 11},
116 {"modem_err", 12},
117 {"mba_err", 13},
118 {"Watchdog", 14},
119 {"Panic", 15},
120 {"power_key", 16},
121 {"power_on", 17},
122 {"Reboot", 18},
123 {"rtc", 19},
124 {"edl", 20},
James Hawkins10f54be2016-02-09 15:32:38 -0800125};
126
127// Converts a string value representing the reason the system booted to an
128// integer representation. This is necessary for logging the boot_reason metric
129// via Tron, which does not accept non-integer buckets in histograms.
130int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkins10f54be2016-02-09 15:32:38 -0800131 auto mapping = kBootReasonMap.find(boot_reason);
132 if (mapping != kBootReasonMap.end()) {
133 return mapping->second;
134 }
135
136 LOG(INFO) << "Unknown boot reason: " << boot_reason;
137 return kUnknownBootReason;
138}
139
James Hawkins2a5d4242016-04-08 15:32:19 -0700140// Returns the appropriate metric key prefix for the boot_complete metric such
141// that boot metrics after a system update are labeled as ota_boot_complete;
142// otherwise, they are labeled as boot_complete. This method encapsulates the
143// bookkeeping required to track when a system update has occurred by storing
144// the UTC timestamp of the system build date and comparing against the current
145// system build date.
146std::string CalculateBootCompletePrefix() {
147 static const std::string kBuildDateKey = "build_date";
148 std::string boot_complete_prefix = "boot_complete";
149
150 std::string build_date_str = GetProperty("ro.build.date.utc");
James Hawkinsf85554e2016-07-28 11:50:23 -0700151 int32_t build_date;
152 if (!android::base::ParseInt(build_date_str.c_str(), &build_date)) {
153 return std::string();
154 }
James Hawkins2a5d4242016-04-08 15:32:19 -0700155
156 BootEventRecordStore boot_event_store;
157 BootEventRecordStore::BootEventRecord record;
158 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record) ||
159 build_date != record.second) {
160 boot_complete_prefix = "ota_" + boot_complete_prefix;
161 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
162 }
163
164 return boot_complete_prefix;
165}
166
James Hawkinseef069a2016-03-11 14:59:50 -0800167// Records several metrics related to the time it takes to boot the device,
168// including disambiguating boot time on encrypted or non-encrypted devices.
169void RecordBootComplete() {
170 BootEventRecordStore boot_event_store;
James Hawkins2a5d4242016-04-08 15:32:19 -0700171 BootEventRecordStore::BootEventRecord record;
James Hawkinseef069a2016-03-11 14:59:50 -0800172 time_t uptime = bootstat::ParseUptime();
173
James Hawkins2a5d4242016-04-08 15:32:19 -0700174 // The boot_complete metric has two variants: boot_complete and
175 // ota_boot_complete. The latter signifies that the device is booting after
176 // a system update.
177 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkinsf85554e2016-07-28 11:50:23 -0700178 if (boot_complete_prefix.empty()) {
179 // The system is hosed because the build date property could not be read.
180 return;
181 }
James Hawkinseef069a2016-03-11 14:59:50 -0800182
183 // post_decrypt_time_elapsed is only logged on encrypted devices.
184 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
185 // Log the amount of time elapsed until the device is decrypted, which
186 // includes the variable amount of time the user takes to enter the
187 // decryption password.
188 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
189
190 // Subtract the decryption time to normalize the boot cycle timing.
191 time_t boot_complete = uptime - record.second;
James Hawkins2a5d4242016-04-08 15:32:19 -0700192 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinseef069a2016-03-11 14:59:50 -0800193 boot_complete);
194
195
196 } else {
James Hawkins2a5d4242016-04-08 15:32:19 -0700197 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
James Hawkinseef069a2016-03-11 14:59:50 -0800198 uptime);
199 }
200
201 // Record the total time from device startup to boot complete, regardless of
202 // encryption state.
James Hawkins2a5d4242016-04-08 15:32:19 -0700203 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime);
James Hawkinseef069a2016-03-11 14:59:50 -0800204}
205
James Hawkins10f54be2016-02-09 15:32:38 -0800206// Records the boot_reason metric by querying the ro.boot.bootreason system
207// property.
208void RecordBootReason() {
209 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
210 BootEventRecordStore boot_event_store;
211 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
212}
213
James Hawkins35349142016-02-16 15:05:54 -0800214// Records two metrics related to the user resetting a device: the time at
215// which the device is reset, and the time since the user last reset the
216// device. The former is only set once per-factory reset.
217void RecordFactoryReset() {
218 BootEventRecordStore boot_event_store;
219 BootEventRecordStore::BootEventRecord record;
220
221 time_t current_time_utc = time(nullptr);
222
James Hawkins6b930bf2016-03-08 16:18:15 -0800223 if (current_time_utc < 0) {
224 // UMA does not display negative values in buckets, so convert to positive.
James Hawkins4b713882016-03-29 16:13:49 -0700225 bootstat::LogHistogram(
226 "factory_reset_current_time_failure", std::abs(current_time_utc));
227
228 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
229 // is losing records somehow.
230 boot_event_store.AddBootEventWithValue(
231 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins6b930bf2016-03-08 16:18:15 -0800232 return;
233 } else {
James Hawkins4b713882016-03-29 16:13:49 -0700234 bootstat::LogHistogram("factory_reset_current_time", current_time_utc);
235
236 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
237 // is losing records somehow.
238 boot_event_store.AddBootEventWithValue(
239 "factory_reset_current_time", current_time_utc);
James Hawkins6b930bf2016-03-08 16:18:15 -0800240 }
241
James Hawkins35349142016-02-16 15:05:54 -0800242 // The factory_reset boot event does not exist after the device is reset, so
243 // use this signal to mark the time of the factory reset.
244 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
245 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkinse5010442016-03-03 14:50:24 -0800246
247 // Don't log the time_since_factory_reset until some time has elapsed.
248 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins35349142016-02-16 15:05:54 -0800249 return;
250 }
251
252 // Calculate and record the difference in time between now and the
253 // factory_reset time.
254 time_t factory_reset_utc = record.second;
James Hawkinse8e8cf32016-03-22 14:13:06 -0700255 bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkins4b713882016-03-29 16:13:49 -0700256
257 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
258 // is losing records somehow.
259 boot_event_store.AddBootEventWithValue(
260 "factory_reset_record_value", factory_reset_utc);
261
James Hawkins35349142016-02-16 15:05:54 -0800262 time_t time_since_factory_reset = difftime(current_time_utc,
263 factory_reset_utc);
264 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
265 time_since_factory_reset);
266}
267
James Hawkinsabd73e62016-01-19 15:10:38 -0800268} // namespace
269
270int main(int argc, char **argv) {
271 android::base::InitLogging(argv);
272
273 const std::string cmd_line = GetCommandLine(argc, argv);
274 LOG(INFO) << "Service started: " << cmd_line;
275
James Hawkins10f54be2016-02-09 15:32:38 -0800276 int option_index = 0;
James Hawkinseef069a2016-03-11 14:59:50 -0800277 static const char boot_complete_str[] = "record_boot_complete";
James Hawkins10f54be2016-02-09 15:32:38 -0800278 static const char boot_reason_str[] = "record_boot_reason";
James Hawkinsc0188da2016-02-23 16:18:19 -0800279 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkins10f54be2016-02-09 15:32:38 -0800280 static const struct option long_options[] = {
281 { "help", no_argument, NULL, 'h' },
282 { "log", no_argument, NULL, 'l' },
283 { "print", no_argument, NULL, 'p' },
284 { "record", required_argument, NULL, 'r' },
James Hawkinseef069a2016-03-11 14:59:50 -0800285 { boot_complete_str, no_argument, NULL, 0 },
James Hawkins10f54be2016-02-09 15:32:38 -0800286 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins35349142016-02-16 15:05:54 -0800287 { factory_reset_str, no_argument, NULL, 0 },
James Hawkins10f54be2016-02-09 15:32:38 -0800288 { NULL, 0, NULL, 0 }
289 };
290
James Hawkinsabd73e62016-01-19 15:10:38 -0800291 int opt = 0;
James Hawkins10f54be2016-02-09 15:32:38 -0800292 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800293 switch (opt) {
James Hawkins10f54be2016-02-09 15:32:38 -0800294 // This case handles long options which have no single-character mapping.
295 case 0: {
296 const std::string option_name = long_options[option_index].name;
James Hawkinseef069a2016-03-11 14:59:50 -0800297 if (option_name == boot_complete_str) {
298 RecordBootComplete();
299 } else if (option_name == boot_reason_str) {
James Hawkins10f54be2016-02-09 15:32:38 -0800300 RecordBootReason();
James Hawkins35349142016-02-16 15:05:54 -0800301 } else if (option_name == factory_reset_str) {
302 RecordFactoryReset();
James Hawkins10f54be2016-02-09 15:32:38 -0800303 } else {
304 LOG(ERROR) << "Invalid option: " << option_name;
305 }
306 break;
307 }
308
James Hawkinsabd73e62016-01-19 15:10:38 -0800309 case 'h': {
310 ShowHelp(argv[0]);
311 break;
312 }
313
314 case 'l': {
315 LogBootEvents();
316 break;
317 }
318
319 case 'p': {
320 PrintBootEvents();
321 break;
322 }
323
324 case 'r': {
325 // |optarg| is an external variable set by getopt representing
326 // the option argument.
327 const char* event = optarg;
328
329 BootEventRecordStore boot_event_store;
330 boot_event_store.AddBootEvent(event);
331 break;
332 }
333
334 default: {
335 DCHECK_EQ(opt, '?');
336
337 // |optopt| is an external variable set by getopt representing
338 // the value of the invalid option.
339 LOG(ERROR) << "Invalid option: " << optopt;
340 ShowHelp(argv[0]);
341 return EXIT_FAILURE;
342 }
343 }
344 }
345
346 return 0;
347}