blob: 2639d0558a0c811012f9749210a6ee55d179c26f [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 Hawkins6f282992016-03-22 14:13:06 -070035#include "histogram_logger.h"
James Hawkinsc08e9962016-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 Hawkins6f282992016-03-22 14:13:06 -070047 bootstat::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080048 }
49}
50
James Hawkinsc6275582016-03-22 10:47:44 -070051// Records the named boot |event| to the record store. If |value| is non-empty
52// and is a proper string representation of an integer value, the converted
53// integer value is associated with the boot event.
54void RecordBootEventFromCommandLine(
55 const std::string& event, const std::string& value_str) {
56 BootEventRecordStore boot_event_store;
57 if (!value_str.empty()) {
58 int32_t value = 0;
59 value = std::stoi(value_str);
60 boot_event_store.AddBootEventWithValue(event, value);
61 } else {
62 boot_event_store.AddBootEvent(event);
63 }
64}
65
James Hawkinsabd73e62016-01-19 15:10:38 -080066void 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 Hawkinsc6275582016-03-22 10:47:44 -070085 " --value Optional value to associate with the boot event\n"
James Hawkins53684ea2016-02-23 16:18:19 -080086 " --record_boot_reason Record the reason why the device booted\n"
87 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080088}
89
90// Constructs a readable, printable string from the givencommand line
91// arguments.
92std::string GetCommandLine(int argc, char **argv) {
93 std::string cmd;
94 for (int i = 0; i < argc; ++i) {
95 cmd += argv[i];
96 cmd += " ";
97 }
98
99 return cmd;
100}
101
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800102// Convenience wrapper over the property API that returns an
103// std::string.
104std::string GetProperty(const char* key) {
105 std::vector<char> temp(PROPERTY_VALUE_MAX);
106 const int len = property_get(key, &temp[0], nullptr);
107 if (len < 0) {
108 return "";
109 }
110 return std::string(&temp[0], len);
111}
112
James Hawkins6f74c0b2016-02-12 15:49:16 -0800113constexpr int32_t kUnknownBootReason = 1;
114
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800115// A mapping from boot reason string, as read from the ro.boot.bootreason
116// system property, to a unique integer ID. Viewers of log data dashboards for
117// the boot_reason metric may refer to this mapping to discern the histogram
118// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800119const std::map<std::string, int32_t> kBootReasonMap = {
120 {"unknown", kUnknownBootReason},
121 {"normal", 2},
122 {"recovery", 3},
123 {"reboot", 4},
124 {"PowerKey", 5},
125 {"hard_reset", 6},
126 {"kernel_panic", 7},
127 {"rpm_err", 8},
128 {"hw_reset", 9},
129 {"tz_err", 10},
130 {"adsp_err", 11},
131 {"modem_err", 12},
132 {"mba_err", 13},
133 {"Watchdog", 14},
134 {"Panic", 15},
135 {"power_key", 16},
136 {"power_on", 17},
137 {"Reboot", 18},
138 {"rtc", 19},
139 {"edl", 20},
James Hawkins45ead352016-03-08 16:42:07 -0800140 {"oem_pon1", 21},
141 {"oem_powerkey", 22},
142 {"oem_unknown_reset", 23},
143 {"srto: HWWDT reset SC", 24},
144 {"srto: HWWDT reset platform", 25},
145 {"srto: bootloader", 26},
146 {"srto: kernel panic", 27},
147 {"srto: kernel watchdog reset", 28},
148 {"srto: normal", 29},
149 {"srto: reboot", 30},
150 {"srto: reboot-bootloader", 31},
151 {"srto: security watchdog reset", 32},
152 {"srto: wakesrc", 33},
153 {"srto: watchdog", 34},
154 {"srto:1-1", 35},
155 {"srto:omap_hsmm", 36},
156 {"srto:phy0", 37},
157 {"srto:rtc0", 38},
158 {"srto:touchpad", 39},
159 {"watchdog", 40},
160 {"watchdogr", 41},
161 {"wdog_bark", 42},
162 {"wdog_bite", 43},
163 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800164};
165
166// Converts a string value representing the reason the system booted to an
167// integer representation. This is necessary for logging the boot_reason metric
168// via Tron, which does not accept non-integer buckets in histograms.
169int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800170 auto mapping = kBootReasonMap.find(boot_reason);
171 if (mapping != kBootReasonMap.end()) {
172 return mapping->second;
173 }
174
175 LOG(INFO) << "Unknown boot reason: " << boot_reason;
176 return kUnknownBootReason;
177}
178
James Hawkinsc08e9962016-03-11 14:59:50 -0800179// Records several metrics related to the time it takes to boot the device,
180// including disambiguating boot time on encrypted or non-encrypted devices.
181void RecordBootComplete() {
182 BootEventRecordStore boot_event_store;
183 time_t uptime = bootstat::ParseUptime();
184
185 BootEventRecordStore::BootEventRecord record;
186
187 // post_decrypt_time_elapsed is only logged on encrypted devices.
188 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
189 // Log the amount of time elapsed until the device is decrypted, which
190 // includes the variable amount of time the user takes to enter the
191 // decryption password.
192 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
193
194 // Subtract the decryption time to normalize the boot cycle timing.
195 time_t boot_complete = uptime - record.second;
196 boot_event_store.AddBootEventWithValue("boot_complete_post_decrypt",
197 boot_complete);
198
199
200 } else {
201 boot_event_store.AddBootEventWithValue("boot_complete_no_encryption",
202 uptime);
203 }
204
205 // Record the total time from device startup to boot complete, regardless of
206 // encryption state.
207 boot_event_store.AddBootEventWithValue("boot_complete", uptime);
208}
209
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800210// Records the boot_reason metric by querying the ro.boot.bootreason system
211// property.
212void RecordBootReason() {
213 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
214 BootEventRecordStore boot_event_store;
215 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
216}
217
James Hawkins500d7152016-02-16 15:05:54 -0800218// Records two metrics related to the user resetting a device: the time at
219// which the device is reset, and the time since the user last reset the
220// device. The former is only set once per-factory reset.
221void RecordFactoryReset() {
222 BootEventRecordStore boot_event_store;
223 BootEventRecordStore::BootEventRecord record;
224
225 time_t current_time_utc = time(nullptr);
226
James Hawkins0660b302016-03-08 16:18:15 -0800227 static const char* factory_reset_current_time = "factory_reset_current_time";
228 if (current_time_utc < 0) {
229 // UMA does not display negative values in buckets, so convert to positive.
James Hawkins6f282992016-03-22 14:13:06 -0700230 bootstat::LogHistogram(factory_reset_current_time, std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800231 return;
232 } else {
James Hawkins6f282992016-03-22 14:13:06 -0700233 bootstat::LogHistogram(factory_reset_current_time, current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800234 }
235
James Hawkins500d7152016-02-16 15:05:54 -0800236 // The factory_reset boot event does not exist after the device is reset, so
237 // use this signal to mark the time of the factory reset.
238 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
239 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800240
241 // Don't log the time_since_factory_reset until some time has elapsed.
242 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800243 return;
244 }
245
246 // Calculate and record the difference in time between now and the
247 // factory_reset time.
248 time_t factory_reset_utc = record.second;
James Hawkins6f282992016-03-22 14:13:06 -0700249 bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkins500d7152016-02-16 15:05:54 -0800250 time_t time_since_factory_reset = difftime(current_time_utc,
251 factory_reset_utc);
252 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
253 time_since_factory_reset);
254}
255
James Hawkinsabd73e62016-01-19 15:10:38 -0800256} // namespace
257
258int main(int argc, char **argv) {
259 android::base::InitLogging(argv);
260
261 const std::string cmd_line = GetCommandLine(argc, argv);
262 LOG(INFO) << "Service started: " << cmd_line;
263
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800264 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700265 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800266 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800267 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800268 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800269 static const struct option long_options[] = {
270 { "help", no_argument, NULL, 'h' },
271 { "log", no_argument, NULL, 'l' },
272 { "print", no_argument, NULL, 'p' },
273 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700274 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800275 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800276 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800277 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800278 { NULL, 0, NULL, 0 }
279 };
280
James Hawkinsc6275582016-03-22 10:47:44 -0700281 std::string boot_event;
282 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800283 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800284 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800285 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800286 // This case handles long options which have no single-character mapping.
287 case 0: {
288 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700289 if (option_name == value_str) {
290 // |optarg| is an external variable set by getopt representing
291 // the option argument.
292 value = optarg;
293 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800294 RecordBootComplete();
295 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800296 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800297 } else if (option_name == factory_reset_str) {
298 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800299 } else {
300 LOG(ERROR) << "Invalid option: " << option_name;
301 }
302 break;
303 }
304
James Hawkinsabd73e62016-01-19 15:10:38 -0800305 case 'h': {
306 ShowHelp(argv[0]);
307 break;
308 }
309
310 case 'l': {
311 LogBootEvents();
312 break;
313 }
314
315 case 'p': {
316 PrintBootEvents();
317 break;
318 }
319
320 case 'r': {
321 // |optarg| is an external variable set by getopt representing
322 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700323 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800324 break;
325 }
326
327 default: {
328 DCHECK_EQ(opt, '?');
329
330 // |optopt| is an external variable set by getopt representing
331 // the value of the invalid option.
332 LOG(ERROR) << "Invalid option: " << optopt;
333 ShowHelp(argv[0]);
334 return EXIT_FAILURE;
335 }
336 }
337 }
338
James Hawkinsc6275582016-03-22 10:47:44 -0700339 if (!boot_event.empty()) {
340 RecordBootEventFromCommandLine(boot_event, value);
341 }
342
James Hawkinsabd73e62016-01-19 15:10:38 -0800343 return 0;
344}