blob: d547c1955eb01dc740932ed0188ea7fa85c2e88e [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 Hawkins16e84cc2016-04-14 14:13:20 -0700172
James Hawkinseef069a2016-03-11 14:59:50 -0800173 time_t uptime = bootstat::ParseUptime();
James Hawkins16e84cc2016-04-14 14:13:20 -0700174 time_t current_time_utc = time(nullptr);
175
176 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
177 time_t last_boot_time_utc = record.second;
178 time_t time_since_last_boot = difftime(current_time_utc,
179 last_boot_time_utc);
180 boot_event_store.AddBootEventWithValue("time_since_last_boot",
181 time_since_last_boot);
182 }
183
184 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinseef069a2016-03-11 14:59:50 -0800185
James Hawkins2a5d4242016-04-08 15:32:19 -0700186 // The boot_complete metric has two variants: boot_complete and
187 // ota_boot_complete. The latter signifies that the device is booting after
188 // a system update.
189 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkinsf85554e2016-07-28 11:50:23 -0700190 if (boot_complete_prefix.empty()) {
191 // The system is hosed because the build date property could not be read.
192 return;
193 }
James Hawkinseef069a2016-03-11 14:59:50 -0800194
195 // post_decrypt_time_elapsed is only logged on encrypted devices.
196 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
197 // Log the amount of time elapsed until the device is decrypted, which
198 // includes the variable amount of time the user takes to enter the
199 // decryption password.
200 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
201
202 // Subtract the decryption time to normalize the boot cycle timing.
203 time_t boot_complete = uptime - record.second;
James Hawkins2a5d4242016-04-08 15:32:19 -0700204 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinseef069a2016-03-11 14:59:50 -0800205 boot_complete);
206
207
208 } else {
James Hawkins2a5d4242016-04-08 15:32:19 -0700209 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
James Hawkinseef069a2016-03-11 14:59:50 -0800210 uptime);
211 }
212
213 // Record the total time from device startup to boot complete, regardless of
214 // encryption state.
James Hawkins2a5d4242016-04-08 15:32:19 -0700215 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime);
James Hawkinseef069a2016-03-11 14:59:50 -0800216}
217
James Hawkins10f54be2016-02-09 15:32:38 -0800218// Records the boot_reason metric by querying the ro.boot.bootreason system
219// property.
220void RecordBootReason() {
221 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
222 BootEventRecordStore boot_event_store;
223 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
224}
225
James Hawkins35349142016-02-16 15:05:54 -0800226// Records two metrics related to the user resetting a device: the time at
227// which the device is reset, and the time since the user last reset the
228// device. The former is only set once per-factory reset.
229void RecordFactoryReset() {
230 BootEventRecordStore boot_event_store;
231 BootEventRecordStore::BootEventRecord record;
232
233 time_t current_time_utc = time(nullptr);
234
James Hawkins6b930bf2016-03-08 16:18:15 -0800235 if (current_time_utc < 0) {
236 // UMA does not display negative values in buckets, so convert to positive.
James Hawkins4b713882016-03-29 16:13:49 -0700237 bootstat::LogHistogram(
238 "factory_reset_current_time_failure", std::abs(current_time_utc));
239
240 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
241 // is losing records somehow.
242 boot_event_store.AddBootEventWithValue(
243 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins6b930bf2016-03-08 16:18:15 -0800244 return;
245 } else {
James Hawkins4b713882016-03-29 16:13:49 -0700246 bootstat::LogHistogram("factory_reset_current_time", current_time_utc);
247
248 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
249 // is losing records somehow.
250 boot_event_store.AddBootEventWithValue(
251 "factory_reset_current_time", current_time_utc);
James Hawkins6b930bf2016-03-08 16:18:15 -0800252 }
253
James Hawkins35349142016-02-16 15:05:54 -0800254 // The factory_reset boot event does not exist after the device is reset, so
255 // use this signal to mark the time of the factory reset.
256 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
257 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkinse5010442016-03-03 14:50:24 -0800258
259 // Don't log the time_since_factory_reset until some time has elapsed.
260 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins35349142016-02-16 15:05:54 -0800261 return;
262 }
263
264 // Calculate and record the difference in time between now and the
265 // factory_reset time.
266 time_t factory_reset_utc = record.second;
James Hawkinse8e8cf32016-03-22 14:13:06 -0700267 bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkins4b713882016-03-29 16:13:49 -0700268
269 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
270 // is losing records somehow.
271 boot_event_store.AddBootEventWithValue(
272 "factory_reset_record_value", factory_reset_utc);
273
James Hawkins35349142016-02-16 15:05:54 -0800274 time_t time_since_factory_reset = difftime(current_time_utc,
275 factory_reset_utc);
276 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
277 time_since_factory_reset);
278}
279
James Hawkinsabd73e62016-01-19 15:10:38 -0800280} // namespace
281
282int main(int argc, char **argv) {
283 android::base::InitLogging(argv);
284
285 const std::string cmd_line = GetCommandLine(argc, argv);
286 LOG(INFO) << "Service started: " << cmd_line;
287
James Hawkins10f54be2016-02-09 15:32:38 -0800288 int option_index = 0;
James Hawkinseef069a2016-03-11 14:59:50 -0800289 static const char boot_complete_str[] = "record_boot_complete";
James Hawkins10f54be2016-02-09 15:32:38 -0800290 static const char boot_reason_str[] = "record_boot_reason";
James Hawkinsc0188da2016-02-23 16:18:19 -0800291 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkins10f54be2016-02-09 15:32:38 -0800292 static const struct option long_options[] = {
293 { "help", no_argument, NULL, 'h' },
294 { "log", no_argument, NULL, 'l' },
295 { "print", no_argument, NULL, 'p' },
296 { "record", required_argument, NULL, 'r' },
James Hawkinseef069a2016-03-11 14:59:50 -0800297 { boot_complete_str, no_argument, NULL, 0 },
James Hawkins10f54be2016-02-09 15:32:38 -0800298 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins35349142016-02-16 15:05:54 -0800299 { factory_reset_str, no_argument, NULL, 0 },
James Hawkins10f54be2016-02-09 15:32:38 -0800300 { NULL, 0, NULL, 0 }
301 };
302
James Hawkinsabd73e62016-01-19 15:10:38 -0800303 int opt = 0;
James Hawkins10f54be2016-02-09 15:32:38 -0800304 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800305 switch (opt) {
James Hawkins10f54be2016-02-09 15:32:38 -0800306 // This case handles long options which have no single-character mapping.
307 case 0: {
308 const std::string option_name = long_options[option_index].name;
James Hawkinseef069a2016-03-11 14:59:50 -0800309 if (option_name == boot_complete_str) {
310 RecordBootComplete();
311 } else if (option_name == boot_reason_str) {
James Hawkins10f54be2016-02-09 15:32:38 -0800312 RecordBootReason();
James Hawkins35349142016-02-16 15:05:54 -0800313 } else if (option_name == factory_reset_str) {
314 RecordFactoryReset();
James Hawkins10f54be2016-02-09 15:32:38 -0800315 } else {
316 LOG(ERROR) << "Invalid option: " << option_name;
317 }
318 break;
319 }
320
James Hawkinsabd73e62016-01-19 15:10:38 -0800321 case 'h': {
322 ShowHelp(argv[0]);
323 break;
324 }
325
326 case 'l': {
327 LogBootEvents();
328 break;
329 }
330
331 case 'p': {
332 PrintBootEvents();
333 break;
334 }
335
336 case 'r': {
337 // |optarg| is an external variable set by getopt representing
338 // the option argument.
339 const char* event = optarg;
340
341 BootEventRecordStore boot_event_store;
342 boot_event_store.AddBootEvent(event);
343 break;
344 }
345
346 default: {
347 DCHECK_EQ(opt, '?');
348
349 // |optopt| is an external variable set by getopt representing
350 // the value of the invalid option.
351 LOG(ERROR) << "Invalid option: " << optopt;
352 ShowHelp(argv[0]);
353 return EXIT_FAILURE;
354 }
355 }
356 }
357
358 return 0;
359}