Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 1 | #include "aos/events/logging/log_reader.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> |
Brian Silverman | 8ff74aa | 2021-02-05 16:37:15 -0800 | [diff] [blame] | 8 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
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 | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 16 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 17 | #include "aos/network/multinode_timestamp_filter.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 | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 21 | #include "aos/network/timestamp_channel.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 22 | #include "aos/time/time.h" |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 23 | #include "aos/util/file.h" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 24 | #include "aos/uuid.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 25 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 26 | #include "openssl/sha.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 27 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 28 | DEFINE_bool(skip_missing_forwarding_entries, false, |
| 29 | "If true, drop any forwarding entries with missing data. If " |
| 30 | "false, CHECK."); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 31 | |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 32 | DECLARE_bool(timestamps_to_csv); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 33 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 34 | DEFINE_bool(skip_order_validation, false, |
| 35 | "If true, ignore any out of orderness in replay"); |
| 36 | |
Austin Schuh | f068866 | 2020-12-19 15:37:45 -0800 | [diff] [blame] | 37 | DEFINE_double( |
| 38 | time_estimation_buffer_seconds, 2.0, |
| 39 | "The time to buffer ahead in the log file to accurately reconstruct time."); |
| 40 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 41 | namespace aos { |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame^] | 42 | namespace configuration { |
| 43 | // We don't really want to expose this publicly, but log reader doesn't really |
| 44 | // want to re-implement it. |
| 45 | void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps, |
| 46 | std::string *name, std::string_view type, const Node *node); |
| 47 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 48 | namespace logger { |
Austin Schuh | 0afc4d1 | 2020-10-19 11:42:04 -0700 | [diff] [blame] | 49 | namespace { |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 50 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 51 | std::string LogFileVectorToString(std::vector<LogFile> log_files) { |
| 52 | std::stringstream ss; |
Austin Schuh | 297d235 | 2021-01-21 19:02:17 -0800 | [diff] [blame] | 53 | for (const auto &f : log_files) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 54 | ss << f << "\n"; |
| 55 | } |
| 56 | return ss.str(); |
| 57 | } |
| 58 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 59 | // Copies the channel, removing the schema as we go. If new_name is provided, |
| 60 | // it is used instead of the name inside the channel. If new_type is provided, |
| 61 | // it is used instead of the type in the channel. |
| 62 | flatbuffers::Offset<Channel> CopyChannel(const Channel *c, |
| 63 | std::string_view new_name, |
| 64 | std::string_view new_type, |
| 65 | flatbuffers::FlatBufferBuilder *fbb) { |
| 66 | flatbuffers::Offset<flatbuffers::String> name_offset = |
| 67 | fbb->CreateSharedString(new_name.empty() ? c->name()->string_view() |
| 68 | : new_name); |
| 69 | flatbuffers::Offset<flatbuffers::String> type_offset = |
| 70 | fbb->CreateSharedString(new_type.empty() ? c->type()->str() : new_type); |
| 71 | flatbuffers::Offset<flatbuffers::String> source_node_offset = |
| 72 | c->has_source_node() ? fbb->CreateSharedString(c->source_node()->str()) |
| 73 | : 0; |
| 74 | |
| 75 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>> |
| 76 | destination_nodes_offset = |
| 77 | aos::RecursiveCopyVectorTable(c->destination_nodes(), fbb); |
| 78 | |
| 79 | flatbuffers::Offset< |
| 80 | flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> |
| 81 | logger_nodes_offset = aos::CopyVectorSharedString(c->logger_nodes(), fbb); |
| 82 | |
| 83 | Channel::Builder channel_builder(*fbb); |
| 84 | channel_builder.add_name(name_offset); |
| 85 | channel_builder.add_type(type_offset); |
| 86 | if (c->has_frequency()) { |
| 87 | channel_builder.add_frequency(c->frequency()); |
| 88 | } |
| 89 | if (c->has_max_size()) { |
| 90 | channel_builder.add_max_size(c->max_size()); |
| 91 | } |
| 92 | if (c->has_num_senders()) { |
| 93 | channel_builder.add_num_senders(c->num_senders()); |
| 94 | } |
| 95 | if (c->has_num_watchers()) { |
| 96 | channel_builder.add_num_watchers(c->num_watchers()); |
| 97 | } |
| 98 | if (!source_node_offset.IsNull()) { |
| 99 | channel_builder.add_source_node(source_node_offset); |
| 100 | } |
| 101 | if (!destination_nodes_offset.IsNull()) { |
| 102 | channel_builder.add_destination_nodes(destination_nodes_offset); |
| 103 | } |
| 104 | if (c->has_logger()) { |
| 105 | channel_builder.add_logger(c->logger()); |
| 106 | } |
| 107 | if (!logger_nodes_offset.IsNull()) { |
| 108 | channel_builder.add_logger_nodes(logger_nodes_offset); |
| 109 | } |
| 110 | if (c->has_read_method()) { |
| 111 | channel_builder.add_read_method(c->read_method()); |
| 112 | } |
| 113 | if (c->has_num_readers()) { |
| 114 | channel_builder.add_num_readers(c->num_readers()); |
| 115 | } |
| 116 | return channel_builder.Finish(); |
| 117 | } |
| 118 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 119 | namespace chrono = std::chrono; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 120 | using message_bridge::RemoteMessage; |
Austin Schuh | 0afc4d1 | 2020-10-19 11:42:04 -0700 | [diff] [blame] | 121 | } // namespace |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 122 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 123 | LogReader::LogReader(std::string_view filename, |
| 124 | const Configuration *replay_configuration) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 125 | : LogReader(SortParts({std::string(filename)}), replay_configuration) {} |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 126 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 127 | LogReader::LogReader(std::vector<LogFile> log_files, |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 128 | const Configuration *replay_configuration) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 129 | : log_files_(std::move(log_files)), |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 130 | replay_configuration_(replay_configuration) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 131 | CHECK_GT(log_files_.size(), 0u); |
| 132 | { |
| 133 | // Validate that we have the same config everwhere. This will be true if |
| 134 | // all the parts were sorted together and the configs match. |
| 135 | const Configuration *config = nullptr; |
Austin Schuh | 297d235 | 2021-01-21 19:02:17 -0800 | [diff] [blame] | 136 | for (const LogFile &log_file : log_files_) { |
| 137 | if (log_file.config.get() == nullptr) { |
| 138 | LOG(FATAL) << "Couldn't find a config in " << log_file; |
| 139 | } |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 140 | if (config == nullptr) { |
| 141 | config = log_file.config.get(); |
| 142 | } else { |
| 143 | CHECK_EQ(config, log_file.config.get()); |
| 144 | } |
| 145 | } |
| 146 | } |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 147 | |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 148 | MakeRemappedConfig(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 149 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 150 | // Remap all existing remote timestamp channels. They will be recreated, and |
| 151 | // the data logged isn't relevant anymore. |
Austin Schuh | 3c5dae5 | 2020-10-06 18:55:18 -0700 | [diff] [blame] | 152 | for (const Node *node : configuration::GetNodes(logged_configuration())) { |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 153 | message_bridge::ChannelTimestampFinder finder(logged_configuration(), |
| 154 | "log_reader", node); |
| 155 | |
| 156 | absl::btree_set<std::string_view> remote_nodes; |
| 157 | |
| 158 | for (const Channel *channel : *logged_configuration()->channels()) { |
| 159 | if (!configuration::ChannelIsSendableOnNode(channel, node)) { |
| 160 | continue; |
| 161 | } |
| 162 | if (!channel->has_destination_nodes()) { |
| 163 | continue; |
| 164 | } |
| 165 | for (const Connection *connection : *channel->destination_nodes()) { |
| 166 | if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection, |
| 167 | node)) { |
| 168 | // Start by seeing if the split timestamp channels are being used for |
| 169 | // this message. If so, remap them. |
| 170 | const Channel *timestamp_channel = configuration::GetChannel( |
| 171 | logged_configuration(), |
| 172 | finder.SplitChannelName(channel, connection), |
| 173 | RemoteMessage::GetFullyQualifiedName(), "", node, true); |
| 174 | |
| 175 | if (timestamp_channel != nullptr) { |
| 176 | if (timestamp_channel->logger() != LoggerConfig::NOT_LOGGED) { |
| 177 | RemapLoggedChannel<RemoteMessage>( |
| 178 | timestamp_channel->name()->string_view(), node); |
| 179 | } |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | // Otherwise collect this one up as a node to look for a combined |
| 184 | // channel from. It is more efficient to compare nodes than channels. |
| 185 | remote_nodes.insert(connection->name()->string_view()); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 190 | std::vector<const Node *> timestamp_logger_nodes = |
| 191 | configuration::TimestampNodes(logged_configuration(), node); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 192 | for (const std::string_view remote_node : remote_nodes) { |
| 193 | const std::string channel = finder.CombinedChannelName(remote_node); |
| 194 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 195 | // See if the log file is an old log with MessageHeader channels in it, or |
| 196 | // a newer log with RemoteMessage. If we find an older log, rename the |
| 197 | // type too along with the name. |
| 198 | if (HasChannel<MessageHeader>(channel, node)) { |
| 199 | CHECK(!HasChannel<RemoteMessage>(channel, node)) |
| 200 | << ": Can't have both a MessageHeader and RemoteMessage remote " |
| 201 | "timestamp channel."; |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 202 | // In theory, we should check NOT_LOGGED like RemoteMessage and be more |
| 203 | // careful about updating the config, but there are fewer and fewer logs |
| 204 | // with MessageHeader remote messages, so it isn't worth the effort. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 205 | RemapLoggedChannel<MessageHeader>(channel, node, "/original", |
| 206 | "aos.message_bridge.RemoteMessage"); |
| 207 | } else { |
| 208 | CHECK(HasChannel<RemoteMessage>(channel, node)) |
| 209 | << ": Failed to find {\"name\": \"" << channel << "\", \"type\": \"" |
| 210 | << RemoteMessage::GetFullyQualifiedName() << "\"} for node " |
| 211 | << node->name()->string_view(); |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 212 | // Only bother to remap if there's something on the channel. We can |
| 213 | // tell if the channel was marked NOT_LOGGED or not. This makes the |
| 214 | // config not change un-necesarily when we replay a log with NOT_LOGGED |
| 215 | // messages. |
| 216 | if (HasLoggedChannel<RemoteMessage>(channel, node)) { |
| 217 | RemapLoggedChannel<RemoteMessage>(channel, node); |
| 218 | } |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 219 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 223 | if (replay_configuration) { |
| 224 | CHECK_EQ(configuration::MultiNode(configuration()), |
| 225 | configuration::MultiNode(replay_configuration)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 226 | << ": Log file and replay config need to both be multi or single " |
| 227 | "node."; |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 230 | if (!configuration::MultiNode(configuration())) { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 231 | states_.emplace_back(std::make_unique<State>( |
| 232 | std::make_unique<TimestampMapper>(FilterPartsForNode(log_files_, "")))); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 233 | } else { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 234 | if (replay_configuration) { |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 235 | CHECK_EQ(logged_configuration()->nodes()->size(), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 236 | replay_configuration->nodes()->size()) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 237 | << ": Log file and replay config need to have matching nodes " |
| 238 | "lists."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 239 | for (const Node *node : *logged_configuration()->nodes()) { |
| 240 | if (configuration::GetNode(replay_configuration, node) == nullptr) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 241 | LOG(FATAL) << "Found node " << FlatbufferToJson(node) |
| 242 | << " in logged config that is not present in the replay " |
| 243 | "config."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 246 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 247 | states_.resize(configuration()->nodes()->size()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 248 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 251 | LogReader::~LogReader() { |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 252 | if (event_loop_factory_unique_ptr_) { |
| 253 | Deregister(); |
| 254 | } else if (event_loop_factory_ != nullptr) { |
| 255 | LOG(FATAL) << "Must call Deregister before the SimulatedEventLoopFactory " |
| 256 | "is destroyed"; |
| 257 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 258 | // Zero out some buffers. It's easy to do use-after-frees on these, so make |
| 259 | // it more obvious. |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 260 | if (remapped_configuration_buffer_) { |
| 261 | remapped_configuration_buffer_->Wipe(); |
| 262 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 263 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 264 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 265 | const Configuration *LogReader::logged_configuration() const { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 266 | return log_files_[0].config.get(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 267 | } |
| 268 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 269 | const Configuration *LogReader::configuration() const { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 270 | return remapped_configuration_; |
| 271 | } |
| 272 | |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 273 | std::vector<const Node *> LogReader::LoggedNodes() const { |
| 274 | return configuration::GetNodes(logged_configuration()); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 275 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 276 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 277 | monotonic_clock::time_point LogReader::monotonic_start_time( |
| 278 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 279 | State *state = |
| 280 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 281 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 282 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 283 | return state->monotonic_start_time(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 286 | realtime_clock::time_point LogReader::realtime_start_time( |
| 287 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 288 | State *state = |
| 289 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 290 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 291 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 292 | return state->realtime_start_time(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 293 | } |
| 294 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 295 | void LogReader::Register() { |
| 296 | event_loop_factory_unique_ptr_ = |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 297 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 298 | Register(event_loop_factory_unique_ptr_.get()); |
| 299 | } |
| 300 | |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 301 | void LogReader::Register(SimulatedEventLoopFactory *event_loop_factory) { |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 302 | event_loop_factory_ = event_loop_factory; |
Austin Schuh | e5bbd9e | 2020-09-21 17:29:20 -0700 | [diff] [blame] | 303 | remapped_configuration_ = event_loop_factory_->configuration(); |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 304 | filters_ = |
| 305 | std::make_unique<message_bridge::MultiNodeNoncausalOffsetEstimator>( |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 306 | event_loop_factory_->configuration(), logged_configuration(), |
Austin Schuh | fe3fb34 | 2021-01-16 18:50:37 -0800 | [diff] [blame] | 307 | FLAGS_skip_order_validation, |
| 308 | chrono::duration_cast<chrono::nanoseconds>( |
| 309 | chrono::duration<double>(FLAGS_time_estimation_buffer_seconds))); |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 310 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 311 | std::vector<TimestampMapper *> timestamp_mappers; |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 312 | for (const Node *node : configuration::GetNodes(configuration())) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 313 | const size_t node_index = |
| 314 | configuration::GetNodeIndex(configuration(), node); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 315 | std::vector<LogParts> filtered_parts = FilterPartsForNode( |
| 316 | log_files_, node != nullptr ? node->name()->string_view() : ""); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 317 | |
| 318 | // Confirm that all the parts are from the same boot if there are enough |
| 319 | // parts to not be from the same boot. |
| 320 | if (filtered_parts.size() > 1u) { |
| 321 | for (size_t i = 1; i < filtered_parts.size(); ++i) { |
| 322 | CHECK_EQ(filtered_parts[i].source_boot_uuid, |
| 323 | filtered_parts[0].source_boot_uuid) |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 324 | << ": Found parts from different boots for node " |
| 325 | << node->name()->string_view() << " " |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 326 | << LogFileVectorToString(log_files_); |
| 327 | } |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 328 | if (!filtered_parts[0].source_boot_uuid.empty()) { |
| 329 | event_loop_factory_->GetNodeEventLoopFactory(node)->set_boot_uuid( |
| 330 | filtered_parts[0].source_boot_uuid); |
| 331 | } |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 334 | states_[node_index] = std::make_unique<State>( |
| 335 | filtered_parts.size() == 0u |
| 336 | ? nullptr |
| 337 | : std::make_unique<TimestampMapper>(std::move(filtered_parts))); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 338 | State *state = states_[node_index].get(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 339 | state->set_event_loop(state->SetNodeEventLoopFactory( |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 340 | event_loop_factory_->GetNodeEventLoopFactory(node))); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 341 | |
| 342 | state->SetChannelCount(logged_configuration()->channels()->size()); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 343 | timestamp_mappers.emplace_back(state->timestamp_mapper()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 344 | } |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 345 | filters_->SetTimestampMappers(std::move(timestamp_mappers)); |
| 346 | |
| 347 | // Note: this needs to be set before any times are pulled, or we won't observe |
| 348 | // the timestamps. |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 349 | event_loop_factory_->SetTimeConverter(filters_.get()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 350 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 351 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 352 | const size_t node_index = |
| 353 | configuration::GetNodeIndex(configuration(), node); |
| 354 | State *state = states_[node_index].get(); |
| 355 | for (const Node *other_node : configuration::GetNodes(configuration())) { |
| 356 | const size_t other_node_index = |
| 357 | configuration::GetNodeIndex(configuration(), other_node); |
| 358 | State *other_state = states_[other_node_index].get(); |
| 359 | if (other_state != state) { |
| 360 | state->AddPeer(other_state); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 365 | // Register after making all the State objects so we can build references |
| 366 | // between them. |
| 367 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 368 | const size_t node_index = |
| 369 | configuration::GetNodeIndex(configuration(), node); |
| 370 | State *state = states_[node_index].get(); |
| 371 | |
| 372 | Register(state->event_loop()); |
| 373 | } |
| 374 | |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 375 | if (live_nodes_ == 0) { |
| 376 | LOG(FATAL) |
| 377 | << "Don't have logs from any of the nodes in the replay config--are " |
| 378 | "you sure that the replay config matches the original config?"; |
| 379 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 380 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 381 | filters_->CheckGraph(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 382 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 383 | for (std::unique_ptr<State> &state : states_) { |
| 384 | state->SeedSortedMessages(); |
| 385 | } |
| 386 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 387 | // We want to start the log file at the last start time of the log files |
| 388 | // from all the nodes. Compute how long each node's simulation needs to run |
| 389 | // to move time to this point. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 390 | distributed_clock::time_point start_time = distributed_clock::min_time; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 391 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 392 | // TODO(austin): We want an "OnStart" callback for each node rather than |
| 393 | // running until the last node. |
| 394 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 395 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 396 | VLOG(1) << "Start time is " << state->monotonic_start_time() << " for node " |
| 397 | << MaybeNodeName(state->event_loop()->node()) << "now " |
| 398 | << state->monotonic_now(); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 399 | if (state->monotonic_start_time() == monotonic_clock::min_time) { |
| 400 | continue; |
| 401 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 402 | // And start computing the start time on the distributed clock now that |
| 403 | // that works. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 404 | start_time = std::max( |
| 405 | start_time, state->ToDistributedClock(state->monotonic_start_time())); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 406 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 407 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 408 | // TODO(austin): If a node doesn't have a start time, we might not queue |
| 409 | // enough. If this happens, we'll explode with a frozen error eventually. |
| 410 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 411 | CHECK_GE(start_time, distributed_clock::epoch()) |
| 412 | << ": Hmm, we have a node starting before the start of time. Offset " |
| 413 | "everything."; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 414 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 415 | // Forwarding is tracked per channel. If it is enabled, we want to turn it |
| 416 | // off. Otherwise messages replayed will get forwarded across to the other |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 417 | // nodes, and also replayed on the other nodes. This may not satisfy all |
| 418 | // our users, but it'll start the discussion. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 419 | if (configuration::MultiNode(event_loop_factory_->configuration())) { |
| 420 | for (size_t i = 0; i < logged_configuration()->channels()->size(); ++i) { |
| 421 | const Channel *channel = logged_configuration()->channels()->Get(i); |
| 422 | const Node *node = configuration::GetNode( |
| 423 | configuration(), channel->source_node()->string_view()); |
| 424 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 425 | State *state = |
| 426 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 427 | |
| 428 | const Channel *remapped_channel = |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 429 | RemapChannel(state->event_loop(), channel); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 430 | |
| 431 | event_loop_factory_->DisableForwarding(remapped_channel); |
| 432 | } |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 433 | |
| 434 | // If we are replaying a log, we don't want a bunch of redundant messages |
| 435 | // from both the real message bridge and simulated message bridge. |
| 436 | event_loop_factory_->DisableStatistics(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 439 | // While we are starting the system up, we might be relying on matching data |
| 440 | // to timestamps on log files where the timestamp log file starts before the |
| 441 | // data. In this case, it is reasonable to expect missing data. |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 442 | { |
| 443 | const bool prior_ignore_missing_data = ignore_missing_data_; |
| 444 | ignore_missing_data_ = true; |
| 445 | VLOG(1) << "Running until " << start_time << " in Register"; |
| 446 | event_loop_factory_->RunFor(start_time.time_since_epoch()); |
| 447 | VLOG(1) << "At start time"; |
| 448 | // Now that we are running for real, missing data means that the log file is |
| 449 | // corrupted or went wrong. |
| 450 | ignore_missing_data_ = prior_ignore_missing_data; |
| 451 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 452 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 453 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 454 | // Make the RT clock be correct before handing it to the user. |
| 455 | if (state->realtime_start_time() != realtime_clock::min_time) { |
| 456 | state->SetRealtimeOffset(state->monotonic_start_time(), |
| 457 | state->realtime_start_time()); |
| 458 | } |
| 459 | VLOG(1) << "Start time is " << state->monotonic_start_time() << " for node " |
| 460 | << MaybeNodeName(state->event_loop()->node()) << "now " |
| 461 | << state->monotonic_now(); |
| 462 | } |
| 463 | |
| 464 | if (FLAGS_timestamps_to_csv) { |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 465 | filters_->Start(event_loop_factory); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 469 | message_bridge::NoncausalOffsetEstimator *LogReader::GetFilter( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 470 | const Node *node_a, const Node *node_b) { |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 471 | if (filters_) { |
| 472 | return filters_->GetFilter(node_a, node_b); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 473 | } |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 474 | return nullptr; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 477 | void LogReader::Register(EventLoop *event_loop) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 478 | State *state = |
| 479 | states_[configuration::GetNodeIndex(configuration(), event_loop->node())] |
| 480 | .get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 481 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 482 | state->set_event_loop(event_loop); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 483 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 484 | // We don't run timing reports when trying to print out logged data, because |
| 485 | // otherwise we would end up printing out the timing reports themselves... |
| 486 | // This is only really relevant when we are replaying into a simulation. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 487 | event_loop->SkipTimingReport(); |
| 488 | event_loop->SkipAosLog(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 489 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 490 | for (size_t logged_channel_index = 0; |
| 491 | logged_channel_index < logged_configuration()->channels()->size(); |
| 492 | ++logged_channel_index) { |
| 493 | const Channel *channel = RemapChannel( |
| 494 | event_loop, |
| 495 | logged_configuration()->channels()->Get(logged_channel_index)); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 496 | |
Austin Schuh | 532656d | 2021-01-11 10:17:18 -0800 | [diff] [blame] | 497 | if (channel->logger() == LoggerConfig::NOT_LOGGED) { |
| 498 | continue; |
| 499 | } |
| 500 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 501 | message_bridge::NoncausalOffsetEstimator *filter = nullptr; |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 502 | RemoteMessageSender *remote_timestamp_sender = nullptr; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 503 | |
| 504 | State *source_state = nullptr; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 505 | |
| 506 | if (!configuration::ChannelIsSendableOnNode(channel, event_loop->node()) && |
| 507 | configuration::ChannelIsReadableOnNode(channel, event_loop->node())) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 508 | // We've got a message which is being forwarded to this node. |
| 509 | const Node *source_node = configuration::GetNode( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 510 | event_loop->configuration(), channel->source_node()->string_view()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 511 | filter = GetFilter(event_loop->node(), source_node); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 512 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 513 | // Delivery timestamps are supposed to be logged back on the source node. |
| 514 | // Configure remote timestamps to be sent. |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 515 | const Connection *connection = |
| 516 | configuration::ConnectionToNode(channel, event_loop->node()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 517 | const bool delivery_time_is_logged = |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 518 | configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection, |
| 519 | source_node); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 520 | |
| 521 | source_state = |
| 522 | states_[configuration::GetNodeIndex(configuration(), source_node)] |
| 523 | .get(); |
| 524 | |
| 525 | if (delivery_time_is_logged) { |
| 526 | remote_timestamp_sender = |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 527 | source_state->RemoteTimestampSender(channel, connection); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 528 | } |
| 529 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 530 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 531 | state->SetChannel( |
| 532 | logged_channel_index, |
| 533 | configuration::ChannelIndex(event_loop->configuration(), channel), |
| 534 | event_loop->MakeRawSender(channel), filter, remote_timestamp_sender, |
| 535 | source_state); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 538 | // If we didn't find any log files with data in them, we won't ever get a |
| 539 | // callback or be live. So skip the rest of the setup. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 540 | if (state->OldestMessageTime() == monotonic_clock::max_time) { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 541 | return; |
| 542 | } |
| 543 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 544 | state->set_timer_handler(event_loop->AddTimer([this, state]() { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 545 | VLOG(1) << "Starting sending " << MaybeNodeName(state->event_loop()->node()) |
| 546 | << "at " << state->event_loop()->context().monotonic_event_time |
| 547 | << " now " << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 548 | if (state->OldestMessageTime() == monotonic_clock::max_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 549 | --live_nodes_; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 550 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Node down!"; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 551 | if (exit_on_finish_ && live_nodes_ == 0) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 552 | event_loop_factory_->Exit(); |
| 553 | } |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 554 | return; |
| 555 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 556 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 557 | TimestampedMessage timestamped_message = state->PopOldest(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 558 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 559 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 560 | state->event_loop()->context().monotonic_event_time; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 561 | if (!FLAGS_skip_order_validation) { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 562 | CHECK(monotonic_now == timestamped_message.monotonic_event_time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 563 | << ": " << FlatbufferToJson(state->event_loop()->node()) << " Now " |
| 564 | << monotonic_now << " trying to send " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 565 | << timestamped_message.monotonic_event_time << " failure " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 566 | << state->DebugString(); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 567 | } else if (monotonic_now != timestamped_message.monotonic_event_time) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 568 | LOG(WARNING) << "Check failed: monotonic_now == " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 569 | "timestamped_message.monotonic_event_time) (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 570 | << monotonic_now << " vs. " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 571 | << timestamped_message.monotonic_event_time |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 572 | << "): " << FlatbufferToJson(state->event_loop()->node()) |
| 573 | << " Now " << monotonic_now << " trying to send " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 574 | << timestamped_message.monotonic_event_time << " failure " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 575 | << state->DebugString(); |
| 576 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 577 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 578 | if (timestamped_message.monotonic_event_time > |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 579 | state->monotonic_start_time() || |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 580 | event_loop_factory_ != nullptr) { |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 581 | if (timestamped_message.data.span().size() != 0u) { |
| 582 | if (timestamped_message.monotonic_remote_time != |
| 583 | monotonic_clock::min_time) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 584 | // Confirm that the message was sent on the sending node before the |
| 585 | // destination node (this node). As a proxy, do this by making sure |
| 586 | // that time on the source node is past when the message was sent. |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 587 | // |
| 588 | // TODO(austin): <= means that the cause message (which we know) could |
| 589 | // happen after the effect even though we know they are at the same |
| 590 | // time. I doubt anyone will notice for a bit, but we should really |
| 591 | // fix that. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 592 | if (!FLAGS_skip_order_validation) { |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 593 | CHECK_LE( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 594 | timestamped_message.monotonic_remote_time, |
| 595 | state->monotonic_remote_now(timestamped_message.channel_index)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 596 | << state->event_loop()->node()->name()->string_view() << " to " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 597 | << state->remote_node(timestamped_message.channel_index) |
| 598 | ->name() |
| 599 | ->string_view() |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 600 | << " while trying to send a message on " |
| 601 | << configuration::CleanedChannelToString( |
| 602 | logged_configuration()->channels()->Get( |
| 603 | timestamped_message.channel_index)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 604 | << " " << state->DebugString(); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 605 | } else if (timestamped_message.monotonic_remote_time > |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 606 | state->monotonic_remote_now( |
| 607 | timestamped_message.channel_index)) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 608 | LOG(WARNING) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 609 | << "Check failed: timestamped_message.monotonic_remote_time < " |
| 610 | "state->monotonic_remote_now(timestamped_message.channel_" |
| 611 | "index) (" |
| 612 | << timestamped_message.monotonic_remote_time << " vs. " |
| 613 | << state->monotonic_remote_now( |
| 614 | timestamped_message.channel_index) |
| 615 | << ") " << state->event_loop()->node()->name()->string_view() |
| 616 | << " to " |
| 617 | << state->remote_node(timestamped_message.channel_index) |
| 618 | ->name() |
| 619 | ->string_view() |
| 620 | << " currently " << timestamped_message.monotonic_event_time |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 621 | << " (" |
| 622 | << state->ToDistributedClock( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 623 | timestamped_message.monotonic_event_time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 624 | << ") remote event time " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 625 | << timestamped_message.monotonic_remote_time << " (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 626 | << state->RemoteToDistributedClock( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 627 | timestamped_message.channel_index, |
| 628 | timestamped_message.monotonic_remote_time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 629 | << ") " << state->DebugString(); |
| 630 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 633 | // 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] | 634 | state->SetRealtimeOffset(timestamped_message.monotonic_event_time, |
| 635 | timestamped_message.realtime_event_time); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 636 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 637 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Sending " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 638 | << timestamped_message.monotonic_event_time; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 639 | // TODO(austin): std::move channel_data in and make that efficient in |
| 640 | // simulation. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 641 | state->Send(std::move(timestamped_message)); |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 642 | } else if (!ignore_missing_data_ && |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 643 | // When starting up, we can have data which was sent before the |
| 644 | // log starts, but the timestamp was after the log starts. This |
| 645 | // is unreasonable to avoid, so ignore the missing data. |
| 646 | timestamped_message.monotonic_remote_time >= |
| 647 | state->monotonic_remote_start_time( |
| 648 | timestamped_message.channel_index) && |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 649 | !FLAGS_skip_missing_forwarding_entries) { |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 650 | // We've found a timestamp without data that we expect to have data for. |
| 651 | // This likely means that we are at the end of the log file. Record it |
| 652 | // and CHECK that in the rest of the log file, we don't find any more |
| 653 | // data on that channel. Not all channels will end at the same point in |
| 654 | // time since they can be in different files. |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 655 | VLOG(1) << "Found the last message on channel " |
| 656 | << timestamped_message.channel_index; |
| 657 | |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 658 | // The user might be working with log files from 1 node but forgot to |
| 659 | // configure the infrastructure to log data for a remote channel on that |
| 660 | // node. That can be very hard to debug, even though the log reader is |
| 661 | // doing the right thing. At least log a warning in that case and tell |
| 662 | // the user what is happening so they can either update their config to |
| 663 | // log the channel or can find a log with the data. |
| 664 | { |
| 665 | const std::vector<std::string> logger_nodes = |
| 666 | FindLoggerNodes(log_files_); |
| 667 | if (logger_nodes.size()) { |
| 668 | // We have old logs which don't have the logger nodes logged. In |
| 669 | // that case, we can't be helpful :( |
| 670 | bool data_logged = false; |
| 671 | const Channel *channel = logged_configuration()->channels()->Get( |
| 672 | timestamped_message.channel_index); |
| 673 | for (const std::string &node : logger_nodes) { |
| 674 | data_logged |= |
| 675 | configuration::ChannelMessageIsLoggedOnNode(channel, node); |
| 676 | } |
| 677 | if (!data_logged) { |
| 678 | LOG(WARNING) << "Got a timestamp without any logfiles which " |
| 679 | "could contain data for channel " |
| 680 | << configuration::CleanedChannelToString(channel); |
| 681 | LOG(WARNING) << "Only have logs logged on [" |
| 682 | << absl::StrJoin(logger_nodes, ", ") << "]"; |
| 683 | LOG(WARNING) |
| 684 | << "Dropping the rest of the data on " |
| 685 | << state->event_loop()->node()->name()->string_view(); |
| 686 | LOG(WARNING) |
| 687 | << "Consider using --skip_missing_forwarding_entries to " |
| 688 | "bypass this, update your config to log it, or add data " |
| 689 | "from one of the nodes it is logged on."; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 694 | // Vector storing if we've seen a nullptr message or not per channel. |
| 695 | std::vector<bool> last_message; |
| 696 | last_message.resize(logged_configuration()->channels()->size(), false); |
| 697 | |
| 698 | last_message[timestamped_message.channel_index] = true; |
| 699 | |
| 700 | // Now that we found the end of one channel, artificially stop the |
| 701 | // rest. It is confusing when part of your data gets replayed but not |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 702 | // all. Read the rest of the messages and drop them on the floor while |
| 703 | // doing some basic validation. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 704 | while (state->OldestMessageTime() != monotonic_clock::max_time) { |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 705 | TimestampedMessage next = state->PopOldest(); |
| 706 | // Make sure that once we have seen the last message on a channel, |
| 707 | // data doesn't start back up again. If the user wants to play |
| 708 | // through events like this, they can set |
| 709 | // --skip_missing_forwarding_entries or ignore_missing_data_. |
| 710 | CHECK_LT(next.channel_index, last_message.size()); |
| 711 | if (next.data.span().size() == 0u) { |
| 712 | last_message[next.channel_index] = true; |
| 713 | } else { |
| 714 | if (last_message[next.channel_index]) { |
| 715 | LOG(FATAL) |
| 716 | << "Found missing data in the middle of the log file on " |
| 717 | "channel " |
| 718 | << next.channel_index << " Last " |
| 719 | << last_message[next.channel_index] << state->DebugString(); |
| 720 | } |
| 721 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 722 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 723 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 724 | } else { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 725 | LOG(WARNING) |
| 726 | << "Not sending data from before the start of the log file. " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 727 | << timestamped_message.monotonic_event_time.time_since_epoch().count() |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 728 | << " start " << monotonic_start_time().time_since_epoch().count() |
Austin Schuh | d85baf8 | 2020-10-19 11:50:12 -0700 | [diff] [blame] | 729 | << " " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 730 | << FlatbufferToJson(timestamped_message.data, |
Austin Schuh | d85baf8 | 2020-10-19 11:50:12 -0700 | [diff] [blame] | 731 | {.multi_line = false, .max_vector_size = 100}); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 732 | } |
| 733 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 734 | const monotonic_clock::time_point next_time = state->OldestMessageTime(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 735 | if (next_time != monotonic_clock::max_time) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 736 | VLOG(1) << "Scheduling " << MaybeNodeName(state->event_loop()->node()) |
| 737 | << "wakeup for " << next_time << "(" |
| 738 | << state->ToDistributedClock(next_time) |
| 739 | << " distributed), now is " << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 740 | state->Setup(next_time); |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 741 | } else { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 742 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) |
| 743 | << "No next message, scheduling shutdown"; |
| 744 | // Set a timer up immediately after now to die. If we don't do this, |
| 745 | // then the senders waiting on the message we just read will never get |
| 746 | // called. |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 747 | if (event_loop_factory_ != nullptr) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 748 | state->Setup(monotonic_now + event_loop_factory_->send_delay() + |
| 749 | std::chrono::nanoseconds(1)); |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 750 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 751 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 752 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 753 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Done sending at " |
| 754 | << state->event_loop()->context().monotonic_event_time << " now " |
| 755 | << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 756 | })); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 757 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 758 | ++live_nodes_; |
| 759 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 760 | if (state->OldestMessageTime() != monotonic_clock::max_time) { |
| 761 | event_loop->OnRun([state]() { state->Setup(state->OldestMessageTime()); }); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
| 765 | void LogReader::Deregister() { |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 766 | // Make sure that things get destroyed in the correct order, rather than |
| 767 | // relying on getting the order correct in the class definition. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 768 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 769 | state->Deregister(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 770 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 771 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 772 | event_loop_factory_unique_ptr_.reset(); |
| 773 | event_loop_factory_ = nullptr; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 774 | } |
| 775 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 776 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 777 | std::string_view add_prefix, |
| 778 | std::string_view new_type) { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 779 | for (size_t ii = 0; ii < logged_configuration()->channels()->size(); ++ii) { |
| 780 | const Channel *const channel = logged_configuration()->channels()->Get(ii); |
| 781 | if (channel->name()->str() == name && |
| 782 | channel->type()->string_view() == type) { |
| 783 | CHECK_EQ(0u, remapped_channels_.count(ii)) |
| 784 | << "Already remapped channel " |
| 785 | << configuration::CleanedChannelToString(channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 786 | RemappedChannel remapped_channel; |
| 787 | remapped_channel.remapped_name = |
| 788 | std::string(add_prefix) + std::string(name); |
| 789 | remapped_channel.new_type = new_type; |
| 790 | remapped_channels_[ii] = std::move(remapped_channel); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 791 | VLOG(1) << "Remapping channel " |
| 792 | << configuration::CleanedChannelToString(channel) |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 793 | << " to have name " << remapped_channels_[ii].remapped_name; |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 794 | MakeRemappedConfig(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 795 | return; |
| 796 | } |
| 797 | } |
| 798 | LOG(FATAL) << "Unabled to locate channel with name " << name << " and type " |
| 799 | << type; |
| 800 | } |
| 801 | |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 802 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
| 803 | const Node *node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 804 | std::string_view add_prefix, |
| 805 | std::string_view new_type) { |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 806 | VLOG(1) << "Node is " << aos::FlatbufferToJson(node); |
| 807 | const Channel *remapped_channel = |
| 808 | configuration::GetChannel(logged_configuration(), name, type, "", node); |
| 809 | CHECK(remapped_channel != nullptr) << ": Failed to find {\"name\": \"" << name |
| 810 | << "\", \"type\": \"" << type << "\"}"; |
| 811 | VLOG(1) << "Original {\"name\": \"" << name << "\", \"type\": \"" << type |
| 812 | << "\"}"; |
| 813 | VLOG(1) << "Remapped " |
| 814 | << aos::configuration::StrippedChannelToString(remapped_channel); |
| 815 | |
| 816 | // We want to make /spray on node 0 go to /0/spray by snooping the maps. And |
| 817 | // we want it to degrade if the heuristics fail to just work. |
| 818 | // |
| 819 | // The easiest way to do this is going to be incredibly specific and verbose. |
| 820 | // Look up /spray, to /0/spray. Then, prefix the result with /original to get |
| 821 | // /original/0/spray. Then, create a map from /original/spray to |
| 822 | // /original/0/spray for just the type we were asked for. |
| 823 | if (name != remapped_channel->name()->string_view()) { |
| 824 | MapT new_map; |
| 825 | new_map.match = std::make_unique<ChannelT>(); |
| 826 | new_map.match->name = absl::StrCat(add_prefix, name); |
| 827 | new_map.match->type = type; |
| 828 | if (node != nullptr) { |
| 829 | new_map.match->source_node = node->name()->str(); |
| 830 | } |
| 831 | new_map.rename = std::make_unique<ChannelT>(); |
| 832 | new_map.rename->name = |
| 833 | absl::StrCat(add_prefix, remapped_channel->name()->string_view()); |
| 834 | maps_.emplace_back(std::move(new_map)); |
| 835 | } |
| 836 | |
| 837 | const size_t channel_index = |
| 838 | configuration::ChannelIndex(logged_configuration(), remapped_channel); |
| 839 | CHECK_EQ(0u, remapped_channels_.count(channel_index)) |
| 840 | << "Already remapped channel " |
| 841 | << configuration::CleanedChannelToString(remapped_channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 842 | |
| 843 | RemappedChannel remapped_channel_struct; |
| 844 | remapped_channel_struct.remapped_name = |
| 845 | std::string(add_prefix) + |
| 846 | std::string(remapped_channel->name()->string_view()); |
| 847 | remapped_channel_struct.new_type = new_type; |
| 848 | remapped_channels_[channel_index] = std::move(remapped_channel_struct); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 849 | MakeRemappedConfig(); |
| 850 | } |
| 851 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 852 | void LogReader::MakeRemappedConfig() { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 853 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 854 | if (state) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 855 | CHECK(!state->event_loop()) |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 856 | << ": Can't change the mapping after the events are scheduled."; |
| 857 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 858 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 859 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 860 | // If no remapping occurred and we are using the original config, then there |
| 861 | // is nothing interesting to do here. |
| 862 | if (remapped_channels_.empty() && replay_configuration_ == nullptr) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 863 | remapped_configuration_ = logged_configuration(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 864 | return; |
| 865 | } |
| 866 | // Config to copy Channel definitions from. Use the specified |
| 867 | // replay_configuration_ if it has been provided. |
| 868 | const Configuration *const base_config = replay_configuration_ == nullptr |
| 869 | ? logged_configuration() |
| 870 | : replay_configuration_; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 871 | |
| 872 | // Create a config with all the channels, but un-sorted/merged. Collect up |
| 873 | // the schemas while we do this. Call MergeConfiguration to sort everything, |
| 874 | // and then merge it all in together. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 875 | |
| 876 | // This is the builder that we use for the config containing all the new |
| 877 | // channels. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 878 | flatbuffers::FlatBufferBuilder fbb; |
| 879 | fbb.ForceDefaults(true); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 880 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 881 | |
| 882 | CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 13u) |
| 883 | << ": Merging logic needs to be updated when the number of channel " |
| 884 | "fields changes."; |
| 885 | |
| 886 | // List of schemas. |
| 887 | std::map<std::string_view, FlatbufferVector<reflection::Schema>> schema_map; |
| 888 | // Make sure our new RemoteMessage schema is in there for old logs without it. |
| 889 | schema_map.insert(std::make_pair( |
| 890 | RemoteMessage::GetFullyQualifiedName(), |
| 891 | FlatbufferVector<reflection::Schema>(FlatbufferSpan<reflection::Schema>( |
| 892 | message_bridge::RemoteMessageSchema())))); |
| 893 | |
| 894 | // Reconstruct the remapped channels. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 895 | for (auto &pair : remapped_channels_) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 896 | const Channel *const c = CHECK_NOTNULL(configuration::GetChannel( |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 897 | base_config, logged_configuration()->channels()->Get(pair.first), "", |
| 898 | nullptr)); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 899 | channel_offsets.emplace_back( |
| 900 | CopyChannel(c, pair.second.remapped_name, "", &fbb)); |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame^] | 901 | |
| 902 | if (c->has_destination_nodes()) { |
| 903 | for (const Connection *connection : *c->destination_nodes()) { |
| 904 | switch (connection->timestamp_logger()) { |
| 905 | case LoggerConfig::LOCAL_LOGGER: |
| 906 | case LoggerConfig::NOT_LOGGED: |
| 907 | // There is no timestamp channel associated with this, so ignore it. |
| 908 | break; |
| 909 | |
| 910 | case LoggerConfig::REMOTE_LOGGER: |
| 911 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 912 | // We want to make a split timestamp channel regardless of what type |
| 913 | // of log this used to be. No sense propagating the single |
| 914 | // timestamp channel. |
| 915 | |
| 916 | CHECK(connection->has_timestamp_logger_nodes()); |
| 917 | for (const flatbuffers::String *timestamp_logger_node : |
| 918 | *connection->timestamp_logger_nodes()) { |
| 919 | const Node *node = configuration::GetNode( |
| 920 | logged_configuration(), timestamp_logger_node->string_view()); |
| 921 | message_bridge::ChannelTimestampFinder finder( |
| 922 | logged_configuration(), "log_reader", node); |
| 923 | |
| 924 | // We are assuming here that all the maps are setup correctly to |
| 925 | // handle arbitrary timestamps. Apply the maps for this node to |
| 926 | // see what name this ends up with. |
| 927 | std::string name = finder.SplitChannelName( |
| 928 | pair.second.remapped_name, c->type()->str(), connection); |
| 929 | std::string unmapped_name = name; |
| 930 | configuration::HandleMaps(logged_configuration()->maps(), &name, |
| 931 | "aos.message_bridge.RemoteMessage", |
| 932 | node); |
| 933 | CHECK_NE(name, unmapped_name) |
| 934 | << ": Remote timestamp channel was not remapped, this is " |
| 935 | "very fishy"; |
| 936 | flatbuffers::Offset<flatbuffers::String> channel_name_offset = |
| 937 | fbb.CreateString(name); |
| 938 | flatbuffers::Offset<flatbuffers::String> channel_type_offset = |
| 939 | fbb.CreateString("aos.message_bridge.RemoteMessage"); |
| 940 | flatbuffers::Offset<flatbuffers::String> source_node_offset = |
| 941 | fbb.CreateString(timestamp_logger_node->string_view()); |
| 942 | |
| 943 | // Now, build a channel. Don't log it, 2 senders, and match the |
| 944 | // source frequency. |
| 945 | Channel::Builder channel_builder(fbb); |
| 946 | channel_builder.add_name(channel_name_offset); |
| 947 | channel_builder.add_type(channel_type_offset); |
| 948 | channel_builder.add_source_node(source_node_offset); |
| 949 | channel_builder.add_logger(LoggerConfig::NOT_LOGGED); |
| 950 | channel_builder.add_num_senders(2); |
| 951 | if (c->has_frequency()) { |
| 952 | channel_builder.add_frequency(c->frequency()); |
| 953 | } |
| 954 | channel_offsets.emplace_back(channel_builder.Finish()); |
| 955 | } |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | } |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 960 | } |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 961 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 962 | // Now reconstruct the original channels, translating types as needed |
| 963 | for (const Channel *c : *base_config->channels()) { |
| 964 | // Search for a mapping channel. |
| 965 | std::string_view new_type = ""; |
| 966 | for (auto &pair : remapped_channels_) { |
| 967 | const Channel *const remapped_channel = |
| 968 | logged_configuration()->channels()->Get(pair.first); |
| 969 | if (remapped_channel->name()->string_view() == c->name()->string_view() && |
| 970 | remapped_channel->type()->string_view() == c->type()->string_view()) { |
| 971 | new_type = pair.second.new_type; |
| 972 | break; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | // Copy everything over. |
| 977 | channel_offsets.emplace_back(CopyChannel(c, "", new_type, &fbb)); |
| 978 | |
| 979 | // Add the schema if it doesn't exist. |
| 980 | if (schema_map.find(c->type()->string_view()) == schema_map.end()) { |
| 981 | CHECK(c->has_schema()); |
| 982 | schema_map.insert(std::make_pair(c->type()->string_view(), |
| 983 | RecursiveCopyFlatBuffer(c->schema()))); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | // The MergeConfiguration API takes a vector, not a map. Convert. |
| 988 | std::vector<FlatbufferVector<reflection::Schema>> schemas; |
| 989 | while (!schema_map.empty()) { |
| 990 | schemas.emplace_back(std::move(schema_map.begin()->second)); |
| 991 | schema_map.erase(schema_map.begin()); |
| 992 | } |
| 993 | |
| 994 | // Create the Configuration containing the new channels that we want to add. |
| 995 | const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 996 | channels_offset = |
| 997 | channel_offsets.empty() ? 0 : fbb.CreateVector(channel_offsets); |
| 998 | |
| 999 | // Copy over the old maps. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1000 | std::vector<flatbuffers::Offset<Map>> map_offsets; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1001 | if (base_config->maps()) { |
| 1002 | for (const Map *map : *base_config->maps()) { |
| 1003 | map_offsets.emplace_back(aos::RecursiveCopyFlatBuffer(map, &fbb)); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | // 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] | 1008 | for (const MapT &map : maps_) { |
| 1009 | const flatbuffers::Offset<flatbuffers::String> match_name_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1010 | fbb.CreateString(map.match->name); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1011 | const flatbuffers::Offset<flatbuffers::String> match_type_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1012 | fbb.CreateString(map.match->type); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1013 | const flatbuffers::Offset<flatbuffers::String> rename_name_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1014 | fbb.CreateString(map.rename->name); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1015 | flatbuffers::Offset<flatbuffers::String> match_source_node_offset; |
| 1016 | if (!map.match->source_node.empty()) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1017 | match_source_node_offset = fbb.CreateString(map.match->source_node); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1018 | } |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1019 | Channel::Builder match_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1020 | match_builder.add_name(match_name_offset); |
| 1021 | match_builder.add_type(match_type_offset); |
| 1022 | if (!map.match->source_node.empty()) { |
| 1023 | match_builder.add_source_node(match_source_node_offset); |
| 1024 | } |
| 1025 | const flatbuffers::Offset<Channel> match_offset = match_builder.Finish(); |
| 1026 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1027 | Channel::Builder rename_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1028 | rename_builder.add_name(rename_name_offset); |
| 1029 | const flatbuffers::Offset<Channel> rename_offset = rename_builder.Finish(); |
| 1030 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1031 | Map::Builder map_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1032 | map_builder.add_match(match_offset); |
| 1033 | map_builder.add_rename(rename_offset); |
| 1034 | map_offsets.emplace_back(map_builder.Finish()); |
| 1035 | } |
| 1036 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1037 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 1038 | maps_offsets = map_offsets.empty() ? 0 : fbb.CreateVector(map_offsets); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1039 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1040 | // And copy everything else over. |
| 1041 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 1042 | nodes_offset = aos::RecursiveCopyVectorTable(base_config->nodes(), &fbb); |
| 1043 | |
| 1044 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 1045 | applications_offset = |
| 1046 | aos::RecursiveCopyVectorTable(base_config->applications(), &fbb); |
| 1047 | |
| 1048 | // Now insert everything else in unmodified. |
| 1049 | ConfigurationBuilder configuration_builder(fbb); |
| 1050 | if (!channels_offset.IsNull()) { |
| 1051 | configuration_builder.add_channels(channels_offset); |
| 1052 | } |
| 1053 | if (!maps_offsets.IsNull()) { |
| 1054 | configuration_builder.add_maps(maps_offsets); |
| 1055 | } |
| 1056 | if (!nodes_offset.IsNull()) { |
| 1057 | configuration_builder.add_nodes(nodes_offset); |
| 1058 | } |
| 1059 | if (!applications_offset.IsNull()) { |
| 1060 | configuration_builder.add_applications(applications_offset); |
| 1061 | } |
| 1062 | |
| 1063 | if (base_config->has_channel_storage_duration()) { |
| 1064 | configuration_builder.add_channel_storage_duration( |
| 1065 | base_config->channel_storage_duration()); |
| 1066 | } |
| 1067 | |
| 1068 | CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u) |
| 1069 | << ": Merging logic needs to be updated when the number of configuration " |
| 1070 | "fields changes."; |
| 1071 | |
| 1072 | fbb.Finish(configuration_builder.Finish()); |
| 1073 | |
| 1074 | // Clean it up and return it! By using MergeConfiguration here, we'll |
| 1075 | // actually get a deduplicated config for free too. |
| 1076 | FlatbufferDetachedBuffer<Configuration> new_merged_config = |
| 1077 | configuration::MergeConfiguration( |
| 1078 | FlatbufferDetachedBuffer<Configuration>(fbb.Release())); |
| 1079 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1080 | remapped_configuration_buffer_ = |
| 1081 | std::make_unique<FlatbufferDetachedBuffer<Configuration>>( |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1082 | configuration::MergeConfiguration(new_merged_config, schemas)); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1083 | |
| 1084 | remapped_configuration_ = &remapped_configuration_buffer_->message(); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1085 | |
| 1086 | // TODO(austin): Lazily re-build to save CPU? |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1089 | const Channel *LogReader::RemapChannel(const EventLoop *event_loop, |
| 1090 | const Channel *channel) { |
| 1091 | std::string_view channel_name = channel->name()->string_view(); |
| 1092 | std::string_view channel_type = channel->type()->string_view(); |
| 1093 | const int channel_index = |
| 1094 | configuration::ChannelIndex(logged_configuration(), channel); |
| 1095 | // If the channel is remapped, find the correct channel name to use. |
| 1096 | if (remapped_channels_.count(channel_index) > 0) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1097 | VLOG(3) << "Got remapped channel on " |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1098 | << configuration::CleanedChannelToString(channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1099 | channel_name = remapped_channels_[channel_index].remapped_name; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1100 | } |
| 1101 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1102 | VLOG(2) << "Going to remap channel " << channel_name << " " << channel_type; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1103 | const Channel *remapped_channel = configuration::GetChannel( |
| 1104 | event_loop->configuration(), channel_name, channel_type, |
| 1105 | event_loop->name(), event_loop->node()); |
| 1106 | |
| 1107 | CHECK(remapped_channel != nullptr) |
| 1108 | << ": Unable to send {\"name\": \"" << channel_name << "\", \"type\": \"" |
| 1109 | << channel_type << "\"} because it is not in the provided configuration."; |
| 1110 | |
| 1111 | return remapped_channel; |
| 1112 | } |
| 1113 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1114 | LogReader::State::State(std::unique_ptr<TimestampMapper> timestamp_mapper) |
| 1115 | : timestamp_mapper_(std::move(timestamp_mapper)) {} |
| 1116 | |
| 1117 | void LogReader::State::AddPeer(State *peer) { |
| 1118 | if (timestamp_mapper_ && peer->timestamp_mapper_) { |
| 1119 | timestamp_mapper_->AddPeer(peer->timestamp_mapper_.get()); |
| 1120 | } |
| 1121 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1122 | |
| 1123 | EventLoop *LogReader::State::SetNodeEventLoopFactory( |
| 1124 | NodeEventLoopFactory *node_event_loop_factory) { |
| 1125 | node_event_loop_factory_ = node_event_loop_factory; |
| 1126 | event_loop_unique_ptr_ = |
| 1127 | node_event_loop_factory_->MakeEventLoop("log_reader"); |
| 1128 | return event_loop_unique_ptr_.get(); |
| 1129 | } |
| 1130 | |
| 1131 | void LogReader::State::SetChannelCount(size_t count) { |
| 1132 | channels_.resize(count); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1133 | remote_timestamp_senders_.resize(count); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1134 | filters_.resize(count); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1135 | channel_source_state_.resize(count); |
| 1136 | factory_channel_index_.resize(count); |
| 1137 | queue_index_map_.resize(count); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | void LogReader::State::SetChannel( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1141 | size_t logged_channel_index, size_t factory_channel_index, |
| 1142 | std::unique_ptr<RawSender> sender, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1143 | message_bridge::NoncausalOffsetEstimator *filter, |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1144 | RemoteMessageSender *remote_timestamp_sender, State *source_state) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1145 | channels_[logged_channel_index] = std::move(sender); |
| 1146 | filters_[logged_channel_index] = filter; |
| 1147 | remote_timestamp_senders_[logged_channel_index] = remote_timestamp_sender; |
| 1148 | |
| 1149 | if (source_state) { |
| 1150 | channel_source_state_[logged_channel_index] = source_state; |
| 1151 | |
| 1152 | if (remote_timestamp_sender != nullptr) { |
| 1153 | source_state->queue_index_map_[logged_channel_index] = |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1154 | std::make_unique<std::vector<State::ContiguousSentTimestamp>>(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | factory_channel_index_[logged_channel_index] = factory_channel_index; |
| 1159 | } |
| 1160 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1161 | bool LogReader::State::Send(const TimestampedMessage ×tamped_message) { |
| 1162 | aos::RawSender *sender = channels_[timestamped_message.channel_index].get(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1163 | uint32_t remote_queue_index = 0xffffffff; |
| 1164 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1165 | if (remote_timestamp_senders_[timestamped_message.channel_index] != nullptr) { |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1166 | std::vector<ContiguousSentTimestamp> *queue_index_map = CHECK_NOTNULL( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1167 | CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]) |
| 1168 | ->queue_index_map_[timestamped_message.channel_index] |
| 1169 | .get()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1170 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1171 | struct SentTimestamp { |
| 1172 | monotonic_clock::time_point monotonic_event_time; |
| 1173 | uint32_t queue_index; |
| 1174 | } search; |
| 1175 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1176 | search.monotonic_event_time = timestamped_message.monotonic_remote_time; |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1177 | search.queue_index = timestamped_message.remote_queue_index; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1178 | |
| 1179 | // Find the sent time if available. |
| 1180 | auto element = std::lower_bound( |
| 1181 | queue_index_map->begin(), queue_index_map->end(), search, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1182 | [](ContiguousSentTimestamp a, SentTimestamp b) { |
| 1183 | if (a.ending_monotonic_event_time < b.monotonic_event_time) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1184 | return true; |
| 1185 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1186 | if (a.starting_monotonic_event_time > b.monotonic_event_time) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1187 | return false; |
| 1188 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1189 | |
| 1190 | if (a.ending_queue_index < b.queue_index) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1191 | return true; |
| 1192 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1193 | if (a.starting_queue_index >= b.queue_index) { |
| 1194 | return false; |
| 1195 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1196 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1197 | // If it isn't clearly below or above, it is below. Since we return |
| 1198 | // the last element <, this will return a match. |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1199 | return false; |
| 1200 | }); |
| 1201 | |
| 1202 | // TODO(austin): Be a bit more principled here, but we will want to do that |
| 1203 | // after the logger rewrite. We hit this when one node finishes, but the |
| 1204 | // other node isn't done yet. So there is no send time, but there is a |
| 1205 | // receive time. |
| 1206 | if (element != queue_index_map->end()) { |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1207 | CHECK_GE(timestamped_message.monotonic_remote_time, |
| 1208 | element->starting_monotonic_event_time); |
| 1209 | CHECK_LE(timestamped_message.monotonic_remote_time, |
| 1210 | element->ending_monotonic_event_time); |
| 1211 | CHECK_GE(timestamped_message.remote_queue_index, |
| 1212 | element->starting_queue_index); |
| 1213 | CHECK_LE(timestamped_message.remote_queue_index, |
| 1214 | element->ending_queue_index); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1215 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1216 | remote_queue_index = timestamped_message.remote_queue_index + |
| 1217 | element->actual_queue_index - |
| 1218 | element->starting_queue_index; |
| 1219 | } else { |
| 1220 | VLOG(1) << "No timestamp match in the map."; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | // Send! Use the replayed queue index here instead of the logged queue index |
| 1225 | // for the remote queue index. This makes re-logging work. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1226 | const bool sent = sender->Send( |
| 1227 | timestamped_message.data.message().data()->Data(), |
| 1228 | timestamped_message.data.message().data()->size(), |
| 1229 | timestamped_message.monotonic_remote_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1230 | timestamped_message.realtime_remote_time, remote_queue_index, |
| 1231 | (channel_source_state_[timestamped_message.channel_index] != nullptr |
| 1232 | ? CHECK_NOTNULL( |
| 1233 | channel_source_state_[timestamped_message.channel_index]) |
| 1234 | ->event_loop_->boot_uuid() |
| 1235 | : event_loop_->boot_uuid())); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1236 | if (!sent) return false; |
| 1237 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1238 | if (queue_index_map_[timestamped_message.channel_index]) { |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1239 | if (queue_index_map_[timestamped_message.channel_index]->empty()) { |
| 1240 | // Nothing here, start a range with 0 length. |
| 1241 | ContiguousSentTimestamp timestamp; |
| 1242 | timestamp.starting_monotonic_event_time = |
| 1243 | timestamp.ending_monotonic_event_time = |
| 1244 | timestamped_message.monotonic_event_time; |
| 1245 | timestamp.starting_queue_index = timestamp.ending_queue_index = |
| 1246 | timestamped_message.queue_index; |
| 1247 | timestamp.actual_queue_index = sender->sent_queue_index(); |
| 1248 | queue_index_map_[timestamped_message.channel_index]->emplace_back( |
| 1249 | timestamp); |
| 1250 | } else { |
| 1251 | // We've got something. See if the next timestamp is still contiguous. If |
| 1252 | // so, grow it. |
| 1253 | ContiguousSentTimestamp *back = |
| 1254 | &queue_index_map_[timestamped_message.channel_index]->back(); |
| 1255 | if ((back->starting_queue_index - back->actual_queue_index) == |
| 1256 | (timestamped_message.queue_index - sender->sent_queue_index())) { |
| 1257 | back->ending_queue_index = timestamped_message.queue_index; |
| 1258 | back->ending_monotonic_event_time = |
| 1259 | timestamped_message.monotonic_event_time; |
| 1260 | } else { |
| 1261 | // Otherwise, make a new one. |
| 1262 | ContiguousSentTimestamp timestamp; |
| 1263 | timestamp.starting_monotonic_event_time = |
| 1264 | timestamp.ending_monotonic_event_time = |
| 1265 | timestamped_message.monotonic_event_time; |
| 1266 | timestamp.starting_queue_index = timestamp.ending_queue_index = |
| 1267 | timestamped_message.queue_index; |
| 1268 | timestamp.actual_queue_index = sender->sent_queue_index(); |
| 1269 | queue_index_map_[timestamped_message.channel_index]->emplace_back( |
| 1270 | timestamp); |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | // TODO(austin): Should we prune the map? On a many day log, I only saw the |
| 1275 | // queue index diverge a couple of elements, which would be a very small |
| 1276 | // map. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1277 | } else if (remote_timestamp_senders_[timestamped_message.channel_index] != |
| 1278 | nullptr) { |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1279 | flatbuffers::FlatBufferBuilder fbb; |
| 1280 | fbb.ForceDefaults(true); |
Austin Schuh | cdd9027 | 2021-03-15 12:46:16 -0700 | [diff] [blame] | 1281 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> boot_uuid_offset = |
| 1282 | event_loop_->boot_uuid().PackVector(&fbb); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1283 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1284 | RemoteMessage::Builder message_header_builder(fbb); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1285 | |
| 1286 | message_header_builder.add_channel_index( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1287 | factory_channel_index_[timestamped_message.channel_index]); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1288 | |
| 1289 | // Swap the remote and sent metrics. They are from the sender's |
| 1290 | // perspective, not the receiver's perspective. |
| 1291 | message_header_builder.add_monotonic_sent_time( |
| 1292 | sender->monotonic_sent_time().time_since_epoch().count()); |
| 1293 | message_header_builder.add_realtime_sent_time( |
| 1294 | sender->realtime_sent_time().time_since_epoch().count()); |
| 1295 | message_header_builder.add_queue_index(sender->sent_queue_index()); |
| 1296 | |
| 1297 | message_header_builder.add_monotonic_remote_time( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1298 | timestamped_message.monotonic_remote_time.time_since_epoch().count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1299 | message_header_builder.add_realtime_remote_time( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1300 | timestamped_message.realtime_remote_time.time_since_epoch().count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1301 | |
| 1302 | message_header_builder.add_remote_queue_index(remote_queue_index); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1303 | message_header_builder.add_boot_uuid(boot_uuid_offset); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1304 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1305 | fbb.Finish(message_header_builder.Finish()); |
| 1306 | |
| 1307 | remote_timestamp_senders_[timestamped_message.channel_index]->Send( |
| 1308 | FlatbufferDetachedBuffer<RemoteMessage>(fbb.Release()), |
| 1309 | timestamped_message.monotonic_timestamp_time); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | return true; |
| 1313 | } |
| 1314 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1315 | LogReader::RemoteMessageSender::RemoteMessageSender( |
| 1316 | aos::Sender<message_bridge::RemoteMessage> sender, EventLoop *event_loop) |
| 1317 | : event_loop_(event_loop), |
| 1318 | sender_(std::move(sender)), |
| 1319 | timer_(event_loop->AddTimer([this]() { SendTimestamp(); })) {} |
| 1320 | |
| 1321 | void LogReader::RemoteMessageSender::ScheduleTimestamp() { |
| 1322 | if (remote_timestamps_.empty()) { |
| 1323 | CHECK_NOTNULL(timer_); |
| 1324 | timer_->Disable(); |
| 1325 | scheduled_time_ = monotonic_clock::min_time; |
| 1326 | return; |
| 1327 | } |
| 1328 | |
| 1329 | if (scheduled_time_ != remote_timestamps_.front().monotonic_timestamp_time) { |
| 1330 | CHECK_NOTNULL(timer_); |
Austin Schuh | 816e5d6 | 2021-01-05 23:42:20 -0800 | [diff] [blame] | 1331 | timer_->Setup(remote_timestamps_.front().monotonic_timestamp_time); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1332 | scheduled_time_ = remote_timestamps_.front().monotonic_timestamp_time; |
Austin Schuh | 3d94be0 | 2021-02-12 23:15:20 -0800 | [diff] [blame] | 1333 | CHECK_GE(scheduled_time_, event_loop_->monotonic_now()) |
| 1334 | << event_loop_->node()->name()->string_view(); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | void LogReader::RemoteMessageSender::Send( |
| 1339 | FlatbufferDetachedBuffer<RemoteMessage> remote_message, |
| 1340 | monotonic_clock::time_point monotonic_timestamp_time) { |
| 1341 | // There are 2 cases. Either we have a monotonic_timestamp_time and need to |
| 1342 | // resend the timestamp at the correct time, or we don't and can send it |
| 1343 | // immediately. |
| 1344 | if (monotonic_timestamp_time == monotonic_clock::min_time) { |
| 1345 | CHECK(remote_timestamps_.empty()) |
| 1346 | << ": Unsupported mix of timestamps and no timestamps."; |
| 1347 | sender_.Send(std::move(remote_message)); |
| 1348 | } else { |
Austin Schuh | b22ae42 | 2021-01-31 17:57:06 -0800 | [diff] [blame] | 1349 | remote_timestamps_.emplace( |
| 1350 | std::upper_bound( |
| 1351 | remote_timestamps_.begin(), remote_timestamps_.end(), |
| 1352 | monotonic_timestamp_time, |
| 1353 | [](const aos::monotonic_clock::time_point monotonic_timestamp_time, |
| 1354 | const Timestamp ×tamp) { |
| 1355 | return monotonic_timestamp_time < |
| 1356 | timestamp.monotonic_timestamp_time; |
| 1357 | }), |
| 1358 | std::move(remote_message), monotonic_timestamp_time); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1359 | ScheduleTimestamp(); |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | void LogReader::RemoteMessageSender::SendTimestamp() { |
Austin Schuh | 3d94be0 | 2021-02-12 23:15:20 -0800 | [diff] [blame] | 1364 | CHECK_EQ(event_loop_->context().monotonic_event_time, scheduled_time_) |
| 1365 | << event_loop_->node()->name()->string_view(); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1366 | CHECK(!remote_timestamps_.empty()); |
| 1367 | |
| 1368 | // Send out all timestamps at the currently scheduled time. |
| 1369 | while (remote_timestamps_.front().monotonic_timestamp_time == |
| 1370 | scheduled_time_) { |
| 1371 | sender_.Send(std::move(remote_timestamps_.front().remote_message)); |
| 1372 | remote_timestamps_.pop_front(); |
| 1373 | if (remote_timestamps_.empty()) { |
| 1374 | break; |
| 1375 | } |
| 1376 | } |
| 1377 | scheduled_time_ = monotonic_clock::min_time; |
| 1378 | |
| 1379 | ScheduleTimestamp(); |
| 1380 | } |
| 1381 | |
| 1382 | LogReader::RemoteMessageSender *LogReader::State::RemoteTimestampSender( |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 1383 | const Channel *channel, const Connection *connection) { |
| 1384 | message_bridge::ChannelTimestampFinder finder(event_loop_); |
| 1385 | // Look at any pre-created channel/connection pairs. |
| 1386 | { |
| 1387 | auto it = |
| 1388 | channel_timestamp_loggers_.find(std::make_pair(channel, connection)); |
| 1389 | if (it != channel_timestamp_loggers_.end()) { |
| 1390 | return it->second.get(); |
| 1391 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 1394 | // That failed, so resolve the RemoteMessage channel timestamps will be logged |
| 1395 | // to. |
| 1396 | const Channel *timestamp_channel = finder.ForChannel(channel, connection); |
| 1397 | |
| 1398 | { |
| 1399 | // See if that has been created before. If so, cache it in |
| 1400 | // channel_timestamp_loggers_ and return. |
| 1401 | auto it = timestamp_loggers_.find(timestamp_channel); |
| 1402 | if (it != timestamp_loggers_.end()) { |
| 1403 | CHECK(channel_timestamp_loggers_ |
| 1404 | .try_emplace(std::make_pair(channel, connection), it->second) |
| 1405 | .second); |
| 1406 | return it->second.get(); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | // Otherwise, make a sender, save it, and cache it. |
| 1411 | auto result = channel_timestamp_loggers_.try_emplace( |
| 1412 | std::make_pair(channel, connection), |
| 1413 | std::make_shared<RemoteMessageSender>( |
| 1414 | event_loop()->MakeSender<RemoteMessage>( |
| 1415 | timestamp_channel->name()->string_view()), |
| 1416 | event_loop())); |
| 1417 | |
| 1418 | CHECK(timestamp_loggers_.try_emplace(timestamp_channel, result.first->second) |
| 1419 | .second); |
| 1420 | return result.first->second.get(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1423 | TimestampedMessage LogReader::State::PopOldest() { |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1424 | CHECK(timestamp_mapper_ != nullptr); |
| 1425 | TimestampedMessage *result_ptr = timestamp_mapper_->Front(); |
| 1426 | CHECK(result_ptr != nullptr); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1427 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1428 | TimestampedMessage result = std::move(*result_ptr); |
| 1429 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1430 | VLOG(2) << MaybeNodeName(event_loop_->node()) << "PopOldest Popping " |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1431 | << result.monotonic_event_time; |
| 1432 | timestamp_mapper_->PopFront(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1433 | SeedSortedMessages(); |
| 1434 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1435 | if (result.monotonic_remote_time != monotonic_clock::min_time) { |
| 1436 | message_bridge::NoncausalOffsetEstimator *filter = |
| 1437 | filters_[result.channel_index]; |
| 1438 | CHECK(filter != nullptr); |
| 1439 | |
| 1440 | // TODO(austin): We probably want to push this down into the timestamp |
| 1441 | // mapper directly. |
Austin Schuh | 3d94be0 | 2021-02-12 23:15:20 -0800 | [diff] [blame] | 1442 | filter->Pop(event_loop_->node(), event_loop_->monotonic_now()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1443 | } |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 1444 | VLOG(1) << "Popped " << result |
| 1445 | << configuration::CleanedChannelToString( |
| 1446 | event_loop_->configuration()->channels()->Get( |
| 1447 | factory_channel_index_[result.channel_index])); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1448 | return result; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | monotonic_clock::time_point LogReader::State::OldestMessageTime() const { |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1452 | if (timestamp_mapper_ == nullptr) { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1453 | return monotonic_clock::max_time; |
| 1454 | } |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1455 | TimestampedMessage *result_ptr = timestamp_mapper_->Front(); |
| 1456 | if (result_ptr == nullptr) { |
| 1457 | return monotonic_clock::max_time; |
| 1458 | } |
| 1459 | VLOG(2) << MaybeNodeName(event_loop_->node()) << "oldest message at " |
| 1460 | << result_ptr->monotonic_event_time; |
| 1461 | return result_ptr->monotonic_event_time; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | void LogReader::State::SeedSortedMessages() { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1465 | if (!timestamp_mapper_) return; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1466 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1467 | timestamp_mapper_->QueueFor(chrono::duration_cast<chrono::seconds>( |
| 1468 | chrono::duration<double>(FLAGS_time_estimation_buffer_seconds))); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | void LogReader::State::Deregister() { |
| 1472 | for (size_t i = 0; i < channels_.size(); ++i) { |
| 1473 | channels_[i].reset(); |
| 1474 | } |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 1475 | channel_timestamp_loggers_.clear(); |
| 1476 | timestamp_loggers_.clear(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1477 | event_loop_unique_ptr_.reset(); |
| 1478 | event_loop_ = nullptr; |
| 1479 | timer_handler_ = nullptr; |
| 1480 | node_event_loop_factory_ = nullptr; |
| 1481 | } |
| 1482 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1483 | } // namespace logger |
| 1484 | } // namespace aos |