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 | |
| 21 | //#define LOG_TAG "bootstat" |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | #include <cstddef> |
| 25 | #include <cstdio> |
| 26 | #include <memory> |
| 27 | #include <string> |
| 28 | #include <base/logging.h> |
| 29 | #include <log/log.h> |
| 30 | #include "boot_event_record_store.h" |
| 31 | #include "event_log_list_builder.h" |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | // Builds an EventLog buffer named |event| containing |data| and writes |
| 36 | // the log into the Tron histogram logs. |
| 37 | void LogBootEvent(const std::string& event, int32_t data) { |
| 38 | LOG(INFO) << "Logging boot time: " << event << " " << data; |
| 39 | |
| 40 | EventLogListBuilder log_builder; |
| 41 | log_builder.Append(event); |
| 42 | log_builder.Append(data); |
| 43 | |
| 44 | std::unique_ptr<uint8_t[]> log; |
| 45 | size_t size; |
| 46 | log_builder.Release(&log, &size); |
| 47 | |
| 48 | android_bWriteLog(HISTOGRAM_LOG_TAG, log.get(), size); |
| 49 | } |
| 50 | |
| 51 | // Scans the boot event record store for record files and logs each boot event |
| 52 | // via EventLog. |
| 53 | void LogBootEvents() { |
| 54 | BootEventRecordStore boot_event_store; |
| 55 | |
| 56 | auto events = boot_event_store.GetAllBootEvents(); |
| 57 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
| 58 | LogBootEvent(i->first, i->second); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void PrintBootEvents() { |
| 63 | printf("Boot events:\n"); |
| 64 | printf("------------\n"); |
| 65 | |
| 66 | BootEventRecordStore boot_event_store; |
| 67 | auto events = boot_event_store.GetAllBootEvents(); |
| 68 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
| 69 | printf("%s\t%d\n", i->first.c_str(), i->second); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void ShowHelp(const char *cmd) { |
| 74 | fprintf(stderr, "Usage: %s [options]\n", cmd); |
| 75 | fprintf(stderr, |
| 76 | "options include:\n" |
| 77 | " -d Dump the boot event records to the console.\n" |
| 78 | " -h Show this help.\n" |
| 79 | " -l Log all metrics to logstorage.\n" |
| 80 | " -r Record the timestamp of a named boot event.\n"); |
| 81 | } |
| 82 | |
| 83 | // Constructs a readable, printable string from the givencommand line |
| 84 | // arguments. |
| 85 | std::string GetCommandLine(int argc, char **argv) { |
| 86 | std::string cmd; |
| 87 | for (int i = 0; i < argc; ++i) { |
| 88 | cmd += argv[i]; |
| 89 | cmd += " "; |
| 90 | } |
| 91 | |
| 92 | return cmd; |
| 93 | } |
| 94 | |
| 95 | } // namespace |
| 96 | |
| 97 | int main(int argc, char **argv) { |
| 98 | android::base::InitLogging(argv); |
| 99 | |
| 100 | const std::string cmd_line = GetCommandLine(argc, argv); |
| 101 | LOG(INFO) << "Service started: " << cmd_line; |
| 102 | |
| 103 | int opt = 0; |
| 104 | while ((opt = getopt(argc, argv, "hlpr:")) != -1) { |
| 105 | switch (opt) { |
| 106 | case 'h': { |
| 107 | ShowHelp(argv[0]); |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | case 'l': { |
| 112 | LogBootEvents(); |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | case 'p': { |
| 117 | PrintBootEvents(); |
| 118 | break; |
| 119 | } |
| 120 | |
| 121 | case 'r': { |
| 122 | // |optarg| is an external variable set by getopt representing |
| 123 | // the option argument. |
| 124 | const char* event = optarg; |
| 125 | |
| 126 | BootEventRecordStore boot_event_store; |
| 127 | boot_event_store.AddBootEvent(event); |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | default: { |
| 132 | DCHECK_EQ(opt, '?'); |
| 133 | |
| 134 | // |optopt| is an external variable set by getopt representing |
| 135 | // the value of the invalid option. |
| 136 | LOG(ERROR) << "Invalid option: " << optopt; |
| 137 | ShowHelp(argv[0]); |
| 138 | return EXIT_FAILURE; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |