Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <vector> |
| 4 | |
| 5 | #include "aos/events/logging/logfile_sorting.h" |
| 6 | #include "aos/events/logging/logfile_utils.h" |
| 7 | #include "aos/init.h" |
| 8 | #include "aos/network/multinode_timestamp_filter.h" |
| 9 | #include "gflags/gflags.h" |
| 10 | |
| 11 | DECLARE_bool(timestamps_to_csv); |
| 12 | DEFINE_bool(skip_order_validation, false, |
| 13 | "If true, ignore any out of orderness in replay"); |
| 14 | |
| 15 | namespace aos::logger { |
| 16 | |
| 17 | namespace chrono = std::chrono; |
| 18 | |
| 19 | std::string LogFileVectorToString(std::vector<logger::LogFile> log_files) { |
| 20 | std::stringstream ss; |
Austin Schuh | 05dad8b | 2021-01-27 22:47:23 -0800 | [diff] [blame] | 21 | for (const auto &f : log_files) { |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 22 | ss << f << "\n"; |
| 23 | } |
| 24 | return ss.str(); |
| 25 | } |
| 26 | |
| 27 | int Main(int argc, char **argv) { |
| 28 | const std::vector<std::string> unsorted_logfiles = FindLogs(argc, argv); |
| 29 | const std::vector<LogFile> log_files = SortParts(unsorted_logfiles); |
| 30 | |
| 31 | CHECK_GT(log_files.size(), 0u); |
| 32 | // Validate that we have the same config everwhere. This will be true if |
| 33 | // all the parts were sorted together and the configs match. |
| 34 | const Configuration *config = nullptr; |
| 35 | for (const LogFile &log_file : log_files) { |
Austin Schuh | 3e20c69 | 2021-11-16 20:43:16 -0800 | [diff] [blame] | 36 | VLOG(1) << log_file; |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 37 | if (config == nullptr) { |
| 38 | config = log_file.config.get(); |
| 39 | } else { |
| 40 | CHECK_EQ(config, log_file.config.get()); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | CHECK(configuration::MultiNode(config)) |
| 45 | << ": Timestamps only make sense in a multi-node world."; |
| 46 | |
| 47 | // Now, build up all the TimestampMapper classes to read and sort the data. |
| 48 | std::vector<std::unique_ptr<TimestampMapper>> mappers; |
| 49 | |
| 50 | for (const Node *node : configuration::GetNodes(config)) { |
| 51 | std::vector<LogParts> filtered_parts = |
| 52 | FilterPartsForNode(log_files, node->name()->string_view()); |
| 53 | |
| 54 | // Confirm that all the parts are from the same boot if there are enough |
| 55 | // parts to not be from the same boot. |
| 56 | if (!filtered_parts.empty()) { |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 57 | // Filter the parts relevant to each node when building the mapper. |
| 58 | mappers.emplace_back( |
| 59 | std::make_unique<TimestampMapper>(std::move(filtered_parts))); |
| 60 | } else { |
| 61 | mappers.emplace_back(nullptr); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Now, build up the estimator used to solve for time. |
| 66 | message_bridge::MultiNodeNoncausalOffsetEstimator multinode_estimator( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 67 | config, config, log_files[0].boots, FLAGS_skip_order_validation, |
| 68 | chrono::seconds(0)); |
| 69 | multinode_estimator.set_reboot_found( |
| 70 | [config](distributed_clock::time_point reboot_time, |
| 71 | const std::vector<logger::BootTimestamp> &node_times) { |
| 72 | LOG(INFO) << "Rebooted at distributed " << reboot_time; |
| 73 | size_t node_index = 0; |
| 74 | for (const logger::BootTimestamp &time : node_times) { |
| 75 | LOG(INFO) << " " |
| 76 | << config->nodes()->Get(node_index)->name()->string_view() |
| 77 | << " " << time; |
| 78 | ++node_index; |
| 79 | } |
| 80 | }); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 81 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 82 | { |
| 83 | std::vector<TimestampMapper *> timestamp_mappers; |
| 84 | for (std::unique_ptr<TimestampMapper> &mapper : mappers) { |
| 85 | timestamp_mappers.emplace_back(mapper.get()); |
| 86 | } |
| 87 | multinode_estimator.SetTimestampMappers(std::move(timestamp_mappers)); |
| 88 | } |
| 89 | |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 90 | // To make things more like the logger and faster, cache the node + channel -> |
| 91 | // filter mapping in a set of vectors. |
| 92 | std::vector<std::vector<message_bridge::NoncausalOffsetEstimator *>> filters; |
| 93 | filters.resize(configuration::NodesCount(config)); |
| 94 | |
| 95 | for (const Node *node : configuration::GetNodes(config)) { |
| 96 | const size_t node_index = configuration::GetNodeIndex(config, node); |
| 97 | filters[node_index].resize(config->channels()->size(), nullptr); |
| 98 | for (size_t channel_index = 0; channel_index < config->channels()->size(); |
| 99 | ++channel_index) { |
| 100 | const Channel *channel = config->channels()->Get(channel_index); |
| 101 | |
| 102 | if (!configuration::ChannelIsSendableOnNode(channel, node) && |
| 103 | configuration::ChannelIsReadableOnNode(channel, node)) { |
| 104 | // We've got a message which is being forwarded to this node. |
| 105 | const Node *source_node = configuration::GetNode( |
| 106 | config, channel->source_node()->string_view()); |
| 107 | filters[node_index][channel_index] = |
| 108 | multinode_estimator.GetFilter(node, source_node); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 113 | multinode_estimator.CheckGraph(); |
| 114 | |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 115 | // Now, read all the timestamps for each node. This is simpler than the |
| 116 | // logger on purpose. It loads in *all* the timestamps in 1 go per node, |
| 117 | // ignoring memory usage. |
| 118 | for (const Node *node : configuration::GetNodes(config)) { |
| 119 | LOG(INFO) << "Reading all data for " << node->name()->string_view(); |
| 120 | const size_t node_index = configuration::GetNodeIndex(config, node); |
| 121 | TimestampMapper *timestamp_mapper = mappers[node_index].get(); |
| 122 | if (timestamp_mapper == nullptr) { |
| 123 | continue; |
| 124 | } |
| 125 | while (true) { |
| 126 | TimestampedMessage *m = timestamp_mapper->Front(); |
| 127 | if (m == nullptr) { |
| 128 | break; |
| 129 | } |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 130 | timestamp_mapper->PopFront(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Don't get clever. Use the first time as the start time. Note: this is |
| 135 | // different than how log_cat and others work. |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 136 | std::optional<const std::tuple<distributed_clock::time_point, |
| 137 | std::vector<BootTimestamp>> *> |
| 138 | next_timestamp = multinode_estimator.QueueNextTimestamp(); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 139 | CHECK(next_timestamp); |
| 140 | LOG(INFO) << "Starting at:"; |
| 141 | for (const Node *node : configuration::GetNodes(config)) { |
| 142 | const size_t node_index = configuration::GetNodeIndex(config, node); |
| 143 | LOG(INFO) << " " << node->name()->string_view() << " -> " |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 144 | << std::get<1>(*next_timestamp.value())[node_index].time; |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 147 | std::vector<monotonic_clock::time_point> just_monotonic( |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 148 | std::get<1>(*next_timestamp.value()).size()); |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 149 | for (size_t i = 0; i < just_monotonic.size(); ++i) { |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 150 | CHECK_EQ(std::get<1>(*next_timestamp.value())[i].boot, 0u); |
| 151 | just_monotonic[i] = std::get<1>(*next_timestamp.value())[i].time; |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 152 | } |
| 153 | multinode_estimator.Start(just_monotonic); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 154 | |
| 155 | // As we pull off all the timestamps, the time problem is continually solved, |
| 156 | // filling in the CSV files. |
| 157 | while (true) { |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 158 | std::optional<const std::tuple<distributed_clock::time_point, |
| 159 | std::vector<BootTimestamp>> *> |
| 160 | next_timestamp = multinode_estimator.QueueNextTimestamp(); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 161 | if (!next_timestamp) { |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 166 | LOG(INFO) << "Done"; |
| 167 | |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | } // namespace aos::logger |
| 172 | |
| 173 | int main(int argc, char **argv) { |
| 174 | FLAGS_timestamps_to_csv = true; |
| 175 | gflags::SetUsageMessage( |
| 176 | "Usage:\n" |
| 177 | " timestamp_extractor [args] logfile1 logfile2 ...\n\nThis program " |
| 178 | "dumps out all the timestamps from a set of log files for plotting. Use " |
| 179 | "--skip_order_validation to skip any time estimation problems we find."); |
| 180 | aos::InitGoogle(&argc, &argv); |
| 181 | |
| 182 | return aos::logger::Main(argc, argv); |
| 183 | } |