blob: 1d16f69e2a5e1e2b958d1bcfb15dd93d2b90832e [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 Hawkinsa4a1a4a2016-02-09 15:32:38 -080025#include <map>
James Hawkinsabd73e62016-01-19 15:10:38 -080026#include <memory>
27#include <string>
James Hawkinseabe08b2016-01-19 16:54:35 -080028#include <android-base/logging.h>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080029#include <cutils/properties.h>
James Hawkinsabd73e62016-01-19 15:10:38 -080030#include <log/log.h>
31#include "boot_event_record_store.h"
32#include "event_log_list_builder.h"
33
34namespace {
35
36// Builds an EventLog buffer named |event| containing |data| and writes
37// the log into the Tron histogram logs.
38void LogBootEvent(const std::string& event, int32_t data) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080039 LOG(INFO) << "Logging boot metric: " << event << " " << data;
James Hawkinsabd73e62016-01-19 15:10:38 -080040
41 EventLogListBuilder log_builder;
42 log_builder.Append(event);
43 log_builder.Append(data);
44
45 std::unique_ptr<uint8_t[]> log;
46 size_t size;
47 log_builder.Release(&log, &size);
48
49 android_bWriteLog(HISTOGRAM_LOG_TAG, log.get(), size);
50}
51
52// Scans the boot event record store for record files and logs each boot event
53// via EventLog.
54void LogBootEvents() {
55 BootEventRecordStore boot_event_store;
56
57 auto events = boot_event_store.GetAllBootEvents();
58 for (auto i = events.cbegin(); i != events.cend(); ++i) {
59 LogBootEvent(i->first, i->second);
60 }
61}
62
63void PrintBootEvents() {
64 printf("Boot events:\n");
65 printf("------------\n");
66
67 BootEventRecordStore boot_event_store;
68 auto events = boot_event_store.GetAllBootEvents();
69 for (auto i = events.cbegin(); i != events.cend(); ++i) {
70 printf("%s\t%d\n", i->first.c_str(), i->second);
71 }
72}
73
74void ShowHelp(const char *cmd) {
75 fprintf(stderr, "Usage: %s [options]\n", cmd);
76 fprintf(stderr,
77 "options include:\n"
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080078 " -h, --help Show this help\n"
79 " -l, --log Log all metrics to logstorage\n"
80 " -p, --print Dump the boot event records to the console\n"
81 " -r, --record Record the timestamp of a named boot event\n"
82 " --record_boot_reason Record the reason why the device booted\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080083}
84
85// Constructs a readable, printable string from the givencommand line
86// arguments.
87std::string GetCommandLine(int argc, char **argv) {
88 std::string cmd;
89 for (int i = 0; i < argc; ++i) {
90 cmd += argv[i];
91 cmd += " ";
92 }
93
94 return cmd;
95}
96
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080097// Convenience wrapper over the property API that returns an
98// std::string.
99std::string GetProperty(const char* key) {
100 std::vector<char> temp(PROPERTY_VALUE_MAX);
101 const int len = property_get(key, &temp[0], nullptr);
102 if (len < 0) {
103 return "";
104 }
105 return std::string(&temp[0], len);
106}
107
James Hawkins6f74c0b2016-02-12 15:49:16 -0800108constexpr int32_t kUnknownBootReason = 1;
109
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800110// A mapping from boot reason string, as read from the ro.boot.bootreason
111// system property, to a unique integer ID. Viewers of log data dashboards for
112// the boot_reason metric may refer to this mapping to discern the histogram
113// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800114const std::map<std::string, int32_t> kBootReasonMap = {
115 {"unknown", kUnknownBootReason},
116 {"normal", 2},
117 {"recovery", 3},
118 {"reboot", 4},
119 {"PowerKey", 5},
120 {"hard_reset", 6},
121 {"kernel_panic", 7},
122 {"rpm_err", 8},
123 {"hw_reset", 9},
124 {"tz_err", 10},
125 {"adsp_err", 11},
126 {"modem_err", 12},
127 {"mba_err", 13},
128 {"Watchdog", 14},
129 {"Panic", 15},
130 {"power_key", 16},
131 {"power_on", 17},
132 {"Reboot", 18},
133 {"rtc", 19},
134 {"edl", 20},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800135};
136
137// Converts a string value representing the reason the system booted to an
138// integer representation. This is necessary for logging the boot_reason metric
139// via Tron, which does not accept non-integer buckets in histograms.
140int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800141 auto mapping = kBootReasonMap.find(boot_reason);
142 if (mapping != kBootReasonMap.end()) {
143 return mapping->second;
144 }
145
146 LOG(INFO) << "Unknown boot reason: " << boot_reason;
147 return kUnknownBootReason;
148}
149
150// Records the boot_reason metric by querying the ro.boot.bootreason system
151// property.
152void RecordBootReason() {
153 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
154 BootEventRecordStore boot_event_store;
155 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
156}
157
James Hawkinsabd73e62016-01-19 15:10:38 -0800158} // namespace
159
160int main(int argc, char **argv) {
161 android::base::InitLogging(argv);
162
163 const std::string cmd_line = GetCommandLine(argc, argv);
164 LOG(INFO) << "Service started: " << cmd_line;
165
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800166 int option_index = 0;
167 static const char boot_reason_str[] = "record_boot_reason";
168 static const struct option long_options[] = {
169 { "help", no_argument, NULL, 'h' },
170 { "log", no_argument, NULL, 'l' },
171 { "print", no_argument, NULL, 'p' },
172 { "record", required_argument, NULL, 'r' },
173 { boot_reason_str, no_argument, NULL, 0 },
174 { NULL, 0, NULL, 0 }
175 };
176
James Hawkinsabd73e62016-01-19 15:10:38 -0800177 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800178 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800179 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800180 // This case handles long options which have no single-character mapping.
181 case 0: {
182 const std::string option_name = long_options[option_index].name;
183 if (option_name == boot_reason_str) {
184 RecordBootReason();
185 } else {
186 LOG(ERROR) << "Invalid option: " << option_name;
187 }
188 break;
189 }
190
James Hawkinsabd73e62016-01-19 15:10:38 -0800191 case 'h': {
192 ShowHelp(argv[0]);
193 break;
194 }
195
196 case 'l': {
197 LogBootEvents();
198 break;
199 }
200
201 case 'p': {
202 PrintBootEvents();
203 break;
204 }
205
206 case 'r': {
207 // |optarg| is an external variable set by getopt representing
208 // the option argument.
209 const char* event = optarg;
210
211 BootEventRecordStore boot_event_store;
212 boot_event_store.AddBootEvent(event);
213 break;
214 }
215
216 default: {
217 DCHECK_EQ(opt, '?');
218
219 // |optopt| is an external variable set by getopt representing
220 // the value of the invalid option.
221 LOG(ERROR) << "Invalid option: " << optopt;
222 ShowHelp(argv[0]);
223 return EXIT_FAILURE;
224 }
225 }
226 }
227
228 return 0;
229}