James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1 | /* |
| 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 Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 21 | #include <getopt.h> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <cstddef> |
| 24 | #include <cstdio> |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 25 | #include <ctime> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 26 | #include <map> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <string> |
James Hawkins | eabe08b | 2016-01-19 16:54:35 -0800 | [diff] [blame] | 29 | #include <android-base/logging.h> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 30 | #include <cutils/properties.h> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 31 | #include <log/log.h> |
| 32 | #include "boot_event_record_store.h" |
| 33 | #include "event_log_list_builder.h" |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | // Builds an EventLog buffer named |event| containing |data| and writes |
| 38 | // the log into the Tron histogram logs. |
| 39 | void LogBootEvent(const std::string& event, int32_t data) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 40 | LOG(INFO) << "Logging boot metric: " << event << " " << data; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 41 | |
| 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. |
| 55 | void 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 | |
| 64 | void 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 | |
| 75 | void ShowHelp(const char *cmd) { |
| 76 | fprintf(stderr, "Usage: %s [options]\n", cmd); |
| 77 | fprintf(stderr, |
| 78 | "options include:\n" |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 79 | " -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 Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame^] | 83 | " --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 Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Constructs a readable, printable string from the givencommand line |
| 88 | // arguments. |
| 89 | std::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 Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 99 | // Convenience wrapper over the property API that returns an |
| 100 | // std::string. |
| 101 | std::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 Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 110 | constexpr int32_t kUnknownBootReason = 1; |
| 111 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 112 | // 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 Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 116 | const 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 Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | // Converts a string value representing the reason the system booted to an |
| 140 | // integer representation. This is necessary for logging the boot_reason metric |
| 141 | // via Tron, which does not accept non-integer buckets in histograms. |
| 142 | int32_t BootReasonStrToEnum(const std::string& boot_reason) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 143 | auto mapping = kBootReasonMap.find(boot_reason); |
| 144 | if (mapping != kBootReasonMap.end()) { |
| 145 | return mapping->second; |
| 146 | } |
| 147 | |
| 148 | LOG(INFO) << "Unknown boot reason: " << boot_reason; |
| 149 | return kUnknownBootReason; |
| 150 | } |
| 151 | |
| 152 | // Records the boot_reason metric by querying the ro.boot.bootreason system |
| 153 | // property. |
| 154 | void RecordBootReason() { |
| 155 | int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason")); |
| 156 | BootEventRecordStore boot_event_store; |
| 157 | boot_event_store.AddBootEventWithValue("boot_reason", boot_reason); |
| 158 | } |
| 159 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 160 | // Records two metrics related to the user resetting a device: the time at |
| 161 | // which the device is reset, and the time since the user last reset the |
| 162 | // device. The former is only set once per-factory reset. |
| 163 | void RecordFactoryReset() { |
| 164 | BootEventRecordStore boot_event_store; |
| 165 | BootEventRecordStore::BootEventRecord record; |
| 166 | |
| 167 | time_t current_time_utc = time(nullptr); |
| 168 | |
| 169 | // The factory_reset boot event does not exist after the device is reset, so |
| 170 | // use this signal to mark the time of the factory reset. |
| 171 | if (!boot_event_store.GetBootEvent("factory_reset", &record)) { |
| 172 | boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc); |
| 173 | boot_event_store.AddBootEventWithValue("time_since_factory_reset", 0); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | // Calculate and record the difference in time between now and the |
| 178 | // factory_reset time. |
| 179 | time_t factory_reset_utc = record.second; |
| 180 | time_t time_since_factory_reset = difftime(current_time_utc, |
| 181 | factory_reset_utc); |
| 182 | boot_event_store.AddBootEventWithValue("time_since_factory_reset", |
| 183 | time_since_factory_reset); |
| 184 | } |
| 185 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 186 | } // namespace |
| 187 | |
| 188 | int main(int argc, char **argv) { |
| 189 | android::base::InitLogging(argv); |
| 190 | |
| 191 | const std::string cmd_line = GetCommandLine(argc, argv); |
| 192 | LOG(INFO) << "Service started: " << cmd_line; |
| 193 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 194 | int option_index = 0; |
| 195 | static const char boot_reason_str[] = "record_boot_reason"; |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame^] | 196 | static const char factory_reset_str[] = "record_time_since_factory_reset"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 197 | static const struct option long_options[] = { |
| 198 | { "help", no_argument, NULL, 'h' }, |
| 199 | { "log", no_argument, NULL, 'l' }, |
| 200 | { "print", no_argument, NULL, 'p' }, |
| 201 | { "record", required_argument, NULL, 'r' }, |
| 202 | { boot_reason_str, no_argument, NULL, 0 }, |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 203 | { factory_reset_str, no_argument, NULL, 0 }, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 204 | { NULL, 0, NULL, 0 } |
| 205 | }; |
| 206 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 207 | int opt = 0; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 208 | while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 209 | switch (opt) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 210 | // This case handles long options which have no single-character mapping. |
| 211 | case 0: { |
| 212 | const std::string option_name = long_options[option_index].name; |
| 213 | if (option_name == boot_reason_str) { |
| 214 | RecordBootReason(); |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 215 | } else if (option_name == factory_reset_str) { |
| 216 | RecordFactoryReset(); |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 217 | } else { |
| 218 | LOG(ERROR) << "Invalid option: " << option_name; |
| 219 | } |
| 220 | break; |
| 221 | } |
| 222 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 223 | case 'h': { |
| 224 | ShowHelp(argv[0]); |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | case 'l': { |
| 229 | LogBootEvents(); |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | case 'p': { |
| 234 | PrintBootEvents(); |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | case 'r': { |
| 239 | // |optarg| is an external variable set by getopt representing |
| 240 | // the option argument. |
| 241 | const char* event = optarg; |
| 242 | |
| 243 | BootEventRecordStore boot_event_store; |
| 244 | boot_event_store.AddBootEvent(event); |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | default: { |
| 249 | DCHECK_EQ(opt, '?'); |
| 250 | |
| 251 | // |optopt| is an external variable set by getopt representing |
| 252 | // the value of the invalid option. |
| 253 | LOG(ERROR) << "Invalid option: " << optopt; |
| 254 | ShowHelp(argv[0]); |
| 255 | return EXIT_FAILURE; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |