blob: b39925caeb82b416bc5b5d91aba83b33a33239a3 [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>
23#include <cstddef>
24#include <cstdio>
James Hawkins500d7152016-02-16 15:05:54 -080025#include <ctime>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080026#include <map>
James Hawkinsabd73e62016-01-19 15:10:38 -080027#include <memory>
28#include <string>
James Hawkinseabe08b2016-01-19 16:54:35 -080029#include <android-base/logging.h>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080030#include <cutils/properties.h>
James Hawkinsabd73e62016-01-19 15:10:38 -080031#include <log/log.h>
32#include "boot_event_record_store.h"
33#include "event_log_list_builder.h"
34
35namespace {
36
37// Builds an EventLog buffer named |event| containing |data| and writes
38// the log into the Tron histogram logs.
39void LogBootEvent(const std::string& event, int32_t data) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080040 LOG(INFO) << "Logging boot metric: " << event << " " << data;
James Hawkinsabd73e62016-01-19 15:10:38 -080041
42 EventLogListBuilder log_builder;
43 log_builder.Append(event);
44 log_builder.Append(data);
45
46 std::unique_ptr<uint8_t[]> log;
47 size_t size;
48 log_builder.Release(&log, &size);
49
50 android_bWriteLog(HISTOGRAM_LOG_TAG, log.get(), size);
51}
52
53// Scans the boot event record store for record files and logs each boot event
54// via EventLog.
55void LogBootEvents() {
56 BootEventRecordStore boot_event_store;
57
58 auto events = boot_event_store.GetAllBootEvents();
59 for (auto i = events.cbegin(); i != events.cend(); ++i) {
60 LogBootEvent(i->first, i->second);
61 }
62}
63
64void PrintBootEvents() {
65 printf("Boot events:\n");
66 printf("------------\n");
67
68 BootEventRecordStore boot_event_store;
69 auto events = boot_event_store.GetAllBootEvents();
70 for (auto i = events.cbegin(); i != events.cend(); ++i) {
71 printf("%s\t%d\n", i->first.c_str(), i->second);
72 }
73}
74
75void ShowHelp(const char *cmd) {
76 fprintf(stderr, "Usage: %s [options]\n", cmd);
77 fprintf(stderr,
78 "options include:\n"
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080079 " -h, --help Show this help\n"
80 " -l, --log Log all metrics to logstorage\n"
81 " -p, --print Dump the boot event records to the console\n"
82 " -r, --record Record the timestamp of a named boot event\n"
James Hawkins53684ea2016-02-23 16:18:19 -080083 " --record_boot_reason Record the reason why the device booted\n"
84 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080085}
86
87// Constructs a readable, printable string from the givencommand line
88// arguments.
89std::string GetCommandLine(int argc, char **argv) {
90 std::string cmd;
91 for (int i = 0; i < argc; ++i) {
92 cmd += argv[i];
93 cmd += " ";
94 }
95
96 return cmd;
97}
98
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080099// Convenience wrapper over the property API that returns an
100// std::string.
101std::string GetProperty(const char* key) {
102 std::vector<char> temp(PROPERTY_VALUE_MAX);
103 const int len = property_get(key, &temp[0], nullptr);
104 if (len < 0) {
105 return "";
106 }
107 return std::string(&temp[0], len);
108}
109
James Hawkins6f74c0b2016-02-12 15:49:16 -0800110constexpr int32_t kUnknownBootReason = 1;
111
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800112// A mapping from boot reason string, as read from the ro.boot.bootreason
113// system property, to a unique integer ID. Viewers of log data dashboards for
114// the boot_reason metric may refer to this mapping to discern the histogram
115// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800116const std::map<std::string, int32_t> kBootReasonMap = {
117 {"unknown", kUnknownBootReason},
118 {"normal", 2},
119 {"recovery", 3},
120 {"reboot", 4},
121 {"PowerKey", 5},
122 {"hard_reset", 6},
123 {"kernel_panic", 7},
124 {"rpm_err", 8},
125 {"hw_reset", 9},
126 {"tz_err", 10},
127 {"adsp_err", 11},
128 {"modem_err", 12},
129 {"mba_err", 13},
130 {"Watchdog", 14},
131 {"Panic", 15},
132 {"power_key", 16},
133 {"power_on", 17},
134 {"Reboot", 18},
135 {"rtc", 19},
136 {"edl", 20},
James Hawkins45ead352016-03-08 16:42:07 -0800137 {"oem_pon1", 21},
138 {"oem_powerkey", 22},
139 {"oem_unknown_reset", 23},
140 {"srto: HWWDT reset SC", 24},
141 {"srto: HWWDT reset platform", 25},
142 {"srto: bootloader", 26},
143 {"srto: kernel panic", 27},
144 {"srto: kernel watchdog reset", 28},
145 {"srto: normal", 29},
146 {"srto: reboot", 30},
147 {"srto: reboot-bootloader", 31},
148 {"srto: security watchdog reset", 32},
149 {"srto: wakesrc", 33},
150 {"srto: watchdog", 34},
151 {"srto:1-1", 35},
152 {"srto:omap_hsmm", 36},
153 {"srto:phy0", 37},
154 {"srto:rtc0", 38},
155 {"srto:touchpad", 39},
156 {"watchdog", 40},
157 {"watchdogr", 41},
158 {"wdog_bark", 42},
159 {"wdog_bite", 43},
160 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800161};
162
163// Converts a string value representing the reason the system booted to an
164// integer representation. This is necessary for logging the boot_reason metric
165// via Tron, which does not accept non-integer buckets in histograms.
166int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800167 auto mapping = kBootReasonMap.find(boot_reason);
168 if (mapping != kBootReasonMap.end()) {
169 return mapping->second;
170 }
171
172 LOG(INFO) << "Unknown boot reason: " << boot_reason;
173 return kUnknownBootReason;
174}
175
176// Records the boot_reason metric by querying the ro.boot.bootreason system
177// property.
178void RecordBootReason() {
179 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
180 BootEventRecordStore boot_event_store;
181 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
182}
183
James Hawkins500d7152016-02-16 15:05:54 -0800184// Records two metrics related to the user resetting a device: the time at
185// which the device is reset, and the time since the user last reset the
186// device. The former is only set once per-factory reset.
187void RecordFactoryReset() {
188 BootEventRecordStore boot_event_store;
189 BootEventRecordStore::BootEventRecord record;
190
191 time_t current_time_utc = time(nullptr);
192
193 // The factory_reset boot event does not exist after the device is reset, so
194 // use this signal to mark the time of the factory reset.
195 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
196 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800197
198 // Don't log the time_since_factory_reset until some time has elapsed.
199 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800200 return;
201 }
202
203 // Calculate and record the difference in time between now and the
204 // factory_reset time.
205 time_t factory_reset_utc = record.second;
206 time_t time_since_factory_reset = difftime(current_time_utc,
207 factory_reset_utc);
208 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
209 time_since_factory_reset);
210}
211
James Hawkinsabd73e62016-01-19 15:10:38 -0800212} // namespace
213
214int main(int argc, char **argv) {
215 android::base::InitLogging(argv);
216
217 const std::string cmd_line = GetCommandLine(argc, argv);
218 LOG(INFO) << "Service started: " << cmd_line;
219
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800220 int option_index = 0;
221 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800222 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800223 static const struct option long_options[] = {
224 { "help", no_argument, NULL, 'h' },
225 { "log", no_argument, NULL, 'l' },
226 { "print", no_argument, NULL, 'p' },
227 { "record", required_argument, NULL, 'r' },
228 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800229 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800230 { NULL, 0, NULL, 0 }
231 };
232
James Hawkinsabd73e62016-01-19 15:10:38 -0800233 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800234 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800235 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800236 // This case handles long options which have no single-character mapping.
237 case 0: {
238 const std::string option_name = long_options[option_index].name;
239 if (option_name == boot_reason_str) {
240 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800241 } else if (option_name == factory_reset_str) {
242 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800243 } else {
244 LOG(ERROR) << "Invalid option: " << option_name;
245 }
246 break;
247 }
248
James Hawkinsabd73e62016-01-19 15:10:38 -0800249 case 'h': {
250 ShowHelp(argv[0]);
251 break;
252 }
253
254 case 'l': {
255 LogBootEvents();
256 break;
257 }
258
259 case 'p': {
260 PrintBootEvents();
261 break;
262 }
263
264 case 'r': {
265 // |optarg| is an external variable set by getopt representing
266 // the option argument.
267 const char* event = optarg;
268
269 BootEventRecordStore boot_event_store;
270 boot_event_store.AddBootEvent(event);
271 break;
272 }
273
274 default: {
275 DCHECK_EQ(opt, '?');
276
277 // |optopt| is an external variable set by getopt representing
278 // the value of the invalid option.
279 LOG(ERROR) << "Invalid option: " << optopt;
280 ShowHelp(argv[0]);
281 return EXIT_FAILURE;
282 }
283 }
284 }
285
286 return 0;
287}