James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 1 | #include "aos/events/logging/logger.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 2 | |
| 3 | #include <fcntl.h> |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 4 | #include <limits.h> |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/uio.h> |
| 8 | #include <vector> |
| 9 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 10 | #include "Eigen/Dense" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 11 | #include "absl/strings/escaping.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 12 | #include "absl/types/span.h" |
| 13 | #include "aos/events/event_loop.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 14 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 15 | #include "aos/events/logging/uuid.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 16 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | 288479d | 2019-12-18 19:47:52 -0800 | [diff] [blame] | 17 | #include "aos/network/team_number.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 18 | #include "aos/time/time.h" |
| 19 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 20 | #include "third_party/gmp/gmpxx.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 21 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 22 | DEFINE_bool(skip_missing_forwarding_entries, false, |
| 23 | "If true, drop any forwarding entries with missing data. If " |
| 24 | "false, CHECK."); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 25 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 26 | DEFINE_bool(timestamps_to_csv, false, |
| 27 | "If true, write all the time synchronization information to a set " |
| 28 | "of CSV files in /tmp/. This should only be needed when debugging " |
| 29 | "time synchronization."); |
| 30 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 31 | DEFINE_bool(skip_order_validation, false, |
| 32 | "If true, ignore any out of orderness in replay"); |
| 33 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 34 | namespace aos { |
| 35 | namespace logger { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 36 | namespace chrono = std::chrono; |
| 37 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 38 | |
| 39 | Logger::Logger(std::string_view base_name, EventLoop *event_loop, |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 40 | std::chrono::milliseconds polling_period) |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 41 | : Logger(base_name, event_loop, event_loop->configuration(), |
| 42 | polling_period) {} |
| 43 | Logger::Logger(std::string_view base_name, EventLoop *event_loop, |
| 44 | const Configuration *configuration, |
| 45 | std::chrono::milliseconds polling_period) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 46 | : Logger(std::make_unique<LocalLogNamer>(base_name, event_loop->node()), |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 47 | event_loop, configuration, polling_period) {} |
| 48 | Logger::Logger(std::unique_ptr<LogNamer> log_namer, EventLoop *event_loop, |
| 49 | std::chrono::milliseconds polling_period) |
| 50 | : Logger(std::move(log_namer), event_loop, event_loop->configuration(), |
| 51 | polling_period) {} |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 52 | |
| 53 | Logger::Logger(std::unique_ptr<LogNamer> log_namer, EventLoop *event_loop, |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 54 | const Configuration *configuration, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 55 | std::chrono::milliseconds polling_period) |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 56 | : event_loop_(event_loop), |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 57 | uuid_(UUID::Random()), |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 58 | log_namer_(std::move(log_namer)), |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 59 | configuration_(configuration), |
| 60 | name_(network::GetHostname()), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 61 | timer_handler_(event_loop_->AddTimer([this]() { DoLogData(); })), |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 62 | polling_period_(polling_period), |
| 63 | server_statistics_fetcher_( |
| 64 | configuration::MultiNode(event_loop_->configuration()) |
| 65 | ? event_loop_->MakeFetcher<message_bridge::ServerStatistics>( |
| 66 | "/aos") |
| 67 | : aos::Fetcher<message_bridge::ServerStatistics>()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 68 | VLOG(1) << "Starting logger for " << FlatbufferToJson(event_loop_->node()); |
| 69 | int channel_index = 0; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 70 | |
| 71 | // Find all the nodes which are logging timestamps on our node. |
| 72 | std::set<const Node *> timestamp_logger_nodes; |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 73 | for (const Channel *channel : *configuration_->channels()) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 74 | if (!configuration::ChannelIsSendableOnNode(channel, event_loop_->node()) || |
| 75 | !channel->has_destination_nodes()) { |
| 76 | continue; |
| 77 | } |
| 78 | for (const Connection *connection : *channel->destination_nodes()) { |
| 79 | const Node *other_node = configuration::GetNode( |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 80 | configuration_, connection->name()->string_view()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 81 | |
| 82 | if (configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 83 | connection, event_loop_->node())) { |
| 84 | VLOG(1) << "Timestamps are logged from " |
| 85 | << FlatbufferToJson(other_node); |
| 86 | timestamp_logger_nodes.insert(other_node); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | std::map<const Channel *, const Node *> timestamp_logger_channels; |
| 92 | |
| 93 | // Now that we have all the nodes accumulated, make remote timestamp loggers |
| 94 | // for them. |
| 95 | for (const Node *node : timestamp_logger_nodes) { |
| 96 | const Channel *channel = configuration::GetChannel( |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 97 | configuration_, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 98 | absl::StrCat("/aos/remote_timestamps/", node->name()->string_view()), |
| 99 | logger::MessageHeader::GetFullyQualifiedName(), event_loop_->name(), |
| 100 | event_loop_->node()); |
| 101 | |
| 102 | CHECK(channel != nullptr) |
| 103 | << ": Remote timestamps are logged on " |
| 104 | << event_loop_->node()->name()->string_view() |
| 105 | << " but can't find channel /aos/remote_timestamps/" |
| 106 | << node->name()->string_view(); |
| 107 | timestamp_logger_channels.insert(std::make_pair(channel, node)); |
| 108 | } |
| 109 | |
| 110 | const size_t our_node_index = configuration::GetNodeIndex( |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 111 | configuration_, event_loop_->node()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 112 | |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 113 | for (const Channel *config_channel : *configuration_->channels()) { |
| 114 | // The MakeRawFetcher method needs a channel which is in the event loop |
| 115 | // configuration() object, not the configuration_ object. Go look that up |
| 116 | // from the config. |
| 117 | const Channel *channel = aos::configuration::GetChannel( |
| 118 | event_loop_->configuration(), config_channel->name()->string_view(), |
| 119 | config_channel->type()->string_view(), "", event_loop_->node()); |
| 120 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 121 | FetcherStruct fs; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 122 | fs.node_index = our_node_index; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 123 | const bool is_local = |
| 124 | configuration::ChannelIsSendableOnNode(channel, event_loop_->node()); |
| 125 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 126 | const bool is_readable = |
| 127 | configuration::ChannelIsReadableOnNode(channel, event_loop_->node()); |
| 128 | const bool log_message = configuration::ChannelMessageIsLoggedOnNode( |
| 129 | channel, event_loop_->node()) && |
| 130 | is_readable; |
| 131 | |
| 132 | const bool log_delivery_times = |
| 133 | (event_loop_->node() == nullptr) |
| 134 | ? false |
| 135 | : configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 136 | channel, event_loop_->node(), event_loop_->node()); |
| 137 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 138 | // Now, detect a MessageHeader timestamp logger where we should just log the |
| 139 | // contents to a file directly. |
| 140 | const bool log_contents = timestamp_logger_channels.find(channel) != |
| 141 | timestamp_logger_channels.end(); |
| 142 | const Node *timestamp_node = |
| 143 | log_contents ? timestamp_logger_channels.find(channel)->second |
| 144 | : nullptr; |
| 145 | |
| 146 | if (log_message || log_delivery_times || log_contents) { |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 147 | fs.fetcher = event_loop->MakeRawFetcher(channel); |
| 148 | VLOG(1) << "Logging channel " |
| 149 | << configuration::CleanedChannelToString(channel); |
| 150 | |
| 151 | if (log_delivery_times) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 152 | VLOG(1) << " Delivery times"; |
| 153 | fs.timestamp_writer = log_namer_->MakeTimestampWriter(channel); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 154 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 155 | if (log_message) { |
| 156 | VLOG(1) << " Data"; |
| 157 | fs.writer = log_namer_->MakeWriter(channel); |
| 158 | if (!is_local) { |
| 159 | fs.log_type = LogType::kLogRemoteMessage; |
| 160 | } |
| 161 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 162 | if (log_contents) { |
| 163 | VLOG(1) << "Timestamp logger channel " |
| 164 | << configuration::CleanedChannelToString(channel); |
| 165 | fs.contents_writer = |
| 166 | log_namer_->MakeForwardedTimestampWriter(channel, timestamp_node); |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 167 | fs.node_index = |
| 168 | configuration::GetNodeIndex(configuration_, timestamp_node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 169 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 170 | fs.channel_index = channel_index; |
| 171 | fs.written = false; |
| 172 | fetchers_.emplace_back(std::move(fs)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 173 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 174 | ++channel_index; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 177 | node_state_.resize(configuration::MultiNode(configuration_) |
| 178 | ? configuration_->nodes()->size() |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 179 | : 1u); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 180 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 181 | for (const Node *node : log_namer_->nodes()) { |
| 182 | const int node_index = |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 183 | configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 184 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 185 | node_state_[node_index].log_file_header = MakeHeader(node); |
| 186 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 187 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 188 | // When things start, we want to log the header, then the most recent |
| 189 | // messages available on each fetcher to capture the previous state, then |
| 190 | // start polling. |
| 191 | event_loop_->OnRun([this]() { StartLogging(); }); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 194 | Logger::~Logger() { |
| 195 | // If we are replaying a log file, or in simulation, we want to force the last |
| 196 | // bit of data to be logged. The easiest way to deal with this is to poll |
| 197 | // everything as we go to destroy the class, ie, shut down the logger, and |
| 198 | // write it to disk. |
| 199 | DoLogData(); |
| 200 | } |
| 201 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 202 | void Logger::StartLogging() { |
| 203 | // Grab data from each channel right before we declare the log file started |
| 204 | // so we can capture the latest message on each channel. This lets us have |
| 205 | // non periodic messages with configuration that now get logged. |
| 206 | for (FetcherStruct &f : fetchers_) { |
| 207 | f.written = !f.fetcher->Fetch(); |
| 208 | } |
| 209 | |
| 210 | // Clear out any old timestamps in case we are re-starting logging. |
| 211 | for (size_t i = 0; i < node_state_.size(); ++i) { |
| 212 | SetStartTime(i, monotonic_clock::min_time, realtime_clock::min_time); |
| 213 | } |
| 214 | |
| 215 | WriteHeader(); |
| 216 | |
| 217 | LOG(INFO) << "Logging node as " << FlatbufferToJson(event_loop_->node()) |
| 218 | << " start_time " << last_synchronized_time_; |
| 219 | |
| 220 | timer_handler_->Setup(event_loop_->monotonic_now() + polling_period_, |
| 221 | polling_period_); |
| 222 | } |
| 223 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 224 | void Logger::WriteHeader() { |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 225 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 226 | server_statistics_fetcher_.Fetch(); |
| 227 | } |
| 228 | |
| 229 | aos::monotonic_clock::time_point monotonic_start_time = |
| 230 | event_loop_->monotonic_now(); |
| 231 | aos::realtime_clock::time_point realtime_start_time = |
| 232 | event_loop_->realtime_now(); |
| 233 | |
| 234 | // We need to pick a point in time to declare the log file "started". This |
| 235 | // starts here. It needs to be after everything is fetched so that the |
| 236 | // fetchers are all pointed at the most recent message before the start |
| 237 | // time. |
| 238 | last_synchronized_time_ = monotonic_start_time; |
| 239 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 240 | for (const Node *node : log_namer_->nodes()) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 241 | const int node_index = |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 242 | configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 243 | MaybeUpdateTimestamp(node, node_index, monotonic_start_time, |
| 244 | realtime_start_time); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 245 | log_namer_->WriteHeader(&node_state_[node_index].log_file_header, node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 246 | } |
| 247 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 248 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 249 | void Logger::WriteMissingTimestamps() { |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 250 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 251 | server_statistics_fetcher_.Fetch(); |
| 252 | } else { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | if (server_statistics_fetcher_.get() == nullptr) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | for (const Node *node : log_namer_->nodes()) { |
| 261 | const int node_index = |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 262 | configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 263 | if (MaybeUpdateTimestamp( |
| 264 | node, node_index, |
| 265 | server_statistics_fetcher_.context().monotonic_event_time, |
| 266 | server_statistics_fetcher_.context().realtime_event_time)) { |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 267 | log_namer_->Rotate(node, &node_state_[node_index].log_file_header); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void Logger::SetStartTime(size_t node_index, |
| 273 | aos::monotonic_clock::time_point monotonic_start_time, |
| 274 | aos::realtime_clock::time_point realtime_start_time) { |
| 275 | node_state_[node_index].monotonic_start_time = monotonic_start_time; |
| 276 | node_state_[node_index].realtime_start_time = realtime_start_time; |
| 277 | node_state_[node_index] |
| 278 | .log_file_header.mutable_message() |
| 279 | ->mutate_monotonic_start_time( |
| 280 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 281 | monotonic_start_time.time_since_epoch()) |
| 282 | .count()); |
| 283 | if (node_state_[node_index] |
| 284 | .log_file_header.mutable_message() |
| 285 | ->has_realtime_start_time()) { |
| 286 | node_state_[node_index] |
| 287 | .log_file_header.mutable_message() |
| 288 | ->mutate_realtime_start_time( |
| 289 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 290 | realtime_start_time.time_since_epoch()) |
| 291 | .count()); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | bool Logger::MaybeUpdateTimestamp( |
| 296 | const Node *node, int node_index, |
| 297 | aos::monotonic_clock::time_point monotonic_start_time, |
| 298 | aos::realtime_clock::time_point realtime_start_time) { |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 299 | // Bail early if the start times are already set. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 300 | if (node_state_[node_index].monotonic_start_time != |
| 301 | monotonic_clock::min_time) { |
| 302 | return false; |
| 303 | } |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 304 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 305 | if (event_loop_->node() == node) { |
| 306 | // There are no offsets to compute for ourself, so always succeed. |
| 307 | SetStartTime(node_index, monotonic_start_time, realtime_start_time); |
| 308 | return true; |
| 309 | } else if (server_statistics_fetcher_.get() != nullptr) { |
| 310 | // We must be a remote node now. Look for the connection and see if it is |
| 311 | // connected. |
| 312 | |
| 313 | for (const message_bridge::ServerConnection *connection : |
| 314 | *server_statistics_fetcher_->connections()) { |
| 315 | if (connection->node()->name()->string_view() != |
| 316 | node->name()->string_view()) { |
| 317 | continue; |
| 318 | } |
| 319 | |
| 320 | if (connection->state() != message_bridge::State::CONNECTED) { |
| 321 | VLOG(1) << node->name()->string_view() |
| 322 | << " is not connected, can't start it yet."; |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | if (!connection->has_monotonic_offset()) { |
| 327 | VLOG(1) << "Missing monotonic offset for setting start time for node " |
| 328 | << aos::FlatbufferToJson(node); |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | VLOG(1) << "Updating start time for " << aos::FlatbufferToJson(node); |
| 333 | |
| 334 | // Found it and it is connected. Compensate and go. |
| 335 | monotonic_start_time += |
| 336 | std::chrono::nanoseconds(connection->monotonic_offset()); |
| 337 | |
| 338 | SetStartTime(node_index, monotonic_start_time, realtime_start_time); |
| 339 | return true; |
| 340 | } |
| 341 | } |
| 342 | } else { |
| 343 | SetStartTime(node_index, monotonic_start_time, realtime_start_time); |
| 344 | return true; |
| 345 | } |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> Logger::MakeHeader( |
| 350 | const Node *node) { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 351 | // Now write the header with this timestamp in it. |
| 352 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 353 | fbb.ForceDefaults(true); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 354 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 355 | // TODO(austin): Compress this much more efficiently. There are a bunch of |
| 356 | // duplicated schemas. |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 357 | flatbuffers::Offset<aos::Configuration> configuration_offset = |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 358 | CopyFlatBuffer(configuration_, &fbb); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 359 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 360 | flatbuffers::Offset<flatbuffers::String> name_offset = |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 361 | fbb.CreateString(name_); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 362 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 363 | flatbuffers::Offset<flatbuffers::String> logger_uuid_offset = |
| 364 | fbb.CreateString(uuid_.string_view()); |
| 365 | |
| 366 | flatbuffers::Offset<flatbuffers::String> parts_uuid_offset = |
| 367 | fbb.CreateString("00000000-0000-4000-8000-000000000000"); |
| 368 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 369 | flatbuffers::Offset<Node> node_offset; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 370 | |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 371 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 372 | node_offset = CopyFlatBuffer(node, &fbb); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | aos::logger::LogFileHeader::Builder log_file_header_builder(fbb); |
| 376 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 377 | log_file_header_builder.add_name(name_offset); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 378 | |
| 379 | // Only add the node if we are running in a multinode configuration. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 380 | if (node != nullptr) { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 381 | log_file_header_builder.add_node(node_offset); |
| 382 | } |
| 383 | |
| 384 | log_file_header_builder.add_configuration(configuration_offset); |
| 385 | // The worst case theoretical out of order is the polling period times 2. |
| 386 | // One message could get logged right after the boundary, but be for right |
| 387 | // before the next boundary. And the reverse could happen for another |
| 388 | // message. Report back 3x to be extra safe, and because the cost isn't |
| 389 | // huge on the read side. |
| 390 | log_file_header_builder.add_max_out_of_order_duration( |
| 391 | std::chrono::duration_cast<std::chrono::nanoseconds>(3 * polling_period_) |
| 392 | .count()); |
| 393 | |
| 394 | log_file_header_builder.add_monotonic_start_time( |
| 395 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 396 | monotonic_clock::min_time.time_since_epoch()) |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 397 | .count()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 398 | if (node == event_loop_->node()) { |
| 399 | log_file_header_builder.add_realtime_start_time( |
| 400 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 401 | realtime_clock::min_time.time_since_epoch()) |
| 402 | .count()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 405 | log_file_header_builder.add_logger_uuid(logger_uuid_offset); |
| 406 | |
| 407 | log_file_header_builder.add_parts_uuid(parts_uuid_offset); |
| 408 | log_file_header_builder.add_parts_index(0); |
| 409 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 410 | fbb.FinishSizePrefixed(log_file_header_builder.Finish()); |
| 411 | return fbb.Release(); |
| 412 | } |
| 413 | |
| 414 | void Logger::Rotate() { |
| 415 | for (const Node *node : log_namer_->nodes()) { |
| 416 | const int node_index = |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 417 | configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 418 | log_namer_->Rotate(node, &node_state_[node_index].log_file_header); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | void Logger::LogUntil(monotonic_clock::time_point t) { |
| 423 | WriteMissingTimestamps(); |
| 424 | |
| 425 | // Write each channel to disk, one at a time. |
| 426 | for (FetcherStruct &f : fetchers_) { |
| 427 | while (true) { |
| 428 | if (f.written) { |
| 429 | if (!f.fetcher->FetchNext()) { |
| 430 | VLOG(2) << "No new data on " |
| 431 | << configuration::CleanedChannelToString( |
| 432 | f.fetcher->channel()); |
| 433 | break; |
| 434 | } else { |
| 435 | f.written = false; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | CHECK(!f.written); |
| 440 | |
| 441 | // TODO(james): Write tests to exercise this logic. |
| 442 | if (f.fetcher->context().monotonic_event_time < t) { |
| 443 | if (f.writer != nullptr) { |
| 444 | // Write! |
| 445 | flatbuffers::FlatBufferBuilder fbb(f.fetcher->context().size + |
| 446 | max_header_size_); |
| 447 | fbb.ForceDefaults(true); |
| 448 | |
| 449 | fbb.FinishSizePrefixed(PackMessage(&fbb, f.fetcher->context(), |
| 450 | f.channel_index, f.log_type)); |
| 451 | |
| 452 | VLOG(2) << "Writing data as node " |
| 453 | << FlatbufferToJson(event_loop_->node()) << " for channel " |
| 454 | << configuration::CleanedChannelToString(f.fetcher->channel()) |
| 455 | << " to " << f.writer->filename() << " data " |
| 456 | << FlatbufferToJson( |
| 457 | flatbuffers::GetSizePrefixedRoot<MessageHeader>( |
| 458 | fbb.GetBufferPointer())); |
| 459 | |
| 460 | max_header_size_ = std::max( |
| 461 | max_header_size_, fbb.GetSize() - f.fetcher->context().size); |
| 462 | f.writer->QueueSizedFlatbuffer(&fbb); |
| 463 | } |
| 464 | |
| 465 | if (f.timestamp_writer != nullptr) { |
| 466 | // And now handle timestamps. |
| 467 | flatbuffers::FlatBufferBuilder fbb; |
| 468 | fbb.ForceDefaults(true); |
| 469 | |
| 470 | fbb.FinishSizePrefixed(PackMessage(&fbb, f.fetcher->context(), |
| 471 | f.channel_index, |
| 472 | LogType::kLogDeliveryTimeOnly)); |
| 473 | |
| 474 | VLOG(2) << "Writing timestamps as node " |
| 475 | << FlatbufferToJson(event_loop_->node()) << " for channel " |
| 476 | << configuration::CleanedChannelToString(f.fetcher->channel()) |
| 477 | << " to " << f.timestamp_writer->filename() << " timestamp " |
| 478 | << FlatbufferToJson( |
| 479 | flatbuffers::GetSizePrefixedRoot<MessageHeader>( |
| 480 | fbb.GetBufferPointer())); |
| 481 | |
| 482 | f.timestamp_writer->QueueSizedFlatbuffer(&fbb); |
| 483 | } |
| 484 | |
| 485 | if (f.contents_writer != nullptr) { |
| 486 | // And now handle the special message contents channel. Copy the |
| 487 | // message into a FlatBufferBuilder and save it to disk. |
| 488 | // TODO(austin): We can be more efficient here when we start to |
| 489 | // care... |
| 490 | flatbuffers::FlatBufferBuilder fbb; |
| 491 | fbb.ForceDefaults(true); |
| 492 | |
| 493 | const MessageHeader *msg = |
| 494 | flatbuffers::GetRoot<MessageHeader>(f.fetcher->context().data); |
| 495 | |
| 496 | logger::MessageHeader::Builder message_header_builder(fbb); |
| 497 | |
| 498 | // Note: this must match the same order as MessageBridgeServer and |
| 499 | // PackMessage. We want identical headers to have identical |
| 500 | // on-the-wire formats to make comparing them easier. |
| 501 | message_header_builder.add_channel_index(msg->channel_index()); |
| 502 | |
| 503 | message_header_builder.add_queue_index(msg->queue_index()); |
| 504 | message_header_builder.add_monotonic_sent_time( |
| 505 | msg->monotonic_sent_time()); |
| 506 | message_header_builder.add_realtime_sent_time( |
| 507 | msg->realtime_sent_time()); |
| 508 | |
| 509 | message_header_builder.add_monotonic_remote_time( |
| 510 | msg->monotonic_remote_time()); |
| 511 | message_header_builder.add_realtime_remote_time( |
| 512 | msg->realtime_remote_time()); |
| 513 | message_header_builder.add_remote_queue_index( |
| 514 | msg->remote_queue_index()); |
| 515 | |
| 516 | fbb.FinishSizePrefixed(message_header_builder.Finish()); |
| 517 | |
| 518 | f.contents_writer->QueueSizedFlatbuffer(&fbb); |
| 519 | } |
| 520 | |
| 521 | f.written = true; |
| 522 | } else { |
| 523 | break; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | last_synchronized_time_ = t; |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 530 | void Logger::DoLogData() { |
| 531 | // We want to guarentee that messages aren't out of order by more than |
| 532 | // max_out_of_order_duration. To do this, we need sync points. Every write |
| 533 | // cycle should be a sync point. |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 534 | const monotonic_clock::time_point monotonic_now = |
| 535 | event_loop_->monotonic_now(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 536 | |
| 537 | do { |
| 538 | // Move the sync point up by at most polling_period. This forces one sync |
| 539 | // per iteration, even if it is small. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 540 | LogUntil( |
| 541 | std::min(last_synchronized_time_ + polling_period_, monotonic_now)); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 542 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 543 | // If we missed cycles, we could be pretty far behind. Spin until we are |
| 544 | // caught up. |
| 545 | } while (last_synchronized_time_ + polling_period_ < monotonic_now); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 548 | std::vector<LogFile> SortParts(const std::vector<std::string> &parts) { |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 549 | // Start by grouping all parts by UUID, and extracting the part index. |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 550 | // Datastructure to hold all the info extracted from a set of parts which go |
| 551 | // together so we can sort them afterwords. |
| 552 | struct UnsortedLogParts { |
| 553 | // Start times. |
| 554 | aos::monotonic_clock::time_point monotonic_start_time; |
| 555 | aos::realtime_clock::time_point realtime_start_time; |
| 556 | |
| 557 | // Node to save. |
| 558 | std::string node; |
| 559 | |
| 560 | // Pairs of the filename and the part index for sorting. |
| 561 | std::vector<std::pair<std::string, int>> parts; |
| 562 | }; |
| 563 | |
| 564 | // Map holding the logger_uuid -> second map. The second map holds the |
| 565 | // parts_uuid -> list of parts for sorting. |
| 566 | std::map<std::string, std::map<std::string, UnsortedLogParts>> parts_list; |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 567 | |
| 568 | // Sort part files without UUIDs and part indexes as well. Extract everything |
| 569 | // useful from the log in the first pass, then sort later. |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 570 | struct UnsortedOldParts { |
| 571 | // Part information with everything but the list of parts. |
| 572 | LogParts parts; |
| 573 | |
| 574 | // Tuple of time for the data and filename needed for sorting after |
| 575 | // extracting. |
| 576 | std::vector<std::pair<monotonic_clock::time_point, std::string>> unsorted_parts; |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 577 | }; |
| 578 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 579 | // A list of all the old parts which we don't know how to sort using uuids. |
| 580 | // There are enough of these in the wild that this is worth supporting. |
| 581 | std::vector<UnsortedOldParts> old_parts; |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 582 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 583 | // Now extract everything into our datastructures above for sorting. |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 584 | for (const std::string &part : parts) { |
| 585 | FlatbufferVector<LogFileHeader> log_header = ReadHeader(part); |
| 586 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 587 | const monotonic_clock::time_point monotonic_start_time( |
| 588 | chrono::nanoseconds(log_header.message().monotonic_start_time())); |
| 589 | const realtime_clock::time_point realtime_start_time( |
| 590 | chrono::nanoseconds(log_header.message().realtime_start_time())); |
| 591 | |
| 592 | const std::string_view node = |
| 593 | log_header.message().has_node() |
| 594 | ? log_header.message().node()->name()->string_view() |
| 595 | : ""; |
| 596 | |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 597 | // Looks like an old log. No UUID, index, and also single node. We have |
| 598 | // little to no multi-node log files in the wild without part UUIDs and |
| 599 | // indexes which we care much about. |
| 600 | if (!log_header.message().has_parts_uuid() && |
| 601 | !log_header.message().has_parts_index() && |
| 602 | !log_header.message().has_node()) { |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 603 | FlatbufferVector<MessageHeader> first_message = ReadNthMessage(part, 0); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 604 | const monotonic_clock::time_point first_message_time( |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 605 | chrono::nanoseconds(first_message.message().monotonic_sent_time())); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 606 | |
| 607 | // Find anything with a matching start time. They all go together. |
| 608 | auto result = std::find_if( |
| 609 | old_parts.begin(), old_parts.end(), |
| 610 | [&](const UnsortedOldParts &parts) { |
| 611 | return parts.parts.monotonic_start_time == monotonic_start_time && |
| 612 | parts.parts.realtime_start_time == realtime_start_time; |
| 613 | }); |
| 614 | |
| 615 | if (result == old_parts.end()) { |
| 616 | old_parts.emplace_back(); |
| 617 | old_parts.back().parts.monotonic_start_time = monotonic_start_time; |
| 618 | old_parts.back().parts.realtime_start_time = realtime_start_time; |
| 619 | old_parts.back().unsorted_parts.emplace_back( |
| 620 | std::make_pair(first_message_time, part)); |
| 621 | } else { |
| 622 | result->unsorted_parts.emplace_back( |
| 623 | std::make_pair(first_message_time, part)); |
| 624 | } |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 625 | continue; |
| 626 | } |
| 627 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 628 | CHECK(log_header.message().has_logger_uuid()); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 629 | CHECK(log_header.message().has_parts_uuid()); |
| 630 | CHECK(log_header.message().has_parts_index()); |
| 631 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 632 | const std::string logger_uuid = log_header.message().logger_uuid()->str(); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 633 | const std::string parts_uuid = log_header.message().parts_uuid()->str(); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 634 | int32_t parts_index = log_header.message().parts_index(); |
| 635 | |
| 636 | auto log_it = parts_list.find(logger_uuid); |
| 637 | if (log_it == parts_list.end()) { |
| 638 | log_it = parts_list |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 639 | .insert(std::make_pair( |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 640 | logger_uuid, std::map<std::string, UnsortedLogParts>())) |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 641 | .first; |
| 642 | } |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 643 | |
| 644 | auto it = log_it->second.find(parts_uuid); |
| 645 | if (it == log_it->second.end()) { |
| 646 | it = log_it->second.insert(std::make_pair(parts_uuid, UnsortedLogParts())) |
| 647 | .first; |
| 648 | it->second.monotonic_start_time = monotonic_start_time; |
| 649 | it->second.realtime_start_time = realtime_start_time; |
| 650 | it->second.node = std::string(node); |
| 651 | } |
| 652 | |
| 653 | // First part might be min_time. If it is, try to put a better time on it. |
| 654 | if (it->second.monotonic_start_time == monotonic_clock::min_time) { |
| 655 | it->second.monotonic_start_time = monotonic_start_time; |
| 656 | } else if (monotonic_start_time != monotonic_clock::min_time) { |
| 657 | CHECK_EQ(it->second.monotonic_start_time, monotonic_start_time); |
| 658 | } |
| 659 | if (it->second.realtime_start_time == realtime_clock::min_time) { |
| 660 | it->second.realtime_start_time = realtime_start_time; |
| 661 | } else if (realtime_start_time != realtime_clock::min_time) { |
| 662 | CHECK_EQ(it->second.realtime_start_time, realtime_start_time); |
| 663 | } |
| 664 | |
| 665 | it->second.parts.emplace_back(std::make_pair(part, parts_index)); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | CHECK_NE(old_parts.empty(), parts_list.empty()) |
| 669 | << ": Can't have a mix of old and new parts."; |
| 670 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 671 | // Now reformat old_parts to be in the right datastructure to report. |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 672 | if (!old_parts.empty()) { |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 673 | std::vector<LogFile> result; |
| 674 | for (UnsortedOldParts &p : old_parts) { |
| 675 | // Sort by the oldest message in each file. |
| 676 | std::sort( |
| 677 | p.unsorted_parts.begin(), p.unsorted_parts.end(), |
| 678 | [](const std::pair<monotonic_clock::time_point, std::string> &a, |
| 679 | const std::pair<monotonic_clock::time_point, std::string> &b) { |
| 680 | return a.first < b.first; |
| 681 | }); |
| 682 | LogFile log_file; |
| 683 | for (std::pair<monotonic_clock::time_point, std::string> &f : |
| 684 | p.unsorted_parts) { |
| 685 | p.parts.parts.emplace_back(std::move(f.second)); |
| 686 | } |
| 687 | log_file.parts.emplace_back(std::move(p.parts)); |
| 688 | result.emplace_back(std::move(log_file)); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 689 | } |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 690 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 691 | return result; |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | // Now, sort them and produce the final vector form. |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 695 | std::vector<LogFile> result; |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 696 | result.reserve(parts_list.size()); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 697 | for (std::pair<const std::string, std::map<std::string, UnsortedLogParts>> &logs : parts_list) { |
| 698 | LogFile new_file; |
| 699 | new_file.logger_uuid = logs.first; |
| 700 | for (std::pair<const std::string, UnsortedLogParts> &parts : logs.second) { |
| 701 | LogParts new_parts; |
| 702 | new_parts.monotonic_start_time = parts.second.monotonic_start_time; |
| 703 | new_parts.realtime_start_time = parts.second.realtime_start_time; |
| 704 | new_parts.logger_uuid = logs.first; |
| 705 | new_parts.parts_uuid = parts.first; |
| 706 | new_parts.node = std::move(parts.second.node); |
| 707 | |
| 708 | std::sort(parts.second.parts.begin(), parts.second.parts.end(), |
| 709 | [](const std::pair<std::string, int> &a, |
| 710 | const std::pair<std::string, int> &b) { |
| 711 | return a.second < b.second; |
| 712 | }); |
| 713 | new_parts.parts.reserve(parts.second.parts.size()); |
| 714 | for (std::pair<std::string, int> &p : parts.second.parts) { |
| 715 | new_parts.parts.emplace_back(std::move(p.first)); |
| 716 | } |
| 717 | new_file.parts.emplace_back(std::move(new_parts)); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 718 | } |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 719 | result.emplace_back(std::move(new_file)); |
| 720 | } |
| 721 | return result; |
| 722 | } |
| 723 | |
| 724 | std::ostream &operator<<(std::ostream &stream, const LogFile &file) { |
| 725 | stream << "{"; |
| 726 | if (!file.logger_uuid.empty()) { |
| 727 | stream << "\"logger_uuid\": \"" << file.logger_uuid << "\", "; |
| 728 | } |
| 729 | stream << "\"parts\": ["; |
| 730 | for (size_t i = 0; i < file.parts.size(); ++i) { |
| 731 | if (i != 0u) { |
| 732 | stream << ", "; |
| 733 | } |
| 734 | stream << file.parts[i]; |
| 735 | } |
| 736 | stream << "]}"; |
| 737 | return stream; |
| 738 | } |
| 739 | std::ostream &operator<<(std::ostream &stream, const LogParts &parts) { |
| 740 | stream << "{"; |
| 741 | if (!parts.logger_uuid.empty()) { |
| 742 | stream << "\"logger_uuid\": \"" << parts.logger_uuid << "\", "; |
| 743 | } |
| 744 | if (!parts.parts_uuid.empty()) { |
| 745 | stream << "\"parts_uuid\": \"" << parts.parts_uuid << "\", "; |
| 746 | } |
| 747 | if (!parts.node.empty()) { |
| 748 | stream << "\"node\": \"" << parts.node << "\", "; |
| 749 | } |
| 750 | stream << "\"monotonic_start_time\": " << parts.monotonic_start_time |
| 751 | << ", \"realtime_start_time\": " << parts.realtime_start_time << ", ["; |
| 752 | |
| 753 | for (size_t i = 0; i < parts.parts.size(); ++i) { |
| 754 | if (i != 0u) { |
| 755 | stream << ", "; |
| 756 | } |
| 757 | stream << parts.parts[i]; |
| 758 | } |
| 759 | |
| 760 | stream << "]}"; |
| 761 | return stream; |
| 762 | } |
| 763 | |
| 764 | std::vector<std::vector<std::string>> ToLogReaderVector( |
| 765 | const std::vector<LogFile> &log_files) { |
| 766 | std::vector<std::vector<std::string>> result; |
| 767 | for (const LogFile &log_file : log_files) { |
| 768 | for (const LogParts &log_parts : log_file.parts) { |
| 769 | std::vector<std::string> parts; |
| 770 | for (const std::string &part : log_parts.parts) { |
| 771 | parts.emplace_back(part); |
| 772 | } |
| 773 | result.emplace_back(std::move(parts)); |
| 774 | } |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 775 | } |
| 776 | return result; |
| 777 | } |
| 778 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 779 | LogReader::LogReader(std::string_view filename, |
| 780 | const Configuration *replay_configuration) |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 781 | : LogReader(std::vector<std::string>{std::string(filename)}, |
| 782 | replay_configuration) {} |
| 783 | |
| 784 | LogReader::LogReader(const std::vector<std::string> &filenames, |
| 785 | const Configuration *replay_configuration) |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 786 | : LogReader(std::vector<std::vector<std::string>>{filenames}, |
| 787 | replay_configuration) {} |
| 788 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 789 | // TODO(austin): Make this the base and kill the others. This has much better |
| 790 | // context for sorting. |
| 791 | LogReader::LogReader(const std::vector<LogFile> &log_files, |
| 792 | const Configuration *replay_configuration) |
| 793 | : LogReader(ToLogReaderVector(log_files), replay_configuration) {} |
| 794 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 795 | LogReader::LogReader(const std::vector<std::vector<std::string>> &filenames, |
| 796 | const Configuration *replay_configuration) |
| 797 | : filenames_(filenames), |
| 798 | log_file_header_(ReadHeader(filenames[0][0])), |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 799 | replay_configuration_(replay_configuration) { |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 800 | MakeRemappedConfig(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 801 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 802 | if (replay_configuration) { |
| 803 | CHECK_EQ(configuration::MultiNode(configuration()), |
| 804 | configuration::MultiNode(replay_configuration)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 805 | << ": Log file and replay config need to both be multi or single " |
| 806 | "node."; |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 807 | } |
| 808 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 809 | if (!configuration::MultiNode(configuration())) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 810 | states_.emplace_back( |
| 811 | std::make_unique<State>(std::make_unique<ChannelMerger>(filenames))); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 812 | } else { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 813 | if (replay_configuration) { |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 814 | CHECK_EQ(logged_configuration()->nodes()->size(), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 815 | replay_configuration->nodes()->size()) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 816 | << ": Log file and replay config need to have matching nodes " |
| 817 | "lists."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 818 | for (const Node *node : *logged_configuration()->nodes()) { |
| 819 | if (configuration::GetNode(replay_configuration, node) == nullptr) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 820 | LOG(FATAL) << "Found node " << FlatbufferToJson(node) |
| 821 | << " in logged config that is not present in the replay " |
| 822 | "config."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 823 | } |
| 824 | } |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 825 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 826 | states_.resize(configuration()->nodes()->size()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 827 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 828 | } |
| 829 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 830 | LogReader::~LogReader() { |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 831 | if (event_loop_factory_unique_ptr_) { |
| 832 | Deregister(); |
| 833 | } else if (event_loop_factory_ != nullptr) { |
| 834 | LOG(FATAL) << "Must call Deregister before the SimulatedEventLoopFactory " |
| 835 | "is destroyed"; |
| 836 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 837 | if (offset_fp_ != nullptr) { |
| 838 | fclose(offset_fp_); |
| 839 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 840 | // Zero out some buffers. It's easy to do use-after-frees on these, so make |
| 841 | // it more obvious. |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 842 | if (remapped_configuration_buffer_) { |
| 843 | remapped_configuration_buffer_->Wipe(); |
| 844 | } |
| 845 | log_file_header_.Wipe(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 846 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 847 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 848 | const Configuration *LogReader::logged_configuration() const { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 849 | return log_file_header_.message().configuration(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 850 | } |
| 851 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 852 | const Configuration *LogReader::configuration() const { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 853 | return remapped_configuration_; |
| 854 | } |
| 855 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 856 | std::vector<const Node *> LogReader::Nodes() const { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 857 | // Because the Node pointer will only be valid if it actually points to |
| 858 | // memory owned by remapped_configuration_, we need to wait for the |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 859 | // remapped_configuration_ to be populated before accessing it. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 860 | // |
| 861 | // Also, note, that when ever a map is changed, the nodes in here are |
| 862 | // invalidated. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 863 | CHECK(remapped_configuration_ != nullptr) |
| 864 | << ": Need to call Register before the node() pointer will be valid."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 865 | return configuration::GetNodes(remapped_configuration_); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 866 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 867 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 868 | monotonic_clock::time_point LogReader::monotonic_start_time( |
| 869 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 870 | State *state = |
| 871 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 872 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 873 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 874 | return state->monotonic_start_time(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 875 | } |
| 876 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame^] | 877 | realtime_clock::time_point LogReader::realtime_start_time( |
| 878 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 879 | State *state = |
| 880 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 881 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 882 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 883 | return state->realtime_start_time(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 884 | } |
| 885 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 886 | void LogReader::Register() { |
| 887 | event_loop_factory_unique_ptr_ = |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 888 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 889 | Register(event_loop_factory_unique_ptr_.get()); |
| 890 | } |
| 891 | |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 892 | void LogReader::Register(SimulatedEventLoopFactory *event_loop_factory) { |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 893 | event_loop_factory_ = event_loop_factory; |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 894 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 895 | for (const Node *node : configuration::GetNodes(configuration())) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 896 | const size_t node_index = |
| 897 | configuration::GetNodeIndex(configuration(), node); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 898 | states_[node_index] = |
| 899 | std::make_unique<State>(std::make_unique<ChannelMerger>(filenames_)); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 900 | State *state = states_[node_index].get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 901 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 902 | Register(state->SetNodeEventLoopFactory( |
| 903 | event_loop_factory_->GetNodeEventLoopFactory(node))); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 904 | } |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 905 | if (live_nodes_ == 0) { |
| 906 | LOG(FATAL) |
| 907 | << "Don't have logs from any of the nodes in the replay config--are " |
| 908 | "you sure that the replay config matches the original config?"; |
| 909 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 910 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 911 | // We need to now seed our per-node time offsets and get everything set up |
| 912 | // to run. |
| 913 | const size_t num_nodes = nodes_count(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 914 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 915 | // It is easiest to solve for per node offsets with a matrix rather than |
| 916 | // trying to solve the equations by hand. So let's get after it. |
| 917 | // |
| 918 | // Now, build up the map matrix. |
| 919 | // |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 920 | // offset_matrix_ = (map_matrix_ + slope_matrix_) * [ta; tb; tc] |
| 921 | map_matrix_ = Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic>::Zero( |
| 922 | filters_.size() + 1, num_nodes); |
| 923 | slope_matrix_ = |
| 924 | Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic>::Zero( |
| 925 | filters_.size() + 1, num_nodes); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 926 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 927 | offset_matrix_ = |
| 928 | Eigen::Matrix<mpq_class, Eigen::Dynamic, 1>::Zero(filters_.size() + 1); |
| 929 | valid_matrix_ = |
| 930 | Eigen::Matrix<bool, Eigen::Dynamic, 1>::Zero(filters_.size() + 1); |
| 931 | last_valid_matrix_ = |
| 932 | Eigen::Matrix<bool, Eigen::Dynamic, 1>::Zero(filters_.size() + 1); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 933 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 934 | time_offset_matrix_ = Eigen::VectorXd::Zero(num_nodes); |
| 935 | time_slope_matrix_ = Eigen::VectorXd::Zero(num_nodes); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 936 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 937 | // All times should average out to the distributed clock. |
| 938 | for (int i = 0; i < map_matrix_.cols(); ++i) { |
| 939 | // 1/num_nodes. |
| 940 | map_matrix_(0, i) = mpq_class(1, num_nodes); |
| 941 | } |
| 942 | valid_matrix_(0) = true; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 943 | |
| 944 | { |
| 945 | // Now, add the a - b -> sample elements. |
| 946 | size_t i = 1; |
| 947 | for (std::pair<const std::tuple<const Node *, const Node *>, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 948 | std::tuple<message_bridge::NoncausalOffsetEstimator>> |
| 949 | &filter : filters_) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 950 | const Node *const node_a = std::get<0>(filter.first); |
| 951 | const Node *const node_b = std::get<1>(filter.first); |
| 952 | |
| 953 | const size_t node_a_index = |
| 954 | configuration::GetNodeIndex(configuration(), node_a); |
| 955 | const size_t node_b_index = |
| 956 | configuration::GetNodeIndex(configuration(), node_b); |
| 957 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 958 | // -a |
| 959 | map_matrix_(i, node_a_index) = mpq_class(-1); |
| 960 | // +b |
| 961 | map_matrix_(i, node_b_index) = mpq_class(1); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 962 | |
| 963 | // -> sample |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 964 | std::get<0>(filter.second) |
| 965 | .set_slope_pointer(&slope_matrix_(i, node_a_index)); |
| 966 | std::get<0>(filter.second).set_offset_pointer(&offset_matrix_(i, 0)); |
| 967 | |
| 968 | valid_matrix_(i) = false; |
| 969 | std::get<0>(filter.second).set_valid_pointer(&valid_matrix_(i)); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 970 | |
| 971 | ++i; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 975 | for (std::unique_ptr<State> &state : states_) { |
| 976 | state->SeedSortedMessages(); |
| 977 | } |
| 978 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 979 | // Rank of the map matrix tells you if all the nodes are in communication |
| 980 | // with each other, which tells you if the offsets are observable. |
| 981 | const size_t connected_nodes = |
| 982 | Eigen::FullPivLU< |
| 983 | Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic>>(map_matrix_) |
| 984 | .rank(); |
| 985 | |
| 986 | // We don't need to support isolated nodes until someone has a real use |
| 987 | // case. |
| 988 | CHECK_EQ(connected_nodes, num_nodes) |
| 989 | << ": There is a node which isn't communicating with the rest."; |
| 990 | |
| 991 | // And solve. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 992 | UpdateOffsets(); |
| 993 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 994 | // We want to start the log file at the last start time of the log files |
| 995 | // from all the nodes. Compute how long each node's simulation needs to run |
| 996 | // to move time to this point. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 997 | distributed_clock::time_point start_time = distributed_clock::min_time; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 998 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 999 | // TODO(austin): We want an "OnStart" callback for each node rather than |
| 1000 | // running until the last node. |
| 1001 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1002 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1003 | VLOG(1) << "Start time is " << state->monotonic_start_time() << " for node " |
| 1004 | << MaybeNodeName(state->event_loop()->node()) << "now " |
| 1005 | << state->monotonic_now(); |
| 1006 | // And start computing the start time on the distributed clock now that |
| 1007 | // that works. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1008 | start_time = std::max( |
| 1009 | start_time, state->ToDistributedClock(state->monotonic_start_time())); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1010 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1011 | |
| 1012 | CHECK_GE(start_time, distributed_clock::epoch()) |
| 1013 | << ": Hmm, we have a node starting before the start of time. Offset " |
| 1014 | "everything."; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1015 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1016 | // Forwarding is tracked per channel. If it is enabled, we want to turn it |
| 1017 | // off. Otherwise messages replayed will get forwarded across to the other |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1018 | // nodes, and also replayed on the other nodes. This may not satisfy all |
| 1019 | // our users, but it'll start the discussion. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1020 | if (configuration::MultiNode(event_loop_factory_->configuration())) { |
| 1021 | for (size_t i = 0; i < logged_configuration()->channels()->size(); ++i) { |
| 1022 | const Channel *channel = logged_configuration()->channels()->Get(i); |
| 1023 | const Node *node = configuration::GetNode( |
| 1024 | configuration(), channel->source_node()->string_view()); |
| 1025 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1026 | State *state = |
| 1027 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1028 | |
| 1029 | const Channel *remapped_channel = |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1030 | RemapChannel(state->event_loop(), channel); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1031 | |
| 1032 | event_loop_factory_->DisableForwarding(remapped_channel); |
| 1033 | } |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1034 | |
| 1035 | // If we are replaying a log, we don't want a bunch of redundant messages |
| 1036 | // from both the real message bridge and simulated message bridge. |
| 1037 | event_loop_factory_->DisableStatistics(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1040 | // While we are starting the system up, we might be relying on matching data |
| 1041 | // to timestamps on log files where the timestamp log file starts before the |
| 1042 | // data. In this case, it is reasonable to expect missing data. |
| 1043 | ignore_missing_data_ = true; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1044 | VLOG(1) << "Running until " << start_time << " in Register"; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1045 | event_loop_factory_->RunFor(start_time.time_since_epoch()); |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1046 | VLOG(1) << "At start time"; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1047 | // Now that we are running for real, missing data means that the log file is |
| 1048 | // corrupted or went wrong. |
| 1049 | ignore_missing_data_ = false; |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1050 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1051 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1052 | // Make the RT clock be correct before handing it to the user. |
| 1053 | if (state->realtime_start_time() != realtime_clock::min_time) { |
| 1054 | state->SetRealtimeOffset(state->monotonic_start_time(), |
| 1055 | state->realtime_start_time()); |
| 1056 | } |
| 1057 | VLOG(1) << "Start time is " << state->monotonic_start_time() << " for node " |
| 1058 | << MaybeNodeName(state->event_loop()->node()) << "now " |
| 1059 | << state->monotonic_now(); |
| 1060 | } |
| 1061 | |
| 1062 | if (FLAGS_timestamps_to_csv) { |
| 1063 | for (std::pair<const std::tuple<const Node *, const Node *>, |
| 1064 | std::tuple<message_bridge::NoncausalOffsetEstimator>> |
| 1065 | &filter : filters_) { |
| 1066 | const Node *const node_a = std::get<0>(filter.first); |
| 1067 | const Node *const node_b = std::get<1>(filter.first); |
| 1068 | |
| 1069 | std::get<0>(filter.second) |
| 1070 | .SetFirstFwdTime(event_loop_factory_->GetNodeEventLoopFactory(node_a) |
| 1071 | ->monotonic_now()); |
| 1072 | std::get<0>(filter.second) |
| 1073 | .SetFirstRevTime(event_loop_factory_->GetNodeEventLoopFactory(node_b) |
| 1074 | ->monotonic_now()); |
| 1075 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1079 | void LogReader::UpdateOffsets() { |
| 1080 | VLOG(2) << "Samples are " << offset_matrix_; |
| 1081 | VLOG(2) << "Map is " << (map_matrix_ + slope_matrix_); |
| 1082 | std::tie(time_slope_matrix_, time_offset_matrix_) = SolveOffsets(); |
| 1083 | Eigen::IOFormat HeavyFmt(Eigen::FullPrecision, 0, ", ", ";\n", "[", "]", "[", |
| 1084 | "]"); |
| 1085 | VLOG(1) << "First slope " << time_slope_matrix_.transpose().format(HeavyFmt) |
| 1086 | << " offset " << time_offset_matrix_.transpose().format(HeavyFmt); |
| 1087 | |
| 1088 | size_t node_index = 0; |
| 1089 | for (std::unique_ptr<State> &state : states_) { |
| 1090 | state->SetDistributedOffset(offset(node_index), slope(node_index)); |
| 1091 | VLOG(1) << "Offset for node " << node_index << " " |
| 1092 | << MaybeNodeName(state->event_loop()->node()) << "is " |
| 1093 | << aos::distributed_clock::time_point(offset(node_index)) |
| 1094 | << " slope " << std::setprecision(9) << std::fixed |
| 1095 | << slope(node_index); |
| 1096 | ++node_index; |
| 1097 | } |
| 1098 | |
| 1099 | if (VLOG_IS_ON(1)) { |
| 1100 | LogFit("Offset is"); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | void LogReader::LogFit(std::string_view prefix) { |
| 1105 | for (std::unique_ptr<State> &state : states_) { |
| 1106 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << " now " |
| 1107 | << state->monotonic_now() << " distributed " |
| 1108 | << event_loop_factory_->distributed_now(); |
| 1109 | } |
| 1110 | |
| 1111 | for (std::pair<const std::tuple<const Node *, const Node *>, |
| 1112 | std::tuple<message_bridge::NoncausalOffsetEstimator>> &filter : |
| 1113 | filters_) { |
| 1114 | message_bridge::NoncausalOffsetEstimator *estimator = |
| 1115 | &std::get<0>(filter.second); |
| 1116 | |
| 1117 | if (estimator->a_timestamps().size() == 0 && |
| 1118 | estimator->b_timestamps().size() == 0) { |
| 1119 | continue; |
| 1120 | } |
| 1121 | |
| 1122 | if (VLOG_IS_ON(1)) { |
| 1123 | estimator->LogFit(prefix); |
| 1124 | } |
| 1125 | |
| 1126 | const Node *const node_a = std::get<0>(filter.first); |
| 1127 | const Node *const node_b = std::get<1>(filter.first); |
| 1128 | |
| 1129 | const size_t node_a_index = |
| 1130 | configuration::GetNodeIndex(configuration(), node_a); |
| 1131 | const size_t node_b_index = |
| 1132 | configuration::GetNodeIndex(configuration(), node_b); |
| 1133 | |
| 1134 | const double recovered_slope = |
| 1135 | slope(node_b_index) / slope(node_a_index) - 1.0; |
| 1136 | const int64_t recovered_offset = |
| 1137 | offset(node_b_index).count() - offset(node_a_index).count() * |
| 1138 | slope(node_b_index) / |
| 1139 | slope(node_a_index); |
| 1140 | |
| 1141 | VLOG(1) << "Recovered slope " << std::setprecision(20) << recovered_slope |
| 1142 | << " (error " << recovered_slope - estimator->fit().slope() << ") " |
| 1143 | << " offset " << std::setprecision(20) << recovered_offset |
| 1144 | << " (error " |
| 1145 | << recovered_offset - estimator->fit().offset().count() << ")"; |
| 1146 | |
| 1147 | const aos::distributed_clock::time_point a0 = |
| 1148 | states_[node_a_index]->ToDistributedClock( |
| 1149 | std::get<0>(estimator->a_timestamps()[0])); |
| 1150 | const aos::distributed_clock::time_point a1 = |
| 1151 | states_[node_a_index]->ToDistributedClock( |
| 1152 | std::get<0>(estimator->a_timestamps()[1])); |
| 1153 | |
| 1154 | VLOG(1) << node_a->name()->string_view() << " timestamps()[0] = " |
| 1155 | << std::get<0>(estimator->a_timestamps()[0]) << " -> " << a0 |
| 1156 | << " distributed -> " << node_b->name()->string_view() << " " |
| 1157 | << states_[node_b_index]->FromDistributedClock(a0) << " should be " |
| 1158 | << aos::monotonic_clock::time_point( |
| 1159 | std::chrono::nanoseconds(static_cast<int64_t>( |
| 1160 | std::get<0>(estimator->a_timestamps()[0]) |
| 1161 | .time_since_epoch() |
| 1162 | .count() * |
| 1163 | (1.0 + estimator->fit().slope()))) + |
| 1164 | estimator->fit().offset()) |
| 1165 | << ((a0 <= event_loop_factory_->distributed_now()) |
| 1166 | ? "" |
| 1167 | : " After now, investigate"); |
| 1168 | VLOG(1) << node_a->name()->string_view() << " timestamps()[1] = " |
| 1169 | << std::get<0>(estimator->a_timestamps()[1]) << " -> " << a1 |
| 1170 | << " distributed -> " << node_b->name()->string_view() << " " |
| 1171 | << states_[node_b_index]->FromDistributedClock(a1) << " should be " |
| 1172 | << aos::monotonic_clock::time_point( |
| 1173 | std::chrono::nanoseconds(static_cast<int64_t>( |
| 1174 | std::get<0>(estimator->a_timestamps()[1]) |
| 1175 | .time_since_epoch() |
| 1176 | .count() * |
| 1177 | (1.0 + estimator->fit().slope()))) + |
| 1178 | estimator->fit().offset()) |
| 1179 | << ((event_loop_factory_->distributed_now() <= a1) |
| 1180 | ? "" |
| 1181 | : " Before now, investigate"); |
| 1182 | |
| 1183 | const aos::distributed_clock::time_point b0 = |
| 1184 | states_[node_b_index]->ToDistributedClock( |
| 1185 | std::get<0>(estimator->b_timestamps()[0])); |
| 1186 | const aos::distributed_clock::time_point b1 = |
| 1187 | states_[node_b_index]->ToDistributedClock( |
| 1188 | std::get<0>(estimator->b_timestamps()[1])); |
| 1189 | |
| 1190 | VLOG(1) << node_b->name()->string_view() << " timestamps()[0] = " |
| 1191 | << std::get<0>(estimator->b_timestamps()[0]) << " -> " << b0 |
| 1192 | << " distributed -> " << node_a->name()->string_view() << " " |
| 1193 | << states_[node_a_index]->FromDistributedClock(b0) |
| 1194 | << ((b0 <= event_loop_factory_->distributed_now()) |
| 1195 | ? "" |
| 1196 | : " After now, investigate"); |
| 1197 | VLOG(1) << node_b->name()->string_view() << " timestamps()[1] = " |
| 1198 | << std::get<0>(estimator->b_timestamps()[1]) << " -> " << b1 |
| 1199 | << " distributed -> " << node_a->name()->string_view() << " " |
| 1200 | << states_[node_a_index]->FromDistributedClock(b1) |
| 1201 | << ((event_loop_factory_->distributed_now() <= b1) |
| 1202 | ? "" |
| 1203 | : " Before now, investigate"); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | message_bridge::NoncausalOffsetEstimator *LogReader::GetFilter( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1208 | const Node *node_a, const Node *node_b) { |
| 1209 | CHECK_NE(node_a, node_b); |
| 1210 | CHECK_EQ(configuration::GetNode(configuration(), node_a), node_a); |
| 1211 | CHECK_EQ(configuration::GetNode(configuration(), node_b), node_b); |
| 1212 | |
| 1213 | if (node_a > node_b) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1214 | return GetFilter(node_b, node_a); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | auto tuple = std::make_tuple(node_a, node_b); |
| 1218 | |
| 1219 | auto it = filters_.find(tuple); |
| 1220 | |
| 1221 | if (it == filters_.end()) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1222 | auto &x = |
| 1223 | filters_ |
| 1224 | .insert(std::make_pair( |
| 1225 | tuple, std::make_tuple(message_bridge::NoncausalOffsetEstimator( |
| 1226 | node_a, node_b)))) |
| 1227 | .first->second; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1228 | if (FLAGS_timestamps_to_csv) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1229 | std::get<0>(x).SetFwdCsvFileName(absl::StrCat( |
| 1230 | "/tmp/timestamp_noncausal_", node_a->name()->string_view(), "_", |
| 1231 | node_b->name()->string_view())); |
| 1232 | std::get<0>(x).SetRevCsvFileName(absl::StrCat( |
| 1233 | "/tmp/timestamp_noncausal_", node_b->name()->string_view(), "_", |
| 1234 | node_a->name()->string_view())); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1237 | return &std::get<0>(x); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1238 | } else { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1239 | return &std::get<0>(it->second); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1243 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1244 | void LogReader::Register(EventLoop *event_loop) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1245 | State *state = |
| 1246 | states_[configuration::GetNodeIndex(configuration(), event_loop->node())] |
| 1247 | .get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1248 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1249 | state->set_event_loop(event_loop); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1250 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1251 | // We don't run timing reports when trying to print out logged data, because |
| 1252 | // otherwise we would end up printing out the timing reports themselves... |
| 1253 | // This is only really relevant when we are replaying into a simulation. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1254 | event_loop->SkipTimingReport(); |
| 1255 | event_loop->SkipAosLog(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1256 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1257 | const bool has_data = state->SetNode(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1258 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1259 | state->SetChannelCount(logged_configuration()->channels()->size()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1260 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1261 | for (size_t i = 0; i < logged_configuration()->channels()->size(); ++i) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1262 | const Channel *channel = |
| 1263 | RemapChannel(event_loop, logged_configuration()->channels()->Get(i)); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 1264 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1265 | NodeEventLoopFactory *channel_target_event_loop_factory = nullptr; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1266 | message_bridge::NoncausalOffsetEstimator *filter = nullptr; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1267 | |
| 1268 | if (!configuration::ChannelIsSendableOnNode(channel, event_loop->node()) && |
| 1269 | configuration::ChannelIsReadableOnNode(channel, event_loop->node())) { |
| 1270 | const Node *target_node = configuration::GetNode( |
| 1271 | event_loop->configuration(), channel->source_node()->string_view()); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1272 | filter = GetFilter(event_loop->node(), target_node); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1273 | |
| 1274 | if (event_loop_factory_ != nullptr) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1275 | channel_target_event_loop_factory = |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1276 | event_loop_factory_->GetNodeEventLoopFactory(target_node); |
| 1277 | } |
| 1278 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1279 | |
| 1280 | state->SetChannel(i, event_loop->MakeRawSender(channel), filter, |
| 1281 | channel_target_event_loop_factory); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1282 | } |
| 1283 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1284 | // If we didn't find any log files with data in them, we won't ever get a |
| 1285 | // callback or be live. So skip the rest of the setup. |
| 1286 | if (!has_data) { |
| 1287 | return; |
| 1288 | } |
| 1289 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1290 | state->set_timer_handler(event_loop->AddTimer([this, state]() { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1291 | VLOG(1) << "Starting sending " << MaybeNodeName(state->event_loop()->node()) |
| 1292 | << "at " << state->event_loop()->context().monotonic_event_time |
| 1293 | << " now " << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1294 | if (state->OldestMessageTime() == monotonic_clock::max_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1295 | --live_nodes_; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1296 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Node down!"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1297 | if (live_nodes_ == 0) { |
| 1298 | event_loop_factory_->Exit(); |
| 1299 | } |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 1300 | return; |
| 1301 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1302 | TimestampMerger::DeliveryTimestamp channel_timestamp; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1303 | int channel_index; |
| 1304 | FlatbufferVector<MessageHeader> channel_data = |
| 1305 | FlatbufferVector<MessageHeader>::Empty(); |
| 1306 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1307 | if (VLOG_IS_ON(1)) { |
| 1308 | LogFit("Offset was"); |
| 1309 | } |
| 1310 | |
| 1311 | bool update_time; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1312 | std::tie(channel_timestamp, channel_index, channel_data) = |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1313 | state->PopOldest(&update_time); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1314 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1315 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1316 | state->event_loop()->context().monotonic_event_time; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1317 | if (!FLAGS_skip_order_validation) { |
| 1318 | CHECK(monotonic_now == channel_timestamp.monotonic_event_time) |
| 1319 | << ": " << FlatbufferToJson(state->event_loop()->node()) << " Now " |
| 1320 | << monotonic_now << " trying to send " |
| 1321 | << channel_timestamp.monotonic_event_time << " failure " |
| 1322 | << state->DebugString(); |
| 1323 | } else if (monotonic_now != channel_timestamp.monotonic_event_time) { |
| 1324 | LOG(WARNING) << "Check failed: monotonic_now == " |
| 1325 | "channel_timestamp.monotonic_event_time) (" |
| 1326 | << monotonic_now << " vs. " |
| 1327 | << channel_timestamp.monotonic_event_time |
| 1328 | << "): " << FlatbufferToJson(state->event_loop()->node()) |
| 1329 | << " Now " << monotonic_now << " trying to send " |
| 1330 | << channel_timestamp.monotonic_event_time << " failure " |
| 1331 | << state->DebugString(); |
| 1332 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1333 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1334 | if (channel_timestamp.monotonic_event_time > |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1335 | state->monotonic_start_time() || |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1336 | event_loop_factory_ != nullptr) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1337 | if ((!ignore_missing_data_ && !FLAGS_skip_missing_forwarding_entries && |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1338 | !state->at_end()) || |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1339 | channel_data.message().data() != nullptr) { |
| 1340 | CHECK(channel_data.message().data() != nullptr) |
| 1341 | << ": Got a message without data. Forwarding entry which was " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1342 | "not matched? Use --skip_missing_forwarding_entries to " |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 1343 | "ignore this."; |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1344 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1345 | if (update_time) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1346 | // Confirm that the message was sent on the sending node before the |
| 1347 | // destination node (this node). As a proxy, do this by making sure |
| 1348 | // that time on the source node is past when the message was sent. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1349 | if (!FLAGS_skip_order_validation) { |
| 1350 | CHECK_LT(channel_timestamp.monotonic_remote_time, |
| 1351 | state->monotonic_remote_now(channel_index)) |
| 1352 | << state->event_loop()->node()->name()->string_view() << " to " |
| 1353 | << state->remote_node(channel_index)->name()->string_view() |
| 1354 | << " " << state->DebugString(); |
| 1355 | } else if (channel_timestamp.monotonic_remote_time >= |
| 1356 | state->monotonic_remote_now(channel_index)) { |
| 1357 | LOG(WARNING) |
| 1358 | << "Check failed: channel_timestamp.monotonic_remote_time < " |
| 1359 | "state->monotonic_remote_now(channel_index) (" |
| 1360 | << channel_timestamp.monotonic_remote_time << " vs. " |
| 1361 | << state->monotonic_remote_now(channel_index) << ") " |
| 1362 | << state->event_loop()->node()->name()->string_view() << " to " |
| 1363 | << state->remote_node(channel_index)->name()->string_view() |
| 1364 | << " currently " << channel_timestamp.monotonic_event_time |
| 1365 | << " (" |
| 1366 | << state->ToDistributedClock( |
| 1367 | channel_timestamp.monotonic_event_time) |
| 1368 | << ") remote event time " |
| 1369 | << channel_timestamp.monotonic_remote_time << " (" |
| 1370 | << state->RemoteToDistributedClock( |
| 1371 | channel_index, channel_timestamp.monotonic_remote_time) |
| 1372 | << ") " << state->DebugString(); |
| 1373 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1374 | |
| 1375 | if (FLAGS_timestamps_to_csv) { |
| 1376 | if (offset_fp_ == nullptr) { |
| 1377 | offset_fp_ = fopen("/tmp/offsets.csv", "w"); |
| 1378 | fprintf( |
| 1379 | offset_fp_, |
| 1380 | "# time_since_start, offset node 0, offset node 1, ...\n"); |
| 1381 | first_time_ = channel_timestamp.realtime_event_time; |
| 1382 | } |
| 1383 | |
| 1384 | fprintf(offset_fp_, "%.9f", |
| 1385 | std::chrono::duration_cast<std::chrono::duration<double>>( |
| 1386 | channel_timestamp.realtime_event_time - first_time_) |
| 1387 | .count()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1388 | for (int i = 1; i < time_offset_matrix_.rows(); ++i) { |
| 1389 | fprintf(offset_fp_, ", %.9f", |
| 1390 | time_offset_matrix_(i, 0) + |
| 1391 | time_slope_matrix_(i, 0) * |
| 1392 | chrono::duration<double>( |
| 1393 | event_loop_factory_->distributed_now() |
| 1394 | .time_since_epoch()) |
| 1395 | .count()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1396 | } |
| 1397 | fprintf(offset_fp_, "\n"); |
| 1398 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1399 | } |
| 1400 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1401 | // If we have access to the factory, use it to fix the realtime time. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1402 | state->SetRealtimeOffset(channel_timestamp.monotonic_event_time, |
| 1403 | channel_timestamp.realtime_event_time); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1404 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1405 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Sending " |
| 1406 | << channel_timestamp.monotonic_event_time; |
| 1407 | // TODO(austin): std::move channel_data in and make that efficient in |
| 1408 | // simulation. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1409 | state->Send(channel_index, channel_data.message().data()->Data(), |
| 1410 | channel_data.message().data()->size(), |
| 1411 | channel_timestamp.monotonic_remote_time, |
| 1412 | channel_timestamp.realtime_remote_time, |
| 1413 | channel_timestamp.remote_queue_index); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1414 | } else if (state->at_end() && !ignore_missing_data_) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1415 | // We are at the end of the log file and found missing data. Finish |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1416 | // reading the rest of the log file and call it quits. We don't want |
| 1417 | // to replay partial data. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1418 | while (state->OldestMessageTime() != monotonic_clock::max_time) { |
| 1419 | bool update_time_dummy; |
| 1420 | state->PopOldest(&update_time_dummy); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1421 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1422 | } else { |
| 1423 | CHECK(channel_data.message().data() == nullptr) << ": Nullptr"; |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1424 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1425 | } else { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1426 | LOG(WARNING) |
| 1427 | << "Not sending data from before the start of the log file. " |
| 1428 | << channel_timestamp.monotonic_event_time.time_since_epoch().count() |
| 1429 | << " start " << monotonic_start_time().time_since_epoch().count() |
| 1430 | << " " << FlatbufferToJson(channel_data); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1433 | const monotonic_clock::time_point next_time = state->OldestMessageTime(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1434 | if (next_time != monotonic_clock::max_time) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1435 | VLOG(1) << "Scheduling " << MaybeNodeName(state->event_loop()->node()) |
| 1436 | << "wakeup for " << next_time << "(" |
| 1437 | << state->ToDistributedClock(next_time) |
| 1438 | << " distributed), now is " << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1439 | state->Setup(next_time); |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 1440 | } else { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1441 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) |
| 1442 | << "No next message, scheduling shutdown"; |
| 1443 | // Set a timer up immediately after now to die. If we don't do this, |
| 1444 | // then the senders waiting on the message we just read will never get |
| 1445 | // called. |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 1446 | if (event_loop_factory_ != nullptr) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1447 | state->Setup(monotonic_now + event_loop_factory_->send_delay() + |
| 1448 | std::chrono::nanoseconds(1)); |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 1449 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1450 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1451 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1452 | // Once we make this call, the current time changes. So do everything |
| 1453 | // which involves time before changing it. That especially includes |
| 1454 | // sending the message. |
| 1455 | if (update_time) { |
| 1456 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) |
| 1457 | << "updating offsets"; |
| 1458 | |
| 1459 | std::vector<aos::monotonic_clock::time_point> before_times; |
| 1460 | before_times.resize(states_.size()); |
| 1461 | std::transform(states_.begin(), states_.end(), before_times.begin(), |
| 1462 | [](const std::unique_ptr<State> &state) { |
| 1463 | return state->monotonic_now(); |
| 1464 | }); |
| 1465 | |
| 1466 | for (size_t i = 0; i < states_.size(); ++i) { |
| 1467 | VLOG(1) << MaybeNodeName( |
| 1468 | states_[i]->event_loop()->node()) |
| 1469 | << "before " << states_[i]->monotonic_now(); |
| 1470 | } |
| 1471 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1472 | UpdateOffsets(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1473 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Now is now " |
| 1474 | << state->monotonic_now(); |
| 1475 | |
| 1476 | for (size_t i = 0; i < states_.size(); ++i) { |
| 1477 | VLOG(1) << MaybeNodeName( |
| 1478 | states_[i]->event_loop()->node()) |
| 1479 | << "after " << states_[i]->monotonic_now(); |
| 1480 | } |
| 1481 | |
| 1482 | // TODO(austin): We should be perfect. |
| 1483 | const std::chrono::nanoseconds kTolerance{3}; |
| 1484 | if (!FLAGS_skip_order_validation) { |
| 1485 | CHECK_GE(next_time, state->monotonic_now()) |
| 1486 | << ": Time skipped the next event."; |
| 1487 | |
| 1488 | for (size_t i = 0; i < states_.size(); ++i) { |
| 1489 | CHECK_GE(states_[i]->monotonic_now(), before_times[i] - kTolerance) |
| 1490 | << ": Time changed too much on node " |
| 1491 | << MaybeNodeName(states_[i]->event_loop()->node()); |
| 1492 | CHECK_LE(states_[i]->monotonic_now(), before_times[i] + kTolerance) |
| 1493 | << ": Time changed too much on node " |
| 1494 | << states_[i]->event_loop()->node()->name()->string_view(); |
| 1495 | } |
| 1496 | } else { |
| 1497 | if (next_time < state->monotonic_now()) { |
| 1498 | LOG(WARNING) << "Check failed: next_time >= " |
| 1499 | "state->monotonic_now() (" |
| 1500 | << next_time << " vs. " << state->monotonic_now() |
| 1501 | << "): Time skipped the next event."; |
| 1502 | } |
| 1503 | for (size_t i = 0; i < states_.size(); ++i) { |
| 1504 | if (states_[i]->monotonic_now() >= before_times[i] - kTolerance) { |
| 1505 | LOG(WARNING) << "Check failed: " |
| 1506 | "states_[i]->monotonic_now() " |
| 1507 | ">= before_times[i] - kTolerance (" |
| 1508 | << states_[i]->monotonic_now() << " vs. " |
| 1509 | << before_times[i] - kTolerance |
| 1510 | << ") : Time changed too much on node " |
| 1511 | << MaybeNodeName(states_[i]->event_loop()->node()); |
| 1512 | } |
| 1513 | if (states_[i]->monotonic_now() <= before_times[i] + kTolerance) { |
| 1514 | LOG(WARNING) << "Check failed: " |
| 1515 | "states_[i]->monotonic_now() " |
| 1516 | "<= before_times[i] + kTolerance (" |
| 1517 | << states_[i]->monotonic_now() << " vs. " |
| 1518 | << before_times[i] - kTolerance |
| 1519 | << ") : Time changed too much on node " |
| 1520 | << MaybeNodeName(states_[i]->event_loop()->node()); |
| 1521 | } |
| 1522 | } |
| 1523 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1524 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1525 | |
| 1526 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Done sending at " |
| 1527 | << state->event_loop()->context().monotonic_event_time << " now " |
| 1528 | << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1529 | })); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1530 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1531 | ++live_nodes_; |
| 1532 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1533 | if (state->OldestMessageTime() != monotonic_clock::max_time) { |
| 1534 | event_loop->OnRun([state]() { state->Setup(state->OldestMessageTime()); }); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | void LogReader::Deregister() { |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1539 | // Make sure that things get destroyed in the correct order, rather than |
| 1540 | // relying on getting the order correct in the class definition. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1541 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1542 | state->Deregister(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1543 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1544 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1545 | event_loop_factory_unique_ptr_.reset(); |
| 1546 | event_loop_factory_ = nullptr; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1549 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
| 1550 | std::string_view add_prefix) { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1551 | for (size_t ii = 0; ii < logged_configuration()->channels()->size(); ++ii) { |
| 1552 | const Channel *const channel = logged_configuration()->channels()->Get(ii); |
| 1553 | if (channel->name()->str() == name && |
| 1554 | channel->type()->string_view() == type) { |
| 1555 | CHECK_EQ(0u, remapped_channels_.count(ii)) |
| 1556 | << "Already remapped channel " |
| 1557 | << configuration::CleanedChannelToString(channel); |
| 1558 | remapped_channels_[ii] = std::string(add_prefix) + std::string(name); |
| 1559 | VLOG(1) << "Remapping channel " |
| 1560 | << configuration::CleanedChannelToString(channel) |
| 1561 | << " to have name " << remapped_channels_[ii]; |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 1562 | MakeRemappedConfig(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1563 | return; |
| 1564 | } |
| 1565 | } |
| 1566 | LOG(FATAL) << "Unabled to locate channel with name " << name << " and type " |
| 1567 | << type; |
| 1568 | } |
| 1569 | |
| 1570 | void LogReader::MakeRemappedConfig() { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1571 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1572 | if (state) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1573 | CHECK(!state->event_loop()) |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1574 | << ": Can't change the mapping after the events are scheduled."; |
| 1575 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1576 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1577 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1578 | // If no remapping occurred and we are using the original config, then there |
| 1579 | // is nothing interesting to do here. |
| 1580 | if (remapped_channels_.empty() && replay_configuration_ == nullptr) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1581 | remapped_configuration_ = logged_configuration(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1582 | return; |
| 1583 | } |
| 1584 | // Config to copy Channel definitions from. Use the specified |
| 1585 | // replay_configuration_ if it has been provided. |
| 1586 | const Configuration *const base_config = replay_configuration_ == nullptr |
| 1587 | ? logged_configuration() |
| 1588 | : replay_configuration_; |
| 1589 | // The remapped config will be identical to the base_config, except that it |
| 1590 | // will have a bunch of extra channels in the channel list, which are exact |
| 1591 | // copies of the remapped channels, but with different names. |
| 1592 | // Because the flatbuffers API is a pain to work with, this requires a bit of |
| 1593 | // a song-and-dance to get copied over. |
| 1594 | // The order of operations is to: |
| 1595 | // 1) Make a flatbuffer builder for a config that will just contain a list of |
| 1596 | // the new channels that we want to add. |
| 1597 | // 2) For each channel that we are remapping: |
| 1598 | // a) Make a buffer/builder and construct into it a Channel table that only |
| 1599 | // contains the new name for the channel. |
| 1600 | // b) Merge the new channel with just the name into the channel that we are |
| 1601 | // trying to copy, built in the flatbuffer builder made in 1. This gives |
| 1602 | // us the new channel definition that we need. |
| 1603 | // 3) Using this list of offsets, build the Configuration of just new |
| 1604 | // Channels. |
| 1605 | // 4) Merge the Configuration with the new Channels into the base_config. |
| 1606 | // 5) Call MergeConfiguration() on that result to give MergeConfiguration a |
| 1607 | // chance to sanitize the config. |
| 1608 | |
| 1609 | // This is the builder that we use for the config containing all the new |
| 1610 | // channels. |
| 1611 | flatbuffers::FlatBufferBuilder new_config_fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 1612 | new_config_fbb.ForceDefaults(true); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1613 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 1614 | for (auto &pair : remapped_channels_) { |
| 1615 | // This is the builder that we use for creating the Channel with just the |
| 1616 | // new name. |
| 1617 | flatbuffers::FlatBufferBuilder new_name_fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 1618 | new_name_fbb.ForceDefaults(true); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1619 | const flatbuffers::Offset<flatbuffers::String> name_offset = |
| 1620 | new_name_fbb.CreateString(pair.second); |
| 1621 | ChannelBuilder new_name_builder(new_name_fbb); |
| 1622 | new_name_builder.add_name(name_offset); |
| 1623 | new_name_fbb.Finish(new_name_builder.Finish()); |
| 1624 | const FlatbufferDetachedBuffer<Channel> new_name = new_name_fbb.Release(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1625 | // Retrieve the channel that we want to copy, confirming that it is |
| 1626 | // actually present in base_config. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1627 | const Channel *const base_channel = CHECK_NOTNULL(configuration::GetChannel( |
| 1628 | base_config, logged_configuration()->channels()->Get(pair.first), "", |
| 1629 | nullptr)); |
| 1630 | // Actually create the new channel and put it into the vector of Offsets |
| 1631 | // that we will use to create the new Configuration. |
| 1632 | channel_offsets.emplace_back(MergeFlatBuffers<Channel>( |
| 1633 | reinterpret_cast<const flatbuffers::Table *>(base_channel), |
| 1634 | reinterpret_cast<const flatbuffers::Table *>(&new_name.message()), |
| 1635 | &new_config_fbb)); |
| 1636 | } |
| 1637 | // Create the Configuration containing the new channels that we want to add. |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 1638 | const auto new_name_vector_offsets = |
| 1639 | new_config_fbb.CreateVector(channel_offsets); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1640 | ConfigurationBuilder new_config_builder(new_config_fbb); |
| 1641 | new_config_builder.add_channels(new_name_vector_offsets); |
| 1642 | new_config_fbb.Finish(new_config_builder.Finish()); |
| 1643 | const FlatbufferDetachedBuffer<Configuration> new_name_config = |
| 1644 | new_config_fbb.Release(); |
| 1645 | // Merge the new channels configuration into the base_config, giving us the |
| 1646 | // remapped configuration. |
| 1647 | remapped_configuration_buffer_ = |
| 1648 | std::make_unique<FlatbufferDetachedBuffer<Configuration>>( |
| 1649 | MergeFlatBuffers<Configuration>(base_config, |
| 1650 | &new_name_config.message())); |
| 1651 | // Call MergeConfiguration to deal with sanitizing the config. |
| 1652 | remapped_configuration_buffer_ = |
| 1653 | std::make_unique<FlatbufferDetachedBuffer<Configuration>>( |
| 1654 | configuration::MergeConfiguration(*remapped_configuration_buffer_)); |
| 1655 | |
| 1656 | remapped_configuration_ = &remapped_configuration_buffer_->message(); |
| 1657 | } |
| 1658 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1659 | const Channel *LogReader::RemapChannel(const EventLoop *event_loop, |
| 1660 | const Channel *channel) { |
| 1661 | std::string_view channel_name = channel->name()->string_view(); |
| 1662 | std::string_view channel_type = channel->type()->string_view(); |
| 1663 | const int channel_index = |
| 1664 | configuration::ChannelIndex(logged_configuration(), channel); |
| 1665 | // If the channel is remapped, find the correct channel name to use. |
| 1666 | if (remapped_channels_.count(channel_index) > 0) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1667 | VLOG(3) << "Got remapped channel on " |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1668 | << configuration::CleanedChannelToString(channel); |
| 1669 | channel_name = remapped_channels_[channel_index]; |
| 1670 | } |
| 1671 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1672 | VLOG(2) << "Going to remap channel " << channel_name << " " << channel_type; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1673 | const Channel *remapped_channel = configuration::GetChannel( |
| 1674 | event_loop->configuration(), channel_name, channel_type, |
| 1675 | event_loop->name(), event_loop->node()); |
| 1676 | |
| 1677 | CHECK(remapped_channel != nullptr) |
| 1678 | << ": Unable to send {\"name\": \"" << channel_name << "\", \"type\": \"" |
| 1679 | << channel_type << "\"} because it is not in the provided configuration."; |
| 1680 | |
| 1681 | return remapped_channel; |
| 1682 | } |
| 1683 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1684 | LogReader::State::State(std::unique_ptr<ChannelMerger> channel_merger) |
| 1685 | : channel_merger_(std::move(channel_merger)) {} |
| 1686 | |
| 1687 | EventLoop *LogReader::State::SetNodeEventLoopFactory( |
| 1688 | NodeEventLoopFactory *node_event_loop_factory) { |
| 1689 | node_event_loop_factory_ = node_event_loop_factory; |
| 1690 | event_loop_unique_ptr_ = |
| 1691 | node_event_loop_factory_->MakeEventLoop("log_reader"); |
| 1692 | return event_loop_unique_ptr_.get(); |
| 1693 | } |
| 1694 | |
| 1695 | void LogReader::State::SetChannelCount(size_t count) { |
| 1696 | channels_.resize(count); |
| 1697 | filters_.resize(count); |
| 1698 | channel_target_event_loop_factory_.resize(count); |
| 1699 | } |
| 1700 | |
| 1701 | void LogReader::State::SetChannel( |
| 1702 | size_t channel, std::unique_ptr<RawSender> sender, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1703 | message_bridge::NoncausalOffsetEstimator *filter, |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1704 | NodeEventLoopFactory *channel_target_event_loop_factory) { |
| 1705 | channels_[channel] = std::move(sender); |
| 1706 | filters_[channel] = filter; |
| 1707 | channel_target_event_loop_factory_[channel] = |
| 1708 | channel_target_event_loop_factory; |
| 1709 | } |
| 1710 | |
| 1711 | std::tuple<TimestampMerger::DeliveryTimestamp, int, |
| 1712 | FlatbufferVector<MessageHeader>> |
| 1713 | LogReader::State::PopOldest(bool *update_time) { |
| 1714 | CHECK_GT(sorted_messages_.size(), 0u); |
| 1715 | |
| 1716 | std::tuple<TimestampMerger::DeliveryTimestamp, int, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1717 | FlatbufferVector<MessageHeader>, |
| 1718 | message_bridge::NoncausalOffsetEstimator *> |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1719 | result = std::move(sorted_messages_.front()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1720 | VLOG(2) << MaybeNodeName(event_loop_->node()) << "PopOldest Popping " |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1721 | << std::get<0>(result).monotonic_event_time; |
| 1722 | sorted_messages_.pop_front(); |
| 1723 | SeedSortedMessages(); |
| 1724 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1725 | if (std::get<3>(result) != nullptr) { |
| 1726 | *update_time = std::get<3>(result)->Pop( |
| 1727 | event_loop_->node(), std::get<0>(result).monotonic_event_time); |
| 1728 | } else { |
| 1729 | *update_time = false; |
| 1730 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1731 | return std::make_tuple(std::get<0>(result), std::get<1>(result), |
| 1732 | std::move(std::get<2>(result))); |
| 1733 | } |
| 1734 | |
| 1735 | monotonic_clock::time_point LogReader::State::OldestMessageTime() const { |
| 1736 | if (sorted_messages_.size() > 0) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1737 | VLOG(2) << MaybeNodeName(event_loop_->node()) << "oldest message at " |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1738 | << std::get<0>(sorted_messages_.front()).monotonic_event_time; |
| 1739 | return std::get<0>(sorted_messages_.front()).monotonic_event_time; |
| 1740 | } |
| 1741 | |
| 1742 | return channel_merger_->OldestMessageTime(); |
| 1743 | } |
| 1744 | |
| 1745 | void LogReader::State::SeedSortedMessages() { |
| 1746 | const aos::monotonic_clock::time_point end_queue_time = |
| 1747 | (sorted_messages_.size() > 0 |
| 1748 | ? std::get<0>(sorted_messages_.front()).monotonic_event_time |
| 1749 | : channel_merger_->monotonic_start_time()) + |
| 1750 | std::chrono::seconds(2); |
| 1751 | |
| 1752 | while (true) { |
| 1753 | if (channel_merger_->OldestMessageTime() == monotonic_clock::max_time) { |
| 1754 | return; |
| 1755 | } |
| 1756 | if (sorted_messages_.size() > 0) { |
| 1757 | // Stop placing sorted messages on the list once we have 2 seconds |
| 1758 | // queued up (but queue at least until the log starts. |
| 1759 | if (end_queue_time < |
| 1760 | std::get<0>(sorted_messages_.back()).monotonic_event_time) { |
| 1761 | return; |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | TimestampMerger::DeliveryTimestamp channel_timestamp; |
| 1766 | int channel_index; |
| 1767 | FlatbufferVector<MessageHeader> channel_data = |
| 1768 | FlatbufferVector<MessageHeader>::Empty(); |
| 1769 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1770 | message_bridge::NoncausalOffsetEstimator *filter = nullptr; |
| 1771 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1772 | std::tie(channel_timestamp, channel_index, channel_data) = |
| 1773 | channel_merger_->PopOldest(); |
| 1774 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1775 | // Skip any messages without forwarding information. |
| 1776 | if (channel_timestamp.monotonic_remote_time != monotonic_clock::min_time) { |
| 1777 | // Got a forwarding timestamp! |
| 1778 | filter = filters_[channel_index]; |
| 1779 | |
| 1780 | CHECK(filter != nullptr); |
| 1781 | |
| 1782 | // Call the correct method depending on if we are the forward or |
| 1783 | // reverse direction here. |
| 1784 | filter->Sample(event_loop_->node(), |
| 1785 | channel_timestamp.monotonic_event_time, |
| 1786 | channel_timestamp.monotonic_remote_time); |
| 1787 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1788 | sorted_messages_.emplace_back(channel_timestamp, channel_index, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1789 | std::move(channel_data), filter); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | void LogReader::State::Deregister() { |
| 1794 | for (size_t i = 0; i < channels_.size(); ++i) { |
| 1795 | channels_[i].reset(); |
| 1796 | } |
| 1797 | event_loop_unique_ptr_.reset(); |
| 1798 | event_loop_ = nullptr; |
| 1799 | timer_handler_ = nullptr; |
| 1800 | node_event_loop_factory_ = nullptr; |
| 1801 | } |
| 1802 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1803 | } // namespace logger |
| 1804 | } // namespace aos |