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> |
| 4 | #include <sys/stat.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/uio.h> |
Brian Silverman | 8ff74aa | 2021-02-05 16:37:15 -0800 | [diff] [blame] | 7 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 8 | #include <climits> |
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" |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 18 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 19 | #include "aos/network/multinode_timestamp_filter.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 20 | #include "aos/network/remote_message_generated.h" |
| 21 | #include "aos/network/remote_message_schema.h" |
Austin Schuh | 288479d | 2019-12-18 19:47:52 -0800 | [diff] [blame] | 22 | #include "aos/network/team_number.h" |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 23 | #include "aos/network/timestamp_channel.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 24 | #include "aos/time/time.h" |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 25 | #include "aos/util/file.h" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 26 | #include "aos/uuid.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 27 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 28 | #include "openssl/sha.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 29 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 30 | DEFINE_bool(skip_missing_forwarding_entries, false, |
| 31 | "If true, drop any forwarding entries with missing data. If " |
| 32 | "false, CHECK."); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 33 | |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 34 | DECLARE_bool(timestamps_to_csv); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 36 | DEFINE_bool(skip_order_validation, false, |
| 37 | "If true, ignore any out of orderness in replay"); |
| 38 | |
Austin Schuh | f068866 | 2020-12-19 15:37:45 -0800 | [diff] [blame] | 39 | DEFINE_double( |
| 40 | time_estimation_buffer_seconds, 2.0, |
| 41 | "The time to buffer ahead in the log file to accurately reconstruct time."); |
| 42 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 43 | DEFINE_string( |
| 44 | start_time, "", |
| 45 | "If set, start at this point in time in the log on the realtime clock."); |
| 46 | DEFINE_string( |
| 47 | end_time, "", |
| 48 | "If set, end at this point in time in the log on the realtime clock."); |
| 49 | |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 50 | DEFINE_bool(drop_realtime_messages_before_start, false, |
| 51 | "If set, will drop any messages sent before the start of the " |
| 52 | "logfile in realtime replay. Setting this guarantees consistency " |
| 53 | "in timing with the original logfile, but means that you lose " |
| 54 | "access to fetched low-frequency messages."); |
| 55 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 56 | DEFINE_double( |
| 57 | threaded_look_ahead_seconds, 2.0, |
| 58 | "Time, in seconds, to add to look-ahead when using multi-threaded replay. " |
| 59 | "Can validly be zero, but higher values are encouraged for realtime replay " |
| 60 | "in order to prevent the replay from ever having to block on waiting for " |
| 61 | "the reader to find the next message."); |
| 62 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 63 | namespace aos { |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 64 | namespace configuration { |
| 65 | // We don't really want to expose this publicly, but log reader doesn't really |
| 66 | // want to re-implement it. |
| 67 | void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps, |
| 68 | std::string *name, std::string_view type, const Node *node); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 69 | } // namespace configuration |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 70 | namespace logger { |
Austin Schuh | 0afc4d1 | 2020-10-19 11:42:04 -0700 | [diff] [blame] | 71 | namespace { |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 72 | |
Austin Schuh | 1c22735 | 2021-09-17 12:53:54 -0700 | [diff] [blame] | 73 | bool CompareChannels(const Channel *c, |
| 74 | ::std::pair<std::string_view, std::string_view> p) { |
| 75 | int name_compare = c->name()->string_view().compare(p.first); |
| 76 | if (name_compare == 0) { |
| 77 | return c->type()->string_view() < p.second; |
| 78 | } else if (name_compare < 0) { |
| 79 | return true; |
| 80 | } else { |
| 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | bool EqualsChannels(const Channel *c, |
| 86 | ::std::pair<std::string_view, std::string_view> p) { |
| 87 | return c->name()->string_view() == p.first && |
| 88 | c->type()->string_view() == p.second; |
| 89 | } |
| 90 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 91 | // Copies the channel, removing the schema as we go. If new_name is provided, |
| 92 | // it is used instead of the name inside the channel. If new_type is provided, |
| 93 | // it is used instead of the type in the channel. |
| 94 | flatbuffers::Offset<Channel> CopyChannel(const Channel *c, |
| 95 | std::string_view new_name, |
| 96 | std::string_view new_type, |
| 97 | flatbuffers::FlatBufferBuilder *fbb) { |
| 98 | flatbuffers::Offset<flatbuffers::String> name_offset = |
| 99 | fbb->CreateSharedString(new_name.empty() ? c->name()->string_view() |
| 100 | : new_name); |
| 101 | flatbuffers::Offset<flatbuffers::String> type_offset = |
| 102 | fbb->CreateSharedString(new_type.empty() ? c->type()->str() : new_type); |
| 103 | flatbuffers::Offset<flatbuffers::String> source_node_offset = |
| 104 | c->has_source_node() ? fbb->CreateSharedString(c->source_node()->str()) |
| 105 | : 0; |
| 106 | |
| 107 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>> |
| 108 | destination_nodes_offset = |
| 109 | aos::RecursiveCopyVectorTable(c->destination_nodes(), fbb); |
| 110 | |
| 111 | flatbuffers::Offset< |
| 112 | flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> |
| 113 | logger_nodes_offset = aos::CopyVectorSharedString(c->logger_nodes(), fbb); |
| 114 | |
| 115 | Channel::Builder channel_builder(*fbb); |
| 116 | channel_builder.add_name(name_offset); |
| 117 | channel_builder.add_type(type_offset); |
| 118 | if (c->has_frequency()) { |
| 119 | channel_builder.add_frequency(c->frequency()); |
| 120 | } |
| 121 | if (c->has_max_size()) { |
| 122 | channel_builder.add_max_size(c->max_size()); |
| 123 | } |
| 124 | if (c->has_num_senders()) { |
| 125 | channel_builder.add_num_senders(c->num_senders()); |
| 126 | } |
| 127 | if (c->has_num_watchers()) { |
| 128 | channel_builder.add_num_watchers(c->num_watchers()); |
| 129 | } |
| 130 | if (!source_node_offset.IsNull()) { |
| 131 | channel_builder.add_source_node(source_node_offset); |
| 132 | } |
| 133 | if (!destination_nodes_offset.IsNull()) { |
| 134 | channel_builder.add_destination_nodes(destination_nodes_offset); |
| 135 | } |
| 136 | if (c->has_logger()) { |
| 137 | channel_builder.add_logger(c->logger()); |
| 138 | } |
| 139 | if (!logger_nodes_offset.IsNull()) { |
| 140 | channel_builder.add_logger_nodes(logger_nodes_offset); |
| 141 | } |
| 142 | if (c->has_read_method()) { |
| 143 | channel_builder.add_read_method(c->read_method()); |
| 144 | } |
| 145 | if (c->has_num_readers()) { |
| 146 | channel_builder.add_num_readers(c->num_readers()); |
| 147 | } |
| 148 | return channel_builder.Finish(); |
| 149 | } |
| 150 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 151 | namespace chrono = std::chrono; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 152 | using message_bridge::RemoteMessage; |
Austin Schuh | 0afc4d1 | 2020-10-19 11:42:04 -0700 | [diff] [blame] | 153 | } // namespace |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 154 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 155 | // Class to manage triggering events on the RT clock while replaying logs. Since |
| 156 | // the RT clock can only change when we get a message, we only need to update |
| 157 | // our timers when new messages are read. |
| 158 | class EventNotifier { |
| 159 | public: |
| 160 | EventNotifier(EventLoop *event_loop, std::function<void()> fn, |
| 161 | std::string_view name, |
| 162 | realtime_clock::time_point realtime_event_time) |
| 163 | : event_loop_(event_loop), |
| 164 | fn_(std::move(fn)), |
| 165 | realtime_event_time_(realtime_event_time) { |
| 166 | CHECK(event_loop_); |
| 167 | event_timer_ = event_loop->AddTimer([this]() { HandleTime(); }); |
| 168 | |
| 169 | if (event_loop_->node() != nullptr) { |
| 170 | event_timer_->set_name( |
| 171 | absl::StrCat(event_loop_->node()->name()->string_view(), "_", name)); |
| 172 | } else { |
| 173 | event_timer_->set_name(name); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | ~EventNotifier() { event_timer_->Disable(); } |
| 178 | |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 179 | // Sets the clock offset for realtime playback. |
| 180 | void SetClockOffset(std::chrono::nanoseconds clock_offset) { |
| 181 | clock_offset_ = clock_offset; |
| 182 | } |
| 183 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 184 | // Returns the event trigger time. |
| 185 | realtime_clock::time_point realtime_event_time() const { |
| 186 | return realtime_event_time_; |
| 187 | } |
| 188 | |
| 189 | // Observes the next message and potentially calls the callback or updates the |
| 190 | // timer. |
| 191 | void ObserveNextMessage(monotonic_clock::time_point monotonic_message_time, |
| 192 | realtime_clock::time_point realtime_message_time) { |
| 193 | if (realtime_message_time < realtime_event_time_) { |
| 194 | return; |
| 195 | } |
| 196 | if (called_) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | // Move the callback wakeup time to the correct time (or make it now if |
| 201 | // there's a gap in time) now that we know it is before the next |
| 202 | // message. |
| 203 | const monotonic_clock::time_point candidate_monotonic = |
| 204 | (realtime_event_time_ - realtime_message_time) + monotonic_message_time; |
| 205 | const monotonic_clock::time_point monotonic_now = |
| 206 | event_loop_->monotonic_now(); |
| 207 | if (candidate_monotonic < monotonic_now) { |
| 208 | // Whops, time went backwards. Just do it now. |
| 209 | HandleTime(); |
| 210 | } else { |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 211 | event_timer_->Setup(candidate_monotonic + clock_offset_); |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | private: |
| 216 | void HandleTime() { |
| 217 | if (!called_) { |
| 218 | called_ = true; |
| 219 | fn_(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | EventLoop *event_loop_ = nullptr; |
| 224 | TimerHandler *event_timer_ = nullptr; |
| 225 | std::function<void()> fn_; |
| 226 | |
| 227 | const realtime_clock::time_point realtime_event_time_ = |
| 228 | realtime_clock::min_time; |
| 229 | |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 230 | std::chrono::nanoseconds clock_offset_{0}; |
| 231 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 232 | bool called_ = false; |
| 233 | }; |
| 234 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 235 | LogReader::LogReader(std::string_view filename, |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 236 | const Configuration *replay_configuration, |
| 237 | const ReplayChannels *replay_channels) |
| 238 | : LogReader(SortParts({std::string(filename)}), replay_configuration, |
| 239 | replay_channels) {} |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 240 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 241 | LogReader::LogReader(std::vector<LogFile> log_files, |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 242 | const Configuration *replay_configuration, |
| 243 | const ReplayChannels *replay_channels) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 244 | : log_files_(std::move(log_files)), |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 245 | replay_configuration_(replay_configuration), |
| 246 | replay_channels_(replay_channels) { |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 247 | SetStartTime(FLAGS_start_time); |
| 248 | SetEndTime(FLAGS_end_time); |
| 249 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 250 | CHECK_GT(log_files_.size(), 0u); |
| 251 | { |
| 252 | // Validate that we have the same config everwhere. This will be true if |
| 253 | // all the parts were sorted together and the configs match. |
| 254 | const Configuration *config = nullptr; |
Austin Schuh | 297d235 | 2021-01-21 19:02:17 -0800 | [diff] [blame] | 255 | for (const LogFile &log_file : log_files_) { |
| 256 | if (log_file.config.get() == nullptr) { |
| 257 | LOG(FATAL) << "Couldn't find a config in " << log_file; |
| 258 | } |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 259 | if (config == nullptr) { |
| 260 | config = log_file.config.get(); |
| 261 | } else { |
| 262 | CHECK_EQ(config, log_file.config.get()); |
| 263 | } |
| 264 | } |
| 265 | } |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 266 | |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 267 | if (replay_channels_ != nullptr) { |
| 268 | CHECK(!replay_channels_->empty()) << "replay_channels is empty which means " |
| 269 | "no messages will get replayed."; |
| 270 | } |
| 271 | |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 272 | MakeRemappedConfig(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 273 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 274 | // Remap all existing remote timestamp channels. They will be recreated, and |
| 275 | // the data logged isn't relevant anymore. |
Austin Schuh | 3c5dae5 | 2020-10-06 18:55:18 -0700 | [diff] [blame] | 276 | for (const Node *node : configuration::GetNodes(logged_configuration())) { |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 277 | message_bridge::ChannelTimestampFinder finder(logged_configuration(), |
| 278 | "log_reader", node); |
| 279 | |
| 280 | absl::btree_set<std::string_view> remote_nodes; |
| 281 | |
| 282 | for (const Channel *channel : *logged_configuration()->channels()) { |
| 283 | if (!configuration::ChannelIsSendableOnNode(channel, node)) { |
| 284 | continue; |
| 285 | } |
| 286 | if (!channel->has_destination_nodes()) { |
| 287 | continue; |
| 288 | } |
| 289 | for (const Connection *connection : *channel->destination_nodes()) { |
| 290 | if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection, |
| 291 | node)) { |
| 292 | // Start by seeing if the split timestamp channels are being used for |
| 293 | // this message. If so, remap them. |
| 294 | const Channel *timestamp_channel = configuration::GetChannel( |
| 295 | logged_configuration(), |
| 296 | finder.SplitChannelName(channel, connection), |
| 297 | RemoteMessage::GetFullyQualifiedName(), "", node, true); |
| 298 | |
| 299 | if (timestamp_channel != nullptr) { |
James Kuszmaul | 53da7f3 | 2022-09-11 11:11:55 -0700 | [diff] [blame] | 300 | // If for some reason a timestamp channel is not NOT_LOGGED (which |
| 301 | // is unusual), then remap the channel so that the replayed channel |
| 302 | // doesn't overlap with the special separate replay we do for |
| 303 | // timestamps. |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 304 | if (timestamp_channel->logger() != LoggerConfig::NOT_LOGGED) { |
| 305 | RemapLoggedChannel<RemoteMessage>( |
| 306 | timestamp_channel->name()->string_view(), node); |
| 307 | } |
| 308 | continue; |
| 309 | } |
| 310 | |
| 311 | // Otherwise collect this one up as a node to look for a combined |
| 312 | // channel from. It is more efficient to compare nodes than channels. |
Austin Schuh | 349e7ad | 2022-04-02 21:12:26 -0700 | [diff] [blame] | 313 | LOG(WARNING) << "Failed to find channel " |
| 314 | << finder.SplitChannelName(channel, connection) |
| 315 | << " on node " << aos::FlatbufferToJson(node); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 316 | remote_nodes.insert(connection->name()->string_view()); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 321 | std::vector<const Node *> timestamp_logger_nodes = |
| 322 | configuration::TimestampNodes(logged_configuration(), node); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 323 | for (const std::string_view remote_node : remote_nodes) { |
| 324 | const std::string channel = finder.CombinedChannelName(remote_node); |
| 325 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 326 | // See if the log file is an old log with MessageHeader channels in it, or |
| 327 | // a newer log with RemoteMessage. If we find an older log, rename the |
| 328 | // type too along with the name. |
| 329 | if (HasChannel<MessageHeader>(channel, node)) { |
| 330 | CHECK(!HasChannel<RemoteMessage>(channel, node)) |
| 331 | << ": Can't have both a MessageHeader and RemoteMessage remote " |
| 332 | "timestamp channel."; |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 333 | // In theory, we should check NOT_LOGGED like RemoteMessage and be more |
| 334 | // careful about updating the config, but there are fewer and fewer logs |
| 335 | // with MessageHeader remote messages, so it isn't worth the effort. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 336 | RemapLoggedChannel<MessageHeader>(channel, node, "/original", |
| 337 | "aos.message_bridge.RemoteMessage"); |
| 338 | } else { |
| 339 | CHECK(HasChannel<RemoteMessage>(channel, node)) |
| 340 | << ": Failed to find {\"name\": \"" << channel << "\", \"type\": \"" |
| 341 | << RemoteMessage::GetFullyQualifiedName() << "\"} for node " |
| 342 | << node->name()->string_view(); |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 343 | // Only bother to remap if there's something on the channel. We can |
| 344 | // tell if the channel was marked NOT_LOGGED or not. This makes the |
| 345 | // config not change un-necesarily when we replay a log with NOT_LOGGED |
| 346 | // messages. |
| 347 | if (HasLoggedChannel<RemoteMessage>(channel, node)) { |
| 348 | RemapLoggedChannel<RemoteMessage>(channel, node); |
| 349 | } |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 350 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 354 | if (replay_configuration) { |
| 355 | CHECK_EQ(configuration::MultiNode(configuration()), |
| 356 | configuration::MultiNode(replay_configuration)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 357 | << ": Log file and replay config need to both be multi or single " |
| 358 | "node."; |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 361 | if (!configuration::MultiNode(configuration())) { |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 362 | states_.resize(1); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 363 | } else { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 364 | if (replay_configuration) { |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 365 | CHECK_EQ(logged_configuration()->nodes()->size(), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 366 | replay_configuration->nodes()->size()) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 367 | << ": Log file and replay config need to have matching nodes " |
| 368 | "lists."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 369 | for (const Node *node : *logged_configuration()->nodes()) { |
| 370 | if (configuration::GetNode(replay_configuration, node) == nullptr) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 371 | LOG(FATAL) << "Found node " << FlatbufferToJson(node) |
| 372 | << " in logged config that is not present in the replay " |
| 373 | "config."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 376 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 377 | states_.resize(configuration()->nodes()->size()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 378 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 381 | LogReader::~LogReader() { |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 382 | if (event_loop_factory_unique_ptr_) { |
| 383 | Deregister(); |
| 384 | } else if (event_loop_factory_ != nullptr) { |
| 385 | LOG(FATAL) << "Must call Deregister before the SimulatedEventLoopFactory " |
| 386 | "is destroyed"; |
| 387 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 388 | // Zero out some buffers. It's easy to do use-after-frees on these, so make |
| 389 | // it more obvious. |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 390 | if (remapped_configuration_buffer_) { |
| 391 | remapped_configuration_buffer_->Wipe(); |
| 392 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 393 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 394 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 395 | const Configuration *LogReader::logged_configuration() const { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 396 | return log_files_[0].config.get(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 397 | } |
| 398 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 399 | const Configuration *LogReader::configuration() const { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 400 | return remapped_configuration_; |
| 401 | } |
| 402 | |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 403 | std::vector<const Node *> LogReader::LoggedNodes() const { |
| 404 | return configuration::GetNodes(logged_configuration()); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 405 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 406 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 407 | monotonic_clock::time_point LogReader::monotonic_start_time( |
| 408 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 409 | State *state = |
| 410 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 411 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 412 | |
Austin Schuh | f665eb4 | 2022-02-03 18:26:25 -0800 | [diff] [blame] | 413 | return state->monotonic_start_time(state->boot_count()); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 416 | realtime_clock::time_point LogReader::realtime_start_time( |
| 417 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 418 | State *state = |
| 419 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 420 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 421 | |
Austin Schuh | f665eb4 | 2022-02-03 18:26:25 -0800 | [diff] [blame] | 422 | return state->realtime_start_time(state->boot_count()); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 425 | void LogReader::OnStart(std::function<void()> fn) { |
| 426 | CHECK(!configuration::MultiNode(configuration())); |
| 427 | OnStart(nullptr, std::move(fn)); |
| 428 | } |
| 429 | |
| 430 | void LogReader::OnStart(const Node *node, std::function<void()> fn) { |
| 431 | const int node_index = configuration::GetNodeIndex(configuration(), node); |
| 432 | CHECK_GE(node_index, 0); |
| 433 | CHECK_LT(node_index, static_cast<int>(states_.size())); |
| 434 | State *state = states_[node_index].get(); |
| 435 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 436 | |
| 437 | state->OnStart(std::move(fn)); |
| 438 | } |
| 439 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 440 | void LogReader::State::QueueThreadUntil(BootTimestamp time) { |
| 441 | if (threading_ == ThreadedBuffering::kYes) { |
| 442 | CHECK(!message_queuer_.has_value()) << "Can't start thread twice."; |
| 443 | message_queuer_.emplace( |
| 444 | [this](const BootTimestamp queue_until) { |
| 445 | // This will be called whenever anything prompts us for any state |
| 446 | // change; there may be wakeups that result in us not having any new |
| 447 | // data to push (even if we aren't done), in which case we will return |
| 448 | // nullopt but not done(). |
| 449 | if (last_queued_message_.has_value() && |
| 450 | queue_until < last_queued_message_) { |
| 451 | return util::ThreadedQueue<TimestampedMessage, |
| 452 | BootTimestamp>::PushResult{ |
| 453 | std::nullopt, false, |
| 454 | last_queued_message_ == BootTimestamp::max_time()}; |
| 455 | } |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 456 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 457 | TimestampedMessage *message = timestamp_mapper_->Front(); |
| 458 | // Upon reaching the end of the log, exit. |
| 459 | if (message == nullptr) { |
| 460 | last_queued_message_ = BootTimestamp::max_time(); |
| 461 | return util::ThreadedQueue<TimestampedMessage, |
| 462 | BootTimestamp>::PushResult{std::nullopt, |
| 463 | false, true}; |
| 464 | } |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 465 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 466 | last_queued_message_ = message->monotonic_event_time; |
| 467 | const util::ThreadedQueue<TimestampedMessage, |
| 468 | BootTimestamp>::PushResult result{ |
| 469 | *message, queue_until >= last_queued_message_, false}; |
| 470 | timestamp_mapper_->PopFront(); |
| 471 | SeedSortedMessages(); |
| 472 | return result; |
| 473 | }, |
| 474 | time); |
| 475 | // Spin until the first few seconds of messages are queued up so that we |
| 476 | // don't end up with delays/inconsistent timing during the first few seconds |
| 477 | // of replay. |
| 478 | message_queuer_->WaitForNoMoreWork(); |
| 479 | } |
| 480 | } |
| 481 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 482 | void LogReader::State::OnStart(std::function<void()> fn) { |
| 483 | on_starts_.emplace_back(std::move(fn)); |
| 484 | } |
| 485 | |
| 486 | void LogReader::State::RunOnStart() { |
| 487 | SetRealtimeOffset(monotonic_start_time(boot_count()), |
| 488 | realtime_start_time(boot_count())); |
| 489 | |
| 490 | VLOG(1) << "Starting " << MaybeNodeName(node()) << "at time " |
| 491 | << monotonic_start_time(boot_count()); |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 492 | auto fn = [this]() { |
| 493 | for (size_t i = 0; i < on_starts_.size(); ++i) { |
| 494 | on_starts_[i](); |
| 495 | } |
| 496 | }; |
| 497 | if (event_loop_factory_) { |
| 498 | event_loop_factory_->AllowApplicationCreationDuring(std::move(fn)); |
| 499 | } else { |
| 500 | fn(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 501 | } |
| 502 | stopped_ = false; |
| 503 | started_ = true; |
| 504 | } |
| 505 | |
| 506 | void LogReader::OnEnd(std::function<void()> fn) { |
| 507 | CHECK(!configuration::MultiNode(configuration())); |
| 508 | OnEnd(nullptr, std::move(fn)); |
| 509 | } |
| 510 | |
| 511 | void LogReader::OnEnd(const Node *node, std::function<void()> fn) { |
| 512 | const int node_index = configuration::GetNodeIndex(configuration(), node); |
| 513 | CHECK_GE(node_index, 0); |
| 514 | CHECK_LT(node_index, static_cast<int>(states_.size())); |
| 515 | State *state = states_[node_index].get(); |
| 516 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 517 | |
| 518 | state->OnEnd(std::move(fn)); |
| 519 | } |
| 520 | |
| 521 | void LogReader::State::OnEnd(std::function<void()> fn) { |
| 522 | on_ends_.emplace_back(std::move(fn)); |
| 523 | } |
| 524 | |
| 525 | void LogReader::State::RunOnEnd() { |
| 526 | VLOG(1) << "Ending " << MaybeNodeName(node()) << "at time " |
| 527 | << monotonic_start_time(boot_count()); |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 528 | auto fn = [this]() { |
| 529 | for (size_t i = 0; i < on_ends_.size(); ++i) { |
| 530 | on_ends_[i](); |
| 531 | } |
| 532 | }; |
| 533 | if (event_loop_factory_) { |
| 534 | event_loop_factory_->AllowApplicationCreationDuring(std::move(fn)); |
| 535 | } else { |
| 536 | fn(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | stopped_ = true; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 540 | started_ = true; |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 541 | if (message_queuer_.has_value()) { |
| 542 | message_queuer_->StopPushing(); |
| 543 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 544 | } |
| 545 | |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 546 | std::vector< |
| 547 | std::pair<const aos::Channel *, NodeEventLoopFactory::ExclusiveSenders>> |
| 548 | LogReader::State::NonExclusiveChannels() { |
| 549 | CHECK_NOTNULL(node_event_loop_factory_); |
| 550 | const aos::Configuration *config = node_event_loop_factory_->configuration(); |
| 551 | std::vector< |
| 552 | std::pair<const aos::Channel *, NodeEventLoopFactory::ExclusiveSenders>> |
| 553 | result{// Timing reports can be sent by logged and replayed applications. |
| 554 | {aos::configuration::GetChannel(config, "/aos", |
| 555 | "aos.timing.Report", "", node_), |
| 556 | NodeEventLoopFactory::ExclusiveSenders::kNo}, |
| 557 | // AOS_LOG may be used in the log and in replay. |
| 558 | {aos::configuration::GetChannel( |
| 559 | config, "/aos", "aos.logging.LogMessageFbs", "", node_), |
| 560 | NodeEventLoopFactory::ExclusiveSenders::kNo}}; |
| 561 | for (const Node *const node : configuration::GetNodes(config)) { |
| 562 | if (node == nullptr) { |
| 563 | break; |
| 564 | } |
| 565 | const Channel *const old_timestamp_channel = aos::configuration::GetChannel( |
| 566 | config, |
| 567 | absl::StrCat("/aos/remote_timestamps/", node->name()->string_view()), |
James Kuszmaul | a90f324 | 2022-08-03 13:39:59 -0700 | [diff] [blame] | 568 | "aos.message_bridge.RemoteMessage", "", node_, /*quiet=*/true); |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 569 | // The old-style remote timestamp channel can be populated from any |
| 570 | // channel, simulated or replayed. |
| 571 | if (old_timestamp_channel != nullptr) { |
| 572 | result.push_back(std::make_pair( |
| 573 | old_timestamp_channel, NodeEventLoopFactory::ExclusiveSenders::kNo)); |
| 574 | } |
| 575 | } |
| 576 | // Remove any channels that weren't found due to not existing in the |
| 577 | // config. |
| 578 | for (size_t ii = 0; ii < result.size();) { |
| 579 | if (result[ii].first == nullptr) { |
| 580 | result.erase(result.begin() + ii); |
| 581 | } else { |
| 582 | ++ii; |
| 583 | } |
| 584 | } |
| 585 | return result; |
| 586 | } |
| 587 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 588 | void LogReader::Register() { |
| 589 | event_loop_factory_unique_ptr_ = |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 590 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 591 | Register(event_loop_factory_unique_ptr_.get()); |
| 592 | } |
| 593 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 594 | void LogReader::RegisterWithoutStarting( |
| 595 | SimulatedEventLoopFactory *event_loop_factory) { |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 596 | event_loop_factory_ = event_loop_factory; |
Austin Schuh | e5bbd9e | 2020-09-21 17:29:20 -0700 | [diff] [blame] | 597 | remapped_configuration_ = event_loop_factory_->configuration(); |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 598 | filters_ = |
| 599 | std::make_unique<message_bridge::MultiNodeNoncausalOffsetEstimator>( |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 600 | event_loop_factory_->configuration(), logged_configuration(), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 601 | log_files_[0].boots, FLAGS_skip_order_validation, |
Austin Schuh | fe3fb34 | 2021-01-16 18:50:37 -0800 | [diff] [blame] | 602 | chrono::duration_cast<chrono::nanoseconds>( |
| 603 | chrono::duration<double>(FLAGS_time_estimation_buffer_seconds))); |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 604 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 605 | std::vector<TimestampMapper *> timestamp_mappers; |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 606 | for (const Node *node : configuration::GetNodes(configuration())) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 607 | const size_t node_index = |
| 608 | configuration::GetNodeIndex(configuration(), node); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 609 | std::vector<LogParts> filtered_parts = FilterPartsForNode( |
| 610 | log_files_, node != nullptr ? node->name()->string_view() : ""); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 611 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 612 | // We don't run with threading on the buffering for simulated event loops |
| 613 | // because we haven't attempted to validate how the interactions beteen the |
| 614 | // buffering and the timestamp mapper works when running multiple nodes |
| 615 | // concurrently. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 616 | states_[node_index] = std::make_unique<State>( |
| 617 | filtered_parts.size() == 0u |
| 618 | ? nullptr |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 619 | : std::make_unique<TimestampMapper>(std::move(filtered_parts)), |
James Kuszmaul | b11a150 | 2022-07-01 16:02:25 -0700 | [diff] [blame] | 620 | filters_.get(), std::bind(&LogReader::NoticeRealtimeEnd, this), node, |
Naman Gupta | cf6d442 | 2023-03-01 11:41:00 -0800 | [diff] [blame] | 621 | State::ThreadedBuffering::kNo, MaybeMakeReplayChannelIndices(node)); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 622 | State *state = states_[node_index].get(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 623 | state->SetNodeEventLoopFactory( |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 624 | event_loop_factory_->GetNodeEventLoopFactory(node), |
| 625 | event_loop_factory_); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 626 | |
| 627 | state->SetChannelCount(logged_configuration()->channels()->size()); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 628 | timestamp_mappers.emplace_back(state->timestamp_mapper()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 629 | } |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 630 | filters_->SetTimestampMappers(std::move(timestamp_mappers)); |
| 631 | |
| 632 | // Note: this needs to be set before any times are pulled, or we won't observe |
| 633 | // the timestamps. |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 634 | event_loop_factory_->SetTimeConverter(filters_.get()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 635 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 636 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 637 | const size_t node_index = |
| 638 | configuration::GetNodeIndex(configuration(), node); |
| 639 | State *state = states_[node_index].get(); |
| 640 | for (const Node *other_node : configuration::GetNodes(configuration())) { |
| 641 | const size_t other_node_index = |
| 642 | configuration::GetNodeIndex(configuration(), other_node); |
| 643 | State *other_state = states_[other_node_index].get(); |
| 644 | if (other_state != state) { |
| 645 | state->AddPeer(other_state); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 650 | // Register after making all the State objects so we can build references |
| 651 | // between them. |
| 652 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 653 | const size_t node_index = |
| 654 | configuration::GetNodeIndex(configuration(), node); |
| 655 | State *state = states_[node_index].get(); |
| 656 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 657 | // If we didn't find any log files with data in them, we won't ever get a |
| 658 | // callback or be live. So skip the rest of the setup. |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 659 | if (state->SingleThreadedOldestMessageTime() == BootTimestamp::max_time()) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 660 | continue; |
| 661 | } |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 662 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 663 | ++live_nodes_; |
| 664 | |
| 665 | NodeEventLoopFactory *node_factory = |
| 666 | event_loop_factory_->GetNodeEventLoopFactory(node); |
| 667 | node_factory->OnStartup([this, state, node]() { |
| 668 | RegisterDuringStartup(state->MakeEventLoop(), node); |
| 669 | }); |
| 670 | node_factory->OnShutdown([this, state, node]() { |
| 671 | RegisterDuringStartup(nullptr, node); |
| 672 | state->DestroyEventLoop(); |
| 673 | }); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 674 | } |
| 675 | |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 676 | if (live_nodes_ == 0) { |
| 677 | LOG(FATAL) |
| 678 | << "Don't have logs from any of the nodes in the replay config--are " |
| 679 | "you sure that the replay config matches the original config?"; |
| 680 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 681 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 682 | filters_->CheckGraph(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 683 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 684 | for (std::unique_ptr<State> &state : states_) { |
| 685 | state->SeedSortedMessages(); |
| 686 | } |
| 687 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 688 | // Forwarding is tracked per channel. If it is enabled, we want to turn it |
| 689 | // off. Otherwise messages replayed will get forwarded across to the other |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 690 | // nodes, and also replayed on the other nodes. This may not satisfy all |
| 691 | // our users, but it'll start the discussion. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 692 | if (configuration::MultiNode(event_loop_factory_->configuration())) { |
| 693 | for (size_t i = 0; i < logged_configuration()->channels()->size(); ++i) { |
| 694 | const Channel *channel = logged_configuration()->channels()->Get(i); |
| 695 | const Node *node = configuration::GetNode( |
| 696 | configuration(), channel->source_node()->string_view()); |
| 697 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 698 | State *state = |
| 699 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 700 | |
| 701 | const Channel *remapped_channel = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 702 | RemapChannel(state->event_loop(), node, channel); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 703 | |
| 704 | event_loop_factory_->DisableForwarding(remapped_channel); |
| 705 | } |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 706 | |
| 707 | // If we are replaying a log, we don't want a bunch of redundant messages |
| 708 | // from both the real message bridge and simulated message bridge. |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 709 | event_loop_factory_->PermanentlyDisableStatistics(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 710 | } |
Austin Schuh | 891214d | 2021-11-11 20:35:02 -0800 | [diff] [blame] | 711 | |
| 712 | // Write pseudo start times out to file now that we are all setup. |
| 713 | filters_->Start(event_loop_factory_); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | void LogReader::Register(SimulatedEventLoopFactory *event_loop_factory) { |
| 717 | RegisterWithoutStarting(event_loop_factory); |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 718 | StartAfterRegister(event_loop_factory); |
| 719 | } |
| 720 | |
| 721 | void LogReader::StartAfterRegister( |
| 722 | SimulatedEventLoopFactory *event_loop_factory) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 723 | // We want to start the log file at the last start time of the log files |
| 724 | // from all the nodes. Compute how long each node's simulation needs to run |
| 725 | // to move time to this point. |
| 726 | distributed_clock::time_point start_time = distributed_clock::min_time; |
| 727 | |
| 728 | // TODO(austin): We want an "OnStart" callback for each node rather than |
| 729 | // running until the last node. |
| 730 | |
| 731 | for (std::unique_ptr<State> &state : states_) { |
| 732 | VLOG(1) << "Start time is " << state->monotonic_start_time(0) |
| 733 | << " for node " << MaybeNodeName(state->node()) << "now " |
| 734 | << state->monotonic_now(); |
| 735 | if (state->monotonic_start_time(0) == monotonic_clock::min_time) { |
| 736 | continue; |
| 737 | } |
| 738 | // And start computing the start time on the distributed clock now that |
| 739 | // that works. |
| 740 | start_time = std::max( |
| 741 | start_time, state->ToDistributedClock(state->monotonic_start_time(0))); |
| 742 | } |
| 743 | |
| 744 | // TODO(austin): If a node doesn't have a start time, we might not queue |
| 745 | // enough. If this happens, we'll explode with a frozen error eventually. |
| 746 | |
| 747 | CHECK_GE(start_time, distributed_clock::epoch()) |
| 748 | << ": Hmm, we have a node starting before the start of time. Offset " |
| 749 | "everything."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 750 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 751 | // While we are starting the system up, we might be relying on matching data |
| 752 | // to timestamps on log files where the timestamp log file starts before the |
| 753 | // data. In this case, it is reasonable to expect missing data. |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 754 | { |
| 755 | const bool prior_ignore_missing_data = ignore_missing_data_; |
| 756 | ignore_missing_data_ = true; |
| 757 | VLOG(1) << "Running until " << start_time << " in Register"; |
| 758 | event_loop_factory_->RunFor(start_time.time_since_epoch()); |
| 759 | VLOG(1) << "At start time"; |
| 760 | // Now that we are running for real, missing data means that the log file is |
| 761 | // corrupted or went wrong. |
| 762 | ignore_missing_data_ = prior_ignore_missing_data; |
| 763 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 764 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 765 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 766 | // Make the RT clock be correct before handing it to the user. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 767 | if (state->realtime_start_time(0) != realtime_clock::min_time) { |
| 768 | state->SetRealtimeOffset(state->monotonic_start_time(0), |
| 769 | state->realtime_start_time(0)); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 770 | } |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 771 | VLOG(1) << "Start time is " << state->monotonic_start_time(0) |
| 772 | << " for node " << MaybeNodeName(state->event_loop()->node()) |
| 773 | << "now " << state->monotonic_now(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | if (FLAGS_timestamps_to_csv) { |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 777 | filters_->Start(event_loop_factory); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 781 | message_bridge::NoncausalOffsetEstimator *LogReader::GetFilter( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 782 | const Node *node_a, const Node *node_b) { |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 783 | if (filters_) { |
| 784 | return filters_->GetFilter(node_a, node_b); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 785 | } |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 786 | return nullptr; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 787 | } |
| 788 | |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 789 | // TODO(jkuszmaul): Make in-line modifications to |
| 790 | // ServerStatistics/ClientStatistics messages for ShmEventLoop-based replay to |
| 791 | // avoid messing up anything that depends on them having valid offsets. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 792 | void LogReader::Register(EventLoop *event_loop) { |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 793 | filters_ = |
| 794 | std::make_unique<message_bridge::MultiNodeNoncausalOffsetEstimator>( |
| 795 | event_loop->configuration(), logged_configuration(), |
| 796 | log_files_[0].boots, FLAGS_skip_order_validation, |
| 797 | chrono::duration_cast<chrono::nanoseconds>( |
| 798 | chrono::duration<double>(FLAGS_time_estimation_buffer_seconds))); |
| 799 | |
| 800 | std::vector<TimestampMapper *> timestamp_mappers; |
| 801 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 802 | const size_t node_index = |
| 803 | configuration::GetNodeIndex(configuration(), node); |
| 804 | std::vector<LogParts> filtered_parts = FilterPartsForNode( |
| 805 | log_files_, node != nullptr ? node->name()->string_view() : ""); |
| 806 | |
| 807 | states_[node_index] = std::make_unique<State>( |
| 808 | filtered_parts.size() == 0u |
| 809 | ? nullptr |
| 810 | : std::make_unique<TimestampMapper>(std::move(filtered_parts)), |
James Kuszmaul | b11a150 | 2022-07-01 16:02:25 -0700 | [diff] [blame] | 811 | filters_.get(), std::bind(&LogReader::NoticeRealtimeEnd, this), node, |
Naman Gupta | cf6d442 | 2023-03-01 11:41:00 -0800 | [diff] [blame] | 812 | State::ThreadedBuffering::kYes, MaybeMakeReplayChannelIndices(node)); |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 813 | State *state = states_[node_index].get(); |
| 814 | |
| 815 | state->SetChannelCount(logged_configuration()->channels()->size()); |
| 816 | timestamp_mappers.emplace_back(state->timestamp_mapper()); |
| 817 | } |
| 818 | |
| 819 | filters_->SetTimestampMappers(std::move(timestamp_mappers)); |
| 820 | |
| 821 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 822 | const size_t node_index = |
| 823 | configuration::GetNodeIndex(configuration(), node); |
| 824 | State *state = states_[node_index].get(); |
| 825 | for (const Node *other_node : configuration::GetNodes(configuration())) { |
| 826 | const size_t other_node_index = |
| 827 | configuration::GetNodeIndex(configuration(), other_node); |
| 828 | State *other_state = states_[other_node_index].get(); |
| 829 | if (other_state != state) { |
| 830 | state->AddPeer(other_state); |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 835 | if (node == nullptr || node->name()->string_view() == |
| 836 | event_loop->node()->name()->string_view()) { |
| 837 | Register(event_loop, event_loop->node()); |
| 838 | } else { |
| 839 | Register(nullptr, node); |
| 840 | } |
| 841 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | void LogReader::Register(EventLoop *event_loop, const Node *node) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 845 | State *state = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 846 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 847 | |
| 848 | // If we didn't find any log files with data in them, we won't ever get a |
| 849 | // callback or be live. So skip the rest of the setup. |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 850 | if (state->SingleThreadedOldestMessageTime() == BootTimestamp::max_time()) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 851 | return; |
| 852 | } |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 853 | |
| 854 | if (event_loop != nullptr) { |
| 855 | ++live_nodes_; |
| 856 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 857 | |
| 858 | if (event_loop_factory_ != nullptr) { |
| 859 | event_loop_factory_->GetNodeEventLoopFactory(node)->OnStartup( |
| 860 | [this, event_loop, node]() { |
| 861 | RegisterDuringStartup(event_loop, node); |
| 862 | }); |
| 863 | } else { |
| 864 | RegisterDuringStartup(event_loop, node); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | void LogReader::RegisterDuringStartup(EventLoop *event_loop, const Node *node) { |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 869 | if (event_loop != nullptr) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 870 | CHECK(event_loop->configuration() == configuration()); |
| 871 | } |
| 872 | |
| 873 | State *state = |
| 874 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 875 | |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 876 | if (event_loop == nullptr) { |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 877 | state->ClearTimeFlags(); |
| 878 | } |
| 879 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 880 | state->set_event_loop(event_loop); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 881 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 882 | // We don't run timing reports when trying to print out logged data, because |
| 883 | // otherwise we would end up printing out the timing reports themselves... |
| 884 | // This is only really relevant when we are replaying into a simulation. |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 885 | if (event_loop != nullptr) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 886 | event_loop->SkipTimingReport(); |
| 887 | event_loop->SkipAosLog(); |
| 888 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 889 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 890 | for (size_t logged_channel_index = 0; |
| 891 | logged_channel_index < logged_configuration()->channels()->size(); |
| 892 | ++logged_channel_index) { |
| 893 | const Channel *channel = RemapChannel( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 894 | event_loop, node, |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 895 | logged_configuration()->channels()->Get(logged_channel_index)); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 896 | |
Austin Schuh | c0b6c4f | 2021-10-11 18:28:38 -0700 | [diff] [blame] | 897 | const bool logged = channel->logger() != LoggerConfig::NOT_LOGGED; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 898 | message_bridge::NoncausalOffsetEstimator *filter = nullptr; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 899 | |
| 900 | State *source_state = nullptr; |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 901 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 902 | if (!configuration::ChannelIsSendableOnNode(channel, node) && |
| 903 | configuration::ChannelIsReadableOnNode(channel, node)) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 904 | const Node *source_node = configuration::GetNode( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 905 | configuration(), channel->source_node()->string_view()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 906 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 907 | // We've got a message which is being forwarded to this node. |
| 908 | filter = GetFilter(node, source_node); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 909 | |
| 910 | source_state = |
| 911 | states_[configuration::GetNodeIndex(configuration(), source_node)] |
| 912 | .get(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 913 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 914 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 915 | // We are the source, and it is forwarded. |
| 916 | const bool is_forwarded = |
| 917 | configuration::ChannelIsSendableOnNode(channel, node) && |
| 918 | configuration::ConnectionCount(channel); |
| 919 | |
Austin Schuh | c0b6c4f | 2021-10-11 18:28:38 -0700 | [diff] [blame] | 920 | state->SetChannel( |
| 921 | logged_channel_index, |
| 922 | configuration::ChannelIndex(configuration(), channel), |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 923 | event_loop && logged && |
| 924 | configuration::ChannelIsReadableOnNode(channel, node) |
| 925 | ? event_loop->MakeRawSender(channel) |
| 926 | : nullptr, |
Austin Schuh | c0b6c4f | 2021-10-11 18:28:38 -0700 | [diff] [blame] | 927 | filter, is_forwarded, source_state); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 928 | |
Austin Schuh | c0b6c4f | 2021-10-11 18:28:38 -0700 | [diff] [blame] | 929 | if (is_forwarded && logged) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 930 | const Node *source_node = configuration::GetNode( |
| 931 | configuration(), channel->source_node()->string_view()); |
| 932 | |
| 933 | for (const Connection *connection : *channel->destination_nodes()) { |
| 934 | const bool delivery_time_is_logged = |
| 935 | configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection, |
| 936 | source_node); |
| 937 | |
| 938 | if (delivery_time_is_logged) { |
| 939 | State *destination_state = |
| 940 | states_[configuration::GetNodeIndex( |
| 941 | configuration(), connection->name()->string_view())] |
| 942 | .get(); |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 943 | if (destination_state) { |
| 944 | destination_state->SetRemoteTimestampSender( |
| 945 | logged_channel_index, |
| 946 | event_loop ? state->RemoteTimestampSender(channel, connection) |
| 947 | : nullptr); |
| 948 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 954 | if (!event_loop) { |
| 955 | state->ClearRemoteTimestampSenders(); |
| 956 | state->set_timer_handler(nullptr); |
| 957 | state->set_startup_timer(nullptr); |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 958 | return; |
| 959 | } |
| 960 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 961 | state->set_timer_handler(event_loop->AddTimer([this, state]() { |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 962 | if (state->MultiThreadedOldestMessageTime() == BootTimestamp::max_time()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 963 | --live_nodes_; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 964 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Node down!"; |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 965 | if (exit_on_finish_ && live_nodes_ == 0 && |
| 966 | event_loop_factory_ != nullptr) { |
James Kuszmaul | b11a150 | 2022-07-01 16:02:25 -0700 | [diff] [blame] | 967 | event_loop_factory_->Exit(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 968 | } |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 969 | return; |
| 970 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 971 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 972 | TimestampedMessage timestamped_message = state->PopOldest(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 973 | |
| 974 | CHECK_EQ(timestamped_message.monotonic_event_time.boot, |
| 975 | state->boot_count()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 976 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 977 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 978 | state->event_loop()->context().monotonic_event_time; |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 979 | if (event_loop_factory_ != nullptr) { |
| 980 | // Only enforce exact timing in simulation. |
| 981 | if (!FLAGS_skip_order_validation) { |
| 982 | CHECK(monotonic_now == timestamped_message.monotonic_event_time.time) |
| 983 | << ": " << FlatbufferToJson(state->event_loop()->node()) << " Now " |
| 984 | << monotonic_now << " trying to send " |
| 985 | << timestamped_message.monotonic_event_time << " failure " |
| 986 | << state->DebugString(); |
| 987 | } else if (BootTimestamp{.boot = state->boot_count(), |
| 988 | .time = monotonic_now} != |
| 989 | timestamped_message.monotonic_event_time) { |
| 990 | LOG(WARNING) << "Check failed: monotonic_now == " |
| 991 | "timestamped_message.monotonic_event_time) (" |
| 992 | << monotonic_now << " vs. " |
| 993 | << timestamped_message.monotonic_event_time |
| 994 | << "): " << FlatbufferToJson(state->event_loop()->node()) |
| 995 | << " Now " << monotonic_now << " trying to send " |
| 996 | << timestamped_message.monotonic_event_time << " failure " |
| 997 | << state->DebugString(); |
| 998 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 999 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1000 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1001 | if (timestamped_message.monotonic_event_time.time > |
| 1002 | state->monotonic_start_time( |
| 1003 | timestamped_message.monotonic_event_time.boot) || |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1004 | event_loop_factory_ != nullptr || |
| 1005 | !FLAGS_drop_realtime_messages_before_start) { |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 1006 | if (timestamped_message.data != nullptr && !state->found_last_message()) { |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1007 | if (timestamped_message.monotonic_remote_time != |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1008 | BootTimestamp::min_time() && |
| 1009 | !FLAGS_skip_order_validation && event_loop_factory_ != nullptr) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1010 | // Confirm that the message was sent on the sending node before the |
| 1011 | // destination node (this node). As a proxy, do this by making sure |
| 1012 | // 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] | 1013 | // |
| 1014 | // TODO(austin): <= means that the cause message (which we know) could |
| 1015 | // happen after the effect even though we know they are at the same |
| 1016 | // time. I doubt anyone will notice for a bit, but we should really |
| 1017 | // fix that. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1018 | BootTimestamp monotonic_remote_now = |
| 1019 | state->monotonic_remote_now(timestamped_message.channel_index); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1020 | if (!FLAGS_skip_order_validation) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1021 | CHECK_EQ(timestamped_message.monotonic_remote_time.boot, |
Austin Schuh | 3e20c69 | 2021-11-16 20:43:16 -0800 | [diff] [blame] | 1022 | monotonic_remote_now.boot) |
| 1023 | << state->event_loop()->node()->name()->string_view() << " to " |
| 1024 | << state->remote_node(timestamped_message.channel_index) |
| 1025 | ->name() |
| 1026 | ->string_view() |
| 1027 | << " while trying to send a message on " |
| 1028 | << configuration::CleanedChannelToString( |
| 1029 | logged_configuration()->channels()->Get( |
| 1030 | timestamped_message.channel_index)) |
| 1031 | << " " << timestamped_message << " " << state->DebugString(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1032 | CHECK_LE(timestamped_message.monotonic_remote_time, |
| 1033 | monotonic_remote_now) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1034 | << state->event_loop()->node()->name()->string_view() << " to " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1035 | << state->remote_node(timestamped_message.channel_index) |
| 1036 | ->name() |
| 1037 | ->string_view() |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1038 | << " while trying to send a message on " |
| 1039 | << configuration::CleanedChannelToString( |
| 1040 | logged_configuration()->channels()->Get( |
| 1041 | timestamped_message.channel_index)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1042 | << " " << state->DebugString(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1043 | } else if (monotonic_remote_now.boot != |
| 1044 | timestamped_message.monotonic_remote_time.boot) { |
| 1045 | LOG(WARNING) << "Missmatched boots, " << monotonic_remote_now.boot |
| 1046 | << " vs " |
| 1047 | << timestamped_message.monotonic_remote_time.boot; |
| 1048 | } else if (timestamped_message.monotonic_remote_time > |
| 1049 | monotonic_remote_now) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1050 | LOG(WARNING) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1051 | << "Check failed: timestamped_message.monotonic_remote_time < " |
| 1052 | "state->monotonic_remote_now(timestamped_message.channel_" |
| 1053 | "index) (" |
| 1054 | << timestamped_message.monotonic_remote_time << " vs. " |
| 1055 | << state->monotonic_remote_now( |
| 1056 | timestamped_message.channel_index) |
| 1057 | << ") " << state->event_loop()->node()->name()->string_view() |
| 1058 | << " to " |
| 1059 | << state->remote_node(timestamped_message.channel_index) |
| 1060 | ->name() |
| 1061 | ->string_view() |
| 1062 | << " currently " << timestamped_message.monotonic_event_time |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1063 | << " (" |
| 1064 | << state->ToDistributedClock( |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1065 | timestamped_message.monotonic_event_time.time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1066 | << ") remote event time " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1067 | << timestamped_message.monotonic_remote_time << " (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1068 | << state->RemoteToDistributedClock( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1069 | timestamped_message.channel_index, |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1070 | timestamped_message.monotonic_remote_time.time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1071 | << ") " << state->DebugString(); |
| 1072 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1075 | // 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] | 1076 | state->SetRealtimeOffset(timestamped_message.monotonic_event_time.time, |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1077 | timestamped_message.realtime_event_time); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1078 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1079 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Sending " |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1080 | << timestamped_message.monotonic_event_time << " " |
| 1081 | << state->DebugString(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1082 | // TODO(austin): std::move channel_data in and make that efficient in |
| 1083 | // simulation. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1084 | state->Send(std::move(timestamped_message)); |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 1085 | } else if (state->found_last_message() || |
| 1086 | (!ignore_missing_data_ && |
| 1087 | // When starting up, we can have data which was sent before |
| 1088 | // the log starts, but the timestamp was after the log |
| 1089 | // starts. This is unreasonable to avoid, so ignore the |
| 1090 | // missing data. |
| 1091 | timestamped_message.monotonic_remote_time.time >= |
| 1092 | state->monotonic_remote_start_time( |
| 1093 | timestamped_message.monotonic_remote_time.boot, |
| 1094 | timestamped_message.channel_index) && |
| 1095 | !FLAGS_skip_missing_forwarding_entries)) { |
| 1096 | if (!state->found_last_message()) { |
| 1097 | // We've found a timestamp without data that we expect to have data |
| 1098 | // for. This likely means that we are at the end of the log file. |
| 1099 | // Record it and CHECK that in the rest of the log file, we don't find |
| 1100 | // any more data on that channel. Not all channels will end at the |
| 1101 | // same point in time since they can be in different files. |
| 1102 | VLOG(1) << "Found the last message on channel " |
| 1103 | << timestamped_message.channel_index << ", " |
| 1104 | << configuration::CleanedChannelToString( |
| 1105 | logged_configuration()->channels()->Get( |
| 1106 | timestamped_message.channel_index)) |
| 1107 | << " on node " << MaybeNodeName(state->event_loop()->node()) |
| 1108 | << timestamped_message; |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1109 | |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 1110 | // The user might be working with log files from 1 node but forgot to |
| 1111 | // configure the infrastructure to log data for a remote channel on |
| 1112 | // that node. That can be very hard to debug, even though the log |
| 1113 | // reader is doing the right thing. At least log a warning in that |
| 1114 | // case and tell the user what is happening so they can either update |
| 1115 | // their config to log the channel or can find a log with the data. |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1116 | const std::vector<std::string> logger_nodes = |
| 1117 | FindLoggerNodes(log_files_); |
| 1118 | if (logger_nodes.size()) { |
| 1119 | // We have old logs which don't have the logger nodes logged. In |
| 1120 | // that case, we can't be helpful :( |
| 1121 | bool data_logged = false; |
| 1122 | const Channel *channel = logged_configuration()->channels()->Get( |
| 1123 | timestamped_message.channel_index); |
| 1124 | for (const std::string &node : logger_nodes) { |
| 1125 | data_logged |= |
| 1126 | configuration::ChannelMessageIsLoggedOnNode(channel, node); |
| 1127 | } |
| 1128 | if (!data_logged) { |
| 1129 | LOG(WARNING) << "Got a timestamp without any logfiles which " |
| 1130 | "could contain data for channel " |
| 1131 | << configuration::CleanedChannelToString(channel); |
| 1132 | LOG(WARNING) << "Only have logs logged on [" |
| 1133 | << absl::StrJoin(logger_nodes, ", ") << "]"; |
| 1134 | LOG(WARNING) |
| 1135 | << "Dropping the rest of the data on " |
| 1136 | << state->event_loop()->node()->name()->string_view(); |
| 1137 | LOG(WARNING) |
| 1138 | << "Consider using --skip_missing_forwarding_entries to " |
| 1139 | "bypass this, update your config to log it, or add data " |
| 1140 | "from one of the nodes it is logged on."; |
| 1141 | } |
| 1142 | } |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 1143 | // Now that we found the end of one channel, artificially stop the |
| 1144 | // rest by setting the found_last_message bit. It is confusing when |
| 1145 | // part of your data gets replayed but not all. The rest of them will |
| 1146 | // get dropped as they are replayed to keep memory usage down. |
| 1147 | state->SetFoundLastMessage(true); |
| 1148 | |
| 1149 | // Vector storing if we've seen a nullptr message or not per channel. |
| 1150 | state->set_last_message(timestamped_message.channel_index); |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 1153 | // Make sure that once we have seen the last message on a channel, |
| 1154 | // data doesn't start back up again. If the user wants to play |
| 1155 | // through events like this, they can set |
| 1156 | // --skip_missing_forwarding_entries or ignore_missing_data_. |
| 1157 | if (timestamped_message.data == nullptr) { |
| 1158 | state->set_last_message(timestamped_message.channel_index); |
| 1159 | } else { |
| 1160 | if (state->last_message(timestamped_message.channel_index)) { |
| 1161 | LOG(FATAL) << "Found missing data in the middle of the log file on " |
| 1162 | "channel " |
| 1163 | << timestamped_message.channel_index << " " |
| 1164 | << configuration::StrippedChannelToString( |
| 1165 | logged_configuration()->channels()->Get( |
| 1166 | timestamped_message.channel_index)) |
| 1167 | << " " << timestamped_message << " " |
| 1168 | << state->DebugString(); |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1169 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1170 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1171 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1172 | } else { |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1173 | LOG(WARNING) |
| 1174 | << "Not sending data from before the start of the log file. " |
| 1175 | << timestamped_message.monotonic_event_time.time.time_since_epoch() |
| 1176 | .count() |
| 1177 | << " start " |
| 1178 | << monotonic_start_time(state->node()).time_since_epoch().count() |
| 1179 | << " timestamped_message.data is null"; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 1182 | const BootTimestamp next_time = state->MultiThreadedOldestMessageTime(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1183 | if (next_time != BootTimestamp::max_time()) { |
| 1184 | if (next_time.boot != state->boot_count()) { |
| 1185 | VLOG(1) << "Next message for " |
| 1186 | << MaybeNodeName(state->event_loop()->node()) |
| 1187 | << "is on the next boot, " << next_time << " now is " |
| 1188 | << state->monotonic_now(); |
| 1189 | CHECK(event_loop_factory_); |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1190 | state->NotifyLogfileEnd(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1191 | return; |
| 1192 | } |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1193 | if (event_loop_factory_ != nullptr) { |
| 1194 | VLOG(1) << "Scheduling " << MaybeNodeName(state->event_loop()->node()) |
| 1195 | << "wakeup for " << next_time.time << "(" |
| 1196 | << state->ToDistributedClock(next_time.time) |
| 1197 | << " distributed), now is " << state->monotonic_now(); |
| 1198 | } else { |
| 1199 | VLOG(1) << "Scheduling " << MaybeNodeName(state->event_loop()->node()) |
| 1200 | << "wakeup for " << next_time.time << ", now is " |
| 1201 | << state->monotonic_now(); |
| 1202 | } |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 1203 | // TODO(james): This can result in negative times getting passed-through |
| 1204 | // in realtime replay. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1205 | state->Setup(next_time.time); |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 1206 | } else { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1207 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) |
| 1208 | << "No next message, scheduling shutdown"; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1209 | state->NotifyLogfileEnd(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1210 | // Set a timer up immediately after now to die. If we don't do this, |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1211 | // then the watchers waiting on the message we just read will never get |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1212 | // called. |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1213 | // Doesn't apply to single-EventLoop replay since the watchers in question |
| 1214 | // are not under our control. |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 1215 | if (event_loop_factory_ != nullptr) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1216 | state->Setup(monotonic_now + event_loop_factory_->send_delay() + |
| 1217 | std::chrono::nanoseconds(1)); |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 1218 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1219 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1220 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1221 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Done sending at " |
| 1222 | << state->event_loop()->context().monotonic_event_time << " now " |
| 1223 | << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1224 | })); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1225 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 1226 | state->SeedSortedMessages(); |
| 1227 | |
| 1228 | if (state->SingleThreadedOldestMessageTime() != BootTimestamp::max_time()) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1229 | state->set_startup_timer( |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1230 | event_loop->AddTimer([state]() { state->NotifyLogfileStart(); })); |
| 1231 | if (start_time_ != realtime_clock::min_time) { |
| 1232 | state->SetStartTimeFlag(start_time_); |
| 1233 | } |
| 1234 | if (end_time_ != realtime_clock::max_time) { |
| 1235 | state->SetEndTimeFlag(end_time_); |
James Kuszmaul | b11a150 | 2022-07-01 16:02:25 -0700 | [diff] [blame] | 1236 | ++live_nodes_with_realtime_time_end_; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1237 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1238 | event_loop->OnRun([state]() { |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 1239 | BootTimestamp next_time = state->SingleThreadedOldestMessageTime(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1240 | CHECK_EQ(next_time.boot, state->boot_count()); |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 1241 | // Queue up messages and then set clock offsets (we don't want to set |
| 1242 | // clock offsets before we've done the work of getting the first messages |
| 1243 | // primed). |
| 1244 | state->QueueThreadUntil( |
| 1245 | next_time + std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 1246 | std::chrono::duration<double>( |
| 1247 | FLAGS_threaded_look_ahead_seconds))); |
James Kuszmaul | c3f34d1 | 2022-08-15 15:57:55 -0700 | [diff] [blame] | 1248 | state->MaybeSetClockOffset(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1249 | state->Setup(next_time.time); |
| 1250 | state->SetupStartupTimer(); |
| 1251 | }); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1252 | } |
| 1253 | } |
| 1254 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1255 | void LogReader::SetEndTime(std::string end_time) { |
| 1256 | if (end_time.empty()) { |
| 1257 | SetEndTime(realtime_clock::max_time); |
| 1258 | } else { |
| 1259 | std::optional<aos::realtime_clock::time_point> parsed_end_time = |
| 1260 | aos::realtime_clock::FromString(end_time); |
| 1261 | CHECK(parsed_end_time) << ": Failed to parse end time '" << end_time |
| 1262 | << "'. Expected a date in the format of " |
| 1263 | "2021-01-15_15-30-35.000000000."; |
| 1264 | SetEndTime(*parsed_end_time); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | void LogReader::SetEndTime(realtime_clock::time_point end_time) { |
| 1269 | end_time_ = end_time; |
| 1270 | } |
| 1271 | |
| 1272 | void LogReader::SetStartTime(std::string start_time) { |
| 1273 | if (start_time.empty()) { |
| 1274 | SetStartTime(realtime_clock::min_time); |
| 1275 | } else { |
| 1276 | std::optional<aos::realtime_clock::time_point> parsed_start_time = |
| 1277 | aos::realtime_clock::FromString(start_time); |
| 1278 | CHECK(parsed_start_time) << ": Failed to parse start time '" << start_time |
| 1279 | << "'. Expected a date in the format of " |
| 1280 | "2021-01-15_15-30-35.000000000."; |
| 1281 | SetStartTime(*parsed_start_time); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | void LogReader::SetStartTime(realtime_clock::time_point start_time) { |
| 1286 | start_time_ = start_time; |
| 1287 | } |
| 1288 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1289 | void LogReader::Deregister() { |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1290 | // Make sure that things get destroyed in the correct order, rather than |
| 1291 | // relying on getting the order correct in the class definition. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1292 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1293 | state->Deregister(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1294 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1295 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1296 | event_loop_factory_unique_ptr_.reset(); |
| 1297 | event_loop_factory_ = nullptr; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
James Kuszmaul | 53da7f3 | 2022-09-11 11:11:55 -0700 | [diff] [blame] | 1300 | namespace { |
| 1301 | // Checks if the specified channel name/type exists in the config and, depending |
| 1302 | // on the value of conflict_handling, calls conflict_handler or just dies. |
| 1303 | template <typename F> |
| 1304 | void CheckAndHandleRemapConflict(std::string_view new_name, |
| 1305 | std::string_view new_type, |
| 1306 | const Configuration *config, |
| 1307 | LogReader::RemapConflict conflict_handling, |
| 1308 | F conflict_handler) { |
| 1309 | const Channel *existing_channel = |
| 1310 | configuration::GetChannel(config, new_name, new_type, "", nullptr, true); |
| 1311 | if (existing_channel != nullptr) { |
| 1312 | switch (conflict_handling) { |
| 1313 | case LogReader::RemapConflict::kDisallow: |
| 1314 | LOG(FATAL) |
| 1315 | << "Channel " |
| 1316 | << configuration::StrippedChannelToString(existing_channel) |
| 1317 | << " is already used--you can't remap a logged channel to it."; |
| 1318 | break; |
| 1319 | case LogReader::RemapConflict::kCascade: |
| 1320 | LOG(INFO) << "Automatically remapping " |
| 1321 | << configuration::StrippedChannelToString(existing_channel) |
| 1322 | << " to avoid conflicts."; |
| 1323 | conflict_handler(); |
| 1324 | break; |
| 1325 | } |
| 1326 | } |
| 1327 | } |
| 1328 | } // namespace |
| 1329 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1330 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1331 | std::string_view add_prefix, |
James Kuszmaul | 53da7f3 | 2022-09-11 11:11:55 -0700 | [diff] [blame] | 1332 | std::string_view new_type, |
| 1333 | RemapConflict conflict_handling) { |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame^] | 1334 | RemapLoggedChannel(name, type, nullptr, add_prefix, new_type, |
| 1335 | conflict_handling); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1336 | } |
| 1337 | |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1338 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
| 1339 | const Node *node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1340 | std::string_view add_prefix, |
James Kuszmaul | 53da7f3 | 2022-09-11 11:11:55 -0700 | [diff] [blame] | 1341 | std::string_view new_type, |
| 1342 | RemapConflict conflict_handling) { |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame^] | 1343 | if (node != nullptr) { |
| 1344 | VLOG(1) << "Node is " << aos::FlatbufferToJson(node); |
| 1345 | } |
| 1346 | if (replay_channels_ != nullptr) { |
| 1347 | CHECK(std::find(replay_channels_->begin(), replay_channels_->end(), |
| 1348 | std::make_pair(std::string{name}, std::string{type})) != |
| 1349 | replay_channels_->end()) |
| 1350 | << "Attempted to remap channel " << name << " " << type |
| 1351 | << " which is not included in the replay channels passed to LogReader."; |
| 1352 | } |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1353 | const Channel *remapped_channel = |
| 1354 | configuration::GetChannel(logged_configuration(), name, type, "", node); |
| 1355 | CHECK(remapped_channel != nullptr) << ": Failed to find {\"name\": \"" << name |
| 1356 | << "\", \"type\": \"" << type << "\"}"; |
| 1357 | VLOG(1) << "Original {\"name\": \"" << name << "\", \"type\": \"" << type |
| 1358 | << "\"}"; |
| 1359 | VLOG(1) << "Remapped " |
| 1360 | << aos::configuration::StrippedChannelToString(remapped_channel); |
| 1361 | |
| 1362 | // We want to make /spray on node 0 go to /0/spray by snooping the maps. And |
| 1363 | // we want it to degrade if the heuristics fail to just work. |
| 1364 | // |
| 1365 | // The easiest way to do this is going to be incredibly specific and verbose. |
| 1366 | // Look up /spray, to /0/spray. Then, prefix the result with /original to get |
| 1367 | // /original/0/spray. Then, create a map from /original/spray to |
| 1368 | // /original/0/spray for just the type we were asked for. |
| 1369 | if (name != remapped_channel->name()->string_view()) { |
| 1370 | MapT new_map; |
| 1371 | new_map.match = std::make_unique<ChannelT>(); |
| 1372 | new_map.match->name = absl::StrCat(add_prefix, name); |
| 1373 | new_map.match->type = type; |
| 1374 | if (node != nullptr) { |
| 1375 | new_map.match->source_node = node->name()->str(); |
| 1376 | } |
| 1377 | new_map.rename = std::make_unique<ChannelT>(); |
| 1378 | new_map.rename->name = |
| 1379 | absl::StrCat(add_prefix, remapped_channel->name()->string_view()); |
| 1380 | maps_.emplace_back(std::move(new_map)); |
| 1381 | } |
| 1382 | |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame^] | 1383 | // Then remap the logged channel to the prefixed channel. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1384 | const size_t channel_index = |
| 1385 | configuration::ChannelIndex(logged_configuration(), remapped_channel); |
| 1386 | CHECK_EQ(0u, remapped_channels_.count(channel_index)) |
| 1387 | << "Already remapped channel " |
| 1388 | << configuration::CleanedChannelToString(remapped_channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1389 | |
| 1390 | RemappedChannel remapped_channel_struct; |
| 1391 | remapped_channel_struct.remapped_name = |
| 1392 | std::string(add_prefix) + |
| 1393 | std::string(remapped_channel->name()->string_view()); |
| 1394 | remapped_channel_struct.new_type = new_type; |
James Kuszmaul | 53da7f3 | 2022-09-11 11:11:55 -0700 | [diff] [blame] | 1395 | const std::string_view remapped_type = new_type.empty() ? type : new_type; |
| 1396 | CheckAndHandleRemapConflict( |
| 1397 | remapped_channel_struct.remapped_name, remapped_type, |
| 1398 | remapped_configuration_, conflict_handling, |
| 1399 | [this, &remapped_channel_struct, remapped_type, node, add_prefix, |
| 1400 | conflict_handling]() { |
| 1401 | RemapLoggedChannel(remapped_channel_struct.remapped_name, remapped_type, |
| 1402 | node, add_prefix, "", conflict_handling); |
| 1403 | }); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1404 | remapped_channels_[channel_index] = std::move(remapped_channel_struct); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1405 | MakeRemappedConfig(); |
| 1406 | } |
| 1407 | |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame^] | 1408 | void LogReader::RenameLoggedChannel(const std::string_view name, |
| 1409 | const std::string_view type, |
| 1410 | const std::string_view new_name, |
| 1411 | const std::vector<MapT> &add_maps) { |
| 1412 | RenameLoggedChannel(name, type, nullptr, new_name, add_maps); |
| 1413 | } |
| 1414 | |
| 1415 | void LogReader::RenameLoggedChannel(const std::string_view name, |
| 1416 | const std::string_view type, |
| 1417 | const Node *const node, |
| 1418 | const std::string_view new_name, |
| 1419 | const std::vector<MapT> &add_maps) { |
| 1420 | if (node != nullptr) { |
| 1421 | VLOG(1) << "Node is " << aos::FlatbufferToJson(node); |
| 1422 | } |
| 1423 | // First find the channel and rename it. |
| 1424 | const Channel *remapped_channel = |
| 1425 | configuration::GetChannel(logged_configuration(), name, type, "", node); |
| 1426 | CHECK(remapped_channel != nullptr) << ": Failed to find {\"name\": \"" << name |
| 1427 | << "\", \"type\": \"" << type << "\"}"; |
| 1428 | VLOG(1) << "Original {\"name\": \"" << name << "\", \"type\": \"" << type |
| 1429 | << "\"}"; |
| 1430 | VLOG(1) << "Remapped " |
| 1431 | << aos::configuration::StrippedChannelToString(remapped_channel); |
| 1432 | |
| 1433 | const size_t channel_index = |
| 1434 | configuration::ChannelIndex(logged_configuration(), remapped_channel); |
| 1435 | CHECK_EQ(0u, remapped_channels_.count(channel_index)) |
| 1436 | << "Already remapped channel " |
| 1437 | << configuration::CleanedChannelToString(remapped_channel); |
| 1438 | |
| 1439 | RemappedChannel remapped_channel_struct; |
| 1440 | remapped_channel_struct.remapped_name = new_name; |
| 1441 | remapped_channel_struct.new_type.clear(); |
| 1442 | remapped_channels_[channel_index] = std::move(remapped_channel_struct); |
| 1443 | |
| 1444 | // Then add any provided maps. |
| 1445 | for (const MapT &map : add_maps) { |
| 1446 | maps_.push_back(map); |
| 1447 | } |
| 1448 | |
| 1449 | // Finally rewrite the config. |
| 1450 | MakeRemappedConfig(); |
| 1451 | } |
| 1452 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1453 | void LogReader::MakeRemappedConfig() { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1454 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1455 | if (state) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1456 | CHECK(!state->event_loop()) |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1457 | << ": Can't change the mapping after the events are scheduled."; |
| 1458 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1459 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1460 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1461 | // If no remapping occurred and we are using the original config, then there |
| 1462 | // is nothing interesting to do here. |
| 1463 | if (remapped_channels_.empty() && replay_configuration_ == nullptr) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1464 | remapped_configuration_ = logged_configuration(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1465 | return; |
| 1466 | } |
| 1467 | // Config to copy Channel definitions from. Use the specified |
| 1468 | // replay_configuration_ if it has been provided. |
| 1469 | const Configuration *const base_config = replay_configuration_ == nullptr |
| 1470 | ? logged_configuration() |
| 1471 | : replay_configuration_; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1472 | |
| 1473 | // Create a config with all the channels, but un-sorted/merged. Collect up |
| 1474 | // the schemas while we do this. Call MergeConfiguration to sort everything, |
| 1475 | // and then merge it all in together. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1476 | |
| 1477 | // This is the builder that we use for the config containing all the new |
| 1478 | // channels. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1479 | flatbuffers::FlatBufferBuilder fbb; |
| 1480 | fbb.ForceDefaults(true); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1481 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1482 | |
| 1483 | CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 13u) |
| 1484 | << ": Merging logic needs to be updated when the number of channel " |
| 1485 | "fields changes."; |
| 1486 | |
| 1487 | // List of schemas. |
| 1488 | std::map<std::string_view, FlatbufferVector<reflection::Schema>> schema_map; |
| 1489 | // Make sure our new RemoteMessage schema is in there for old logs without it. |
| 1490 | schema_map.insert(std::make_pair( |
| 1491 | RemoteMessage::GetFullyQualifiedName(), |
| 1492 | FlatbufferVector<reflection::Schema>(FlatbufferSpan<reflection::Schema>( |
| 1493 | message_bridge::RemoteMessageSchema())))); |
| 1494 | |
| 1495 | // Reconstruct the remapped channels. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1496 | for (auto &pair : remapped_channels_) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1497 | const Channel *const c = CHECK_NOTNULL(configuration::GetChannel( |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1498 | base_config, logged_configuration()->channels()->Get(pair.first), "", |
| 1499 | nullptr)); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1500 | channel_offsets.emplace_back( |
| 1501 | CopyChannel(c, pair.second.remapped_name, "", &fbb)); |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 1502 | |
| 1503 | if (c->has_destination_nodes()) { |
| 1504 | for (const Connection *connection : *c->destination_nodes()) { |
| 1505 | switch (connection->timestamp_logger()) { |
| 1506 | case LoggerConfig::LOCAL_LOGGER: |
| 1507 | case LoggerConfig::NOT_LOGGED: |
| 1508 | // There is no timestamp channel associated with this, so ignore it. |
| 1509 | break; |
| 1510 | |
| 1511 | case LoggerConfig::REMOTE_LOGGER: |
| 1512 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 1513 | // We want to make a split timestamp channel regardless of what type |
| 1514 | // of log this used to be. No sense propagating the single |
| 1515 | // timestamp channel. |
| 1516 | |
| 1517 | CHECK(connection->has_timestamp_logger_nodes()); |
| 1518 | for (const flatbuffers::String *timestamp_logger_node : |
| 1519 | *connection->timestamp_logger_nodes()) { |
| 1520 | const Node *node = configuration::GetNode( |
| 1521 | logged_configuration(), timestamp_logger_node->string_view()); |
| 1522 | message_bridge::ChannelTimestampFinder finder( |
| 1523 | logged_configuration(), "log_reader", node); |
| 1524 | |
| 1525 | // We are assuming here that all the maps are setup correctly to |
| 1526 | // handle arbitrary timestamps. Apply the maps for this node to |
| 1527 | // see what name this ends up with. |
| 1528 | std::string name = finder.SplitChannelName( |
| 1529 | pair.second.remapped_name, c->type()->str(), connection); |
| 1530 | std::string unmapped_name = name; |
| 1531 | configuration::HandleMaps(logged_configuration()->maps(), &name, |
| 1532 | "aos.message_bridge.RemoteMessage", |
| 1533 | node); |
| 1534 | CHECK_NE(name, unmapped_name) |
| 1535 | << ": Remote timestamp channel was not remapped, this is " |
| 1536 | "very fishy"; |
| 1537 | flatbuffers::Offset<flatbuffers::String> channel_name_offset = |
| 1538 | fbb.CreateString(name); |
| 1539 | flatbuffers::Offset<flatbuffers::String> channel_type_offset = |
| 1540 | fbb.CreateString("aos.message_bridge.RemoteMessage"); |
| 1541 | flatbuffers::Offset<flatbuffers::String> source_node_offset = |
| 1542 | fbb.CreateString(timestamp_logger_node->string_view()); |
| 1543 | |
| 1544 | // Now, build a channel. Don't log it, 2 senders, and match the |
| 1545 | // source frequency. |
| 1546 | Channel::Builder channel_builder(fbb); |
| 1547 | channel_builder.add_name(channel_name_offset); |
| 1548 | channel_builder.add_type(channel_type_offset); |
| 1549 | channel_builder.add_source_node(source_node_offset); |
| 1550 | channel_builder.add_logger(LoggerConfig::NOT_LOGGED); |
| 1551 | channel_builder.add_num_senders(2); |
| 1552 | if (c->has_frequency()) { |
| 1553 | channel_builder.add_frequency(c->frequency()); |
| 1554 | } |
| 1555 | channel_offsets.emplace_back(channel_builder.Finish()); |
| 1556 | } |
| 1557 | break; |
| 1558 | } |
| 1559 | } |
| 1560 | } |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1561 | } |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1562 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1563 | // Now reconstruct the original channels, translating types as needed |
| 1564 | for (const Channel *c : *base_config->channels()) { |
| 1565 | // Search for a mapping channel. |
| 1566 | std::string_view new_type = ""; |
| 1567 | for (auto &pair : remapped_channels_) { |
| 1568 | const Channel *const remapped_channel = |
| 1569 | logged_configuration()->channels()->Get(pair.first); |
| 1570 | if (remapped_channel->name()->string_view() == c->name()->string_view() && |
| 1571 | remapped_channel->type()->string_view() == c->type()->string_view()) { |
| 1572 | new_type = pair.second.new_type; |
| 1573 | break; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | // Copy everything over. |
| 1578 | channel_offsets.emplace_back(CopyChannel(c, "", new_type, &fbb)); |
| 1579 | |
| 1580 | // Add the schema if it doesn't exist. |
| 1581 | if (schema_map.find(c->type()->string_view()) == schema_map.end()) { |
| 1582 | CHECK(c->has_schema()); |
| 1583 | schema_map.insert(std::make_pair(c->type()->string_view(), |
| 1584 | RecursiveCopyFlatBuffer(c->schema()))); |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | // The MergeConfiguration API takes a vector, not a map. Convert. |
| 1589 | std::vector<FlatbufferVector<reflection::Schema>> schemas; |
| 1590 | while (!schema_map.empty()) { |
| 1591 | schemas.emplace_back(std::move(schema_map.begin()->second)); |
| 1592 | schema_map.erase(schema_map.begin()); |
| 1593 | } |
| 1594 | |
| 1595 | // Create the Configuration containing the new channels that we want to add. |
| 1596 | const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 1597 | channels_offset = |
| 1598 | channel_offsets.empty() ? 0 : fbb.CreateVector(channel_offsets); |
| 1599 | |
| 1600 | // Copy over the old maps. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1601 | std::vector<flatbuffers::Offset<Map>> map_offsets; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1602 | if (base_config->maps()) { |
| 1603 | for (const Map *map : *base_config->maps()) { |
| 1604 | map_offsets.emplace_back(aos::RecursiveCopyFlatBuffer(map, &fbb)); |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | // 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] | 1609 | for (const MapT &map : maps_) { |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame^] | 1610 | CHECK(!map.match->name.empty()); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1611 | const flatbuffers::Offset<flatbuffers::String> match_name_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1612 | fbb.CreateString(map.match->name); |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame^] | 1613 | flatbuffers::Offset<flatbuffers::String> match_type_offset; |
| 1614 | if (!map.match->type.empty()) { |
| 1615 | match_type_offset = fbb.CreateString(map.match->type); |
| 1616 | } |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1617 | flatbuffers::Offset<flatbuffers::String> match_source_node_offset; |
| 1618 | if (!map.match->source_node.empty()) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1619 | match_source_node_offset = fbb.CreateString(map.match->source_node); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1620 | } |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame^] | 1621 | CHECK(!map.rename->name.empty()); |
| 1622 | const flatbuffers::Offset<flatbuffers::String> rename_name_offset = |
| 1623 | fbb.CreateString(map.rename->name); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1624 | Channel::Builder match_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1625 | match_builder.add_name(match_name_offset); |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame^] | 1626 | if (!match_type_offset.IsNull()) { |
| 1627 | match_builder.add_type(match_type_offset); |
| 1628 | } |
| 1629 | if (!match_source_node_offset.IsNull()) { |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1630 | match_builder.add_source_node(match_source_node_offset); |
| 1631 | } |
| 1632 | const flatbuffers::Offset<Channel> match_offset = match_builder.Finish(); |
| 1633 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1634 | Channel::Builder rename_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1635 | rename_builder.add_name(rename_name_offset); |
| 1636 | const flatbuffers::Offset<Channel> rename_offset = rename_builder.Finish(); |
| 1637 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1638 | Map::Builder map_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1639 | map_builder.add_match(match_offset); |
| 1640 | map_builder.add_rename(rename_offset); |
| 1641 | map_offsets.emplace_back(map_builder.Finish()); |
| 1642 | } |
| 1643 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1644 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 1645 | maps_offsets = map_offsets.empty() ? 0 : fbb.CreateVector(map_offsets); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1646 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1647 | // And copy everything else over. |
| 1648 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 1649 | nodes_offset = aos::RecursiveCopyVectorTable(base_config->nodes(), &fbb); |
| 1650 | |
| 1651 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 1652 | applications_offset = |
| 1653 | aos::RecursiveCopyVectorTable(base_config->applications(), &fbb); |
| 1654 | |
| 1655 | // Now insert everything else in unmodified. |
| 1656 | ConfigurationBuilder configuration_builder(fbb); |
| 1657 | if (!channels_offset.IsNull()) { |
| 1658 | configuration_builder.add_channels(channels_offset); |
| 1659 | } |
| 1660 | if (!maps_offsets.IsNull()) { |
| 1661 | configuration_builder.add_maps(maps_offsets); |
| 1662 | } |
| 1663 | if (!nodes_offset.IsNull()) { |
| 1664 | configuration_builder.add_nodes(nodes_offset); |
| 1665 | } |
| 1666 | if (!applications_offset.IsNull()) { |
| 1667 | configuration_builder.add_applications(applications_offset); |
| 1668 | } |
| 1669 | |
| 1670 | if (base_config->has_channel_storage_duration()) { |
| 1671 | configuration_builder.add_channel_storage_duration( |
| 1672 | base_config->channel_storage_duration()); |
| 1673 | } |
| 1674 | |
| 1675 | CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u) |
| 1676 | << ": Merging logic needs to be updated when the number of configuration " |
| 1677 | "fields changes."; |
| 1678 | |
| 1679 | fbb.Finish(configuration_builder.Finish()); |
| 1680 | |
| 1681 | // Clean it up and return it! By using MergeConfiguration here, we'll |
| 1682 | // actually get a deduplicated config for free too. |
| 1683 | FlatbufferDetachedBuffer<Configuration> new_merged_config = |
| 1684 | configuration::MergeConfiguration( |
| 1685 | FlatbufferDetachedBuffer<Configuration>(fbb.Release())); |
| 1686 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1687 | remapped_configuration_buffer_ = |
| 1688 | std::make_unique<FlatbufferDetachedBuffer<Configuration>>( |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1689 | configuration::MergeConfiguration(new_merged_config, schemas)); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1690 | |
| 1691 | remapped_configuration_ = &remapped_configuration_buffer_->message(); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1692 | |
| 1693 | // TODO(austin): Lazily re-build to save CPU? |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1694 | } |
| 1695 | |
Naman Gupta | cf6d442 | 2023-03-01 11:41:00 -0800 | [diff] [blame] | 1696 | std::unique_ptr<const ReplayChannelIndices> |
| 1697 | LogReader::MaybeMakeReplayChannelIndices(const Node *node) { |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 1698 | if (replay_channels_ == nullptr) { |
| 1699 | return nullptr; |
| 1700 | } else { |
Naman Gupta | cf6d442 | 2023-03-01 11:41:00 -0800 | [diff] [blame] | 1701 | std::unique_ptr<ReplayChannelIndices> replay_channel_indices = |
| 1702 | std::make_unique<ReplayChannelIndices>(); |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 1703 | for (auto const &channel : *replay_channels_) { |
| 1704 | const Channel *ch = configuration::GetChannel( |
| 1705 | logged_configuration(), channel.first, channel.second, "", node); |
| 1706 | if (ch == nullptr) { |
| 1707 | LOG(WARNING) << "Channel: " << channel.first << " " << channel.second |
| 1708 | << " not found in configuration for node: " |
| 1709 | << node->name()->string_view() << " Skipping ..."; |
| 1710 | continue; |
| 1711 | } |
| 1712 | const size_t channel_index = |
| 1713 | configuration::ChannelIndex(logged_configuration(), ch); |
Naman Gupta | cf6d442 | 2023-03-01 11:41:00 -0800 | [diff] [blame] | 1714 | replay_channel_indices->emplace_back(channel_index); |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 1715 | } |
Naman Gupta | cf6d442 | 2023-03-01 11:41:00 -0800 | [diff] [blame] | 1716 | std::sort(replay_channel_indices->begin(), replay_channel_indices->end()); |
| 1717 | return replay_channel_indices; |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 1718 | } |
| 1719 | } |
| 1720 | |
Austin Schuh | 1c22735 | 2021-09-17 12:53:54 -0700 | [diff] [blame] | 1721 | std::vector<const Channel *> LogReader::RemappedChannels() const { |
| 1722 | std::vector<const Channel *> result; |
| 1723 | result.reserve(remapped_channels_.size()); |
| 1724 | for (auto &pair : remapped_channels_) { |
| 1725 | const Channel *const logged_channel = |
| 1726 | CHECK_NOTNULL(logged_configuration()->channels()->Get(pair.first)); |
| 1727 | |
| 1728 | auto channel_iterator = std::lower_bound( |
| 1729 | remapped_configuration_->channels()->cbegin(), |
| 1730 | remapped_configuration_->channels()->cend(), |
| 1731 | std::make_pair(std::string_view(pair.second.remapped_name), |
| 1732 | logged_channel->type()->string_view()), |
| 1733 | CompareChannels); |
| 1734 | |
| 1735 | CHECK(channel_iterator != remapped_configuration_->channels()->cend()); |
| 1736 | CHECK(EqualsChannels( |
| 1737 | *channel_iterator, |
| 1738 | std::make_pair(std::string_view(pair.second.remapped_name), |
| 1739 | logged_channel->type()->string_view()))); |
| 1740 | result.push_back(*channel_iterator); |
| 1741 | } |
| 1742 | return result; |
| 1743 | } |
| 1744 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1745 | const Channel *LogReader::RemapChannel(const EventLoop *event_loop, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1746 | const Node *node, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1747 | const Channel *channel) { |
| 1748 | std::string_view channel_name = channel->name()->string_view(); |
| 1749 | std::string_view channel_type = channel->type()->string_view(); |
| 1750 | const int channel_index = |
| 1751 | configuration::ChannelIndex(logged_configuration(), channel); |
| 1752 | // If the channel is remapped, find the correct channel name to use. |
| 1753 | if (remapped_channels_.count(channel_index) > 0) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1754 | VLOG(3) << "Got remapped channel on " |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1755 | << configuration::CleanedChannelToString(channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1756 | channel_name = remapped_channels_[channel_index].remapped_name; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1757 | } |
| 1758 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1759 | VLOG(2) << "Going to remap channel " << channel_name << " " << channel_type; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1760 | const Channel *remapped_channel = configuration::GetChannel( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1761 | configuration(), channel_name, channel_type, |
| 1762 | event_loop ? event_loop->name() : "log_reader", node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1763 | |
| 1764 | CHECK(remapped_channel != nullptr) |
| 1765 | << ": Unable to send {\"name\": \"" << channel_name << "\", \"type\": \"" |
| 1766 | << channel_type << "\"} because it is not in the provided configuration."; |
| 1767 | |
| 1768 | return remapped_channel; |
| 1769 | } |
| 1770 | |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1771 | LogReader::State::State( |
| 1772 | std::unique_ptr<TimestampMapper> timestamp_mapper, |
| 1773 | message_bridge::MultiNodeNoncausalOffsetEstimator *multinode_filters, |
James Kuszmaul | b11a150 | 2022-07-01 16:02:25 -0700 | [diff] [blame] | 1774 | std::function<void()> notice_realtime_end, const Node *node, |
| 1775 | LogReader::State::ThreadedBuffering threading, |
Naman Gupta | cf6d442 | 2023-03-01 11:41:00 -0800 | [diff] [blame] | 1776 | std::unique_ptr<const ReplayChannelIndices> replay_channel_indices) |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1777 | : timestamp_mapper_(std::move(timestamp_mapper)), |
James Kuszmaul | b11a150 | 2022-07-01 16:02:25 -0700 | [diff] [blame] | 1778 | notice_realtime_end_(notice_realtime_end), |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1779 | node_(node), |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 1780 | multinode_filters_(multinode_filters), |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 1781 | threading_(threading), |
Naman Gupta | cf6d442 | 2023-03-01 11:41:00 -0800 | [diff] [blame] | 1782 | replay_channel_indices_(std::move(replay_channel_indices)) { |
Naman Gupta | a68401c | 2022-12-08 14:34:06 -0800 | [diff] [blame] | 1783 | // If timestamp_mapper_ is nullptr, then there are no log parts associated |
| 1784 | // with this node. If there are no log parts for the node, there will be no |
| 1785 | // log data, and so we do not need to worry about the replay channel filters. |
| 1786 | if (replay_channel_indices_ != nullptr && timestamp_mapper_ != nullptr) { |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 1787 | timestamp_mapper_->set_replay_channels_callback( |
Naman Gupta | cf6d442 | 2023-03-01 11:41:00 -0800 | [diff] [blame] | 1788 | [filter = replay_channel_indices_.get()]( |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 1789 | const TimestampedMessage &message) -> bool { |
| 1790 | auto const begin = filter->cbegin(); |
| 1791 | auto const end = filter->cend(); |
| 1792 | // TODO: benchmark strategies for channel_index matching |
| 1793 | return std::binary_search(begin, end, message.channel_index); |
| 1794 | }); |
| 1795 | } |
| 1796 | } |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1797 | |
| 1798 | void LogReader::State::AddPeer(State *peer) { |
| 1799 | if (timestamp_mapper_ && peer->timestamp_mapper_) { |
| 1800 | timestamp_mapper_->AddPeer(peer->timestamp_mapper_.get()); |
| 1801 | } |
| 1802 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1803 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1804 | void LogReader::State::SetNodeEventLoopFactory( |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1805 | NodeEventLoopFactory *node_event_loop_factory, |
| 1806 | SimulatedEventLoopFactory *event_loop_factory) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1807 | node_event_loop_factory_ = node_event_loop_factory; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1808 | event_loop_factory_ = event_loop_factory; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | void LogReader::State::SetChannelCount(size_t count) { |
| 1812 | channels_.resize(count); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1813 | remote_timestamp_senders_.resize(count); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1814 | filters_.resize(count); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1815 | channel_source_state_.resize(count); |
| 1816 | factory_channel_index_.resize(count); |
| 1817 | queue_index_map_.resize(count); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1820 | void LogReader::State::SetRemoteTimestampSender( |
| 1821 | size_t logged_channel_index, RemoteMessageSender *remote_timestamp_sender) { |
| 1822 | remote_timestamp_senders_[logged_channel_index] = remote_timestamp_sender; |
| 1823 | } |
| 1824 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1825 | void LogReader::State::SetChannel( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1826 | size_t logged_channel_index, size_t factory_channel_index, |
| 1827 | std::unique_ptr<RawSender> sender, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1828 | message_bridge::NoncausalOffsetEstimator *filter, bool is_forwarded, |
| 1829 | State *source_state) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1830 | channels_[logged_channel_index] = std::move(sender); |
| 1831 | filters_[logged_channel_index] = filter; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1832 | channel_source_state_[logged_channel_index] = source_state; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1833 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1834 | if (is_forwarded) { |
| 1835 | queue_index_map_[logged_channel_index] = |
| 1836 | std::make_unique<std::vector<State::ContiguousSentTimestamp>>(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | factory_channel_index_[logged_channel_index] = factory_channel_index; |
| 1840 | } |
| 1841 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 1842 | void LogReader::State::TrackMessageSendTiming( |
| 1843 | const RawSender &sender, monotonic_clock::time_point expected_send_time) { |
| 1844 | if (event_loop_ == nullptr || !timing_statistics_sender_.valid()) { |
| 1845 | return; |
| 1846 | } |
| 1847 | |
| 1848 | timing::MessageTimingT sample; |
| 1849 | sample.channel = configuration::ChannelIndex(event_loop_->configuration(), |
| 1850 | sender.channel()); |
| 1851 | sample.expected_send_time = expected_send_time.time_since_epoch().count(); |
| 1852 | sample.actual_send_time = |
| 1853 | sender.monotonic_sent_time().time_since_epoch().count(); |
| 1854 | sample.send_time_error = aos::time::DurationInSeconds( |
| 1855 | expected_send_time - sender.monotonic_sent_time()); |
| 1856 | send_timings_.push_back(sample); |
| 1857 | |
| 1858 | // Somewhat arbitrarily send out timing information in batches of 100. No need |
| 1859 | // to create excessive overhead in regenerated logfiles. |
| 1860 | // TODO(james): The overhead may be fine. |
| 1861 | constexpr size_t kMaxTimesPerStatisticsMessage = 100; |
| 1862 | CHECK(timing_statistics_sender_.valid()); |
| 1863 | if (send_timings_.size() == kMaxTimesPerStatisticsMessage) { |
| 1864 | SendMessageTimings(); |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | void LogReader::State::SendMessageTimings() { |
| 1869 | if (send_timings_.empty() || !timing_statistics_sender_.valid()) { |
| 1870 | return; |
| 1871 | } |
| 1872 | auto builder = timing_statistics_sender_.MakeBuilder(); |
| 1873 | std::vector<flatbuffers::Offset<timing::MessageTiming>> timing_offsets; |
| 1874 | for (const auto &timing : send_timings_) { |
| 1875 | timing_offsets.push_back( |
| 1876 | timing::MessageTiming::Pack(*builder.fbb(), &timing)); |
| 1877 | } |
| 1878 | send_timings_.clear(); |
| 1879 | flatbuffers::Offset< |
| 1880 | flatbuffers::Vector<flatbuffers::Offset<timing::MessageTiming>>> |
| 1881 | timings_offset = builder.fbb()->CreateVector(timing_offsets); |
| 1882 | timing::ReplayTiming::Builder timing_builder = |
| 1883 | builder.MakeBuilder<timing::ReplayTiming>(); |
| 1884 | timing_builder.add_messages(timings_offset); |
| 1885 | timing_statistics_sender_.CheckOk(builder.Send(timing_builder.Finish())); |
| 1886 | } |
| 1887 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1888 | bool LogReader::State::Send(const TimestampedMessage ×tamped_message) { |
| 1889 | aos::RawSender *sender = channels_[timestamped_message.channel_index].get(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1890 | CHECK(sender); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1891 | uint32_t remote_queue_index = 0xffffffff; |
| 1892 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1893 | if (remote_timestamp_senders_[timestamped_message.channel_index] != nullptr) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1894 | State *source_state = |
| 1895 | CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]); |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1896 | std::vector<ContiguousSentTimestamp> *queue_index_map = CHECK_NOTNULL( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1897 | source_state->queue_index_map_[timestamped_message.channel_index] |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1898 | .get()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1899 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1900 | struct SentTimestamp { |
| 1901 | monotonic_clock::time_point monotonic_event_time; |
| 1902 | uint32_t queue_index; |
| 1903 | } search; |
| 1904 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1905 | CHECK_EQ(timestamped_message.monotonic_remote_time.boot, |
| 1906 | source_state->boot_count()); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 1907 | search.monotonic_event_time = |
| 1908 | timestamped_message.monotonic_remote_time.time; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1909 | search.queue_index = timestamped_message.remote_queue_index.index; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1910 | |
| 1911 | // Find the sent time if available. |
| 1912 | auto element = std::lower_bound( |
| 1913 | queue_index_map->begin(), queue_index_map->end(), search, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1914 | [](ContiguousSentTimestamp a, SentTimestamp b) { |
| 1915 | if (a.ending_monotonic_event_time < b.monotonic_event_time) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1916 | return true; |
| 1917 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1918 | if (a.starting_monotonic_event_time > b.monotonic_event_time) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1919 | return false; |
| 1920 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1921 | |
| 1922 | if (a.ending_queue_index < b.queue_index) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1923 | return true; |
| 1924 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1925 | if (a.starting_queue_index >= b.queue_index) { |
| 1926 | return false; |
| 1927 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1928 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1929 | // If it isn't clearly below or above, it is below. Since we return |
| 1930 | // the last element <, this will return a match. |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1931 | return false; |
| 1932 | }); |
| 1933 | |
| 1934 | // TODO(austin): Be a bit more principled here, but we will want to do that |
| 1935 | // after the logger rewrite. We hit this when one node finishes, but the |
| 1936 | // other node isn't done yet. So there is no send time, but there is a |
| 1937 | // receive time. |
| 1938 | if (element != queue_index_map->end()) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1939 | CHECK_EQ(timestamped_message.monotonic_remote_time.boot, |
| 1940 | source_state->boot_count()); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1941 | |
| 1942 | CHECK_GE(timestamped_message.monotonic_remote_time.time, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1943 | element->starting_monotonic_event_time); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1944 | CHECK_LE(timestamped_message.monotonic_remote_time.time, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1945 | element->ending_monotonic_event_time); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1946 | CHECK_GE(timestamped_message.remote_queue_index.index, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1947 | element->starting_queue_index); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1948 | CHECK_LE(timestamped_message.remote_queue_index.index, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1949 | element->ending_queue_index); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1950 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1951 | remote_queue_index = timestamped_message.remote_queue_index.index + |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1952 | element->actual_queue_index - |
| 1953 | element->starting_queue_index; |
| 1954 | } else { |
| 1955 | VLOG(1) << "No timestamp match in the map."; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1956 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1957 | CHECK_EQ(timestamped_message.monotonic_remote_time.boot, |
| 1958 | source_state->boot_count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1961 | if (event_loop_factory_ != nullptr && |
| 1962 | channel_source_state_[timestamped_message.channel_index] != nullptr && |
| 1963 | multinode_filters_ != nullptr) { |
| 1964 | // Sanity check that we are using consistent boot uuids. |
| 1965 | State *source_state = |
| 1966 | channel_source_state_[timestamped_message.channel_index]; |
| 1967 | CHECK_EQ(multinode_filters_->boot_uuid( |
| 1968 | configuration::GetNodeIndex(event_loop_->configuration(), |
| 1969 | source_state->node()), |
| 1970 | timestamped_message.monotonic_remote_time.boot), |
| 1971 | CHECK_NOTNULL( |
| 1972 | CHECK_NOTNULL( |
| 1973 | channel_source_state_[timestamped_message.channel_index]) |
| 1974 | ->event_loop_) |
| 1975 | ->boot_uuid()); |
| 1976 | } |
| 1977 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1978 | // Send! Use the replayed queue index here instead of the logged queue index |
| 1979 | // for the remote queue index. This makes re-logging work. |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1980 | const auto err = sender->Send( |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1981 | RawSender::SharedSpan(timestamped_message.data, |
| 1982 | ×tamped_message.data->span), |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1983 | timestamped_message.monotonic_remote_time.time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1984 | timestamped_message.realtime_remote_time, remote_queue_index, |
| 1985 | (channel_source_state_[timestamped_message.channel_index] != nullptr |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 1986 | ? CHECK_NOTNULL(multinode_filters_) |
| 1987 | ->boot_uuid(configuration::GetNodeIndex( |
| 1988 | event_loop_->configuration(), |
| 1989 | channel_source_state_[timestamped_message |
| 1990 | .channel_index] |
| 1991 | ->node()), |
| 1992 | timestamped_message.monotonic_remote_time.boot) |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1993 | : event_loop_->boot_uuid())); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1994 | if (err != RawSender::Error::kOk) return false; |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 1995 | if (monotonic_start_time(timestamped_message.monotonic_event_time.boot) <= |
| 1996 | timestamped_message.monotonic_event_time.time) { |
| 1997 | // Only track errors for non-fetched messages. |
| 1998 | TrackMessageSendTiming( |
| 1999 | *sender, |
| 2000 | timestamped_message.monotonic_event_time.time + clock_offset()); |
| 2001 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2002 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2003 | if (queue_index_map_[timestamped_message.channel_index]) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2004 | CHECK_EQ(timestamped_message.monotonic_event_time.boot, boot_count()); |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 2005 | if (queue_index_map_[timestamped_message.channel_index]->empty()) { |
| 2006 | // Nothing here, start a range with 0 length. |
| 2007 | ContiguousSentTimestamp timestamp; |
| 2008 | timestamp.starting_monotonic_event_time = |
| 2009 | timestamp.ending_monotonic_event_time = |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 2010 | timestamped_message.monotonic_event_time.time; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 2011 | timestamp.starting_queue_index = timestamp.ending_queue_index = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2012 | timestamped_message.queue_index.index; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 2013 | timestamp.actual_queue_index = sender->sent_queue_index(); |
| 2014 | queue_index_map_[timestamped_message.channel_index]->emplace_back( |
| 2015 | timestamp); |
| 2016 | } else { |
| 2017 | // We've got something. See if the next timestamp is still contiguous. If |
| 2018 | // so, grow it. |
| 2019 | ContiguousSentTimestamp *back = |
| 2020 | &queue_index_map_[timestamped_message.channel_index]->back(); |
| 2021 | if ((back->starting_queue_index - back->actual_queue_index) == |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2022 | (timestamped_message.queue_index.index - |
| 2023 | sender->sent_queue_index())) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2024 | back->ending_queue_index = timestamped_message.queue_index.index; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 2025 | back->ending_monotonic_event_time = |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 2026 | timestamped_message.monotonic_event_time.time; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 2027 | } else { |
| 2028 | // Otherwise, make a new one. |
| 2029 | ContiguousSentTimestamp timestamp; |
| 2030 | timestamp.starting_monotonic_event_time = |
| 2031 | timestamp.ending_monotonic_event_time = |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 2032 | timestamped_message.monotonic_event_time.time; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 2033 | timestamp.starting_queue_index = timestamp.ending_queue_index = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2034 | timestamped_message.queue_index.index; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 2035 | timestamp.actual_queue_index = sender->sent_queue_index(); |
| 2036 | queue_index_map_[timestamped_message.channel_index]->emplace_back( |
| 2037 | timestamp); |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | // TODO(austin): Should we prune the map? On a many day log, I only saw the |
| 2042 | // queue index diverge a couple of elements, which would be a very small |
| 2043 | // map. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2044 | } else if (remote_timestamp_senders_[timestamped_message.channel_index] != |
| 2045 | nullptr) { |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 2046 | // TODO(james): Currently, If running replay against a single event loop, |
| 2047 | // remote timestamps will not get replayed because this code-path only |
| 2048 | // gets triggered on the event loop that receives the forwarded message |
| 2049 | // that the timestamps correspond to. This code, as written, also doesn't |
| 2050 | // correctly handle a non-zero clock_offset for the *_remote_time fields. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2051 | State *source_state = |
| 2052 | CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]); |
| 2053 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2054 | flatbuffers::FlatBufferBuilder fbb; |
| 2055 | fbb.ForceDefaults(true); |
Austin Schuh | cdd9027 | 2021-03-15 12:46:16 -0700 | [diff] [blame] | 2056 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> boot_uuid_offset = |
| 2057 | event_loop_->boot_uuid().PackVector(&fbb); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 2058 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2059 | RemoteMessage::Builder message_header_builder(fbb); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2060 | |
| 2061 | message_header_builder.add_channel_index( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2062 | factory_channel_index_[timestamped_message.channel_index]); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2063 | |
| 2064 | // Swap the remote and sent metrics. They are from the sender's |
| 2065 | // perspective, not the receiver's perspective. |
| 2066 | message_header_builder.add_monotonic_sent_time( |
| 2067 | sender->monotonic_sent_time().time_since_epoch().count()); |
| 2068 | message_header_builder.add_realtime_sent_time( |
| 2069 | sender->realtime_sent_time().time_since_epoch().count()); |
| 2070 | message_header_builder.add_queue_index(sender->sent_queue_index()); |
| 2071 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2072 | CHECK_EQ(timestamped_message.monotonic_remote_time.boot, |
| 2073 | source_state->boot_count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2074 | message_header_builder.add_monotonic_remote_time( |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 2075 | timestamped_message.monotonic_remote_time.time.time_since_epoch() |
| 2076 | .count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2077 | message_header_builder.add_realtime_remote_time( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2078 | timestamped_message.realtime_remote_time.time_since_epoch().count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2079 | |
| 2080 | message_header_builder.add_remote_queue_index(remote_queue_index); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 2081 | message_header_builder.add_boot_uuid(boot_uuid_offset); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2082 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2083 | fbb.Finish(message_header_builder.Finish()); |
| 2084 | |
| 2085 | remote_timestamp_senders_[timestamped_message.channel_index]->Send( |
| 2086 | FlatbufferDetachedBuffer<RemoteMessage>(fbb.Release()), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2087 | timestamped_message.monotonic_timestamp_time, |
| 2088 | source_state->boot_count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | return true; |
| 2092 | } |
| 2093 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2094 | LogReader::RemoteMessageSender::RemoteMessageSender( |
| 2095 | aos::Sender<message_bridge::RemoteMessage> sender, EventLoop *event_loop) |
| 2096 | : event_loop_(event_loop), |
| 2097 | sender_(std::move(sender)), |
| 2098 | timer_(event_loop->AddTimer([this]() { SendTimestamp(); })) {} |
| 2099 | |
| 2100 | void LogReader::RemoteMessageSender::ScheduleTimestamp() { |
| 2101 | if (remote_timestamps_.empty()) { |
| 2102 | CHECK_NOTNULL(timer_); |
| 2103 | timer_->Disable(); |
| 2104 | scheduled_time_ = monotonic_clock::min_time; |
| 2105 | return; |
| 2106 | } |
| 2107 | |
| 2108 | if (scheduled_time_ != remote_timestamps_.front().monotonic_timestamp_time) { |
| 2109 | CHECK_NOTNULL(timer_); |
Austin Schuh | 816e5d6 | 2021-01-05 23:42:20 -0800 | [diff] [blame] | 2110 | timer_->Setup(remote_timestamps_.front().monotonic_timestamp_time); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2111 | scheduled_time_ = remote_timestamps_.front().monotonic_timestamp_time; |
Austin Schuh | 3d94be0 | 2021-02-12 23:15:20 -0800 | [diff] [blame] | 2112 | CHECK_GE(scheduled_time_, event_loop_->monotonic_now()) |
| 2113 | << event_loop_->node()->name()->string_view(); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | void LogReader::RemoteMessageSender::Send( |
| 2118 | FlatbufferDetachedBuffer<RemoteMessage> remote_message, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2119 | BootTimestamp monotonic_timestamp_time, size_t source_boot_count) { |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 2120 | // There are 2 variants of logs. |
| 2121 | // 1) Logs without monotonic_timestamp_time |
| 2122 | // 2) Logs with monotonic_timestamp_time |
| 2123 | // |
| 2124 | // As of Jan 2021, we shouldn't have any more logs without |
| 2125 | // monotonic_timestamp_time. We don't have data locked up in those logs worth |
| 2126 | // the effort of saving. |
| 2127 | // |
| 2128 | // This gives us 3 cases, 2 of which are undistinguishable. |
| 2129 | // 1) Old log without monotonic_timestamp_time. |
| 2130 | // 2) New log with monotonic_timestamp_time where the timestamp was logged |
| 2131 | // remotely so we actually have monotonic_timestamp_time. |
| 2132 | // 3) New log, but the timestamp was logged on the node receiving the message |
| 2133 | // so there is no monotonic_timestamp_time. |
| 2134 | // |
| 2135 | // Our goal when replaying is to accurately reproduce the state of the world |
| 2136 | // present when logging. If a timestamp wasn't sent back across the network, |
| 2137 | // we shouldn't replay one back across the network. |
| 2138 | // |
| 2139 | // Given that we don't really care about 1, we can use the presence of the |
| 2140 | // timestamp to distinguish 2 and 3, and ignore 1. If we don't have a |
| 2141 | // monotonic_timestamp_time, this means the message was logged locally and |
| 2142 | // remote timestamps can be ignored. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2143 | if (monotonic_timestamp_time == BootTimestamp::min_time()) { |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 2144 | return; |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2145 | } |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 2146 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2147 | CHECK_EQ(monotonic_timestamp_time.boot, source_boot_count); |
| 2148 | |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 2149 | remote_timestamps_.emplace( |
| 2150 | std::upper_bound( |
| 2151 | remote_timestamps_.begin(), remote_timestamps_.end(), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2152 | monotonic_timestamp_time.time, |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 2153 | [](const aos::monotonic_clock::time_point monotonic_timestamp_time, |
| 2154 | const Timestamp ×tamp) { |
| 2155 | return monotonic_timestamp_time < |
| 2156 | timestamp.monotonic_timestamp_time; |
| 2157 | }), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2158 | std::move(remote_message), monotonic_timestamp_time.time); |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 2159 | ScheduleTimestamp(); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | void LogReader::RemoteMessageSender::SendTimestamp() { |
Austin Schuh | 3d94be0 | 2021-02-12 23:15:20 -0800 | [diff] [blame] | 2163 | CHECK_EQ(event_loop_->context().monotonic_event_time, scheduled_time_) |
| 2164 | << event_loop_->node()->name()->string_view(); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2165 | CHECK(!remote_timestamps_.empty()); |
| 2166 | |
| 2167 | // Send out all timestamps at the currently scheduled time. |
| 2168 | while (remote_timestamps_.front().monotonic_timestamp_time == |
| 2169 | scheduled_time_) { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2170 | CHECK_EQ(sender_.Send(std::move(remote_timestamps_.front().remote_message)), |
| 2171 | RawSender::Error::kOk); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2172 | remote_timestamps_.pop_front(); |
| 2173 | if (remote_timestamps_.empty()) { |
| 2174 | break; |
| 2175 | } |
| 2176 | } |
| 2177 | scheduled_time_ = monotonic_clock::min_time; |
| 2178 | |
| 2179 | ScheduleTimestamp(); |
| 2180 | } |
| 2181 | |
| 2182 | LogReader::RemoteMessageSender *LogReader::State::RemoteTimestampSender( |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 2183 | const Channel *channel, const Connection *connection) { |
| 2184 | message_bridge::ChannelTimestampFinder finder(event_loop_); |
| 2185 | // Look at any pre-created channel/connection pairs. |
| 2186 | { |
| 2187 | auto it = |
| 2188 | channel_timestamp_loggers_.find(std::make_pair(channel, connection)); |
| 2189 | if (it != channel_timestamp_loggers_.end()) { |
| 2190 | return it->second.get(); |
| 2191 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2192 | } |
| 2193 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 2194 | // That failed, so resolve the RemoteMessage channel timestamps will be logged |
| 2195 | // to. |
| 2196 | const Channel *timestamp_channel = finder.ForChannel(channel, connection); |
| 2197 | |
| 2198 | { |
| 2199 | // See if that has been created before. If so, cache it in |
| 2200 | // channel_timestamp_loggers_ and return. |
| 2201 | auto it = timestamp_loggers_.find(timestamp_channel); |
| 2202 | if (it != timestamp_loggers_.end()) { |
| 2203 | CHECK(channel_timestamp_loggers_ |
| 2204 | .try_emplace(std::make_pair(channel, connection), it->second) |
| 2205 | .second); |
| 2206 | return it->second.get(); |
| 2207 | } |
| 2208 | } |
| 2209 | |
| 2210 | // Otherwise, make a sender, save it, and cache it. |
| 2211 | auto result = channel_timestamp_loggers_.try_emplace( |
| 2212 | std::make_pair(channel, connection), |
| 2213 | std::make_shared<RemoteMessageSender>( |
| 2214 | event_loop()->MakeSender<RemoteMessage>( |
| 2215 | timestamp_channel->name()->string_view()), |
| 2216 | event_loop())); |
| 2217 | |
| 2218 | CHECK(timestamp_loggers_.try_emplace(timestamp_channel, result.first->second) |
| 2219 | .second); |
| 2220 | return result.first->second.get(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2221 | } |
| 2222 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 2223 | TimestampedMessage LogReader::State::PopOldest() { |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 2224 | // multithreaded |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 2225 | if (message_queuer_.has_value()) { |
| 2226 | std::optional<TimestampedMessage> message = message_queuer_->Pop(); |
| 2227 | CHECK(message.has_value()) << ": Unexpectedly ran out of messages."; |
| 2228 | message_queuer_->SetState( |
| 2229 | message.value().monotonic_event_time + |
| 2230 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 2231 | std::chrono::duration<double>(FLAGS_threaded_look_ahead_seconds))); |
| 2232 | return message.value(); |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 2233 | } else { // single threaded |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 2234 | CHECK(timestamp_mapper_ != nullptr); |
| 2235 | TimestampedMessage *result_ptr = timestamp_mapper_->Front(); |
| 2236 | CHECK(result_ptr != nullptr); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2237 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 2238 | TimestampedMessage result = std::move(*result_ptr); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2239 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 2240 | VLOG(2) << MaybeNodeName(event_loop_->node()) << "PopOldest Popping " |
| 2241 | << result.monotonic_event_time; |
| 2242 | timestamp_mapper_->PopFront(); |
| 2243 | SeedSortedMessages(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2244 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 2245 | CHECK_EQ(result.monotonic_event_time.boot, boot_count()); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 2246 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 2247 | VLOG(1) << "Popped " << result |
| 2248 | << configuration::CleanedChannelToString( |
| 2249 | event_loop_->configuration()->channels()->Get( |
| 2250 | factory_channel_index_[result.channel_index])); |
| 2251 | return result; |
| 2252 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 2255 | BootTimestamp LogReader::State::MultiThreadedOldestMessageTime() { |
| 2256 | if (!message_queuer_.has_value()) { |
| 2257 | return SingleThreadedOldestMessageTime(); |
| 2258 | } |
| 2259 | std::optional<TimestampedMessage> message = message_queuer_->Peek(); |
| 2260 | if (!message.has_value()) { |
| 2261 | return BootTimestamp::max_time(); |
| 2262 | } |
| 2263 | if (message.value().monotonic_event_time.boot == boot_count()) { |
| 2264 | ObserveNextMessage(message.value().monotonic_event_time.time, |
| 2265 | message.value().realtime_event_time); |
| 2266 | } |
| 2267 | return message.value().monotonic_event_time; |
| 2268 | } |
| 2269 | |
| 2270 | BootTimestamp LogReader::State::SingleThreadedOldestMessageTime() { |
| 2271 | CHECK(!message_queuer_.has_value()) |
| 2272 | << "Cannot use SingleThreadedOldestMessageTime() once the queuer thread " |
| 2273 | "is created."; |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2274 | if (timestamp_mapper_ == nullptr) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2275 | return BootTimestamp::max_time(); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2276 | } |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2277 | TimestampedMessage *result_ptr = timestamp_mapper_->Front(); |
| 2278 | if (result_ptr == nullptr) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2279 | return BootTimestamp::max_time(); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2280 | } |
Austin Schuh | 0b8a550 | 2021-11-18 15:34:12 -0800 | [diff] [blame] | 2281 | VLOG(2) << MaybeNodeName(node()) << "oldest message at " |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 2282 | << result_ptr->monotonic_event_time.time; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 2283 | if (result_ptr->monotonic_event_time.boot == boot_count()) { |
| 2284 | ObserveNextMessage(result_ptr->monotonic_event_time.time, |
| 2285 | result_ptr->realtime_event_time); |
| 2286 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2287 | return result_ptr->monotonic_event_time; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | void LogReader::State::SeedSortedMessages() { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2291 | if (!timestamp_mapper_) return; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2292 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2293 | timestamp_mapper_->QueueFor(chrono::duration_cast<chrono::seconds>( |
| 2294 | chrono::duration<double>(FLAGS_time_estimation_buffer_seconds))); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | void LogReader::State::Deregister() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2298 | if (started_ && !stopped_) { |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 2299 | NotifyLogfileEnd(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2300 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2301 | for (size_t i = 0; i < channels_.size(); ++i) { |
| 2302 | channels_[i].reset(); |
| 2303 | } |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 2304 | ClearTimeFlags(); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 2305 | channel_timestamp_loggers_.clear(); |
| 2306 | timestamp_loggers_.clear(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2307 | event_loop_unique_ptr_.reset(); |
| 2308 | event_loop_ = nullptr; |
| 2309 | timer_handler_ = nullptr; |
| 2310 | node_event_loop_factory_ = nullptr; |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 2311 | timing_statistics_sender_ = Sender<timing::ReplayTiming>(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2312 | } |
| 2313 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 2314 | void LogReader::State::SetStartTimeFlag(realtime_clock::time_point start_time) { |
| 2315 | if (start_time != realtime_clock::min_time) { |
| 2316 | start_event_notifier_ = std::make_unique<EventNotifier>( |
| 2317 | event_loop_, [this]() { NotifyFlagStart(); }, "flag_start", start_time); |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | void LogReader::State::SetEndTimeFlag(realtime_clock::time_point end_time) { |
| 2322 | if (end_time != realtime_clock::max_time) { |
| 2323 | end_event_notifier_ = std::make_unique<EventNotifier>( |
| 2324 | event_loop_, [this]() { NotifyFlagEnd(); }, "flag_end", end_time); |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | void LogReader::State::ObserveNextMessage( |
| 2329 | monotonic_clock::time_point monotonic_event, |
| 2330 | realtime_clock::time_point realtime_event) { |
| 2331 | if (start_event_notifier_) { |
| 2332 | start_event_notifier_->ObserveNextMessage(monotonic_event, realtime_event); |
| 2333 | } |
| 2334 | if (end_event_notifier_) { |
| 2335 | end_event_notifier_->ObserveNextMessage(monotonic_event, realtime_event); |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | void LogReader::State::ClearTimeFlags() { |
| 2340 | start_event_notifier_.reset(); |
| 2341 | end_event_notifier_.reset(); |
| 2342 | } |
| 2343 | |
| 2344 | void LogReader::State::NotifyLogfileStart() { |
| 2345 | if (start_event_notifier_) { |
| 2346 | if (start_event_notifier_->realtime_event_time() > |
| 2347 | realtime_start_time(boot_count())) { |
| 2348 | VLOG(1) << "Skipping, " << start_event_notifier_->realtime_event_time() |
| 2349 | << " > " << realtime_start_time(boot_count()); |
| 2350 | return; |
| 2351 | } |
| 2352 | } |
| 2353 | if (found_last_message_) { |
| 2354 | VLOG(1) << "Last message already found, bailing"; |
| 2355 | return; |
| 2356 | } |
| 2357 | RunOnStart(); |
| 2358 | } |
| 2359 | |
| 2360 | void LogReader::State::NotifyFlagStart() { |
| 2361 | if (start_event_notifier_->realtime_event_time() >= |
| 2362 | realtime_start_time(boot_count())) { |
| 2363 | RunOnStart(); |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | void LogReader::State::NotifyLogfileEnd() { |
| 2368 | if (found_last_message_) { |
| 2369 | return; |
| 2370 | } |
| 2371 | |
| 2372 | if (!stopped_ && started_) { |
| 2373 | RunOnEnd(); |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | void LogReader::State::NotifyFlagEnd() { |
| 2378 | if (!stopped_ && started_) { |
| 2379 | RunOnEnd(); |
| 2380 | SetFoundLastMessage(true); |
James Kuszmaul | b11a150 | 2022-07-01 16:02:25 -0700 | [diff] [blame] | 2381 | CHECK(notice_realtime_end_); |
| 2382 | notice_realtime_end_(); |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 2383 | } |
| 2384 | } |
| 2385 | |
James Kuszmaul | c3f34d1 | 2022-08-15 15:57:55 -0700 | [diff] [blame] | 2386 | void LogReader::State::MaybeSetClockOffset() { |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 2387 | if (node_event_loop_factory_ == nullptr) { |
| 2388 | // If not running with simulated event loop, set the monotonic clock |
| 2389 | // offset. |
| 2390 | clock_offset_ = event_loop()->monotonic_now() - monotonic_start_time(0); |
| 2391 | |
| 2392 | if (start_event_notifier_) { |
| 2393 | start_event_notifier_->SetClockOffset(clock_offset_); |
| 2394 | } |
| 2395 | if (end_event_notifier_) { |
| 2396 | end_event_notifier_->SetClockOffset(clock_offset_); |
| 2397 | } |
| 2398 | } |
| 2399 | } |
| 2400 | |
James Kuszmaul | b67409b | 2022-06-20 16:25:03 -0700 | [diff] [blame] | 2401 | void LogReader::SetRealtimeReplayRate(double replay_rate) { |
| 2402 | CHECK(event_loop_factory_ != nullptr) |
| 2403 | << ": Can't set replay rate without an event loop factory (have you " |
| 2404 | "called Register()?)."; |
| 2405 | event_loop_factory_->SetRealtimeReplayRate(replay_rate); |
| 2406 | } |
| 2407 | |
James Kuszmaul | b11a150 | 2022-07-01 16:02:25 -0700 | [diff] [blame] | 2408 | void LogReader::NoticeRealtimeEnd() { |
| 2409 | CHECK_GE(live_nodes_with_realtime_time_end_, 1u); |
| 2410 | --live_nodes_with_realtime_time_end_; |
| 2411 | if (live_nodes_with_realtime_time_end_ == 0 && exit_on_finish() && |
| 2412 | event_loop_factory_ != nullptr) { |
| 2413 | event_loop_factory_->Exit(); |
| 2414 | } |
| 2415 | } |
| 2416 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 2417 | } // namespace logger |
| 2418 | } // namespace aos |