blob: 9214e6ddff6f3a88ab7fc2fa2a899f75383b0d78 [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
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 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 Hawkins53684ea2016-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 Hawkinsa4a1a4a2016-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 Hawkins6f74c0b2016-02-12 15:49:16 -0800112constexpr int32_t kUnknownBootReason = 1;
113
James Hawkinsa4a1a4a2016-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 Hawkins6f74c0b2016-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 Hawkins45ead352016-03-08 16:42:07 -0800139 {"oem_pon1", 21},
140 {"oem_powerkey", 22},
141 {"oem_unknown_reset", 23},
142 {"srto: HWWDT reset SC", 24},
143 {"srto: HWWDT reset platform", 25},
144 {"srto: bootloader", 26},
145 {"srto: kernel panic", 27},
146 {"srto: kernel watchdog reset", 28},
147 {"srto: normal", 29},
148 {"srto: reboot", 30},
149 {"srto: reboot-bootloader", 31},
150 {"srto: security watchdog reset", 32},
151 {"srto: wakesrc", 33},
152 {"srto: watchdog", 34},
153 {"srto:1-1", 35},
154 {"srto:omap_hsmm", 36},
155 {"srto:phy0", 37},
156 {"srto:rtc0", 38},
157 {"srto:touchpad", 39},
158 {"watchdog", 40},
159 {"watchdogr", 41},
160 {"wdog_bark", 42},
161 {"wdog_bite", 43},
162 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800163};
164
165// Converts a string value representing the reason the system booted to an
166// integer representation. This is necessary for logging the boot_reason metric
167// via Tron, which does not accept non-integer buckets in histograms.
168int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800169 auto mapping = kBootReasonMap.find(boot_reason);
170 if (mapping != kBootReasonMap.end()) {
171 return mapping->second;
172 }
173
174 LOG(INFO) << "Unknown boot reason: " << boot_reason;
175 return kUnknownBootReason;
176}
177
James Hawkinsc08e9962016-03-11 14:59:50 -0800178// Records several metrics related to the time it takes to boot the device,
179// including disambiguating boot time on encrypted or non-encrypted devices.
180void RecordBootComplete() {
181 BootEventRecordStore boot_event_store;
182 time_t uptime = bootstat::ParseUptime();
183
184 BootEventRecordStore::BootEventRecord record;
185
186 // post_decrypt_time_elapsed is only logged on encrypted devices.
187 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
188 // Log the amount of time elapsed until the device is decrypted, which
189 // includes the variable amount of time the user takes to enter the
190 // decryption password.
191 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
192
193 // Subtract the decryption time to normalize the boot cycle timing.
194 time_t boot_complete = uptime - record.second;
195 boot_event_store.AddBootEventWithValue("boot_complete_post_decrypt",
196 boot_complete);
197
198
199 } else {
200 boot_event_store.AddBootEventWithValue("boot_complete_no_encryption",
201 uptime);
202 }
203
204 // Record the total time from device startup to boot complete, regardless of
205 // encryption state.
206 boot_event_store.AddBootEventWithValue("boot_complete", uptime);
207}
208
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800209// Records the boot_reason metric by querying the ro.boot.bootreason system
210// property.
211void RecordBootReason() {
212 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
213 BootEventRecordStore boot_event_store;
214 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
215}
216
James Hawkins500d7152016-02-16 15:05:54 -0800217// Records two metrics related to the user resetting a device: the time at
218// which the device is reset, and the time since the user last reset the
219// device. The former is only set once per-factory reset.
220void RecordFactoryReset() {
221 BootEventRecordStore boot_event_store;
222 BootEventRecordStore::BootEventRecord record;
223
224 time_t current_time_utc = time(nullptr);
225
James Hawkins0660b302016-03-08 16:18:15 -0800226 static const char* factory_reset_current_time = "factory_reset_current_time";
227 if (current_time_utc < 0) {
228 // UMA does not display negative values in buckets, so convert to positive.
229 LogBootEvent(factory_reset_current_time, std::abs(current_time_utc));
230 return;
231 } else {
232 LogBootEvent(factory_reset_current_time, current_time_utc);
233 }
234
James Hawkins500d7152016-02-16 15:05:54 -0800235 // The factory_reset boot event does not exist after the device is reset, so
236 // use this signal to mark the time of the factory reset.
237 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
238 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800239
240 // Don't log the time_since_factory_reset until some time has elapsed.
241 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800242 return;
243 }
244
245 // Calculate and record the difference in time between now and the
246 // factory_reset time.
247 time_t factory_reset_utc = record.second;
James Hawkins0660b302016-03-08 16:18:15 -0800248 LogBootEvent("factory_reset_record_value", factory_reset_utc);
James Hawkins500d7152016-02-16 15:05:54 -0800249 time_t time_since_factory_reset = difftime(current_time_utc,
250 factory_reset_utc);
251 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
252 time_since_factory_reset);
253}
254
James Hawkinsabd73e62016-01-19 15:10:38 -0800255} // namespace
256
257int main(int argc, char **argv) {
258 android::base::InitLogging(argv);
259
260 const std::string cmd_line = GetCommandLine(argc, argv);
261 LOG(INFO) << "Service started: " << cmd_line;
262
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800263 int option_index = 0;
James Hawkinsc08e9962016-03-11 14:59:50 -0800264 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800265 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800266 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800267 static const struct option long_options[] = {
268 { "help", no_argument, NULL, 'h' },
269 { "log", no_argument, NULL, 'l' },
270 { "print", no_argument, NULL, 'p' },
271 { "record", required_argument, NULL, 'r' },
James Hawkinsc08e9962016-03-11 14:59:50 -0800272 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800273 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800274 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800275 { NULL, 0, NULL, 0 }
276 };
277
James Hawkinsabd73e62016-01-19 15:10:38 -0800278 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800279 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800280 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800281 // This case handles long options which have no single-character mapping.
282 case 0: {
283 const std::string option_name = long_options[option_index].name;
James Hawkinsc08e9962016-03-11 14:59:50 -0800284 if (option_name == boot_complete_str) {
285 RecordBootComplete();
286 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800287 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800288 } else if (option_name == factory_reset_str) {
289 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800290 } else {
291 LOG(ERROR) << "Invalid option: " << option_name;
292 }
293 break;
294 }
295
James Hawkinsabd73e62016-01-19 15:10:38 -0800296 case 'h': {
297 ShowHelp(argv[0]);
298 break;
299 }
300
301 case 'l': {
302 LogBootEvents();
303 break;
304 }
305
306 case 'p': {
307 PrintBootEvents();
308 break;
309 }
310
311 case 'r': {
312 // |optarg| is an external variable set by getopt representing
313 // the option argument.
314 const char* event = optarg;
315
316 BootEventRecordStore boot_event_store;
317 boot_event_store.AddBootEvent(event);
318 break;
319 }
320
321 default: {
322 DCHECK_EQ(opt, '?');
323
324 // |optopt| is an external variable set by getopt representing
325 // the value of the invalid option.
326 LOG(ERROR) << "Invalid option: " << optopt;
327 ShowHelp(argv[0]);
328 return EXIT_FAILURE;
329 }
330 }
331 }
332
333 return 0;
334}