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> |
Mark Salyzyn | 25900dd | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 32 | #include <regex> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 33 | #include <string> |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 34 | #include <utility> |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 35 | #include <vector> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 36 | |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 37 | #include <android-base/chrono_utils.h> |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 38 | #include <android-base/file.h> |
James Hawkins | eabe08b | 2016-01-19 16:54:35 -0800 | [diff] [blame] | 39 | #include <android-base/logging.h> |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 40 | #include <android-base/parseint.h> |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 41 | #include <android-base/strings.h> |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 42 | #include <android/log.h> |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 43 | #include <cutils/android_reboot.h> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 44 | #include <cutils/properties.h> |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 45 | #include <log/logcat.h> |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 46 | #include <metricslogger/metrics_logger.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 47 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 48 | #include "boot_event_record_store.h" |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 49 | |
| 50 | namespace { |
| 51 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 52 | // Scans the boot event record store for record files and logs each boot event |
| 53 | // via EventLog. |
| 54 | void LogBootEvents() { |
| 55 | BootEventRecordStore boot_event_store; |
| 56 | |
| 57 | auto events = boot_event_store.GetAllBootEvents(); |
| 58 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 59 | android::metricslogger::LogHistogram(i->first, i->second); |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 63 | // Records the named boot |event| to the record store. If |value| is non-empty |
| 64 | // and is a proper string representation of an integer value, the converted |
| 65 | // integer value is associated with the boot event. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 66 | void RecordBootEventFromCommandLine(const std::string& event, const std::string& value_str) { |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 67 | BootEventRecordStore boot_event_store; |
| 68 | if (!value_str.empty()) { |
| 69 | int32_t value = 0; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 70 | if (android::base::ParseInt(value_str, &value)) { |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 71 | boot_event_store.AddBootEventWithValue(event, value); |
| 72 | } |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 73 | } else { |
| 74 | boot_event_store.AddBootEvent(event); |
| 75 | } |
| 76 | } |
| 77 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 78 | void PrintBootEvents() { |
| 79 | printf("Boot events:\n"); |
| 80 | printf("------------\n"); |
| 81 | |
| 82 | BootEventRecordStore boot_event_store; |
| 83 | auto events = boot_event_store.GetAllBootEvents(); |
| 84 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
| 85 | printf("%s\t%d\n", i->first.c_str(), i->second); |
| 86 | } |
| 87 | } |
| 88 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 89 | void ShowHelp(const char* cmd) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 90 | fprintf(stderr, "Usage: %s [options]\n", cmd); |
| 91 | fprintf(stderr, |
| 92 | "options include:\n" |
Yongqin Liu | 78b2b94 | 2017-07-07 13:26:49 +0800 | [diff] [blame] | 93 | " -h, --help Show this help\n" |
| 94 | " -l, --log Log all metrics to logstorage\n" |
| 95 | " -p, --print Dump the boot event records to the console\n" |
| 96 | " -r, --record Record the timestamp of a named boot event\n" |
| 97 | " --value Optional value to associate with the boot event\n" |
| 98 | " --record_boot_complete Record metrics related to the time for the device boot\n" |
| 99 | " --record_boot_reason Record the reason why the device booted\n" |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 100 | " --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] | 101 | } |
| 102 | |
| 103 | // Constructs a readable, printable string from the givencommand line |
| 104 | // arguments. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 105 | std::string GetCommandLine(int argc, char** argv) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 106 | std::string cmd; |
| 107 | for (int i = 0; i < argc; ++i) { |
| 108 | cmd += argv[i]; |
| 109 | cmd += " "; |
| 110 | } |
| 111 | |
| 112 | return cmd; |
| 113 | } |
| 114 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 115 | // Convenience wrapper over the property API that returns an |
| 116 | // std::string. |
| 117 | std::string GetProperty(const char* key) { |
| 118 | std::vector<char> temp(PROPERTY_VALUE_MAX); |
| 119 | const int len = property_get(key, &temp[0], nullptr); |
| 120 | if (len < 0) { |
| 121 | return ""; |
| 122 | } |
| 123 | return std::string(&temp[0], len); |
| 124 | } |
| 125 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 126 | void SetProperty(const char* key, const std::string& val) { |
| 127 | property_set(key, val.c_str()); |
| 128 | } |
| 129 | |
| 130 | void SetProperty(const char* key, const char* val) { |
| 131 | property_set(key, val); |
| 132 | } |
| 133 | |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 134 | constexpr int32_t kEmptyBootReason = 0; |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 135 | constexpr int32_t kUnknownBootReason = 1; |
| 136 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 137 | // A mapping from boot reason string, as read from the ro.boot.bootreason |
| 138 | // system property, to a unique integer ID. Viewers of log data dashboards for |
| 139 | // the boot_reason metric may refer to this mapping to discern the histogram |
| 140 | // values. |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 141 | const std::map<std::string, int32_t> kBootReasonMap = { |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 142 | {"empty", kEmptyBootReason}, |
Mark Salyzyn | 2b82053 | 2018-03-16 08:53:34 -0700 | [diff] [blame] | 143 | {"__BOOTSTAT_UNKNOWN__", kUnknownBootReason}, |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 144 | {"normal", 2}, |
| 145 | {"recovery", 3}, |
| 146 | {"reboot", 4}, |
| 147 | {"PowerKey", 5}, |
| 148 | {"hard_reset", 6}, |
| 149 | {"kernel_panic", 7}, |
| 150 | {"rpm_err", 8}, |
| 151 | {"hw_reset", 9}, |
| 152 | {"tz_err", 10}, |
| 153 | {"adsp_err", 11}, |
| 154 | {"modem_err", 12}, |
| 155 | {"mba_err", 13}, |
| 156 | {"Watchdog", 14}, |
| 157 | {"Panic", 15}, |
| 158 | {"power_key", 16}, |
| 159 | {"power_on", 17}, |
| 160 | {"Reboot", 18}, |
| 161 | {"rtc", 19}, |
| 162 | {"edl", 20}, |
| 163 | {"oem_pon1", 21}, |
| 164 | {"oem_powerkey", 22}, |
| 165 | {"oem_unknown_reset", 23}, |
| 166 | {"srto: HWWDT reset SC", 24}, |
| 167 | {"srto: HWWDT reset platform", 25}, |
| 168 | {"srto: bootloader", 26}, |
| 169 | {"srto: kernel panic", 27}, |
| 170 | {"srto: kernel watchdog reset", 28}, |
| 171 | {"srto: normal", 29}, |
| 172 | {"srto: reboot", 30}, |
| 173 | {"srto: reboot-bootloader", 31}, |
| 174 | {"srto: security watchdog reset", 32}, |
| 175 | {"srto: wakesrc", 33}, |
| 176 | {"srto: watchdog", 34}, |
| 177 | {"srto:1-1", 35}, |
| 178 | {"srto:omap_hsmm", 36}, |
| 179 | {"srto:phy0", 37}, |
| 180 | {"srto:rtc0", 38}, |
| 181 | {"srto:touchpad", 39}, |
| 182 | {"watchdog", 40}, |
| 183 | {"watchdogr", 41}, |
| 184 | {"wdog_bark", 42}, |
| 185 | {"wdog_bite", 43}, |
| 186 | {"wdog_reset", 44}, |
| 187 | {"shutdown,", 45}, // Trailing comma is intentional. |
| 188 | {"shutdown,userrequested", 46}, |
| 189 | {"reboot,bootloader", 47}, |
| 190 | {"reboot,cold", 48}, |
| 191 | {"reboot,recovery", 49}, |
| 192 | {"thermal_shutdown", 50}, |
| 193 | {"s3_wakeup", 51}, |
| 194 | {"kernel_panic,sysrq", 52}, |
| 195 | {"kernel_panic,NULL", 53}, |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 196 | {"kernel_panic,null", 53}, |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 197 | {"kernel_panic,BUG", 54}, |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 198 | {"kernel_panic,bug", 54}, |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 199 | {"bootloader", 55}, |
| 200 | {"cold", 56}, |
| 201 | {"hard", 57}, |
| 202 | {"warm", 58}, |
Mark Salyzyn | 2b82053 | 2018-03-16 08:53:34 -0700 | [diff] [blame] | 203 | // {"recovery", 59}, // Duplicate of enum 3 above. Immediate reuse possible. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 204 | {"thermal-shutdown", 60}, |
| 205 | {"shutdown,thermal", 61}, |
| 206 | {"shutdown,battery", 62}, |
| 207 | {"reboot,ota", 63}, |
| 208 | {"reboot,factory_reset", 64}, |
| 209 | {"reboot,", 65}, |
| 210 | {"reboot,shell", 66}, |
| 211 | {"reboot,adb", 67}, |
Mark Salyzyn | 9033bf5 | 2017-09-21 11:30:29 -0700 | [diff] [blame] | 212 | {"reboot,userrequested", 68}, |
Mark Salyzyn | 161b862 | 2017-09-26 08:26:12 -0700 | [diff] [blame] | 213 | {"shutdown,container", 69}, // Host OS asking Android Container to shutdown |
Mark Salyzyn | 243fa29 | 2017-10-11 09:02:04 -0700 | [diff] [blame] | 214 | {"cold,powerkey", 70}, |
| 215 | {"warm,s3_wakeup", 71}, |
| 216 | {"hard,hw_reset", 72}, |
| 217 | {"shutdown,suspend", 73}, // Suspend to RAM |
| 218 | {"shutdown,hibernate", 74}, // Suspend to DISK |
James Hawkins | 34073b5 | 2017-10-17 15:53:27 -0700 | [diff] [blame] | 219 | {"power_on_key", 75}, |
| 220 | {"reboot_by_key", 76}, |
| 221 | {"wdt_by_pass_pwk", 77}, |
| 222 | {"reboot_longkey", 78}, |
| 223 | {"powerkey", 79}, |
| 224 | {"usb", 80}, |
| 225 | {"wdt", 81}, |
| 226 | {"tool_by_pass_pwk", 82}, |
| 227 | {"2sec_reboot", 83}, |
| 228 | {"reboot,by_key", 84}, |
| 229 | {"reboot,longkey", 85}, |
Mark Salyzyn | cabbe4f | 2017-10-23 13:52:39 -0700 | [diff] [blame] | 230 | {"reboot,2sec", 86}, |
Mark Salyzyn | c89f9da | 2017-10-24 15:35:34 -0700 | [diff] [blame] | 231 | {"shutdown,thermal,battery", 87}, |
Mark Salyzyn | 72a8ea3 | 2017-10-25 09:23:19 -0700 | [diff] [blame] | 232 | {"reboot,its_just_so_hard", 88}, // produced by boot_reason_test |
| 233 | {"reboot,Its Just So Hard", 89}, // produced by boot_reason_test |
Mark Salyzyn | 2b82053 | 2018-03-16 08:53:34 -0700 | [diff] [blame] | 234 | // {"usb", 90}, // Duplicate of enum 80 above. Immediate reuse possible. |
James Hawkins | 74b1758 | 2017-11-20 14:13:41 -0800 | [diff] [blame] | 235 | {"charge", 91}, |
| 236 | {"oem_tz_crash", 92}, |
| 237 | {"uvlo", 93}, |
| 238 | {"oem_ps_hold", 94}, |
| 239 | {"abnormal_reset", 95}, |
| 240 | {"oemerr_unknown", 96}, |
| 241 | {"reboot_fastboot_mode", 97}, |
James Hawkins | 5f85f83 | 2017-11-29 14:30:06 -0800 | [diff] [blame] | 242 | {"watchdog_apps_bite", 98}, |
| 243 | {"xpu_err", 99}, |
| 244 | {"power_on_usb", 100}, |
James Hawkins | f4444f0 | 2017-11-30 15:01:40 -0800 | [diff] [blame] | 245 | {"watchdog_rpm", 101}, |
| 246 | {"watchdog_nonsec", 102}, |
| 247 | {"watchdog_apps_bark", 103}, |
| 248 | {"reboot_dmverity_corrupted", 104}, |
James Hawkins | 00433a2 | 2017-12-04 14:20:21 -0800 | [diff] [blame] | 249 | {"reboot_smpl", 105}, |
| 250 | {"watchdog_sdi_apps_reset", 106}, |
| 251 | {"smpl", 107}, |
| 252 | {"oem_modem_failed_to_powerup", 108}, |
James Hawkins | e2c2724 | 2017-12-18 13:40:27 -0800 | [diff] [blame] | 253 | {"reboot_normal", 109}, |
| 254 | {"oem_lpass_cfg", 110}, |
| 255 | {"oem_xpu_ns_error", 111}, |
| 256 | {"power_key_press", 112}, |
| 257 | {"hardware_reset", 113}, |
| 258 | {"reboot_by_powerkey", 114}, |
| 259 | {"reboot_verity", 115}, |
| 260 | {"oem_rpm_undef_error", 116}, |
| 261 | {"oem_crash_on_the_lk", 117}, |
| 262 | {"oem_rpm_reset", 118}, |
| 263 | {"oem_lpass_cfg", 119}, |
| 264 | {"oem_xpu_ns_error", 120}, |
| 265 | {"factory_cable", 121}, |
| 266 | {"oem_ar6320_failed_to_powerup", 122}, |
| 267 | {"watchdog_rpm_bite", 123}, |
| 268 | {"power_on_cable", 124}, |
| 269 | {"reboot_unknown", 125}, |
| 270 | {"wireless_charger", 126}, |
| 271 | {"0x776655ff", 127}, |
| 272 | {"oem_thermal_bite_reset", 128}, |
| 273 | {"charger", 129}, |
| 274 | {"pon1", 130}, |
| 275 | {"unknown", 131}, |
| 276 | {"reboot_rtc", 132}, |
| 277 | {"cold_boot", 133}, |
| 278 | {"hard_rst", 134}, |
James Hawkins | b607dae | 2018-01-05 14:42:55 -0800 | [diff] [blame] | 279 | {"power-on", 135}, |
| 280 | {"oem_adsp_resetting_the_soc", 136}, |
| 281 | {"kpdpwr", 137}, |
| 282 | {"oem_modem_timeout_waiting", 138}, |
| 283 | {"usb_chg", 139}, |
| 284 | {"warm_reset_0x02", 140}, |
| 285 | {"warm_reset_0x80", 141}, |
| 286 | {"pon_reason_0xb0", 142}, |
| 287 | {"reboot_download", 143}, |
James Hawkins | 79a4ee2 | 2018-01-26 14:31:04 -0800 | [diff] [blame] | 288 | {"reboot_recovery_mode", 144}, |
| 289 | {"oem_sdi_err_fatal", 145}, |
| 290 | {"pmic_watchdog", 146}, |
| 291 | {"software_master", 147}, |
Mark Salyzyn | 8aa36c6 | 2018-03-16 11:00:14 -0700 | [diff] [blame] | 292 | {"cold,charger", 148}, |
| 293 | {"cold,rtc", 149}, |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame^] | 294 | {"cold,rtc,2sec", 150}, |
| 295 | {"reboot,tool", 151}, |
| 296 | {"reboot,wdt", 152}, |
| 297 | {"reboot,unknown", 153}, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | // Converts a string value representing the reason the system booted to an |
| 301 | // integer representation. This is necessary for logging the boot_reason metric |
| 302 | // via Tron, which does not accept non-integer buckets in histograms. |
| 303 | int32_t BootReasonStrToEnum(const std::string& boot_reason) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 304 | auto mapping = kBootReasonMap.find(boot_reason); |
| 305 | if (mapping != kBootReasonMap.end()) { |
| 306 | return mapping->second; |
| 307 | } |
| 308 | |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 309 | if (boot_reason.empty()) { |
| 310 | return kEmptyBootReason; |
| 311 | } |
| 312 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 313 | LOG(INFO) << "Unknown boot reason: " << boot_reason; |
| 314 | return kUnknownBootReason; |
| 315 | } |
| 316 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 317 | // Canonical list of supported primary reboot reasons. |
| 318 | const std::vector<const std::string> knownReasons = { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 319 | // clang-format off |
| 320 | // kernel |
| 321 | "watchdog", |
| 322 | "kernel_panic", |
| 323 | // strong |
| 324 | "recovery", // Should not happen from ro.boot.bootreason |
| 325 | "bootloader", // Should not happen from ro.boot.bootreason |
| 326 | // blunt |
| 327 | "cold", |
| 328 | "hard", |
| 329 | "warm", |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 330 | // super blunt |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 331 | "shutdown", // Can not happen from ro.boot.bootreason |
| 332 | "reboot", // Default catch-all for anything unknown |
| 333 | // clang-format on |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | // Returns true if the supplied reason prefix is considered detailed enough. |
| 337 | bool isStrongRebootReason(const std::string& r) { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 338 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 339 | if (s == "cold") break; |
| 340 | // Prefix defined as terminated by a nul or comma (,). |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 341 | if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 342 | return true; |
| 343 | } |
| 344 | } |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | // Returns true if the supplied reason prefix is associated with the kernel. |
| 349 | bool isKernelRebootReason(const std::string& r) { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 350 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 351 | if (s == "recovery") break; |
| 352 | // Prefix defined as terminated by a nul or comma (,). |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 353 | if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 354 | return true; |
| 355 | } |
| 356 | } |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | // Returns true if the supplied reason prefix is considered known. |
| 361 | bool isKnownRebootReason(const std::string& r) { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 362 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 363 | // Prefix defined as terminated by a nul or comma (,). |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 364 | if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 365 | return true; |
| 366 | } |
| 367 | } |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | // If the reboot reason should be improved, report true if is too blunt. |
| 372 | bool isBluntRebootReason(const std::string& r) { |
| 373 | if (isStrongRebootReason(r)) return false; |
| 374 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 375 | if (!isKnownRebootReason(r)) return true; // Can not support unknown as detail |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 376 | |
| 377 | size_t pos = 0; |
| 378 | while ((pos = r.find(',', pos)) != std::string::npos) { |
| 379 | ++pos; |
| 380 | std::string next(r.substr(pos)); |
| 381 | if (next.length() == 0) break; |
| 382 | if (next[0] == ',') continue; |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 383 | if (!isKnownRebootReason(next)) return false; // Unknown subreason is good. |
| 384 | if (isStrongRebootReason(next)) return false; // eg: reboot,reboot |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 385 | } |
| 386 | return true; |
| 387 | } |
| 388 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 389 | bool readPstoreConsole(std::string& console) { |
| 390 | if (android::base::ReadFileToString("/sys/fs/pstore/console-ramoops-0", &console)) { |
| 391 | return true; |
| 392 | } |
| 393 | return android::base::ReadFileToString("/sys/fs/pstore/console-ramoops", &console); |
| 394 | } |
| 395 | |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 396 | // Implement a variant of std::string::rfind that is resilient to errors in |
| 397 | // the data stream being inspected. |
| 398 | class pstoreConsole { |
| 399 | private: |
| 400 | const size_t kBitErrorRate = 8; // number of bits per error |
| 401 | const std::string& console; |
| 402 | |
| 403 | // Number of bits that differ between the two arguments l and r. |
| 404 | // Returns zero if the values for l and r are identical. |
| 405 | size_t numError(uint8_t l, uint8_t r) const { return std::bitset<8>(l ^ r).count(); } |
| 406 | |
| 407 | // A string comparison function, reports the number of errors discovered |
| 408 | // in the match to a maximum of the bitLength / kBitErrorRate, at that |
| 409 | // point returning npos to indicate match is too poor. |
| 410 | // |
| 411 | // Since called in rfind which works backwards, expect cache locality will |
| 412 | // help if we check in reverse here as well for performance. |
| 413 | // |
| 414 | // Assumption: l (from console.c_str() + pos) is long enough to house |
| 415 | // _r.length(), checked in rfind caller below. |
| 416 | // |
| 417 | size_t numError(size_t pos, const std::string& _r) const { |
| 418 | const char* l = console.c_str() + pos; |
| 419 | const char* r = _r.c_str(); |
| 420 | size_t n = _r.length(); |
| 421 | const uint8_t* le = reinterpret_cast<const uint8_t*>(l) + n; |
| 422 | const uint8_t* re = reinterpret_cast<const uint8_t*>(r) + n; |
| 423 | size_t count = 0; |
| 424 | n = 0; |
| 425 | do { |
| 426 | // individual character bit error rate > threshold + slop |
| 427 | size_t num = numError(*--le, *--re); |
| 428 | if (num > ((8 + kBitErrorRate) / kBitErrorRate)) return std::string::npos; |
| 429 | // total bit error rate > threshold + slop |
| 430 | count += num; |
| 431 | ++n; |
| 432 | if (count > ((n * 8 + kBitErrorRate - (n > 2)) / kBitErrorRate)) { |
| 433 | return std::string::npos; |
| 434 | } |
| 435 | } while (le != reinterpret_cast<const uint8_t*>(l)); |
| 436 | return count; |
| 437 | } |
| 438 | |
| 439 | public: |
| 440 | explicit pstoreConsole(const std::string& console) : console(console) {} |
| 441 | // scope of argument must be equal to or greater than scope of pstoreConsole |
| 442 | explicit pstoreConsole(const std::string&& console) = delete; |
| 443 | explicit pstoreConsole(std::string&& console) = delete; |
| 444 | |
| 445 | // Our implementation of rfind, use exact match first, then resort to fuzzy. |
| 446 | size_t rfind(const std::string& needle) const { |
| 447 | size_t pos = console.rfind(needle); // exact match? |
| 448 | if (pos != std::string::npos) return pos; |
| 449 | |
| 450 | // Check to make sure needle fits in console string. |
| 451 | pos = console.length(); |
| 452 | if (needle.length() > pos) return std::string::npos; |
| 453 | pos -= needle.length(); |
| 454 | // fuzzy match to maximum kBitErrorRate |
Ivan Lozano | 44d3cac | 2017-11-07 13:13:55 -0800 | [diff] [blame] | 455 | for (;;) { |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 456 | if (numError(pos, needle) != std::string::npos) return pos; |
Ivan Lozano | 44d3cac | 2017-11-07 13:13:55 -0800 | [diff] [blame] | 457 | if (pos == 0) break; |
| 458 | --pos; |
| 459 | } |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 460 | return std::string::npos; |
| 461 | } |
| 462 | |
| 463 | // Our implementation of find, use only fuzzy match. |
| 464 | size_t find(const std::string& needle, size_t start = 0) const { |
| 465 | // Check to make sure needle fits in console string. |
| 466 | if (needle.length() > console.length()) return std::string::npos; |
| 467 | const size_t last_pos = console.length() - needle.length(); |
| 468 | // fuzzy match to maximum kBitErrorRate |
| 469 | for (size_t pos = start; pos <= last_pos; ++pos) { |
| 470 | if (numError(pos, needle) != std::string::npos) return pos; |
| 471 | } |
| 472 | return std::string::npos; |
| 473 | } |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame^] | 474 | |
| 475 | operator const std::string&() const { return console; } |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | // If bit error match to needle, correct it. |
| 479 | // Return true if any corrections were discovered and applied. |
Mark Salyzyn | 1e7d1c7 | 2018-03-16 08:57:20 -0700 | [diff] [blame] | 480 | bool correctForBitError(std::string& reason, const std::string& needle) { |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 481 | bool corrected = false; |
| 482 | if (reason.length() < needle.length()) return corrected; |
| 483 | const pstoreConsole console(reason); |
| 484 | const size_t last_pos = reason.length() - needle.length(); |
| 485 | for (size_t pos = 0; pos <= last_pos; pos += needle.length()) { |
| 486 | pos = console.find(needle, pos); |
| 487 | if (pos == std::string::npos) break; |
| 488 | |
| 489 | // exact match has no malice |
| 490 | if (needle == reason.substr(pos, needle.length())) continue; |
| 491 | |
| 492 | corrected = true; |
| 493 | reason = reason.substr(0, pos) + needle + reason.substr(pos + needle.length()); |
| 494 | } |
| 495 | return corrected; |
| 496 | } |
| 497 | |
Mark Salyzyn | 1e7d1c7 | 2018-03-16 08:57:20 -0700 | [diff] [blame] | 498 | // If bit error match to needle, correct it. |
| 499 | // Return true if any corrections were discovered and applied. |
| 500 | // Try again if we can replace underline with spaces. |
| 501 | bool correctForBitErrorOrUnderline(std::string& reason, const std::string& needle) { |
| 502 | bool corrected = correctForBitError(reason, needle); |
| 503 | std::string _needle(needle); |
| 504 | std::transform(_needle.begin(), _needle.end(), _needle.begin(), |
| 505 | [](char c) { return (c == '_') ? ' ' : c; }); |
| 506 | if (needle != _needle) { |
| 507 | corrected |= correctForBitError(reason, _needle); |
| 508 | } |
| 509 | return corrected; |
| 510 | } |
| 511 | |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame^] | 512 | // Converts a string value representing the reason the system booted to a |
| 513 | // string complying with Android system standard reason. |
| 514 | void transformReason(std::string& reason) { |
| 515 | std::transform(reason.begin(), reason.end(), reason.begin(), ::tolower); |
| 516 | std::transform(reason.begin(), reason.end(), reason.begin(), |
| 517 | [](char c) { return ::isblank(c) ? '_' : c; }); |
| 518 | std::transform(reason.begin(), reason.end(), reason.begin(), |
| 519 | [](char c) { return ::isprint(c) ? c : '?'; }); |
| 520 | } |
| 521 | |
| 522 | // Pull out and correct quoted (') subreason, pos just beyond first quote. |
| 523 | // Check subreasons for reboot,<subreason> and kernel_panic,sysrq,<subreason> |
| 524 | std::string getSubreason(const std::string& content, size_t pos) { |
| 525 | static constexpr size_t max_reason_length = 256; |
| 526 | |
| 527 | std::string subReason(content.substr(pos, max_reason_length)); |
| 528 | // Correct against any known strings that Bit Error Match |
| 529 | for (const auto& s : knownReasons) { |
| 530 | correctForBitErrorOrUnderline(subReason, s); |
| 531 | } |
| 532 | for (const auto& m : kBootReasonMap) { |
| 533 | if (m.first.length() <= strlen("cold")) continue; // too short? |
| 534 | if (correctForBitErrorOrUnderline(subReason, m.first + "'")) continue; |
| 535 | if (m.first.length() <= strlen("reboot,cold")) continue; // short? |
| 536 | if (android::base::StartsWith(m.first, "reboot,")) { |
| 537 | correctForBitErrorOrUnderline(subReason, m.first.substr(strlen("reboot,")) + "'"); |
| 538 | } else if (android::base::StartsWith(m.first, "kernel_panic,sysrq,")) { |
| 539 | correctForBitErrorOrUnderline(subReason, m.first.substr(strlen("kernel_panic,sysrq,")) + "'"); |
| 540 | } |
| 541 | } |
| 542 | for (pos = 0; pos < subReason.length(); ++pos) { |
| 543 | char c = subReason[pos]; |
| 544 | // #, &, %, / are common single bit error for ' that we can block |
| 545 | if (!::isprint(c) || (c == '\'') || (c == '#') || (c == '&') || (c == '%') || (c == '/')) { |
| 546 | subReason.erase(pos); |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | transformReason(subReason); |
| 551 | return subReason; |
| 552 | } |
| 553 | |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 554 | bool addKernelPanicSubReason(const pstoreConsole& console, std::string& ret) { |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 555 | // Check for kernel panic types to refine information |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 556 | if ((console.rfind("SysRq : Trigger a crash") != std::string::npos) || |
| 557 | (console.rfind("PC is at sysrq_handle_crash+") != std::string::npos)) { |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 558 | ret = "kernel_panic,sysrq"; |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame^] | 559 | // Invented for Android to allow daemons that specifically trigger sysrq |
| 560 | // to communicate more accurate boot subreasons via last console messages. |
| 561 | static constexpr char sysrqSubreason[] = "SysRq : Trigger a crash : '"; |
| 562 | auto pos = console.rfind(sysrqSubreason); |
| 563 | if (pos != std::string::npos) { |
| 564 | ret += "," + getSubreason(console, pos + strlen(sysrqSubreason)); |
| 565 | } |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 566 | return true; |
| 567 | } |
| 568 | if (console.rfind("Unable to handle kernel NULL pointer dereference at virtual address") != |
| 569 | std::string::npos) { |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 570 | ret = "kernel_panic,null"; |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 571 | return true; |
| 572 | } |
| 573 | if (console.rfind("Kernel BUG at ") != std::string::npos) { |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 574 | ret = "kernel_panic,bug"; |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 575 | return true; |
| 576 | } |
| 577 | return false; |
| 578 | } |
| 579 | |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 580 | bool addKernelPanicSubReason(const std::string& content, std::string& ret) { |
| 581 | return addKernelPanicSubReason(pstoreConsole(content), ret); |
| 582 | } |
| 583 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 584 | const char system_reboot_reason_property[] = "sys.boot.reason"; |
| 585 | const char last_reboot_reason_property[] = LAST_REBOOT_REASON_PROPERTY; |
| 586 | const char bootloader_reboot_reason_property[] = "ro.boot.bootreason"; |
| 587 | |
| 588 | // Scrub, Sanitize, Standardize and Enhance the boot reason string supplied. |
| 589 | std::string BootReasonStrToReason(const std::string& boot_reason) { |
| 590 | std::string ret(GetProperty(system_reboot_reason_property)); |
| 591 | std::string reason(boot_reason); |
| 592 | // If sys.boot.reason == ro.boot.bootreason, let's re-evaluate |
| 593 | if (reason == ret) ret = ""; |
| 594 | |
Mark Salyzyn | 88d692c | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 595 | transformReason(reason); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 596 | |
| 597 | // Is the current system boot reason sys.boot.reason valid? |
| 598 | if (!isKnownRebootReason(ret)) ret = ""; |
| 599 | |
| 600 | if (ret == "") { |
| 601 | // Is the bootloader boot reason ro.boot.bootreason known? |
| 602 | std::vector<std::string> words(android::base::Split(reason, ",_-")); |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 603 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 604 | std::string blunt; |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 605 | for (auto& r : words) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 606 | if (r == s) { |
| 607 | if (isBluntRebootReason(s)) { |
| 608 | blunt = s; |
| 609 | } else { |
| 610 | ret = s; |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | if (ret == "") ret = blunt; |
| 616 | if (ret != "") break; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | if (ret == "") { |
| 621 | // A series of checks to take some officially unsupported reasons |
| 622 | // reported by the bootloader and find some logical and canonical |
| 623 | // sense. In an ideal world, we would require those bootloaders |
Mark Salyzyn | 25900dd | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 624 | // to behave and follow our CTS standards. |
| 625 | // |
| 626 | // first member is the output |
| 627 | // second member is an unanchored regex for an alias |
| 628 | // |
Mark Salyzyn | 2819328 | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 629 | // If output has a prefix of <bang> '!', we do not use it as a |
| 630 | // match needle (and drop the <bang> prefix when landing in output), |
| 631 | // otherwise look for it as well. This helps keep the scale of the |
Mark Salyzyn | 25900dd | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 632 | // following table smaller. |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 633 | 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] | 634 | {"watchdog", "wdog"}, |
Mark Salyzyn | 25900dd | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 635 | {"cold,powerkey", "powerkey|power_key|PowerKey"}, |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 636 | {"kernel_panic", "panic"}, |
| 637 | {"shutdown,thermal", "thermal"}, |
| 638 | {"warm,s3_wakeup", "s3_wakeup"}, |
| 639 | {"hard,hw_reset", "hw_reset"}, |
Mark Salyzyn | 8aa36c6 | 2018-03-16 11:00:14 -0700 | [diff] [blame] | 640 | {"cold,charger", "usb"}, |
| 641 | {"cold,rtc", "rtc"}, |
Mark Salyzyn | cabbe4f | 2017-10-23 13:52:39 -0700 | [diff] [blame] | 642 | {"reboot,2sec", "2sec_reboot"}, |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 643 | {"bootloader", ""}, |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 644 | }; |
| 645 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 646 | for (auto& s : aliasReasons) { |
Mark Salyzyn | 2819328 | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 647 | size_t firstHasNot = s.first[0] == '!'; |
| 648 | if (!firstHasNot && (reason.find(s.first) != std::string::npos)) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 649 | ret = s.first; |
| 650 | break; |
| 651 | } |
Mark Salyzyn | 25900dd | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 652 | if (s.second.size() && std::regex_search(reason, std::regex(s.second))) { |
Mark Salyzyn | 2819328 | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 653 | ret = s.first.substr(firstHasNot); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 654 | break; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | // If watchdog is the reason, see if there is a security angle? |
| 660 | if (ret == "watchdog") { |
| 661 | if (reason.find("sec") != std::string::npos) { |
| 662 | ret += ",security"; |
| 663 | } |
| 664 | } |
| 665 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 666 | if (ret == "kernel_panic") { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 667 | // Check to see if last klog has some refinement hints. |
| 668 | std::string content; |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 669 | if (readPstoreConsole(content)) { |
| 670 | addKernelPanicSubReason(content, ret); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 671 | } |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 672 | } else if (isBluntRebootReason(ret)) { |
| 673 | // Check the other available reason resources if the reason is still blunt. |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 674 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 675 | // Check to see if last klog has some refinement hints. |
| 676 | std::string content; |
| 677 | if (readPstoreConsole(content)) { |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 678 | const pstoreConsole console(content); |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 679 | // The toybox reboot command used directly (unlikely)? But also |
| 680 | // catches init's response to Android's more controlled reboot command. |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 681 | if (console.rfind("reboot: Power down") != std::string::npos) { |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 682 | ret = "shutdown"; // Still too blunt, but more accurate. |
| 683 | // ToDo: init should record the shutdown reason to kernel messages ala: |
| 684 | // init: shutdown system with command 'last_reboot_reason' |
| 685 | // so that if pstore has persistence we can get some details |
| 686 | // that could be missing in last_reboot_reason_property. |
| 687 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 688 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 689 | static const char cmd[] = "reboot: Restarting system with command '"; |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 690 | size_t pos = console.rfind(cmd); |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 691 | if (pos != std::string::npos) { |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame^] | 692 | std::string subReason(getSubreason(content, pos + strlen(cmd))); |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 693 | if (subReason != "") { // Will not land "reboot" as that is too blunt. |
| 694 | if (isKernelRebootReason(subReason)) { |
| 695 | ret = "reboot," + subReason; // User space can't talk kernel reasons. |
Mark Salyzyn | dafced9 | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 696 | } else if (isKnownRebootReason(subReason)) { |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 697 | ret = subReason; |
Mark Salyzyn | dafced9 | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 698 | } else { |
| 699 | ret = "reboot," + subReason; // legitimize unknown reasons |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 700 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 703 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 704 | // Check for kernel panics, allowed to override reboot command. |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 705 | if (!addKernelPanicSubReason(console, ret) && |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 706 | // check for long-press power down |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 707 | ((console.rfind("Power held for ") != std::string::npos) || |
| 708 | (console.rfind("charger: [") != std::string::npos))) { |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 709 | ret = "cold"; |
| 710 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | // The following battery test should migrate to a default system health HAL |
| 714 | |
| 715 | // Let us not worry if the reboot command was issued, for the cases of |
| 716 | // reboot -p, reboot <no reason>, reboot cold, reboot warm and reboot hard. |
| 717 | // Same for bootloader and ro.boot.bootreasons of this set, but a dead |
| 718 | // battery could conceivably lead to these, so worthy of override. |
| 719 | if (isBluntRebootReason(ret)) { |
| 720 | // Heuristic to determine if shutdown possibly because of a dead battery? |
| 721 | // Really a hail-mary pass to find it in last klog content ... |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 722 | static const int battery_dead_threshold = 2; // percent |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 723 | static const char battery[] = "healthd: battery l="; |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 724 | const pstoreConsole console(content); |
| 725 | size_t pos = console.rfind(battery); // last one |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 726 | std::string digits; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 727 | if (pos != std::string::npos) { |
Mark Salyzyn | 747c0e6 | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 728 | digits = content.substr(pos + strlen(battery), strlen("100 ")); |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 729 | // correct common errors |
Mark Salyzyn | 1e7d1c7 | 2018-03-16 08:57:20 -0700 | [diff] [blame] | 730 | correctForBitError(digits, "100 "); |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 731 | if (digits[0] == '!') digits[0] = '1'; |
| 732 | if (digits[1] == '!') digits[1] = '1'; |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 733 | } |
Mark Salyzyn | 747c0e6 | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 734 | const char* endptr = digits.c_str(); |
| 735 | unsigned level = 0; |
| 736 | while (::isdigit(*endptr)) { |
| 737 | level *= 10; |
| 738 | level += *endptr++ - '0'; |
| 739 | // make sure no leading zeros, except zero itself, and range check. |
| 740 | if ((level == 0) || (level > 100)) break; |
| 741 | } |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 742 | // example bit error rate issues for 10% |
| 743 | // 'l=10 ' no bits in error |
| 744 | // 'l=00 ' single bit error (fails above) |
| 745 | // 'l=1 ' single bit error |
| 746 | // 'l=0 ' double bit error |
| 747 | // There are others, not typically critical because of 2% |
| 748 | // battery_dead_threshold. KISS check, make sure second |
| 749 | // character after digit sequence is not a space. |
| 750 | if ((level <= 100) && (endptr != digits.c_str()) && (endptr[0] == ' ') && (endptr[1] != ' ')) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 751 | LOG(INFO) << "Battery level at shutdown " << level << "%"; |
| 752 | if (level <= battery_dead_threshold) { |
| 753 | ret = "shutdown,battery"; |
| 754 | } |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 755 | } else { // Most likely |
| 756 | digits = ""; // reset digits |
| 757 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 758 | // Content buffer no longer will have console data. Beware if more |
| 759 | // checks added below, that depend on parsing console content. |
| 760 | content = ""; |
| 761 | |
| 762 | LOG(DEBUG) << "Can not find last low battery in last console messages"; |
| 763 | android_logcat_context ctx = create_android_logcat(); |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 764 | FILE* fp = android_logcat_popen(&ctx, "logcat -b kernel -v brief -d"); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 765 | if (fp != nullptr) { |
| 766 | android::base::ReadFdToString(fileno(fp), &content); |
| 767 | } |
| 768 | android_logcat_pclose(&ctx, fp); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 769 | static const char logcat_battery[] = "W/healthd ( 0): battery l="; |
| 770 | const char* match = logcat_battery; |
| 771 | |
| 772 | if (content == "") { |
| 773 | // Service logd.klog not running, go to smaller buffer in the kernel. |
| 774 | int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0); |
| 775 | if (rc > 0) { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 776 | ssize_t len = rc + 1024; // 1K Margin should it grow between calls. |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 777 | std::unique_ptr<char[]> buf(new char[len]); |
| 778 | rc = klogctl(KLOG_READ_ALL, buf.get(), len); |
| 779 | if (rc < len) { |
| 780 | len = rc + 1; |
| 781 | } |
| 782 | buf[--len] = '\0'; |
| 783 | content = buf.get(); |
| 784 | } |
| 785 | match = battery; |
| 786 | } |
| 787 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 788 | pos = content.find(match); // The first one it finds. |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 789 | if (pos != std::string::npos) { |
Mark Salyzyn | 747c0e6 | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 790 | digits = content.substr(pos + strlen(match), strlen("100 ")); |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 791 | } |
Mark Salyzyn | 747c0e6 | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 792 | endptr = digits.c_str(); |
| 793 | level = 0; |
| 794 | while (::isdigit(*endptr)) { |
| 795 | level *= 10; |
| 796 | level += *endptr++ - '0'; |
| 797 | // make sure no leading zeros, except zero itself, and range check. |
| 798 | if ((level == 0) || (level > 100)) break; |
| 799 | } |
Mark Salyzyn | a16e437 | 2017-09-20 08:36:12 -0700 | [diff] [blame] | 800 | if ((level <= 100) && (endptr != digits.c_str()) && (*endptr == ' ')) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 801 | LOG(INFO) << "Battery level at startup " << level << "%"; |
| 802 | if (level <= battery_dead_threshold) { |
| 803 | ret = "shutdown,battery"; |
| 804 | } |
| 805 | } else { |
| 806 | LOG(DEBUG) << "Can not find first battery level in dmesg or logcat"; |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | // Is there a controlled shutdown hint in last_reboot_reason_property? |
| 812 | if (isBluntRebootReason(ret)) { |
| 813 | // Content buffer no longer will have console data. Beware if more |
| 814 | // checks added below, that depend on parsing console content. |
| 815 | content = GetProperty(last_reboot_reason_property); |
Mark Salyzyn | 88d692c | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 816 | transformReason(content); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 817 | |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 818 | // Anything in last is better than 'super-blunt' reboot or shutdown. |
| 819 | if ((ret == "") || (ret == "reboot") || (ret == "shutdown") || !isBluntRebootReason(content)) { |
| 820 | ret = content; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
| 824 | // Other System Health HAL reasons? |
| 825 | |
| 826 | // ToDo: /proc/sys/kernel/boot_reason needs a HAL interface to |
| 827 | // possibly offer hardware-specific clues from the PMIC. |
| 828 | } |
| 829 | |
| 830 | // If unknown left over from above, make it "reboot,<boot_reason>" |
| 831 | if (ret == "") { |
| 832 | ret = "reboot"; |
| 833 | if (android::base::StartsWith(reason, "reboot")) { |
| 834 | reason = reason.substr(strlen("reboot")); |
Mark Salyzyn | 0af71a5 | 2017-10-05 13:58:04 -0700 | [diff] [blame] | 835 | while ((reason[0] == ',') || (reason[0] == '_')) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 836 | reason = reason.substr(1); |
| 837 | } |
| 838 | } |
| 839 | if (reason != "") { |
| 840 | ret += ","; |
| 841 | ret += reason; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | LOG(INFO) << "Canonical boot reason: " << ret; |
| 846 | if (isKernelRebootReason(ret) && (GetProperty(last_reboot_reason_property) != "")) { |
| 847 | // Rewrite as it must be old news, kernel reasons trump user space. |
| 848 | SetProperty(last_reboot_reason_property, ret); |
| 849 | } |
| 850 | return ret; |
| 851 | } |
| 852 | |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 853 | // Returns the appropriate metric key prefix for the boot_complete metric such |
| 854 | // that boot metrics after a system update are labeled as ota_boot_complete; |
| 855 | // otherwise, they are labeled as boot_complete. This method encapsulates the |
| 856 | // bookkeeping required to track when a system update has occurred by storing |
| 857 | // the UTC timestamp of the system build date and comparing against the current |
| 858 | // system build date. |
| 859 | std::string CalculateBootCompletePrefix() { |
| 860 | static const std::string kBuildDateKey = "build_date"; |
| 861 | std::string boot_complete_prefix = "boot_complete"; |
| 862 | |
| 863 | std::string build_date_str = GetProperty("ro.build.date.utc"); |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 864 | int32_t build_date; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 865 | if (!android::base::ParseInt(build_date_str, &build_date)) { |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 866 | return std::string(); |
| 867 | } |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 868 | |
| 869 | BootEventRecordStore boot_event_store; |
| 870 | BootEventRecordStore::BootEventRecord record; |
James Hawkins | 0bc4ad4 | 2017-05-30 15:03:15 -0700 | [diff] [blame] | 871 | if (!boot_event_store.GetBootEvent(kBuildDateKey, &record)) { |
| 872 | boot_complete_prefix = "factory_reset_" + boot_complete_prefix; |
| 873 | boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date); |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 874 | LOG(INFO) << "Canonical boot reason: reboot,factory_reset"; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 875 | SetProperty(system_reboot_reason_property, "reboot,factory_reset"); |
James Hawkins | 0bc4ad4 | 2017-05-30 15:03:15 -0700 | [diff] [blame] | 876 | } else if (build_date != record.second) { |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 877 | boot_complete_prefix = "ota_" + boot_complete_prefix; |
| 878 | boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date); |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 879 | LOG(INFO) << "Canonical boot reason: reboot,ota"; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 880 | SetProperty(system_reboot_reason_property, "reboot,ota"); |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | return boot_complete_prefix; |
| 884 | } |
| 885 | |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 886 | // Records the value of a given ro.boottime.init property in milliseconds. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 887 | void RecordInitBootTimeProp(BootEventRecordStore* boot_event_store, const char* property) { |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 888 | std::string value = GetProperty(property); |
| 889 | |
James Hawkins | 27c0522 | 2017-01-26 11:55:44 -0800 | [diff] [blame] | 890 | int32_t time_in_ms; |
| 891 | if (android::base::ParseInt(value, &time_in_ms)) { |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 892 | boot_event_store->AddBootEventWithValue(property, time_in_ms); |
| 893 | } |
| 894 | } |
| 895 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 896 | // A map from bootloader timing stage to the time that stage took during boot. |
| 897 | typedef std::map<std::string, int32_t> BootloaderTimingMap; |
| 898 | |
| 899 | // Returns a mapping from bootloader stage names to the time those stages |
| 900 | // took to boot. |
| 901 | const BootloaderTimingMap GetBootLoaderTimings() { |
| 902 | BootloaderTimingMap timings; |
| 903 | |
| 904 | // |ro.boot.boottime| is of the form 'stage1:time1,...,stageN:timeN', |
| 905 | // where timeN is in milliseconds. |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 906 | std::string value = GetProperty("ro.boot.boottime"); |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 907 | if (value.empty()) { |
| 908 | // ro.boot.boottime is not reported on all devices. |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 909 | return BootloaderTimingMap(); |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 910 | } |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 911 | |
| 912 | auto stages = android::base::Split(value, ","); |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 913 | for (const auto& stageTiming : stages) { |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 914 | // |stageTiming| is of the form 'stage:time'. |
| 915 | auto stageTimingValues = android::base::Split(stageTiming, ":"); |
James Hawkins | 0bc4ad4 | 2017-05-30 15:03:15 -0700 | [diff] [blame] | 916 | DCHECK_EQ(2U, stageTimingValues.size()); |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 917 | |
| 918 | std::string stageName = stageTimingValues[0]; |
| 919 | int32_t time_ms; |
| 920 | if (android::base::ParseInt(stageTimingValues[1], &time_ms)) { |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 921 | timings[stageName] = time_ms; |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 922 | } |
| 923 | } |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 924 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 925 | return timings; |
| 926 | } |
| 927 | |
| 928 | // Parses and records the set of bootloader stages and associated boot times |
| 929 | // from the ro.boot.boottime system property. |
| 930 | void RecordBootloaderTimings(BootEventRecordStore* boot_event_store, |
| 931 | const BootloaderTimingMap& bootloader_timings) { |
| 932 | int32_t total_time = 0; |
| 933 | for (const auto& timing : bootloader_timings) { |
| 934 | total_time += timing.second; |
| 935 | boot_event_store->AddBootEventWithValue("boottime.bootloader." + timing.first, timing.second); |
| 936 | } |
| 937 | |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 938 | boot_event_store->AddBootEventWithValue("boottime.bootloader.total", total_time); |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 939 | } |
| 940 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 941 | // Records the closest estimation to the absolute device boot time, i.e., |
| 942 | // from power on to boot_complete, including bootloader times. |
| 943 | void RecordAbsoluteBootTime(BootEventRecordStore* boot_event_store, |
| 944 | const BootloaderTimingMap& bootloader_timings, |
| 945 | std::chrono::milliseconds uptime) { |
| 946 | int32_t bootloader_time_ms = 0; |
| 947 | |
| 948 | for (const auto& timing : bootloader_timings) { |
| 949 | if (timing.first.compare("SW") != 0) { |
| 950 | bootloader_time_ms += timing.second; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | auto bootloader_duration = std::chrono::milliseconds(bootloader_time_ms); |
| 955 | auto absolute_total = |
| 956 | std::chrono::duration_cast<std::chrono::seconds>(bootloader_duration + uptime); |
| 957 | boot_event_store->AddBootEventWithValue("absolute_boot_time", absolute_total.count()); |
| 958 | } |
| 959 | |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 960 | // Records several metrics related to the time it takes to boot the device, |
| 961 | // including disambiguating boot time on encrypted or non-encrypted devices. |
| 962 | void RecordBootComplete() { |
| 963 | BootEventRecordStore boot_event_store; |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 964 | BootEventRecordStore::BootEventRecord record; |
James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 965 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 966 | auto time_since_epoch = android::base::boot_clock::now().time_since_epoch(); |
| 967 | auto uptime = std::chrono::duration_cast<std::chrono::seconds>(time_since_epoch); |
James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 968 | time_t current_time_utc = time(nullptr); |
| 969 | |
| 970 | if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) { |
| 971 | time_t last_boot_time_utc = record.second; |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 972 | time_t time_since_last_boot = difftime(current_time_utc, last_boot_time_utc); |
| 973 | boot_event_store.AddBootEventWithValue("time_since_last_boot", time_since_last_boot); |
James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 977 | |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 978 | // The boot_complete metric has two variants: boot_complete and |
| 979 | // ota_boot_complete. The latter signifies that the device is booting after |
| 980 | // a system update. |
| 981 | std::string boot_complete_prefix = CalculateBootCompletePrefix(); |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 982 | if (boot_complete_prefix.empty()) { |
| 983 | // The system is hosed because the build date property could not be read. |
| 984 | return; |
| 985 | } |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 986 | |
| 987 | // post_decrypt_time_elapsed is only logged on encrypted devices. |
| 988 | if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) { |
| 989 | // Log the amount of time elapsed until the device is decrypted, which |
| 990 | // includes the variable amount of time the user takes to enter the |
| 991 | // decryption password. |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 992 | boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime.count()); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 993 | |
| 994 | // Subtract the decryption time to normalize the boot cycle timing. |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 995 | std::chrono::seconds boot_complete = std::chrono::seconds(uptime.count() - record.second); |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 996 | boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt", |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 997 | boot_complete.count()); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 998 | } else { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 999 | boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption", uptime.count()); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | // Record the total time from device startup to boot complete, regardless of |
| 1003 | // encryption state. |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 1004 | boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime.count()); |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 1005 | |
| 1006 | RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init"); |
| 1007 | RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux"); |
| 1008 | RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait"); |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 1009 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 1010 | const BootloaderTimingMap bootloader_timings = GetBootLoaderTimings(); |
| 1011 | RecordBootloaderTimings(&boot_event_store, bootloader_timings); |
| 1012 | |
| 1013 | auto uptime_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_since_epoch); |
| 1014 | RecordAbsoluteBootTime(&boot_event_store, bootloader_timings, uptime_ms); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1017 | // Records the boot_reason metric by querying the ro.boot.bootreason system |
| 1018 | // property. |
| 1019 | void RecordBootReason() { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 1020 | const std::string reason(GetProperty(bootloader_reboot_reason_property)); |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 1021 | |
| 1022 | if (reason.empty()) { |
| 1023 | // Log an empty boot reason value as '<EMPTY>' to ensure the value is intentional |
| 1024 | // (and not corruption anywhere else in the reporting pipeline). |
| 1025 | android::metricslogger::LogMultiAction(android::metricslogger::ACTION_BOOT, |
| 1026 | android::metricslogger::FIELD_PLATFORM_REASON, "<EMPTY>"); |
| 1027 | } else { |
| 1028 | android::metricslogger::LogMultiAction(android::metricslogger::ACTION_BOOT, |
| 1029 | android::metricslogger::FIELD_PLATFORM_REASON, reason); |
| 1030 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 1031 | |
| 1032 | // Log the raw bootloader_boot_reason property value. |
| 1033 | int32_t boot_reason = BootReasonStrToEnum(reason); |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1034 | BootEventRecordStore boot_event_store; |
| 1035 | boot_event_store.AddBootEventWithValue("boot_reason", boot_reason); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 1036 | |
| 1037 | // Log the scrubbed system_boot_reason. |
| 1038 | const std::string system_reason(BootReasonStrToReason(reason)); |
| 1039 | int32_t system_boot_reason = BootReasonStrToEnum(system_reason); |
| 1040 | boot_event_store.AddBootEventWithValue("system_boot_reason", system_boot_reason); |
| 1041 | |
| 1042 | // Record the scrubbed system_boot_reason to the property |
| 1043 | SetProperty(system_reboot_reason_property, system_reason); |
| 1044 | if (reason == "") { |
| 1045 | SetProperty(bootloader_reboot_reason_property, system_reason); |
| 1046 | } |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 1049 | // Records two metrics related to the user resetting a device: the time at |
| 1050 | // which the device is reset, and the time since the user last reset the |
| 1051 | // device. The former is only set once per-factory reset. |
| 1052 | void RecordFactoryReset() { |
| 1053 | BootEventRecordStore boot_event_store; |
| 1054 | BootEventRecordStore::BootEventRecord record; |
| 1055 | |
| 1056 | time_t current_time_utc = time(nullptr); |
| 1057 | |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 1058 | if (current_time_utc < 0) { |
| 1059 | // UMA does not display negative values in buckets, so convert to positive. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1060 | android::metricslogger::LogHistogram("factory_reset_current_time_failure", |
| 1061 | std::abs(current_time_utc)); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1062 | |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 1063 | // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1064 | // is losing records somehow. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1065 | boot_event_store.AddBootEventWithValue("factory_reset_current_time_failure", |
| 1066 | std::abs(current_time_utc)); |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 1067 | return; |
| 1068 | } else { |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 1069 | android::metricslogger::LogHistogram("factory_reset_current_time", current_time_utc); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1070 | |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 1071 | // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1072 | // is losing records somehow. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1073 | boot_event_store.AddBootEventWithValue("factory_reset_current_time", current_time_utc); |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 1076 | // The factory_reset boot event does not exist after the device is reset, so |
| 1077 | // use this signal to mark the time of the factory reset. |
| 1078 | if (!boot_event_store.GetBootEvent("factory_reset", &record)) { |
| 1079 | boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc); |
James Hawkins | 3bf9b14 | 2016-03-03 14:50:24 -0800 | [diff] [blame] | 1080 | |
| 1081 | // Don't log the time_since_factory_reset until some time has elapsed. |
| 1082 | // The data is not meaningful yet and skews the histogram buckets. |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 1083 | return; |
| 1084 | } |
| 1085 | |
| 1086 | // Calculate and record the difference in time between now and the |
| 1087 | // factory_reset time. |
| 1088 | time_t factory_reset_utc = record.second; |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 1089 | android::metricslogger::LogHistogram("factory_reset_record_value", factory_reset_utc); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1090 | |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 1091 | // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1092 | // is losing records somehow. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1093 | boot_event_store.AddBootEventWithValue("factory_reset_record_value", factory_reset_utc); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1094 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1095 | time_t time_since_factory_reset = difftime(current_time_utc, factory_reset_utc); |
| 1096 | boot_event_store.AddBootEventWithValue("time_since_factory_reset", time_since_factory_reset); |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1099 | } // namespace |
| 1100 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1101 | int main(int argc, char** argv) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1102 | android::base::InitLogging(argv); |
| 1103 | |
| 1104 | const std::string cmd_line = GetCommandLine(argc, argv); |
| 1105 | LOG(INFO) << "Service started: " << cmd_line; |
| 1106 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1107 | int option_index = 0; |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 1108 | static const char value_str[] = "value"; |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1109 | static const char boot_complete_str[] = "record_boot_complete"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1110 | static const char boot_reason_str[] = "record_boot_reason"; |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 1111 | static const char factory_reset_str[] = "record_time_since_factory_reset"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1112 | static const struct option long_options[] = { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1113 | // clang-format off |
| 1114 | { "help", no_argument, NULL, 'h' }, |
| 1115 | { "log", no_argument, NULL, 'l' }, |
| 1116 | { "print", no_argument, NULL, 'p' }, |
| 1117 | { "record", required_argument, NULL, 'r' }, |
| 1118 | { value_str, required_argument, NULL, 0 }, |
| 1119 | { boot_complete_str, no_argument, NULL, 0 }, |
| 1120 | { boot_reason_str, no_argument, NULL, 0 }, |
| 1121 | { factory_reset_str, no_argument, NULL, 0 }, |
| 1122 | { NULL, 0, NULL, 0 } |
| 1123 | // clang-format on |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1124 | }; |
| 1125 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 1126 | std::string boot_event; |
| 1127 | std::string value; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1128 | int opt = 0; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1129 | while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1130 | switch (opt) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1131 | // This case handles long options which have no single-character mapping. |
| 1132 | case 0: { |
| 1133 | const std::string option_name = long_options[option_index].name; |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 1134 | if (option_name == value_str) { |
| 1135 | // |optarg| is an external variable set by getopt representing |
| 1136 | // the option argument. |
| 1137 | value = optarg; |
| 1138 | } else if (option_name == boot_complete_str) { |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1139 | RecordBootComplete(); |
| 1140 | } else if (option_name == boot_reason_str) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1141 | RecordBootReason(); |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 1142 | } else if (option_name == factory_reset_str) { |
| 1143 | RecordFactoryReset(); |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1144 | } else { |
| 1145 | LOG(ERROR) << "Invalid option: " << option_name; |
| 1146 | } |
| 1147 | break; |
| 1148 | } |
| 1149 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1150 | case 'h': { |
| 1151 | ShowHelp(argv[0]); |
| 1152 | break; |
| 1153 | } |
| 1154 | |
| 1155 | case 'l': { |
| 1156 | LogBootEvents(); |
| 1157 | break; |
| 1158 | } |
| 1159 | |
| 1160 | case 'p': { |
| 1161 | PrintBootEvents(); |
| 1162 | break; |
| 1163 | } |
| 1164 | |
| 1165 | case 'r': { |
| 1166 | // |optarg| is an external variable set by getopt representing |
| 1167 | // the option argument. |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 1168 | boot_event = optarg; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1169 | break; |
| 1170 | } |
| 1171 | |
| 1172 | default: { |
| 1173 | DCHECK_EQ(opt, '?'); |
| 1174 | |
| 1175 | // |optopt| is an external variable set by getopt representing |
| 1176 | // the value of the invalid option. |
| 1177 | LOG(ERROR) << "Invalid option: " << optopt; |
| 1178 | ShowHelp(argv[0]); |
| 1179 | return EXIT_FAILURE; |
| 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 1184 | if (!boot_event.empty()) { |
| 1185 | RecordBootEventFromCommandLine(boot_event, value); |
| 1186 | } |
| 1187 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1188 | return 0; |
| 1189 | } |