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