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