blob: 360447f8110aca3c60632150051200b88c4f9360 [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 static const char* factory_reset_current_time = "factory_reset_current_time";
188 if (current_time_utc < 0) {
189 // UMA does not display negative values in buckets, so convert to positive.
James Hawkinse8e8cf32016-03-22 14:13:06 -0700190 bootstat::LogHistogram(factory_reset_current_time, std::abs(current_time_utc));
James Hawkins6b930bf2016-03-08 16:18:15 -0800191 return;
192 } else {
James Hawkinse8e8cf32016-03-22 14:13:06 -0700193 bootstat::LogHistogram(factory_reset_current_time, current_time_utc);
James Hawkins6b930bf2016-03-08 16:18:15 -0800194 }
195
James Hawkins35349142016-02-16 15:05:54 -0800196 // The factory_reset boot event does not exist after the device is reset, so
197 // use this signal to mark the time of the factory reset.
198 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
199 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkinse5010442016-03-03 14:50:24 -0800200
201 // Don't log the time_since_factory_reset until some time has elapsed.
202 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins35349142016-02-16 15:05:54 -0800203 return;
204 }
205
206 // Calculate and record the difference in time between now and the
207 // factory_reset time.
208 time_t factory_reset_utc = record.second;
James Hawkinse8e8cf32016-03-22 14:13:06 -0700209 bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkins35349142016-02-16 15:05:54 -0800210 time_t time_since_factory_reset = difftime(current_time_utc,
211 factory_reset_utc);
212 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
213 time_since_factory_reset);
214}
215
James Hawkinsabd73e62016-01-19 15:10:38 -0800216} // namespace
217
218int main(int argc, char **argv) {
219 android::base::InitLogging(argv);
220
221 const std::string cmd_line = GetCommandLine(argc, argv);
222 LOG(INFO) << "Service started: " << cmd_line;
223
James Hawkins10f54be2016-02-09 15:32:38 -0800224 int option_index = 0;
James Hawkinseef069a2016-03-11 14:59:50 -0800225 static const char boot_complete_str[] = "record_boot_complete";
James Hawkins10f54be2016-02-09 15:32:38 -0800226 static const char boot_reason_str[] = "record_boot_reason";
James Hawkinsc0188da2016-02-23 16:18:19 -0800227 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkins10f54be2016-02-09 15:32:38 -0800228 static const struct option long_options[] = {
229 { "help", no_argument, NULL, 'h' },
230 { "log", no_argument, NULL, 'l' },
231 { "print", no_argument, NULL, 'p' },
232 { "record", required_argument, NULL, 'r' },
James Hawkinseef069a2016-03-11 14:59:50 -0800233 { boot_complete_str, no_argument, NULL, 0 },
James Hawkins10f54be2016-02-09 15:32:38 -0800234 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins35349142016-02-16 15:05:54 -0800235 { factory_reset_str, no_argument, NULL, 0 },
James Hawkins10f54be2016-02-09 15:32:38 -0800236 { NULL, 0, NULL, 0 }
237 };
238
James Hawkinsabd73e62016-01-19 15:10:38 -0800239 int opt = 0;
James Hawkins10f54be2016-02-09 15:32:38 -0800240 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800241 switch (opt) {
James Hawkins10f54be2016-02-09 15:32:38 -0800242 // This case handles long options which have no single-character mapping.
243 case 0: {
244 const std::string option_name = long_options[option_index].name;
James Hawkinseef069a2016-03-11 14:59:50 -0800245 if (option_name == boot_complete_str) {
246 RecordBootComplete();
247 } else if (option_name == boot_reason_str) {
James Hawkins10f54be2016-02-09 15:32:38 -0800248 RecordBootReason();
James Hawkins35349142016-02-16 15:05:54 -0800249 } else if (option_name == factory_reset_str) {
250 RecordFactoryReset();
James Hawkins10f54be2016-02-09 15:32:38 -0800251 } else {
252 LOG(ERROR) << "Invalid option: " << option_name;
253 }
254 break;
255 }
256
James Hawkinsabd73e62016-01-19 15:10:38 -0800257 case 'h': {
258 ShowHelp(argv[0]);
259 break;
260 }
261
262 case 'l': {
263 LogBootEvents();
264 break;
265 }
266
267 case 'p': {
268 PrintBootEvents();
269 break;
270 }
271
272 case 'r': {
273 // |optarg| is an external variable set by getopt representing
274 // the option argument.
275 const char* event = optarg;
276
277 BootEventRecordStore boot_event_store;
278 boot_event_store.AddBootEvent(event);
279 break;
280 }
281
282 default: {
283 DCHECK_EQ(opt, '?');
284
285 // |optopt| is an external variable set by getopt representing
286 // the value of the invalid option.
287 LOG(ERROR) << "Invalid option: " << optopt;
288 ShowHelp(argv[0]);
289 return EXIT_FAILURE;
290 }
291 }
292 }
293
294 return 0;
295}