| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2016 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | // The bootstat command provides options to persist boot events with the current | 
 | 18 | // timestamp, dump the persisted events, and log all events to EventLog to be | 
 | 19 | // uploaded to Android log storage via Tron. | 
 | 20 |  | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 21 | #include <getopt.h> | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 22 | #include <unistd.h> | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 23 |  | 
| James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 24 | #include <chrono> | 
| James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 25 | #include <cmath> | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 26 | #include <cstddef> | 
 | 27 | #include <cstdio> | 
| James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 28 | #include <ctime> | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 29 | #include <map> | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 30 | #include <memory> | 
 | 31 | #include <string> | 
| James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 32 | #include <vector> | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 33 |  | 
| James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 34 | #include <android-base/chrono_utils.h> | 
| James Hawkins | eabe08b | 2016-01-19 16:54:35 -0800 | [diff] [blame] | 35 | #include <android-base/logging.h> | 
| James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 36 | #include <android-base/parseint.h> | 
| James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 37 | #include <android-base/strings.h> | 
| James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 38 | #include <android/log.h> | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 39 | #include <cutils/properties.h> | 
| James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 40 | #include <metricslogger/metrics_logger.h> | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 41 |  | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 42 | #include "boot_event_record_store.h" | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 43 |  | 
 | 44 | namespace { | 
 | 45 |  | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 46 | // Scans the boot event record store for record files and logs each boot event | 
 | 47 | // via EventLog. | 
 | 48 | void LogBootEvents() { | 
 | 49 |   BootEventRecordStore boot_event_store; | 
 | 50 |  | 
 | 51 |   auto events = boot_event_store.GetAllBootEvents(); | 
 | 52 |   for (auto i = events.cbegin(); i != events.cend(); ++i) { | 
| James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 53 |     android::metricslogger::LogHistogram(i->first, i->second); | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 54 |   } | 
 | 55 | } | 
 | 56 |  | 
| James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 57 | // Records the named boot |event| to the record store. If |value| is non-empty | 
 | 58 | // and is a proper string representation of an integer value, the converted | 
 | 59 | // integer value is associated with the boot event. | 
 | 60 | void RecordBootEventFromCommandLine( | 
 | 61 |     const std::string& event, const std::string& value_str) { | 
 | 62 |   BootEventRecordStore boot_event_store; | 
 | 63 |   if (!value_str.empty()) { | 
 | 64 |     int32_t value = 0; | 
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 65 |     if (android::base::ParseInt(value_str, &value)) { | 
| James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 66 |       boot_event_store.AddBootEventWithValue(event, value); | 
 | 67 |     } | 
| James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 68 |   } else { | 
 | 69 |     boot_event_store.AddBootEvent(event); | 
 | 70 |   } | 
 | 71 | } | 
 | 72 |  | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 73 | void PrintBootEvents() { | 
 | 74 |   printf("Boot events:\n"); | 
 | 75 |   printf("------------\n"); | 
 | 76 |  | 
 | 77 |   BootEventRecordStore boot_event_store; | 
 | 78 |   auto events = boot_event_store.GetAllBootEvents(); | 
 | 79 |   for (auto i = events.cbegin(); i != events.cend(); ++i) { | 
 | 80 |     printf("%s\t%d\n", i->first.c_str(), i->second); | 
 | 81 |   } | 
 | 82 | } | 
 | 83 |  | 
 | 84 | void ShowHelp(const char *cmd) { | 
 | 85 |   fprintf(stderr, "Usage: %s [options]\n", cmd); | 
 | 86 |   fprintf(stderr, | 
 | 87 |           "options include:\n" | 
| Yongqin Liu | 78b2b94 | 2017-07-07 13:26:49 +0800 | [diff] [blame] | 88 |           "  -h, --help              Show this help\n" | 
 | 89 |           "  -l, --log               Log all metrics to logstorage\n" | 
 | 90 |           "  -p, --print             Dump the boot event records to the console\n" | 
 | 91 |           "  -r, --record            Record the timestamp of a named boot event\n" | 
 | 92 |           "  --value                 Optional value to associate with the boot event\n" | 
 | 93 |           "  --record_boot_complete  Record metrics related to the time for the device boot\n" | 
 | 94 |           "  --record_boot_reason    Record the reason why the device booted\n" | 
| James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 95 |           "  --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] | 96 | } | 
 | 97 |  | 
 | 98 | // Constructs a readable, printable string from the givencommand line | 
 | 99 | // arguments. | 
 | 100 | std::string GetCommandLine(int argc, char **argv) { | 
 | 101 |   std::string cmd; | 
 | 102 |   for (int i = 0; i < argc; ++i) { | 
 | 103 |     cmd += argv[i]; | 
 | 104 |     cmd += " "; | 
 | 105 |   } | 
 | 106 |  | 
 | 107 |   return cmd; | 
 | 108 | } | 
 | 109 |  | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 110 | // Convenience wrapper over the property API that returns an | 
 | 111 | // std::string. | 
 | 112 | std::string GetProperty(const char* key) { | 
 | 113 |   std::vector<char> temp(PROPERTY_VALUE_MAX); | 
 | 114 |   const int len = property_get(key, &temp[0], nullptr); | 
 | 115 |   if (len < 0) { | 
 | 116 |     return ""; | 
 | 117 |   } | 
 | 118 |   return std::string(&temp[0], len); | 
 | 119 | } | 
 | 120 |  | 
| James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 121 | constexpr int32_t kUnknownBootReason = 1; | 
 | 122 |  | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 123 | // A mapping from boot reason string, as read from the ro.boot.bootreason | 
 | 124 | // system property, to a unique integer ID. Viewers of log data dashboards for | 
 | 125 | // the boot_reason metric may refer to this mapping to discern the histogram | 
 | 126 | // values. | 
| James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 127 | const std::map<std::string, int32_t> kBootReasonMap = { | 
 | 128 |   {"unknown", kUnknownBootReason}, | 
 | 129 |   {"normal", 2}, | 
 | 130 |   {"recovery", 3}, | 
 | 131 |   {"reboot", 4}, | 
 | 132 |   {"PowerKey", 5}, | 
 | 133 |   {"hard_reset", 6}, | 
 | 134 |   {"kernel_panic", 7}, | 
 | 135 |   {"rpm_err", 8}, | 
 | 136 |   {"hw_reset", 9}, | 
 | 137 |   {"tz_err", 10}, | 
 | 138 |   {"adsp_err", 11}, | 
 | 139 |   {"modem_err", 12}, | 
 | 140 |   {"mba_err", 13}, | 
 | 141 |   {"Watchdog", 14}, | 
 | 142 |   {"Panic", 15}, | 
 | 143 |   {"power_key", 16}, | 
 | 144 |   {"power_on", 17}, | 
 | 145 |   {"Reboot", 18}, | 
 | 146 |   {"rtc", 19}, | 
 | 147 |   {"edl", 20}, | 
| James Hawkins | 45ead35 | 2016-03-08 16:42:07 -0800 | [diff] [blame] | 148 |   {"oem_pon1", 21}, | 
 | 149 |   {"oem_powerkey", 22}, | 
 | 150 |   {"oem_unknown_reset", 23}, | 
 | 151 |   {"srto: HWWDT reset SC", 24}, | 
 | 152 |   {"srto: HWWDT reset platform", 25}, | 
 | 153 |   {"srto: bootloader", 26}, | 
 | 154 |   {"srto: kernel panic", 27}, | 
 | 155 |   {"srto: kernel watchdog reset", 28}, | 
 | 156 |   {"srto: normal", 29}, | 
 | 157 |   {"srto: reboot", 30}, | 
 | 158 |   {"srto: reboot-bootloader", 31}, | 
 | 159 |   {"srto: security watchdog reset", 32}, | 
 | 160 |   {"srto: wakesrc", 33}, | 
 | 161 |   {"srto: watchdog", 34}, | 
 | 162 |   {"srto:1-1", 35}, | 
 | 163 |   {"srto:omap_hsmm", 36}, | 
 | 164 |   {"srto:phy0", 37}, | 
 | 165 |   {"srto:rtc0", 38}, | 
 | 166 |   {"srto:touchpad", 39}, | 
 | 167 |   {"watchdog", 40}, | 
 | 168 |   {"watchdogr", 41}, | 
 | 169 |   {"wdog_bark", 42}, | 
 | 170 |   {"wdog_bite", 43}, | 
 | 171 |   {"wdog_reset", 44}, | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 172 | }; | 
 | 173 |  | 
 | 174 | // Converts a string value representing the reason the system booted to an | 
 | 175 | // integer representation. This is necessary for logging the boot_reason metric | 
 | 176 | // via Tron, which does not accept non-integer buckets in histograms. | 
 | 177 | int32_t BootReasonStrToEnum(const std::string& boot_reason) { | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 178 |   auto mapping = kBootReasonMap.find(boot_reason); | 
 | 179 |   if (mapping != kBootReasonMap.end()) { | 
 | 180 |     return mapping->second; | 
 | 181 |   } | 
 | 182 |  | 
 | 183 |   LOG(INFO) << "Unknown boot reason: " << boot_reason; | 
 | 184 |   return kUnknownBootReason; | 
 | 185 | } | 
 | 186 |  | 
| James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 187 | // Returns the appropriate metric key prefix for the boot_complete metric such | 
 | 188 | // that boot metrics after a system update are labeled as ota_boot_complete; | 
 | 189 | // otherwise, they are labeled as boot_complete.  This method encapsulates the | 
 | 190 | // bookkeeping required to track when a system update has occurred by storing | 
 | 191 | // the UTC timestamp of the system build date and comparing against the current | 
 | 192 | // system build date. | 
 | 193 | std::string CalculateBootCompletePrefix() { | 
 | 194 |   static const std::string kBuildDateKey = "build_date"; | 
 | 195 |   std::string boot_complete_prefix = "boot_complete"; | 
 | 196 |  | 
 | 197 |   std::string build_date_str = GetProperty("ro.build.date.utc"); | 
| James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 198 |   int32_t build_date; | 
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 199 |   if (!android::base::ParseInt(build_date_str, &build_date)) { | 
| James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 200 |     return std::string(); | 
 | 201 |   } | 
| James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 202 |  | 
 | 203 |   BootEventRecordStore boot_event_store; | 
 | 204 |   BootEventRecordStore::BootEventRecord record; | 
| James Hawkins | 0bc4ad4 | 2017-05-30 15:03:15 -0700 | [diff] [blame] | 205 |   if (!boot_event_store.GetBootEvent(kBuildDateKey, &record)) { | 
 | 206 |     boot_complete_prefix = "factory_reset_" + boot_complete_prefix; | 
 | 207 |     boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date); | 
 | 208 |   } else if (build_date != record.second) { | 
| James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 209 |     boot_complete_prefix = "ota_" + boot_complete_prefix; | 
 | 210 |     boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date); | 
 | 211 |   } | 
 | 212 |  | 
 | 213 |   return boot_complete_prefix; | 
 | 214 | } | 
 | 215 |  | 
| James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 216 | // Records the value of a given ro.boottime.init property in milliseconds. | 
 | 217 | void RecordInitBootTimeProp( | 
 | 218 |     BootEventRecordStore* boot_event_store, const char* property) { | 
 | 219 |   std::string value = GetProperty(property); | 
 | 220 |  | 
| James Hawkins | 27c0522 | 2017-01-26 11:55:44 -0800 | [diff] [blame] | 221 |   int32_t time_in_ms; | 
 | 222 |   if (android::base::ParseInt(value, &time_in_ms)) { | 
| James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 223 |     boot_event_store->AddBootEventWithValue(property, time_in_ms); | 
 | 224 |   } | 
 | 225 | } | 
 | 226 |  | 
| James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 227 | // A map from bootloader timing stage to the time that stage took during boot. | 
 | 228 | typedef std::map<std::string, int32_t> BootloaderTimingMap; | 
 | 229 |  | 
 | 230 | // Returns a mapping from bootloader stage names to the time those stages | 
 | 231 | // took to boot. | 
 | 232 | const BootloaderTimingMap GetBootLoaderTimings() { | 
 | 233 |   BootloaderTimingMap timings; | 
 | 234 |  | 
 | 235 |   // |ro.boot.boottime| is of the form 'stage1:time1,...,stageN:timeN', | 
 | 236 |   // where timeN is in milliseconds. | 
| James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 237 |   std::string value = GetProperty("ro.boot.boottime"); | 
| James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 238 |   if (value.empty()) { | 
 | 239 |     // ro.boot.boottime is not reported on all devices. | 
| James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 240 |     return BootloaderTimingMap(); | 
| James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 241 |   } | 
| James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 242 |  | 
 | 243 |   auto stages = android::base::Split(value, ","); | 
| James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 244 |   for (const auto& stageTiming : stages) { | 
| James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 245 |     // |stageTiming| is of the form 'stage:time'. | 
 | 246 |     auto stageTimingValues = android::base::Split(stageTiming, ":"); | 
| James Hawkins | 0bc4ad4 | 2017-05-30 15:03:15 -0700 | [diff] [blame] | 247 |     DCHECK_EQ(2U, stageTimingValues.size()); | 
| James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 248 |  | 
 | 249 |     std::string stageName = stageTimingValues[0]; | 
 | 250 |     int32_t time_ms; | 
 | 251 |     if (android::base::ParseInt(stageTimingValues[1], &time_ms)) { | 
| James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 252 |       timings[stageName] = time_ms; | 
| James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 253 |     } | 
 | 254 |   } | 
| James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 255 |  | 
| James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 256 |   return timings; | 
 | 257 | } | 
 | 258 |  | 
 | 259 | // Parses and records the set of bootloader stages and associated boot times | 
 | 260 | // from the ro.boot.boottime system property. | 
 | 261 | void RecordBootloaderTimings(BootEventRecordStore* boot_event_store, | 
 | 262 |                              const BootloaderTimingMap& bootloader_timings) { | 
 | 263 |   int32_t total_time = 0; | 
 | 264 |   for (const auto& timing : bootloader_timings) { | 
 | 265 |     total_time += timing.second; | 
 | 266 |     boot_event_store->AddBootEventWithValue("boottime.bootloader." + timing.first, timing.second); | 
 | 267 |   } | 
 | 268 |  | 
| James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 269 |   boot_event_store->AddBootEventWithValue("boottime.bootloader.total", total_time); | 
| James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 270 | } | 
 | 271 |  | 
| James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 272 | // Records the closest estimation to the absolute device boot time, i.e., | 
 | 273 | // from power on to boot_complete, including bootloader times. | 
 | 274 | void RecordAbsoluteBootTime(BootEventRecordStore* boot_event_store, | 
 | 275 |                             const BootloaderTimingMap& bootloader_timings, | 
 | 276 |                             std::chrono::milliseconds uptime) { | 
 | 277 |   int32_t bootloader_time_ms = 0; | 
 | 278 |  | 
 | 279 |   for (const auto& timing : bootloader_timings) { | 
 | 280 |     if (timing.first.compare("SW") != 0) { | 
 | 281 |       bootloader_time_ms += timing.second; | 
 | 282 |     } | 
 | 283 |   } | 
 | 284 |  | 
 | 285 |   auto bootloader_duration = std::chrono::milliseconds(bootloader_time_ms); | 
 | 286 |   auto absolute_total = | 
 | 287 |       std::chrono::duration_cast<std::chrono::seconds>(bootloader_duration + uptime); | 
 | 288 |   boot_event_store->AddBootEventWithValue("absolute_boot_time", absolute_total.count()); | 
 | 289 | } | 
 | 290 |  | 
| James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 291 | // Records several metrics related to the time it takes to boot the device, | 
 | 292 | // including disambiguating boot time on encrypted or non-encrypted devices. | 
 | 293 | void RecordBootComplete() { | 
 | 294 |   BootEventRecordStore boot_event_store; | 
| James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 295 |   BootEventRecordStore::BootEventRecord record; | 
| James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 296 |  | 
| James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 297 |   auto time_since_epoch = android::base::boot_clock::now().time_since_epoch(); | 
 | 298 |   auto uptime = std::chrono::duration_cast<std::chrono::seconds>(time_since_epoch); | 
| James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 299 |   time_t current_time_utc = time(nullptr); | 
 | 300 |  | 
 | 301 |   if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) { | 
 | 302 |     time_t last_boot_time_utc = record.second; | 
 | 303 |     time_t time_since_last_boot = difftime(current_time_utc, | 
 | 304 |                                            last_boot_time_utc); | 
 | 305 |     boot_event_store.AddBootEventWithValue("time_since_last_boot", | 
 | 306 |                                            time_since_last_boot); | 
 | 307 |   } | 
 | 308 |  | 
 | 309 |   boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc); | 
