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