blob: e0548447dff1e9ce223dd9aa5845f5e0816bfd15 [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 Hawkinsc08e9962016-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 Hawkinsa4a1a4a2016-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
James Hawkinsc6275582016-03-22 10:47:44 -070066// Records the named boot |event| to the record store. If |value| is non-empty
67// and is a proper string representation of an integer value, the converted
68// integer value is associated with the boot event.
69void RecordBootEventFromCommandLine(
70 const std::string& event, const std::string& value_str) {
71 BootEventRecordStore boot_event_store;
72 if (!value_str.empty()) {
73 int32_t value = 0;
74 value = std::stoi(value_str);
75 boot_event_store.AddBootEventWithValue(event, value);
76 } else {
77 boot_event_store.AddBootEvent(event);
78 }
79}
80
James Hawkinsabd73e62016-01-19 15:10:38 -080081void PrintBootEvents() {
82 printf("Boot events:\n");
83 printf("------------\n");
84
85 BootEventRecordStore boot_event_store;
86 auto events = boot_event_store.GetAllBootEvents();
87 for (auto i = events.cbegin(); i != events.cend(); ++i) {
88 printf("%s\t%d\n", i->first.c_str(), i->second);
89 }
90}
91
92void ShowHelp(const char *cmd) {
93 fprintf(stderr, "Usage: %s [options]\n", cmd);
94 fprintf(stderr,
95 "options include:\n"
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080096 " -h, --help Show this help\n"
97 " -l, --log Log all metrics to logstorage\n"
98 " -p, --print Dump the boot event records to the console\n"
99 " -r, --record Record the timestamp of a named boot event\n"
James Hawkinsc6275582016-03-22 10:47:44 -0700100 " --value Optional value to associate with the boot event\n"
James Hawkins53684ea2016-02-23 16:18:19 -0800101 " --record_boot_reason Record the reason why the device booted\n"
102 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -0800103}
104
105// Constructs a readable, printable string from the givencommand line
106// arguments.
107std::string GetCommandLine(int argc, char **argv) {
108 std::string cmd;
109 for (int i = 0; i < argc; ++i) {
110 cmd += argv[i];
111 cmd += " ";
112 }
113
114 return cmd;
115}
116
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800117// Convenience wrapper over the property API that returns an
118// std::string.
119std::string GetProperty(const char* key) {
120 std::vector<char> temp(PROPERTY_VALUE_MAX);
121 const int len = property_get(key, &temp[0], nullptr);
122 if (len < 0) {
123 return "";
124 }
125 return std::string(&temp[0], len);
126}
127
James Hawkins6f74c0b2016-02-12 15:49:16 -0800128constexpr int32_t kUnknownBootReason = 1;
129
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800130// A mapping from boot reason string, as read from the ro.boot.bootreason
131// system property, to a unique integer ID. Viewers of log data dashboards for
132// the boot_reason metric may refer to this mapping to discern the histogram
133// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800134const std::map<std::string, int32_t> kBootReasonMap = {
135 {"unknown", kUnknownBootReason},
136 {"normal", 2},
137 {"recovery", 3},
138 {"reboot", 4},
139 {"PowerKey", 5},
140 {"hard_reset", 6},
141 {"kernel_panic", 7},
142 {"rpm_err", 8},
143 {"hw_reset", 9},
144 {"tz_err", 10},
145 {"adsp_err", 11},
146 {"modem_err", 12},
147 {"mba_err", 13},
148 {"Watchdog", 14},
149 {"Panic", 15},
150 {"power_key", 16},
151 {"power_on", 17},
152 {"Reboot", 18},
153 {"rtc", 19},
154 {"edl", 20},
James Hawkins45ead352016-03-08 16:42:07 -0800155 {"oem_pon1", 21},
156 {"oem_powerkey", 22},
157 {"oem_unknown_reset", 23},
158 {"srto: HWWDT reset SC", 24},
159 {"srto: HWWDT reset platform", 25},
160 {"srto: bootloader", 26},
161 {"srto: kernel panic", 27},
162 {"srto: kernel watchdog reset", 28},
163 {"srto: normal", 29},
164 {"srto: reboot", 30},
165 {"srto: reboot-bootloader", 31},
166 {"srto: security watchdog reset", 32},
167 {"srto: wakesrc", 33},
168 {"srto: watchdog", 34},
169 {"srto:1-1", 35},
170 {"srto:omap_hsmm", 36},
171 {"srto:phy0", 37},
172 {"srto:rtc0", 38},
173 {"srto:touchpad", 39},
174 {"watchdog", 40},
175 {"watchdogr", 41},
176 {"wdog_bark", 42},
177 {"wdog_bite", 43},
178 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800179};
180
181// Converts a string value representing the reason the system booted to an
182// integer representation. This is necessary for logging the boot_reason metric
183// via Tron, which does not accept non-integer buckets in histograms.
184int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800185 auto mapping = kBootReasonMap.find(boot_reason);
186 if (mapping != kBootReasonMap.end()) {
187 return mapping->second;
188 }
189
190 LOG(INFO) << "Unknown boot reason: " << boot_reason;
191 return kUnknownBootReason;
192}
193
James Hawkinsc08e9962016-03-11 14:59:50 -0800194// Records several metrics related to the time it takes to boot the device,
195// including disambiguating boot time on encrypted or non-encrypted devices.
196void RecordBootComplete() {
197 BootEventRecordStore boot_event_store;
198 time_t uptime = bootstat::ParseUptime();
199
200 BootEventRecordStore::BootEventRecord record;
201
202 // post_decrypt_time_elapsed is only logged on encrypted devices.
203 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
204 // Log the amount of time elapsed until the device is decrypted, which
205 // includes the variable amount of time the user takes to enter the
206 // decryption password.
207 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
208
209 // Subtract the decryption time to normalize the boot cycle timing.
210 time_t boot_complete = uptime - record.second;
211 boot_event_store.AddBootEventWithValue("boot_complete_post_decrypt",
212 boot_complete);
213
214
215 } else {
216 boot_event_store.AddBootEventWithValue("boot_complete_no_encryption",
217 uptime);
218 }
219
220 // Record the total time from device startup to boot complete, regardless of
221 // encryption state.
222 boot_event_store.AddBootEventWithValue("boot_complete", uptime);
223}
224
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800225// Records the boot_reason metric by querying the ro.boot.bootreason system
226// property.
227void RecordBootReason() {
228 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
229 BootEventRecordStore boot_event_store;
230 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
231}
232
James Hawkins500d7152016-02-16 15:05:54 -0800233// Records two metrics related to the user resetting a device: the time at
234// which the device is reset, and the time since the user last reset the
235// device. The former is only set once per-factory reset.
236void RecordFactoryReset() {
237 BootEventRecordStore boot_event_store;
238 BootEventRecordStore::BootEventRecord record;
239
240 time_t current_time_utc = time(nullptr);
241
James Hawkins0660b302016-03-08 16:18:15 -0800242 static const char* factory_reset_current_time = "factory_reset_current_time";
243 if (current_time_utc < 0) {
244 // UMA does not display negative values in buckets, so convert to positive.
245 LogBootEvent(factory_reset_current_time, std::abs(current_time_utc));
246 return;
247 } else {
248 LogBootEvent(factory_reset_current_time, current_time_utc);
249 }
250
James Hawkins500d7152016-02-16 15:05:54 -0800251 // The factory_reset boot event does not exist after the device is reset, so
252 // use this signal to mark the time of the factory reset.
253 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
254 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800255
256 // Don't log the time_since_factory_reset until some time has elapsed.
257 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800258 return;
259 }
260
261 // Calculate and record the difference in time between now and the
262 // factory_reset time.
263 time_t factory_reset_utc = record.second;
James Hawkins0660b302016-03-08 16:18:15 -0800264 LogBootEvent("factory_reset_record_value", factory_reset_utc);
James Hawkins500d7152016-02-16 15:05:54 -0800265 time_t time_since_factory_reset = difftime(current_time_utc,
266 factory_reset_utc);
267 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
268 time_since_factory_reset);
269}
270
James Hawkinsabd73e62016-01-19 15:10:38 -0800271} // namespace
272
273int main(int argc, char **argv) {
274 android::base::InitLogging(argv);
275
276 const std::string cmd_line = GetCommandLine(argc, argv);
277 LOG(INFO) << "Service started: " << cmd_line;
278
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800279 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700280 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800281 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800282 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800283 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800284 static const struct option long_options[] = {
285 { "help", no_argument, NULL, 'h' },
286 { "log", no_argument, NULL, 'l' },
287 { "print", no_argument, NULL, 'p' },
288 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700289 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800290 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800291 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800292 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800293 { NULL, 0, NULL, 0 }
294 };
295
James Hawkinsc6275582016-03-22 10:47:44 -0700296 std::string boot_event;
297 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800298 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800299 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800300 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800301 // This case handles long options which have no single-character mapping.
302 case 0: {
303 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700304 if (option_name == value_str) {
305 // |optarg| is an external variable set by getopt representing
306 // the option argument.
307 value = optarg;
308 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800309 RecordBootComplete();
310 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800311 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800312 } else if (option_name == factory_reset_str) {
313 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800314 } else {
315 LOG(ERROR) << "Invalid option: " << option_name;
316 }
317 break;
318 }
319
James Hawkinsabd73e62016-01-19 15:10:38 -0800320 case 'h': {
321 ShowHelp(argv[0]);
322 break;
323 }
324
325 case 'l': {
326 LogBootEvents();
327 break;
328 }
329
330 case 'p': {
331 PrintBootEvents();
332 break;
333 }
334
335 case 'r': {
336 // |optarg| is an external variable set by getopt representing
337 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700338 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800339 break;
340 }
341
342 default: {
343 DCHECK_EQ(opt, '?');
344
345 // |optopt| is an external variable set by getopt representing
346 // the value of the invalid option.
347 LOG(ERROR) << "Invalid option: " << optopt;
348 ShowHelp(argv[0]);
349 return EXIT_FAILURE;
350 }
351 }
352 }
353
James Hawkinsc6275582016-03-22 10:47:44 -0700354 if (!boot_event.empty()) {
355 RecordBootEventFromCommandLine(boot_event, value);
356 }
357
James Hawkinsabd73e62016-01-19 15:10:38 -0800358 return 0;
359}