blob: 31c84c7537ac0250f066ffa11d840375d2eb20a8 [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
108// A mapping from boot reason string, as read from the ro.boot.bootreason
109// system property, to a unique integer ID. Viewers of log data dashboards for
110// the boot_reason metric may refer to this mapping to discern the histogram
111// values.
112const std::map<std::string, int> kBootReasonMap = {
113 {"normal", 0},
114 {"recovery", 1},
115 {"reboot", 2},
116 {"PowerKey", 3},
117 {"hard_reset", 4},
118 {"kernel_panic", 5},
119 {"rpm_err", 6},
120 {"hw_reset", 7},
121 {"tz_err", 8},
122 {"adsp_err", 9},
123 {"modem_err", 10},
124 {"mba_err", 11},
125 {"Watchdog", 12},
126 {"Panic", 13},
127};
128
129// Converts a string value representing the reason the system booted to an
130// integer representation. This is necessary for logging the boot_reason metric
131// via Tron, which does not accept non-integer buckets in histograms.
132int32_t BootReasonStrToEnum(const std::string& boot_reason) {
133 static const int32_t kUnknownBootReason = -1;
134
135 auto mapping = kBootReasonMap.find(boot_reason);
136 if (mapping != kBootReasonMap.end()) {
137 return mapping->second;
138 }
139
140 LOG(INFO) << "Unknown boot reason: " << boot_reason;
141 return kUnknownBootReason;
142}
143
144// Records the boot_reason metric by querying the ro.boot.bootreason system
145// property.
146void RecordBootReason() {
147 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
148 BootEventRecordStore boot_event_store;
149 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
150}
151
James Hawkinsabd73e62016-01-19 15:10:38 -0800152} // namespace
153
154int main(int argc, char **argv) {
155 android::base::InitLogging(argv);
156
157 const std::string cmd_line = GetCommandLine(argc, argv);
158 LOG(INFO) << "Service started: " << cmd_line;
159
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800160 int option_index = 0;
161 static const char boot_reason_str[] = "record_boot_reason";
162 static const struct option long_options[] = {
163 { "help", no_argument, NULL, 'h' },
164 { "log", no_argument, NULL, 'l' },
165 { "print", no_argument, NULL, 'p' },
166 { "record", required_argument, NULL, 'r' },
167 { boot_reason_str, no_argument, NULL, 0 },
168 { NULL, 0, NULL, 0 }
169 };
170
James Hawkinsabd73e62016-01-19 15:10:38 -0800171 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800172 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800173 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800174 // This case handles long options which have no single-character mapping.
175 case 0: {
176 const std::string option_name = long_options[option_index].name;
177 if (option_name == boot_reason_str) {
178 RecordBootReason();
179 } else {
180 LOG(ERROR) << "Invalid option: " << option_name;
181 }
182 break;
183 }
184
James Hawkinsabd73e62016-01-19 15:10:38 -0800185 case 'h': {
186 ShowHelp(argv[0]);
187 break;
188 }
189
190 case 'l': {
191 LogBootEvents();
192 break;
193 }
194
195 case 'p': {
196 PrintBootEvents();
197 break;
198 }
199
200 case 'r': {
201 // |optarg| is an external variable set by getopt representing
202 // the option argument.
203 const char* event = optarg;
204
205 BootEventRecordStore boot_event_store;
206 boot_event_store.AddBootEvent(event);
207 break;
208 }
209
210 default: {
211 DCHECK_EQ(opt, '?');
212
213 // |optopt| is an external variable set by getopt representing
214 // the value of the invalid option.
215 LOG(ERROR) << "Invalid option: " << optopt;
216 ShowHelp(argv[0]);
217 return EXIT_FAILURE;
218 }
219 }
220 }
221
222 return 0;
223}