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