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> |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 22 | #include <sys/klog.h> |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 24 | |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 25 | #include <chrono> |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 26 | #include <cmath> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 27 | #include <cstddef> |
| 28 | #include <cstdio> |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 29 | #include <ctime> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 30 | #include <map> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 31 | #include <memory> |
| 32 | #include <string> |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 33 | #include <vector> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 34 | |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 35 | #include <android-base/chrono_utils.h> |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 36 | #include <android-base/file.h> |
James Hawkins | eabe08b | 2016-01-19 16:54:35 -0800 | [diff] [blame] | 37 | #include <android-base/logging.h> |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 38 | #include <android-base/parseint.h> |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 39 | #include <android-base/strings.h> |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 40 | #include <android/log.h> |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 41 | #include <cutils/android_reboot.h> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 42 | #include <cutils/properties.h> |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 43 | #include <log/logcat.h> |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 44 | #include <metricslogger/metrics_logger.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 45 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 46 | #include "boot_event_record_store.h" |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 47 | |
| 48 | namespace { |
| 49 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 50 | // Scans the boot event record store for record files and logs each boot event |
| 51 | // via EventLog. |
| 52 | void LogBootEvents() { |
| 53 | BootEventRecordStore boot_event_store; |
| 54 | |
| 55 | auto events = boot_event_store.GetAllBootEvents(); |
| 56 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 57 | android::metricslogger::LogHistogram(i->first, i->second); |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 61 | // Records the named boot |event| to the record store. If |value| is non-empty |
| 62 | // and is a proper string representation of an integer value, the converted |
| 63 | // integer value is associated with the boot event. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 64 | void RecordBootEventFromCommandLine(const std::string& event, const std::string& value_str) { |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 65 | BootEventRecordStore boot_event_store; |
| 66 | if (!value_str.empty()) { |
| 67 | int32_t value = 0; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 68 | if (android::base::ParseInt(value_str, &value)) { |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 69 | boot_event_store.AddBootEventWithValue(event, value); |
| 70 | } |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 71 | } else { |
| 72 | boot_event_store.AddBootEvent(event); |
| 73 | } |
| 74 | } |
| 75 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 76 | void PrintBootEvents() { |
| 77 | printf("Boot events:\n"); |
| 78 | printf("------------\n"); |
| 79 | |
| 80 | BootEventRecordStore boot_event_store; |
| 81 | auto events = boot_event_store.GetAllBootEvents(); |
| 82 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
| 83 | printf("%s\t%d\n", i->first.c_str(), i->second); |
| 84 | } |
| 85 | } |
| 86 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 87 | void ShowHelp(const char* cmd) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 88 | fprintf(stderr, "Usage: %s [options]\n", cmd); |
| 89 | fprintf(stderr, |
| 90 | "options include:\n" |
Yongqin Liu | 78b2b94 | 2017-07-07 13:26:49 +0800 | [diff] [blame] | 91 | " -h, --help Show this help\n" |
| 92 | " -l, --log Log all metrics to logstorage\n" |
| 93 | " -p, --print Dump the boot event records to the console\n" |
| 94 | " -r, --record Record the timestamp of a named boot event\n" |
| 95 | " --value Optional value to associate with the boot event\n" |
| 96 | " --record_boot_complete Record metrics related to the time for the device boot\n" |
| 97 | " --record_boot_reason Record the reason why the device booted\n" |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 98 | " --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] | 99 | } |
| 100 | |
| 101 | // Constructs a readable, printable string from the givencommand line |
| 102 | // arguments. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 103 | std::string GetCommandLine(int argc, char** argv) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 104 | std::string cmd; |
| 105 | for (int i = 0; i < argc; ++i) { |
| 106 | cmd += argv[i]; |
| 107 | cmd += " "; |
| 108 | } |
| 109 | |
| 110 | return cmd; |
| 111 | } |
| 112 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 113 | // Convenience wrapper over the property API that returns an |
| 114 | // std::string. |
| 115 | std::string GetProperty(const char* key) { |
| 116 | std::vector<char> temp(PROPERTY_VALUE_MAX); |
| 117 | const int len = property_get(key, &temp[0], nullptr); |
| 118 | if (len < 0) { |
| 119 | return ""; |
| 120 | } |
| 121 | return std::string(&temp[0], len); |
| 122 | } |
| 123 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 124 | void SetProperty(const char* key, const std::string& val) { |
| 125 | property_set(key, val.c_str()); |
| 126 | } |
| 127 | |
| 128 | void SetProperty(const char* key, const char* val) { |
| 129 | property_set(key, val); |
| 130 | } |
| 131 | |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 132 | constexpr int32_t kEmptyBootReason = 0; |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 133 | constexpr int32_t kUnknownBootReason = 1; |
| 134 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 135 | // A mapping from boot reason string, as read from the ro.boot.bootreason |
| 136 | // system property, to a unique integer ID. Viewers of log data dashboards for |
| 137 | // the boot_reason metric may refer to this mapping to discern the histogram |
| 138 | // values. |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 139 | const std::map<std::string, int32_t> kBootReasonMap = { |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 140 | {"empty", kEmptyBootReason}, |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 141 | {"unknown", kUnknownBootReason}, |
| 142 | {"normal", 2}, |
| 143 | {"recovery", 3}, |
| 144 | {"reboot", 4}, |
| 145 | {"PowerKey", 5}, |
| 146 | {"hard_reset", 6}, |
| 147 | {"kernel_panic", 7}, |
| 148 | {"rpm_err", 8}, |
| 149 | {"hw_reset", 9}, |
| 150 | {"tz_err", 10}, |
| 151 | {"adsp_err", 11}, |
| 152 | {"modem_err", 12}, |
| 153 | {"mba_err", 13}, |
| 154 | {"Watchdog", 14}, |
| 155 | {"Panic", 15}, |
| 156 | {"power_key", 16}, |
| 157 | {"power_on", 17}, |
| 158 | {"Reboot", 18}, |
| 159 | {"rtc", 19}, |
| 160 | {"edl", 20}, |
| 161 | {"oem_pon1", 21}, |
| 162 | {"oem_powerkey", 22}, |
| 163 | {"oem_unknown_reset", 23}, |
| 164 | {"srto: HWWDT reset SC", 24}, |
| 165 | {"srto: HWWDT reset platform", 25}, |
| 166 | {"srto: bootloader", 26}, |
| 167 | {"srto: kernel panic", 27}, |
| 168 | {"srto: kernel watchdog reset", 28}, |
| 169 | {"srto: normal", 29}, |
| 170 | {"srto: reboot", 30}, |
| 171 | {"srto: reboot-bootloader", 31}, |
| 172 | {"srto: security watchdog reset", 32}, |
| 173 | {"srto: wakesrc", 33}, |
| 174 | {"srto: watchdog", 34}, |
| 175 | {"srto:1-1", 35}, |
| 176 | {"srto:omap_hsmm", 36}, |
| 177 | {"srto:phy0", 37}, |
| 178 | {"srto:rtc0", 38}, |
| 179 | {"srto:touchpad", 39}, |
| 180 | {"watchdog", 40}, |
| 181 | {"watchdogr", 41}, |
| 182 | {"wdog_bark", 42}, |
| 183 | {"wdog_bite", 43}, |
| 184 | {"wdog_reset", 44}, |
| 185 | {"shutdown,", 45}, // Trailing comma is intentional. |
| 186 | {"shutdown,userrequested", 46}, |
| 187 | {"reboot,bootloader", 47}, |
| 188 | {"reboot,cold", 48}, |
| 189 | {"reboot,recovery", 49}, |
| 190 | {"thermal_shutdown", 50}, |
| 191 | {"s3_wakeup", 51}, |
| 192 | {"kernel_panic,sysrq", 52}, |
| 193 | {"kernel_panic,NULL", 53}, |
| 194 | {"kernel_panic,BUG", 54}, |
| 195 | {"bootloader", 55}, |
| 196 | {"cold", 56}, |
| 197 | {"hard", 57}, |
| 198 | {"warm", 58}, |
| 199 | {"recovery", 59}, |
| 200 | {"thermal-shutdown", 60}, |
| 201 | {"shutdown,thermal", 61}, |
| 202 | {"shutdown,battery", 62}, |
| 203 | {"reboot,ota", 63}, |
| 204 | {"reboot,factory_reset", 64}, |
| 205 | {"reboot,", 65}, |
| 206 | {"reboot,shell", 66}, |
| 207 | {"reboot,adb", 67}, |
Mark Salyzyn | 9033bf5 | 2017-09-21 11:30:29 -0700 | [diff] [blame] | 208 | {"reboot,userrequested", 68}, |
Mark Salyzyn | 161b862 | 2017-09-26 08:26:12 -0700 | [diff] [blame] | 209 | {"shutdown,container", 69}, // Host OS asking Android Container to shutdown |
Mark Salyzyn | 243fa29 | 2017-10-11 09:02:04 -0700 | [diff] [blame] | 210 | {"cold,powerkey", 70}, |
| 211 | {"warm,s3_wakeup", 71}, |
| 212 | {"hard,hw_reset", 72}, |
| 213 | {"shutdown,suspend", 73}, // Suspend to RAM |
| 214 | {"shutdown,hibernate", 74}, // Suspend to DISK |
James Hawkins | 34073b5 | 2017-10-17 15:53:27 -0700 | [diff] [blame] | 215 | {"power_on_key", 75}, |
| 216 | {"reboot_by_key", 76}, |
| 217 | {"wdt_by_pass_pwk", 77}, |
| 218 | {"reboot_longkey", 78}, |
| 219 | {"powerkey", 79}, |
| 220 | {"usb", 80}, |
| 221 | {"wdt", 81}, |
| 222 | {"tool_by_pass_pwk", 82}, |
| 223 | {"2sec_reboot", 83}, |
| 224 | {"reboot,by_key", 84}, |
| 225 | {"reboot,longkey", 85}, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | // Converts a string value representing the reason the system booted to an |
| 229 | // integer representation. This is necessary for logging the boot_reason metric |
| 230 | // via Tron, which does not accept non-integer buckets in histograms. |
| 231 | int32_t BootReasonStrToEnum(const std::string& boot_reason) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 232 | auto mapping = kBootReasonMap.find(boot_reason); |
| 233 | if (mapping != kBootReasonMap.end()) { |
| 234 | return mapping->second; |
| 235 | } |
| 236 | |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 237 | if (boot_reason.empty()) { |
| 238 | return kEmptyBootReason; |
| 239 | } |
| 240 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 241 | LOG(INFO) << "Unknown boot reason: " << boot_reason; |
| 242 | return kUnknownBootReason; |
| 243 | } |
| 244 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 245 | // Canonical list of supported primary reboot reasons. |
| 246 | const std::vector<const std::string> knownReasons = { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 247 | // clang-format off |
| 248 | // kernel |
| 249 | "watchdog", |
| 250 | "kernel_panic", |
| 251 | // strong |
| 252 | "recovery", // Should not happen from ro.boot.bootreason |
| 253 | "bootloader", // Should not happen from ro.boot.bootreason |
| 254 | // blunt |
| 255 | "cold", |
| 256 | "hard", |
| 257 | "warm", |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 258 | // super blunt |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 259 | "shutdown", // Can not happen from ro.boot.bootreason |
| 260 | "reboot", // Default catch-all for anything unknown |
| 261 | // clang-format on |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | // Returns true if the supplied reason prefix is considered detailed enough. |
| 265 | bool isStrongRebootReason(const std::string& r) { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 266 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 267 | if (s == "cold") break; |
| 268 | // Prefix defined as terminated by a nul or comma (,). |
| 269 | if (android::base::StartsWith(r, s.c_str()) && |
| 270 | ((r.length() == s.length()) || (r[s.length()] == ','))) { |
| 271 | return true; |
| 272 | } |
| 273 | } |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | // Returns true if the supplied reason prefix is associated with the kernel. |
| 278 | bool isKernelRebootReason(const std::string& r) { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 279 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 280 | if (s == "recovery") break; |
| 281 | // Prefix defined as terminated by a nul or comma (,). |
| 282 | if (android::base::StartsWith(r, s.c_str()) && |
| 283 | ((r.length() == s.length()) || (r[s.length()] == ','))) { |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | // Returns true if the supplied reason prefix is considered known. |
| 291 | bool isKnownRebootReason(const std::string& r) { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 292 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 293 | // Prefix defined as terminated by a nul or comma (,). |
| 294 | if (android::base::StartsWith(r, s.c_str()) && |
| 295 | ((r.length() == s.length()) || (r[s.length()] == ','))) { |
| 296 | return true; |
| 297 | } |
| 298 | } |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | // If the reboot reason should be improved, report true if is too blunt. |
| 303 | bool isBluntRebootReason(const std::string& r) { |
| 304 | if (isStrongRebootReason(r)) return false; |
| 305 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 306 | if (!isKnownRebootReason(r)) return true; // Can not support unknown as detail |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 307 | |
| 308 | size_t pos = 0; |
| 309 | while ((pos = r.find(',', pos)) != std::string::npos) { |
| 310 | ++pos; |
| 311 | std::string next(r.substr(pos)); |
| 312 | if (next.length() == 0) break; |
| 313 | if (next[0] == ',') continue; |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 314 | if (!isKnownRebootReason(next)) return false; // Unknown subreason is good. |
| 315 | if (isStrongRebootReason(next)) return false; // eg: reboot,reboot |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 316 | } |
| 317 | return true; |
| 318 | } |
| 319 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 320 | bool readPstoreConsole(std::string& console) { |
| 321 | if (android::base::ReadFileToString("/sys/fs/pstore/console-ramoops-0", &console)) { |
| 322 | return true; |
| 323 | } |
| 324 | return android::base::ReadFileToString("/sys/fs/pstore/console-ramoops", &console); |
| 325 | } |
| 326 | |
| 327 | bool addKernelPanicSubReason(const std::string& console, std::string& ret) { |
| 328 | // Check for kernel panic types to refine information |
| 329 | if (console.rfind("SysRq : Trigger a crash") != std::string::npos) { |
| 330 | // Can not happen, except on userdebug, during testing/debugging. |
| 331 | ret = "kernel_panic,sysrq"; |
| 332 | return true; |
| 333 | } |
| 334 | if (console.rfind("Unable to handle kernel NULL pointer dereference at virtual address") != |
| 335 | std::string::npos) { |
| 336 | ret = "kernel_panic,NULL"; |
| 337 | return true; |
| 338 | } |
| 339 | if (console.rfind("Kernel BUG at ") != std::string::npos) { |
| 340 | ret = "kernel_panic,BUG"; |
| 341 | return true; |
| 342 | } |
| 343 | return false; |
| 344 | } |
| 345 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 346 | // std::transform Helper callback functions: |
| 347 | // Converts a string value representing the reason the system booted to a |
| 348 | // string complying with Android system standard reason. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 349 | char tounderline(char c) { |
| 350 | return ::isblank(c) ? '_' : c; |
| 351 | } |
| 352 | char toprintable(char c) { |
| 353 | return ::isprint(c) ? c : '?'; |
| 354 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 355 | |
| 356 | const char system_reboot_reason_property[] = "sys.boot.reason"; |
| 357 | const char last_reboot_reason_property[] = LAST_REBOOT_REASON_PROPERTY; |
| 358 | const char bootloader_reboot_reason_property[] = "ro.boot.bootreason"; |
| 359 | |
| 360 | // Scrub, Sanitize, Standardize and Enhance the boot reason string supplied. |
| 361 | std::string BootReasonStrToReason(const std::string& boot_reason) { |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 362 | static const size_t max_reason_length = 256; |
| 363 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 364 | std::string ret(GetProperty(system_reboot_reason_property)); |
| 365 | std::string reason(boot_reason); |
| 366 | // If sys.boot.reason == ro.boot.bootreason, let's re-evaluate |
| 367 | if (reason == ret) ret = ""; |
| 368 | |
| 369 | // Cleanup boot_reason regarding acceptable character set |
| 370 | std::transform(reason.begin(), reason.end(), reason.begin(), ::tolower); |
| 371 | std::transform(reason.begin(), reason.end(), reason.begin(), tounderline); |
| 372 | std::transform(reason.begin(), reason.end(), reason.begin(), toprintable); |
| 373 | |
| 374 | // Is the current system boot reason sys.boot.reason valid? |
| 375 | if (!isKnownRebootReason(ret)) ret = ""; |
| 376 | |
| 377 | if (ret == "") { |
| 378 | // Is the bootloader boot reason ro.boot.bootreason known? |
| 379 | std::vector<std::string> words(android::base::Split(reason, ",_-")); |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 380 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 381 | std::string blunt; |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 382 | for (auto& r : words) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 383 | if (r == s) { |
| 384 | if (isBluntRebootReason(s)) { |
| 385 | blunt = s; |
| 386 | } else { |
| 387 | ret = s; |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | if (ret == "") ret = blunt; |
| 393 | if (ret != "") break; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (ret == "") { |
| 398 | // A series of checks to take some officially unsupported reasons |
| 399 | // reported by the bootloader and find some logical and canonical |
| 400 | // sense. In an ideal world, we would require those bootloaders |
| 401 | // to behave and follow our standards. |
| 402 | static const std::vector<std::pair<const std::string, const std::string>> aliasReasons = { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 403 | {"watchdog", "wdog"}, |
| 404 | {"cold,powerkey", "powerkey"}, |
| 405 | {"kernel_panic", "panic"}, |
| 406 | {"shutdown,thermal", "thermal"}, |
| 407 | {"warm,s3_wakeup", "s3_wakeup"}, |
| 408 | {"hard,hw_reset", "hw_reset"}, |
| 409 | {"bootloader", ""}, |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 410 | }; |
| 411 | |
| 412 | // Either the primary or alias is found _somewhere_ in the reason string. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 413 | for (auto& s : aliasReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 414 | if (reason.find(s.first) != std::string::npos) { |
| 415 | ret = s.first; |
| 416 | break; |
| 417 | } |
| 418 | if (s.second.size() && (reason.find(s.second) != std::string::npos)) { |
| 419 | ret = s.first; |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // If watchdog is the reason, see if there is a security angle? |
| 426 | if (ret == "watchdog") { |
| 427 | if (reason.find("sec") != std::string::npos) { |
| 428 | ret += ",security"; |
| 429 | } |
| 430 | } |
| 431 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 432 | if (ret == "kernel_panic") { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 433 | // Check to see if last klog has some refinement hints. |
| 434 | std::string content; |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 435 | if (readPstoreConsole(content)) { |
| 436 | addKernelPanicSubReason(content, ret); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 437 | } |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 438 | } else if (isBluntRebootReason(ret)) { |
| 439 | // Check the other available reason resources if the reason is still blunt. |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 440 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 441 | // Check to see if last klog has some refinement hints. |
| 442 | std::string content; |
| 443 | if (readPstoreConsole(content)) { |
| 444 | // The toybox reboot command used directly (unlikely)? But also |
| 445 | // catches init's response to Android's more controlled reboot command. |
| 446 | if (content.rfind("reboot: Power down") != std::string::npos) { |
| 447 | ret = "shutdown"; // Still too blunt, but more accurate. |
| 448 | // ToDo: init should record the shutdown reason to kernel messages ala: |
| 449 | // init: shutdown system with command 'last_reboot_reason' |
| 450 | // so that if pstore has persistence we can get some details |
| 451 | // that could be missing in last_reboot_reason_property. |
| 452 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 453 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 454 | static const char cmd[] = "reboot: Restarting system with command '"; |
| 455 | size_t pos = content.rfind(cmd); |
| 456 | if (pos != std::string::npos) { |
| 457 | pos += strlen(cmd); |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 458 | std::string subReason(content.substr(pos, max_reason_length)); |
| 459 | for (pos = 0; pos < subReason.length(); ++pos) { |
| 460 | char c = tounderline(subReason[pos]); |
| 461 | if (!::isprint(c) || (c == '\'')) { |
| 462 | subReason.erase(pos); |
| 463 | break; |
| 464 | } |
| 465 | subReason[pos] = ::tolower(c); |
| 466 | } |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 467 | if (subReason != "") { // Will not land "reboot" as that is too blunt. |
| 468 | if (isKernelRebootReason(subReason)) { |
| 469 | ret = "reboot," + subReason; // User space can't talk kernel reasons. |
| 470 | } else { |
| 471 | ret = subReason; |
| 472 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 475 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 476 | // Check for kernel panics, allowed to override reboot command. |
| 477 | if (!addKernelPanicSubReason(content, ret) && |
| 478 | // check for long-press power down |
| 479 | ((content.rfind("Power held for ") != std::string::npos) || |
| 480 | (content.rfind("charger: [") != std::string::npos))) { |
| 481 | ret = "cold"; |
| 482 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // The following battery test should migrate to a default system health HAL |
| 486 | |
| 487 | // Let us not worry if the reboot command was issued, for the cases of |
| 488 | // reboot -p, reboot <no reason>, reboot cold, reboot warm and reboot hard. |
| 489 | // Same for bootloader and ro.boot.bootreasons of this set, but a dead |
| 490 | // battery could conceivably lead to these, so worthy of override. |
| 491 | if (isBluntRebootReason(ret)) { |
| 492 | // Heuristic to determine if shutdown possibly because of a dead battery? |
| 493 | // Really a hail-mary pass to find it in last klog content ... |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 494 | static const int battery_dead_threshold = 2; // percent |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 495 | static const char battery[] = "healthd: battery l="; |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 496 | size_t pos = content.rfind(battery); // last one |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 497 | std::string digits; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 498 | if (pos != std::string::npos) { |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 499 | digits = content.substr(pos + strlen(battery)); |
| 500 | } |
| 501 | char* endptr = NULL; |
| 502 | unsigned long long level = strtoull(digits.c_str(), &endptr, 10); |
| 503 | if ((level <= 100) && (endptr != digits.c_str()) && (*endptr == ' ')) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 504 | LOG(INFO) << "Battery level at shutdown " << level << "%"; |
| 505 | if (level <= battery_dead_threshold) { |
| 506 | ret = "shutdown,battery"; |
| 507 | } |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 508 | } else { // Most likely |
| 509 | digits = ""; // reset digits |
| 510 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 511 | // Content buffer no longer will have console data. Beware if more |
| 512 | // checks added below, that depend on parsing console content. |
| 513 | content = ""; |
| 514 | |
| 515 | LOG(DEBUG) << "Can not find last low battery in last console messages"; |
| 516 | android_logcat_context ctx = create_android_logcat(); |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 517 | FILE* fp = android_logcat_popen(&ctx, "logcat -b kernel -v brief -d"); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 518 | if (fp != nullptr) { |
| 519 | android::base::ReadFdToString(fileno(fp), &content); |
| 520 | } |
| 521 | android_logcat_pclose(&ctx, fp); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 522 | static const char logcat_battery[] = "W/healthd ( 0): battery l="; |
| 523 | const char* match = logcat_battery; |
| 524 | |
| 525 | if (content == "") { |
| 526 | // Service logd.klog not running, go to smaller buffer in the kernel. |
| 527 | int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0); |
| 528 | if (rc > 0) { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 529 | ssize_t len = rc + 1024; // 1K Margin should it grow between calls. |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 530 | std::unique_ptr<char[]> buf(new char[len]); |
| 531 | rc = klogctl(KLOG_READ_ALL, buf.get(), len); |
| 532 | if (rc < len) { |
| 533 | len = rc + 1; |
| 534 | } |
| 535 | buf[--len] = '\0'; |
| 536 | content = buf.get(); |
| 537 | } |
| 538 | match = battery; |
| 539 | } |
| 540 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 541 | pos = content.find(match); // The first one it finds. |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 542 | if (pos != std::string::npos) { |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 543 | digits = content.substr(pos + strlen(match)); |
| 544 | } |
| 545 | endptr = NULL; |
| 546 | level = strtoull(digits.c_str(), &endptr, 10); |
| 547 | if ((level <= 100) && (endptr != digits.c_str()) && (*endptr == ' ')) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 548 | LOG(INFO) << "Battery level at startup " << level << "%"; |
| 549 | if (level <= battery_dead_threshold) { |
| 550 | ret = "shutdown,battery"; |
| 551 | } |
| 552 | } else { |
| 553 | LOG(DEBUG) << "Can not find first battery level in dmesg or logcat"; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | // Is there a controlled shutdown hint in last_reboot_reason_property? |
| 559 | if (isBluntRebootReason(ret)) { |
| 560 | // Content buffer no longer will have console data. Beware if more |
| 561 | // checks added below, that depend on parsing console content. |
| 562 | content = GetProperty(last_reboot_reason_property); |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 563 | // Cleanup last_boot_reason regarding acceptable character set |
| 564 | std::transform(content.begin(), content.end(), content.begin(), ::tolower); |
| 565 | std::transform(content.begin(), content.end(), content.begin(), tounderline); |
| 566 | std::transform(content.begin(), content.end(), content.begin(), toprintable); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 567 | |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 568 | // Anything in last is better than 'super-blunt' reboot or shutdown. |
| 569 | if ((ret == "") || (ret == "reboot") || (ret == "shutdown") || !isBluntRebootReason(content)) { |
| 570 | ret = content; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | // Other System Health HAL reasons? |
| 575 | |
| 576 | // ToDo: /proc/sys/kernel/boot_reason needs a HAL interface to |
| 577 | // possibly offer hardware-specific clues from the PMIC. |
| 578 | } |
| 579 | |
| 580 | // If unknown left over from above, make it "reboot,<boot_reason>" |
| 581 | if (ret == "") { |
| 582 | ret = "reboot"; |
| 583 | if (android::base::StartsWith(reason, "reboot")) { |
| 584 | reason = reason.substr(strlen("reboot")); |
Mark Salyzyn | 0af71a5 | 2017-10-05 13:58:04 -0700 | [diff] [blame] | 585 | while ((reason[0] == ',') || (reason[0] == '_')) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 586 | reason = reason.substr(1); |
| 587 | } |
| 588 | } |
| 589 | if (reason != "") { |
| 590 | ret += ","; |
| 591 | ret += reason; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | LOG(INFO) << "Canonical boot reason: " << ret; |
| 596 | if (isKernelRebootReason(ret) && (GetProperty(last_reboot_reason_property) != "")) { |
| 597 | // Rewrite as it must be old news, kernel reasons trump user space. |
| 598 | SetProperty(last_reboot_reason_property, ret); |
| 599 | } |
| 600 | return ret; |
| 601 | } |
| 602 | |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 603 | // Returns the appropriate metric key prefix for the boot_complete metric such |
| 604 | // that boot metrics after a system update are labeled as ota_boot_complete; |
| 605 | // otherwise, they are labeled as boot_complete. This method encapsulates the |
| 606 | // bookkeeping required to track when a system update has occurred by storing |
| 607 | // the UTC timestamp of the system build date and comparing against the current |
| 608 | // system build date. |
| 609 | std::string CalculateBootCompletePrefix() { |
| 610 | static const std::string kBuildDateKey = "build_date"; |
| 611 | std::string boot_complete_prefix = "boot_complete"; |
| 612 | |
| 613 | std::string build_date_str = GetProperty("ro.build.date.utc"); |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 614 | int32_t build_date; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 615 | if (!android::base::ParseInt(build_date_str, &build_date)) { |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 616 | return std::string(); |
| 617 | } |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 618 | |
| 619 | BootEventRecordStore boot_event_store; |
| 620 | BootEventRecordStore::BootEventRecord record; |
James Hawkins | 0bc4ad4 | 2017-05-30 15:03:15 -0700 | [diff] [blame] | 621 | if (!boot_event_store.GetBootEvent(kBuildDateKey, &record)) { |
| 622 | boot_complete_prefix = "factory_reset_" + boot_complete_prefix; |
| 623 | boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date); |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 624 | LOG(INFO) << "Canonical boot reason: reboot,factory_reset"; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 625 | SetProperty(system_reboot_reason_property, "reboot,factory_reset"); |
James Hawkins | 0bc4ad4 | 2017-05-30 15:03:15 -0700 | [diff] [blame] | 626 | } else if (build_date != record.second) { |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 627 | boot_complete_prefix = "ota_" + boot_complete_prefix; |
| 628 | boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date); |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 629 | LOG(INFO) << "Canonical boot reason: reboot,ota"; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 630 | SetProperty(system_reboot_reason_property, "reboot,ota"); |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | return boot_complete_prefix; |
| 634 | } |
| 635 | |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 636 | // Records the value of a given ro.boottime.init property in milliseconds. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 637 | void RecordInitBootTimeProp(BootEventRecordStore* boot_event_store, const char* property) { |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 638 | std::string value = GetProperty(property); |
| 639 | |
James Hawkins | 27c0522 | 2017-01-26 11:55:44 -0800 | [diff] [blame] | 640 | int32_t time_in_ms; |
| 641 | if (android::base::ParseInt(value, &time_in_ms)) { |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 642 | boot_event_store->AddBootEventWithValue(property, time_in_ms); |
| 643 | } |
| 644 | } |
| 645 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 646 | // A map from bootloader timing stage to the time that stage took during boot. |
| 647 | typedef std::map<std::string, int32_t> BootloaderTimingMap; |
| 648 | |
| 649 | // Returns a mapping from bootloader stage names to the time those stages |
| 650 | // took to boot. |
| 651 | const BootloaderTimingMap GetBootLoaderTimings() { |
| 652 | BootloaderTimingMap timings; |
| 653 | |
| 654 | // |ro.boot.boottime| is of the form 'stage1:time1,...,stageN:timeN', |
| 655 | // where timeN is in milliseconds. |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 656 | std::string value = GetProperty("ro.boot.boottime"); |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 657 | if (value.empty()) { |
| 658 | // ro.boot.boottime is not reported on all devices. |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 659 | return BootloaderTimingMap(); |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 660 | } |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 661 | |
| 662 | auto stages = android::base::Split(value, ","); |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 663 | for (const auto& stageTiming : stages) { |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 664 | // |stageTiming| is of the form 'stage:time'. |
| 665 | auto stageTimingValues = android::base::Split(stageTiming, ":"); |
James Hawkins | 0bc4ad4 | 2017-05-30 15:03:15 -0700 | [diff] [blame] | 666 | DCHECK_EQ(2U, stageTimingValues.size()); |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 667 | |
| 668 | std::string stageName = stageTimingValues[0]; |
| 669 | int32_t time_ms; |
| 670 | if (android::base::ParseInt(stageTimingValues[1], &time_ms)) { |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 671 | timings[stageName] = time_ms; |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 672 | } |
| 673 | } |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 674 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 675 | return timings; |
| 676 | } |
| 677 | |
| 678 | // Parses and records the set of bootloader stages and associated boot times |
| 679 | // from the ro.boot.boottime system property. |
| 680 | void RecordBootloaderTimings(BootEventRecordStore* boot_event_store, |
| 681 | const BootloaderTimingMap& bootloader_timings) { |
| 682 | int32_t total_time = 0; |
| 683 | for (const auto& timing : bootloader_timings) { |
| 684 | total_time += timing.second; |
| 685 | boot_event_store->AddBootEventWithValue("boottime.bootloader." + timing.first, timing.second); |
| 686 | } |
| 687 | |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 688 | boot_event_store->AddBootEventWithValue("boottime.bootloader.total", total_time); |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 689 | } |
| 690 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 691 | // Records the closest estimation to the absolute device boot time, i.e., |
| 692 | // from power on to boot_complete, including bootloader times. |
| 693 | void RecordAbsoluteBootTime(BootEventRecordStore* boot_event_store, |
| 694 | const BootloaderTimingMap& bootloader_timings, |
| 695 | std::chrono::milliseconds uptime) { |
| 696 | int32_t bootloader_time_ms = 0; |
| 697 | |
| 698 | for (const auto& timing : bootloader_timings) { |
| 699 | if (timing.first.compare("SW") != 0) { |
| 700 | bootloader_time_ms += timing.second; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | auto bootloader_duration = std::chrono::milliseconds(bootloader_time_ms); |
| 705 | auto absolute_total = |
| 706 | std::chrono::duration_cast<std::chrono::seconds>(bootloader_duration + uptime); |
| 707 | boot_event_store->AddBootEventWithValue("absolute_boot_time", absolute_total.count()); |
| 708 | } |
| 709 | |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 710 | // Records several metrics related to the time it takes to boot the device, |
| 711 | // including disambiguating boot time on encrypted or non-encrypted devices. |
| 712 | void RecordBootComplete() { |
| 713 | BootEventRecordStore boot_event_store; |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 714 | BootEventRecordStore::BootEventRecord record; |
James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 715 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 716 | auto time_since_epoch = android::base::boot_clock::now().time_since_epoch(); |
| 717 | auto uptime = std::chrono::duration_cast<std::chrono::seconds>(time_since_epoch); |
James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 718 | time_t current_time_utc = time(nullptr); |
| 719 | |
| 720 | if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) { |
| 721 | time_t last_boot_time_utc = record.second; |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 722 | time_t time_since_last_boot = difftime(current_time_utc, last_boot_time_utc); |
| 723 | boot_event_store.AddBootEventWithValue("time_since_last_boot", time_since_last_boot); |
James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 727 | |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 728 | // The boot_complete metric has two variants: boot_complete and |
| 729 | // ota_boot_complete. The latter signifies that the device is booting after |
| 730 | // a system update. |
| 731 | std::string boot_complete_prefix = CalculateBootCompletePrefix(); |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 732 | if (boot_complete_prefix.empty()) { |
| 733 | // The system is hosed because the build date property could not be read. |
| 734 | return; |
| 735 | } |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 736 | |
| 737 | // post_decrypt_time_elapsed is only logged on encrypted devices. |
| 738 | if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) { |
| 739 | // Log the amount of time elapsed until the device is decrypted, which |
| 740 | // includes the variable amount of time the user takes to enter the |
| 741 | // decryption password. |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 742 | boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime.count()); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 743 | |
| 744 | // Subtract the decryption time to normalize the boot cycle timing. |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 745 | std::chrono::seconds boot_complete = std::chrono::seconds(uptime.count() - record.second); |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 746 | boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt", |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 747 | boot_complete.count()); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 748 | } else { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 749 | boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption", uptime.count()); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | // Record the total time from device startup to boot complete, regardless of |
| 753 | // encryption state. |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 754 | boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime.count()); |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 755 | |
| 756 | RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init"); |
| 757 | RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux"); |
| 758 | RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait"); |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 759 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 760 | const BootloaderTimingMap bootloader_timings = GetBootLoaderTimings(); |
| 761 | RecordBootloaderTimings(&boot_event_store, bootloader_timings); |
| 762 | |
| 763 | auto uptime_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_since_epoch); |
| 764 | RecordAbsoluteBootTime(&boot_event_store, bootloader_timings, uptime_ms); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 765 | } |
| 766 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 767 | // Records the boot_reason metric by querying the ro.boot.bootreason system |
| 768 | // property. |
| 769 | void RecordBootReason() { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 770 | const std::string reason(GetProperty(bootloader_reboot_reason_property)); |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 771 | |
| 772 | if (reason.empty()) { |
| 773 | // Log an empty boot reason value as '<EMPTY>' to ensure the value is intentional |
| 774 | // (and not corruption anywhere else in the reporting pipeline). |
| 775 | android::metricslogger::LogMultiAction(android::metricslogger::ACTION_BOOT, |
| 776 | android::metricslogger::FIELD_PLATFORM_REASON, "<EMPTY>"); |
| 777 | } else { |
| 778 | android::metricslogger::LogMultiAction(android::metricslogger::ACTION_BOOT, |
| 779 | android::metricslogger::FIELD_PLATFORM_REASON, reason); |
| 780 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 781 | |
| 782 | // Log the raw bootloader_boot_reason property value. |
| 783 | int32_t boot_reason = BootReasonStrToEnum(reason); |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 784 | BootEventRecordStore boot_event_store; |
| 785 | boot_event_store.AddBootEventWithValue("boot_reason", boot_reason); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 786 | |
| 787 | // Log the scrubbed system_boot_reason. |
| 788 | const std::string system_reason(BootReasonStrToReason(reason)); |
| 789 | int32_t system_boot_reason = BootReasonStrToEnum(system_reason); |
| 790 | boot_event_store.AddBootEventWithValue("system_boot_reason", system_boot_reason); |
| 791 | |
| 792 | // Record the scrubbed system_boot_reason to the property |
| 793 | SetProperty(system_reboot_reason_property, system_reason); |
| 794 | if (reason == "") { |
| 795 | SetProperty(bootloader_reboot_reason_property, system_reason); |
| 796 | } |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 797 | } |
| 798 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 799 | // Records two metrics related to the user resetting a device: the time at |
| 800 | // which the device is reset, and the time since the user last reset the |
| 801 | // device. The former is only set once per-factory reset. |
| 802 | void RecordFactoryReset() { |
| 803 | BootEventRecordStore boot_event_store; |
| 804 | BootEventRecordStore::BootEventRecord record; |
| 805 | |
| 806 | time_t current_time_utc = time(nullptr); |
| 807 | |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 808 | if (current_time_utc < 0) { |
| 809 | // UMA does not display negative values in buckets, so convert to positive. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 810 | android::metricslogger::LogHistogram("factory_reset_current_time_failure", |
| 811 | std::abs(current_time_utc)); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 812 | |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 813 | // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 814 | // is losing records somehow. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 815 | boot_event_store.AddBootEventWithValue("factory_reset_current_time_failure", |
| 816 | std::abs(current_time_utc)); |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 817 | return; |
| 818 | } else { |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 819 | android::metricslogger::LogHistogram("factory_reset_current_time", current_time_utc); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 820 | |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 821 | // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 822 | // is losing records somehow. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 823 | boot_event_store.AddBootEventWithValue("factory_reset_current_time", current_time_utc); |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 824 | } |
| 825 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 826 | // The factory_reset boot event does not exist after the device is reset, so |
| 827 | // use this signal to mark the time of the factory reset. |
| 828 | if (!boot_event_store.GetBootEvent("factory_reset", &record)) { |
| 829 | boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc); |
James Hawkins | 3bf9b14 | 2016-03-03 14:50:24 -0800 | [diff] [blame] | 830 | |
| 831 | // Don't log the time_since_factory_reset until some time has elapsed. |
| 832 | // The data is not meaningful yet and skews the histogram buckets. |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 833 | return; |
| 834 | } |
| 835 | |
| 836 | // Calculate and record the difference in time between now and the |
| 837 | // factory_reset time. |
| 838 | time_t factory_reset_utc = record.second; |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 839 | android::metricslogger::LogHistogram("factory_reset_record_value", factory_reset_utc); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 840 | |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 841 | // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 842 | // is losing records somehow. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 843 | boot_event_store.AddBootEventWithValue("factory_reset_record_value", factory_reset_utc); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 844 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 845 | time_t time_since_factory_reset = difftime(current_time_utc, factory_reset_utc); |
| 846 | boot_event_store.AddBootEventWithValue("time_since_factory_reset", time_since_factory_reset); |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 847 | } |
| 848 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 849 | } // namespace |
| 850 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 851 | int main(int argc, char** argv) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 852 | android::base::InitLogging(argv); |
| 853 | |
| 854 | const std::string cmd_line = GetCommandLine(argc, argv); |
| 855 | LOG(INFO) << "Service started: " << cmd_line; |
| 856 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 857 | int option_index = 0; |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 858 | static const char value_str[] = "value"; |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 859 | static const char boot_complete_str[] = "record_boot_complete"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 860 | static const char boot_reason_str[] = "record_boot_reason"; |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 861 | static const char factory_reset_str[] = "record_time_since_factory_reset"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 862 | static const struct option long_options[] = { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 863 | // clang-format off |
| 864 | { "help", no_argument, NULL, 'h' }, |
| 865 | { "log", no_argument, NULL, 'l' }, |
| 866 | { "print", no_argument, NULL, 'p' }, |
| 867 | { "record", required_argument, NULL, 'r' }, |
| 868 | { value_str, required_argument, NULL, 0 }, |
| 869 | { boot_complete_str, no_argument, NULL, 0 }, |
| 870 | { boot_reason_str, no_argument, NULL, 0 }, |
| 871 | { factory_reset_str, no_argument, NULL, 0 }, |
| 872 | { NULL, 0, NULL, 0 } |
| 873 | // clang-format on |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 874 | }; |
| 875 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 876 | std::string boot_event; |
| 877 | std::string value; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 878 | int opt = 0; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 879 | while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 880 | switch (opt) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 881 | // This case handles long options which have no single-character mapping. |
| 882 | case 0: { |
| 883 | const std::string option_name = long_options[option_index].name; |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 884 | if (option_name == value_str) { |
| 885 | // |optarg| is an external variable set by getopt representing |
| 886 | // the option argument. |
| 887 | value = optarg; |
| 888 | } else if (option_name == boot_complete_str) { |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 889 | RecordBootComplete(); |
| 890 | } else if (option_name == boot_reason_str) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 891 | RecordBootReason(); |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 892 | } else if (option_name == factory_reset_str) { |
| 893 | RecordFactoryReset(); |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 894 | } else { |
| 895 | LOG(ERROR) << "Invalid option: " << option_name; |
| 896 | } |
| 897 | break; |
| 898 | } |
| 899 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 900 | case 'h': { |
| 901 | ShowHelp(argv[0]); |
| 902 | break; |
| 903 | } |
| 904 | |
| 905 | case 'l': { |
| 906 | LogBootEvents(); |
| 907 | break; |
| 908 | } |
| 909 | |
| 910 | case 'p': { |
| 911 | PrintBootEvents(); |
| 912 | break; |
| 913 | } |
| 914 | |
| 915 | case 'r': { |
| 916 | // |optarg| is an external variable set by getopt representing |
| 917 | // the option argument. |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 918 | boot_event = optarg; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 919 | break; |
| 920 | } |
| 921 | |
| 922 | default: { |
| 923 | DCHECK_EQ(opt, '?'); |
| 924 | |
| 925 | // |optopt| is an external variable set by getopt representing |
| 926 | // the value of the invalid option. |
| 927 | LOG(ERROR) << "Invalid option: " << optopt; |
| 928 | ShowHelp(argv[0]); |
| 929 | return EXIT_FAILURE; |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 934 | if (!boot_event.empty()) { |
| 935 | RecordBootEventFromCommandLine(boot_event, value); |
| 936 | } |
| 937 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 938 | return 0; |
| 939 | } |