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