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