blob: 4c8c8b64c2c85e0c51641c938537570a952e233e [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 Hawkinseef069a2016-03-11 14:59:50 -080035#include "uptime_parser.h"
James Hawkinsabd73e62016-01-19 15:10:38 -080036
37namespace {
38
39// Builds an EventLog buffer named |event| containing |data| and writes
40// the log into the Tron histogram logs.
41void LogBootEvent(const std::string& event, int32_t data) {
James Hawkins10f54be2016-02-09 15:32:38 -080042 LOG(INFO) << "Logging boot metric: " << event << " " << data;
James Hawkinsabd73e62016-01-19 15:10:38 -080043
44 EventLogListBuilder log_builder;
45 log_builder.Append(event);
46 log_builder.Append(data);
47
48 std::unique_ptr<uint8_t[]> log;
49 size_t size;
50 log_builder.Release(&log, &size);
51
52 android_bWriteLog(HISTOGRAM_LOG_TAG, log.get(), size);
53}
54
55// Scans the boot event record store for record files and logs each boot event
56// via EventLog.
57void LogBootEvents() {
58 BootEventRecordStore boot_event_store;
59
60 auto events = boot_event_store.GetAllBootEvents();
61 for (auto i = events.cbegin(); i != events.cend(); ++i) {
62 LogBootEvent(i->first, i->second);
63 }
64}
65
66void 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 Hawkins10f54be2016-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 Hawkinsc0188da2016-02-23 16:18:19 -080085 " --record_boot_reason Record the reason why the device booted\n"
86 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080087}
88
89// Constructs a readable, printable string from the givencommand line
90// arguments.
91std::string GetCommandLine(int argc, char **argv) {
92 std::string cmd;
93 for (int i = 0; i < argc; ++i) {
94 cmd += argv[i];
95 cmd += " ";
96 }
97
98 return cmd;
99}
100
James Hawkins10f54be2016-02-09 15:32:38 -0800101// Convenience wrapper over the property API that returns an
102// std::string.
103std::string GetProperty(const char* key) {
104 std::vector<char> temp(PROPERTY_VALUE_MAX);
105 const int len = property_get(key, &temp[0], nullptr);
106 if (len < 0) {
107 return "";
108 }
109 return std::string(&temp[0], len);
110}
111
James Hawkinsee79e732016-02-12 15:49:16 -0800112constexpr int32_t kUnknownBootReason = 1;
113
James Hawkins10f54be2016-02-09 15:32:38 -0800114// A mapping from boot reason string, as read from the ro.boot.bootreason
115// system property, to a unique integer ID. Viewers of log data dashboards for
116// the boot_reason metric may refer to this mapping to discern the histogram
117// values.
James Hawkinsee79e732016-02-12 15:49:16 -0800118const std::map<std::string, int32_t> kBootReasonMap = {
119 {"unknown", kUnknownBootReason},
120 {"normal", 2},
121 {"recovery", 3},
122 {"reboot", 4},
123 {"PowerKey", 5},
124 {"hard_reset", 6},
125 {"kernel_panic", 7},
126 {"rpm_err", 8},
127 {"hw_reset", 9},
128 {"tz_err", 10},
129 {"adsp_err", 11},
130 {"modem_err", 12},
131 {"mba_err", 13},
132 {"Watchdog", 14},
133 {"Panic", 15},
134 {"power_key", 16},
135 {"power_on", 17},
136 {"Reboot", 18},
137 {"rtc", 19},
138 {"edl", 20},
James Hawkins10f54be2016-02-09 15:32:38 -0800139};
140
141// Converts a string value representing the reason the system booted to an
142// integer representation. This is necessary for logging the boot_reason metric
143// via Tron, which does not accept non-integer buckets in histograms.
144int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkins10f54be2016-02-09 15:32:38 -0800145 auto mapping = kBootReasonMap.find(boot_reason);
146 if (mapping != kBootReasonMap.end()) {
147 return mapping->second;
148 }
149
150 LOG(INFO) << "Unknown boot reason: " << boot_reason;
151 return kUnknownBootReason;
152}
153
James Hawkinseef069a2016-03-11 14:59:50 -0800154// Records several metrics related to the time it takes to boot the device,
155// including disambiguating boot time on encrypted or non-encrypted devices.
156void RecordBootComplete() {
157 BootEventRecordStore boot_event_store;
158 time_t uptime = bootstat::ParseUptime();
159
160 BootEventRecordStore::BootEventRecord record;
161
162 // post_decrypt_time_elapsed is only logged on encrypted devices.
163 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
164 // Log the amount of time elapsed until the device is decrypted, which
165 // includes the variable amount of time the user takes to enter the
166 // decryption password.
167 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
168
169 // Subtract the decryption time to normalize the boot cycle timing.
170 time_t boot_complete = uptime - record.second;
171 boot_event_store.AddBootEventWithValue("boot_complete_post_decrypt",
172 boot_complete);
173
174
175 } else {
176 boot_event_store.AddBootEventWithValue("boot_complete_no_encryption",
177 uptime);
178 }
179
180 // Record the total time from device startup to boot complete, regardless of
181 // encryption state.
182 boot_event_store.AddBootEventWithValue("boot_complete", uptime);
183}
184
James Hawkins10f54be2016-02-09 15:32:38 -0800185// Records the boot_reason metric by querying the ro.boot.bootreason system
186// property.
187void RecordBootReason() {
188 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
189 BootEventRecordStore boot_event_store;
190 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
191}
192
James Hawkins35349142016-02-16 15:05:54 -0800193// Records two metrics related to the user resetting a device: the time at
194// which the device is reset, and the time since the user last reset the
195// device. The former is only set once per-factory reset.
196void RecordFactoryReset() {
197 BootEventRecordStore boot_event_store;
198 BootEventRecordStore::BootEventRecord record;
199
200 time_t current_time_utc = time(nullptr);
201
James Hawkins6b930bf2016-03-08 16:18:15 -0800202 static const char* factory_reset_current_time = "factory_reset_current_time";
203 if (current_time_utc < 0) {
204 // UMA does not display negative values in buckets, so convert to positive.
205 LogBootEvent(factory_reset_current_time, std::abs(current_time_utc));
206 return;
207 } else {
208 LogBootEvent(factory_reset_current_time, current_time_utc);
209 }
210
James Hawkins35349142016-02-16 15:05:54 -0800211 // The factory_reset boot event does not exist after the device is reset, so
212 // use this signal to mark the time of the factory reset.
213 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
214 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkinse5010442016-03-03 14:50:24 -0800215
216 // Don't log the time_since_factory_reset until some time has elapsed.
217 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins35349142016-02-16 15:05:54 -0800218 return;
219 }
220
221 // Calculate and record the difference in time between now and the
222 // factory_reset time.
223 time_t factory_reset_utc = record.second;
James Hawkins6b930bf2016-03-08 16:18:15 -0800224 LogBootEvent("factory_reset_record_value", factory_reset_utc);
James Hawkins35349142016-02-16 15:05:54 -0800225 time_t time_since_factory_reset = difftime(current_time_utc,
226 factory_reset_utc);
227 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
228 time_since_factory_reset);
229}
230
James Hawkinsabd73e62016-01-19 15:10:38 -0800231} // namespace
232
233int main(int argc, char **argv) {
234 android::base::InitLogging(argv);
235
236 const std::string cmd_line = GetCommandLine(argc, argv);
237 LOG(INFO) << "Service started: " << cmd_line;
238
James Hawkins10f54be2016-02-09 15:32:38 -0800239 int option_index = 0;
James Hawkinseef069a2016-03-11 14:59:50 -0800240 static const char boot_complete_str[] = "record_boot_complete";
James Hawkins10f54be2016-02-09 15:32:38 -0800241 static const char boot_reason_str[] = "record_boot_reason";
James Hawkinsc0188da2016-02-23 16:18:19 -0800242 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkins10f54be2016-02-09 15:32:38 -0800243 static const struct option long_options[] = {
244 { "help", no_argument, NULL, 'h' },
245 { "log", no_argument, NULL, 'l' },
246 { "print", no_argument, NULL, 'p' },
247 { "record", required_argument, NULL, 'r' },
James Hawkinseef069a2016-03-11 14:59:50 -0800248 { boot_complete_str, no_argument, NULL, 0 },
James Hawkins10f54be2016-02-09 15:32:38 -0800249 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins35349142016-02-16 15:05:54 -0800250 { factory_reset_str, no_argument, NULL, 0 },
James Hawkins10f54be2016-02-09 15:32:38 -0800251 { NULL, 0, NULL, 0 }
252 };
253
James Hawkinsabd73e62016-01-19 15:10:38 -0800254 int opt = 0;
James Hawkins10f54be2016-02-09 15:32:38 -0800255 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800256 switch (opt) {
James Hawkins10f54be2016-02-09 15:32:38 -0800257 // This case handles long options which have no single-character mapping.
258 case 0: {
259 const std::string option_name = long_options[option_index].name;
James Hawkinseef069a2016-03-11 14:59:50 -0800260 if (option_name == boot_complete_str) {
261 RecordBootComplete();
262 } else if (option_name == boot_reason_str) {
James Hawkins10f54be2016-02-09 15:32:38 -0800263 RecordBootReason();
James Hawkins35349142016-02-16 15:05:54 -0800264 } else if (option_name == factory_reset_str) {
265 RecordFactoryReset();
James Hawkins10f54be2016-02-09 15:32:38 -0800266 } else {
267 LOG(ERROR) << "Invalid option: " << option_name;
268 }
269 break;
270 }
271
James Hawkinsabd73e62016-01-19 15:10:38 -0800272 case 'h': {
273 ShowHelp(argv[0]);
274 break;
275 }
276
277 case 'l': {
278 LogBootEvents();
279 break;
280 }
281
282 case 'p': {
283 PrintBootEvents();
284 break;
285 }
286
287 case 'r': {
288 // |optarg| is an external variable set by getopt representing
289 // the option argument.
290 const char* event = optarg;
291
292 BootEventRecordStore boot_event_store;
293 boot_event_store.AddBootEvent(event);
294 break;
295 }
296
297 default: {
298 DCHECK_EQ(opt, '?');
299
300 // |optopt| is an external variable set by getopt representing
301 // the value of the invalid option.
302 LOG(ERROR) << "Invalid option: " << optopt;
303 ShowHelp(argv[0]);
304 return EXIT_FAILURE;
305 }
306 }
307 }
308
309 return 0;
310}