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