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> |
| 23 | #include <cstddef> |
| 24 | #include <cstdio> |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 25 | #include <ctime> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 26 | #include <map> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <string> |
James Hawkins | eabe08b | 2016-01-19 16:54:35 -0800 | [diff] [blame] | 29 | #include <android-base/logging.h> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 30 | #include <cutils/properties.h> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 31 | #include <log/log.h> |
| 32 | #include "boot_event_record_store.h" |
| 33 | #include "event_log_list_builder.h" |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | // Builds an EventLog buffer named |event| containing |data| and writes |
| 38 | // the log into the Tron histogram logs. |
| 39 | void LogBootEvent(const std::string& event, int32_t data) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 40 | LOG(INFO) << "Logging boot metric: " << event << " " << data; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 41 | |
| 42 | EventLogListBuilder log_builder; |
| 43 | log_builder.Append(event); |
| 44 | log_builder.Append(data); |
| 45 | |
| 46 | std::unique_ptr<uint8_t[]> log; |
| 47 | size_t size; |
| 48 | log_builder.Release(&log, &size); |
| 49 | |
| 50 | android_bWriteLog(HISTOGRAM_LOG_TAG, log.get(), size); |
| 51 | } |
| 52 | |
| 53 | // Scans the boot event record store for record files and logs each boot event |
| 54 | // via EventLog. |
| 55 | void LogBootEvents() { |
| 56 | BootEventRecordStore boot_event_store; |
| 57 | |
| 58 | auto events = boot_event_store.GetAllBootEvents(); |
| 59 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
| 60 | LogBootEvent(i->first, i->second); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void PrintBootEvents() { |
| 65 | printf("Boot events:\n"); |
| 66 | printf("------------\n"); |
| 67 | |
| 68 | BootEventRecordStore boot_event_store; |
| 69 | auto events = boot_event_store.GetAllBootEvents(); |
| 70 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
| 71 | printf("%s\t%d\n", i->first.c_str(), i->second); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void ShowHelp(const char *cmd) { |
| 76 | fprintf(stderr, "Usage: %s [options]\n", cmd); |
| 77 | fprintf(stderr, |
| 78 | "options include:\n" |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 79 | " -h, --help Show this help\n" |
| 80 | " -l, --log Log all metrics to logstorage\n" |
| 81 | " -p, --print Dump the boot event records to the console\n" |
| 82 | " -r, --record Record the timestamp of a named boot event\n" |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 83 | " --record_boot_reason Record the reason why the device booted\n" |
| 84 | " --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] | 85 | } |
| 86 | |
| 87 | // Constructs a readable, printable string from the givencommand line |
| 88 | // arguments. |
| 89 | std::string GetCommandLine(int argc, char **argv) { |
| 90 | std::string cmd; |
| 91 | for (int i = 0; i < argc; ++i) { |
| 92 | cmd += argv[i]; |
| 93 | cmd += " "; |
| 94 | } |
| 95 | |
| 96 | return cmd; |
| 97 | } |
| 98 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 99 | // Convenience wrapper over the property API that returns an |
| 100 | // std::string. |
| 101 | std::string GetProperty(const char* key) { |
| 102 | std::vector<char> temp(PROPERTY_VALUE_MAX); |
| 103 | const int len = property_get(key, &temp[0], nullptr); |
| 104 | if (len < 0) { |
| 105 | return ""; |
| 106 | } |
| 107 | return std::string(&temp[0], len); |
| 108 | } |
| 109 | |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 110 | constexpr int32_t kUnknownBootReason = 1; |
| 111 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 112 | // A mapping from boot reason string, as read from the ro.boot.bootreason |
| 113 | // system property, to a unique integer ID. Viewers of log data dashboards for |
| 114 | // the boot_reason metric may refer to this mapping to discern the histogram |
| 115 | // values. |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 116 | const std::map<std::string, int32_t> kBootReasonMap = { |
| 117 | {"unknown", kUnknownBootReason}, |
| 118 | {"normal", 2}, |
| 119 | {"recovery", 3}, |
| 120 | {"reboot", 4}, |
| 121 | {"PowerKey", 5}, |
| 122 | {"hard_reset", 6}, |
| 123 | {"kernel_panic", 7}, |
| 124 | {"rpm_err", 8}, |
| 125 | {"hw_reset", 9}, |
| 126 | {"tz_err", 10}, |
| 127 | {"adsp_err", 11}, |
| 128 | {"modem_err", 12}, |
| 129 | {"mba_err", 13}, |
| 130 | {"Watchdog", 14}, |
| 131 | {"Panic", 15}, |
| 132 | {"power_key", 16}, |
| 133 | {"power_on", 17}, |
| 134 | {"Reboot", 18}, |
| 135 | {"rtc", 19}, |
| 136 | {"edl", 20}, |
James Hawkins | 45ead35 | 2016-03-08 16:42:07 -0800 | [diff] [blame^] | 137 | {"oem_pon1", 21}, |
| 138 | {"oem_powerkey", 22}, |
| 139 | {"oem_unknown_reset", 23}, |
| 140 | {"srto: HWWDT reset SC", 24}, |
| 141 | {"srto: HWWDT reset platform", 25}, |
| 142 | {"srto: bootloader", 26}, |
| 143 | {"srto: kernel panic", 27}, |
| 144 | {"srto: kernel watchdog reset", 28}, |
| 145 | {"srto: normal", 29}, |
| 146 | {"srto: reboot", 30}, |
| 147 | {"srto: reboot-bootloader", 31}, |
| 148 | {"srto: security watchdog reset", 32}, |
| 149 | {"srto: wakesrc", 33}, |
| 150 | {"srto: watchdog", 34}, |
| 151 | {"srto:1-1", 35}, |
| 152 | {"srto:omap_hsmm", 36}, |
| 153 | {"srto:phy0", 37}, |
| 154 | {"srto:rtc0", 38}, |
| 155 | {"srto:touchpad", 39}, |
| 156 | {"watchdog", 40}, |
| 157 | {"watchdogr", 41}, |
| 158 | {"wdog_bark", 42}, |
| 159 | {"wdog_bite", 43}, |
| 160 | {"wdog_reset", 44}, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | // Converts a string value representing the reason the system booted to an |
| 164 | // integer representation. This is necessary for logging the boot_reason metric |
| 165 | // via Tron, which does not accept non-integer buckets in histograms. |
| 166 | int32_t BootReasonStrToEnum(const std::string& boot_reason) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 167 | auto mapping = kBootReasonMap.find(boot_reason); |
| 168 | if (mapping != kBootReasonMap.end()) { |
| 169 | return mapping->second; |
| 170 | } |
| 171 | |
| 172 | LOG(INFO) << "Unknown boot reason: " << boot_reason; |
| 173 | return kUnknownBootReason; |
| 174 | } |
| 175 | |
| 176 | // Records the boot_reason metric by querying the ro.boot.bootreason system |
| 177 | // property. |
| 178 | void RecordBootReason() { |
| 179 | int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason")); |
| 180 | BootEventRecordStore boot_event_store; |
| 181 | boot_event_store.AddBootEventWithValue("boot_reason", boot_reason); |
| 182 | } |
| 183 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 184 | // Records two metrics related to the user resetting a device: the time at |
| 185 | // which the device is reset, and the time since the user last reset the |
| 186 | // device. The former is only set once per-factory reset. |
| 187 | void RecordFactoryReset() { |
| 188 | BootEventRecordStore boot_event_store; |
| 189 | BootEventRecordStore::BootEventRecord record; |
| 190 | |
| 191 | time_t current_time_utc = time(nullptr); |
| 192 | |
| 193 | // The factory_reset boot event does not exist after the device is reset, so |
| 194 | // use this signal to mark the time of the factory reset. |
| 195 | if (!boot_event_store.GetBootEvent("factory_reset", &record)) { |
| 196 | boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc); |
James Hawkins | 3bf9b14 | 2016-03-03 14:50:24 -0800 | [diff] [blame] | 197 | |
| 198 | // Don't log the time_since_factory_reset until some time has elapsed. |
| 199 | // The data is not meaningful yet and skews the histogram buckets. |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | |
| 203 | // Calculate and record the difference in time between now and the |
| 204 | // factory_reset time. |
| 205 | time_t factory_reset_utc = record.second; |
| 206 | time_t time_since_factory_reset = difftime(current_time_utc, |
| 207 | factory_reset_utc); |
| 208 | boot_event_store.AddBootEventWithValue("time_since_factory_reset", |
| 209 | time_since_factory_reset); |
| 210 | } |
| 211 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 212 | } // namespace |
| 213 | |
| 214 | int main(int argc, char **argv) { |
| 215 | android::base::InitLogging(argv); |
| 216 | |
| 217 | const std::string cmd_line = GetCommandLine(argc, argv); |
| 218 | LOG(INFO) << "Service started: " << cmd_line; |
| 219 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 220 | int option_index = 0; |
| 221 | static const char boot_reason_str[] = "record_boot_reason"; |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 222 | static const char factory_reset_str[] = "record_time_since_factory_reset"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 223 | static const struct option long_options[] = { |
| 224 | { "help", no_argument, NULL, 'h' }, |
| 225 | { "log", no_argument, NULL, 'l' }, |
| 226 | { "print", no_argument, NULL, 'p' }, |
| 227 | { "record", required_argument, NULL, 'r' }, |
| 228 | { boot_reason_str, no_argument, NULL, 0 }, |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 229 | { factory_reset_str, no_argument, NULL, 0 }, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 230 | { NULL, 0, NULL, 0 } |
| 231 | }; |
| 232 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 233 | int opt = 0; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 234 | while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 235 | switch (opt) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 236 | // This case handles long options which have no single-character mapping. |
| 237 | case 0: { |
| 238 | const std::string option_name = long_options[option_index].name; |
| 239 | if (option_name == boot_reason_str) { |
| 240 | RecordBootReason(); |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 241 | } else if (option_name == factory_reset_str) { |
| 242 | RecordFactoryReset(); |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 243 | } else { |
| 244 | LOG(ERROR) << "Invalid option: " << option_name; |
| 245 | } |
| 246 | break; |
| 247 | } |
| 248 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 249 | case 'h': { |
| 250 | ShowHelp(argv[0]); |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | case 'l': { |
| 255 | LogBootEvents(); |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | case 'p': { |
| 260 | PrintBootEvents(); |
| 261 | break; |
| 262 | } |
| 263 | |
| 264 | case 'r': { |
| 265 | // |optarg| is an external variable set by getopt representing |
| 266 | // the option argument. |
| 267 | const char* event = optarg; |
| 268 | |
| 269 | BootEventRecordStore boot_event_store; |
| 270 | boot_event_store.AddBootEvent(event); |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | default: { |
| 275 | DCHECK_EQ(opt, '?'); |
| 276 | |
| 277 | // |optopt| is an external variable set by getopt representing |
| 278 | // the value of the invalid option. |
| 279 | LOG(ERROR) << "Invalid option: " << optopt; |
| 280 | ShowHelp(argv[0]); |
| 281 | return EXIT_FAILURE; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | } |