blob: fa6f594e35e37e003a6629d1afb69bd3016c457b [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 Hawkins10f54be2016-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 Hawkinse8e8cf32016-03-22 14:13:06 -070035#include "histogram_logger.h"
James Hawkinseef069a2016-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 Hawkinse8e8cf32016-03-22 14:13:06 -070047 bootstat::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080048 }
49}
50
51void PrintBootEvents() {
52 printf("Boot events:\n");
53 printf("------------\n");
54
55 BootEventRecordStore boot_event_store;
56 auto events = boot_event_store.GetAllBootEvents();
57 for (auto i = events.cbegin(); i != events.cend(); ++i) {
58 printf("%s\t%d\n", i->first.c_str(), i->second);
59 }
60}
61
62void ShowHelp(const char *cmd) {
63 fprintf(stderr, "Usage: %s [options]\n", cmd);
64 fprintf(stderr,
65 "options include:\n"
James Hawkins10f54be2016-02-09 15:32:38 -080066 " -h, --help Show this help\n"
67 " -l, --log Log all metrics to logstorage\n"
68 " -p, --print Dump the boot event records to the console\n"
69 " -r, --record Record the timestamp of a named boot event\n"
James Hawkinsc0188da2016-02-23 16:18:19 -080070 " --record_boot_reason Record the reason why the device booted\n"
71 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080072}
73
74// Constructs a readable, printable string from the givencommand line
75// arguments.
76std::string GetCommandLine(int argc, char **argv) {
77 std::string cmd;
78 for (int i = 0; i < argc; ++i) {
79 cmd += argv[i];
80 cmd += " ";
81 }
82
83 return cmd;
84}
85
James Hawkins10f54be2016-02-09 15:32:38 -080086// Convenience wrapper over the property API that returns an
87// std::string.
88std::string GetProperty(const char* key) {
89 std::vector<char> temp(PROPERTY_VALUE_MAX);
90 const int len = property_get(key, &temp[0], nullptr);
91 if (len < 0) {
92 return "";
93 }
94 return std::string(&temp[0], len);
95}
96
James Hawkinsee79e732016-02-12 15:49:16 -080097constexpr int32_t kUnknownBootReason = 1;
98
James Hawkins10f54be2016-02-09 15:32:38 -080099// A mapping from boot reason string, as read from the ro.boot.bootreason
100// system property, to a unique integer ID. Viewers of log data dashboards for
101// the boot_reason metric may refer to this mapping to discern the histogram
102// values.
James Hawkinsee79e732016-02-12 15:49:16 -0800103const std::map<std::string, int32_t> kBootReasonMap = {
104 {"unknown", kUnknownBootReason},
105 {"normal", 2},
106 {"recovery", 3},
107 {"reboot", 4},
108 {"PowerKey", 5},
109 {"hard_reset", 6},
110 {"kernel_panic", 7},
111 {"rpm_err", 8},
112 {"hw_reset", 9},
113 {"tz_err", 10},
114 {"adsp_err", 11},
115 {"modem_err", 12},
116 {"mba_err", 13},
117 {"Watchdog", 14},
118 {"Panic", 15},
119 {"power_key", 16},
120 {"power_on", 17},
121 {"Reboot", 18},
122 {"rtc", 19},
123 {"edl", 20},
James Hawkins10f54be2016-02-09 15:32:38 -0800124};
125
126// Converts a string value representing the reason the system booted to an
127// integer representation. This is necessary for logging the boot_reason metric
128// via Tron, which does not accept non-integer buckets in histograms.
129int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkins10f54be2016-02-09 15:32:38 -0800130 auto mapping = kBootReasonMap.find(boot_reason);
131 if (mapping != kBootReasonMap.end()) {
132 return mapping->second;
133 }
134
135 LOG(INFO) << "Unknown boot reason: " << boot_reason;
136 return kUnknownBootReason;
137}
138
James Hawkinseef069a2016-03-11 14:59:50 -0800139// Records several metrics related to the time it takes to boot the device,
140// including disambiguating boot time on encrypted or non-encrypted devices.
141void RecordBootComplete() {
142 BootEventRecordStore boot_event_store;
143 time_t uptime = bootstat::ParseUptime();
144
145 BootEventRecordStore::BootEventRecord record;
146
147 // post_decrypt_time_elapsed is only logged on encrypted devices.
148 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
149 // Log the amount of time elapsed until the device is decrypted, which
150 // includes the variable amount of time the user takes to enter the
151 // decryption password.
152 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
153
154 // Subtract the decryption time to normalize the boot cycle timing.
155 time_t boot_complete = uptime - record.second;
156 boot_event_store.AddBootEventWithValue("boot_complete_post_decrypt",
157 boot_complete);
158
159
160 } else {
161 boot_event_store.AddBootEventWithValue("boot_complete_no_encryption",
162 uptime);
163 }
164
165 // Record the total time from device startup to boot complete, regardless of
166 // encryption state.
167 boot_event_store.AddBootEventWithValue("boot_complete", uptime);
168}
169
James Hawkins10f54be2016-02-09 15:32:38 -0800170// Records the boot_reason metric by querying the ro.boot.bootreason system
171// property.
172void RecordBootReason() {
173 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
174 BootEventRecordStore boot_event_store;
175 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
176}
177
James Hawkins35349142016-02-16 15:05:54 -0800178// Records two metrics related to the user resetting a device: the time at
179// which the device is reset, and the time since the user last reset the
180// device. The former is only set once per-factory reset.
181void RecordFactoryReset() {
182 BootEventRecordStore boot_event_store;
183 BootEventRecordStore::BootEventRecord record;
184
185 time_t current_time_utc = time(nullptr);
186
James Hawkins6b930bf2016-03-08 16:18:15 -0800187 if (current_time_utc < 0) {
188 // UMA does not display negative values in buckets, so convert to positive.
James Hawkins4b713882016-03-29 16:13:49 -0700189 bootstat::LogHistogram(
190 "factory_reset_current_time_failure", std::abs(current_time_utc));
191
192 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
193 // is losing records somehow.
194 boot_event_store.AddBootEventWithValue(
195 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins6b930bf2016-03-08 16:18:15 -0800196 return;
197 } else {
James Hawkins4b713882016-03-29 16:13:49 -0700198 bootstat::LogHistogram("factory_reset_current_time", current_time_utc);
199
200 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
201 // is losing records somehow.
202 boot_event_store.AddBootEventWithValue(
203 "factory_reset_current_time", current_time_utc);
James Hawkins6b930bf2016-03-08 16:18:15 -0800204 }
205
James Hawkins35349142016-02-16 15:05:54 -0800206 // The factory_reset boot event does not exist after the device is reset, so
207 // use this signal to mark the time of the factory reset.
208 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
209 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkinse5010442016-03-03 14:50:24 -0800210
211 // Don't log the time_since_factory_reset until some time has elapsed.
212 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins35349142016-02-16 15:05:54 -0800213 return;
214 }
215
216 // Calculate and record the difference in time between now and the
217 // factory_reset time.
218 time_t factory_reset_utc = record.second;
James Hawkinse8e8cf32016-03-22 14:13:06 -0700219 bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkins4b713882016-03-29 16:13:49 -0700220
221 // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
222 // is losing records somehow.
223 boot_event_store.AddBootEventWithValue(
224 "factory_reset_record_value", factory_reset_utc);
225
James Hawkins35349142016-02-16 15:05:54 -0800226 time_t time_since_factory_reset = difftime(current_time_utc,
227 factory_reset_utc);
228 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
229 time_since_factory_reset);
230}
231
James Hawkinsabd73e62016-01-19 15:10:38 -0800232} // namespace
233
234int main(int argc, char **argv) {
235 android::base::InitLogging(argv);
236
237 const std::string cmd_line = GetCommandLine(argc, argv);
238 LOG(INFO) << "Service started: " << cmd_line;
239
James Hawkins10f54be2016-02-09 15:32:38 -0800240 int option_index = 0;
James Hawkinseef069a2016-03-11 14:59:50 -0800241 static const char boot_complete_str[] = "record_boot_complete";
James Hawkins10f54be2016-02-09 15:32:38 -0800242 static const char boot_reason_str[] = "record_boot_reason";
James Hawkinsc0188da2016-02-23 16:18:19 -0800243 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkins10f54be2016-02-09 15:32:38 -0800244 static const struct option long_options[] = {
245 { "help", no_argument, NULL, 'h' },
246 { "log", no_argument, NULL, 'l' },
247 { "print", no_argument, NULL, 'p' },
248 { "record", required_argument, NULL, 'r' },
James Hawkinseef069a2016-03-11 14:59:50 -0800249 { boot_complete_str, no_argument, NULL, 0 },
James Hawkins10f54be2016-02-09 15:32:38 -0800250 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins35349142016-02-16 15:05:54 -0800251 { factory_reset_str, no_argument, NULL, 0 },
James Hawkins10f54be2016-02-09 15:32:38 -0800252 { NULL, 0, NULL, 0 }
253 };
254
James Hawkinsabd73e62016-01-19 15:10:38 -0800255 int opt = 0;
James Hawkins10f54be2016-02-09 15:32:38 -0800256 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800257 switch (opt) {
James Hawkins10f54be2016-02-09 15:32:38 -0800258 // This case handles long options which have no single-character mapping.
259 case 0: {
260 const std::string option_name = long_options[option_index].name;
James Hawkinseef069a2016-03-11 14:59:50 -0800261 if (option_name == boot_complete_str) {
262 RecordBootComplete();
263 } else if (option_name == boot_reason_str) {
James Hawkins10f54be2016-02-09 15:32:38 -0800264 RecordBootReason();
James Hawkins35349142016-02-16 15:05:54 -0800265 } else if (option_name == factory_reset_str) {
266 RecordFactoryReset();
James Hawkins10f54be2016-02-09 15:32:38 -0800267 } else {
268 LOG(ERROR) << "Invalid option: " << option_name;
269 }
270 break;
271 }
272
James Hawkinsabd73e62016-01-19 15:10:38 -0800273 case 'h': {
274 ShowHelp(argv[0]);
275 break;
276 }
277
278 case 'l': {
279 LogBootEvents();
280 break;
281 }
282
283 case 'p': {
284 PrintBootEvents();
285 break;
286 }
287
288 case 'r': {
289 // |optarg| is an external variable set by getopt representing
290 // the option argument.
291 const char* event = optarg;
292
293 BootEventRecordStore boot_event_store;
294 boot_event_store.AddBootEvent(event);
295 break;
296 }
297
298 default: {
299 DCHECK_EQ(opt, '?');
300
301 // |optopt| is an external variable set by getopt representing
302 // the value of the invalid option.
303 LOG(ERROR) << "Invalid option: " << optopt;
304 ShowHelp(argv[0]);
305 return EXIT_FAILURE;
306 }
307 }
308 }
309
310 return 0;
311}