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 | 10f54be | 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 | 6b930bf | 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 | 3534914 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 26 | #include <ctime> |
James Hawkins | 10f54be | 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 | 10f54be | 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 | e8e8cf3 | 2016-03-22 14:13:06 -0700 | [diff] [blame] | 35 | #include "histogram_logger.h" |
James Hawkins | eef069a | 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 | e8e8cf3 | 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 | |
| 51 | void PrintBootEvents() { |
| 52 | printf("Boot events:\n"); |
| 53 | printf("------------\n"); |
| 54 | |
| 55 | BootEventRecordStore boot_event_store; |
| 56 | auto events = boot_event_store.GetAllBootEvents(); |
| 57 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
| 58 | printf("%s\t%d\n", i->first.c_str(), i->second); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void ShowHelp(const char *cmd) { |
| 63 | fprintf(stderr, "Usage: %s [options]\n", cmd); |
| 64 | fprintf(stderr, |
| 65 | "options include:\n" |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 66 | " -h, --help Show this help\n" |
| 67 | " -l, --log Log all metrics to logstorage\n" |
| 68 | " -p, --print Dump the boot event records to the console\n" |
| 69 | " -r, --record Record the timestamp of a named boot event\n" |
James Hawkins | c0188da | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 70 | " --record_boot_reason Record the reason why the device booted\n" |
| 71 | " --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] | 72 | } |
| 73 | |
| 74 | // Constructs a readable, printable string from the givencommand line |
| 75 | // arguments. |
| 76 | std::string GetCommandLine(int argc, char **argv) { |
| 77 | std::string cmd; |
| 78 | for (int i = 0; i < argc; ++i) { |
| 79 | cmd += argv[i]; |
| 80 | cmd += " "; |
| 81 | } |
| 82 | |
| 83 | return cmd; |
| 84 | } |
| 85 | |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 86 | // Convenience wrapper over the property API that returns an |
| 87 | // std::string. |
| 88 | std::string GetProperty(const char* key) { |
| 89 | std::vector<char> temp(PROPERTY_VALUE_MAX); |
| 90 | const int len = property_get(key, &temp[0], nullptr); |
| 91 | if (len < 0) { |
| 92 | return ""; |
| 93 | } |
| 94 | return std::string(&temp[0], len); |
| 95 | } |
| 96 | |
James Hawkins | ee79e73 | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 97 | constexpr int32_t kUnknownBootReason = 1; |
| 98 | |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 99 | // A mapping from boot reason string, as read from the ro.boot.bootreason |
| 100 | // system property, to a unique integer ID. Viewers of log data dashboards for |
| 101 | // the boot_reason metric may refer to this mapping to discern the histogram |
| 102 | // values. |
James Hawkins | ee79e73 | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 103 | const std::map<std::string, int32_t> kBootReasonMap = { |
| 104 | {"unknown", kUnknownBootReason}, |
| 105 | {"normal", 2}, |
| 106 | {"recovery", 3}, |
| 107 | {"reboot", 4}, |
| 108 | {"PowerKey", 5}, |
| 109 | {"hard_reset", 6}, |
| 110 | {"kernel_panic", 7}, |
| 111 | {"rpm_err", 8}, |
| 112 | {"hw_reset", 9}, |
| 113 | {"tz_err", 10}, |
| 114 | {"adsp_err", 11}, |
| 115 | {"modem_err", 12}, |
| 116 | {"mba_err", 13}, |
| 117 | {"Watchdog", 14}, |
| 118 | {"Panic", 15}, |
| 119 | {"power_key", 16}, |
| 120 | {"power_on", 17}, |
| 121 | {"Reboot", 18}, |
| 122 | {"rtc", 19}, |
| 123 | {"edl", 20}, |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | // Converts a string value representing the reason the system booted to an |
| 127 | // integer representation. This is necessary for logging the boot_reason metric |
| 128 | // via Tron, which does not accept non-integer buckets in histograms. |
| 129 | int32_t BootReasonStrToEnum(const std::string& boot_reason) { |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 130 | auto mapping = kBootReasonMap.find(boot_reason); |
| 131 | if (mapping != kBootReasonMap.end()) { |
| 132 | return mapping->second; |
| 133 | } |
| 134 | |
| 135 | LOG(INFO) << "Unknown boot reason: " << boot_reason; |
| 136 | return kUnknownBootReason; |
| 137 | } |
| 138 | |
James Hawkins | eef069a | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 139 | // Records several metrics related to the time it takes to boot the device, |
| 140 | // including disambiguating boot time on encrypted or non-encrypted devices. |
| 141 | void RecordBootComplete() { |
| 142 | BootEventRecordStore boot_event_store; |
| 143 | time_t uptime = bootstat::ParseUptime(); |
| 144 | |
| 145 | BootEventRecordStore::BootEventRecord record; |
| 146 | |
| 147 | // post_decrypt_time_elapsed is only logged on encrypted devices. |
| 148 | if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) { |
| 149 | // Log the amount of time elapsed until the device is decrypted, which |
| 150 | // includes the variable amount of time the user takes to enter the |
| 151 | // decryption password. |
| 152 | boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime); |
| 153 | |
| 154 | // Subtract the decryption time to normalize the boot cycle timing. |
| 155 | time_t boot_complete = uptime - record.second; |
| 156 | boot_event_store.AddBootEventWithValue("boot_complete_post_decrypt", |
| 157 | boot_complete); |
| 158 | |
| 159 | |
| 160 | } else { |
| 161 | boot_event_store.AddBootEventWithValue("boot_complete_no_encryption", |
| 162 | uptime); |
| 163 | } |
| 164 | |
| 165 | // Record the total time from device startup to boot complete, regardless of |
| 166 | // encryption state. |
| 167 | boot_event_store.AddBootEventWithValue("boot_complete", uptime); |
| 168 | } |
| 169 | |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 170 | // Records the boot_reason metric by querying the ro.boot.bootreason system |
| 171 | // property. |
| 172 | void RecordBootReason() { |
| 173 | int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason")); |
| 174 | BootEventRecordStore boot_event_store; |
| 175 | boot_event_store.AddBootEventWithValue("boot_reason", boot_reason); |
| 176 | } |
| 177 | |
James Hawkins | 3534914 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 178 | // Records two metrics related to the user resetting a device: the time at |
| 179 | // which the device is reset, and the time since the user last reset the |
| 180 | // device. The former is only set once per-factory reset. |
| 181 | void RecordFactoryReset() { |
| 182 | BootEventRecordStore boot_event_store; |
| 183 | BootEventRecordStore::BootEventRecord record; |
| 184 | |
| 185 | time_t current_time_utc = time(nullptr); |
| 186 | |
James Hawkins | 6b930bf | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 187 | static const char* factory_reset_current_time = "factory_reset_current_time"; |
| 188 | if (current_time_utc < 0) { |
| 189 | // UMA does not display negative values in buckets, so convert to positive. |
James Hawkins | e8e8cf3 | 2016-03-22 14:13:06 -0700 | [diff] [blame] | 190 | bootstat::LogHistogram(factory_reset_current_time, std::abs(current_time_utc)); |
James Hawkins | 6b930bf | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 191 | return; |
| 192 | } else { |
James Hawkins | e8e8cf3 | 2016-03-22 14:13:06 -0700 | [diff] [blame] | 193 | bootstat::LogHistogram(factory_reset_current_time, current_time_utc); |
James Hawkins | 6b930bf | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 194 | } |
| 195 | |
James Hawkins | 3534914 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 196 | // The factory_reset boot event does not exist after the device is reset, so |
| 197 | // use this signal to mark the time of the factory reset. |
| 198 | if (!boot_event_store.GetBootEvent("factory_reset", &record)) { |
| 199 | boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc); |
James Hawkins | e501044 | 2016-03-03 14:50:24 -0800 | [diff] [blame] | 200 | |
| 201 | // Don't log the time_since_factory_reset until some time has elapsed. |
| 202 | // The data is not meaningful yet and skews the histogram buckets. |
James Hawkins | 3534914 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
| 206 | // Calculate and record the difference in time between now and the |
| 207 | // factory_reset time. |
| 208 | time_t factory_reset_utc = record.second; |
James Hawkins | e8e8cf3 | 2016-03-22 14:13:06 -0700 | [diff] [blame] | 209 | bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc); |
James Hawkins | 3534914 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 210 | time_t time_since_factory_reset = difftime(current_time_utc, |
| 211 | factory_reset_utc); |
| 212 | boot_event_store.AddBootEventWithValue("time_since_factory_reset", |
| 213 | time_since_factory_reset); |
| 214 | } |
| 215 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 216 | } // namespace |
| 217 | |
| 218 | int main(int argc, char **argv) { |
| 219 | android::base::InitLogging(argv); |
| 220 | |
| 221 | const std::string cmd_line = GetCommandLine(argc, argv); |
| 222 | LOG(INFO) << "Service started: " << cmd_line; |
| 223 | |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 224 | int option_index = 0; |
James Hawkins | eef069a | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 225 | static const char boot_complete_str[] = "record_boot_complete"; |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 226 | static const char boot_reason_str[] = "record_boot_reason"; |
James Hawkins | c0188da | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 227 | static const char factory_reset_str[] = "record_time_since_factory_reset"; |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 228 | static const struct option long_options[] = { |
| 229 | { "help", no_argument, NULL, 'h' }, |
| 230 | { "log", no_argument, NULL, 'l' }, |
| 231 | { "print", no_argument, NULL, 'p' }, |
| 232 | { "record", required_argument, NULL, 'r' }, |
James Hawkins | eef069a | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 233 | { boot_complete_str, no_argument, NULL, 0 }, |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 234 | { boot_reason_str, no_argument, NULL, 0 }, |
James Hawkins | 3534914 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 235 | { factory_reset_str, no_argument, NULL, 0 }, |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 236 | { NULL, 0, NULL, 0 } |
| 237 | }; |
| 238 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 239 | int opt = 0; |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 240 | while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 241 | switch (opt) { |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 242 | // This case handles long options which have no single-character mapping. |
| 243 | case 0: { |
| 244 | const std::string option_name = long_options[option_index].name; |
James Hawkins | eef069a | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 245 | if (option_name == boot_complete_str) { |
| 246 | RecordBootComplete(); |
| 247 | } else if (option_name == boot_reason_str) { |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 248 | RecordBootReason(); |
James Hawkins | 3534914 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 249 | } else if (option_name == factory_reset_str) { |
| 250 | RecordFactoryReset(); |
James Hawkins | 10f54be | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 251 | } else { |
| 252 | LOG(ERROR) << "Invalid option: " << option_name; |
| 253 | } |
| 254 | break; |
| 255 | } |
| 256 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 257 | case 'h': { |
| 258 | ShowHelp(argv[0]); |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | case 'l': { |
| 263 | LogBootEvents(); |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | case 'p': { |
| 268 | PrintBootEvents(); |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | case 'r': { |
| 273 | // |optarg| is an external variable set by getopt representing |
| 274 | // the option argument. |
| 275 | const char* event = optarg; |
| 276 | |
| 277 | BootEventRecordStore boot_event_store; |
| 278 | boot_event_store.AddBootEvent(event); |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | default: { |
| 283 | DCHECK_EQ(opt, '?'); |
| 284 | |
| 285 | // |optopt| is an external variable set by getopt representing |
| 286 | // the value of the invalid option. |
| 287 | LOG(ERROR) << "Invalid option: " << optopt; |
| 288 | ShowHelp(argv[0]); |
| 289 | return EXIT_FAILURE; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |