blob: 7c1b7f38d04667eae4fd33ce58b82069bff689a4 [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 Hawkinsa4a1a4a2016-02-09 15:32:38 -080031#include <cutils/properties.h>
James Hawkinsabd73e62016-01-19 15:10:38 -080032#include <log/log.h>
33#include "boot_event_record_store.h"
34#include "event_log_list_builder.h"
James Hawkins6f282992016-03-22 14:13:06 -070035#include "histogram_logger.h"
James Hawkinsc08e9962016-03-11 14:59:50 -080036#include "uptime_parser.h"
James Hawkinsabd73e62016-01-19 15:10:38 -080037
38namespace {
39
James Hawkinsabd73e62016-01-19 15:10:38 -080040// Scans the boot event record store for record files and logs each boot event
41// via EventLog.
42void LogBootEvents() {
43 BootEventRecordStore boot_event_store;
44
45 auto events = boot_event_store.GetAllBootEvents();
46 for (auto i = events.cbegin(); i != events.cend(); ++i) {
James Hawkins6f282992016-03-22 14:13:06 -070047 bootstat::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080048 }
49}
50
James Hawkinsc6275582016-03-22 10:47:44 -070051// Records the named boot |event| to the record store. If |value| is non-empty
52// and is a proper string representation of an integer value, the converted
53// integer value is associated with the boot event.
54void RecordBootEventFromCommandLine(
55 const std::string& event, const std::string& value_str) {
56 BootEventRecordStore boot_event_store;
57 if (!value_str.empty()) {
58 int32_t value = 0;
59 value = std::stoi(value_str);
60 boot_event_store.AddBootEventWithValue(event, value);
61 } else {
62 boot_event_store.AddBootEvent(event);
63 }
64}
65
James Hawkinsabd73e62016-01-19 15:10:38 -080066void PrintBootEvents() {
67 printf("Boot events:\n");
68 printf("------------\n");
69
70 BootEventRecordStore boot_event_store;
71 auto events = boot_event_store.GetAllBootEvents();
72 for (auto i = events.cbegin(); i != events.cend(); ++i) {
73 printf("%s\t%d\n", i->first.c_str(), i->second);
74 }
75}
76
77void ShowHelp(const char *cmd) {
78 fprintf(stderr, "Usage: %s [options]\n", cmd);
79 fprintf(stderr,
80 "options include:\n"
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080081 " -h, --help Show this help\n"
82 " -l, --log Log all metrics to logstorage\n"
83 " -p, --print Dump the boot event records to the console\n"
84 " -r, --record Record the timestamp of a named boot event\n"
James Hawkinsc6275582016-03-22 10:47:44 -070085 " --value Optional value to associate with the boot event\n"
James Hawkins53684ea2016-02-23 16:18:19 -080086 " --record_boot_reason Record the reason why the device booted\n"
87 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080088}
89
90// Constructs a readable, printable string from the givencommand line
91// arguments.
92std::string GetCommandLine(int argc, char **argv) {
93 std::string cmd;
94 for (int i = 0; i < argc; ++i) {
95 cmd += argv[i];
96 cmd += " ";
97 }
98
99 return cmd;
100}
101
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800102// Convenience wrapper over the property API that returns an
103// std::string.
104std::string GetProperty(const char* key) {
105 std::vector<char> temp(PROPERTY_VALUE_MAX);
106 const int len = property_get(key, &temp[0], nullptr);
107 if (len < 0) {
108 return "";
109 }
110 return std::string(&temp[0], len);
111}
112
James Hawkins6f74c0b2016-02-12 15:49:16 -0800113constexpr int32_t kUnknownBootReason = 1;
114
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800115// A mapping from boot reason string, as read from the ro.boot.bootreason
116// system property, to a unique integer ID. Viewers of log data dashboards for
117// the boot_reason metric may refer to this mapping to discern the histogram
118// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800119const std::map<std::string, int32_t> kBootReasonMap = {
120 {"unknown", kUnknownBootReason},
121 {"normal", 2},
122 {"recovery", 3},
123 {"reboot", 4},
124 {"PowerKey", 5},
125 {"hard_reset", 6},
126 {"kernel_panic", 7},
127 {"rpm_err", 8},
128 {"hw_reset", 9},
129 {"tz_err", 10},
130 {"adsp_err", 11},
131 {"modem_err", 12},
132 {"mba_err", 13},
133 {"Watchdog", 14},
134 {"Panic", 15},
135 {"power_key", 16},
136 {"power_on", 17},
137 {"Reboot", 18},
138 {"rtc", 19},
139 {"edl", 20},
James Hawkins45ead352016-03-08 16:42:07 -0800140 {"oem_pon1", 21},
141 {"oem_powerkey", 22},
142 {"oem_unknown_reset", 23},
143 {"srto: HWWDT reset SC", 24},
144 {"srto: HWWDT reset platform", 25},
145 {"srto: bootloader", 26},
146 {"srto: kernel panic", 27},
147 {"srto: kernel watchdog reset", 28},
148 {"srto: normal", 29},
149 {"srto: reboot", 30},
150 {"srto: reboot-bootloader", 31},
151 {"srto: security watchdog reset", 32},
152 {"srto: wakesrc", 33},
153 {"srto: watchdog", 34},
154 {"srto:1-1", 35},
155 {"srto:omap_hsmm", 36},
156 {"srto:phy0", 37},
157 {"srto:rtc0", 38},
158 {"srto:touchpad", 39},
159 {"watchdog", 40},
160 {"watchdogr", 41},
161 {"wdog_bark", 42},
162 {"wdog_bite", 43},
163 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800164};
165
166// Converts a string value representing the reason the system booted to an
167// integer representation. This is necessary for logging the boot_reason metric
168// via Tron, which does not accept non-integer buckets in histograms.
169int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800170 auto mapping = kBootReasonMap.find(boot_reason);
171 if (mapping != kBootReasonMap.end()) {
172 return mapping->second;
173 }
174
175 LOG(INFO) << "Unknown boot reason: " << boot_reason;
176 return kUnknownBootReason;
177}
178
James Hawkinsb9cf7712016-04-08 15:32:19 -0700179// Returns the appropriate metric key prefix for the boot_complete metric such
180// that boot metrics after a system update are labeled as ota_boot_complete;
181// otherwise, they are labeled as boot_complete. This method encapsulates the
182// bookkeeping required to track when a system update has occurred by storing
183// the UTC timestamp of the system build date and comparing against the current
184// system build date.
185std::string CalculateBootCompletePrefix() {
186 static const std::string kBuildDateKey = "build_date";
187 std::string boot_complete_prefix = "boot_complete";
188
189 std::string build_date_str = GetProperty("ro.build.date.utc");
190 int32_t build_date = std::stoi(build_date_str);
191
192 BootEventRecordStore boot_event_store;
193 BootEventRecordStore::BootEventRecord record;
194 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record) ||
195 build_date != record.second) {
196 boot_complete_prefix = "ota_" + boot_complete_prefix;
197 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
198 }
199
200 return boot_complete_prefix;
201}
202
James Hawkinsc08e9962016-03-11 14:59:50 -0800203// Records several metrics related to the time it takes to boot the device,
204// including disambiguating boot time on encrypted or non-encrypted devices.
205void RecordBootComplete() {
206 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700207 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -0700208
James Hawkinsc08e9962016-03-11 14:59:50 -0800209 time_t uptime = bootstat::ParseUptime();
James Hawkins2d8b3e62016-04-14 14:13:20 -0700210 time_t current_time_utc = time(nullptr);
211
212 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
213 time_t last_boot_time_utc = record.second;
214 time_t time_since_last_boot = difftime(current_time_utc,
215 last_boot_time_utc);
216 boot_event_store.AddBootEventWithValue("time_since_last_boot",
217 time_since_last_boot);
218 }
219
220 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -0800221
James Hawkinsb9cf7712016-04-08 15:32:19 -0700222 // The boot_complete metric has two variants: boot_complete and
223 // ota_boot_complete. The latter signifies that the device is booting after
224 // a system update.
225 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkinsc08e9962016-03-11 14:59:50 -0800226
227 // post_decrypt_time_elapsed is only logged on encrypted devices.
228 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
229 // Log the amount of time elapsed until the device is decrypted, which
230 // includes the variable amount of time the user takes to enter the
231 // decryption password.
232 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
233
234 // Subtract the decryption time to normalize the boot cycle timing.
235 time_t boot_complete = uptime - record.second;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700236 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinsc08e9962016-03-11 14:59:50 -0800237 boot_complete);
238
239
240 } else {
James Hawkinsb9cf7712016-04-08 15:32:19 -0700241 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
James Hawkinsc08e9962016-03-11 14:59:50 -0800242 uptime);
243 }
244
245 // Record the total time from device startup to boot complete, regardless of
246 // encryption state.
James Hawkinsb9cf7712016-04-08 15:32:19 -0700247 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime);
James Hawkinsc08e9962016-03-11 14:59:50 -0800248}
249
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800250// Records the boot_reason metric by querying the ro.boot.bootreason system
251// property.
252void RecordBootReason() {
253 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
254 BootEventRecordStore boot_event_store;
255 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
256}
257
James Hawkins500d7152016-02-16 15:05:54 -0800258// Records two metrics related to the user resetting a device: the time at
259// which the device is reset, and the time since the user last reset the
260// device. The former is only set once per-factory reset.
261void RecordFactoryReset() {
262 BootEventRecordStore boot_event_store;
263 BootEventRecordStore::BootEventRecord record;
264
265 time_t current_time_utc = time(nullptr);
266
James Hawkins0660b302016-03-08 16:18:15 -0800267 if (current_time_utc < 0) {
268 // UMA does not display negative values in buckets, so convert to positive.
James Hawkinsfff95ba2016-03-29 16:13:49 -0700269 bootstat::LogHistogram(
270 "factory_reset_current_time_failure", std::abs(current_time_utc));
271
272 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
273 // is losing records somehow.
274 boot_event_store.AddBootEventWithValue(
275 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800276 return;
277 } else {
James Hawkinsfff95ba2016-03-29 16:13:49 -0700278 bootstat::LogHistogram("factory_reset_current_time", current_time_utc);
279
280 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
281 // is losing records somehow.
282 boot_event_store.AddBootEventWithValue(
283 "factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800284 }
285
James Hawkins500d7152016-02-16 15:05:54 -0800286 // The factory_reset boot event does not exist after the device is reset, so
287 // use this signal to mark the time of the factory reset.
288 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
289 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800290
291 // Don't log the time_since_factory_reset until some time has elapsed.
292 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800293 return;
294 }
295
296 // Calculate and record the difference in time between now and the
297 // factory_reset time.
298 time_t factory_reset_utc = record.second;
James Hawkins6f282992016-03-22 14:13:06 -0700299 bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700300
301 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
302 // is losing records somehow.
303 boot_event_store.AddBootEventWithValue(
304 "factory_reset_record_value", factory_reset_utc);
305
James Hawkins500d7152016-02-16 15:05:54 -0800306 time_t time_since_factory_reset = difftime(current_time_utc,
307 factory_reset_utc);
308 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
309 time_since_factory_reset);
310}
311
James Hawkinsabd73e62016-01-19 15:10:38 -0800312} // namespace
313
314int main(int argc, char **argv) {
315 android::base::InitLogging(argv);
316
317 const std::string cmd_line = GetCommandLine(argc, argv);
318 LOG(INFO) << "Service started: " << cmd_line;
319
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800320 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700321 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800322 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800323 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800324 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800325 static const struct option long_options[] = {
326 { "help", no_argument, NULL, 'h' },
327 { "log", no_argument, NULL, 'l' },
328 { "print", no_argument, NULL, 'p' },
329 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700330 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800331 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800332 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800333 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800334 { NULL, 0, NULL, 0 }
335 };
336
James Hawkinsc6275582016-03-22 10:47:44 -0700337 std::string boot_event;
338 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800339 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800340 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800341 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800342 // This case handles long options which have no single-character mapping.
343 case 0: {
344 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700345 if (option_name == value_str) {
346 // |optarg| is an external variable set by getopt representing
347 // the option argument.
348 value = optarg;
349 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800350 RecordBootComplete();
351 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800352 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800353 } else if (option_name == factory_reset_str) {
354 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800355 } else {
356 LOG(ERROR) << "Invalid option: " << option_name;
357 }
358 break;
359 }
360
James Hawkinsabd73e62016-01-19 15:10:38 -0800361 case 'h': {
362 ShowHelp(argv[0]);
363 break;
364 }
365
366 case 'l': {
367 LogBootEvents();
368 break;
369 }
370
371 case 'p': {
372 PrintBootEvents();
373 break;
374 }
375
376 case 'r': {
377 // |optarg| is an external variable set by getopt representing
378 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700379 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800380 break;
381 }
382
383 default: {
384 DCHECK_EQ(opt, '?');
385
386 // |optopt| is an external variable set by getopt representing
387 // the value of the invalid option.
388 LOG(ERROR) << "Invalid option: " << optopt;
389 ShowHelp(argv[0]);
390 return EXIT_FAILURE;
391 }
392 }
393 }
394
James Hawkinsc6275582016-03-22 10:47:44 -0700395 if (!boot_event.empty()) {
396 RecordBootEventFromCommandLine(boot_event, value);
397 }
398
James Hawkinsabd73e62016-01-19 15:10:38 -0800399 return 0;
400}