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> |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 23 | #include <cmath> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 24 | #include <cstddef> |
| 25 | #include <cstdio> |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 26 | #include <ctime> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 27 | #include <map> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 28 | #include <memory> |
| 29 | #include <string> |
James Hawkins | eabe08b | 2016-01-19 16:54:35 -0800 | [diff] [blame] | 30 | #include <android-base/logging.h> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 31 | #include <cutils/properties.h> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 32 | #include <log/log.h> |
| 33 | #include "boot_event_record_store.h" |
| 34 | #include "event_log_list_builder.h" |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 35 | #include "uptime_parser.h" |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 36 | |
| 37 | namespace { |
| 38 | |
| 39 | // Builds an EventLog buffer named |event| containing |data| and writes |
| 40 | // the log into the Tron histogram logs. |
| 41 | void LogBootEvent(const std::string& event, int32_t data) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 42 | LOG(INFO) << "Logging boot metric: " << event << " " << data; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 43 | |
| 44 | EventLogListBuilder log_builder; |
| 45 | log_builder.Append(event); |
| 46 | log_builder.Append(data); |
| 47 | |
| 48 | std::unique_ptr<uint8_t[]> log; |
| 49 | size_t size; |
| 50 | log_builder.Release(&log, &size); |
| 51 | |
| 52 | android_bWriteLog(HISTOGRAM_LOG_TAG, log.get(), size); |
| 53 | } |
| 54 | |
| 55 | // Scans the boot event record store for record files and logs each boot event |
| 56 | // via EventLog. |
| 57 | void LogBootEvents() { |
| 58 | BootEventRecordStore boot_event_store; |
| 59 | |
| 60 | auto events = boot_event_store.GetAllBootEvents(); |
| 61 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
| 62 | LogBootEvent(i->first, i->second); |
| 63 | } |
| 64 | } |
| 65 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame^] | 66 | // Records the named boot |event| to the record store. If |value| is non-empty |
| 67 | // and is a proper string representation of an integer value, the converted |
| 68 | // integer value is associated with the boot event. |
| 69 | void RecordBootEventFromCommandLine( |
| 70 | const std::string& event, const std::string& value_str) { |
| 71 | BootEventRecordStore boot_event_store; |
| 72 | if (!value_str.empty()) { |
| 73 | int32_t value = 0; |
| 74 | value = std::stoi(value_str); |
| 75 | boot_event_store.AddBootEventWithValue(event, value); |
| 76 | } else { |
| 77 | boot_event_store.AddBootEvent(event); |
| 78 | } |
| 79 | } |
| 80 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 81 | void PrintBootEvents() { |
| 82 | printf("Boot events:\n"); |
| 83 | printf("------------\n"); |
| 84 | |
| 85 | BootEventRecordStore boot_event_store; |
| 86 | auto events = boot_event_store.GetAllBootEvents(); |
| 87 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
| 88 | printf("%s\t%d\n", i->first.c_str(), i->second); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void ShowHelp(const char *cmd) { |
| 93 | fprintf(stderr, "Usage: %s [options]\n", cmd); |
| 94 | fprintf(stderr, |
| 95 | "options include:\n" |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 96 | " -h, --help Show this help\n" |
| 97 | " -l, --log Log all metrics to logstorage\n" |
| 98 | " -p, --print Dump the boot event records to the console\n" |
| 99 | " -r, --record Record the timestamp of a named boot event\n" |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame^] | 100 | " --value Optional value to associate with the boot event\n" |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 101 | " --record_boot_reason Record the reason why the device booted\n" |
| 102 | " --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] | 103 | } |
| 104 | |
| 105 | // Constructs a readable, printable string from the givencommand line |
| 106 | // arguments. |
| 107 | std::string GetCommandLine(int argc, char **argv) { |
| 108 | std::string cmd; |
| 109 | for (int i = 0; i < argc; ++i) { |
| 110 | cmd += argv[i]; |
| 111 | cmd += " "; |
| 112 | } |
| 113 | |
| 114 | return cmd; |
| 115 | } |
| 116 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 117 | // Convenience wrapper over the property API that returns an |
| 118 | // std::string. |
| 119 | std::string GetProperty(const char* key) { |
| 120 | std::vector<char> temp(PROPERTY_VALUE_MAX); |
| 121 | const int len = property_get(key, &temp[0], nullptr); |
| 122 | if (len < 0) { |
| 123 | return ""; |
| 124 | } |
| 125 | return std::string(&temp[0], len); |
| 126 | } |
| 127 | |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 128 | constexpr int32_t kUnknownBootReason = 1; |
| 129 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 130 | // A mapping from boot reason string, as read from the ro.boot.bootreason |
| 131 | // system property, to a unique integer ID. Viewers of log data dashboards for |
| 132 | // the boot_reason metric may refer to this mapping to discern the histogram |
| 133 | // values. |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 134 | const std::map<std::string, int32_t> kBootReasonMap = { |
| 135 | {"unknown", kUnknownBootReason}, |
| 136 | {"normal", 2}, |
| 137 | {"recovery", 3}, |
| 138 | {"reboot", 4}, |
| 139 | {"PowerKey", 5}, |
| 140 | {"hard_reset", 6}, |
| 141 | {"kernel_panic", 7}, |
| 142 | {"rpm_err", 8}, |
| 143 | {"hw_reset", 9}, |
| 144 | {"tz_err", 10}, |
| 145 | {"adsp_err", 11}, |
| 146 | {"modem_err", 12}, |
| 147 | {"mba_err", 13}, |
| 148 | {"Watchdog", 14}, |
| 149 | {"Panic", 15}, |
| 150 | {"power_key", 16}, |
| 151 | {"power_on", 17}, |
| 152 | {"Reboot", 18}, |
| 153 | {"rtc", 19}, |
| 154 | {"edl", 20}, |
James Hawkins | 45ead35 | 2016-03-08 16:42:07 -0800 | [diff] [blame] | 155 | {"oem_pon1", 21}, |
| 156 | {"oem_powerkey", 22}, |
| 157 | {"oem_unknown_reset", 23}, |
| 158 | {"srto: HWWDT reset SC", 24}, |
| 159 | {"srto: HWWDT reset platform", 25}, |
| 160 | {"srto: bootloader", 26}, |
| 161 | {"srto: kernel panic", 27}, |
| 162 | {"srto: kernel watchdog reset", 28}, |
| 163 | {"srto: normal", 29}, |
| 164 | {"srto: reboot", 30}, |
| 165 | {"srto: reboot-bootloader", 31}, |
| 166 | {"srto: security watchdog reset", 32}, |
| 167 | {"srto: wakesrc", 33}, |
| 168 | {"srto: watchdog", 34}, |
| 169 | {"srto:1-1", 35}, |
| 170 | {"srto:omap_hsmm", 36}, |
| 171 | {"srto:phy0", 37}, |
| 172 | {"srto:rtc0", 38}, |
| 173 | {"srto:touchpad", 39}, |
| 174 | {"watchdog", 40}, |
| 175 | {"watchdogr", 41}, |
| 176 | {"wdog_bark", 42}, |
| 177 | {"wdog_bite", 43}, |
| 178 | {"wdog_reset", 44}, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | // Converts a string value representing the reason the system booted to an |
| 182 | // integer representation. This is necessary for logging the boot_reason metric |
| 183 | // via Tron, which does not accept non-integer buckets in histograms. |
| 184 | int32_t BootReasonStrToEnum(const std::string& boot_reason) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 185 | auto mapping = kBootReasonMap.find(boot_reason); |
| 186 | if (mapping != kBootReasonMap.end()) { |
| 187 | return mapping->second; |
| 188 | } |
| 189 | |
| 190 | LOG(INFO) << "Unknown boot reason: " << boot_reason; |
| 191 | return kUnknownBootReason; |
| 192 | } |
| 193 | |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 194 | // Records several metrics related to the time it takes to boot the device, |
| 195 | // including disambiguating boot time on encrypted or non-encrypted devices. |
| 196 | void RecordBootComplete() { |
| 197 | BootEventRecordStore boot_event_store; |
| 198 | time_t uptime = bootstat::ParseUptime(); |
| 199 | |
| 200 | BootEventRecordStore::BootEventRecord record; |
| 201 | |
| 202 | // post_decrypt_time_elapsed is only logged on encrypted devices. |
| 203 | if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) { |
| 204 | // Log the amount of time elapsed until the device is decrypted, which |
| 205 | // includes the variable amount of time the user takes to enter the |
| 206 | // decryption password. |
| 207 | boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime); |
| 208 | |
| 209 | // Subtract the decryption time to normalize the boot cycle timing. |
| 210 | time_t boot_complete = uptime - record.second; |
| 211 | boot_event_store.AddBootEventWithValue("boot_complete_post_decrypt", |
| 212 | boot_complete); |
| 213 | |
| 214 | |
| 215 | } else { |
| 216 | boot_event_store.AddBootEventWithValue("boot_complete_no_encryption", |
| 217 | uptime); |
| 218 | } |
| 219 | |
| 220 | // Record the total time from device startup to boot complete, regardless of |
| 221 | // encryption state. |
| 222 | boot_event_store.AddBootEventWithValue("boot_complete", uptime); |
| 223 | } |
| 224 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 225 | // Records the boot_reason metric by querying the ro.boot.bootreason system |
| 226 | // property. |
| 227 | void RecordBootReason() { |
| 228 | int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason")); |
| 229 | BootEventRecordStore boot_event_store; |
| 230 | boot_event_store.AddBootEventWithValue("boot_reason", boot_reason); |
| 231 | } |
| 232 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 233 | // Records two metrics related to the user resetting a device: the time at |
| 234 | // which the device is reset, and the time since the user last reset the |
| 235 | // device. The former is only set once per-factory reset. |
| 236 | void RecordFactoryReset() { |
| 237 | BootEventRecordStore boot_event_store; |
| 238 | BootEventRecordStore::BootEventRecord record; |
| 239 | |
| 240 | time_t current_time_utc = time(nullptr); |
| 241 | |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 242 | static const char* factory_reset_current_time = "factory_reset_current_time"; |
| 243 | if (current_time_utc < 0) { |
| 244 | // UMA does not display negative values in buckets, so convert to positive. |
| 245 | LogBootEvent(factory_reset_current_time, std::abs(current_time_utc)); |
| 246 | return; |
| 247 | } else { |
| 248 | LogBootEvent(factory_reset_current_time, current_time_utc); |
| 249 | } |
| 250 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 251 | // The factory_reset boot event does not exist after the device is reset, so |
| 252 | // use this signal to mark the time of the factory reset. |
| 253 | if (!boot_event_store.GetBootEvent("factory_reset", &record)) { |
| 254 | boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc); |
James Hawkins | 3bf9b14 | 2016-03-03 14:50:24 -0800 | [diff] [blame] | 255 | |
| 256 | // Don't log the time_since_factory_reset until some time has elapsed. |
| 257 | // The data is not meaningful yet and skews the histogram buckets. |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
| 261 | // Calculate and record the difference in time between now and the |
| 262 | // factory_reset time. |
| 263 | time_t factory_reset_utc = record.second; |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 264 | LogBootEvent("factory_reset_record_value", factory_reset_utc); |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 265 | time_t time_since_factory_reset = difftime(current_time_utc, |
| 266 | factory_reset_utc); |
| 267 | boot_event_store.AddBootEventWithValue("time_since_factory_reset", |
| 268 | time_since_factory_reset); |
| 269 | } |
| 270 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 271 | } // namespace |
| 272 | |
| 273 | int main(int argc, char **argv) { |
| 274 | android::base::InitLogging(argv); |
| 275 | |
| 276 | const std::string cmd_line = GetCommandLine(argc, argv); |
| 277 | LOG(INFO) << "Service started: " << cmd_line; |
| 278 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 279 | int option_index = 0; |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame^] | 280 | static const char value_str[] = "value"; |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 281 | static const char boot_complete_str[] = "record_boot_complete"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 282 | static const char boot_reason_str[] = "record_boot_reason"; |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 283 | static const char factory_reset_str[] = "record_time_since_factory_reset"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 284 | static const struct option long_options[] = { |
| 285 | { "help", no_argument, NULL, 'h' }, |
| 286 | { "log", no_argument, NULL, 'l' }, |
| 287 | { "print", no_argument, NULL, 'p' }, |
| 288 | { "record", required_argument, NULL, 'r' }, |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame^] | 289 | { value_str, required_argument, NULL, 0 }, |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 290 | { boot_complete_str, no_argument, NULL, 0 }, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 291 | { boot_reason_str, no_argument, NULL, 0 }, |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 292 | { factory_reset_str, no_argument, NULL, 0 }, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 293 | { NULL, 0, NULL, 0 } |
| 294 | }; |
| 295 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame^] | 296 | std::string boot_event; |
| 297 | std::string value; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 298 | int opt = 0; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 299 | while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 300 | switch (opt) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 301 | // This case handles long options which have no single-character mapping. |
| 302 | case 0: { |
| 303 | const std::string option_name = long_options[option_index].name; |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame^] | 304 | if (option_name == value_str) { |
| 305 | // |optarg| is an external variable set by getopt representing |
| 306 | // the option argument. |
| 307 | value = optarg; |
| 308 | } else if (option_name == boot_complete_str) { |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 309 | RecordBootComplete(); |
| 310 | } else if (option_name == boot_reason_str) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 311 | RecordBootReason(); |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 312 | } else if (option_name == factory_reset_str) { |
| 313 | RecordFactoryReset(); |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 314 | } else { |
| 315 | LOG(ERROR) << "Invalid option: " << option_name; |
| 316 | } |
| 317 | break; |
| 318 | } |
| 319 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 320 | case 'h': { |
| 321 | ShowHelp(argv[0]); |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | case 'l': { |
| 326 | LogBootEvents(); |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | case 'p': { |
| 331 | PrintBootEvents(); |
| 332 | break; |
| 333 | } |
| 334 | |
| 335 | case 'r': { |
| 336 | // |optarg| is an external variable set by getopt representing |
| 337 | // the option argument. |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame^] | 338 | boot_event = optarg; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | |
| 342 | default: { |
| 343 | DCHECK_EQ(opt, '?'); |
| 344 | |
| 345 | // |optopt| is an external variable set by getopt representing |
| 346 | // the value of the invalid option. |
| 347 | LOG(ERROR) << "Invalid option: " << optopt; |
| 348 | ShowHelp(argv[0]); |
| 349 | return EXIT_FAILURE; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame^] | 354 | if (!boot_event.empty()) { |
| 355 | RecordBootEventFromCommandLine(boot_event, value); |
| 356 | } |
| 357 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 358 | return 0; |
| 359 | } |