| James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 310 |  | 
| James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 311 |   // The boot_complete metric has two variants: boot_complete and | 
 | 312 |   // ota_boot_complete.  The latter signifies that the device is booting after | 
 | 313 |   // a system update. | 
 | 314 |   std::string boot_complete_prefix = CalculateBootCompletePrefix(); | 
| James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 315 |   if (boot_complete_prefix.empty()) { | 
 | 316 |     // The system is hosed because the build date property could not be read. | 
 | 317 |     return; | 
 | 318 |   } | 
| James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 319 |  | 
 | 320 |   // post_decrypt_time_elapsed is only logged on encrypted devices. | 
 | 321 |   if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) { | 
 | 322 |     // Log the amount of time elapsed until the device is decrypted, which | 
 | 323 |     // includes the variable amount of time the user takes to enter the | 
 | 324 |     // decryption password. | 
| James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 325 |     boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime.count()); | 
| James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 326 |  | 
 | 327 |     // Subtract the decryption time to normalize the boot cycle timing. | 
| James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 328 |     std::chrono::seconds boot_complete = std::chrono::seconds(uptime.count() - record.second); | 
| James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 329 |     boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt", | 
| James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 330 |                                            boot_complete.count()); | 
| James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 331 |   } else { | 
| James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 332 |       boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption", | 
 | 333 |                                              uptime.count()); | 
| James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 334 |   } | 
 | 335 |  | 
 | 336 |   // Record the total time from device startup to boot complete, regardless of | 
 | 337 |   // encryption state. | 
| James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 338 |   boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime.count()); | 
| James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 339 |  | 
 | 340 |   RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init"); | 
 | 341 |   RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux"); | 
 | 342 |   RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait"); | 
| James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 343 |  | 
| James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 344 |   const BootloaderTimingMap bootloader_timings = GetBootLoaderTimings(); | 
 | 345 |   RecordBootloaderTimings(&boot_event_store, bootloader_timings); | 
 | 346 |  | 
 | 347 |   auto uptime_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_since_epoch); | 
 | 348 |   RecordAbsoluteBootTime(&boot_event_store, bootloader_timings, uptime_ms); | 
| James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 349 | } | 
 | 350 |  | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 351 | // Records the boot_reason metric by querying the ro.boot.bootreason system | 
 | 352 | // property. | 
 | 353 | void RecordBootReason() { | 
 | 354 |   int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason")); | 
 | 355 |   BootEventRecordStore boot_event_store; | 
 | 356 |   boot_event_store.AddBootEventWithValue("boot_reason", boot_reason); | 
 | 357 | } | 
 | 358 |  | 
| James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 359 | // Records two metrics related to the user resetting a device: the time at | 
 | 360 | // which the device is reset, and the time since the user last reset the | 
 | 361 | // device.  The former is only set once per-factory reset. | 
 | 362 | void RecordFactoryReset() { | 
 | 363 |   BootEventRecordStore boot_event_store; | 
 | 364 |   BootEventRecordStore::BootEventRecord record; | 
 | 365 |  | 
 | 366 |   time_t current_time_utc = time(nullptr); | 
 | 367 |  | 
| James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 368 |   if (current_time_utc < 0) { | 
 | 369 |     // UMA does not display negative values in buckets, so convert to positive. | 
| James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 370 |     android::metricslogger::LogHistogram( | 
| James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 371 |         "factory_reset_current_time_failure", std::abs(current_time_utc)); | 
 | 372 |  | 
| James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 373 |     // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram | 
| James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 374 |     // is losing records somehow. | 
 | 375 |     boot_event_store.AddBootEventWithValue( | 
 | 376 |         "factory_reset_current_time_failure", std::abs(current_time_utc)); | 
| James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 377 |     return; | 
 | 378 |   } else { | 
| James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 379 |     android::metricslogger::LogHistogram("factory_reset_current_time", current_time_utc); | 
| James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 380 |  | 
| James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 381 |     // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram | 
| James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 382 |     // is losing records somehow. | 
 | 383 |     boot_event_store.AddBootEventWithValue( | 
 | 384 |         "factory_reset_current_time", current_time_utc); | 
| James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 385 |   } | 
 | 386 |  | 
| James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 387 |   // The factory_reset boot event does not exist after the device is reset, so | 
 | 388 |   // use this signal to mark the time of the factory reset. | 
 | 389 |   if (!boot_event_store.GetBootEvent("factory_reset", &record)) { | 
 | 390 |     boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc); | 
| James Hawkins | 3bf9b14 | 2016-03-03 14:50:24 -0800 | [diff] [blame] | 391 |  | 
 | 392 |     // Don't log the time_since_factory_reset until some time has elapsed. | 
 | 393 |     // The data is not meaningful yet and skews the histogram buckets. | 
| James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 394 |     return; | 
 | 395 |   } | 
 | 396 |  | 
 | 397 |   // Calculate and record the difference in time between now and the | 
 | 398 |   // factory_reset time. | 
 | 399 |   time_t factory_reset_utc = record.second; | 
