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