blob: 850a564957e1a74758f9996827209948aad05101 [file] [log] [blame]
Austin Schuhb06f03b2021-02-17 22:00:37 -08001#include "aos/events/logging/log_reader.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -08002
Austin Schuhaf8a0d32023-05-03 09:53:06 -07003#include <dirent.h>
Austin Schuhe309d2a2019-11-29 13:25:21 -08004#include <fcntl.h>
5#include <sys/stat.h>
6#include <sys/types.h>
7#include <sys/uio.h>
Brian Silverman8ff74aa2021-02-05 16:37:15 -08008
Tyler Chatowbf0609c2021-07-31 16:13:27 -07009#include <climits>
Eric Schmiedebergae00e732023-04-12 15:53:17 -060010#include <utility>
Austin Schuhe309d2a2019-11-29 13:25:21 -080011#include <vector>
12
Austin Schuh2f8fd752020-09-01 22:38:28 -070013#include "absl/strings/escaping.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080014#include "absl/types/span.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070015#include "flatbuffers/flatbuffers.h"
16#include "openssl/sha.h"
17
Austin Schuhe309d2a2019-11-29 13:25:21 -080018#include "aos/events/event_loop.h"
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070019#include "aos/events/logging/boot_timestamp.h"
Austin Schuhf6f9bf32020-10-11 14:37:43 -070020#include "aos/events/logging/logfile_sorting.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080021#include "aos/events/logging/logger_generated.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080022#include "aos/flatbuffer_merge.h"
James Kuszmaul09632422022-05-25 15:56:19 -070023#include "aos/json_to_flatbuffer.h"
Austin Schuh0ca1fd32020-12-18 22:53:05 -080024#include "aos/network/multinode_timestamp_filter.h"
Austin Schuh0de30f32020-12-06 12:44:28 -080025#include "aos/network/remote_message_generated.h"
26#include "aos/network/remote_message_schema.h"
Austin Schuh288479d2019-12-18 19:47:52 -080027#include "aos/network/team_number.h"
Austin Schuh61e973f2021-02-21 21:43:56 -080028#include "aos/network/timestamp_channel.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080029#include "aos/time/time.h"
Brian Silvermanae7c0332020-09-30 16:58:23 -070030#include "aos/util/file.h"
Austin Schuh4385b142021-03-14 21:31:13 -070031#include "aos/uuid.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080032
Austin Schuh15649d62019-12-28 16:36:38 -080033DEFINE_bool(skip_missing_forwarding_entries, false,
34 "If true, drop any forwarding entries with missing data. If "
35 "false, CHECK.");
Austin Schuhe309d2a2019-11-29 13:25:21 -080036
Austin Schuh0ca1fd32020-12-18 22:53:05 -080037DECLARE_bool(timestamps_to_csv);
Austin Schuh63097262023-08-16 17:04:29 -070038DEFINE_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.");
46DEFINE_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 Schuh8bd96322020-02-13 21:18:22 -080051
Austin Schuh2f8fd752020-09-01 22:38:28 -070052DEFINE_bool(skip_order_validation, false,
53 "If true, ignore any out of orderness in replay");
54
Austin Schuhf0688662020-12-19 15:37:45 -080055DEFINE_double(
56 time_estimation_buffer_seconds, 2.0,
57 "The time to buffer ahead in the log file to accurately reconstruct time.");
58
Austin Schuhe33c08d2022-02-03 18:15:21 -080059DEFINE_string(
60 start_time, "",
61 "If set, start at this point in time in the log on the realtime clock.");
62DEFINE_string(
63 end_time, "",
64 "If set, end at this point in time in the log on the realtime clock.");
65
James Kuszmaul09632422022-05-25 15:56:19 -070066DEFINE_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 Kuszmaula16a7912022-06-17 10:58:12 -070072DEFINE_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 Schuhe309d2a2019-11-29 13:25:21 -080079namespace aos {
Austin Schuh006a9f52021-04-07 16:24:18 -070080namespace configuration {
81// We don't really want to expose this publicly, but log reader doesn't really
82// want to re-implement it.
83void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
84 std::string *name, std::string_view type, const Node *node);
Tyler Chatowbf0609c2021-07-31 16:13:27 -070085} // namespace configuration
Austin Schuhe309d2a2019-11-29 13:25:21 -080086namespace logger {
Austin Schuh0afc4d12020-10-19 11:42:04 -070087namespace {
Austin Schuh8c399962020-12-25 21:51:45 -080088
Austin Schuhe309d2a2019-11-29 13:25:21 -080089namespace chrono = std::chrono;
Austin Schuh0de30f32020-12-06 12:44:28 -080090using message_bridge::RemoteMessage;
Austin Schuh0afc4d12020-10-19 11:42:04 -070091} // namespace
Austin Schuhe309d2a2019-11-29 13:25:21 -080092
Austin Schuhe33c08d2022-02-03 18:15:21 -080093// 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.
96class 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 Kuszmaul09632422022-05-25 15:56:19 -0700117 // Sets the clock offset for realtime playback.
118 void SetClockOffset(std::chrono::nanoseconds clock_offset) {
119 clock_offset_ = clock_offset;
120 }
121
Austin Schuhe33c08d2022-02-03 18:15:21 -0800122 // 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 Schradera6712522023-07-05 20:25:11 -0700149 event_timer_->Schedule(candidate_monotonic + clock_offset_);
Austin Schuhe33c08d2022-02-03 18:15:21 -0800150 }
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 Kuszmaul09632422022-05-25 15:56:19 -0700168 std::chrono::nanoseconds clock_offset_{0};
169
Austin Schuhe33c08d2022-02-03 18:15:21 -0800170 bool called_ = false;
171};
172
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800173LogReader::LogReader(std::string_view filename,
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700174 const Configuration *replay_configuration,
175 const ReplayChannels *replay_channels)
Alexei Strots1f51ac72023-05-15 10:14:54 -0700176 : LogReader(LogFilesContainer(SortParts({std::string(filename)})),
177 replay_configuration, replay_channels) {}
Austin Schuhfa895892020-01-07 20:07:41 -0800178
Austin Schuh287d43d2020-12-04 20:19:33 -0800179LogReader::LogReader(std::vector<LogFile> log_files,
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700180 const Configuration *replay_configuration,
181 const ReplayChannels *replay_channels)
Alexei Strots1f51ac72023-05-15 10:14:54 -0700182 : LogReader(LogFilesContainer(std::move(log_files)), replay_configuration,
183 replay_channels) {}
184
185LogReader::LogReader(LogFilesContainer log_files,
186 const Configuration *replay_configuration,
187 const ReplayChannels *replay_channels)
Austin Schuh287d43d2020-12-04 20:19:33 -0800188 : log_files_(std::move(log_files)),
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700189 replay_configuration_(replay_configuration),
Eric Schmiedeberge279b532023-04-19 16:36:02 -0600190 replay_channels_(replay_channels),
Austin Schuh63097262023-08-16 17:04:29 -0700191 config_remapper_(log_files_.config().get(), replay_configuration_,
Eric Schmiedeberge279b532023-04-19 16:36:02 -0600192 replay_channels_) {
Austin Schuhe33c08d2022-02-03 18:15:21 -0800193 SetStartTime(FLAGS_start_time);
194 SetEndTime(FLAGS_end_time);
195
Austin Schuh0ca51f32020-12-25 21:51:45 -0800196 {
Alexei Strots1f51ac72023-05-15 10:14:54 -0700197 // Log files container validates that log files shared the same config.
Austin Schuh63097262023-08-16 17:04:29 -0700198 const Configuration *config = log_files_.config().get();
Alexei Strots1f51ac72023-05-15 10:14:54 -0700199 CHECK_NOTNULL(config);
Austin Schuh0ca51f32020-12-25 21:51:45 -0800200 }
Austin Schuhdda74ec2021-01-03 19:30:37 -0800201
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700202 if (replay_channels_ != nullptr) {
203 CHECK(!replay_channels_->empty()) << "replay_channels is empty which means "
204 "no messages will get replayed.";
205 }
206
Austin Schuh6f3babe2020-01-26 20:34:50 -0800207 if (!configuration::MultiNode(configuration())) {
James Kuszmaul09632422022-05-25 15:56:19 -0700208 states_.resize(1);
Austin Schuh8bd96322020-02-13 21:18:22 -0800209 } else {
Austin Schuh6aa77be2020-02-22 21:06:40 -0800210 if (replay_configuration) {
James Kuszmaul46d82582020-05-09 19:50:09 -0700211 CHECK_EQ(logged_configuration()->nodes()->size(),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800212 replay_configuration->nodes()->size())
Austin Schuh2f8fd752020-09-01 22:38:28 -0700213 << ": Log file and replay config need to have matching nodes "
214 "lists.";
James Kuszmaul46d82582020-05-09 19:50:09 -0700215 for (const Node *node : *logged_configuration()->nodes()) {
216 if (configuration::GetNode(replay_configuration, node) == nullptr) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700217 LOG(FATAL) << "Found node " << FlatbufferToJson(node)
218 << " in logged config that is not present in the replay "
219 "config.";
James Kuszmaul46d82582020-05-09 19:50:09 -0700220 }
221 }
Austin Schuh6aa77be2020-02-22 21:06:40 -0800222 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800223 states_.resize(configuration()->nodes()->size());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800224 }
Eric Schmiedebergae00e732023-04-12 15:53:17 -0600225
226 before_send_callbacks_.resize(configuration()->channels()->size());
Austin Schuhe309d2a2019-11-29 13:25:21 -0800227}
228
Austin Schuh6aa77be2020-02-22 21:06:40 -0800229LogReader::~LogReader() {
Austin Schuh39580f12020-08-01 14:44:08 -0700230 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 Schuh8bd96322020-02-13 21:18:22 -0800236}
Austin Schuhe309d2a2019-11-29 13:25:21 -0800237
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800238const Configuration *LogReader::logged_configuration() const {
Eric Schmiedeberge279b532023-04-19 16:36:02 -0600239 return config_remapper_.original_configuration();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800240}
241
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800242const Configuration *LogReader::configuration() const {
Eric Schmiedeberge279b532023-04-19 16:36:02 -0600243 return config_remapper_.remapped_configuration();
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800244}
245
Austin Schuh07676622021-01-21 18:59:17 -0800246std::vector<const Node *> LogReader::LoggedNodes() const {
247 return configuration::GetNodes(logged_configuration());
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800248}
Austin Schuh15649d62019-12-28 16:36:38 -0800249
Austin Schuh11d43732020-09-21 17:28:30 -0700250monotonic_clock::time_point LogReader::monotonic_start_time(
251 const Node *node) const {
Austin Schuh8bd96322020-02-13 21:18:22 -0800252 State *state =
253 states_[configuration::GetNodeIndex(configuration(), node)].get();
254 CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node);
255
Austin Schuhf665eb42022-02-03 18:26:25 -0800256 return state->monotonic_start_time(state->boot_count());
Austin Schuhe309d2a2019-11-29 13:25:21 -0800257}
258
Austin Schuh11d43732020-09-21 17:28:30 -0700259realtime_clock::time_point LogReader::realtime_start_time(
260 const Node *node) const {
Austin Schuh8bd96322020-02-13 21:18:22 -0800261 State *state =
262 states_[configuration::GetNodeIndex(configuration(), node)].get();
263 CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node);
264
Austin Schuhf665eb42022-02-03 18:26:25 -0800265 return state->realtime_start_time(state->boot_count());
Austin Schuhe309d2a2019-11-29 13:25:21 -0800266}
267
Austin Schuh58646e22021-08-23 23:51:46 -0700268void LogReader::OnStart(std::function<void()> fn) {
269 CHECK(!configuration::MultiNode(configuration()));
270 OnStart(nullptr, std::move(fn));
271}
272
273void 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 Kuszmaula16a7912022-06-17 10:58:12 -0700283void 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 Schmiedebergb38477e2022-12-02 16:08:04 -0700299
James Kuszmaula16a7912022-06-17 10:58:12 -0700300 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 Schmiedebergb38477e2022-12-02 16:08:04 -0700308
James Kuszmaula16a7912022-06-17 10:58:12 -0700309 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 Schuh63097262023-08-16 17:04:29 -0700314 MaybeSeedSortedMessages();
James Kuszmaula16a7912022-06-17 10:58:12 -0700315 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 Schuh58646e22021-08-23 23:51:46 -0700325void LogReader::State::OnStart(std::function<void()> fn) {
326 on_starts_.emplace_back(std::move(fn));
327}
328
329void LogReader::State::RunOnStart() {
330 SetRealtimeOffset(monotonic_start_time(boot_count()),
331 realtime_start_time(boot_count()));
332
Alexei Strots036d84e2023-05-03 16:05:12 -0700333 VLOG(1) << "Starting for node '" << MaybeNodeName(node()) << "' at time "
Austin Schuh58646e22021-08-23 23:51:46 -0700334 << monotonic_start_time(boot_count());
Austin Schuhe33c08d2022-02-03 18:15:21 -0800335 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 Schuh58646e22021-08-23 23:51:46 -0700344 }
345 stopped_ = false;
346 started_ = true;
347}
348
349void LogReader::OnEnd(std::function<void()> fn) {
350 CHECK(!configuration::MultiNode(configuration()));
351 OnEnd(nullptr, std::move(fn));
352}
353
354void 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
364void LogReader::State::OnEnd(std::function<void()> fn) {
365 on_ends_.emplace_back(std::move(fn));
366}
367
368void LogReader::State::RunOnEnd() {
Alexei Strots036d84e2023-05-03 16:05:12 -0700369 VLOG(1) << "Ending for node '" << MaybeNodeName(node()) << "' at time "
Austin Schuh58646e22021-08-23 23:51:46 -0700370 << monotonic_start_time(boot_count());
Austin Schuhe33c08d2022-02-03 18:15:21 -0800371 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 Schuh58646e22021-08-23 23:51:46 -0700380 }
381
382 stopped_ = true;
Austin Schuhe33c08d2022-02-03 18:15:21 -0800383 started_ = true;
James Kuszmaula16a7912022-06-17 10:58:12 -0700384 if (message_queuer_.has_value()) {
385 message_queuer_->StopPushing();
386 }
Austin Schuh58646e22021-08-23 23:51:46 -0700387}
388
James Kuszmaul94ca5132022-07-19 09:11:08 -0700389std::vector<
390 std::pair<const aos::Channel *, NodeEventLoopFactory::ExclusiveSenders>>
391LogReader::State::NonExclusiveChannels() {
392 CHECK_NOTNULL(node_event_loop_factory_);
393 const aos::Configuration *config = node_event_loop_factory_->configuration();
394 std::vector<
395 std::pair<const aos::Channel *, NodeEventLoopFactory::ExclusiveSenders>>
396 result{// Timing reports can be sent by logged and replayed applications.
397 {aos::configuration::GetChannel(config, "/aos",
398 "aos.timing.Report", "", node_),
399 NodeEventLoopFactory::ExclusiveSenders::kNo},
400 // AOS_LOG may be used in the log and in replay.
401 {aos::configuration::GetChannel(
402 config, "/aos", "aos.logging.LogMessageFbs", "", node_),
403 NodeEventLoopFactory::ExclusiveSenders::kNo}};
404 for (const Node *const node : configuration::GetNodes(config)) {
405 if (node == nullptr) {
406 break;
407 }
408 const Channel *const old_timestamp_channel = aos::configuration::GetChannel(
409 config,
410 absl::StrCat("/aos/remote_timestamps/", node->name()->string_view()),
James Kuszmaula90f3242022-08-03 13:39:59 -0700411 "aos.message_bridge.RemoteMessage", "", node_, /*quiet=*/true);
James Kuszmaul94ca5132022-07-19 09:11:08 -0700412 // The old-style remote timestamp channel can be populated from any
413 // channel, simulated or replayed.
414 if (old_timestamp_channel != nullptr) {
415 result.push_back(std::make_pair(
416 old_timestamp_channel, NodeEventLoopFactory::ExclusiveSenders::kNo));
417 }
418 }
419 // Remove any channels that weren't found due to not existing in the
420 // config.
421 for (size_t ii = 0; ii < result.size();) {
422 if (result[ii].first == nullptr) {
423 result.erase(result.begin() + ii);
424 } else {
425 ++ii;
426 }
427 }
428 return result;
429}
430
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800431void LogReader::Register() {
432 event_loop_factory_unique_ptr_ =
Austin Schuhac0771c2020-01-07 18:36:30 -0800433 std::make_unique<SimulatedEventLoopFactory>(configuration());
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800434 Register(event_loop_factory_unique_ptr_.get());
435}
436
Austin Schuh58646e22021-08-23 23:51:46 -0700437void LogReader::RegisterWithoutStarting(
438 SimulatedEventLoopFactory *event_loop_factory) {
Austin Schuh92547522019-12-28 14:33:43 -0800439 event_loop_factory_ = event_loop_factory;
Eric Schmiedeberge279b532023-04-19 16:36:02 -0600440 config_remapper_.set_configuration(event_loop_factory_->configuration());
Austin Schuh63097262023-08-16 17:04:29 -0700441
442 const TimestampQueueStrategy timestamp_queue_strategy =
443 ComputeTimestampQueueStrategy();
444
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800445 filters_ =
446 std::make_unique<message_bridge::MultiNodeNoncausalOffsetEstimator>(
Austin Schuhba20ea72021-01-21 16:47:01 -0800447 event_loop_factory_->configuration(), logged_configuration(),
Alexei Strots58017402023-05-03 22:05:06 -0700448 log_files_.boots(), FLAGS_skip_order_validation,
Austin Schuh63097262023-08-16 17:04:29 -0700449 timestamp_queue_strategy ==
450 TimestampQueueStrategy::kQueueTimestampsAtStartup
451 ? chrono::seconds(0)
452 : chrono::duration_cast<chrono::nanoseconds>(
453 chrono::duration<double>(
454 FLAGS_time_estimation_buffer_seconds)));
Austin Schuh92547522019-12-28 14:33:43 -0800455
Austin Schuhe639ea12021-01-25 13:00:22 -0800456 std::vector<TimestampMapper *> timestamp_mappers;
Brian Silvermand90905f2020-09-23 14:42:56 -0700457 for (const Node *node : configuration::GetNodes(configuration())) {
Alexei Strots1f51ac72023-05-15 10:14:54 -0700458 size_t node_index = configuration::GetNodeIndex(configuration(), node);
459 std::string_view node_name = MaybeNodeName(node);
Austin Schuh315b96b2020-12-11 21:21:12 -0800460
James Kuszmaula16a7912022-06-17 10:58:12 -0700461 // We don't run with threading on the buffering for simulated event loops
462 // because we haven't attempted to validate how the interactions beteen the
463 // buffering and the timestamp mapper works when running multiple nodes
464 // concurrently.
Austin Schuh287d43d2020-12-04 20:19:33 -0800465 states_[node_index] = std::make_unique<State>(
Alexei Strots1f51ac72023-05-15 10:14:54 -0700466 !log_files_.ContainsPartsForNode(node_name)
Austin Schuh287d43d2020-12-04 20:19:33 -0800467 ? nullptr
Austin Schuh63097262023-08-16 17:04:29 -0700468 : std::make_unique<TimestampMapper>(node_name, log_files_,
469 timestamp_queue_strategy),
470 timestamp_queue_strategy, filters_.get(),
471 std::bind(&LogReader::NoticeRealtimeEnd, this), node,
Eric Schmiedebergae00e732023-04-12 15:53:17 -0600472 State::ThreadedBuffering::kNo, MaybeMakeReplayChannelIndices(node),
473 before_send_callbacks_);
Austin Schuh8bd96322020-02-13 21:18:22 -0800474 State *state = states_[node_index].get();
Austin Schuh58646e22021-08-23 23:51:46 -0700475 state->SetNodeEventLoopFactory(
Austin Schuhe33c08d2022-02-03 18:15:21 -0800476 event_loop_factory_->GetNodeEventLoopFactory(node),
477 event_loop_factory_);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700478
479 state->SetChannelCount(logged_configuration()->channels()->size());
Austin Schuhe639ea12021-01-25 13:00:22 -0800480 timestamp_mappers.emplace_back(state->timestamp_mapper());
Austin Schuhcde938c2020-02-02 17:30:07 -0800481 }
Austin Schuhe639ea12021-01-25 13:00:22 -0800482 filters_->SetTimestampMappers(std::move(timestamp_mappers));
483
484 // Note: this needs to be set before any times are pulled, or we won't observe
485 // the timestamps.
Austin Schuh87dd3832021-01-01 23:07:31 -0800486 event_loop_factory_->SetTimeConverter(filters_.get());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700487
Austin Schuh287d43d2020-12-04 20:19:33 -0800488 for (const Node *node : configuration::GetNodes(configuration())) {
489 const size_t node_index =
490 configuration::GetNodeIndex(configuration(), node);
491 State *state = states_[node_index].get();
492 for (const Node *other_node : configuration::GetNodes(configuration())) {
493 const size_t other_node_index =
494 configuration::GetNodeIndex(configuration(), other_node);
495 State *other_state = states_[other_node_index].get();
496 if (other_state != state) {
497 state->AddPeer(other_state);
498 }
499 }
500 }
501
Austin Schuh63097262023-08-16 17:04:29 -0700502 // Now that every state has a peer, load all the timestamps into RAM.
503 if (timestamp_queue_strategy ==
504 TimestampQueueStrategy::kQueueTimestampsAtStartup) {
505 for (std::unique_ptr<State> &state : states_) {
506 state->ReadTimestamps();
507 }
508 }
509
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700510 // Register after making all the State objects so we can build references
511 // between them.
512 for (const Node *node : configuration::GetNodes(configuration())) {
513 const size_t node_index =
514 configuration::GetNodeIndex(configuration(), node);
515 State *state = states_[node_index].get();
516
Austin Schuh58646e22021-08-23 23:51:46 -0700517 // If we didn't find any log files with data in them, we won't ever get a
518 // callback or be live. So skip the rest of the setup.
James Kuszmaula16a7912022-06-17 10:58:12 -0700519 if (state->SingleThreadedOldestMessageTime() == BootTimestamp::max_time()) {
Austin Schuh58646e22021-08-23 23:51:46 -0700520 continue;
521 }
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700522
Austin Schuh58646e22021-08-23 23:51:46 -0700523 ++live_nodes_;
524
525 NodeEventLoopFactory *node_factory =
526 event_loop_factory_->GetNodeEventLoopFactory(node);
527 node_factory->OnStartup([this, state, node]() {
528 RegisterDuringStartup(state->MakeEventLoop(), node);
529 });
530 node_factory->OnShutdown([this, state, node]() {
531 RegisterDuringStartup(nullptr, node);
532 state->DestroyEventLoop();
533 });
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700534 }
535
James Kuszmaul46d82582020-05-09 19:50:09 -0700536 if (live_nodes_ == 0) {
537 LOG(FATAL)
538 << "Don't have logs from any of the nodes in the replay config--are "
539 "you sure that the replay config matches the original config?";
540 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800541
Austin Schuh87dd3832021-01-01 23:07:31 -0800542 filters_->CheckGraph();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800543
Austin Schuh858c9f32020-08-31 16:56:12 -0700544 for (std::unique_ptr<State> &state : states_) {
Austin Schuh63097262023-08-16 17:04:29 -0700545 state->MaybeSeedSortedMessages();
Austin Schuh858c9f32020-08-31 16:56:12 -0700546 }
547
Austin Schuh6f3babe2020-01-26 20:34:50 -0800548 // Forwarding is tracked per channel. If it is enabled, we want to turn it
549 // off. Otherwise messages replayed will get forwarded across to the other
Austin Schuh2f8fd752020-09-01 22:38:28 -0700550 // nodes, and also replayed on the other nodes. This may not satisfy all
551 // our users, but it'll start the discussion.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800552 if (configuration::MultiNode(event_loop_factory_->configuration())) {
553 for (size_t i = 0; i < logged_configuration()->channels()->size(); ++i) {
554 const Channel *channel = logged_configuration()->channels()->Get(i);
555 const Node *node = configuration::GetNode(
556 configuration(), channel->source_node()->string_view());
557
Austin Schuh8bd96322020-02-13 21:18:22 -0800558 State *state =
559 states_[configuration::GetNodeIndex(configuration(), node)].get();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800560
561 const Channel *remapped_channel =
Eric Schmiedeberge279b532023-04-19 16:36:02 -0600562 config_remapper_.RemapChannel(state->event_loop(), node, channel);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800563
564 event_loop_factory_->DisableForwarding(remapped_channel);
565 }
Austin Schuh4c3b9702020-08-30 11:34:55 -0700566
567 // If we are replaying a log, we don't want a bunch of redundant messages
568 // from both the real message bridge and simulated message bridge.
James Kuszmaul94ca5132022-07-19 09:11:08 -0700569 event_loop_factory_->PermanentlyDisableStatistics();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800570 }
Austin Schuh891214d2021-11-11 20:35:02 -0800571
572 // Write pseudo start times out to file now that we are all setup.
573 filters_->Start(event_loop_factory_);
Austin Schuh58646e22021-08-23 23:51:46 -0700574}
575
576void LogReader::Register(SimulatedEventLoopFactory *event_loop_factory) {
577 RegisterWithoutStarting(event_loop_factory);
Austin Schuhe33c08d2022-02-03 18:15:21 -0800578 StartAfterRegister(event_loop_factory);
579}
580
581void LogReader::StartAfterRegister(
582 SimulatedEventLoopFactory *event_loop_factory) {
Austin Schuh58646e22021-08-23 23:51:46 -0700583 // We want to start the log file at the last start time of the log files
584 // from all the nodes. Compute how long each node's simulation needs to run
585 // to move time to this point.
586 distributed_clock::time_point start_time = distributed_clock::min_time;
587
588 // TODO(austin): We want an "OnStart" callback for each node rather than
589 // running until the last node.
590
591 for (std::unique_ptr<State> &state : states_) {
Alexei Strotsb8c3a702023-04-19 21:38:25 -0700592 CHECK(state);
Austin Schuh58646e22021-08-23 23:51:46 -0700593 VLOG(1) << "Start time is " << state->monotonic_start_time(0)
Alexei Strots036d84e2023-05-03 16:05:12 -0700594 << " for node '" << MaybeNodeName(state->node()) << "' now "
Austin Schuh63097262023-08-16 17:04:29 -0700595 << (state->event_loop() != nullptr ? state->monotonic_now()
596 : monotonic_clock::min_time);
Austin Schuh58646e22021-08-23 23:51:46 -0700597 if (state->monotonic_start_time(0) == monotonic_clock::min_time) {
598 continue;
599 }
600 // And start computing the start time on the distributed clock now that
601 // that works.
602 start_time = std::max(
603 start_time, state->ToDistributedClock(state->monotonic_start_time(0)));
604 }
605
606 // TODO(austin): If a node doesn't have a start time, we might not queue
607 // enough. If this happens, we'll explode with a frozen error eventually.
608
609 CHECK_GE(start_time, distributed_clock::epoch())
610 << ": Hmm, we have a node starting before the start of time. Offset "
611 "everything.";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800612
Austin Schuhdda74ec2021-01-03 19:30:37 -0800613 {
Austin Schuhdda74ec2021-01-03 19:30:37 -0800614 VLOG(1) << "Running until " << start_time << " in Register";
615 event_loop_factory_->RunFor(start_time.time_since_epoch());
616 VLOG(1) << "At start time";
Austin Schuhdda74ec2021-01-03 19:30:37 -0800617 }
Austin Schuh92547522019-12-28 14:33:43 -0800618
Austin Schuh8bd96322020-02-13 21:18:22 -0800619 for (std::unique_ptr<State> &state : states_) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700620 // Make the RT clock be correct before handing it to the user.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700621 if (state->realtime_start_time(0) != realtime_clock::min_time) {
622 state->SetRealtimeOffset(state->monotonic_start_time(0),
623 state->realtime_start_time(0));
Austin Schuh2f8fd752020-09-01 22:38:28 -0700624 }
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700625 VLOG(1) << "Start time is " << state->monotonic_start_time(0)
Alexei Strots036d84e2023-05-03 16:05:12 -0700626 << " for node '" << MaybeNodeName(state->event_loop()->node())
627 << "' now " << state->monotonic_now();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700628 }
629
630 if (FLAGS_timestamps_to_csv) {
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800631 filters_->Start(event_loop_factory);
Austin Schuh8bd96322020-02-13 21:18:22 -0800632 }
633}
634
Austin Schuh2f8fd752020-09-01 22:38:28 -0700635message_bridge::NoncausalOffsetEstimator *LogReader::GetFilter(
Austin Schuh8bd96322020-02-13 21:18:22 -0800636 const Node *node_a, const Node *node_b) {
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800637 if (filters_) {
638 return filters_->GetFilter(node_a, node_b);
Austin Schuh8bd96322020-02-13 21:18:22 -0800639 }
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800640 return nullptr;
Austin Schuh8bd96322020-02-13 21:18:22 -0800641}
642
Austin Schuh63097262023-08-16 17:04:29 -0700643TimestampQueueStrategy LogReader::ComputeTimestampQueueStrategy() const {
644 if ((log_files_.TimestampsStoredSeparately() &&
645 FLAGS_enable_timestamp_loading) ||
646 FLAGS_force_timestamp_loading) {
647 return TimestampQueueStrategy::kQueueTimestampsAtStartup;
648 } else {
649 return TimestampQueueStrategy::kQueueTogether;
650 }
651}
652
James Kuszmaul09632422022-05-25 15:56:19 -0700653// TODO(jkuszmaul): Make in-line modifications to
654// ServerStatistics/ClientStatistics messages for ShmEventLoop-based replay to
655// avoid messing up anything that depends on them having valid offsets.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800656void LogReader::Register(EventLoop *event_loop) {
Austin Schuh63097262023-08-16 17:04:29 -0700657 const TimestampQueueStrategy timestamp_queue_strategy =
658 ComputeTimestampQueueStrategy();
659
James Kuszmaul09632422022-05-25 15:56:19 -0700660 filters_ =
661 std::make_unique<message_bridge::MultiNodeNoncausalOffsetEstimator>(
662 event_loop->configuration(), logged_configuration(),
Alexei Strots58017402023-05-03 22:05:06 -0700663 log_files_.boots(), FLAGS_skip_order_validation,
Austin Schuh63097262023-08-16 17:04:29 -0700664 timestamp_queue_strategy ==
665 TimestampQueueStrategy::kQueueTimestampsAtStartup
666 ? chrono::seconds(0)
667 : chrono::duration_cast<chrono::nanoseconds>(
668 chrono::duration<double>(
669 FLAGS_time_estimation_buffer_seconds)));
James Kuszmaul09632422022-05-25 15:56:19 -0700670
671 std::vector<TimestampMapper *> timestamp_mappers;
672 for (const Node *node : configuration::GetNodes(configuration())) {
Alexei Strots1f51ac72023-05-15 10:14:54 -0700673 auto node_name = MaybeNodeName(node);
James Kuszmaul09632422022-05-25 15:56:19 -0700674 const size_t node_index =
675 configuration::GetNodeIndex(configuration(), node);
James Kuszmaul09632422022-05-25 15:56:19 -0700676
677 states_[node_index] = std::make_unique<State>(
Alexei Strots1f51ac72023-05-15 10:14:54 -0700678 !log_files_.ContainsPartsForNode(node_name)
James Kuszmaul09632422022-05-25 15:56:19 -0700679 ? nullptr
Austin Schuh63097262023-08-16 17:04:29 -0700680 : std::make_unique<TimestampMapper>(node_name, log_files_,
681 timestamp_queue_strategy),
682 timestamp_queue_strategy, filters_.get(),
683 std::bind(&LogReader::NoticeRealtimeEnd, this), node,
Eric Schmiedebergae00e732023-04-12 15:53:17 -0600684 State::ThreadedBuffering::kYes, MaybeMakeReplayChannelIndices(node),
685 before_send_callbacks_);
James Kuszmaul09632422022-05-25 15:56:19 -0700686 State *state = states_[node_index].get();
687
688 state->SetChannelCount(logged_configuration()->channels()->size());
689 timestamp_mappers.emplace_back(state->timestamp_mapper());
690 }
691
692 filters_->SetTimestampMappers(std::move(timestamp_mappers));
693
694 for (const Node *node : configuration::GetNodes(configuration())) {
695 const size_t node_index =
696 configuration::GetNodeIndex(configuration(), node);
697 State *state = states_[node_index].get();
698 for (const Node *other_node : configuration::GetNodes(configuration())) {
699 const size_t other_node_index =
700 configuration::GetNodeIndex(configuration(), other_node);
701 State *other_state = states_[other_node_index].get();
702 if (other_state != state) {
703 state->AddPeer(other_state);
704 }
705 }
706 }
Austin Schuh63097262023-08-16 17:04:29 -0700707
708 // Now that all the peers are added, we can buffer up all the timestamps if
709 // needed.
710 if (timestamp_queue_strategy ==
711 TimestampQueueStrategy::kQueueTimestampsAtStartup) {
712 for (std::unique_ptr<State> &state : states_) {
713 state->ReadTimestamps();
714 }
715 }
716
James Kuszmaul09632422022-05-25 15:56:19 -0700717 for (const Node *node : configuration::GetNodes(configuration())) {
718 if (node == nullptr || node->name()->string_view() ==
719 event_loop->node()->name()->string_view()) {
720 Register(event_loop, event_loop->node());
721 } else {
722 Register(nullptr, node);
723 }
724 }
Austin Schuh58646e22021-08-23 23:51:46 -0700725}
726
727void LogReader::Register(EventLoop *event_loop, const Node *node) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800728 State *state =
Austin Schuh58646e22021-08-23 23:51:46 -0700729 states_[configuration::GetNodeIndex(configuration(), node)].get();
730
731 // If we didn't find any log files with data in them, we won't ever get a
732 // callback or be live. So skip the rest of the setup.
James Kuszmaula16a7912022-06-17 10:58:12 -0700733 if (state->SingleThreadedOldestMessageTime() == BootTimestamp::max_time()) {
Austin Schuh58646e22021-08-23 23:51:46 -0700734 return;
735 }
James Kuszmaul09632422022-05-25 15:56:19 -0700736
737 if (event_loop != nullptr) {
738 ++live_nodes_;
739 }
Austin Schuh58646e22021-08-23 23:51:46 -0700740
741 if (event_loop_factory_ != nullptr) {
742 event_loop_factory_->GetNodeEventLoopFactory(node)->OnStartup(
743 [this, event_loop, node]() {
744 RegisterDuringStartup(event_loop, node);
745 });
746 } else {
747 RegisterDuringStartup(event_loop, node);
748 }
749}
750
751void LogReader::RegisterDuringStartup(EventLoop *event_loop, const Node *node) {
James Kuszmaul09632422022-05-25 15:56:19 -0700752 if (event_loop != nullptr) {
Austin Schuh58646e22021-08-23 23:51:46 -0700753 CHECK(event_loop->configuration() == configuration());
754 }
755
756 State *state =
757 states_[configuration::GetNodeIndex(configuration(), node)].get();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800758
James Kuszmaul09632422022-05-25 15:56:19 -0700759 if (event_loop == nullptr) {
Austin Schuhe33c08d2022-02-03 18:15:21 -0800760 state->ClearTimeFlags();
761 }
762
Austin Schuh858c9f32020-08-31 16:56:12 -0700763 state->set_event_loop(event_loop);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800764
Tyler Chatow67ddb032020-01-12 14:30:04 -0800765 // We don't run timing reports when trying to print out logged data, because
766 // otherwise we would end up printing out the timing reports themselves...
767 // This is only really relevant when we are replaying into a simulation.
James Kuszmaul09632422022-05-25 15:56:19 -0700768 if (event_loop != nullptr) {
Austin Schuh58646e22021-08-23 23:51:46 -0700769 event_loop->SkipTimingReport();
770 event_loop->SkipAosLog();
771 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800772
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700773 for (size_t logged_channel_index = 0;
774 logged_channel_index < logged_configuration()->channels()->size();
775 ++logged_channel_index) {
Eric Schmiedeberge279b532023-04-19 16:36:02 -0600776 const Channel *channel = config_remapper_.RemapChannel(
Austin Schuh58646e22021-08-23 23:51:46 -0700777 event_loop, node,
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700778 logged_configuration()->channels()->Get(logged_channel_index));
Austin Schuh8bd96322020-02-13 21:18:22 -0800779
Austin Schuhc0b6c4f2021-10-11 18:28:38 -0700780 const bool logged = channel->logger() != LoggerConfig::NOT_LOGGED;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700781 message_bridge::NoncausalOffsetEstimator *filter = nullptr;
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700782
783 State *source_state = nullptr;
James Kuszmaul09632422022-05-25 15:56:19 -0700784
Austin Schuh58646e22021-08-23 23:51:46 -0700785 if (!configuration::ChannelIsSendableOnNode(channel, node) &&
786 configuration::ChannelIsReadableOnNode(channel, node)) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700787 const Node *source_node = configuration::GetNode(
Austin Schuh58646e22021-08-23 23:51:46 -0700788 configuration(), channel->source_node()->string_view());
Austin Schuh8bd96322020-02-13 21:18:22 -0800789
Austin Schuh58646e22021-08-23 23:51:46 -0700790 // We've got a message which is being forwarded to this node.
791 filter = GetFilter(node, source_node);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700792
793 source_state =
794 states_[configuration::GetNodeIndex(configuration(), source_node)]
795 .get();
Austin Schuh8bd96322020-02-13 21:18:22 -0800796 }
Austin Schuh858c9f32020-08-31 16:56:12 -0700797
Austin Schuh58646e22021-08-23 23:51:46 -0700798 // We are the source, and it is forwarded.
799 const bool is_forwarded =
800 configuration::ChannelIsSendableOnNode(channel, node) &&
801 configuration::ConnectionCount(channel);
802
Austin Schuhc0b6c4f2021-10-11 18:28:38 -0700803 state->SetChannel(
804 logged_channel_index,
805 configuration::ChannelIndex(configuration(), channel),
James Kuszmaul09632422022-05-25 15:56:19 -0700806 event_loop && logged &&
807 configuration::ChannelIsReadableOnNode(channel, node)
808 ? event_loop->MakeRawSender(channel)
809 : nullptr,
Austin Schuhc0b6c4f2021-10-11 18:28:38 -0700810 filter, is_forwarded, source_state);
Austin Schuh58646e22021-08-23 23:51:46 -0700811
Austin Schuhc0b6c4f2021-10-11 18:28:38 -0700812 if (is_forwarded && logged) {
Austin Schuh58646e22021-08-23 23:51:46 -0700813 const Node *source_node = configuration::GetNode(
814 configuration(), channel->source_node()->string_view());
815
816 for (const Connection *connection : *channel->destination_nodes()) {
817 const bool delivery_time_is_logged =
818 configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection,
819 source_node);
820
821 if (delivery_time_is_logged) {
822 State *destination_state =
823 states_[configuration::GetNodeIndex(
824 configuration(), connection->name()->string_view())]
825 .get();
James Kuszmaul09632422022-05-25 15:56:19 -0700826 if (destination_state) {
827 destination_state->SetRemoteTimestampSender(
828 logged_channel_index,
829 event_loop ? state->RemoteTimestampSender(channel, connection)
830 : nullptr);
831 }
Austin Schuh58646e22021-08-23 23:51:46 -0700832 }
833 }
834 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800835 }
836
Austin Schuh58646e22021-08-23 23:51:46 -0700837 if (!event_loop) {
838 state->ClearRemoteTimestampSenders();
839 state->set_timer_handler(nullptr);
840 state->set_startup_timer(nullptr);
Austin Schuh6aa77be2020-02-22 21:06:40 -0800841 return;
842 }
843
Austin Schuh858c9f32020-08-31 16:56:12 -0700844 state->set_timer_handler(event_loop->AddTimer([this, state]() {
James Kuszmaula16a7912022-06-17 10:58:12 -0700845 if (state->MultiThreadedOldestMessageTime() == BootTimestamp::max_time()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800846 --live_nodes_;
Alexei Strots036d84e2023-05-03 16:05:12 -0700847 VLOG(1) << "Node '" << MaybeNodeName(state->event_loop()->node())
848 << "' down!";
James Kuszmaula16a7912022-06-17 10:58:12 -0700849 if (exit_on_finish_ && live_nodes_ == 0 &&
850 event_loop_factory_ != nullptr) {
James Kuszmaulb11a1502022-07-01 16:02:25 -0700851 event_loop_factory_->Exit();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800852 }
James Kuszmaul314f1672020-01-03 20:02:08 -0800853 return;
854 }
Austin Schuh2f8fd752020-09-01 22:38:28 -0700855
Austin Schuhdda74ec2021-01-03 19:30:37 -0800856 TimestampedMessage timestamped_message = state->PopOldest();
Austin Schuh58646e22021-08-23 23:51:46 -0700857
858 CHECK_EQ(timestamped_message.monotonic_event_time.boot,
859 state->boot_count());
Austin Schuh05b70472020-01-01 17:11:17 -0800860
Austin Schuhe309d2a2019-11-29 13:25:21 -0800861 const monotonic_clock::time_point monotonic_now =
Austin Schuh858c9f32020-08-31 16:56:12 -0700862 state->event_loop()->context().monotonic_event_time;
James Kuszmaul09632422022-05-25 15:56:19 -0700863 if (event_loop_factory_ != nullptr) {
864 // Only enforce exact timing in simulation.
865 if (!FLAGS_skip_order_validation) {
866 CHECK(monotonic_now == timestamped_message.monotonic_event_time.time)
867 << ": " << FlatbufferToJson(state->event_loop()->node()) << " Now "
868 << monotonic_now << " trying to send "
869 << timestamped_message.monotonic_event_time << " failure "
870 << state->DebugString();
871 } else if (BootTimestamp{.boot = state->boot_count(),
872 .time = monotonic_now} !=
873 timestamped_message.monotonic_event_time) {
874 LOG(WARNING) << "Check failed: monotonic_now == "
875 "timestamped_message.monotonic_event_time) ("
876 << monotonic_now << " vs. "
877 << timestamped_message.monotonic_event_time
878 << "): " << FlatbufferToJson(state->event_loop()->node())
879 << " Now " << monotonic_now << " trying to send "
880 << timestamped_message.monotonic_event_time << " failure "
881 << state->DebugString();
882 }
Austin Schuh2f8fd752020-09-01 22:38:28 -0700883 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800884
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700885 if (timestamped_message.monotonic_event_time.time >
886 state->monotonic_start_time(
887 timestamped_message.monotonic_event_time.boot) ||
James Kuszmaul09632422022-05-25 15:56:19 -0700888 event_loop_factory_ != nullptr ||
889 !FLAGS_drop_realtime_messages_before_start) {
Austin Schuhbd5f74a2021-11-11 20:55:38 -0800890 if (timestamped_message.data != nullptr && !state->found_last_message()) {
Austin Schuhdda74ec2021-01-03 19:30:37 -0800891 if (timestamped_message.monotonic_remote_time !=
James Kuszmaul09632422022-05-25 15:56:19 -0700892 BootTimestamp::min_time() &&
893 !FLAGS_skip_order_validation && event_loop_factory_ != nullptr) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800894 // Confirm that the message was sent on the sending node before the
895 // destination node (this node). As a proxy, do this by making sure
896 // that time on the source node is past when the message was sent.
Austin Schuh87dd3832021-01-01 23:07:31 -0800897 //
898 // TODO(austin): <= means that the cause message (which we know) could
899 // happen after the effect even though we know they are at the same
900 // time. I doubt anyone will notice for a bit, but we should really
901 // fix that.
Austin Schuh58646e22021-08-23 23:51:46 -0700902 BootTimestamp monotonic_remote_now =
903 state->monotonic_remote_now(timestamped_message.channel_index);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700904 if (!FLAGS_skip_order_validation) {
Austin Schuh58646e22021-08-23 23:51:46 -0700905 CHECK_EQ(timestamped_message.monotonic_remote_time.boot,
Austin Schuh3e20c692021-11-16 20:43:16 -0800906 monotonic_remote_now.boot)
907 << state->event_loop()->node()->name()->string_view() << " to "
908 << state->remote_node(timestamped_message.channel_index)
909 ->name()
910 ->string_view()
911 << " while trying to send a message on "
912 << configuration::CleanedChannelToString(
913 logged_configuration()->channels()->Get(
914 timestamped_message.channel_index))
915 << " " << timestamped_message << " " << state->DebugString();
Austin Schuh58646e22021-08-23 23:51:46 -0700916 CHECK_LE(timestamped_message.monotonic_remote_time,
917 monotonic_remote_now)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700918 << state->event_loop()->node()->name()->string_view() << " to "
Austin Schuh287d43d2020-12-04 20:19:33 -0800919 << state->remote_node(timestamped_message.channel_index)
920 ->name()
921 ->string_view()
Austin Schuh315b96b2020-12-11 21:21:12 -0800922 << " while trying to send a message on "
923 << configuration::CleanedChannelToString(
924 logged_configuration()->channels()->Get(
925 timestamped_message.channel_index))
Austin Schuh2f8fd752020-09-01 22:38:28 -0700926 << " " << state->DebugString();
Austin Schuh58646e22021-08-23 23:51:46 -0700927 } else if (monotonic_remote_now.boot !=
928 timestamped_message.monotonic_remote_time.boot) {
929 LOG(WARNING) << "Missmatched boots, " << monotonic_remote_now.boot
930 << " vs "
931 << timestamped_message.monotonic_remote_time.boot;
932 } else if (timestamped_message.monotonic_remote_time >
933 monotonic_remote_now) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700934 LOG(WARNING)
Austin Schuh287d43d2020-12-04 20:19:33 -0800935 << "Check failed: timestamped_message.monotonic_remote_time < "
936 "state->monotonic_remote_now(timestamped_message.channel_"
937 "index) ("
938 << timestamped_message.monotonic_remote_time << " vs. "
939 << state->monotonic_remote_now(
940 timestamped_message.channel_index)
941 << ") " << state->event_loop()->node()->name()->string_view()
942 << " to "
943 << state->remote_node(timestamped_message.channel_index)
944 ->name()
945 ->string_view()
946 << " currently " << timestamped_message.monotonic_event_time
Austin Schuh2f8fd752020-09-01 22:38:28 -0700947 << " ("
948 << state->ToDistributedClock(
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700949 timestamped_message.monotonic_event_time.time)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700950 << ") remote event time "
Austin Schuh287d43d2020-12-04 20:19:33 -0800951 << timestamped_message.monotonic_remote_time << " ("
Austin Schuh2f8fd752020-09-01 22:38:28 -0700952 << state->RemoteToDistributedClock(
Austin Schuh287d43d2020-12-04 20:19:33 -0800953 timestamped_message.channel_index,
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700954 timestamped_message.monotonic_remote_time.time)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700955 << ") " << state->DebugString();
956 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800957 }
958
Austin Schuh15649d62019-12-28 16:36:38 -0800959 // If we have access to the factory, use it to fix the realtime time.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700960 state->SetRealtimeOffset(timestamped_message.monotonic_event_time.time,
Austin Schuh287d43d2020-12-04 20:19:33 -0800961 timestamped_message.realtime_event_time);
Austin Schuh15649d62019-12-28 16:36:38 -0800962
Alexei Strots036d84e2023-05-03 16:05:12 -0700963 VLOG(1) << "For node '" << MaybeNodeName(state->event_loop()->node())
964 << "' sending at " << timestamped_message.monotonic_event_time
965 << " : " << state->DebugString();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700966 // TODO(austin): std::move channel_data in and make that efficient in
967 // simulation.
Austin Schuh287d43d2020-12-04 20:19:33 -0800968 state->Send(std::move(timestamped_message));
Austin Schuhbd5f74a2021-11-11 20:55:38 -0800969 } else if (state->found_last_message() ||
970 (!ignore_missing_data_ &&
971 // When starting up, we can have data which was sent before
972 // the log starts, but the timestamp was after the log
973 // starts. This is unreasonable to avoid, so ignore the
974 // missing data.
975 timestamped_message.monotonic_remote_time.time >=
976 state->monotonic_remote_start_time(
977 timestamped_message.monotonic_remote_time.boot,
978 timestamped_message.channel_index) &&
979 !FLAGS_skip_missing_forwarding_entries)) {
980 if (!state->found_last_message()) {
981 // We've found a timestamp without data that we expect to have data
982 // for. This likely means that we are at the end of the log file.
983 // Record it and CHECK that in the rest of the log file, we don't find
984 // any more data on that channel. Not all channels will end at the
985 // same point in time since they can be in different files.
986 VLOG(1) << "Found the last message on channel "
987 << timestamped_message.channel_index << ", "
988 << configuration::CleanedChannelToString(
989 logged_configuration()->channels()->Get(
990 timestamped_message.channel_index))
Alexei Strots036d84e2023-05-03 16:05:12 -0700991 << " on node '" << MaybeNodeName(state->event_loop()->node())
992 << "' at " << timestamped_message;
Austin Schuhdda74ec2021-01-03 19:30:37 -0800993
Austin Schuhbd5f74a2021-11-11 20:55:38 -0800994 // The user might be working with log files from 1 node but forgot to
995 // configure the infrastructure to log data for a remote channel on
996 // that node. That can be very hard to debug, even though the log
997 // reader is doing the right thing. At least log a warning in that
998 // case and tell the user what is happening so they can either update
999 // their config to log the channel or can find a log with the data.
Austin Schuh2bb80e02021-03-20 21:46:17 -07001000 const std::vector<std::string> logger_nodes =
Alexei Strots1f51ac72023-05-15 10:14:54 -07001001 log_files_.logger_nodes();
1002 if (!logger_nodes.empty()) {
Austin Schuh2bb80e02021-03-20 21:46:17 -07001003 // We have old logs which don't have the logger nodes logged. In
1004 // that case, we can't be helpful :(
1005 bool data_logged = false;
1006 const Channel *channel = logged_configuration()->channels()->Get(
1007 timestamped_message.channel_index);
1008 for (const std::string &node : logger_nodes) {
1009 data_logged |=
1010 configuration::ChannelMessageIsLoggedOnNode(channel, node);
1011 }
1012 if (!data_logged) {
1013 LOG(WARNING) << "Got a timestamp without any logfiles which "
1014 "could contain data for channel "
1015 << configuration::CleanedChannelToString(channel);
1016 LOG(WARNING) << "Only have logs logged on ["
1017 << absl::StrJoin(logger_nodes, ", ") << "]";
1018 LOG(WARNING)
1019 << "Dropping the rest of the data on "
1020 << state->event_loop()->node()->name()->string_view();
1021 LOG(WARNING)
1022 << "Consider using --skip_missing_forwarding_entries to "
1023 "bypass this, update your config to log it, or add data "
1024 "from one of the nodes it is logged on.";
1025 }
1026 }
Austin Schuhbd5f74a2021-11-11 20:55:38 -08001027 // Now that we found the end of one channel, artificially stop the
1028 // rest by setting the found_last_message bit. It is confusing when
1029 // part of your data gets replayed but not all. The rest of them will
1030 // get dropped as they are replayed to keep memory usage down.
1031 state->SetFoundLastMessage(true);
1032
1033 // Vector storing if we've seen a nullptr message or not per channel.
1034 state->set_last_message(timestamped_message.channel_index);
Austin Schuh2bb80e02021-03-20 21:46:17 -07001035 }
1036
Austin Schuhbd5f74a2021-11-11 20:55:38 -08001037 // Make sure that once we have seen the last message on a channel,
1038 // data doesn't start back up again. If the user wants to play
1039 // through events like this, they can set
1040 // --skip_missing_forwarding_entries or ignore_missing_data_.
1041 if (timestamped_message.data == nullptr) {
1042 state->set_last_message(timestamped_message.channel_index);
1043 } else {
1044 if (state->last_message(timestamped_message.channel_index)) {
1045 LOG(FATAL) << "Found missing data in the middle of the log file on "
1046 "channel "
1047 << timestamped_message.channel_index << " "
1048 << configuration::StrippedChannelToString(
1049 logged_configuration()->channels()->Get(
1050 timestamped_message.channel_index))
1051 << " " << timestamped_message << " "
1052 << state->DebugString();
Austin Schuhdda74ec2021-01-03 19:30:37 -08001053 }
Austin Schuh8bd96322020-02-13 21:18:22 -08001054 }
Austin Schuh92547522019-12-28 14:33:43 -08001055 }
Austin Schuhe309d2a2019-11-29 13:25:21 -08001056 } else {
James Kuszmaul09632422022-05-25 15:56:19 -07001057 LOG(WARNING)
1058 << "Not sending data from before the start of the log file. "
1059 << timestamped_message.monotonic_event_time.time.time_since_epoch()
1060 .count()
1061 << " start "
1062 << monotonic_start_time(state->node()).time_since_epoch().count()
1063 << " timestamped_message.data is null";
Austin Schuhe309d2a2019-11-29 13:25:21 -08001064 }
1065
James Kuszmaula16a7912022-06-17 10:58:12 -07001066 const BootTimestamp next_time = state->MultiThreadedOldestMessageTime();
Austin Schuh58646e22021-08-23 23:51:46 -07001067 if (next_time != BootTimestamp::max_time()) {
1068 if (next_time.boot != state->boot_count()) {
Alexei Strots036d84e2023-05-03 16:05:12 -07001069 VLOG(1) << "Next message for node '"
Austin Schuh58646e22021-08-23 23:51:46 -07001070 << MaybeNodeName(state->event_loop()->node())
Alexei Strots036d84e2023-05-03 16:05:12 -07001071 << "' is on the next boot, " << next_time << " now is "
Austin Schuh58646e22021-08-23 23:51:46 -07001072 << state->monotonic_now();
1073 CHECK(event_loop_factory_);
Austin Schuhe33c08d2022-02-03 18:15:21 -08001074 state->NotifyLogfileEnd();
Austin Schuh58646e22021-08-23 23:51:46 -07001075 return;
1076 }
James Kuszmaul09632422022-05-25 15:56:19 -07001077 if (event_loop_factory_ != nullptr) {
Alexei Strots036d84e2023-05-03 16:05:12 -07001078 VLOG(1) << "Scheduling for node '"
1079 << MaybeNodeName(state->event_loop()->node()) << "' wakeup for "
1080 << next_time.time << "("
James Kuszmaul09632422022-05-25 15:56:19 -07001081 << state->ToDistributedClock(next_time.time)
1082 << " distributed), now is " << state->monotonic_now();
1083 } else {
Alexei Strots036d84e2023-05-03 16:05:12 -07001084 VLOG(1) << "Scheduling for node '"
1085 << MaybeNodeName(state->event_loop()->node()) << "' wakeup for "
1086 << next_time.time << ", now is " << state->monotonic_now();
James Kuszmaul09632422022-05-25 15:56:19 -07001087 }
James Kuszmaula16a7912022-06-17 10:58:12 -07001088 // TODO(james): This can result in negative times getting passed-through
1089 // in realtime replay.
Philipp Schradera6712522023-07-05 20:25:11 -07001090 state->Schedule(next_time.time);
James Kuszmaul314f1672020-01-03 20:02:08 -08001091 } else {
Alexei Strots036d84e2023-05-03 16:05:12 -07001092 VLOG(1) << "Node '" << MaybeNodeName(state->event_loop()->node())
1093 << "': No next message, scheduling shutdown";
Austin Schuhe33c08d2022-02-03 18:15:21 -08001094 state->NotifyLogfileEnd();
Austin Schuh2f8fd752020-09-01 22:38:28 -07001095 // Set a timer up immediately after now to die. If we don't do this,
James Kuszmaul09632422022-05-25 15:56:19 -07001096 // then the watchers waiting on the message we just read will never get
Austin Schuh2f8fd752020-09-01 22:38:28 -07001097 // called.
James Kuszmaul09632422022-05-25 15:56:19 -07001098 // Doesn't apply to single-EventLoop replay since the watchers in question
1099 // are not under our control.
Austin Schuheecb9282020-01-08 17:43:30 -08001100 if (event_loop_factory_ != nullptr) {
Philipp Schradera6712522023-07-05 20:25:11 -07001101 state->Schedule(monotonic_now + event_loop_factory_->send_delay() +
1102 std::chrono::nanoseconds(1));
Austin Schuheecb9282020-01-08 17:43:30 -08001103 }
Austin Schuhe309d2a2019-11-29 13:25:21 -08001104 }
Austin Schuh8bd96322020-02-13 21:18:22 -08001105
Alexei Strots036d84e2023-05-03 16:05:12 -07001106 VLOG(1) << "Node '" << MaybeNodeName(state->event_loop()->node())
1107 << "': Done sending at "
Austin Schuh2f8fd752020-09-01 22:38:28 -07001108 << state->event_loop()->context().monotonic_event_time << " now "
1109 << state->monotonic_now();
Austin Schuh858c9f32020-08-31 16:56:12 -07001110 }));
Austin Schuhe309d2a2019-11-29 13:25:21 -08001111
Austin Schuh63097262023-08-16 17:04:29 -07001112 state->MaybeSeedSortedMessages();
James Kuszmaula16a7912022-06-17 10:58:12 -07001113
1114 if (state->SingleThreadedOldestMessageTime() != BootTimestamp::max_time()) {
Austin Schuh58646e22021-08-23 23:51:46 -07001115 state->set_startup_timer(
Austin Schuhe33c08d2022-02-03 18:15:21 -08001116 event_loop->AddTimer([state]() { state->NotifyLogfileStart(); }));
1117 if (start_time_ != realtime_clock::min_time) {
1118 state->SetStartTimeFlag(start_time_);
1119 }
1120 if (end_time_ != realtime_clock::max_time) {
1121 state->SetEndTimeFlag(end_time_);
James Kuszmaulb11a1502022-07-01 16:02:25 -07001122 ++live_nodes_with_realtime_time_end_;
Austin Schuhe33c08d2022-02-03 18:15:21 -08001123 }
Austin Schuh58646e22021-08-23 23:51:46 -07001124 event_loop->OnRun([state]() {
James Kuszmaula16a7912022-06-17 10:58:12 -07001125 BootTimestamp next_time = state->SingleThreadedOldestMessageTime();
Austin Schuh58646e22021-08-23 23:51:46 -07001126 CHECK_EQ(next_time.boot, state->boot_count());
James Kuszmaula16a7912022-06-17 10:58:12 -07001127 // Queue up messages and then set clock offsets (we don't want to set
1128 // clock offsets before we've done the work of getting the first messages
1129 // primed).
1130 state->QueueThreadUntil(
1131 next_time + std::chrono::duration_cast<std::chrono::nanoseconds>(
1132 std::chrono::duration<double>(
1133 FLAGS_threaded_look_ahead_seconds)));
James Kuszmaulc3f34d12022-08-15 15:57:55 -07001134 state->MaybeSetClockOffset();
Philipp Schradera6712522023-07-05 20:25:11 -07001135 state->Schedule(next_time.time);
1136 state->SetUpStartupTimer();
Austin Schuh58646e22021-08-23 23:51:46 -07001137 });
Austin Schuhe309d2a2019-11-29 13:25:21 -08001138 }
1139}
1140
Austin Schuhe33c08d2022-02-03 18:15:21 -08001141void LogReader::SetEndTime(std::string end_time) {
1142 if (end_time.empty()) {
1143 SetEndTime(realtime_clock::max_time);
1144 } else {
1145 std::optional<aos::realtime_clock::time_point> parsed_end_time =
1146 aos::realtime_clock::FromString(end_time);
1147 CHECK(parsed_end_time) << ": Failed to parse end time '" << end_time
1148 << "'. Expected a date in the format of "
1149 "2021-01-15_15-30-35.000000000.";
1150 SetEndTime(*parsed_end_time);
1151 }
1152}
1153
1154void LogReader::SetEndTime(realtime_clock::time_point end_time) {
1155 end_time_ = end_time;
1156}
1157
1158void LogReader::SetStartTime(std::string start_time) {
1159 if (start_time.empty()) {
1160 SetStartTime(realtime_clock::min_time);
1161 } else {
1162 std::optional<aos::realtime_clock::time_point> parsed_start_time =
1163 aos::realtime_clock::FromString(start_time);
1164 CHECK(parsed_start_time) << ": Failed to parse start time '" << start_time
1165 << "'. Expected a date in the format of "
1166 "2021-01-15_15-30-35.000000000.";
1167 SetStartTime(*parsed_start_time);
1168 }
1169}
1170
1171void LogReader::SetStartTime(realtime_clock::time_point start_time) {
1172 start_time_ = start_time;
1173}
1174
Austin Schuhe309d2a2019-11-29 13:25:21 -08001175void LogReader::Deregister() {
James Kuszmaul84ff3e52020-01-03 19:48:53 -08001176 // Make sure that things get destroyed in the correct order, rather than
1177 // relying on getting the order correct in the class definition.
Austin Schuh8bd96322020-02-13 21:18:22 -08001178 for (std::unique_ptr<State> &state : states_) {
Austin Schuh858c9f32020-08-31 16:56:12 -07001179 state->Deregister();
Austin Schuhe309d2a2019-11-29 13:25:21 -08001180 }
Austin Schuh92547522019-12-28 14:33:43 -08001181
James Kuszmaul84ff3e52020-01-03 19:48:53 -08001182 event_loop_factory_unique_ptr_.reset();
1183 event_loop_factory_ = nullptr;
Austin Schuhe309d2a2019-11-29 13:25:21 -08001184}
1185
James Kuszmaul53da7f32022-09-11 11:11:55 -07001186namespace {
1187// Checks if the specified channel name/type exists in the config and, depending
1188// on the value of conflict_handling, calls conflict_handler or just dies.
1189template <typename F>
Eric Schmiedeberge279b532023-04-19 16:36:02 -06001190void CheckAndHandleRemapConflict(
1191 std::string_view new_name, std::string_view new_type,
1192 const Configuration *config,
1193 ConfigRemapper::RemapConflict conflict_handling, F conflict_handler) {
James Kuszmaul53da7f32022-09-11 11:11:55 -07001194 const Channel *existing_channel =
1195 configuration::GetChannel(config, new_name, new_type, "", nullptr, true);
1196 if (existing_channel != nullptr) {
1197 switch (conflict_handling) {
Eric Schmiedeberge279b532023-04-19 16:36:02 -06001198 case ConfigRemapper::RemapConflict::kDisallow:
James Kuszmaul53da7f32022-09-11 11:11:55 -07001199 LOG(FATAL)
1200 << "Channel "
1201 << configuration::StrippedChannelToString(existing_channel)
1202 << " is already used--you can't remap a logged channel to it.";
1203 break;
Eric Schmiedeberge279b532023-04-19 16:36:02 -06001204 case ConfigRemapper::RemapConflict::kCascade:
James Kuszmaul53da7f32022-09-11 11:11:55 -07001205 LOG(INFO) << "Automatically remapping "
1206 << configuration::StrippedChannelToString(existing_channel)
1207 << " to avoid conflicts.";
1208 conflict_handler();
1209 break;
1210 }
1211 }
1212}
1213} // namespace
1214
Eric Schmiedeberge279b532023-04-19 16:36:02 -06001215void LogReader::RemapLoggedChannel(
1216 std::string_view name, std::string_view type, std::string_view add_prefix,
1217 std::string_view new_type,
1218 ConfigRemapper::RemapConflict conflict_handling) {
1219 CheckEventsAreNotScheduled();
1220 config_remapper_.RemapOriginalChannel(name, type, nullptr, add_prefix,
1221 new_type, conflict_handling);
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001222}
1223
Eric Schmiedeberge279b532023-04-19 16:36:02 -06001224void LogReader::RemapLoggedChannel(
1225 std::string_view name, std::string_view type, const Node *node,
1226 std::string_view add_prefix, std::string_view new_type,
1227 ConfigRemapper::RemapConflict conflict_handling) {
1228 CheckEventsAreNotScheduled();
1229 config_remapper_.RemapOriginalChannel(name, type, node, add_prefix, new_type,
1230 conflict_handling);
Austin Schuh01b4c352020-09-21 23:09:39 -07001231}
1232
Sanjay Narayanan5ec00232022-07-08 15:21:30 -07001233void LogReader::RenameLoggedChannel(const std::string_view name,
1234 const std::string_view type,
1235 const std::string_view new_name,
1236 const std::vector<MapT> &add_maps) {
Eric Schmiedeberge279b532023-04-19 16:36:02 -06001237 CheckEventsAreNotScheduled();
Sanjay Narayanan5ec00232022-07-08 15:21:30 -07001238 RenameLoggedChannel(name, type, nullptr, new_name, add_maps);
1239}
1240
1241void LogReader::RenameLoggedChannel(const std::string_view name,
1242 const std::string_view type,
1243 const Node *const node,
1244 const std::string_view new_name,
1245 const std::vector<MapT> &add_maps) {
Eric Schmiedeberge279b532023-04-19 16:36:02 -06001246 CheckEventsAreNotScheduled();
1247 config_remapper_.RenameOriginalChannel(name, type, node, new_name, add_maps);
Sanjay Narayanan5ec00232022-07-08 15:21:30 -07001248}
1249
Eric Schmiedeberge279b532023-04-19 16:36:02 -06001250void LogReader::CheckEventsAreNotScheduled() {
Austin Schuh8bd96322020-02-13 21:18:22 -08001251 for (std::unique_ptr<State> &state : states_) {
Austin Schuh6aa77be2020-02-22 21:06:40 -08001252 if (state) {
Austin Schuh858c9f32020-08-31 16:56:12 -07001253 CHECK(!state->event_loop())
Austin Schuh6aa77be2020-02-22 21:06:40 -08001254 << ": Can't change the mapping after the events are scheduled.";
1255 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001256 }
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001257}
1258
Naman Guptacf6d4422023-03-01 11:41:00 -08001259std::unique_ptr<const ReplayChannelIndices>
1260LogReader::MaybeMakeReplayChannelIndices(const Node *node) {
Eric Schmiedebergb38477e2022-12-02 16:08:04 -07001261 if (replay_channels_ == nullptr) {
1262 return nullptr;
1263 } else {
Naman Guptacf6d4422023-03-01 11:41:00 -08001264 std::unique_ptr<ReplayChannelIndices> replay_channel_indices =
1265 std::make_unique<ReplayChannelIndices>();
Eric Schmiedebergb38477e2022-12-02 16:08:04 -07001266 for (auto const &channel : *replay_channels_) {
1267 const Channel *ch = configuration::GetChannel(
1268 logged_configuration(), channel.first, channel.second, "", node);
1269 if (ch == nullptr) {
1270 LOG(WARNING) << "Channel: " << channel.first << " " << channel.second
1271 << " not found in configuration for node: "
1272 << node->name()->string_view() << " Skipping ...";
1273 continue;
1274 }
1275 const size_t channel_index =
1276 configuration::ChannelIndex(logged_configuration(), ch);
Naman Guptacf6d4422023-03-01 11:41:00 -08001277 replay_channel_indices->emplace_back(channel_index);
Eric Schmiedebergb38477e2022-12-02 16:08:04 -07001278 }
Naman Guptacf6d4422023-03-01 11:41:00 -08001279 std::sort(replay_channel_indices->begin(), replay_channel_indices->end());
1280 return replay_channel_indices;
Eric Schmiedebergb38477e2022-12-02 16:08:04 -07001281 }
1282}
1283
Austin Schuh1c227352021-09-17 12:53:54 -07001284std::vector<const Channel *> LogReader::RemappedChannels() const {
Eric Schmiedeberge279b532023-04-19 16:36:02 -06001285 return config_remapper_.RemappedChannels();
Austin Schuh1c227352021-09-17 12:53:54 -07001286}
1287
Austin Schuh6f3babe2020-01-26 20:34:50 -08001288const Channel *LogReader::RemapChannel(const EventLoop *event_loop,
Austin Schuh58646e22021-08-23 23:51:46 -07001289 const Node *node,
Austin Schuh6f3babe2020-01-26 20:34:50 -08001290 const Channel *channel) {
Eric Schmiedeberge279b532023-04-19 16:36:02 -06001291 return config_remapper_.RemapChannel(event_loop, node, channel);
Austin Schuh6f3babe2020-01-26 20:34:50 -08001292}
1293
James Kuszmaul09632422022-05-25 15:56:19 -07001294LogReader::State::State(
1295 std::unique_ptr<TimestampMapper> timestamp_mapper,
Austin Schuh63097262023-08-16 17:04:29 -07001296 TimestampQueueStrategy timestamp_queue_strategy,
James Kuszmaul09632422022-05-25 15:56:19 -07001297 message_bridge::MultiNodeNoncausalOffsetEstimator *multinode_filters,
James Kuszmaulb11a1502022-07-01 16:02:25 -07001298 std::function<void()> notice_realtime_end, const Node *node,
1299 LogReader::State::ThreadedBuffering threading,
Eric Schmiedebergae00e732023-04-12 15:53:17 -06001300 std::unique_ptr<const ReplayChannelIndices> replay_channel_indices,
1301 const std::vector<std::function<void(void *message)>>
1302 &before_send_callbacks)
James Kuszmaul09632422022-05-25 15:56:19 -07001303 : timestamp_mapper_(std::move(timestamp_mapper)),
Austin Schuh63097262023-08-16 17:04:29 -07001304 timestamp_queue_strategy_(timestamp_queue_strategy),
James Kuszmaulb11a1502022-07-01 16:02:25 -07001305 notice_realtime_end_(notice_realtime_end),
James Kuszmaul09632422022-05-25 15:56:19 -07001306 node_(node),
James Kuszmaula16a7912022-06-17 10:58:12 -07001307 multinode_filters_(multinode_filters),
Eric Schmiedebergb38477e2022-12-02 16:08:04 -07001308 threading_(threading),
Eric Schmiedebergae00e732023-04-12 15:53:17 -06001309 replay_channel_indices_(std::move(replay_channel_indices)),
1310 before_send_callbacks_(before_send_callbacks) {
Naman Guptaa68401c2022-12-08 14:34:06 -08001311 // If timestamp_mapper_ is nullptr, then there are no log parts associated
1312 // with this node. If there are no log parts for the node, there will be no
1313 // log data, and so we do not need to worry about the replay channel filters.
1314 if (replay_channel_indices_ != nullptr && timestamp_mapper_ != nullptr) {
Eric Schmiedebergb38477e2022-12-02 16:08:04 -07001315 timestamp_mapper_->set_replay_channels_callback(
Naman Guptacf6d4422023-03-01 11:41:00 -08001316 [filter = replay_channel_indices_.get()](
Eric Schmiedebergb38477e2022-12-02 16:08:04 -07001317 const TimestampedMessage &message) -> bool {
1318 auto const begin = filter->cbegin();
1319 auto const end = filter->cend();
1320 // TODO: benchmark strategies for channel_index matching
1321 return std::binary_search(begin, end, message.channel_index);
1322 });
1323 }
1324}
Austin Schuh287d43d2020-12-04 20:19:33 -08001325
1326void LogReader::State::AddPeer(State *peer) {
1327 if (timestamp_mapper_ && peer->timestamp_mapper_) {
1328 timestamp_mapper_->AddPeer(peer->timestamp_mapper_.get());
1329 }
1330}
Austin Schuh858c9f32020-08-31 16:56:12 -07001331
Austin Schuh58646e22021-08-23 23:51:46 -07001332void LogReader::State::SetNodeEventLoopFactory(
Austin Schuhe33c08d2022-02-03 18:15:21 -08001333 NodeEventLoopFactory *node_event_loop_factory,
1334 SimulatedEventLoopFactory *event_loop_factory) {
Austin Schuh858c9f32020-08-31 16:56:12 -07001335 node_event_loop_factory_ = node_event_loop_factory;
Austin Schuhe33c08d2022-02-03 18:15:21 -08001336 event_loop_factory_ = event_loop_factory;
Austin Schuh858c9f32020-08-31 16:56:12 -07001337}
1338
1339void LogReader::State::SetChannelCount(size_t count) {
1340 channels_.resize(count);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001341 remote_timestamp_senders_.resize(count);
Austin Schuh858c9f32020-08-31 16:56:12 -07001342 filters_.resize(count);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001343 channel_source_state_.resize(count);
1344 factory_channel_index_.resize(count);
1345 queue_index_map_.resize(count);
Austin Schuh858c9f32020-08-31 16:56:12 -07001346}
1347
Austin Schuh58646e22021-08-23 23:51:46 -07001348void LogReader::State::SetRemoteTimestampSender(
1349 size_t logged_channel_index, RemoteMessageSender *remote_timestamp_sender) {
1350 remote_timestamp_senders_[logged_channel_index] = remote_timestamp_sender;
1351}
1352
Austin Schuh858c9f32020-08-31 16:56:12 -07001353void LogReader::State::SetChannel(
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001354 size_t logged_channel_index, size_t factory_channel_index,
1355 std::unique_ptr<RawSender> sender,
Austin Schuh58646e22021-08-23 23:51:46 -07001356 message_bridge::NoncausalOffsetEstimator *filter, bool is_forwarded,
1357 State *source_state) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001358 channels_[logged_channel_index] = std::move(sender);
1359 filters_[logged_channel_index] = filter;
Austin Schuh58646e22021-08-23 23:51:46 -07001360 channel_source_state_[logged_channel_index] = source_state;
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001361
Austin Schuh58646e22021-08-23 23:51:46 -07001362 if (is_forwarded) {
1363 queue_index_map_[logged_channel_index] =
1364 std::make_unique<std::vector<State::ContiguousSentTimestamp>>();
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001365 }
1366
1367 factory_channel_index_[logged_channel_index] = factory_channel_index;
1368}
1369
James Kuszmaula16a7912022-06-17 10:58:12 -07001370void LogReader::State::TrackMessageSendTiming(
1371 const RawSender &sender, monotonic_clock::time_point expected_send_time) {
1372 if (event_loop_ == nullptr || !timing_statistics_sender_.valid()) {
1373 return;
1374 }
1375
1376 timing::MessageTimingT sample;
1377 sample.channel = configuration::ChannelIndex(event_loop_->configuration(),
1378 sender.channel());
1379 sample.expected_send_time = expected_send_time.time_since_epoch().count();
1380 sample.actual_send_time =
1381 sender.monotonic_sent_time().time_since_epoch().count();
1382 sample.send_time_error = aos::time::DurationInSeconds(
1383 expected_send_time - sender.monotonic_sent_time());
1384 send_timings_.push_back(sample);
1385
1386 // Somewhat arbitrarily send out timing information in batches of 100. No need
1387 // to create excessive overhead in regenerated logfiles.
1388 // TODO(james): The overhead may be fine.
1389 constexpr size_t kMaxTimesPerStatisticsMessage = 100;
1390 CHECK(timing_statistics_sender_.valid());
1391 if (send_timings_.size() == kMaxTimesPerStatisticsMessage) {
1392 SendMessageTimings();
1393 }
1394}
1395
1396void LogReader::State::SendMessageTimings() {
1397 if (send_timings_.empty() || !timing_statistics_sender_.valid()) {
1398 return;
1399 }
1400 auto builder = timing_statistics_sender_.MakeBuilder();
1401 std::vector<flatbuffers::Offset<timing::MessageTiming>> timing_offsets;
1402 for (const auto &timing : send_timings_) {
1403 timing_offsets.push_back(
1404 timing::MessageTiming::Pack(*builder.fbb(), &timing));
1405 }
1406 send_timings_.clear();
1407 flatbuffers::Offset<
1408 flatbuffers::Vector<flatbuffers::Offset<timing::MessageTiming>>>
1409 timings_offset = builder.fbb()->CreateVector(timing_offsets);
1410 timing::ReplayTiming::Builder timing_builder =
1411 builder.MakeBuilder<timing::ReplayTiming>();
1412 timing_builder.add_messages(timings_offset);
1413 timing_statistics_sender_.CheckOk(builder.Send(timing_builder.Finish()));
1414}
1415
Eric Schmiedebergae00e732023-04-12 15:53:17 -06001416bool LogReader::State::Send(const TimestampedMessage &&timestamped_message) {
Austin Schuh287d43d2020-12-04 20:19:33 -08001417 aos::RawSender *sender = channels_[timestamped_message.channel_index].get();
Austin Schuh58646e22021-08-23 23:51:46 -07001418 CHECK(sender);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001419 uint32_t remote_queue_index = 0xffffffff;
1420
Austin Schuh287d43d2020-12-04 20:19:33 -08001421 if (remote_timestamp_senders_[timestamped_message.channel_index] != nullptr) {
Austin Schuh58646e22021-08-23 23:51:46 -07001422 State *source_state =
1423 CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]);
Austin Schuh9942bae2021-01-07 22:06:44 -08001424 std::vector<ContiguousSentTimestamp> *queue_index_map = CHECK_NOTNULL(
Austin Schuh58646e22021-08-23 23:51:46 -07001425 source_state->queue_index_map_[timestamped_message.channel_index]
Austin Schuh287d43d2020-12-04 20:19:33 -08001426 .get());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001427
Austin Schuh9942bae2021-01-07 22:06:44 -08001428 struct SentTimestamp {
1429 monotonic_clock::time_point monotonic_event_time;
1430 uint32_t queue_index;
1431 } search;
1432
Austin Schuh58646e22021-08-23 23:51:46 -07001433 CHECK_EQ(timestamped_message.monotonic_remote_time.boot,
1434 source_state->boot_count());
Tyler Chatowbf0609c2021-07-31 16:13:27 -07001435 search.monotonic_event_time =
1436 timestamped_message.monotonic_remote_time.time;
Austin Schuh58646e22021-08-23 23:51:46 -07001437 search.queue_index = timestamped_message.remote_queue_index.index;
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001438
1439 // Find the sent time if available.
1440 auto element = std::lower_bound(
1441 queue_index_map->begin(), queue_index_map->end(), search,
Austin Schuh9942bae2021-01-07 22:06:44 -08001442 [](ContiguousSentTimestamp a, SentTimestamp b) {
1443 if (a.ending_monotonic_event_time < b.monotonic_event_time) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001444 return true;
1445 }
Austin Schuh9942bae2021-01-07 22:06:44 -08001446 if (a.starting_monotonic_event_time > b.monotonic_event_time) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001447 return false;
1448 }
Austin Schuh9942bae2021-01-07 22:06:44 -08001449
1450 if (a.ending_queue_index < b.queue_index) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001451 return true;
1452 }
Austin Schuh9942bae2021-01-07 22:06:44 -08001453 if (a.starting_queue_index >= b.queue_index) {
1454 return false;
1455 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001456
Austin Schuh9942bae2021-01-07 22:06:44 -08001457 // If it isn't clearly below or above, it is below. Since we return
1458 // the last element <, this will return a match.
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001459 return false;
1460 });
1461
1462 // TODO(austin): Be a bit more principled here, but we will want to do that
1463 // after the logger rewrite. We hit this when one node finishes, but the
1464 // other node isn't done yet. So there is no send time, but there is a
1465 // receive time.
1466 if (element != queue_index_map->end()) {
Austin Schuh58646e22021-08-23 23:51:46 -07001467 CHECK_EQ(timestamped_message.monotonic_remote_time.boot,
1468 source_state->boot_count());
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001469
1470 CHECK_GE(timestamped_message.monotonic_remote_time.time,
Austin Schuh9942bae2021-01-07 22:06:44 -08001471 element->starting_monotonic_event_time);
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001472 CHECK_LE(timestamped_message.monotonic_remote_time.time,
Austin Schuh9942bae2021-01-07 22:06:44 -08001473 element->ending_monotonic_event_time);
Austin Schuh58646e22021-08-23 23:51:46 -07001474 CHECK_GE(timestamped_message.remote_queue_index.index,
Austin Schuh9942bae2021-01-07 22:06:44 -08001475 element->starting_queue_index);
Austin Schuh58646e22021-08-23 23:51:46 -07001476 CHECK_LE(timestamped_message.remote_queue_index.index,
Austin Schuh9942bae2021-01-07 22:06:44 -08001477 element->ending_queue_index);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001478
Austin Schuh58646e22021-08-23 23:51:46 -07001479 remote_queue_index = timestamped_message.remote_queue_index.index +
Austin Schuh9942bae2021-01-07 22:06:44 -08001480 element->actual_queue_index -
1481 element->starting_queue_index;
1482 } else {
1483 VLOG(1) << "No timestamp match in the map.";
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001484 }
Austin Schuh58646e22021-08-23 23:51:46 -07001485 CHECK_EQ(timestamped_message.monotonic_remote_time.boot,
1486 source_state->boot_count());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001487 }
1488
James Kuszmaul09632422022-05-25 15:56:19 -07001489 if (event_loop_factory_ != nullptr &&
1490 channel_source_state_[timestamped_message.channel_index] != nullptr &&
1491 multinode_filters_ != nullptr) {
1492 // Sanity check that we are using consistent boot uuids.
1493 State *source_state =
1494 channel_source_state_[timestamped_message.channel_index];
1495 CHECK_EQ(multinode_filters_->boot_uuid(
1496 configuration::GetNodeIndex(event_loop_->configuration(),
1497 source_state->node()),
1498 timestamped_message.monotonic_remote_time.boot),
1499 CHECK_NOTNULL(
1500 CHECK_NOTNULL(
1501 channel_source_state_[timestamped_message.channel_index])
1502 ->event_loop_)
1503 ->boot_uuid());
1504 }
1505
Eric Schmiedebergae00e732023-04-12 15:53:17 -06001506 // Right before sending allow the user to process the message.
1507 if (before_send_callbacks_[timestamped_message.channel_index]) {
1508 // Only channels that are forwarded and sent from this State's node will be
1509 // in the queue_index_map_
1510 if (queue_index_map_[timestamped_message.channel_index]) {
1511 before_send_callbacks_[timestamped_message.channel_index](
1512 timestamped_message.data->mutable_data());
1513 }
1514 }
1515
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001516 // Send! Use the replayed queue index here instead of the logged queue index
1517 // for the remote queue index. This makes re-logging work.
Austin Schuhaf8a0d32023-05-03 09:53:06 -07001518 const RawSender::Error err = sender->Send(
Austin Schuhe0ab4de2023-05-03 08:05:08 -07001519 SharedSpan(timestamped_message.data, &timestamped_message.data->span),
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001520 timestamped_message.monotonic_remote_time.time,
Austin Schuh8902fa52021-03-14 22:39:24 -07001521 timestamped_message.realtime_remote_time, remote_queue_index,
1522 (channel_source_state_[timestamped_message.channel_index] != nullptr
James Kuszmaul09632422022-05-25 15:56:19 -07001523 ? CHECK_NOTNULL(multinode_filters_)
1524 ->boot_uuid(configuration::GetNodeIndex(
1525 event_loop_->configuration(),
1526 channel_source_state_[timestamped_message
1527 .channel_index]
1528 ->node()),
1529 timestamped_message.monotonic_remote_time.boot)
Austin Schuh8902fa52021-03-14 22:39:24 -07001530 : event_loop_->boot_uuid()));
milind1f1dca32021-07-03 13:50:07 -07001531 if (err != RawSender::Error::kOk) return false;
James Kuszmaula16a7912022-06-17 10:58:12 -07001532 if (monotonic_start_time(timestamped_message.monotonic_event_time.boot) <=
1533 timestamped_message.monotonic_event_time.time) {
1534 // Only track errors for non-fetched messages.
1535 TrackMessageSendTiming(
1536 *sender,
1537 timestamped_message.monotonic_event_time.time + clock_offset());
1538 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001539
Austin Schuh287d43d2020-12-04 20:19:33 -08001540 if (queue_index_map_[timestamped_message.channel_index]) {
Austin Schuh58646e22021-08-23 23:51:46 -07001541 CHECK_EQ(timestamped_message.monotonic_event_time.boot, boot_count());
Austin Schuh9942bae2021-01-07 22:06:44 -08001542 if (queue_index_map_[timestamped_message.channel_index]->empty()) {
1543 // Nothing here, start a range with 0 length.
1544 ContiguousSentTimestamp timestamp;
1545 timestamp.starting_monotonic_event_time =
1546 timestamp.ending_monotonic_event_time =
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001547 timestamped_message.monotonic_event_time.time;
Austin Schuh9942bae2021-01-07 22:06:44 -08001548 timestamp.starting_queue_index = timestamp.ending_queue_index =
Austin Schuh58646e22021-08-23 23:51:46 -07001549 timestamped_message.queue_index.index;
Austin Schuh9942bae2021-01-07 22:06:44 -08001550 timestamp.actual_queue_index = sender->sent_queue_index();
1551 queue_index_map_[timestamped_message.channel_index]->emplace_back(
1552 timestamp);
1553 } else {
1554 // We've got something. See if the next timestamp is still contiguous. If
1555 // so, grow it.
1556 ContiguousSentTimestamp *back =
1557 &queue_index_map_[timestamped_message.channel_index]->back();
1558 if ((back->starting_queue_index - back->actual_queue_index) ==
milind1f1dca32021-07-03 13:50:07 -07001559 (timestamped_message.queue_index.index -
1560 sender->sent_queue_index())) {
Austin Schuh58646e22021-08-23 23:51:46 -07001561 back->ending_queue_index = timestamped_message.queue_index.index;
Austin Schuh9942bae2021-01-07 22:06:44 -08001562 back->ending_monotonic_event_time =
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001563 timestamped_message.monotonic_event_time.time;
Austin Schuh9942bae2021-01-07 22:06:44 -08001564 } else {
1565 // Otherwise, make a new one.
1566 ContiguousSentTimestamp timestamp;
1567 timestamp.starting_monotonic_event_time =
1568 timestamp.ending_monotonic_event_time =
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001569 timestamped_message.monotonic_event_time.time;
Austin Schuh9942bae2021-01-07 22:06:44 -08001570 timestamp.starting_queue_index = timestamp.ending_queue_index =
Austin Schuh58646e22021-08-23 23:51:46 -07001571 timestamped_message.queue_index.index;
Austin Schuh9942bae2021-01-07 22:06:44 -08001572 timestamp.actual_queue_index = sender->sent_queue_index();
1573 queue_index_map_[timestamped_message.channel_index]->emplace_back(
1574 timestamp);
1575 }
1576 }
1577
1578 // TODO(austin): Should we prune the map? On a many day log, I only saw the
1579 // queue index diverge a couple of elements, which would be a very small
1580 // map.
Austin Schuh287d43d2020-12-04 20:19:33 -08001581 } else if (remote_timestamp_senders_[timestamped_message.channel_index] !=
1582 nullptr) {
James Kuszmaul09632422022-05-25 15:56:19 -07001583 // TODO(james): Currently, If running replay against a single event loop,
1584 // remote timestamps will not get replayed because this code-path only
1585 // gets triggered on the event loop that receives the forwarded message
1586 // that the timestamps correspond to. This code, as written, also doesn't
1587 // correctly handle a non-zero clock_offset for the *_remote_time fields.
Austin Schuh58646e22021-08-23 23:51:46 -07001588 State *source_state =
1589 CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]);
1590
Austin Schuh969cd602021-01-03 00:09:45 -08001591 flatbuffers::FlatBufferBuilder fbb;
1592 fbb.ForceDefaults(true);
Austin Schuhcdd90272021-03-15 12:46:16 -07001593 flatbuffers::Offset<flatbuffers::Vector<uint8_t>> boot_uuid_offset =
1594 event_loop_->boot_uuid().PackVector(&fbb);
Austin Schuh315b96b2020-12-11 21:21:12 -08001595
Austin Schuh969cd602021-01-03 00:09:45 -08001596 RemoteMessage::Builder message_header_builder(fbb);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001597
1598 message_header_builder.add_channel_index(
Austin Schuh287d43d2020-12-04 20:19:33 -08001599 factory_channel_index_[timestamped_message.channel_index]);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001600
1601 // Swap the remote and sent metrics. They are from the sender's
1602 // perspective, not the receiver's perspective.
1603 message_header_builder.add_monotonic_sent_time(
1604 sender->monotonic_sent_time().time_since_epoch().count());
1605 message_header_builder.add_realtime_sent_time(
1606 sender->realtime_sent_time().time_since_epoch().count());
1607 message_header_builder.add_queue_index(sender->sent_queue_index());
1608
Austin Schuh58646e22021-08-23 23:51:46 -07001609 CHECK_EQ(timestamped_message.monotonic_remote_time.boot,
1610 source_state->boot_count());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001611 message_header_builder.add_monotonic_remote_time(
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001612 timestamped_message.monotonic_remote_time.time.time_since_epoch()
1613 .count());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001614 message_header_builder.add_realtime_remote_time(
Austin Schuh287d43d2020-12-04 20:19:33 -08001615 timestamped_message.realtime_remote_time.time_since_epoch().count());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001616
1617 message_header_builder.add_remote_queue_index(remote_queue_index);
Austin Schuh315b96b2020-12-11 21:21:12 -08001618 message_header_builder.add_boot_uuid(boot_uuid_offset);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001619
Austin Schuh969cd602021-01-03 00:09:45 -08001620 fbb.Finish(message_header_builder.Finish());
1621
1622 remote_timestamp_senders_[timestamped_message.channel_index]->Send(
1623 FlatbufferDetachedBuffer<RemoteMessage>(fbb.Release()),
Austin Schuh58646e22021-08-23 23:51:46 -07001624 timestamped_message.monotonic_timestamp_time,
1625 source_state->boot_count());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001626 }
1627
1628 return true;
1629}
1630
Austin Schuh969cd602021-01-03 00:09:45 -08001631LogReader::RemoteMessageSender::RemoteMessageSender(
1632 aos::Sender<message_bridge::RemoteMessage> sender, EventLoop *event_loop)
1633 : event_loop_(event_loop),
1634 sender_(std::move(sender)),
1635 timer_(event_loop->AddTimer([this]() { SendTimestamp(); })) {}
1636
1637void LogReader::RemoteMessageSender::ScheduleTimestamp() {
1638 if (remote_timestamps_.empty()) {
1639 CHECK_NOTNULL(timer_);
1640 timer_->Disable();
1641 scheduled_time_ = monotonic_clock::min_time;
1642 return;
1643 }
1644
1645 if (scheduled_time_ != remote_timestamps_.front().monotonic_timestamp_time) {
1646 CHECK_NOTNULL(timer_);
Philipp Schradera6712522023-07-05 20:25:11 -07001647 timer_->Schedule(remote_timestamps_.front().monotonic_timestamp_time);
Austin Schuh969cd602021-01-03 00:09:45 -08001648 scheduled_time_ = remote_timestamps_.front().monotonic_timestamp_time;
Austin Schuh3d94be02021-02-12 23:15:20 -08001649 CHECK_GE(scheduled_time_, event_loop_->monotonic_now())
1650 << event_loop_->node()->name()->string_view();
Austin Schuh969cd602021-01-03 00:09:45 -08001651 }
1652}
1653
1654void LogReader::RemoteMessageSender::Send(
1655 FlatbufferDetachedBuffer<RemoteMessage> remote_message,
Austin Schuh58646e22021-08-23 23:51:46 -07001656 BootTimestamp monotonic_timestamp_time, size_t source_boot_count) {
Austin Schuhc41d6a82021-07-16 14:49:23 -07001657 // There are 2 variants of logs.
1658 // 1) Logs without monotonic_timestamp_time
1659 // 2) Logs with monotonic_timestamp_time
1660 //
1661 // As of Jan 2021, we shouldn't have any more logs without
1662 // monotonic_timestamp_time. We don't have data locked up in those logs worth
1663 // the effort of saving.
1664 //
1665 // This gives us 3 cases, 2 of which are undistinguishable.
1666 // 1) Old log without monotonic_timestamp_time.
1667 // 2) New log with monotonic_timestamp_time where the timestamp was logged
1668 // remotely so we actually have monotonic_timestamp_time.
1669 // 3) New log, but the timestamp was logged on the node receiving the message
1670 // so there is no monotonic_timestamp_time.
1671 //
1672 // Our goal when replaying is to accurately reproduce the state of the world
1673 // present when logging. If a timestamp wasn't sent back across the network,
1674 // we shouldn't replay one back across the network.
1675 //
1676 // Given that we don't really care about 1, we can use the presence of the
1677 // timestamp to distinguish 2 and 3, and ignore 1. If we don't have a
1678 // monotonic_timestamp_time, this means the message was logged locally and
1679 // remote timestamps can be ignored.
Austin Schuh58646e22021-08-23 23:51:46 -07001680 if (monotonic_timestamp_time == BootTimestamp::min_time()) {
Austin Schuhc41d6a82021-07-16 14:49:23 -07001681 return;
Austin Schuh969cd602021-01-03 00:09:45 -08001682 }
Austin Schuhc41d6a82021-07-16 14:49:23 -07001683
Austin Schuh58646e22021-08-23 23:51:46 -07001684 CHECK_EQ(monotonic_timestamp_time.boot, source_boot_count);
1685
Austin Schuhc41d6a82021-07-16 14:49:23 -07001686 remote_timestamps_.emplace(
1687 std::upper_bound(
1688 remote_timestamps_.begin(), remote_timestamps_.end(),
Austin Schuh58646e22021-08-23 23:51:46 -07001689 monotonic_timestamp_time.time,
Austin Schuhc41d6a82021-07-16 14:49:23 -07001690 [](const aos::monotonic_clock::time_point monotonic_timestamp_time,
1691 const Timestamp &timestamp) {
1692 return monotonic_timestamp_time <
1693 timestamp.monotonic_timestamp_time;
1694 }),
Austin Schuh58646e22021-08-23 23:51:46 -07001695 std::move(remote_message), monotonic_timestamp_time.time);
Austin Schuhc41d6a82021-07-16 14:49:23 -07001696 ScheduleTimestamp();
Austin Schuh969cd602021-01-03 00:09:45 -08001697}
1698
1699void LogReader::RemoteMessageSender::SendTimestamp() {
Austin Schuh3d94be02021-02-12 23:15:20 -08001700 CHECK_EQ(event_loop_->context().monotonic_event_time, scheduled_time_)
1701 << event_loop_->node()->name()->string_view();
Austin Schuh969cd602021-01-03 00:09:45 -08001702 CHECK(!remote_timestamps_.empty());
1703
1704 // Send out all timestamps at the currently scheduled time.
1705 while (remote_timestamps_.front().monotonic_timestamp_time ==
1706 scheduled_time_) {
milind1f1dca32021-07-03 13:50:07 -07001707 CHECK_EQ(sender_.Send(std::move(remote_timestamps_.front().remote_message)),
1708 RawSender::Error::kOk);
Austin Schuh969cd602021-01-03 00:09:45 -08001709 remote_timestamps_.pop_front();
1710 if (remote_timestamps_.empty()) {
1711 break;
1712 }
1713 }
1714 scheduled_time_ = monotonic_clock::min_time;
1715
1716 ScheduleTimestamp();
1717}
1718
1719LogReader::RemoteMessageSender *LogReader::State::RemoteTimestampSender(
Austin Schuh61e973f2021-02-21 21:43:56 -08001720 const Channel *channel, const Connection *connection) {
1721 message_bridge::ChannelTimestampFinder finder(event_loop_);
1722 // Look at any pre-created channel/connection pairs.
1723 {
1724 auto it =
1725 channel_timestamp_loggers_.find(std::make_pair(channel, connection));
1726 if (it != channel_timestamp_loggers_.end()) {
1727 return it->second.get();
1728 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001729 }
1730
Austin Schuh61e973f2021-02-21 21:43:56 -08001731 // That failed, so resolve the RemoteMessage channel timestamps will be logged
1732 // to.
1733 const Channel *timestamp_channel = finder.ForChannel(channel, connection);
1734
1735 {
1736 // See if that has been created before. If so, cache it in
1737 // channel_timestamp_loggers_ and return.
1738 auto it = timestamp_loggers_.find(timestamp_channel);
1739 if (it != timestamp_loggers_.end()) {
1740 CHECK(channel_timestamp_loggers_
1741 .try_emplace(std::make_pair(channel, connection), it->second)
1742 .second);
1743 return it->second.get();
1744 }
1745 }
1746
1747 // Otherwise, make a sender, save it, and cache it.
1748 auto result = channel_timestamp_loggers_.try_emplace(
1749 std::make_pair(channel, connection),
1750 std::make_shared<RemoteMessageSender>(
1751 event_loop()->MakeSender<RemoteMessage>(
1752 timestamp_channel->name()->string_view()),
1753 event_loop()));
1754
1755 CHECK(timestamp_loggers_.try_emplace(timestamp_channel, result.first->second)
1756 .second);
1757 return result.first->second.get();
Austin Schuh858c9f32020-08-31 16:56:12 -07001758}
1759
Austin Schuhdda74ec2021-01-03 19:30:37 -08001760TimestampedMessage LogReader::State::PopOldest() {
Eric Schmiedebergb38477e2022-12-02 16:08:04 -07001761 // multithreaded
James Kuszmaula16a7912022-06-17 10:58:12 -07001762 if (message_queuer_.has_value()) {
1763 std::optional<TimestampedMessage> message = message_queuer_->Pop();
1764 CHECK(message.has_value()) << ": Unexpectedly ran out of messages.";
1765 message_queuer_->SetState(
1766 message.value().monotonic_event_time +
1767 std::chrono::duration_cast<std::chrono::nanoseconds>(
1768 std::chrono::duration<double>(FLAGS_threaded_look_ahead_seconds)));
Austin Schuh63097262023-08-16 17:04:29 -07001769 VLOG(1) << "Popped " << message.value()
1770 << configuration::CleanedChannelToString(
1771 event_loop_->configuration()->channels()->Get(
1772 factory_channel_index_[message->channel_index]));
James Kuszmaula16a7912022-06-17 10:58:12 -07001773 return message.value();
Eric Schmiedebergb38477e2022-12-02 16:08:04 -07001774 } else { // single threaded
James Kuszmaula16a7912022-06-17 10:58:12 -07001775 CHECK(timestamp_mapper_ != nullptr);
1776 TimestampedMessage *result_ptr = timestamp_mapper_->Front();
1777 CHECK(result_ptr != nullptr);
Austin Schuh858c9f32020-08-31 16:56:12 -07001778
James Kuszmaula16a7912022-06-17 10:58:12 -07001779 TimestampedMessage result = std::move(*result_ptr);
Austin Schuhe639ea12021-01-25 13:00:22 -08001780
Alexei Strots036d84e2023-05-03 16:05:12 -07001781 VLOG(2) << "Node '" << MaybeNodeName(event_loop_->node())
1782 << "': PopOldest Popping " << result.monotonic_event_time;
James Kuszmaula16a7912022-06-17 10:58:12 -07001783 timestamp_mapper_->PopFront();
Austin Schuh63097262023-08-16 17:04:29 -07001784 MaybeSeedSortedMessages();
Austin Schuh858c9f32020-08-31 16:56:12 -07001785
James Kuszmaula16a7912022-06-17 10:58:12 -07001786 CHECK_EQ(result.monotonic_event_time.boot, boot_count());
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001787
James Kuszmaula16a7912022-06-17 10:58:12 -07001788 VLOG(1) << "Popped " << result
1789 << configuration::CleanedChannelToString(
1790 event_loop_->configuration()->channels()->Get(
1791 factory_channel_index_[result.channel_index]));
1792 return result;
1793 }
Austin Schuh858c9f32020-08-31 16:56:12 -07001794}
1795
James Kuszmaula16a7912022-06-17 10:58:12 -07001796BootTimestamp LogReader::State::MultiThreadedOldestMessageTime() {
1797 if (!message_queuer_.has_value()) {
1798 return SingleThreadedOldestMessageTime();
1799 }
1800 std::optional<TimestampedMessage> message = message_queuer_->Peek();
1801 if (!message.has_value()) {
1802 return BootTimestamp::max_time();
1803 }
1804 if (message.value().monotonic_event_time.boot == boot_count()) {
1805 ObserveNextMessage(message.value().monotonic_event_time.time,
1806 message.value().realtime_event_time);
1807 }
1808 return message.value().monotonic_event_time;
1809}
1810
1811BootTimestamp LogReader::State::SingleThreadedOldestMessageTime() {
1812 CHECK(!message_queuer_.has_value())
1813 << "Cannot use SingleThreadedOldestMessageTime() once the queuer thread "
1814 "is created.";
Austin Schuhe639ea12021-01-25 13:00:22 -08001815 if (timestamp_mapper_ == nullptr) {
Austin Schuh58646e22021-08-23 23:51:46 -07001816 return BootTimestamp::max_time();
Austin Schuh287d43d2020-12-04 20:19:33 -08001817 }
Austin Schuhe639ea12021-01-25 13:00:22 -08001818 TimestampedMessage *result_ptr = timestamp_mapper_->Front();
1819 if (result_ptr == nullptr) {
Austin Schuh58646e22021-08-23 23:51:46 -07001820 return BootTimestamp::max_time();
Austin Schuhe639ea12021-01-25 13:00:22 -08001821 }
Alexei Strots036d84e2023-05-03 16:05:12 -07001822 VLOG(2) << "Node '" << MaybeNodeName(node()) << "': oldest message at "
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001823 << result_ptr->monotonic_event_time.time;
Austin Schuhe33c08d2022-02-03 18:15:21 -08001824 if (result_ptr->monotonic_event_time.boot == boot_count()) {
1825 ObserveNextMessage(result_ptr->monotonic_event_time.time,
1826 result_ptr->realtime_event_time);
1827 }
Austin Schuh58646e22021-08-23 23:51:46 -07001828 return result_ptr->monotonic_event_time;
Austin Schuh858c9f32020-08-31 16:56:12 -07001829}
1830
Austin Schuh63097262023-08-16 17:04:29 -07001831void LogReader::State::ReadTimestamps() {
Austin Schuh287d43d2020-12-04 20:19:33 -08001832 if (!timestamp_mapper_) return;
Austin Schuh63097262023-08-16 17:04:29 -07001833 timestamp_mapper_->QueueTimestamps();
1834
1835 // TODO(austin): Maybe make timestamp mapper do this so we don't need to clear
1836 // it out?
1837 //
1838 // If we don't clear the timestamp callback, we end up getting a callback both
1839 // when SplitTimestampBootMerger sees the timestamp, and when TimestampMapper
1840 // sees it.
1841 timestamp_mapper_->set_timestamp_callback([](TimestampedMessage *) {});
1842}
1843
1844void LogReader::State::MaybeSeedSortedMessages() {
1845 if (!timestamp_mapper_) return;
1846
1847 // The whole purpose of seeding is to make timestamp_mapper load timestamps.
1848 // So if we have already loaded them, skip seeding.
1849 if (timestamp_queue_strategy_ ==
1850 TimestampQueueStrategy::kQueueTimestampsAtStartup)
1851 return;
Austin Schuh858c9f32020-08-31 16:56:12 -07001852
Austin Schuhe639ea12021-01-25 13:00:22 -08001853 timestamp_mapper_->QueueFor(chrono::duration_cast<chrono::seconds>(
1854 chrono::duration<double>(FLAGS_time_estimation_buffer_seconds)));
Austin Schuh858c9f32020-08-31 16:56:12 -07001855}
1856
1857void LogReader::State::Deregister() {
Austin Schuh58646e22021-08-23 23:51:46 -07001858 if (started_ && !stopped_) {
Austin Schuhe33c08d2022-02-03 18:15:21 -08001859 NotifyLogfileEnd();
Austin Schuh58646e22021-08-23 23:51:46 -07001860 }
Austin Schuh858c9f32020-08-31 16:56:12 -07001861 for (size_t i = 0; i < channels_.size(); ++i) {
1862 channels_[i].reset();
1863 }
Austin Schuhe33c08d2022-02-03 18:15:21 -08001864 ClearTimeFlags();
Austin Schuh61e973f2021-02-21 21:43:56 -08001865 channel_timestamp_loggers_.clear();
1866 timestamp_loggers_.clear();
Austin Schuh858c9f32020-08-31 16:56:12 -07001867 event_loop_unique_ptr_.reset();
1868 event_loop_ = nullptr;
1869 timer_handler_ = nullptr;
1870 node_event_loop_factory_ = nullptr;
James Kuszmaula16a7912022-06-17 10:58:12 -07001871 timing_statistics_sender_ = Sender<timing::ReplayTiming>();
Austin Schuh858c9f32020-08-31 16:56:12 -07001872}
1873
Austin Schuhe33c08d2022-02-03 18:15:21 -08001874void LogReader::State::SetStartTimeFlag(realtime_clock::time_point start_time) {
1875 if (start_time != realtime_clock::min_time) {
1876 start_event_notifier_ = std::make_unique<EventNotifier>(
1877 event_loop_, [this]() { NotifyFlagStart(); }, "flag_start", start_time);
1878 }
1879}
1880
1881void LogReader::State::SetEndTimeFlag(realtime_clock::time_point end_time) {
1882 if (end_time != realtime_clock::max_time) {
1883 end_event_notifier_ = std::make_unique<EventNotifier>(
1884 event_loop_, [this]() { NotifyFlagEnd(); }, "flag_end", end_time);
1885 }
1886}
1887
1888void LogReader::State::ObserveNextMessage(
1889 monotonic_clock::time_point monotonic_event,
1890 realtime_clock::time_point realtime_event) {
1891 if (start_event_notifier_) {
1892 start_event_notifier_->ObserveNextMessage(monotonic_event, realtime_event);
1893 }
1894 if (end_event_notifier_) {
1895 end_event_notifier_->ObserveNextMessage(monotonic_event, realtime_event);
1896 }
1897}
1898
1899void LogReader::State::ClearTimeFlags() {
1900 start_event_notifier_.reset();
1901 end_event_notifier_.reset();
1902}
1903
1904void LogReader::State::NotifyLogfileStart() {
James Kuszmaul82c3b512023-07-08 20:25:41 -07001905 // If the start_event_notifier_ is set, that means that a realtime start time
1906 // was set manually; when the override is set, we want to delay any startup
1907 // handlers that would've happened before requested start time until that
1908 // start time.
Austin Schuhe33c08d2022-02-03 18:15:21 -08001909 if (start_event_notifier_) {
Philipp Schrader790cb542023-07-05 21:06:52 -07001910 // Only call OnStart() if the start time for this node
1911 // (realtime_start_time())
Austin Schuhe33c08d2022-02-03 18:15:21 -08001912 if (start_event_notifier_->realtime_event_time() >
1913 realtime_start_time(boot_count())) {
1914 VLOG(1) << "Skipping, " << start_event_notifier_->realtime_event_time()
1915 << " > " << realtime_start_time(boot_count());
1916 return;
1917 }
1918 }
1919 if (found_last_message_) {
1920 VLOG(1) << "Last message already found, bailing";
1921 return;
1922 }
1923 RunOnStart();
1924}
1925
1926void LogReader::State::NotifyFlagStart() {
James Kuszmaul82c3b512023-07-08 20:25:41 -07001927 // Should only be called if start_event_notifier_ has been set (which happens
1928 // as part of setting an explicit start time); only call the startup functions
1929 // that occurred *before* the start flag value.
Austin Schuhe33c08d2022-02-03 18:15:21 -08001930 if (start_event_notifier_->realtime_event_time() >=
1931 realtime_start_time(boot_count())) {
1932 RunOnStart();
1933 }
1934}
1935
1936void LogReader::State::NotifyLogfileEnd() {
James Kuszmaul82c3b512023-07-08 20:25:41 -07001937 // Don't execute the OnEnd handlers if the logfile was ended artifically
1938 // early.
Austin Schuhe33c08d2022-02-03 18:15:21 -08001939 if (found_last_message_) {
1940 return;
1941 }
1942
James Kuszmaul82c3b512023-07-08 20:25:41 -07001943 // Ensure that we only call OnEnd() if OnStart() was already called for this
1944 // boot (and don't call OnEnd() twice).
Austin Schuhe33c08d2022-02-03 18:15:21 -08001945 if (!stopped_ && started_) {
1946 RunOnEnd();
1947 }
1948}
1949
1950void LogReader::State::NotifyFlagEnd() {
James Kuszmaul82c3b512023-07-08 20:25:41 -07001951 // Ensure that we only call OnEnd() if OnStart() was already called for this
1952 // boot (and don't call OnEnd() twice).
Austin Schuhe33c08d2022-02-03 18:15:21 -08001953 if (!stopped_ && started_) {
1954 RunOnEnd();
1955 SetFoundLastMessage(true);
James Kuszmaulb11a1502022-07-01 16:02:25 -07001956 CHECK(notice_realtime_end_);
1957 notice_realtime_end_();
Austin Schuhe33c08d2022-02-03 18:15:21 -08001958 }
1959}
1960
James Kuszmaulc3f34d12022-08-15 15:57:55 -07001961void LogReader::State::MaybeSetClockOffset() {
James Kuszmaul09632422022-05-25 15:56:19 -07001962 if (node_event_loop_factory_ == nullptr) {
1963 // If not running with simulated event loop, set the monotonic clock
1964 // offset.
1965 clock_offset_ = event_loop()->monotonic_now() - monotonic_start_time(0);
1966
1967 if (start_event_notifier_) {
1968 start_event_notifier_->SetClockOffset(clock_offset_);
1969 }
1970 if (end_event_notifier_) {
1971 end_event_notifier_->SetClockOffset(clock_offset_);
1972 }
1973 }
1974}
1975
James Kuszmaulb67409b2022-06-20 16:25:03 -07001976void LogReader::SetRealtimeReplayRate(double replay_rate) {
1977 CHECK(event_loop_factory_ != nullptr)
1978 << ": Can't set replay rate without an event loop factory (have you "
1979 "called Register()?).";
1980 event_loop_factory_->SetRealtimeReplayRate(replay_rate);
1981}
1982
James Kuszmaulb11a1502022-07-01 16:02:25 -07001983void LogReader::NoticeRealtimeEnd() {
1984 CHECK_GE(live_nodes_with_realtime_time_end_, 1u);
1985 --live_nodes_with_realtime_time_end_;
1986 if (live_nodes_with_realtime_time_end_ == 0 && exit_on_finish() &&
1987 event_loop_factory_ != nullptr) {
1988 event_loop_factory_->Exit();
1989 }
1990}
1991
Eric Schmiedebergae00e732023-04-12 15:53:17 -06001992bool LogReader::AreStatesInitialized() const {
1993 for (const auto &state : states_) {
1994 if (state) {
1995 return true;
1996 }
1997 }
1998 return false;
1999}
2000
Austin Schuhe309d2a2019-11-29 13:25:21 -08002001} // namespace logger
2002} // namespace aos