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