| James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 400 |   android::metricslogger::LogHistogram("factory_reset_record_value", factory_reset_utc); | 
| James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 401 |  | 
| James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 402 |   // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram | 
| James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 403 |   // is losing records somehow. | 
 | 404 |   boot_event_store.AddBootEventWithValue( | 
 | 405 |       "factory_reset_record_value", factory_reset_utc); | 
 | 406 |  | 
| James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 407 |   time_t time_since_factory_reset = difftime(current_time_utc, | 
 | 408 |                                              factory_reset_utc); | 
 | 409 |   boot_event_store.AddBootEventWithValue("time_since_factory_reset", | 
 | 410 |                                          time_since_factory_reset); | 
 | 411 | } | 
 | 412 |  | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 413 | }  // namespace | 
 | 414 |  | 
 | 415 | int main(int argc, char **argv) { | 
 | 416 |   android::base::InitLogging(argv); | 
 | 417 |  | 
 | 418 |   const std::string cmd_line = GetCommandLine(argc, argv); | 
 | 419 |   LOG(INFO) << "Service started: " << cmd_line; | 
 | 420 |  | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 421 |   int option_index = 0; | 
| James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 422 |   static const char value_str[] = "value"; | 
| James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 423 |   static const char boot_complete_str[] = "record_boot_complete"; | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 424 |   static const char boot_reason_str[] = "record_boot_reason"; | 
| James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 425 |   static const char factory_reset_str[] = "record_time_since_factory_reset"; | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 426 |   static const struct option long_options[] = { | 
 | 427 |     { "help",            no_argument,       NULL,   'h' }, | 
 | 428 |     { "log",             no_argument,       NULL,   'l' }, | 
 | 429 |     { "print",           no_argument,       NULL,   'p' }, | 
 | 430 |     { "record",          required_argument, NULL,   'r' }, | 
| James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 431 |     { value_str,         required_argument, NULL,   0 }, | 
| James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 432 |     { boot_complete_str, no_argument,       NULL,   0 }, | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 433 |     { boot_reason_str,   no_argument,       NULL,   0 }, | 
| James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 434 |     { factory_reset_str, no_argument,       NULL,   0 }, | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 435 |     { NULL,              0,                 NULL,   0 } | 
 | 436 |   }; | 
 | 437 |  | 
| James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 438 |   std::string boot_event; | 
 | 439 |   std::string value; | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 440 |   int opt = 0; | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 441 |   while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) { | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 442 |     switch (opt) { | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 443 |       // This case handles long options which have no single-character mapping. | 
 | 444 |       case 0: { | 
 | 445 |         const std::string option_name = long_options[option_index].name; | 
| James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 446 |         if (option_name == value_str) { | 
 | 447 |           // |optarg| is an external variable set by getopt representing | 
 | 448 |           // the option argument. | 
 | 449 |           value = optarg; | 
 | 450 |         } else if (option_name == boot_complete_str) { | 
| James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 451 |           RecordBootComplete(); | 
 | 452 |         } else if (option_name == boot_reason_str) { | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 453 |           RecordBootReason(); | 
| James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 454 |         } else if (option_name == factory_reset_str) { | 
 | 455 |           RecordFactoryReset(); | 
| James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 456 |         } else { | 
 | 457 |           LOG(ERROR) << "Invalid option: " << option_name; | 
 | 458 |         } | 
 | 459 |         break; | 
 | 460 |       } | 
 | 461 |  | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 462 |       case 'h': { | 
 | 463 |         ShowHelp(argv[0]); | 
 | 464 |         break; | 
 | 465 |       } | 
 | 466 |  | 
 | 467 |       case 'l': { | 
 | 468 |         LogBootEvents(); | 
 | 469 |         break; | 
 | 470 |       } | 
 | 471 |  | 
 | 472 |       case 'p': { | 
 | 473 |         PrintBootEvents(); | 
 | 474 |         break; | 
 | 475 |       } | 
 | 476 |  | 
 | 477 |       case 'r': { | 
 | 478 |         // |optarg| is an external variable set by getopt representing | 
 | 479 |         // the option argument. | 
| James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 480 |         boot_event = optarg; | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 481 |         break; | 
 | 482 |       } | 
 | 483 |  | 
 | 484 |       default: { | 
 | 485 |         DCHECK_EQ(opt, '?'); | 
 | 486 |  | 
 | 487 |         // |optopt| is an external variable set by getopt representing | 
 | 488 |         // the value of the invalid option. | 
 | 489 |         LOG(ERROR) << "Invalid option: " << optopt; | 
 | 490 |         ShowHelp(argv[0]); | 
 | 491 |         return EXIT_FAILURE; | 
 | 492 |       } | 
 | 493 |     } | 
 | 494 |   } | 
 | 495 |  | 
| James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 496 |   if (!boot_event.empty()) { | 
 | 497 |     RecordBootEventFromCommandLine(boot_event, value); | 
 | 498 |   } | 
 | 499 |  | 
| James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 500 |   return 0; | 
 | 501 | } |