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" |
Austin Schuh | f6f9bf3 | 2020-10-11 14:37:43 -0700 | [diff] [blame] | 14 | #include "aos/events/logging/logfile_sorting.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 15 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 16 | #include "aos/events/logging/uuid.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 17 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 18 | #include "aos/network/remote_message_generated.h" |
| 19 | #include "aos/network/remote_message_schema.h" |
Austin Schuh | 288479d | 2019-12-18 19:47:52 -0800 | [diff] [blame] | 20 | #include "aos/network/team_number.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 21 | #include "aos/time/time.h" |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 22 | #include "aos/util/file.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 23 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 24 | #include "third_party/gmp/gmpxx.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 25 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 26 | DEFINE_bool(skip_missing_forwarding_entries, false, |
| 27 | "If true, drop any forwarding entries with missing data. If " |
| 28 | "false, CHECK."); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 29 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 30 | DEFINE_bool(timestamps_to_csv, false, |
| 31 | "If true, write all the time synchronization information to a set " |
| 32 | "of CSV files in /tmp/. This should only be needed when debugging " |
| 33 | "time synchronization."); |
| 34 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 35 | DEFINE_bool(skip_order_validation, false, |
| 36 | "If true, ignore any out of orderness in replay"); |
| 37 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 38 | namespace aos { |
| 39 | namespace logger { |
Austin Schuh | 0afc4d1 | 2020-10-19 11:42:04 -0700 | [diff] [blame] | 40 | namespace { |
| 41 | // Helper to safely read a header, or CHECK. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 42 | SizePrefixedFlatbufferVector<LogFileHeader> MaybeReadHeaderOrDie( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 43 | const std::vector<LogFile> &log_files) { |
| 44 | CHECK_GE(log_files.size(), 1u) << ": Empty filenames list"; |
| 45 | CHECK_GE(log_files[0].parts.size(), 1u) << ": Empty filenames list"; |
| 46 | CHECK_GE(log_files[0].parts[0].parts.size(), 1u) << ": Empty filenames list"; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 47 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> result = |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 48 | ReadHeader(log_files[0].parts[0].parts[0]); |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 49 | CHECK(result); |
| 50 | return result.value(); |
Austin Schuh | 0afc4d1 | 2020-10-19 11:42:04 -0700 | [diff] [blame] | 51 | } |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 52 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 53 | std::string LogFileVectorToString(std::vector<LogFile> log_files) { |
| 54 | std::stringstream ss; |
| 55 | for (const auto f : log_files) { |
| 56 | ss << f << "\n"; |
| 57 | } |
| 58 | return ss.str(); |
| 59 | } |
| 60 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 61 | // Copies the channel, removing the schema as we go. If new_name is provided, |
| 62 | // it is used instead of the name inside the channel. If new_type is provided, |
| 63 | // it is used instead of the type in the channel. |
| 64 | flatbuffers::Offset<Channel> CopyChannel(const Channel *c, |
| 65 | std::string_view new_name, |
| 66 | std::string_view new_type, |
| 67 | flatbuffers::FlatBufferBuilder *fbb) { |
| 68 | flatbuffers::Offset<flatbuffers::String> name_offset = |
| 69 | fbb->CreateSharedString(new_name.empty() ? c->name()->string_view() |
| 70 | : new_name); |
| 71 | flatbuffers::Offset<flatbuffers::String> type_offset = |
| 72 | fbb->CreateSharedString(new_type.empty() ? c->type()->str() : new_type); |
| 73 | flatbuffers::Offset<flatbuffers::String> source_node_offset = |
| 74 | c->has_source_node() ? fbb->CreateSharedString(c->source_node()->str()) |
| 75 | : 0; |
| 76 | |
| 77 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>> |
| 78 | destination_nodes_offset = |
| 79 | aos::RecursiveCopyVectorTable(c->destination_nodes(), fbb); |
| 80 | |
| 81 | flatbuffers::Offset< |
| 82 | flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> |
| 83 | logger_nodes_offset = aos::CopyVectorSharedString(c->logger_nodes(), fbb); |
| 84 | |
| 85 | Channel::Builder channel_builder(*fbb); |
| 86 | channel_builder.add_name(name_offset); |
| 87 | channel_builder.add_type(type_offset); |
| 88 | if (c->has_frequency()) { |
| 89 | channel_builder.add_frequency(c->frequency()); |
| 90 | } |
| 91 | if (c->has_max_size()) { |
| 92 | channel_builder.add_max_size(c->max_size()); |
| 93 | } |
| 94 | if (c->has_num_senders()) { |
| 95 | channel_builder.add_num_senders(c->num_senders()); |
| 96 | } |
| 97 | if (c->has_num_watchers()) { |
| 98 | channel_builder.add_num_watchers(c->num_watchers()); |
| 99 | } |
| 100 | if (!source_node_offset.IsNull()) { |
| 101 | channel_builder.add_source_node(source_node_offset); |
| 102 | } |
| 103 | if (!destination_nodes_offset.IsNull()) { |
| 104 | channel_builder.add_destination_nodes(destination_nodes_offset); |
| 105 | } |
| 106 | if (c->has_logger()) { |
| 107 | channel_builder.add_logger(c->logger()); |
| 108 | } |
| 109 | if (!logger_nodes_offset.IsNull()) { |
| 110 | channel_builder.add_logger_nodes(logger_nodes_offset); |
| 111 | } |
| 112 | if (c->has_read_method()) { |
| 113 | channel_builder.add_read_method(c->read_method()); |
| 114 | } |
| 115 | if (c->has_num_readers()) { |
| 116 | channel_builder.add_num_readers(c->num_readers()); |
| 117 | } |
| 118 | return channel_builder.Finish(); |
| 119 | } |
| 120 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 121 | namespace chrono = std::chrono; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 122 | using message_bridge::RemoteMessage; |
Austin Schuh | 0afc4d1 | 2020-10-19 11:42:04 -0700 | [diff] [blame] | 123 | } // namespace |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 124 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 125 | Logger::Logger(EventLoop *event_loop, const Configuration *configuration, |
| 126 | std::function<bool(const Channel *)> should_log) |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 127 | : event_loop_(event_loop), |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 128 | configuration_(configuration), |
| 129 | name_(network::GetHostname()), |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 130 | timer_handler_(event_loop_->AddTimer( |
| 131 | [this]() { DoLogData(event_loop_->monotonic_now()); })), |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 132 | server_statistics_fetcher_( |
| 133 | configuration::MultiNode(event_loop_->configuration()) |
| 134 | ? event_loop_->MakeFetcher<message_bridge::ServerStatistics>( |
| 135 | "/aos") |
| 136 | : aos::Fetcher<message_bridge::ServerStatistics>()) { |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 137 | VLOG(1) << "Creating logger for " << FlatbufferToJson(event_loop_->node()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 138 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 139 | // Find all the nodes which are logging timestamps on our node. This may |
| 140 | // over-estimate if should_log is specified. |
| 141 | std::vector<const Node *> timestamp_logger_nodes = |
| 142 | configuration::TimestampNodes(configuration_, event_loop_->node()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 143 | |
| 144 | std::map<const Channel *, const Node *> timestamp_logger_channels; |
| 145 | |
| 146 | // Now that we have all the nodes accumulated, make remote timestamp loggers |
| 147 | // for them. |
| 148 | for (const Node *node : timestamp_logger_nodes) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 149 | // Note: since we are doing a find using the event loop channel, we need to |
| 150 | // make sure this channel pointer is part of the event loop configuration, |
| 151 | // not configuration_. This only matters when configuration_ != |
| 152 | // event_loop->configuration(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 153 | const Channel *channel = configuration::GetChannel( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 154 | event_loop->configuration(), |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 155 | absl::StrCat("/aos/remote_timestamps/", node->name()->string_view()), |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 156 | RemoteMessage::GetFullyQualifiedName(), event_loop_->name(), |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 157 | event_loop_->node()); |
| 158 | |
| 159 | CHECK(channel != nullptr) |
| 160 | << ": Remote timestamps are logged on " |
| 161 | << event_loop_->node()->name()->string_view() |
| 162 | << " but can't find channel /aos/remote_timestamps/" |
| 163 | << node->name()->string_view(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 164 | if (!should_log(channel)) { |
| 165 | continue; |
| 166 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 167 | timestamp_logger_channels.insert(std::make_pair(channel, node)); |
| 168 | } |
| 169 | |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 170 | const size_t our_node_index = |
| 171 | configuration::GetNodeIndex(configuration_, event_loop_->node()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 172 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 173 | for (size_t channel_index = 0; |
| 174 | channel_index < configuration_->channels()->size(); ++channel_index) { |
| 175 | const Channel *const config_channel = |
| 176 | configuration_->channels()->Get(channel_index); |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 177 | // The MakeRawFetcher method needs a channel which is in the event loop |
| 178 | // configuration() object, not the configuration_ object. Go look that up |
| 179 | // from the config. |
| 180 | const Channel *channel = aos::configuration::GetChannel( |
| 181 | event_loop_->configuration(), config_channel->name()->string_view(), |
| 182 | config_channel->type()->string_view(), "", event_loop_->node()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 183 | CHECK(channel != nullptr) |
| 184 | << ": Failed to look up channel " |
| 185 | << aos::configuration::CleanedChannelToString(config_channel); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 186 | if (!should_log(channel)) { |
| 187 | continue; |
| 188 | } |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 189 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 190 | FetcherStruct fs; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 191 | fs.channel_index = channel_index; |
| 192 | fs.channel = channel; |
| 193 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 194 | const bool is_local = |
| 195 | configuration::ChannelIsSendableOnNode(channel, event_loop_->node()); |
| 196 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 197 | const bool is_readable = |
| 198 | configuration::ChannelIsReadableOnNode(channel, event_loop_->node()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 199 | const bool is_logged = configuration::ChannelMessageIsLoggedOnNode( |
| 200 | channel, event_loop_->node()); |
| 201 | const bool log_message = is_logged && is_readable; |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 202 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 203 | bool log_delivery_times = false; |
| 204 | if (event_loop_->node() != nullptr) { |
| 205 | log_delivery_times = configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 206 | channel, event_loop_->node(), event_loop_->node()); |
| 207 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 208 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 209 | // Now, detect a RemoteMessage timestamp logger where we should just log the |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 210 | // contents to a file directly. |
| 211 | const bool log_contents = timestamp_logger_channels.find(channel) != |
| 212 | timestamp_logger_channels.end(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 213 | |
| 214 | if (log_message || log_delivery_times || log_contents) { |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 215 | fs.fetcher = event_loop->MakeRawFetcher(channel); |
| 216 | VLOG(1) << "Logging channel " |
| 217 | << configuration::CleanedChannelToString(channel); |
| 218 | |
| 219 | if (log_delivery_times) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 220 | VLOG(1) << " Delivery times"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 221 | fs.wants_timestamp_writer = true; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 222 | fs.timestamp_node_index = our_node_index; |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 223 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 224 | if (log_message) { |
| 225 | VLOG(1) << " Data"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 226 | fs.wants_writer = true; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 227 | if (!is_local) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 228 | const Node *source_node = configuration::GetNode( |
| 229 | configuration_, channel->source_node()->string_view()); |
| 230 | fs.data_node_index = |
| 231 | configuration::GetNodeIndex(configuration_, source_node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 232 | fs.log_type = LogType::kLogRemoteMessage; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 233 | } else { |
| 234 | fs.data_node_index = our_node_index; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 237 | if (log_contents) { |
| 238 | VLOG(1) << "Timestamp logger channel " |
| 239 | << configuration::CleanedChannelToString(channel); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 240 | fs.timestamp_node = timestamp_logger_channels.find(channel)->second; |
| 241 | fs.wants_contents_writer = true; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 242 | fs.contents_node_index = |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 243 | configuration::GetNodeIndex(configuration_, fs.timestamp_node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 244 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 245 | fetchers_.emplace_back(std::move(fs)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 246 | } |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 247 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 248 | |
| 249 | // When we are logging remote timestamps, we need to be able to translate from |
| 250 | // the channel index that the event loop uses to the channel index in the |
| 251 | // config in the log file. |
| 252 | event_loop_to_logged_channel_index_.resize( |
| 253 | event_loop->configuration()->channels()->size(), -1); |
| 254 | for (size_t event_loop_channel_index = 0; |
| 255 | event_loop_channel_index < |
| 256 | event_loop->configuration()->channels()->size(); |
| 257 | ++event_loop_channel_index) { |
| 258 | const Channel *event_loop_channel = |
| 259 | event_loop->configuration()->channels()->Get(event_loop_channel_index); |
| 260 | |
| 261 | const Channel *logged_channel = aos::configuration::GetChannel( |
| 262 | configuration_, event_loop_channel->name()->string_view(), |
| 263 | event_loop_channel->type()->string_view(), "", |
| 264 | configuration::GetNode(configuration_, event_loop_->node())); |
| 265 | |
| 266 | if (logged_channel != nullptr) { |
| 267 | event_loop_to_logged_channel_index_[event_loop_channel_index] = |
| 268 | configuration::ChannelIndex(configuration_, logged_channel); |
| 269 | } |
| 270 | } |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | Logger::~Logger() { |
| 274 | if (log_namer_) { |
| 275 | // If we are replaying a log file, or in simulation, we want to force the |
| 276 | // last bit of data to be logged. The easiest way to deal with this is to |
| 277 | // poll everything as we go to destroy the class, ie, shut down the logger, |
| 278 | // and write it to disk. |
| 279 | StopLogging(event_loop_->monotonic_now()); |
| 280 | } |
| 281 | } |
| 282 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 283 | void Logger::StartLogging(std::unique_ptr<LogNamer> log_namer, |
| 284 | std::string_view log_start_uuid) { |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 285 | CHECK(!log_namer_) << ": Already logging"; |
| 286 | log_namer_ = std::move(log_namer); |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 287 | log_event_uuid_ = UUID::Random(); |
| 288 | log_start_uuid_ = log_start_uuid; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 289 | VLOG(1) << "Starting logger for " << FlatbufferToJson(event_loop_->node()); |
| 290 | |
| 291 | // We want to do as much work as possible before the initial Fetch. Time |
| 292 | // between that and actually starting to log opens up the possibility of |
| 293 | // falling off the end of the queue during that time. |
| 294 | |
| 295 | for (FetcherStruct &f : fetchers_) { |
| 296 | if (f.wants_writer) { |
| 297 | f.writer = log_namer_->MakeWriter(f.channel); |
| 298 | } |
| 299 | if (f.wants_timestamp_writer) { |
| 300 | f.timestamp_writer = log_namer_->MakeTimestampWriter(f.channel); |
| 301 | } |
| 302 | if (f.wants_contents_writer) { |
| 303 | f.contents_writer = log_namer_->MakeForwardedTimestampWriter( |
| 304 | f.channel, CHECK_NOTNULL(f.timestamp_node)); |
| 305 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 308 | CHECK(node_state_.empty()); |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 309 | node_state_.resize(configuration::MultiNode(configuration_) |
| 310 | ? configuration_->nodes()->size() |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 311 | : 1u); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 312 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 313 | for (const Node *node : log_namer_->nodes()) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 314 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 315 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 316 | node_state_[node_index].log_file_header = MakeHeader(node); |
| 317 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 318 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 319 | // Grab data from each channel right before we declare the log file started |
| 320 | // so we can capture the latest message on each channel. This lets us have |
| 321 | // non periodic messages with configuration that now get logged. |
| 322 | for (FetcherStruct &f : fetchers_) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 323 | const auto start = event_loop_->monotonic_now(); |
| 324 | const bool got_new = f.fetcher->Fetch(); |
| 325 | const auto end = event_loop_->monotonic_now(); |
| 326 | RecordFetchResult(start, end, got_new, &f); |
| 327 | |
| 328 | // If there is a message, we want to write it. |
| 329 | f.written = f.fetcher->context().data == nullptr; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // Clear out any old timestamps in case we are re-starting logging. |
| 333 | for (size_t i = 0; i < node_state_.size(); ++i) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 334 | SetStartTime(i, monotonic_clock::min_time, realtime_clock::min_time, |
| 335 | monotonic_clock::min_time, realtime_clock::min_time); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | WriteHeader(); |
| 339 | |
| 340 | LOG(INFO) << "Logging node as " << FlatbufferToJson(event_loop_->node()) |
| 341 | << " start_time " << last_synchronized_time_; |
| 342 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 343 | // Force logging up until the start of the log file now, so the messages at |
| 344 | // the start are always ordered before the rest of the messages. |
| 345 | // Note: this ship may have already sailed, but we don't have to make it |
| 346 | // worse. |
| 347 | // TODO(austin): Test... |
| 348 | LogUntil(last_synchronized_time_); |
| 349 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 350 | timer_handler_->Setup(event_loop_->monotonic_now() + polling_period_, |
| 351 | polling_period_); |
| 352 | } |
| 353 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 354 | std::unique_ptr<LogNamer> Logger::StopLogging( |
| 355 | aos::monotonic_clock::time_point end_time) { |
| 356 | CHECK(log_namer_) << ": Not logging right now"; |
| 357 | |
| 358 | if (end_time != aos::monotonic_clock::min_time) { |
| 359 | LogUntil(end_time); |
| 360 | } |
| 361 | timer_handler_->Disable(); |
| 362 | |
| 363 | for (FetcherStruct &f : fetchers_) { |
| 364 | f.writer = nullptr; |
| 365 | f.timestamp_writer = nullptr; |
| 366 | f.contents_writer = nullptr; |
| 367 | } |
| 368 | node_state_.clear(); |
| 369 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 370 | log_event_uuid_ = UUID::Zero(); |
| 371 | log_start_uuid_ = std::string(); |
| 372 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 373 | return std::move(log_namer_); |
| 374 | } |
| 375 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 376 | void Logger::WriteHeader() { |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 377 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 378 | server_statistics_fetcher_.Fetch(); |
| 379 | } |
| 380 | |
| 381 | aos::monotonic_clock::time_point monotonic_start_time = |
| 382 | event_loop_->monotonic_now(); |
| 383 | aos::realtime_clock::time_point realtime_start_time = |
| 384 | event_loop_->realtime_now(); |
| 385 | |
| 386 | // We need to pick a point in time to declare the log file "started". This |
| 387 | // starts here. It needs to be after everything is fetched so that the |
| 388 | // fetchers are all pointed at the most recent message before the start |
| 389 | // time. |
| 390 | last_synchronized_time_ = monotonic_start_time; |
| 391 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 392 | for (const Node *node : log_namer_->nodes()) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 393 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 394 | MaybeUpdateTimestamp(node, node_index, monotonic_start_time, |
| 395 | realtime_start_time); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 396 | MaybeWriteHeader(node_index, node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 397 | } |
| 398 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 399 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 400 | void Logger::MaybeWriteHeader(int node_index) { |
| 401 | if (configuration::MultiNode(configuration_)) { |
| 402 | return MaybeWriteHeader(node_index, |
| 403 | configuration_->nodes()->Get(node_index)); |
| 404 | } else { |
| 405 | return MaybeWriteHeader(node_index, nullptr); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | void Logger::MaybeWriteHeader(int node_index, const Node *node) { |
| 410 | // This function is responsible for writing the header when the header both |
| 411 | // has valid data, and when it needs to be written. |
| 412 | if (node_state_[node_index].header_written && |
| 413 | node_state_[node_index].header_valid) { |
| 414 | // The header has been written and is valid, nothing to do. |
| 415 | return; |
| 416 | } |
| 417 | if (!node_state_[node_index].has_source_node_boot_uuid) { |
| 418 | // Can't write a header if we don't have the boot UUID. |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | // WriteHeader writes the first header in a log file. We want to do this only |
| 423 | // once. |
| 424 | // |
| 425 | // Rotate rewrites the same header with a new part ID, but keeps the same part |
| 426 | // UUID. We don't want that when things reboot, because that implies that |
| 427 | // parts go together across a reboot. |
| 428 | // |
| 429 | // Reboot resets the parts UUID. So, once we've written a header the first |
| 430 | // time, we want to use Reboot to rotate the log and reset the parts UUID. |
| 431 | // |
| 432 | // header_valid is cleared whenever the remote reboots. |
| 433 | if (node_state_[node_index].header_written) { |
| 434 | log_namer_->Reboot(node, &node_state_[node_index].log_file_header); |
| 435 | } else { |
| 436 | log_namer_->WriteHeader(&node_state_[node_index].log_file_header, node); |
| 437 | |
| 438 | node_state_[node_index].header_written = true; |
| 439 | } |
| 440 | node_state_[node_index].header_valid = true; |
| 441 | } |
| 442 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 443 | void Logger::WriteMissingTimestamps() { |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 444 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 445 | server_statistics_fetcher_.Fetch(); |
| 446 | } else { |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | if (server_statistics_fetcher_.get() == nullptr) { |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | for (const Node *node : log_namer_->nodes()) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 455 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 456 | if (MaybeUpdateTimestamp( |
| 457 | node, node_index, |
| 458 | server_statistics_fetcher_.context().monotonic_event_time, |
| 459 | server_statistics_fetcher_.context().realtime_event_time)) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 460 | CHECK(node_state_[node_index].header_written); |
| 461 | CHECK(node_state_[node_index].header_valid); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 462 | log_namer_->Rotate(node, &node_state_[node_index].log_file_header); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 463 | } else { |
| 464 | MaybeWriteHeader(node_index, node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 469 | void Logger::SetStartTime( |
| 470 | size_t node_index, aos::monotonic_clock::time_point monotonic_start_time, |
| 471 | aos::realtime_clock::time_point realtime_start_time, |
| 472 | aos::monotonic_clock::time_point logger_monotonic_start_time, |
| 473 | aos::realtime_clock::time_point logger_realtime_start_time) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 474 | node_state_[node_index].monotonic_start_time = monotonic_start_time; |
| 475 | node_state_[node_index].realtime_start_time = realtime_start_time; |
| 476 | node_state_[node_index] |
| 477 | .log_file_header.mutable_message() |
| 478 | ->mutate_monotonic_start_time( |
| 479 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 480 | monotonic_start_time.time_since_epoch()) |
| 481 | .count()); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 482 | |
| 483 | // Add logger start times if they are available in the log file header. |
| 484 | if (node_state_[node_index] |
| 485 | .log_file_header.mutable_message() |
| 486 | ->has_logger_monotonic_start_time()) { |
| 487 | node_state_[node_index] |
| 488 | .log_file_header.mutable_message() |
| 489 | ->mutate_logger_monotonic_start_time( |
| 490 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 491 | logger_monotonic_start_time.time_since_epoch()) |
| 492 | .count()); |
| 493 | } |
| 494 | |
| 495 | if (node_state_[node_index] |
| 496 | .log_file_header.mutable_message() |
| 497 | ->has_logger_realtime_start_time()) { |
| 498 | node_state_[node_index] |
| 499 | .log_file_header.mutable_message() |
| 500 | ->mutate_logger_realtime_start_time( |
| 501 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 502 | logger_realtime_start_time.time_since_epoch()) |
| 503 | .count()); |
| 504 | } |
| 505 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 506 | if (node_state_[node_index] |
| 507 | .log_file_header.mutable_message() |
| 508 | ->has_realtime_start_time()) { |
| 509 | node_state_[node_index] |
| 510 | .log_file_header.mutable_message() |
| 511 | ->mutate_realtime_start_time( |
| 512 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 513 | realtime_start_time.time_since_epoch()) |
| 514 | .count()); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | bool Logger::MaybeUpdateTimestamp( |
| 519 | const Node *node, int node_index, |
| 520 | aos::monotonic_clock::time_point monotonic_start_time, |
| 521 | aos::realtime_clock::time_point realtime_start_time) { |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 522 | // Bail early if the start times are already set. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 523 | if (node_state_[node_index].monotonic_start_time != |
| 524 | monotonic_clock::min_time) { |
| 525 | return false; |
| 526 | } |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 527 | if (event_loop_->node() == node || |
| 528 | !configuration::MultiNode(configuration_)) { |
| 529 | // There are no offsets to compute for ourself, so always succeed. |
| 530 | SetStartTime(node_index, monotonic_start_time, realtime_start_time, |
| 531 | monotonic_start_time, realtime_start_time); |
| 532 | node_state_[node_index].SetBootUUID(event_loop_->boot_uuid().string_view()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 533 | return true; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 534 | } else if (server_statistics_fetcher_.get() != nullptr) { |
| 535 | // We must be a remote node now. Look for the connection and see if it is |
| 536 | // connected. |
| 537 | |
| 538 | for (const message_bridge::ServerConnection *connection : |
| 539 | *server_statistics_fetcher_->connections()) { |
| 540 | if (connection->node()->name()->string_view() != |
| 541 | node->name()->string_view()) { |
| 542 | continue; |
| 543 | } |
| 544 | |
| 545 | if (connection->state() != message_bridge::State::CONNECTED) { |
| 546 | VLOG(1) << node->name()->string_view() |
| 547 | << " is not connected, can't start it yet."; |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | // Update the boot UUID as soon as we know we are connected. |
| 552 | if (!connection->has_boot_uuid()) { |
| 553 | VLOG(1) << "Missing boot_uuid for node " << aos::FlatbufferToJson(node); |
| 554 | break; |
| 555 | } |
| 556 | |
| 557 | if (!node_state_[node_index].has_source_node_boot_uuid || |
| 558 | node_state_[node_index].source_node_boot_uuid != |
| 559 | connection->boot_uuid()->string_view()) { |
| 560 | node_state_[node_index].SetBootUUID( |
| 561 | connection->boot_uuid()->string_view()); |
| 562 | } |
| 563 | |
| 564 | if (!connection->has_monotonic_offset()) { |
| 565 | VLOG(1) << "Missing monotonic offset for setting start time for node " |
| 566 | << aos::FlatbufferToJson(node); |
| 567 | break; |
| 568 | } |
| 569 | |
| 570 | // Found it and it is connected. Compensate and go. |
| 571 | SetStartTime(node_index, |
| 572 | monotonic_start_time + |
| 573 | std::chrono::nanoseconds(connection->monotonic_offset()), |
| 574 | realtime_start_time, monotonic_start_time, |
| 575 | realtime_start_time); |
| 576 | return true; |
| 577 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 578 | } |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> Logger::MakeHeader( |
| 583 | const Node *node) { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 584 | // Now write the header with this timestamp in it. |
| 585 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 586 | fbb.ForceDefaults(true); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 587 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 588 | // TODO(austin): Compress this much more efficiently. There are a bunch of |
| 589 | // duplicated schemas. |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 590 | const flatbuffers::Offset<aos::Configuration> configuration_offset = |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 591 | CopyFlatBuffer(configuration_, &fbb); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 592 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 593 | const flatbuffers::Offset<flatbuffers::String> name_offset = |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 594 | fbb.CreateString(name_); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 595 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 596 | CHECK(log_event_uuid_ != UUID::Zero()); |
| 597 | const flatbuffers::Offset<flatbuffers::String> log_event_uuid_offset = |
| 598 | fbb.CreateString(log_event_uuid_.string_view()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 599 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 600 | const flatbuffers::Offset<flatbuffers::String> logger_instance_uuid_offset = |
| 601 | fbb.CreateString(logger_instance_uuid_.string_view()); |
| 602 | |
| 603 | flatbuffers::Offset<flatbuffers::String> log_start_uuid_offset; |
| 604 | if (!log_start_uuid_.empty()) { |
| 605 | log_start_uuid_offset = fbb.CreateString(log_start_uuid_); |
| 606 | } |
| 607 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 608 | const flatbuffers::Offset<flatbuffers::String> logger_node_boot_uuid_offset = |
| 609 | fbb.CreateString(event_loop_->boot_uuid().string_view()); |
| 610 | |
| 611 | const flatbuffers::Offset<flatbuffers::String> source_node_boot_uuid_offset = |
| 612 | fbb.CreateString(event_loop_->boot_uuid().string_view()); |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 613 | |
| 614 | const flatbuffers::Offset<flatbuffers::String> parts_uuid_offset = |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 615 | fbb.CreateString("00000000-0000-4000-8000-000000000000"); |
| 616 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 617 | flatbuffers::Offset<Node> node_offset; |
Brian Silverman | 80993c2 | 2020-10-01 15:05:19 -0700 | [diff] [blame] | 618 | flatbuffers::Offset<Node> logger_node_offset; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 619 | |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 620 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 621 | // TODO(austin): Reuse the node we just copied in above. |
| 622 | node_offset = RecursiveCopyFlatBuffer(node, &fbb); |
| 623 | logger_node_offset = RecursiveCopyFlatBuffer(event_loop_->node(), &fbb); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | aos::logger::LogFileHeader::Builder log_file_header_builder(fbb); |
| 627 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 628 | log_file_header_builder.add_name(name_offset); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 629 | |
| 630 | // Only add the node if we are running in a multinode configuration. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 631 | if (node != nullptr) { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 632 | log_file_header_builder.add_node(node_offset); |
Brian Silverman | 80993c2 | 2020-10-01 15:05:19 -0700 | [diff] [blame] | 633 | log_file_header_builder.add_logger_node(logger_node_offset); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | log_file_header_builder.add_configuration(configuration_offset); |
| 637 | // The worst case theoretical out of order is the polling period times 2. |
| 638 | // One message could get logged right after the boundary, but be for right |
| 639 | // before the next boundary. And the reverse could happen for another |
| 640 | // message. Report back 3x to be extra safe, and because the cost isn't |
| 641 | // huge on the read side. |
| 642 | log_file_header_builder.add_max_out_of_order_duration( |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 643 | std::chrono::nanoseconds(3 * polling_period_).count()); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 644 | |
| 645 | log_file_header_builder.add_monotonic_start_time( |
| 646 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 647 | monotonic_clock::min_time.time_since_epoch()) |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 648 | .count()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 649 | if (node == event_loop_->node()) { |
| 650 | log_file_header_builder.add_realtime_start_time( |
| 651 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 652 | realtime_clock::min_time.time_since_epoch()) |
| 653 | .count()); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 654 | } else { |
| 655 | log_file_header_builder.add_logger_monotonic_start_time( |
| 656 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 657 | monotonic_clock::min_time.time_since_epoch()) |
| 658 | .count()); |
| 659 | log_file_header_builder.add_logger_realtime_start_time( |
| 660 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 661 | realtime_clock::min_time.time_since_epoch()) |
| 662 | .count()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 665 | log_file_header_builder.add_log_event_uuid(log_event_uuid_offset); |
| 666 | log_file_header_builder.add_logger_instance_uuid(logger_instance_uuid_offset); |
| 667 | if (!log_start_uuid_offset.IsNull()) { |
| 668 | log_file_header_builder.add_log_start_uuid(log_start_uuid_offset); |
| 669 | } |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 670 | log_file_header_builder.add_logger_node_boot_uuid( |
| 671 | logger_node_boot_uuid_offset); |
| 672 | log_file_header_builder.add_source_node_boot_uuid( |
| 673 | source_node_boot_uuid_offset); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 674 | |
| 675 | log_file_header_builder.add_parts_uuid(parts_uuid_offset); |
| 676 | log_file_header_builder.add_parts_index(0); |
| 677 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 678 | fbb.FinishSizePrefixed(log_file_header_builder.Finish()); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 679 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> result( |
| 680 | fbb.Release()); |
| 681 | |
| 682 | CHECK(result.Verify()) << ": Built a corrupted header."; |
| 683 | |
| 684 | return result; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 687 | void Logger::ResetStatisics() { |
| 688 | max_message_fetch_time_ = std::chrono::nanoseconds::zero(); |
| 689 | max_message_fetch_time_channel_ = -1; |
| 690 | max_message_fetch_time_size_ = -1; |
| 691 | total_message_fetch_time_ = std::chrono::nanoseconds::zero(); |
| 692 | total_message_fetch_count_ = 0; |
| 693 | total_message_fetch_bytes_ = 0; |
| 694 | total_nop_fetch_time_ = std::chrono::nanoseconds::zero(); |
| 695 | total_nop_fetch_count_ = 0; |
| 696 | max_copy_time_ = std::chrono::nanoseconds::zero(); |
| 697 | max_copy_time_channel_ = -1; |
| 698 | max_copy_time_size_ = -1; |
| 699 | total_copy_time_ = std::chrono::nanoseconds::zero(); |
| 700 | total_copy_count_ = 0; |
| 701 | total_copy_bytes_ = 0; |
| 702 | } |
| 703 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 704 | void Logger::Rotate() { |
| 705 | for (const Node *node : log_namer_->nodes()) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 706 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 707 | log_namer_->Rotate(node, &node_state_[node_index].log_file_header); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | void Logger::LogUntil(monotonic_clock::time_point t) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 712 | // Grab the latest ServerStatistics message. This will always have the |
| 713 | // oppertunity to be >= to the current time, so it will always represent any |
| 714 | // reboots which may have happened. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 715 | WriteMissingTimestamps(); |
| 716 | |
| 717 | // Write each channel to disk, one at a time. |
| 718 | for (FetcherStruct &f : fetchers_) { |
| 719 | while (true) { |
| 720 | if (f.written) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 721 | const auto start = event_loop_->monotonic_now(); |
| 722 | const bool got_new = f.fetcher->FetchNext(); |
| 723 | const auto end = event_loop_->monotonic_now(); |
| 724 | RecordFetchResult(start, end, got_new, &f); |
| 725 | if (!got_new) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 726 | VLOG(2) << "No new data on " |
| 727 | << configuration::CleanedChannelToString( |
| 728 | f.fetcher->channel()); |
| 729 | break; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 730 | } |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 731 | f.written = false; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 734 | // TODO(james): Write tests to exercise this logic. |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 735 | if (f.fetcher->context().monotonic_event_time >= t) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 736 | break; |
| 737 | } |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 738 | if (f.writer != nullptr) { |
| 739 | // Write! |
| 740 | const auto start = event_loop_->monotonic_now(); |
| 741 | flatbuffers::FlatBufferBuilder fbb(f.fetcher->context().size + |
| 742 | max_header_size_); |
| 743 | fbb.ForceDefaults(true); |
| 744 | |
| 745 | fbb.FinishSizePrefixed(PackMessage(&fbb, f.fetcher->context(), |
| 746 | f.channel_index, f.log_type)); |
| 747 | const auto end = event_loop_->monotonic_now(); |
| 748 | RecordCreateMessageTime(start, end, &f); |
| 749 | |
| 750 | VLOG(2) << "Writing data as node " |
| 751 | << FlatbufferToJson(event_loop_->node()) << " for channel " |
| 752 | << configuration::CleanedChannelToString(f.fetcher->channel()) |
| 753 | << " to " << f.writer->filename() << " data " |
| 754 | << FlatbufferToJson( |
| 755 | flatbuffers::GetSizePrefixedRoot<MessageHeader>( |
| 756 | fbb.GetBufferPointer())); |
| 757 | |
| 758 | max_header_size_ = std::max(max_header_size_, |
| 759 | fbb.GetSize() - f.fetcher->context().size); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 760 | CHECK(node_state_[f.data_node_index].header_valid) |
| 761 | << ": Can't write data before the header on channel " |
| 762 | << configuration::CleanedChannelToString(f.fetcher->channel()); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 763 | f.writer->QueueSizedFlatbuffer(&fbb); |
| 764 | } |
| 765 | |
| 766 | if (f.timestamp_writer != nullptr) { |
| 767 | // And now handle timestamps. |
| 768 | const auto start = event_loop_->monotonic_now(); |
| 769 | flatbuffers::FlatBufferBuilder fbb; |
| 770 | fbb.ForceDefaults(true); |
| 771 | |
| 772 | fbb.FinishSizePrefixed(PackMessage(&fbb, f.fetcher->context(), |
| 773 | f.channel_index, |
| 774 | LogType::kLogDeliveryTimeOnly)); |
| 775 | const auto end = event_loop_->monotonic_now(); |
| 776 | RecordCreateMessageTime(start, end, &f); |
| 777 | |
| 778 | VLOG(2) << "Writing timestamps as node " |
| 779 | << FlatbufferToJson(event_loop_->node()) << " for channel " |
| 780 | << configuration::CleanedChannelToString(f.fetcher->channel()) |
| 781 | << " to " << f.timestamp_writer->filename() << " timestamp " |
| 782 | << FlatbufferToJson( |
| 783 | flatbuffers::GetSizePrefixedRoot<MessageHeader>( |
| 784 | fbb.GetBufferPointer())); |
| 785 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 786 | CHECK(node_state_[f.timestamp_node_index].header_valid) |
| 787 | << ": Can't write data before the header on channel " |
| 788 | << configuration::CleanedChannelToString(f.fetcher->channel()); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 789 | f.timestamp_writer->QueueSizedFlatbuffer(&fbb); |
| 790 | } |
| 791 | |
| 792 | if (f.contents_writer != nullptr) { |
| 793 | const auto start = event_loop_->monotonic_now(); |
| 794 | // And now handle the special message contents channel. Copy the |
| 795 | // message into a FlatBufferBuilder and save it to disk. |
| 796 | // TODO(austin): We can be more efficient here when we start to |
| 797 | // care... |
| 798 | flatbuffers::FlatBufferBuilder fbb; |
| 799 | fbb.ForceDefaults(true); |
| 800 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 801 | const RemoteMessage *msg = |
| 802 | flatbuffers::GetRoot<RemoteMessage>(f.fetcher->context().data); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 803 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 804 | CHECK(msg->has_boot_uuid()) << ": " << aos::FlatbufferToJson(msg); |
| 805 | if (!node_state_[f.contents_node_index].has_source_node_boot_uuid || |
| 806 | node_state_[f.contents_node_index].source_node_boot_uuid != |
| 807 | msg->boot_uuid()->string_view()) { |
| 808 | node_state_[f.contents_node_index].SetBootUUID( |
| 809 | msg->boot_uuid()->string_view()); |
| 810 | |
| 811 | MaybeWriteHeader(f.contents_node_index); |
| 812 | } |
| 813 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 814 | logger::MessageHeader::Builder message_header_builder(fbb); |
| 815 | |
| 816 | // TODO(austin): This needs to check the channel_index and confirm |
| 817 | // that it should be logged before squirreling away the timestamp to |
| 818 | // disk. We don't want to log irrelevant timestamps. |
| 819 | |
| 820 | // Note: this must match the same order as MessageBridgeServer and |
| 821 | // PackMessage. We want identical headers to have identical |
| 822 | // on-the-wire formats to make comparing them easier. |
| 823 | |
| 824 | // Translate from the channel index that the event loop uses to the |
| 825 | // channel index in the log file. |
| 826 | message_header_builder.add_channel_index( |
| 827 | event_loop_to_logged_channel_index_[msg->channel_index()]); |
| 828 | |
| 829 | message_header_builder.add_queue_index(msg->queue_index()); |
| 830 | message_header_builder.add_monotonic_sent_time( |
| 831 | msg->monotonic_sent_time()); |
| 832 | message_header_builder.add_realtime_sent_time( |
| 833 | msg->realtime_sent_time()); |
| 834 | |
| 835 | message_header_builder.add_monotonic_remote_time( |
| 836 | msg->monotonic_remote_time()); |
| 837 | message_header_builder.add_realtime_remote_time( |
| 838 | msg->realtime_remote_time()); |
| 839 | message_header_builder.add_remote_queue_index( |
| 840 | msg->remote_queue_index()); |
| 841 | |
| 842 | fbb.FinishSizePrefixed(message_header_builder.Finish()); |
| 843 | const auto end = event_loop_->monotonic_now(); |
| 844 | RecordCreateMessageTime(start, end, &f); |
| 845 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 846 | CHECK(node_state_[f.contents_node_index].header_valid) |
| 847 | << ": Can't write data before the header on channel " |
| 848 | << configuration::CleanedChannelToString(f.fetcher->channel()); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 849 | f.contents_writer->QueueSizedFlatbuffer(&fbb); |
| 850 | } |
| 851 | |
| 852 | f.written = true; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | last_synchronized_time_ = t; |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 856 | } |
| 857 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 858 | void Logger::DoLogData(const monotonic_clock::time_point end_time) { |
| 859 | // We want to guarantee that messages aren't out of order by more than |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 860 | // max_out_of_order_duration. To do this, we need sync points. Every write |
| 861 | // cycle should be a sync point. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 862 | |
| 863 | do { |
| 864 | // Move the sync point up by at most polling_period. This forces one sync |
| 865 | // per iteration, even if it is small. |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 866 | LogUntil(std::min(last_synchronized_time_ + polling_period_, end_time)); |
| 867 | |
| 868 | on_logged_period_(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 869 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 870 | // If we missed cycles, we could be pretty far behind. Spin until we are |
| 871 | // caught up. |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 872 | } while (last_synchronized_time_ + polling_period_ < end_time); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 875 | void Logger::RecordFetchResult(aos::monotonic_clock::time_point start, |
| 876 | aos::monotonic_clock::time_point end, |
| 877 | bool got_new, FetcherStruct *fetcher) { |
| 878 | const auto duration = end - start; |
| 879 | if (!got_new) { |
| 880 | ++total_nop_fetch_count_; |
| 881 | total_nop_fetch_time_ += duration; |
| 882 | return; |
| 883 | } |
| 884 | ++total_message_fetch_count_; |
| 885 | total_message_fetch_bytes_ += fetcher->fetcher->context().size; |
| 886 | total_message_fetch_time_ += duration; |
| 887 | if (duration > max_message_fetch_time_) { |
| 888 | max_message_fetch_time_ = duration; |
| 889 | max_message_fetch_time_channel_ = fetcher->channel_index; |
| 890 | max_message_fetch_time_size_ = fetcher->fetcher->context().size; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | void Logger::RecordCreateMessageTime(aos::monotonic_clock::time_point start, |
| 895 | aos::monotonic_clock::time_point end, |
| 896 | FetcherStruct *fetcher) { |
| 897 | const auto duration = end - start; |
| 898 | total_copy_time_ += duration; |
| 899 | ++total_copy_count_; |
| 900 | total_copy_bytes_ += fetcher->fetcher->context().size; |
| 901 | if (duration > max_copy_time_) { |
| 902 | max_copy_time_ = duration; |
| 903 | max_copy_time_channel_ = fetcher->channel_index; |
| 904 | max_copy_time_size_ = fetcher->fetcher->context().size; |
| 905 | } |
| 906 | } |
| 907 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 908 | std::vector<std::vector<std::string>> ToLogReaderVector( |
| 909 | const std::vector<LogFile> &log_files) { |
| 910 | std::vector<std::vector<std::string>> result; |
| 911 | for (const LogFile &log_file : log_files) { |
| 912 | for (const LogParts &log_parts : log_file.parts) { |
| 913 | std::vector<std::string> parts; |
| 914 | for (const std::string &part : log_parts.parts) { |
| 915 | parts.emplace_back(part); |
| 916 | } |
| 917 | result.emplace_back(std::move(parts)); |
| 918 | } |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 919 | } |
| 920 | return result; |
| 921 | } |
| 922 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 923 | LogReader::LogReader(std::string_view filename, |
| 924 | const Configuration *replay_configuration) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 925 | : LogReader(SortParts({std::string(filename)}), replay_configuration) {} |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 926 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 927 | LogReader::LogReader(std::vector<LogFile> log_files, |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 928 | const Configuration *replay_configuration) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 929 | : log_files_(std::move(log_files)), |
| 930 | log_file_header_(MaybeReadHeaderOrDie(log_files_)), |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 931 | replay_configuration_(replay_configuration) { |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 932 | MakeRemappedConfig(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 933 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 934 | // Remap all existing remote timestamp channels. They will be recreated, and |
| 935 | // the data logged isn't relevant anymore. |
Austin Schuh | 3c5dae5 | 2020-10-06 18:55:18 -0700 | [diff] [blame] | 936 | for (const Node *node : configuration::GetNodes(logged_configuration())) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 937 | std::vector<const Node *> timestamp_logger_nodes = |
| 938 | configuration::TimestampNodes(logged_configuration(), node); |
| 939 | for (const Node *remote_node : timestamp_logger_nodes) { |
| 940 | const std::string channel = absl::StrCat( |
| 941 | "/aos/remote_timestamps/", remote_node->name()->string_view()); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 942 | // See if the log file is an old log with MessageHeader channels in it, or |
| 943 | // a newer log with RemoteMessage. If we find an older log, rename the |
| 944 | // type too along with the name. |
| 945 | if (HasChannel<MessageHeader>(channel, node)) { |
| 946 | CHECK(!HasChannel<RemoteMessage>(channel, node)) |
| 947 | << ": Can't have both a MessageHeader and RemoteMessage remote " |
| 948 | "timestamp channel."; |
| 949 | RemapLoggedChannel<MessageHeader>(channel, node, "/original", |
| 950 | "aos.message_bridge.RemoteMessage"); |
| 951 | } else { |
| 952 | CHECK(HasChannel<RemoteMessage>(channel, node)) |
| 953 | << ": Failed to find {\"name\": \"" << channel << "\", \"type\": \"" |
| 954 | << RemoteMessage::GetFullyQualifiedName() << "\"} for node " |
| 955 | << node->name()->string_view(); |
| 956 | RemapLoggedChannel<RemoteMessage>(channel, node); |
| 957 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 961 | if (replay_configuration) { |
| 962 | CHECK_EQ(configuration::MultiNode(configuration()), |
| 963 | configuration::MultiNode(replay_configuration)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 964 | << ": Log file and replay config need to both be multi or single " |
| 965 | "node."; |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 966 | } |
| 967 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 968 | if (!configuration::MultiNode(configuration())) { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 969 | states_.emplace_back(std::make_unique<State>( |
| 970 | std::make_unique<TimestampMapper>(FilterPartsForNode(log_files_, "")))); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 971 | } else { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 972 | if (replay_configuration) { |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 973 | CHECK_EQ(logged_configuration()->nodes()->size(), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 974 | replay_configuration->nodes()->size()) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 975 | << ": Log file and replay config need to have matching nodes " |
| 976 | "lists."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 977 | for (const Node *node : *logged_configuration()->nodes()) { |
| 978 | if (configuration::GetNode(replay_configuration, node) == nullptr) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 979 | LOG(FATAL) << "Found node " << FlatbufferToJson(node) |
| 980 | << " in logged config that is not present in the replay " |
| 981 | "config."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 982 | } |
| 983 | } |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 984 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 985 | states_.resize(configuration()->nodes()->size()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 986 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 989 | LogReader::~LogReader() { |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 990 | if (event_loop_factory_unique_ptr_) { |
| 991 | Deregister(); |
| 992 | } else if (event_loop_factory_ != nullptr) { |
| 993 | LOG(FATAL) << "Must call Deregister before the SimulatedEventLoopFactory " |
| 994 | "is destroyed"; |
| 995 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 996 | if (offset_fp_ != nullptr) { |
| 997 | fclose(offset_fp_); |
| 998 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 999 | // Zero out some buffers. It's easy to do use-after-frees on these, so make |
| 1000 | // it more obvious. |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 1001 | if (remapped_configuration_buffer_) { |
| 1002 | remapped_configuration_buffer_->Wipe(); |
| 1003 | } |
| 1004 | log_file_header_.Wipe(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1005 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1006 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1007 | const Configuration *LogReader::logged_configuration() const { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1008 | return log_file_header_.message().configuration(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1011 | const Configuration *LogReader::configuration() const { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1012 | return remapped_configuration_; |
| 1013 | } |
| 1014 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1015 | std::vector<const Node *> LogReader::Nodes() const { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1016 | // Because the Node pointer will only be valid if it actually points to |
| 1017 | // memory owned by remapped_configuration_, we need to wait for the |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1018 | // remapped_configuration_ to be populated before accessing it. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1019 | // |
| 1020 | // Also, note, that when ever a map is changed, the nodes in here are |
| 1021 | // invalidated. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1022 | CHECK(remapped_configuration_ != nullptr) |
| 1023 | << ": Need to call Register before the node() pointer will be valid."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1024 | return configuration::GetNodes(remapped_configuration_); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1025 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1026 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1027 | monotonic_clock::time_point LogReader::monotonic_start_time( |
| 1028 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1029 | State *state = |
| 1030 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 1031 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 1032 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1033 | return state->monotonic_start_time(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1036 | realtime_clock::time_point LogReader::realtime_start_time( |
| 1037 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1038 | State *state = |
| 1039 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 1040 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 1041 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1042 | return state->realtime_start_time(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1045 | void LogReader::Register() { |
| 1046 | event_loop_factory_unique_ptr_ = |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1047 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1048 | Register(event_loop_factory_unique_ptr_.get()); |
| 1049 | } |
| 1050 | |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1051 | void LogReader::Register(SimulatedEventLoopFactory *event_loop_factory) { |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1052 | event_loop_factory_ = event_loop_factory; |
Austin Schuh | e5bbd9e | 2020-09-21 17:29:20 -0700 | [diff] [blame] | 1053 | remapped_configuration_ = event_loop_factory_->configuration(); |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1054 | |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 1055 | for (const Node *node : configuration::GetNodes(configuration())) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1056 | const size_t node_index = |
| 1057 | configuration::GetNodeIndex(configuration(), node); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1058 | std::vector<LogParts> filtered_parts = FilterPartsForNode( |
| 1059 | log_files_, node != nullptr ? node->name()->string_view() : ""); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 1060 | |
| 1061 | // Confirm that all the parts are from the same boot if there are enough |
| 1062 | // parts to not be from the same boot. |
| 1063 | if (filtered_parts.size() > 1u) { |
| 1064 | for (size_t i = 1; i < filtered_parts.size(); ++i) { |
| 1065 | CHECK_EQ(filtered_parts[i].source_boot_uuid, |
| 1066 | filtered_parts[0].source_boot_uuid) |
| 1067 | << ": Found parts from different boots " |
| 1068 | << LogFileVectorToString(log_files_); |
| 1069 | } |
| 1070 | } |
| 1071 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1072 | states_[node_index] = std::make_unique<State>( |
| 1073 | filtered_parts.size() == 0u |
| 1074 | ? nullptr |
| 1075 | : std::make_unique<TimestampMapper>(std::move(filtered_parts))); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1076 | State *state = states_[node_index].get(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1077 | state->set_event_loop(state->SetNodeEventLoopFactory( |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1078 | event_loop_factory_->GetNodeEventLoopFactory(node))); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1079 | |
| 1080 | state->SetChannelCount(logged_configuration()->channels()->size()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1081 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1082 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1083 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 1084 | const size_t node_index = |
| 1085 | configuration::GetNodeIndex(configuration(), node); |
| 1086 | State *state = states_[node_index].get(); |
| 1087 | for (const Node *other_node : configuration::GetNodes(configuration())) { |
| 1088 | const size_t other_node_index = |
| 1089 | configuration::GetNodeIndex(configuration(), other_node); |
| 1090 | State *other_state = states_[other_node_index].get(); |
| 1091 | if (other_state != state) { |
| 1092 | state->AddPeer(other_state); |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1097 | // Register after making all the State objects so we can build references |
| 1098 | // between them. |
| 1099 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 1100 | const size_t node_index = |
| 1101 | configuration::GetNodeIndex(configuration(), node); |
| 1102 | State *state = states_[node_index].get(); |
| 1103 | |
| 1104 | Register(state->event_loop()); |
| 1105 | } |
| 1106 | |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 1107 | if (live_nodes_ == 0) { |
| 1108 | LOG(FATAL) |
| 1109 | << "Don't have logs from any of the nodes in the replay config--are " |
| 1110 | "you sure that the replay config matches the original config?"; |
| 1111 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1112 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1113 | // We need to now seed our per-node time offsets and get everything set up |
| 1114 | // to run. |
| 1115 | const size_t num_nodes = nodes_count(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1116 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1117 | // It is easiest to solve for per node offsets with a matrix rather than |
| 1118 | // trying to solve the equations by hand. So let's get after it. |
| 1119 | // |
| 1120 | // Now, build up the map matrix. |
| 1121 | // |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1122 | // offset_matrix_ = (map_matrix_ + slope_matrix_) * [ta; tb; tc] |
| 1123 | map_matrix_ = Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic>::Zero( |
| 1124 | filters_.size() + 1, num_nodes); |
| 1125 | slope_matrix_ = |
| 1126 | Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic>::Zero( |
| 1127 | filters_.size() + 1, num_nodes); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1128 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1129 | offset_matrix_ = |
| 1130 | Eigen::Matrix<mpq_class, Eigen::Dynamic, 1>::Zero(filters_.size() + 1); |
| 1131 | valid_matrix_ = |
| 1132 | Eigen::Matrix<bool, Eigen::Dynamic, 1>::Zero(filters_.size() + 1); |
| 1133 | last_valid_matrix_ = |
| 1134 | Eigen::Matrix<bool, Eigen::Dynamic, 1>::Zero(filters_.size() + 1); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1135 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1136 | time_offset_matrix_ = Eigen::VectorXd::Zero(num_nodes); |
| 1137 | time_slope_matrix_ = Eigen::VectorXd::Zero(num_nodes); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1138 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1139 | // All times should average out to the distributed clock. |
| 1140 | for (int i = 0; i < map_matrix_.cols(); ++i) { |
| 1141 | // 1/num_nodes. |
| 1142 | map_matrix_(0, i) = mpq_class(1, num_nodes); |
| 1143 | } |
| 1144 | valid_matrix_(0) = true; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1145 | |
| 1146 | { |
| 1147 | // Now, add the a - b -> sample elements. |
| 1148 | size_t i = 1; |
| 1149 | for (std::pair<const std::tuple<const Node *, const Node *>, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1150 | std::tuple<message_bridge::NoncausalOffsetEstimator>> |
| 1151 | &filter : filters_) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1152 | const Node *const node_a = std::get<0>(filter.first); |
| 1153 | const Node *const node_b = std::get<1>(filter.first); |
| 1154 | |
| 1155 | const size_t node_a_index = |
| 1156 | configuration::GetNodeIndex(configuration(), node_a); |
| 1157 | const size_t node_b_index = |
| 1158 | configuration::GetNodeIndex(configuration(), node_b); |
| 1159 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1160 | // -a |
| 1161 | map_matrix_(i, node_a_index) = mpq_class(-1); |
| 1162 | // +b |
| 1163 | map_matrix_(i, node_b_index) = mpq_class(1); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1164 | |
| 1165 | // -> sample |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1166 | std::get<0>(filter.second) |
| 1167 | .set_slope_pointer(&slope_matrix_(i, node_a_index)); |
| 1168 | std::get<0>(filter.second).set_offset_pointer(&offset_matrix_(i, 0)); |
| 1169 | |
| 1170 | valid_matrix_(i) = false; |
| 1171 | std::get<0>(filter.second).set_valid_pointer(&valid_matrix_(i)); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1172 | |
| 1173 | ++i; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1177 | for (std::unique_ptr<State> &state : states_) { |
| 1178 | state->SeedSortedMessages(); |
| 1179 | } |
| 1180 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1181 | // Rank of the map matrix tells you if all the nodes are in communication |
| 1182 | // with each other, which tells you if the offsets are observable. |
| 1183 | const size_t connected_nodes = |
| 1184 | Eigen::FullPivLU< |
| 1185 | Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic>>(map_matrix_) |
| 1186 | .rank(); |
| 1187 | |
| 1188 | // We don't need to support isolated nodes until someone has a real use |
| 1189 | // case. |
| 1190 | CHECK_EQ(connected_nodes, num_nodes) |
| 1191 | << ": There is a node which isn't communicating with the rest."; |
| 1192 | |
| 1193 | // And solve. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1194 | UpdateOffsets(); |
| 1195 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1196 | // We want to start the log file at the last start time of the log files |
| 1197 | // from all the nodes. Compute how long each node's simulation needs to run |
| 1198 | // to move time to this point. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1199 | distributed_clock::time_point start_time = distributed_clock::min_time; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1200 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1201 | // TODO(austin): We want an "OnStart" callback for each node rather than |
| 1202 | // running until the last node. |
| 1203 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1204 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1205 | VLOG(1) << "Start time is " << state->monotonic_start_time() << " for node " |
| 1206 | << MaybeNodeName(state->event_loop()->node()) << "now " |
| 1207 | << state->monotonic_now(); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1208 | if (state->monotonic_start_time() == monotonic_clock::min_time) { |
| 1209 | continue; |
| 1210 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1211 | // And start computing the start time on the distributed clock now that |
| 1212 | // that works. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1213 | start_time = std::max( |
| 1214 | start_time, state->ToDistributedClock(state->monotonic_start_time())); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1215 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1216 | |
| 1217 | CHECK_GE(start_time, distributed_clock::epoch()) |
| 1218 | << ": Hmm, we have a node starting before the start of time. Offset " |
| 1219 | "everything."; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1220 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1221 | // Forwarding is tracked per channel. If it is enabled, we want to turn it |
| 1222 | // off. Otherwise messages replayed will get forwarded across to the other |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1223 | // nodes, and also replayed on the other nodes. This may not satisfy all |
| 1224 | // our users, but it'll start the discussion. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1225 | if (configuration::MultiNode(event_loop_factory_->configuration())) { |
| 1226 | for (size_t i = 0; i < logged_configuration()->channels()->size(); ++i) { |
| 1227 | const Channel *channel = logged_configuration()->channels()->Get(i); |
| 1228 | const Node *node = configuration::GetNode( |
| 1229 | configuration(), channel->source_node()->string_view()); |
| 1230 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1231 | State *state = |
| 1232 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1233 | |
| 1234 | const Channel *remapped_channel = |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1235 | RemapChannel(state->event_loop(), channel); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1236 | |
| 1237 | event_loop_factory_->DisableForwarding(remapped_channel); |
| 1238 | } |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1239 | |
| 1240 | // If we are replaying a log, we don't want a bunch of redundant messages |
| 1241 | // from both the real message bridge and simulated message bridge. |
| 1242 | event_loop_factory_->DisableStatistics(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1245 | // While we are starting the system up, we might be relying on matching data |
| 1246 | // to timestamps on log files where the timestamp log file starts before the |
| 1247 | // data. In this case, it is reasonable to expect missing data. |
| 1248 | ignore_missing_data_ = true; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1249 | VLOG(1) << "Running until " << start_time << " in Register"; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1250 | event_loop_factory_->RunFor(start_time.time_since_epoch()); |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1251 | VLOG(1) << "At start time"; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1252 | // Now that we are running for real, missing data means that the log file is |
| 1253 | // corrupted or went wrong. |
| 1254 | ignore_missing_data_ = false; |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1255 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1256 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1257 | // Make the RT clock be correct before handing it to the user. |
| 1258 | if (state->realtime_start_time() != realtime_clock::min_time) { |
| 1259 | state->SetRealtimeOffset(state->monotonic_start_time(), |
| 1260 | state->realtime_start_time()); |
| 1261 | } |
| 1262 | VLOG(1) << "Start time is " << state->monotonic_start_time() << " for node " |
| 1263 | << MaybeNodeName(state->event_loop()->node()) << "now " |
| 1264 | << state->monotonic_now(); |
| 1265 | } |
| 1266 | |
| 1267 | if (FLAGS_timestamps_to_csv) { |
| 1268 | for (std::pair<const std::tuple<const Node *, const Node *>, |
| 1269 | std::tuple<message_bridge::NoncausalOffsetEstimator>> |
| 1270 | &filter : filters_) { |
| 1271 | const Node *const node_a = std::get<0>(filter.first); |
| 1272 | const Node *const node_b = std::get<1>(filter.first); |
| 1273 | |
| 1274 | std::get<0>(filter.second) |
| 1275 | .SetFirstFwdTime(event_loop_factory_->GetNodeEventLoopFactory(node_a) |
| 1276 | ->monotonic_now()); |
| 1277 | std::get<0>(filter.second) |
| 1278 | .SetFirstRevTime(event_loop_factory_->GetNodeEventLoopFactory(node_b) |
| 1279 | ->monotonic_now()); |
| 1280 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1284 | void LogReader::UpdateOffsets() { |
| 1285 | VLOG(2) << "Samples are " << offset_matrix_; |
| 1286 | VLOG(2) << "Map is " << (map_matrix_ + slope_matrix_); |
| 1287 | std::tie(time_slope_matrix_, time_offset_matrix_) = SolveOffsets(); |
| 1288 | Eigen::IOFormat HeavyFmt(Eigen::FullPrecision, 0, ", ", ";\n", "[", "]", "[", |
| 1289 | "]"); |
| 1290 | VLOG(1) << "First slope " << time_slope_matrix_.transpose().format(HeavyFmt) |
| 1291 | << " offset " << time_offset_matrix_.transpose().format(HeavyFmt); |
| 1292 | |
| 1293 | size_t node_index = 0; |
| 1294 | for (std::unique_ptr<State> &state : states_) { |
| 1295 | state->SetDistributedOffset(offset(node_index), slope(node_index)); |
| 1296 | VLOG(1) << "Offset for node " << node_index << " " |
| 1297 | << MaybeNodeName(state->event_loop()->node()) << "is " |
| 1298 | << aos::distributed_clock::time_point(offset(node_index)) |
| 1299 | << " slope " << std::setprecision(9) << std::fixed |
| 1300 | << slope(node_index); |
| 1301 | ++node_index; |
| 1302 | } |
| 1303 | |
| 1304 | if (VLOG_IS_ON(1)) { |
| 1305 | LogFit("Offset is"); |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | void LogReader::LogFit(std::string_view prefix) { |
| 1310 | for (std::unique_ptr<State> &state : states_) { |
| 1311 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << " now " |
| 1312 | << state->monotonic_now() << " distributed " |
| 1313 | << event_loop_factory_->distributed_now(); |
| 1314 | } |
| 1315 | |
| 1316 | for (std::pair<const std::tuple<const Node *, const Node *>, |
| 1317 | std::tuple<message_bridge::NoncausalOffsetEstimator>> &filter : |
| 1318 | filters_) { |
| 1319 | message_bridge::NoncausalOffsetEstimator *estimator = |
| 1320 | &std::get<0>(filter.second); |
| 1321 | |
| 1322 | if (estimator->a_timestamps().size() == 0 && |
| 1323 | estimator->b_timestamps().size() == 0) { |
| 1324 | continue; |
| 1325 | } |
| 1326 | |
| 1327 | if (VLOG_IS_ON(1)) { |
| 1328 | estimator->LogFit(prefix); |
| 1329 | } |
| 1330 | |
| 1331 | const Node *const node_a = std::get<0>(filter.first); |
| 1332 | const Node *const node_b = std::get<1>(filter.first); |
| 1333 | |
| 1334 | const size_t node_a_index = |
| 1335 | configuration::GetNodeIndex(configuration(), node_a); |
| 1336 | const size_t node_b_index = |
| 1337 | configuration::GetNodeIndex(configuration(), node_b); |
| 1338 | |
| 1339 | const double recovered_slope = |
| 1340 | slope(node_b_index) / slope(node_a_index) - 1.0; |
| 1341 | const int64_t recovered_offset = |
| 1342 | offset(node_b_index).count() - offset(node_a_index).count() * |
| 1343 | slope(node_b_index) / |
| 1344 | slope(node_a_index); |
| 1345 | |
| 1346 | VLOG(1) << "Recovered slope " << std::setprecision(20) << recovered_slope |
| 1347 | << " (error " << recovered_slope - estimator->fit().slope() << ") " |
| 1348 | << " offset " << std::setprecision(20) << recovered_offset |
| 1349 | << " (error " |
| 1350 | << recovered_offset - estimator->fit().offset().count() << ")"; |
| 1351 | |
| 1352 | const aos::distributed_clock::time_point a0 = |
| 1353 | states_[node_a_index]->ToDistributedClock( |
| 1354 | std::get<0>(estimator->a_timestamps()[0])); |
| 1355 | const aos::distributed_clock::time_point a1 = |
| 1356 | states_[node_a_index]->ToDistributedClock( |
| 1357 | std::get<0>(estimator->a_timestamps()[1])); |
| 1358 | |
| 1359 | VLOG(1) << node_a->name()->string_view() << " timestamps()[0] = " |
| 1360 | << std::get<0>(estimator->a_timestamps()[0]) << " -> " << a0 |
| 1361 | << " distributed -> " << node_b->name()->string_view() << " " |
| 1362 | << states_[node_b_index]->FromDistributedClock(a0) << " should be " |
| 1363 | << aos::monotonic_clock::time_point( |
| 1364 | std::chrono::nanoseconds(static_cast<int64_t>( |
| 1365 | std::get<0>(estimator->a_timestamps()[0]) |
| 1366 | .time_since_epoch() |
| 1367 | .count() * |
| 1368 | (1.0 + estimator->fit().slope()))) + |
| 1369 | estimator->fit().offset()) |
| 1370 | << ((a0 <= event_loop_factory_->distributed_now()) |
| 1371 | ? "" |
| 1372 | : " After now, investigate"); |
| 1373 | VLOG(1) << node_a->name()->string_view() << " timestamps()[1] = " |
| 1374 | << std::get<0>(estimator->a_timestamps()[1]) << " -> " << a1 |
| 1375 | << " distributed -> " << node_b->name()->string_view() << " " |
| 1376 | << states_[node_b_index]->FromDistributedClock(a1) << " should be " |
| 1377 | << aos::monotonic_clock::time_point( |
| 1378 | std::chrono::nanoseconds(static_cast<int64_t>( |
| 1379 | std::get<0>(estimator->a_timestamps()[1]) |
| 1380 | .time_since_epoch() |
| 1381 | .count() * |
| 1382 | (1.0 + estimator->fit().slope()))) + |
| 1383 | estimator->fit().offset()) |
| 1384 | << ((event_loop_factory_->distributed_now() <= a1) |
| 1385 | ? "" |
| 1386 | : " Before now, investigate"); |
| 1387 | |
| 1388 | const aos::distributed_clock::time_point b0 = |
| 1389 | states_[node_b_index]->ToDistributedClock( |
| 1390 | std::get<0>(estimator->b_timestamps()[0])); |
| 1391 | const aos::distributed_clock::time_point b1 = |
| 1392 | states_[node_b_index]->ToDistributedClock( |
| 1393 | std::get<0>(estimator->b_timestamps()[1])); |
| 1394 | |
| 1395 | VLOG(1) << node_b->name()->string_view() << " timestamps()[0] = " |
| 1396 | << std::get<0>(estimator->b_timestamps()[0]) << " -> " << b0 |
| 1397 | << " distributed -> " << node_a->name()->string_view() << " " |
| 1398 | << states_[node_a_index]->FromDistributedClock(b0) |
| 1399 | << ((b0 <= event_loop_factory_->distributed_now()) |
| 1400 | ? "" |
| 1401 | : " After now, investigate"); |
| 1402 | VLOG(1) << node_b->name()->string_view() << " timestamps()[1] = " |
| 1403 | << std::get<0>(estimator->b_timestamps()[1]) << " -> " << b1 |
| 1404 | << " distributed -> " << node_a->name()->string_view() << " " |
| 1405 | << states_[node_a_index]->FromDistributedClock(b1) |
| 1406 | << ((event_loop_factory_->distributed_now() <= b1) |
| 1407 | ? "" |
| 1408 | : " Before now, investigate"); |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | message_bridge::NoncausalOffsetEstimator *LogReader::GetFilter( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1413 | const Node *node_a, const Node *node_b) { |
| 1414 | CHECK_NE(node_a, node_b); |
| 1415 | CHECK_EQ(configuration::GetNode(configuration(), node_a), node_a); |
| 1416 | CHECK_EQ(configuration::GetNode(configuration(), node_b), node_b); |
| 1417 | |
| 1418 | if (node_a > node_b) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1419 | return GetFilter(node_b, node_a); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | auto tuple = std::make_tuple(node_a, node_b); |
| 1423 | |
| 1424 | auto it = filters_.find(tuple); |
| 1425 | |
| 1426 | if (it == filters_.end()) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1427 | auto &x = |
| 1428 | filters_ |
| 1429 | .insert(std::make_pair( |
| 1430 | tuple, std::make_tuple(message_bridge::NoncausalOffsetEstimator( |
| 1431 | node_a, node_b)))) |
| 1432 | .first->second; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1433 | if (FLAGS_timestamps_to_csv) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1434 | std::get<0>(x).SetFwdCsvFileName(absl::StrCat( |
| 1435 | "/tmp/timestamp_noncausal_", node_a->name()->string_view(), "_", |
| 1436 | node_b->name()->string_view())); |
| 1437 | std::get<0>(x).SetRevCsvFileName(absl::StrCat( |
| 1438 | "/tmp/timestamp_noncausal_", node_b->name()->string_view(), "_", |
| 1439 | node_a->name()->string_view())); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1440 | } |
| 1441 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1442 | return &std::get<0>(x); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1443 | } else { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1444 | return &std::get<0>(it->second); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1448 | void LogReader::Register(EventLoop *event_loop) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1449 | State *state = |
| 1450 | states_[configuration::GetNodeIndex(configuration(), event_loop->node())] |
| 1451 | .get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1452 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1453 | state->set_event_loop(event_loop); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1454 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1455 | // We don't run timing reports when trying to print out logged data, because |
| 1456 | // otherwise we would end up printing out the timing reports themselves... |
| 1457 | // This is only really relevant when we are replaying into a simulation. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1458 | event_loop->SkipTimingReport(); |
| 1459 | event_loop->SkipAosLog(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1460 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1461 | for (size_t logged_channel_index = 0; |
| 1462 | logged_channel_index < logged_configuration()->channels()->size(); |
| 1463 | ++logged_channel_index) { |
| 1464 | const Channel *channel = RemapChannel( |
| 1465 | event_loop, |
| 1466 | logged_configuration()->channels()->Get(logged_channel_index)); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1467 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1468 | message_bridge::NoncausalOffsetEstimator *filter = nullptr; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1469 | aos::Sender<RemoteMessage> *remote_timestamp_sender = nullptr; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1470 | |
| 1471 | State *source_state = nullptr; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1472 | |
| 1473 | if (!configuration::ChannelIsSendableOnNode(channel, event_loop->node()) && |
| 1474 | configuration::ChannelIsReadableOnNode(channel, event_loop->node())) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1475 | // We've got a message which is being forwarded to this node. |
| 1476 | const Node *source_node = configuration::GetNode( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1477 | event_loop->configuration(), channel->source_node()->string_view()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1478 | filter = GetFilter(event_loop->node(), source_node); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1479 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1480 | // Delivery timestamps are supposed to be logged back on the source node. |
| 1481 | // Configure remote timestamps to be sent. |
| 1482 | const bool delivery_time_is_logged = |
| 1483 | configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 1484 | channel, event_loop->node(), source_node); |
| 1485 | |
| 1486 | source_state = |
| 1487 | states_[configuration::GetNodeIndex(configuration(), source_node)] |
| 1488 | .get(); |
| 1489 | |
| 1490 | if (delivery_time_is_logged) { |
| 1491 | remote_timestamp_sender = |
| 1492 | source_state->RemoteTimestampSender(event_loop->node()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1493 | } |
| 1494 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1495 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1496 | state->SetChannel( |
| 1497 | logged_channel_index, |
| 1498 | configuration::ChannelIndex(event_loop->configuration(), channel), |
| 1499 | event_loop->MakeRawSender(channel), filter, remote_timestamp_sender, |
| 1500 | source_state); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1501 | } |
| 1502 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1503 | // If we didn't find any log files with data in them, we won't ever get a |
| 1504 | // callback or be live. So skip the rest of the setup. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1505 | if (state->OldestMessageTime() == monotonic_clock::max_time) { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1506 | return; |
| 1507 | } |
| 1508 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1509 | state->set_timer_handler(event_loop->AddTimer([this, state]() { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1510 | VLOG(1) << "Starting sending " << MaybeNodeName(state->event_loop()->node()) |
| 1511 | << "at " << state->event_loop()->context().monotonic_event_time |
| 1512 | << " now " << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1513 | if (state->OldestMessageTime() == monotonic_clock::max_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1514 | --live_nodes_; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1515 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Node down!"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1516 | if (live_nodes_ == 0) { |
| 1517 | event_loop_factory_->Exit(); |
| 1518 | } |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 1519 | return; |
| 1520 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1521 | if (VLOG_IS_ON(1)) { |
| 1522 | LogFit("Offset was"); |
| 1523 | } |
| 1524 | |
| 1525 | bool update_time; |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1526 | TimestampedMessage timestamped_message = state->PopOldest(&update_time); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1527 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1528 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1529 | state->event_loop()->context().monotonic_event_time; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1530 | if (!FLAGS_skip_order_validation) { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1531 | CHECK(monotonic_now == timestamped_message.monotonic_event_time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1532 | << ": " << FlatbufferToJson(state->event_loop()->node()) << " Now " |
| 1533 | << monotonic_now << " trying to send " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1534 | << timestamped_message.monotonic_event_time << " failure " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1535 | << state->DebugString(); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1536 | } else if (monotonic_now != timestamped_message.monotonic_event_time) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1537 | LOG(WARNING) << "Check failed: monotonic_now == " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1538 | "timestamped_message.monotonic_event_time) (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1539 | << monotonic_now << " vs. " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1540 | << timestamped_message.monotonic_event_time |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1541 | << "): " << FlatbufferToJson(state->event_loop()->node()) |
| 1542 | << " Now " << monotonic_now << " trying to send " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1543 | << timestamped_message.monotonic_event_time << " failure " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1544 | << state->DebugString(); |
| 1545 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1546 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1547 | if (timestamped_message.monotonic_event_time > |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1548 | state->monotonic_start_time() || |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1549 | event_loop_factory_ != nullptr) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1550 | if ((!ignore_missing_data_ && !FLAGS_skip_missing_forwarding_entries && |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1551 | !state->at_end()) || |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1552 | timestamped_message.data.span().size() != 0u) { |
| 1553 | CHECK_NE(timestamped_message.data.span().size(), 0u) |
Austin Schuh | d32ca31 | 2020-12-13 16:38:36 -0800 | [diff] [blame] | 1554 | << ": Got a message without data on channel " |
| 1555 | << configuration::CleanedChannelToString( |
| 1556 | logged_configuration()->channels()->Get( |
| 1557 | timestamped_message.channel_index)) |
| 1558 | << ". Forwarding entry which was not matched? Use " |
| 1559 | "--skip_missing_forwarding_entries to ignore this."; |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1560 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1561 | if (update_time) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1562 | // Confirm that the message was sent on the sending node before the |
| 1563 | // destination node (this node). As a proxy, do this by making sure |
| 1564 | // 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] | 1565 | if (!FLAGS_skip_order_validation) { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1566 | CHECK_LT( |
| 1567 | timestamped_message.monotonic_remote_time, |
| 1568 | state->monotonic_remote_now(timestamped_message.channel_index)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1569 | << state->event_loop()->node()->name()->string_view() << " to " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1570 | << state->remote_node(timestamped_message.channel_index) |
| 1571 | ->name() |
| 1572 | ->string_view() |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 1573 | << " while trying to send a message on " |
| 1574 | << configuration::CleanedChannelToString( |
| 1575 | logged_configuration()->channels()->Get( |
| 1576 | timestamped_message.channel_index)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1577 | << " " << state->DebugString(); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1578 | } else if (timestamped_message.monotonic_remote_time >= |
| 1579 | state->monotonic_remote_now( |
| 1580 | timestamped_message.channel_index)) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1581 | LOG(WARNING) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1582 | << "Check failed: timestamped_message.monotonic_remote_time < " |
| 1583 | "state->monotonic_remote_now(timestamped_message.channel_" |
| 1584 | "index) (" |
| 1585 | << timestamped_message.monotonic_remote_time << " vs. " |
| 1586 | << state->monotonic_remote_now( |
| 1587 | timestamped_message.channel_index) |
| 1588 | << ") " << state->event_loop()->node()->name()->string_view() |
| 1589 | << " to " |
| 1590 | << state->remote_node(timestamped_message.channel_index) |
| 1591 | ->name() |
| 1592 | ->string_view() |
| 1593 | << " currently " << timestamped_message.monotonic_event_time |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1594 | << " (" |
| 1595 | << state->ToDistributedClock( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1596 | timestamped_message.monotonic_event_time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1597 | << ") remote event time " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1598 | << timestamped_message.monotonic_remote_time << " (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1599 | << state->RemoteToDistributedClock( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1600 | timestamped_message.channel_index, |
| 1601 | timestamped_message.monotonic_remote_time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1602 | << ") " << state->DebugString(); |
| 1603 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1604 | |
| 1605 | if (FLAGS_timestamps_to_csv) { |
| 1606 | if (offset_fp_ == nullptr) { |
| 1607 | offset_fp_ = fopen("/tmp/offsets.csv", "w"); |
| 1608 | fprintf( |
| 1609 | offset_fp_, |
| 1610 | "# time_since_start, offset node 0, offset node 1, ...\n"); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1611 | first_time_ = timestamped_message.realtime_event_time; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | fprintf(offset_fp_, "%.9f", |
| 1615 | std::chrono::duration_cast<std::chrono::duration<double>>( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1616 | timestamped_message.realtime_event_time - first_time_) |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1617 | .count()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1618 | for (int i = 1; i < time_offset_matrix_.rows(); ++i) { |
| 1619 | fprintf(offset_fp_, ", %.9f", |
| 1620 | time_offset_matrix_(i, 0) + |
| 1621 | time_slope_matrix_(i, 0) * |
| 1622 | chrono::duration<double>( |
| 1623 | event_loop_factory_->distributed_now() |
| 1624 | .time_since_epoch()) |
| 1625 | .count()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1626 | } |
| 1627 | fprintf(offset_fp_, "\n"); |
| 1628 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1629 | } |
| 1630 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1631 | // If we have access to the factory, use it to fix the realtime time. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1632 | state->SetRealtimeOffset(timestamped_message.monotonic_event_time, |
| 1633 | timestamped_message.realtime_event_time); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1634 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1635 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Sending " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1636 | << timestamped_message.monotonic_event_time; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1637 | // TODO(austin): std::move channel_data in and make that efficient in |
| 1638 | // simulation. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1639 | state->Send(std::move(timestamped_message)); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1640 | } else if (state->at_end() && !ignore_missing_data_) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1641 | // 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] | 1642 | // reading the rest of the log file and call it quits. We don't want |
| 1643 | // to replay partial data. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1644 | while (state->OldestMessageTime() != monotonic_clock::max_time) { |
| 1645 | bool update_time_dummy; |
| 1646 | state->PopOldest(&update_time_dummy); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1647 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1648 | } else { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1649 | CHECK(timestamped_message.data.span().data() == nullptr) << ": Nullptr"; |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1650 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1651 | } else { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1652 | LOG(WARNING) |
| 1653 | << "Not sending data from before the start of the log file. " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1654 | << timestamped_message.monotonic_event_time.time_since_epoch().count() |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1655 | << " start " << monotonic_start_time().time_since_epoch().count() |
Austin Schuh | d85baf8 | 2020-10-19 11:50:12 -0700 | [diff] [blame] | 1656 | << " " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1657 | << FlatbufferToJson(timestamped_message.data, |
Austin Schuh | d85baf8 | 2020-10-19 11:50:12 -0700 | [diff] [blame] | 1658 | {.multi_line = false, .max_vector_size = 100}); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1659 | } |
| 1660 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1661 | const monotonic_clock::time_point next_time = state->OldestMessageTime(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1662 | if (next_time != monotonic_clock::max_time) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1663 | VLOG(1) << "Scheduling " << MaybeNodeName(state->event_loop()->node()) |
| 1664 | << "wakeup for " << next_time << "(" |
| 1665 | << state->ToDistributedClock(next_time) |
| 1666 | << " distributed), now is " << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1667 | state->Setup(next_time); |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 1668 | } else { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1669 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) |
| 1670 | << "No next message, scheduling shutdown"; |
| 1671 | // Set a timer up immediately after now to die. If we don't do this, |
| 1672 | // then the senders waiting on the message we just read will never get |
| 1673 | // called. |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 1674 | if (event_loop_factory_ != nullptr) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1675 | state->Setup(monotonic_now + event_loop_factory_->send_delay() + |
| 1676 | std::chrono::nanoseconds(1)); |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 1677 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1678 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1679 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1680 | // Once we make this call, the current time changes. So do everything |
| 1681 | // which involves time before changing it. That especially includes |
| 1682 | // sending the message. |
| 1683 | if (update_time) { |
| 1684 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) |
| 1685 | << "updating offsets"; |
| 1686 | |
| 1687 | std::vector<aos::monotonic_clock::time_point> before_times; |
| 1688 | before_times.resize(states_.size()); |
| 1689 | std::transform(states_.begin(), states_.end(), before_times.begin(), |
| 1690 | [](const std::unique_ptr<State> &state) { |
| 1691 | return state->monotonic_now(); |
| 1692 | }); |
| 1693 | |
| 1694 | for (size_t i = 0; i < states_.size(); ++i) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 1695 | VLOG(1) << MaybeNodeName(states_[i]->event_loop()->node()) << "before " |
| 1696 | << states_[i]->monotonic_now(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1697 | } |
| 1698 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1699 | UpdateOffsets(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1700 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Now is now " |
| 1701 | << state->monotonic_now(); |
| 1702 | |
| 1703 | for (size_t i = 0; i < states_.size(); ++i) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 1704 | VLOG(1) << MaybeNodeName(states_[i]->event_loop()->node()) << "after " |
| 1705 | << states_[i]->monotonic_now(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | // TODO(austin): We should be perfect. |
| 1709 | const std::chrono::nanoseconds kTolerance{3}; |
| 1710 | if (!FLAGS_skip_order_validation) { |
| 1711 | CHECK_GE(next_time, state->monotonic_now()) |
| 1712 | << ": Time skipped the next event."; |
| 1713 | |
| 1714 | for (size_t i = 0; i < states_.size(); ++i) { |
| 1715 | CHECK_GE(states_[i]->monotonic_now(), before_times[i] - kTolerance) |
| 1716 | << ": Time changed too much on node " |
| 1717 | << MaybeNodeName(states_[i]->event_loop()->node()); |
| 1718 | CHECK_LE(states_[i]->monotonic_now(), before_times[i] + kTolerance) |
| 1719 | << ": Time changed too much on node " |
| 1720 | << states_[i]->event_loop()->node()->name()->string_view(); |
| 1721 | } |
| 1722 | } else { |
| 1723 | if (next_time < state->monotonic_now()) { |
| 1724 | LOG(WARNING) << "Check failed: next_time >= " |
| 1725 | "state->monotonic_now() (" |
| 1726 | << next_time << " vs. " << state->monotonic_now() |
| 1727 | << "): Time skipped the next event."; |
| 1728 | } |
| 1729 | for (size_t i = 0; i < states_.size(); ++i) { |
Austin Schuh | 724032b | 2020-12-18 22:54:59 -0800 | [diff] [blame] | 1730 | if (states_[i]->monotonic_now() < before_times[i] - kTolerance) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1731 | LOG(WARNING) << "Check failed: " |
| 1732 | "states_[i]->monotonic_now() " |
| 1733 | ">= before_times[i] - kTolerance (" |
| 1734 | << states_[i]->monotonic_now() << " vs. " |
| 1735 | << before_times[i] - kTolerance |
| 1736 | << ") : Time changed too much on node " |
| 1737 | << MaybeNodeName(states_[i]->event_loop()->node()); |
| 1738 | } |
Austin Schuh | 724032b | 2020-12-18 22:54:59 -0800 | [diff] [blame] | 1739 | if (states_[i]->monotonic_now() > before_times[i] + kTolerance) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1740 | LOG(WARNING) << "Check failed: " |
| 1741 | "states_[i]->monotonic_now() " |
| 1742 | "<= before_times[i] + kTolerance (" |
| 1743 | << states_[i]->monotonic_now() << " vs. " |
Austin Schuh | 724032b | 2020-12-18 22:54:59 -0800 | [diff] [blame] | 1744 | << before_times[i] + kTolerance |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1745 | << ") : Time changed too much on node " |
| 1746 | << MaybeNodeName(states_[i]->event_loop()->node()); |
| 1747 | } |
| 1748 | } |
| 1749 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1750 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1751 | |
| 1752 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Done sending at " |
| 1753 | << state->event_loop()->context().monotonic_event_time << " now " |
| 1754 | << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1755 | })); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1756 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1757 | ++live_nodes_; |
| 1758 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1759 | if (state->OldestMessageTime() != monotonic_clock::max_time) { |
| 1760 | event_loop->OnRun([state]() { state->Setup(state->OldestMessageTime()); }); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | void LogReader::Deregister() { |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1765 | // Make sure that things get destroyed in the correct order, rather than |
| 1766 | // relying on getting the order correct in the class definition. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1767 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1768 | state->Deregister(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1769 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1770 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1771 | event_loop_factory_unique_ptr_.reset(); |
| 1772 | event_loop_factory_ = nullptr; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1775 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1776 | std::string_view add_prefix, |
| 1777 | std::string_view new_type) { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1778 | for (size_t ii = 0; ii < logged_configuration()->channels()->size(); ++ii) { |
| 1779 | const Channel *const channel = logged_configuration()->channels()->Get(ii); |
| 1780 | if (channel->name()->str() == name && |
| 1781 | channel->type()->string_view() == type) { |
| 1782 | CHECK_EQ(0u, remapped_channels_.count(ii)) |
| 1783 | << "Already remapped channel " |
| 1784 | << configuration::CleanedChannelToString(channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1785 | RemappedChannel remapped_channel; |
| 1786 | remapped_channel.remapped_name = |
| 1787 | std::string(add_prefix) + std::string(name); |
| 1788 | remapped_channel.new_type = new_type; |
| 1789 | remapped_channels_[ii] = std::move(remapped_channel); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1790 | VLOG(1) << "Remapping channel " |
| 1791 | << configuration::CleanedChannelToString(channel) |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1792 | << " to have name " << remapped_channels_[ii].remapped_name; |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 1793 | MakeRemappedConfig(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1794 | return; |
| 1795 | } |
| 1796 | } |
| 1797 | LOG(FATAL) << "Unabled to locate channel with name " << name << " and type " |
| 1798 | << type; |
| 1799 | } |
| 1800 | |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1801 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
| 1802 | const Node *node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1803 | std::string_view add_prefix, |
| 1804 | std::string_view new_type) { |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1805 | VLOG(1) << "Node is " << aos::FlatbufferToJson(node); |
| 1806 | const Channel *remapped_channel = |
| 1807 | configuration::GetChannel(logged_configuration(), name, type, "", node); |
| 1808 | CHECK(remapped_channel != nullptr) << ": Failed to find {\"name\": \"" << name |
| 1809 | << "\", \"type\": \"" << type << "\"}"; |
| 1810 | VLOG(1) << "Original {\"name\": \"" << name << "\", \"type\": \"" << type |
| 1811 | << "\"}"; |
| 1812 | VLOG(1) << "Remapped " |
| 1813 | << aos::configuration::StrippedChannelToString(remapped_channel); |
| 1814 | |
| 1815 | // We want to make /spray on node 0 go to /0/spray by snooping the maps. And |
| 1816 | // we want it to degrade if the heuristics fail to just work. |
| 1817 | // |
| 1818 | // The easiest way to do this is going to be incredibly specific and verbose. |
| 1819 | // Look up /spray, to /0/spray. Then, prefix the result with /original to get |
| 1820 | // /original/0/spray. Then, create a map from /original/spray to |
| 1821 | // /original/0/spray for just the type we were asked for. |
| 1822 | if (name != remapped_channel->name()->string_view()) { |
| 1823 | MapT new_map; |
| 1824 | new_map.match = std::make_unique<ChannelT>(); |
| 1825 | new_map.match->name = absl::StrCat(add_prefix, name); |
| 1826 | new_map.match->type = type; |
| 1827 | if (node != nullptr) { |
| 1828 | new_map.match->source_node = node->name()->str(); |
| 1829 | } |
| 1830 | new_map.rename = std::make_unique<ChannelT>(); |
| 1831 | new_map.rename->name = |
| 1832 | absl::StrCat(add_prefix, remapped_channel->name()->string_view()); |
| 1833 | maps_.emplace_back(std::move(new_map)); |
| 1834 | } |
| 1835 | |
| 1836 | const size_t channel_index = |
| 1837 | configuration::ChannelIndex(logged_configuration(), remapped_channel); |
| 1838 | CHECK_EQ(0u, remapped_channels_.count(channel_index)) |
| 1839 | << "Already remapped channel " |
| 1840 | << configuration::CleanedChannelToString(remapped_channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1841 | |
| 1842 | RemappedChannel remapped_channel_struct; |
| 1843 | remapped_channel_struct.remapped_name = |
| 1844 | std::string(add_prefix) + |
| 1845 | std::string(remapped_channel->name()->string_view()); |
| 1846 | remapped_channel_struct.new_type = new_type; |
| 1847 | remapped_channels_[channel_index] = std::move(remapped_channel_struct); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1848 | MakeRemappedConfig(); |
| 1849 | } |
| 1850 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1851 | void LogReader::MakeRemappedConfig() { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1852 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1853 | if (state) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1854 | CHECK(!state->event_loop()) |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1855 | << ": Can't change the mapping after the events are scheduled."; |
| 1856 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1857 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1858 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1859 | // If no remapping occurred and we are using the original config, then there |
| 1860 | // is nothing interesting to do here. |
| 1861 | if (remapped_channels_.empty() && replay_configuration_ == nullptr) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1862 | remapped_configuration_ = logged_configuration(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1863 | return; |
| 1864 | } |
| 1865 | // Config to copy Channel definitions from. Use the specified |
| 1866 | // replay_configuration_ if it has been provided. |
| 1867 | const Configuration *const base_config = replay_configuration_ == nullptr |
| 1868 | ? logged_configuration() |
| 1869 | : replay_configuration_; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1870 | |
| 1871 | // Create a config with all the channels, but un-sorted/merged. Collect up |
| 1872 | // the schemas while we do this. Call MergeConfiguration to sort everything, |
| 1873 | // and then merge it all in together. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1874 | |
| 1875 | // This is the builder that we use for the config containing all the new |
| 1876 | // channels. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1877 | flatbuffers::FlatBufferBuilder fbb; |
| 1878 | fbb.ForceDefaults(true); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1879 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1880 | |
| 1881 | CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 13u) |
| 1882 | << ": Merging logic needs to be updated when the number of channel " |
| 1883 | "fields changes."; |
| 1884 | |
| 1885 | // List of schemas. |
| 1886 | std::map<std::string_view, FlatbufferVector<reflection::Schema>> schema_map; |
| 1887 | // Make sure our new RemoteMessage schema is in there for old logs without it. |
| 1888 | schema_map.insert(std::make_pair( |
| 1889 | RemoteMessage::GetFullyQualifiedName(), |
| 1890 | FlatbufferVector<reflection::Schema>(FlatbufferSpan<reflection::Schema>( |
| 1891 | message_bridge::RemoteMessageSchema())))); |
| 1892 | |
| 1893 | // Reconstruct the remapped channels. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1894 | for (auto &pair : remapped_channels_) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1895 | const Channel *const c = CHECK_NOTNULL(configuration::GetChannel( |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1896 | base_config, logged_configuration()->channels()->Get(pair.first), "", |
| 1897 | nullptr)); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1898 | channel_offsets.emplace_back( |
| 1899 | CopyChannel(c, pair.second.remapped_name, "", &fbb)); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1900 | } |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1901 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1902 | // Now reconstruct the original channels, translating types as needed |
| 1903 | for (const Channel *c : *base_config->channels()) { |
| 1904 | // Search for a mapping channel. |
| 1905 | std::string_view new_type = ""; |
| 1906 | for (auto &pair : remapped_channels_) { |
| 1907 | const Channel *const remapped_channel = |
| 1908 | logged_configuration()->channels()->Get(pair.first); |
| 1909 | if (remapped_channel->name()->string_view() == c->name()->string_view() && |
| 1910 | remapped_channel->type()->string_view() == c->type()->string_view()) { |
| 1911 | new_type = pair.second.new_type; |
| 1912 | break; |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | // Copy everything over. |
| 1917 | channel_offsets.emplace_back(CopyChannel(c, "", new_type, &fbb)); |
| 1918 | |
| 1919 | // Add the schema if it doesn't exist. |
| 1920 | if (schema_map.find(c->type()->string_view()) == schema_map.end()) { |
| 1921 | CHECK(c->has_schema()); |
| 1922 | schema_map.insert(std::make_pair(c->type()->string_view(), |
| 1923 | RecursiveCopyFlatBuffer(c->schema()))); |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | // The MergeConfiguration API takes a vector, not a map. Convert. |
| 1928 | std::vector<FlatbufferVector<reflection::Schema>> schemas; |
| 1929 | while (!schema_map.empty()) { |
| 1930 | schemas.emplace_back(std::move(schema_map.begin()->second)); |
| 1931 | schema_map.erase(schema_map.begin()); |
| 1932 | } |
| 1933 | |
| 1934 | // Create the Configuration containing the new channels that we want to add. |
| 1935 | const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 1936 | channels_offset = |
| 1937 | channel_offsets.empty() ? 0 : fbb.CreateVector(channel_offsets); |
| 1938 | |
| 1939 | // Copy over the old maps. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1940 | std::vector<flatbuffers::Offset<Map>> map_offsets; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1941 | if (base_config->maps()) { |
| 1942 | for (const Map *map : *base_config->maps()) { |
| 1943 | map_offsets.emplace_back(aos::RecursiveCopyFlatBuffer(map, &fbb)); |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | // Now create the new maps. These are second so they take effect first. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1948 | for (const MapT &map : maps_) { |
| 1949 | const flatbuffers::Offset<flatbuffers::String> match_name_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1950 | fbb.CreateString(map.match->name); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1951 | const flatbuffers::Offset<flatbuffers::String> match_type_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1952 | fbb.CreateString(map.match->type); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1953 | const flatbuffers::Offset<flatbuffers::String> rename_name_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1954 | fbb.CreateString(map.rename->name); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1955 | flatbuffers::Offset<flatbuffers::String> match_source_node_offset; |
| 1956 | if (!map.match->source_node.empty()) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1957 | match_source_node_offset = fbb.CreateString(map.match->source_node); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1958 | } |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1959 | Channel::Builder match_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1960 | match_builder.add_name(match_name_offset); |
| 1961 | match_builder.add_type(match_type_offset); |
| 1962 | if (!map.match->source_node.empty()) { |
| 1963 | match_builder.add_source_node(match_source_node_offset); |
| 1964 | } |
| 1965 | const flatbuffers::Offset<Channel> match_offset = match_builder.Finish(); |
| 1966 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1967 | Channel::Builder rename_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1968 | rename_builder.add_name(rename_name_offset); |
| 1969 | const flatbuffers::Offset<Channel> rename_offset = rename_builder.Finish(); |
| 1970 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1971 | Map::Builder map_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1972 | map_builder.add_match(match_offset); |
| 1973 | map_builder.add_rename(rename_offset); |
| 1974 | map_offsets.emplace_back(map_builder.Finish()); |
| 1975 | } |
| 1976 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1977 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 1978 | maps_offsets = map_offsets.empty() ? 0 : fbb.CreateVector(map_offsets); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1979 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1980 | // And copy everything else over. |
| 1981 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 1982 | nodes_offset = aos::RecursiveCopyVectorTable(base_config->nodes(), &fbb); |
| 1983 | |
| 1984 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 1985 | applications_offset = |
| 1986 | aos::RecursiveCopyVectorTable(base_config->applications(), &fbb); |
| 1987 | |
| 1988 | // Now insert everything else in unmodified. |
| 1989 | ConfigurationBuilder configuration_builder(fbb); |
| 1990 | if (!channels_offset.IsNull()) { |
| 1991 | configuration_builder.add_channels(channels_offset); |
| 1992 | } |
| 1993 | if (!maps_offsets.IsNull()) { |
| 1994 | configuration_builder.add_maps(maps_offsets); |
| 1995 | } |
| 1996 | if (!nodes_offset.IsNull()) { |
| 1997 | configuration_builder.add_nodes(nodes_offset); |
| 1998 | } |
| 1999 | if (!applications_offset.IsNull()) { |
| 2000 | configuration_builder.add_applications(applications_offset); |
| 2001 | } |
| 2002 | |
| 2003 | if (base_config->has_channel_storage_duration()) { |
| 2004 | configuration_builder.add_channel_storage_duration( |
| 2005 | base_config->channel_storage_duration()); |
| 2006 | } |
| 2007 | |
| 2008 | CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u) |
| 2009 | << ": Merging logic needs to be updated when the number of configuration " |
| 2010 | "fields changes."; |
| 2011 | |
| 2012 | fbb.Finish(configuration_builder.Finish()); |
| 2013 | |
| 2014 | // Clean it up and return it! By using MergeConfiguration here, we'll |
| 2015 | // actually get a deduplicated config for free too. |
| 2016 | FlatbufferDetachedBuffer<Configuration> new_merged_config = |
| 2017 | configuration::MergeConfiguration( |
| 2018 | FlatbufferDetachedBuffer<Configuration>(fbb.Release())); |
| 2019 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 2020 | remapped_configuration_buffer_ = |
| 2021 | std::make_unique<FlatbufferDetachedBuffer<Configuration>>( |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 2022 | configuration::MergeConfiguration(new_merged_config, schemas)); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 2023 | |
| 2024 | remapped_configuration_ = &remapped_configuration_buffer_->message(); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 2025 | |
| 2026 | // TODO(austin): Lazily re-build to save CPU? |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 2027 | } |
| 2028 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 2029 | const Channel *LogReader::RemapChannel(const EventLoop *event_loop, |
| 2030 | const Channel *channel) { |
| 2031 | std::string_view channel_name = channel->name()->string_view(); |
| 2032 | std::string_view channel_type = channel->type()->string_view(); |
| 2033 | const int channel_index = |
| 2034 | configuration::ChannelIndex(logged_configuration(), channel); |
| 2035 | // If the channel is remapped, find the correct channel name to use. |
| 2036 | if (remapped_channels_.count(channel_index) > 0) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 2037 | VLOG(3) << "Got remapped channel on " |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 2038 | << configuration::CleanedChannelToString(channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 2039 | channel_name = remapped_channels_[channel_index].remapped_name; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 2040 | } |
| 2041 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 2042 | VLOG(2) << "Going to remap channel " << channel_name << " " << channel_type; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 2043 | const Channel *remapped_channel = configuration::GetChannel( |
| 2044 | event_loop->configuration(), channel_name, channel_type, |
| 2045 | event_loop->name(), event_loop->node()); |
| 2046 | |
| 2047 | CHECK(remapped_channel != nullptr) |
| 2048 | << ": Unable to send {\"name\": \"" << channel_name << "\", \"type\": \"" |
| 2049 | << channel_type << "\"} because it is not in the provided configuration."; |
| 2050 | |
| 2051 | return remapped_channel; |
| 2052 | } |
| 2053 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2054 | LogReader::State::State(std::unique_ptr<TimestampMapper> timestamp_mapper) |
| 2055 | : timestamp_mapper_(std::move(timestamp_mapper)) {} |
| 2056 | |
| 2057 | void LogReader::State::AddPeer(State *peer) { |
| 2058 | if (timestamp_mapper_ && peer->timestamp_mapper_) { |
| 2059 | timestamp_mapper_->AddPeer(peer->timestamp_mapper_.get()); |
| 2060 | } |
| 2061 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2062 | |
| 2063 | EventLoop *LogReader::State::SetNodeEventLoopFactory( |
| 2064 | NodeEventLoopFactory *node_event_loop_factory) { |
| 2065 | node_event_loop_factory_ = node_event_loop_factory; |
| 2066 | event_loop_unique_ptr_ = |
| 2067 | node_event_loop_factory_->MakeEventLoop("log_reader"); |
| 2068 | return event_loop_unique_ptr_.get(); |
| 2069 | } |
| 2070 | |
| 2071 | void LogReader::State::SetChannelCount(size_t count) { |
| 2072 | channels_.resize(count); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2073 | remote_timestamp_senders_.resize(count); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2074 | filters_.resize(count); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2075 | channel_source_state_.resize(count); |
| 2076 | factory_channel_index_.resize(count); |
| 2077 | queue_index_map_.resize(count); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | void LogReader::State::SetChannel( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2081 | size_t logged_channel_index, size_t factory_channel_index, |
| 2082 | std::unique_ptr<RawSender> sender, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 2083 | message_bridge::NoncausalOffsetEstimator *filter, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 2084 | aos::Sender<RemoteMessage> *remote_timestamp_sender, State *source_state) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2085 | channels_[logged_channel_index] = std::move(sender); |
| 2086 | filters_[logged_channel_index] = filter; |
| 2087 | remote_timestamp_senders_[logged_channel_index] = remote_timestamp_sender; |
| 2088 | |
| 2089 | if (source_state) { |
| 2090 | channel_source_state_[logged_channel_index] = source_state; |
| 2091 | |
| 2092 | if (remote_timestamp_sender != nullptr) { |
| 2093 | source_state->queue_index_map_[logged_channel_index] = |
| 2094 | std::make_unique<std::vector<State::SentTimestamp>>(); |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | factory_channel_index_[logged_channel_index] = factory_channel_index; |
| 2099 | } |
| 2100 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2101 | bool LogReader::State::Send(const TimestampedMessage ×tamped_message) { |
| 2102 | aos::RawSender *sender = channels_[timestamped_message.channel_index].get(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2103 | uint32_t remote_queue_index = 0xffffffff; |
| 2104 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2105 | if (remote_timestamp_senders_[timestamped_message.channel_index] != nullptr) { |
| 2106 | std::vector<SentTimestamp> *queue_index_map = CHECK_NOTNULL( |
| 2107 | CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]) |
| 2108 | ->queue_index_map_[timestamped_message.channel_index] |
| 2109 | .get()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2110 | |
| 2111 | SentTimestamp search; |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2112 | search.monotonic_event_time = timestamped_message.monotonic_remote_time; |
| 2113 | search.realtime_event_time = timestamped_message.realtime_remote_time; |
| 2114 | search.queue_index = timestamped_message.remote_queue_index; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2115 | |
| 2116 | // Find the sent time if available. |
| 2117 | auto element = std::lower_bound( |
| 2118 | queue_index_map->begin(), queue_index_map->end(), search, |
| 2119 | [](SentTimestamp a, SentTimestamp b) { |
| 2120 | if (b.monotonic_event_time < a.monotonic_event_time) { |
| 2121 | return false; |
| 2122 | } |
| 2123 | if (b.monotonic_event_time > a.monotonic_event_time) { |
| 2124 | return true; |
| 2125 | } |
| 2126 | |
| 2127 | if (b.queue_index < a.queue_index) { |
| 2128 | return false; |
| 2129 | } |
| 2130 | if (b.queue_index > a.queue_index) { |
| 2131 | return true; |
| 2132 | } |
| 2133 | |
| 2134 | CHECK_EQ(a.realtime_event_time, b.realtime_event_time); |
| 2135 | return false; |
| 2136 | }); |
| 2137 | |
| 2138 | // TODO(austin): Be a bit more principled here, but we will want to do that |
| 2139 | // after the logger rewrite. We hit this when one node finishes, but the |
| 2140 | // other node isn't done yet. So there is no send time, but there is a |
| 2141 | // receive time. |
| 2142 | if (element != queue_index_map->end()) { |
| 2143 | CHECK_EQ(element->monotonic_event_time, |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2144 | timestamped_message.monotonic_remote_time); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2145 | CHECK_EQ(element->realtime_event_time, |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2146 | timestamped_message.realtime_remote_time); |
| 2147 | CHECK_EQ(element->queue_index, timestamped_message.remote_queue_index); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2148 | |
| 2149 | remote_queue_index = element->actual_queue_index; |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | // Send! Use the replayed queue index here instead of the logged queue index |
| 2154 | // for the remote queue index. This makes re-logging work. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2155 | const bool sent = sender->Send( |
| 2156 | timestamped_message.data.message().data()->Data(), |
| 2157 | timestamped_message.data.message().data()->size(), |
| 2158 | timestamped_message.monotonic_remote_time, |
| 2159 | timestamped_message.realtime_remote_time, remote_queue_index); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2160 | if (!sent) return false; |
| 2161 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2162 | if (queue_index_map_[timestamped_message.channel_index]) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2163 | SentTimestamp timestamp; |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2164 | timestamp.monotonic_event_time = timestamped_message.monotonic_event_time; |
| 2165 | timestamp.realtime_event_time = timestamped_message.realtime_event_time; |
| 2166 | timestamp.queue_index = timestamped_message.queue_index; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2167 | timestamp.actual_queue_index = sender->sent_queue_index(); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2168 | queue_index_map_[timestamped_message.channel_index]->emplace_back( |
| 2169 | timestamp); |
| 2170 | } else if (remote_timestamp_senders_[timestamped_message.channel_index] != |
| 2171 | nullptr) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 2172 | aos::Sender<RemoteMessage>::Builder builder = |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2173 | remote_timestamp_senders_[timestamped_message.channel_index] |
| 2174 | ->MakeBuilder(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2175 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 2176 | flatbuffers::Offset<flatbuffers::String> boot_uuid_offset = |
| 2177 | builder.fbb()->CreateString(event_loop_->boot_uuid().string_view()); |
| 2178 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 2179 | RemoteMessage::Builder message_header_builder = |
| 2180 | builder.MakeBuilder<RemoteMessage>(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2181 | |
| 2182 | message_header_builder.add_channel_index( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2183 | factory_channel_index_[timestamped_message.channel_index]); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2184 | |
| 2185 | // Swap the remote and sent metrics. They are from the sender's |
| 2186 | // perspective, not the receiver's perspective. |
| 2187 | message_header_builder.add_monotonic_sent_time( |
| 2188 | sender->monotonic_sent_time().time_since_epoch().count()); |
| 2189 | message_header_builder.add_realtime_sent_time( |
| 2190 | sender->realtime_sent_time().time_since_epoch().count()); |
| 2191 | message_header_builder.add_queue_index(sender->sent_queue_index()); |
| 2192 | |
| 2193 | message_header_builder.add_monotonic_remote_time( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2194 | timestamped_message.monotonic_remote_time.time_since_epoch().count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2195 | message_header_builder.add_realtime_remote_time( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2196 | timestamped_message.realtime_remote_time.time_since_epoch().count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2197 | |
| 2198 | message_header_builder.add_remote_queue_index(remote_queue_index); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame^] | 2199 | message_header_builder.add_boot_uuid(boot_uuid_offset); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2200 | |
| 2201 | builder.Send(message_header_builder.Finish()); |
| 2202 | } |
| 2203 | |
| 2204 | return true; |
| 2205 | } |
| 2206 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 2207 | aos::Sender<RemoteMessage> *LogReader::State::RemoteTimestampSender( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2208 | const Node *delivered_node) { |
| 2209 | auto sender = remote_timestamp_senders_map_.find(delivered_node); |
| 2210 | |
| 2211 | if (sender == remote_timestamp_senders_map_.end()) { |
| 2212 | sender = remote_timestamp_senders_map_ |
| 2213 | .emplace(std::make_pair( |
| 2214 | delivered_node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 2215 | event_loop()->MakeSender<RemoteMessage>( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2216 | absl::StrCat("/aos/remote_timestamps/", |
| 2217 | delivered_node->name()->string_view())))) |
| 2218 | .first; |
| 2219 | } |
| 2220 | |
| 2221 | return &(sender->second); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2222 | } |
| 2223 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2224 | TimestampedMessage LogReader::State::PopOldest(bool *update_time) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2225 | CHECK_GT(sorted_messages_.size(), 0u); |
| 2226 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2227 | std::tuple<TimestampedMessage, message_bridge::NoncausalOffsetEstimator *> |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2228 | result = std::move(sorted_messages_.front()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 2229 | VLOG(2) << MaybeNodeName(event_loop_->node()) << "PopOldest Popping " |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2230 | << std::get<0>(result).monotonic_event_time; |
| 2231 | sorted_messages_.pop_front(); |
| 2232 | SeedSortedMessages(); |
| 2233 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2234 | if (std::get<1>(result) != nullptr) { |
| 2235 | *update_time = std::get<1>(result)->Pop( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 2236 | event_loop_->node(), std::get<0>(result).monotonic_event_time); |
| 2237 | } else { |
| 2238 | *update_time = false; |
| 2239 | } |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2240 | return std::move(std::get<0>(result)); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | monotonic_clock::time_point LogReader::State::OldestMessageTime() const { |
| 2244 | if (sorted_messages_.size() > 0) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 2245 | VLOG(2) << MaybeNodeName(event_loop_->node()) << "oldest message at " |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2246 | << std::get<0>(sorted_messages_.front()).monotonic_event_time; |
| 2247 | return std::get<0>(sorted_messages_.front()).monotonic_event_time; |
| 2248 | } |
| 2249 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2250 | TimestampedMessage *m = |
| 2251 | timestamp_mapper_ ? timestamp_mapper_->Front() : nullptr; |
| 2252 | if (m == nullptr) { |
| 2253 | return monotonic_clock::max_time; |
| 2254 | } |
| 2255 | return m->monotonic_event_time; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | void LogReader::State::SeedSortedMessages() { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2259 | if (!timestamp_mapper_) return; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2260 | const aos::monotonic_clock::time_point end_queue_time = |
| 2261 | (sorted_messages_.size() > 0 |
| 2262 | ? std::get<0>(sorted_messages_.front()).monotonic_event_time |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2263 | : timestamp_mapper_->monotonic_start_time()) + |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2264 | std::chrono::seconds(2); |
| 2265 | |
| 2266 | while (true) { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2267 | TimestampedMessage *m = timestamp_mapper_->Front(); |
| 2268 | if (m == nullptr) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2269 | return; |
| 2270 | } |
| 2271 | if (sorted_messages_.size() > 0) { |
| 2272 | // Stop placing sorted messages on the list once we have 2 seconds |
| 2273 | // queued up (but queue at least until the log starts. |
| 2274 | if (end_queue_time < |
| 2275 | std::get<0>(sorted_messages_.back()).monotonic_event_time) { |
| 2276 | return; |
| 2277 | } |
| 2278 | } |
| 2279 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 2280 | message_bridge::NoncausalOffsetEstimator *filter = nullptr; |
| 2281 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2282 | TimestampedMessage timestamped_message = std::move(*m); |
| 2283 | timestamp_mapper_->PopFront(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2284 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 2285 | // Skip any messages without forwarding information. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 2286 | if (timestamped_message.monotonic_remote_time != |
| 2287 | monotonic_clock::min_time) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 2288 | // Got a forwarding timestamp! |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2289 | filter = filters_[timestamped_message.channel_index]; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 2290 | |
| 2291 | CHECK(filter != nullptr); |
| 2292 | |
| 2293 | // Call the correct method depending on if we are the forward or |
| 2294 | // reverse direction here. |
| 2295 | filter->Sample(event_loop_->node(), |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2296 | timestamped_message.monotonic_event_time, |
| 2297 | timestamped_message.monotonic_remote_time); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 2298 | } |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2299 | sorted_messages_.emplace_back(std::move(timestamped_message), filter); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | void LogReader::State::Deregister() { |
| 2304 | for (size_t i = 0; i < channels_.size(); ++i) { |
| 2305 | channels_[i].reset(); |
| 2306 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2307 | remote_timestamp_senders_map_.clear(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2308 | event_loop_unique_ptr_.reset(); |
| 2309 | event_loop_ = nullptr; |
| 2310 | timer_handler_ = nullptr; |
| 2311 | node_event_loop_factory_ = nullptr; |
| 2312 | } |
| 2313 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 2314 | } // namespace logger |
| 2315 | } // namespace aos |