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