blob: ddf9116628a1f28091e52700d25e165034bc4c2d [file] [log] [blame]
Austin Schuhb06f03b2021-02-17 22:00:37 -08001#ifndef AOS_EVENTS_LOGGING_LOG_READER_H_
2#define AOS_EVENTS_LOGGING_LOG_READER_H_
Austin Schuhe309d2a2019-11-29 13:25:21 -08003
Austin Schuh8bd96322020-02-13 21:18:22 -08004#include <chrono>
Austin Schuhe309d2a2019-11-29 13:25:21 -08005#include <deque>
James Kuszmaula16a7912022-06-17 10:58:12 -07006#include <queue>
James Kuszmaulc3f34d12022-08-15 15:57:55 -07007#include <string_view>
Austin Schuh2f8fd752020-09-01 22:38:28 -07008#include <tuple>
Austin Schuh6f3babe2020-01-26 20:34:50 -08009#include <vector>
Austin Schuhe309d2a2019-11-29 13:25:21 -080010
James Kuszmaulc3f34d12022-08-15 15:57:55 -070011#include "aos/condition.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080012#include "aos/events/event_loop.h"
Austin Schuhf6f9bf32020-10-11 14:37:43 -070013#include "aos/events/logging/logfile_sorting.h"
Austin Schuha36c8902019-12-30 18:07:15 -080014#include "aos/events/logging/logfile_utils.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080015#include "aos/events/logging/logger_generated.h"
James Kuszmaula16a7912022-06-17 10:58:12 -070016#include "aos/events/logging/replay_timing_generated.h"
James Kuszmaul09632422022-05-25 15:56:19 -070017#include "aos/events/shm_event_loop.h"
Austin Schuh92547522019-12-28 14:33:43 -080018#include "aos/events/simulated_event_loop.h"
James Kuszmaulc3f34d12022-08-15 15:57:55 -070019#include "aos/mutex/mutex.h"
Austin Schuh2f8fd752020-09-01 22:38:28 -070020#include "aos/network/message_bridge_server_generated.h"
Austin Schuh0ca1fd32020-12-18 22:53:05 -080021#include "aos/network/multinode_timestamp_filter.h"
Austin Schuh0de30f32020-12-06 12:44:28 -080022#include "aos/network/remote_message_generated.h"
Austin Schuh8bd96322020-02-13 21:18:22 -080023#include "aos/network/timestamp_filter.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080024#include "aos/time/time.h"
James Kuszmaula16a7912022-06-17 10:58:12 -070025#include "aos/util/threaded_queue.h"
James Kuszmaulc3f34d12022-08-15 15:57:55 -070026#include "aos/uuid.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080027#include "flatbuffers/flatbuffers.h"
28
29namespace aos {
30namespace logger {
31
Austin Schuhe33c08d2022-02-03 18:15:21 -080032class EventNotifier;
33
Eric Schmiedebergb38477e2022-12-02 16:08:04 -070034// Vector of pair of name and type of the channel
Sanjay Narayanan5ec00232022-07-08 15:21:30 -070035using ReplayChannels = std::vector<std::pair<std::string, std::string>>;
Eric Schmiedebergb38477e2022-12-02 16:08:04 -070036// Vector of channel indices
Naman Guptacf6d4422023-03-01 11:41:00 -080037using ReplayChannelIndices = std::vector<size_t>;
Eric Schmiedebergb38477e2022-12-02 16:08:04 -070038
Austin Schuh6f3babe2020-01-26 20:34:50 -080039// We end up with one of the following 3 log file types.
40//
41// Single node logged as the source node.
42// -> Replayed just on the source node.
43//
44// Forwarding timestamps only logged from the perspective of the destination
45// node.
46// -> Matched with data on source node and logged.
47//
48// Forwarding timestamps with data logged as the destination node.
49// -> Replayed just as the destination
50// -> Replayed as the source (Much harder, ordering is not defined)
51//
52// Duplicate data logged. -> CHECK that it matches and explode otherwise.
53//
54// This can be boiled down to a set of constraints and tools.
55//
56// 1) Forwarding timestamps and data need to be logged separately.
57// 2) Any forwarded data logged on the destination node needs to be logged
58// separately such that it can be sorted.
59//
60// 1) Log reader needs to be able to sort a list of log files.
61// 2) Log reader needs to be able to merge sorted lists of log files.
62// 3) Log reader needs to be able to match timestamps with messages.
63//
64// We also need to be able to generate multiple views of a log file depending on
65// the target.
James Kuszmaul298b4a22023-06-28 20:01:03 -070066//
67// In general, we aim to guarantee that if you are using the LogReader
68// "normally" you should be able to observe all the messages that existed on the
69// live system between the start time and the end of the logfile, and that
70// CHECK-failures will be generated if the LogReader cannot satisfy that
71// guarantee. There are currently a few deliberate exceptions to this:
72// * Any channel marked NOT_LOGGED in the configuration is known not to
73// have been logged and thus will be silently absent in log replay.
74// * If an incomplete set of log files is provided to the reader (e.g.,
75// only logs logged on a single node on a multi-node system), then
76// any *individual* channel as observed on a given node will be
77// consistent, but similarly to a NOT_LOGGED channel, some data may
78// not be available.
79// * At the end of a log, data for some channels/nodes may end before
80// others; during this time period, you may observe silently dropped
81// messages. This will be most obvious on uncleanly terminated logs or
82// when merging logfiles across nodes (as the logs on different nodes
83// will not finish at identical times).
Austin Schuh6f3babe2020-01-26 20:34:50 -080084
Austin Schuhe309d2a2019-11-29 13:25:21 -080085// Replays all the channels in the logfile to the event loop.
86class LogReader {
87 public:
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080088 // If you want to supply a new configuration that will be used for replay
89 // (e.g., to change message rates, or to populate an updated schema), then
90 // pass it in here. It must provide all the channels that the original logged
91 // config did.
Austin Schuh6f3babe2020-01-26 20:34:50 -080092 //
Eric Schmiedebergb38477e2022-12-02 16:08:04 -070093 // If certain messages should not be replayed, the replay_channels param can
94 // be used as an inclusive list of channels for messages to be replayed.
95 //
Austin Schuh287d43d2020-12-04 20:19:33 -080096 // The single file constructor calls SortParts internally.
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080097 LogReader(std::string_view filename,
Eric Schmiedebergb38477e2022-12-02 16:08:04 -070098 const Configuration *replay_configuration = nullptr,
99 const ReplayChannels *replay_channels = nullptr);
Austin Schuh287d43d2020-12-04 20:19:33 -0800100 LogReader(std::vector<LogFile> log_files,
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700101 const Configuration *replay_configuration = nullptr,
102 const ReplayChannels *replay_channels = nullptr);
James Kuszmaul7daef362019-12-31 18:28:17 -0800103 ~LogReader();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800104
Austin Schuh6331ef92020-01-07 18:28:09 -0800105 // Registers all the callbacks to send the log file data out on an event loop
106 // created in event_loop_factory. This also updates time to be at the start
107 // of the log file by running until the log file starts.
108 // Note: the configuration used in the factory should be configuration()
109 // below, but can be anything as long as the locations needed to send
110 // everything are available.
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800111 void Register(SimulatedEventLoopFactory *event_loop_factory);
Austin Schuhe33c08d2022-02-03 18:15:21 -0800112
Austin Schuh58646e22021-08-23 23:51:46 -0700113 // Registers all the callbacks to send the log file data out to an event loop
114 // factory. This does not start replaying or change the current distributed
115 // time of the factory. It does change the monotonic clocks to be right.
116 void RegisterWithoutStarting(SimulatedEventLoopFactory *event_loop_factory);
Austin Schuhe33c08d2022-02-03 18:15:21 -0800117 // Runs the log until the last start time. Register above is defined as:
118 // Register(...) {
119 // RegisterWithoutStarting
120 // StartAfterRegister
121 // }
122 // This should generally be considered as a stepping stone to convert from
123 // Register() to RegisterWithoutStarting() incrementally.
124 void StartAfterRegister(SimulatedEventLoopFactory *event_loop_factory);
125
Austin Schuh6331ef92020-01-07 18:28:09 -0800126 // Creates an SimulatedEventLoopFactory accessible via event_loop_factory(),
127 // and then calls Register.
128 void Register();
James Kuszmaul09632422022-05-25 15:56:19 -0700129
Austin Schuh6331ef92020-01-07 18:28:09 -0800130 // Registers callbacks for all the events after the log file starts. This is
131 // only useful when replaying live.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800132 void Register(EventLoop *event_loop);
Austin Schuh6331ef92020-01-07 18:28:09 -0800133
James Kuszmaula16a7912022-06-17 10:58:12 -0700134 // Sets a sender that should be used for tracking timing statistics. If not
135 // set, no statistics will be recorded.
136 void set_timing_accuracy_sender(
137 const Node *node, aos::Sender<timing::ReplayTiming> timing_sender) {
138 states_[configuration::GetNodeIndex(configuration(), node)]
139 ->set_timing_accuracy_sender(std::move(timing_sender));
140 }
141
Austin Schuh58646e22021-08-23 23:51:46 -0700142 // Called whenever a log file starts for a node.
143 void OnStart(std::function<void()> fn);
144 void OnStart(const Node *node, std::function<void()> fn);
145 // Called whenever a log file ends for a node.
146 void OnEnd(std::function<void()> fn);
147 void OnEnd(const Node *node, std::function<void()> fn);
148
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800149 // Unregisters the senders. You only need to call this if you separately
150 // supplied an event loop or event loop factory and the lifetimes are such
151 // that they need to be explicitly destroyed before the LogReader destructor
152 // gets called.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800153 void Deregister();
154
Austin Schuh0c297012020-09-16 18:41:59 -0700155 // Returns the configuration being used for replay from the log file.
156 // Note that this may be different from the configuration actually used for
157 // handling events. You should generally only use this to create a
158 // SimulatedEventLoopFactory, and then get the configuration from there for
159 // everything else.
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800160 const Configuration *logged_configuration() const;
Austin Schuh11d43732020-09-21 17:28:30 -0700161 // Returns the configuration being used for replay from the log file.
162 // Note that this may be different from the configuration actually used for
163 // handling events. You should generally only use this to create a
164 // SimulatedEventLoopFactory, and then get the configuration from there for
165 // everything else.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800166 // The pointer is invalidated whenever RemapLoggedChannel is called.
Austin Schuh15649d62019-12-28 16:36:38 -0800167 const Configuration *configuration() const;
168
Austin Schuh6f3babe2020-01-26 20:34:50 -0800169 // Returns the nodes that this log file was created on. This is a list of
Austin Schuh07676622021-01-21 18:59:17 -0800170 // pointers to a node in the nodes() list inside logged_configuration().
171 std::vector<const Node *> LoggedNodes() const;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800172
173 // Returns the starting timestamp for the log file.
James Kuszmaul298b4a22023-06-28 20:01:03 -0700174 // All logged channels for the specified node should be entirely available
175 // after the specified time (i.e., any message that was available on the node
176 // in question after the monotonic start time but before the logs end and
177 // whose channel is present in any of the provided logs will either be
178 // available in the log or will result in an internal CHECK-failure of the
179 // LogReader if it would be skipped).
Austin Schuh11d43732020-09-21 17:28:30 -0700180 monotonic_clock::time_point monotonic_start_time(
181 const Node *node = nullptr) const;
182 realtime_clock::time_point realtime_start_time(
183 const Node *node = nullptr) const;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800184
Austin Schuhe33c08d2022-02-03 18:15:21 -0800185 // Sets the start and end times to replay data until for all nodes. This
186 // overrides the --start_time and --end_time flags. The default is to replay
187 // all data.
188 void SetStartTime(std::string start_time);
189 void SetStartTime(realtime_clock::time_point start_time);
190 void SetEndTime(std::string end_time);
191 void SetEndTime(realtime_clock::time_point end_time);
192
James Kuszmaul53da7f32022-09-11 11:11:55 -0700193 // Enum to use for indicating how RemapLoggedChannel behaves when there is
194 // already a channel with the remapped name (e.g., as may happen when
195 // replaying a logfile that was itself generated from replay).
196 enum class RemapConflict {
197 // LOG(FATAL) on conflicts in remappings.
198 kDisallow,
199 // If we run into a conflict, attempt to remap the channel we would be
200 // overriding (and continue to do so if remapping *that* channel also
201 // generates a conflict).
202 // This will mean that if we repeatedly replay a log, we will end up
203 // stacking more and more /original's on the start of the oldest version
204 // of the channels.
205 kCascade
206 };
207
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800208 // Causes the logger to publish the provided channel on a different name so
209 // that replayed applications can publish on the proper channel name without
210 // interference. This operates on raw channel names, without any node or
211 // application specific mappings.
James Kuszmaul53da7f32022-09-11 11:11:55 -0700212 void RemapLoggedChannel(
213 std::string_view name, std::string_view type,
214 std::string_view add_prefix = "/original", std::string_view new_type = "",
215 RemapConflict conflict_handling = RemapConflict::kCascade);
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800216 template <typename T>
James Kuszmaul53da7f32022-09-11 11:11:55 -0700217 void RemapLoggedChannel(
218 std::string_view name, std::string_view add_prefix = "/original",
219 std::string_view new_type = "",
220 RemapConflict conflict_handling = RemapConflict::kCascade) {
221 RemapLoggedChannel(name, T::GetFullyQualifiedName(), add_prefix, new_type,
222 conflict_handling);
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800223 }
Austin Schuh01b4c352020-09-21 23:09:39 -0700224 // Remaps the provided channel, though this respects node mappings, and
225 // preserves them too. This makes it so if /aos -> /pi1/aos on one node,
226 // /original/aos -> /original/pi1/aos on the same node after renaming, just
Austin Schuh0de30f32020-12-06 12:44:28 -0800227 // like you would hope. If new_type is not empty, the new channel will use
228 // the provided type instead. This allows for renaming messages.
Austin Schuh01b4c352020-09-21 23:09:39 -0700229 //
230 // TODO(austin): If you have 2 nodes remapping something to the same channel,
231 // this doesn't handle that. No use cases exist yet for that, so it isn't
232 // being done yet.
James Kuszmaul53da7f32022-09-11 11:11:55 -0700233 void RemapLoggedChannel(
234 std::string_view name, std::string_view type, const Node *node,
235 std::string_view add_prefix = "/original", std::string_view new_type = "",
236 RemapConflict conflict_handling = RemapConflict::kCascade);
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700237 template <typename T>
James Kuszmaul53da7f32022-09-11 11:11:55 -0700238 void RemapLoggedChannel(
239 std::string_view name, const Node *node,
240 std::string_view add_prefix = "/original", std::string_view new_type = "",
241 RemapConflict conflict_handling = RemapConflict::kCascade) {
Austin Schuh0de30f32020-12-06 12:44:28 -0800242 RemapLoggedChannel(name, T::GetFullyQualifiedName(), node, add_prefix,
James Kuszmaul53da7f32022-09-11 11:11:55 -0700243 new_type, conflict_handling);
Austin Schuh01b4c352020-09-21 23:09:39 -0700244 }
245
Sanjay Narayanan5ec00232022-07-08 15:21:30 -0700246 // Similar to RemapLoggedChannel(), but lets you specify a name for the new
247 // channel without constraints. This is useful when an application has been
248 // updated to use new channels but you want to support replaying old logs. By
249 // default, this will not add any maps for the new channel. Use add_maps to
250 // specify any maps you'd like added.
251 void RenameLoggedChannel(std::string_view name, std::string_view type,
252 std::string_view new_name,
253 const std::vector<MapT> &add_maps = {});
254 template <typename T>
255 void RenameLoggedChannel(std::string_view name, std::string_view new_name,
256 const std::vector<MapT> &add_maps = {}) {
257 RenameLoggedChannel(name, T::GetFullyQualifiedName(), new_name, add_maps);
258 }
259 // The following overloads are more suitable for multi-node configurations,
260 // and let you rename a channel on a specific node.
261 void RenameLoggedChannel(std::string_view name, std::string_view type,
262 const Node *node, std::string_view new_name,
263 const std::vector<MapT> &add_maps = {});
264 template <typename T>
265 void RenameLoggedChannel(std::string_view name, const Node *node,
266 std::string_view new_name,
267 const std::vector<MapT> &add_maps = {}) {
268 RenameLoggedChannel(name, T::GetFullyQualifiedName(), node, new_name,
269 add_maps);
270 }
271
Austin Schuh01b4c352020-09-21 23:09:39 -0700272 template <typename T>
273 bool HasChannel(std::string_view name, const Node *node = nullptr) {
Sanjay Narayanan5ec00232022-07-08 15:21:30 -0700274 return HasChannel(name, T::GetFullyQualifiedName(), node);
275 }
276 bool HasChannel(std::string_view name, std::string_view type,
277 const Node *node) {
278 return configuration::GetChannel(logged_configuration(), name, type, "",
279 node, true) != nullptr;
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700280 }
281
Austin Schuh82529062021-12-08 12:09:52 -0800282 template <typename T>
283 void MaybeRemapLoggedChannel(std::string_view name,
284 const Node *node = nullptr) {
285 if (HasChannel<T>(name, node)) {
286 RemapLoggedChannel<T>(name, node);
287 }
288 }
Sanjay Narayanan5ec00232022-07-08 15:21:30 -0700289 template <typename T>
290 void MaybeRenameLoggedChannel(std::string_view name, const Node *node,
291 std::string_view new_name,
292 const std::vector<MapT> &add_maps = {}) {
293 if (HasChannel<T>(name, node)) {
294 RenameLoggedChannel<T>(name, node, new_name, add_maps);
295 }
296 }
Austin Schuh82529062021-12-08 12:09:52 -0800297
James Kuszmaul4f106fb2021-01-05 20:53:02 -0800298 // Returns true if the channel exists on the node and was logged.
299 template <typename T>
300 bool HasLoggedChannel(std::string_view name, const Node *node = nullptr) {
Austin Schuh5ee56872021-01-30 16:53:34 -0800301 const Channel *channel =
302 configuration::GetChannel(logged_configuration(), name,
303 T::GetFullyQualifiedName(), "", node, true);
James Kuszmaul4f106fb2021-01-05 20:53:02 -0800304 if (channel == nullptr) return false;
305 return channel->logger() != LoggerConfig::NOT_LOGGED;
306 }
307
Austin Schuh1c227352021-09-17 12:53:54 -0700308 // Returns a list of all the original channels from remapping.
309 std::vector<const Channel *> RemappedChannels() const;
310
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800311 SimulatedEventLoopFactory *event_loop_factory() {
312 return event_loop_factory_;
313 }
314
Austin Schuh0ca51f32020-12-25 21:51:45 -0800315 std::string_view name() const { return log_files_[0].name; }
Austin Schuh84dd1332023-05-03 13:09:47 -0700316 std::string_view log_event_uuid() const {
317 return log_files_[0].log_event_uuid;
318 }
Austin Schuh0c297012020-09-16 18:41:59 -0700319
James Kuszmaul71a81932020-12-15 21:08:01 -0800320 // Set whether to exit the SimulatedEventLoopFactory when we finish reading
321 // the logfile.
322 void set_exit_on_finish(bool exit_on_finish) {
323 exit_on_finish_ = exit_on_finish;
324 }
James Kuszmaulb11a1502022-07-01 16:02:25 -0700325 bool exit_on_finish() const { return exit_on_finish_; }
James Kuszmaul71a81932020-12-15 21:08:01 -0800326
James Kuszmaulb67409b2022-06-20 16:25:03 -0700327 // Sets the realtime replay rate. A value of 1.0 will cause the scheduler to
328 // try to play events in realtime. 0.5 will run at half speed. Use infinity
329 // (the default) to run as fast as possible. This can be changed during
330 // run-time.
331 // Only applies when running against a SimulatedEventLoopFactory.
332 void SetRealtimeReplayRate(double replay_rate);
333
Austin Schuhe309d2a2019-11-29 13:25:21 -0800334 private:
Austin Schuh58646e22021-08-23 23:51:46 -0700335 void Register(EventLoop *event_loop, const Node *node);
336
337 void RegisterDuringStartup(EventLoop *event_loop, const Node *node);
338
339 const Channel *RemapChannel(const EventLoop *event_loop, const Node *node,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800340 const Channel *channel);
341
Austin Schuhe309d2a2019-11-29 13:25:21 -0800342 // Queues at least max_out_of_order_duration_ messages into channels_.
343 void QueueMessages();
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800344 // Handle constructing a configuration with all the additional remapped
345 // channels from calls to RemapLoggedChannel.
346 void MakeRemappedConfig();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800347
Austin Schuh2f8fd752020-09-01 22:38:28 -0700348 // Returns the number of nodes.
349 size_t nodes_count() const {
350 return !configuration::MultiNode(logged_configuration())
351 ? 1u
352 : logged_configuration()->nodes()->size();
353 }
354
James Kuszmaulb11a1502022-07-01 16:02:25 -0700355 // Handles when an individual node hits the realtime end time, exitting the
356 // entire event loop once all nodes are stopped.
357 void NoticeRealtimeEnd();
358
Austin Schuh287d43d2020-12-04 20:19:33 -0800359 const std::vector<LogFile> log_files_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800360
Austin Schuh969cd602021-01-03 00:09:45 -0800361 // Class to manage sending RemoteMessages on the provided node after the
362 // correct delay.
Austin Schuh5ee56872021-01-30 16:53:34 -0800363 class RemoteMessageSender {
Austin Schuh969cd602021-01-03 00:09:45 -0800364 public:
365 RemoteMessageSender(aos::Sender<message_bridge::RemoteMessage> sender,
366 EventLoop *event_loop);
367 RemoteMessageSender(RemoteMessageSender const &) = delete;
368 RemoteMessageSender &operator=(RemoteMessageSender const &) = delete;
369
370 // Sends the provided message. If monotonic_timestamp_time is min_time,
371 // send it immediately.
372 void Send(
373 FlatbufferDetachedBuffer<message_bridge::RemoteMessage> remote_message,
Austin Schuh58646e22021-08-23 23:51:46 -0700374 BootTimestamp monotonic_timestamp_time, size_t source_boot_count);
Austin Schuh969cd602021-01-03 00:09:45 -0800375
376 private:
377 // Handles actually sending the timestamp if we were delayed.
378 void SendTimestamp();
379 // Handles scheduling the timer to send at the correct time.
380 void ScheduleTimestamp();
381
382 EventLoop *event_loop_;
383 aos::Sender<message_bridge::RemoteMessage> sender_;
384 aos::TimerHandler *timer_;
385
386 // Time we are scheduled for, or min_time if we aren't scheduled.
387 monotonic_clock::time_point scheduled_time_ = monotonic_clock::min_time;
388
389 struct Timestamp {
390 Timestamp(FlatbufferDetachedBuffer<message_bridge::RemoteMessage>
391 new_remote_message,
392 monotonic_clock::time_point new_monotonic_timestamp_time)
393 : remote_message(std::move(new_remote_message)),
394 monotonic_timestamp_time(new_monotonic_timestamp_time) {}
395 FlatbufferDetachedBuffer<message_bridge::RemoteMessage> remote_message;
396 monotonic_clock::time_point monotonic_timestamp_time;
397 };
398
399 // List of messages to send. The timer works through them and then disables
400 // itself automatically.
401 std::deque<Timestamp> remote_timestamps_;
402 };
403
Austin Schuh6f3babe2020-01-26 20:34:50 -0800404 // State per node.
Austin Schuh858c9f32020-08-31 16:56:12 -0700405 class State {
406 public:
James Kuszmaula16a7912022-06-17 10:58:12 -0700407 // Whether we should spin up a separate thread for buffering up messages.
408 // Only allowed in realtime replay--see comments on threading_ member for
409 // details.
410 enum class ThreadedBuffering { kYes, kNo };
James Kuszmaul09632422022-05-25 15:56:19 -0700411 State(std::unique_ptr<TimestampMapper> timestamp_mapper,
412 message_bridge::MultiNodeNoncausalOffsetEstimator *multinode_filters,
James Kuszmaulb11a1502022-07-01 16:02:25 -0700413 std::function<void()> notice_realtime_end, const Node *node,
414 ThreadedBuffering threading,
Naman Guptacf6d4422023-03-01 11:41:00 -0800415 std::unique_ptr<const ReplayChannelIndices> replay_channel_indices);
Austin Schuh287d43d2020-12-04 20:19:33 -0800416
417 // Connects up the timestamp mappers.
418 void AddPeer(State *peer);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800419
Austin Schuhe639ea12021-01-25 13:00:22 -0800420 TimestampMapper *timestamp_mapper() { return timestamp_mapper_.get(); }
421
Austin Schuhdda74ec2021-01-03 19:30:37 -0800422 // Returns the next sorted message with all the timestamps extracted and
423 // matched.
424 TimestampedMessage PopOldest();
Austin Schuh188eabe2020-12-29 23:41:13 -0800425
Austin Schuh858c9f32020-08-31 16:56:12 -0700426 // Returns the monotonic time of the oldest message.
James Kuszmaula16a7912022-06-17 10:58:12 -0700427 BootTimestamp SingleThreadedOldestMessageTime();
428 // Returns the monotonic time of the oldest message, handling querying the
429 // separate thread of ThreadedBuffering was set.
430 BootTimestamp MultiThreadedOldestMessageTime();
Austin Schuh58646e22021-08-23 23:51:46 -0700431
432 size_t boot_count() const {
433 // If we are replaying directly into an event loop, we can't reboot. So
434 // we will stay stuck on the 0th boot.
James Kuszmaul09632422022-05-25 15:56:19 -0700435 if (!node_event_loop_factory_) {
436 if (event_loop_ == nullptr) {
437 // If boot_count is being checked after startup for any of the
438 // non-primary nodes, then returning 0 may not be accurate (since
439 // remote nodes *can* reboot even if the EventLoop being played to
440 // can't).
441 CHECK(!started_);
442 CHECK(!stopped_);
443 }
444 return 0u;
445 }
Austin Schuh58646e22021-08-23 23:51:46 -0700446 return node_event_loop_factory_->boot_count();
447 }
Austin Schuh858c9f32020-08-31 16:56:12 -0700448
449 // Primes the queues inside State. Should be called before calling
450 // OldestMessageTime.
451 void SeedSortedMessages();
Austin Schuh8bd96322020-02-13 21:18:22 -0800452
Austin Schuh58646e22021-08-23 23:51:46 -0700453 void SetupStartupTimer() {
454 const monotonic_clock::time_point start_time =
455 monotonic_start_time(boot_count());
456 if (start_time == monotonic_clock::min_time) {
457 LOG(ERROR)
458 << "No start time, skipping, please figure out when this happens";
Austin Schuhe33c08d2022-02-03 18:15:21 -0800459 NotifyLogfileStart();
Austin Schuh58646e22021-08-23 23:51:46 -0700460 return;
461 }
James Kuszmaul09632422022-05-25 15:56:19 -0700462 if (node_event_loop_factory_) {
463 CHECK_GE(start_time + clock_offset(), event_loop_->monotonic_now());
464 }
465 startup_timer_->Setup(start_time + clock_offset());
Austin Schuh58646e22021-08-23 23:51:46 -0700466 }
467
468 void set_startup_timer(TimerHandler *timer_handler) {
469 startup_timer_ = timer_handler;
470 if (startup_timer_) {
471 if (event_loop_->node() != nullptr) {
472 startup_timer_->set_name(absl::StrCat(
473 event_loop_->node()->name()->string_view(), "_startup"));
474 } else {
475 startup_timer_->set_name("startup");
476 }
477 }
478 }
479
Austin Schuh858c9f32020-08-31 16:56:12 -0700480 // Returns the starting time for this node.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700481 monotonic_clock::time_point monotonic_start_time(size_t boot_count) const {
482 return timestamp_mapper_
483 ? timestamp_mapper_->monotonic_start_time(boot_count)
484 : monotonic_clock::min_time;
Austin Schuh858c9f32020-08-31 16:56:12 -0700485 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700486 realtime_clock::time_point realtime_start_time(size_t boot_count) const {
487 return timestamp_mapper_
488 ? timestamp_mapper_->realtime_start_time(boot_count)
489 : realtime_clock::min_time;
Austin Schuh858c9f32020-08-31 16:56:12 -0700490 }
491
492 // Sets the node event loop factory for replaying into a
493 // SimulatedEventLoopFactory. Returns the EventLoop to use.
Austin Schuh60e77942022-05-16 17:48:24 -0700494 void SetNodeEventLoopFactory(NodeEventLoopFactory *node_event_loop_factory,
495 SimulatedEventLoopFactory *event_loop_factory);
Austin Schuh858c9f32020-08-31 16:56:12 -0700496
497 // Sets and gets the event loop to use.
498 void set_event_loop(EventLoop *event_loop) { event_loop_ = event_loop; }
499 EventLoop *event_loop() { return event_loop_; }
500
Austin Schuh58646e22021-08-23 23:51:46 -0700501 const Node *node() const { return node_; }
502
503 void Register(EventLoop *event_loop);
504
505 void OnStart(std::function<void()> fn);
506 void OnEnd(std::function<void()> fn);
507
Austin Schuh858c9f32020-08-31 16:56:12 -0700508 // Sets the current realtime offset from the monotonic clock for this node
509 // (if we are on a simulated event loop).
510 void SetRealtimeOffset(monotonic_clock::time_point monotonic_time,
511 realtime_clock::time_point realtime_time) {
512 if (node_event_loop_factory_ != nullptr) {
513 node_event_loop_factory_->SetRealtimeOffset(monotonic_time,
514 realtime_time);
515 }
516 }
517
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700518 // Returns the MessageHeader sender to log delivery timestamps to for the
519 // provided remote node.
Austin Schuh61e973f2021-02-21 21:43:56 -0800520 RemoteMessageSender *RemoteTimestampSender(const Channel *channel,
521 const Connection *connection);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700522
Austin Schuh858c9f32020-08-31 16:56:12 -0700523 // Converts a timestamp from the monotonic clock on this node to the
524 // distributed clock.
525 distributed_clock::time_point ToDistributedClock(
526 monotonic_clock::time_point time) {
James Kuszmaul09632422022-05-25 15:56:19 -0700527 CHECK(node_event_loop_factory_);
Austin Schuh858c9f32020-08-31 16:56:12 -0700528 return node_event_loop_factory_->ToDistributedClock(time);
529 }
530
Austin Schuh858c9f32020-08-31 16:56:12 -0700531 // Returns the current time on the remote node which sends messages on
532 // channel_index.
Austin Schuh58646e22021-08-23 23:51:46 -0700533 BootTimestamp monotonic_remote_now(size_t channel_index) {
534 State *s = channel_source_state_[channel_index];
535 return BootTimestamp{
536 .boot = s->boot_count(),
537 .time = s->node_event_loop_factory_->monotonic_now()};
Austin Schuh858c9f32020-08-31 16:56:12 -0700538 }
539
Austin Schuh5ee56872021-01-30 16:53:34 -0800540 // Returns the start time of the remote for the provided channel.
541 monotonic_clock::time_point monotonic_remote_start_time(
Austin Schuh58646e22021-08-23 23:51:46 -0700542 size_t boot_count, size_t channel_index) {
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700543 return channel_source_state_[channel_index]->monotonic_start_time(
544 boot_count);
Austin Schuh5ee56872021-01-30 16:53:34 -0800545 }
546
Austin Schuh58646e22021-08-23 23:51:46 -0700547 void DestroyEventLoop() { event_loop_unique_ptr_.reset(); }
548
549 EventLoop *MakeEventLoop() {
550 CHECK(!event_loop_unique_ptr_);
James Kuszmaul890c2492022-04-06 14:59:31 -0700551 // TODO(james): Enable exclusive senders on LogReader to allow us to
552 // ensure we are remapping channels correctly.
553 event_loop_unique_ptr_ = node_event_loop_factory_->MakeEventLoop(
554 "log_reader", {NodeEventLoopFactory::CheckSentTooFast::kNo,
James Kuszmaul94ca5132022-07-19 09:11:08 -0700555 NodeEventLoopFactory::ExclusiveSenders::kYes,
556 NonExclusiveChannels()});
Austin Schuh58646e22021-08-23 23:51:46 -0700557 return event_loop_unique_ptr_.get();
558 }
559
Austin Schuh2f8fd752020-09-01 22:38:28 -0700560 distributed_clock::time_point RemoteToDistributedClock(
561 size_t channel_index, monotonic_clock::time_point time) {
James Kuszmaul09632422022-05-25 15:56:19 -0700562 CHECK(node_event_loop_factory_);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700563 return channel_source_state_[channel_index]
564 ->node_event_loop_factory_->ToDistributedClock(time);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700565 }
566
567 const Node *remote_node(size_t channel_index) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700568 return channel_source_state_[channel_index]
569 ->node_event_loop_factory_->node();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700570 }
571
Stephan Pleines559fa6c2022-01-06 17:23:51 -0800572 monotonic_clock::time_point monotonic_now() const {
Alexei Strotsb8c3a702023-04-19 21:38:25 -0700573 CHECK_NOTNULL(event_loop_);
James Kuszmaul09632422022-05-25 15:56:19 -0700574 return event_loop_->monotonic_now();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700575 }
576
Austin Schuh858c9f32020-08-31 16:56:12 -0700577 // Sets the number of channels.
578 void SetChannelCount(size_t count);
579
580 // Sets the sender, filter, and target factory for a channel.
Austin Schuh969cd602021-01-03 00:09:45 -0800581 void SetChannel(size_t logged_channel_index, size_t factory_channel_index,
582 std::unique_ptr<RawSender> sender,
583 message_bridge::NoncausalOffsetEstimator *filter,
Austin Schuh58646e22021-08-23 23:51:46 -0700584 bool is_forwarded, State *source_state);
585
586 void SetRemoteTimestampSender(size_t logged_channel_index,
587 RemoteMessageSender *remote_timestamp_sender);
588
589 void RunOnStart();
590 void RunOnEnd();
Austin Schuh858c9f32020-08-31 16:56:12 -0700591
Austin Schuhe33c08d2022-02-03 18:15:21 -0800592 // Handles a logfile start event to potentially call the OnStart callbacks.
593 void NotifyLogfileStart();
594 // Handles a start time flag start event to potentially call the OnStart
595 // callbacks.
596 void NotifyFlagStart();
597
598 // Handles a logfile end event to potentially call the OnEnd callbacks.
599 void NotifyLogfileEnd();
600 // Handles a end time flag start event to potentially call the OnEnd
601 // callbacks.
602 void NotifyFlagEnd();
603
Austin Schuh858c9f32020-08-31 16:56:12 -0700604 // Unregisters everything so we can destory the event loop.
Austin Schuh58646e22021-08-23 23:51:46 -0700605 // TODO(austin): Is this needed? OnShutdown should be able to serve this
606 // need.
Austin Schuh858c9f32020-08-31 16:56:12 -0700607 void Deregister();
608
609 // Sets the current TimerHandle for the replay callback.
610 void set_timer_handler(TimerHandler *timer_handler) {
611 timer_handler_ = timer_handler;
Austin Schuh58646e22021-08-23 23:51:46 -0700612 if (timer_handler_) {
613 if (event_loop_->node() != nullptr) {
614 timer_handler_->set_name(absl::StrCat(
615 event_loop_->node()->name()->string_view(), "_main"));
616 } else {
617 timer_handler_->set_name("main");
618 }
619 }
Austin Schuh858c9f32020-08-31 16:56:12 -0700620 }
621
Austin Schuhe33c08d2022-02-03 18:15:21 -0800622 // Creates and registers the --start_time and --end_time event callbacks.
623 void SetStartTimeFlag(realtime_clock::time_point start_time);
624 void SetEndTimeFlag(realtime_clock::time_point end_time);
625
626 // Notices the next message to update the start/end time callbacks.
627 void ObserveNextMessage(monotonic_clock::time_point monotonic_event,
628 realtime_clock::time_point realtime_event);
629
630 // Clears the start and end time flag handlers so we can delete the event
631 // loop.
632 void ClearTimeFlags();
633
Austin Schuh858c9f32020-08-31 16:56:12 -0700634 // Sets the next wakeup time on the replay callback.
635 void Setup(monotonic_clock::time_point next_time) {
James Kuszmaul8866e642022-06-10 16:00:36 -0700636 timer_handler_->Setup(
637 std::max(monotonic_now(), next_time + clock_offset()));
Austin Schuh858c9f32020-08-31 16:56:12 -0700638 }
639
640 // Sends a buffer on the provided channel index.
Austin Schuh287d43d2020-12-04 20:19:33 -0800641 bool Send(const TimestampedMessage &timestamped_message);
Austin Schuh858c9f32020-08-31 16:56:12 -0700642
James Kuszmaulc3f34d12022-08-15 15:57:55 -0700643 void MaybeSetClockOffset();
James Kuszmaul09632422022-05-25 15:56:19 -0700644 std::chrono::nanoseconds clock_offset() const { return clock_offset_; }
645
Austin Schuh858c9f32020-08-31 16:56:12 -0700646 // Returns a debug string for the channel merger.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700647 std::string DebugString() const {
Austin Schuh287d43d2020-12-04 20:19:33 -0800648 if (!timestamp_mapper_) {
Austin Schuhe639ea12021-01-25 13:00:22 -0800649 return "";
Austin Schuh287d43d2020-12-04 20:19:33 -0800650 }
Austin Schuhe639ea12021-01-25 13:00:22 -0800651 return timestamp_mapper_->DebugString();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700652 }
Austin Schuh858c9f32020-08-31 16:56:12 -0700653
Austin Schuh58646e22021-08-23 23:51:46 -0700654 void ClearRemoteTimestampSenders() {
655 channel_timestamp_loggers_.clear();
656 timestamp_loggers_.clear();
657 }
658
Austin Schuhbd5f74a2021-11-11 20:55:38 -0800659 void SetFoundLastMessage(bool val) {
660 found_last_message_ = val;
661 last_message_.resize(factory_channel_index_.size(), false);
662 }
663 bool found_last_message() const { return found_last_message_; }
664
665 void set_last_message(size_t channel_index) {
666 CHECK_LT(channel_index, last_message_.size());
667 last_message_[channel_index] = true;
668 }
669
670 bool last_message(size_t channel_index) {
671 CHECK_LT(channel_index, last_message_.size());
672 return last_message_[channel_index];
673 }
674
James Kuszmaula16a7912022-06-17 10:58:12 -0700675 void set_timing_accuracy_sender(
676 aos::Sender<timing::ReplayTiming> timing_sender) {
677 timing_statistics_sender_ = std::move(timing_sender);
678 OnEnd([this]() { SendMessageTimings(); });
679 }
680
681 // If running with ThreadedBuffering::kYes, will start the processing thread
682 // and queue up messages until the specified time. No-op of
683 // ThreadedBuffering::kNo is set. Should only be called once.
684 void QueueThreadUntil(BootTimestamp time);
685
Austin Schuh858c9f32020-08-31 16:56:12 -0700686 private:
James Kuszmaulc3f34d12022-08-15 15:57:55 -0700687 void TrackMessageSendTiming(const RawSender &sender,
688 monotonic_clock::time_point expected_send_time);
James Kuszmaula16a7912022-06-17 10:58:12 -0700689 void SendMessageTimings();
Austin Schuh858c9f32020-08-31 16:56:12 -0700690 // Log file.
Austin Schuh287d43d2020-12-04 20:19:33 -0800691 std::unique_ptr<TimestampMapper> timestamp_mapper_;
Austin Schuh858c9f32020-08-31 16:56:12 -0700692
Austin Schuh858c9f32020-08-31 16:56:12 -0700693 // Senders.
694 std::vector<std::unique_ptr<RawSender>> channels_;
Austin Schuh969cd602021-01-03 00:09:45 -0800695 std::vector<RemoteMessageSender *> remote_timestamp_senders_;
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700696 // The mapping from logged channel index to sent channel index. Needed for
697 // sending out MessageHeaders.
698 std::vector<int> factory_channel_index_;
699
Austin Schuh9942bae2021-01-07 22:06:44 -0800700 struct ContiguousSentTimestamp {
701 // Most timestamps make it through the network, so it saves a ton of
702 // memory and CPU to store the start and end, and search for valid ranges.
703 // For one of the logs I looked at, we had 2 ranges for 4 days.
704 //
705 // Save monotonic times as well to help if a queue index ever wraps. Odds
706 // are very low, but doesn't hurt.
707 //
708 // The starting time and matching queue index.
709 monotonic_clock::time_point starting_monotonic_event_time =
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700710 monotonic_clock::min_time;
Austin Schuh9942bae2021-01-07 22:06:44 -0800711 uint32_t starting_queue_index = 0xffffffff;
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700712
Austin Schuh9942bae2021-01-07 22:06:44 -0800713 // Ending time and queue index.
714 monotonic_clock::time_point ending_monotonic_event_time =
715 monotonic_clock::max_time;
716 uint32_t ending_queue_index = 0xffffffff;
717
718 // The queue index that the first message was *actually* sent with. The
719 // queue indices are assumed to be contiguous through this range.
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700720 uint32_t actual_queue_index = 0xffffffff;
721 };
722
James Kuszmaul94ca5132022-07-19 09:11:08 -0700723 // Returns a list of channels which LogReader will send on but which may
724 // *also* get sent on by other applications in replay.
725 std::vector<
726 std::pair<const aos::Channel *, NodeEventLoopFactory::ExclusiveSenders>>
727 NonExclusiveChannels();
728
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700729 // Stores all the timestamps that have been sent on this channel. This is
730 // only done for channels which are forwarded and on the node which
Austin Schuh9942bae2021-01-07 22:06:44 -0800731 // initially sends the message. Compress using ranges and offsets.
732 std::vector<std::unique_ptr<std::vector<ContiguousSentTimestamp>>>
733 queue_index_map_;
Austin Schuh858c9f32020-08-31 16:56:12 -0700734
735 // Factory (if we are in sim) that this loop was created on.
736 NodeEventLoopFactory *node_event_loop_factory_ = nullptr;
Austin Schuhe33c08d2022-02-03 18:15:21 -0800737 SimulatedEventLoopFactory *event_loop_factory_ = nullptr;
738
James Kuszmaulb11a1502022-07-01 16:02:25 -0700739 // Callback for when this node hits its realtime end time.
740 std::function<void()> notice_realtime_end_;
741
Austin Schuh858c9f32020-08-31 16:56:12 -0700742 std::unique_ptr<EventLoop> event_loop_unique_ptr_;
743 // Event loop.
Austin Schuh58646e22021-08-23 23:51:46 -0700744 const Node *node_ = nullptr;
Austin Schuh858c9f32020-08-31 16:56:12 -0700745 EventLoop *event_loop_ = nullptr;
746 // And timer used to send messages.
Austin Schuh58646e22021-08-23 23:51:46 -0700747 TimerHandler *timer_handler_ = nullptr;
748 TimerHandler *startup_timer_ = nullptr;
Austin Schuh858c9f32020-08-31 16:56:12 -0700749
Austin Schuhe33c08d2022-02-03 18:15:21 -0800750 std::unique_ptr<EventNotifier> start_event_notifier_;
751 std::unique_ptr<EventNotifier> end_event_notifier_;
752
Austin Schuh8bd96322020-02-13 21:18:22 -0800753 // Filters (or nullptr if it isn't a forwarded channel) for each channel.
754 // This corresponds to the object which is shared among all the channels
755 // going between 2 nodes. The second element in the tuple indicates if this
756 // is the primary direction or not.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700757 std::vector<message_bridge::NoncausalOffsetEstimator *> filters_;
James Kuszmaul09632422022-05-25 15:56:19 -0700758 message_bridge::MultiNodeNoncausalOffsetEstimator *multinode_filters_;
Austin Schuh8bd96322020-02-13 21:18:22 -0800759
Austin Schuh84dd1332023-05-03 13:09:47 -0700760 // List of States (or nullptr if it isn't a forwarded channel) which
761 // correspond to the originating node.
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700762 std::vector<State *> channel_source_state_;
763
Austin Schuh61e973f2021-02-21 21:43:56 -0800764 // This is a cache for channel, connection mapping to the corresponding
765 // sender.
766 absl::btree_map<std::pair<const Channel *, const Connection *>,
767 std::shared_ptr<RemoteMessageSender>>
768 channel_timestamp_loggers_;
769
770 // Mapping from resolved RemoteMessage channel to RemoteMessage sender. This
771 // is the channel that timestamps are published to.
772 absl::btree_map<const Channel *, std::shared_ptr<RemoteMessageSender>>
773 timestamp_loggers_;
Austin Schuh58646e22021-08-23 23:51:46 -0700774
James Kuszmaul09632422022-05-25 15:56:19 -0700775 // Time offset between the log's monotonic clock and the current event
776 // loop's monotonic clock. Useful when replaying logs with non-simulated
777 // event loops.
778 std::chrono::nanoseconds clock_offset_{0};
779
Austin Schuh58646e22021-08-23 23:51:46 -0700780 std::vector<std::function<void()>> on_starts_;
781 std::vector<std::function<void()>> on_ends_;
782
James Kuszmaula16a7912022-06-17 10:58:12 -0700783 std::atomic<bool> stopped_ = false;
784 std::atomic<bool> started_ = false;
Austin Schuhbd5f74a2021-11-11 20:55:38 -0800785
786 bool found_last_message_ = false;
787 std::vector<bool> last_message_;
James Kuszmaula16a7912022-06-17 10:58:12 -0700788
789 std::vector<timing::MessageTimingT> send_timings_;
790 aos::Sender<timing::ReplayTiming> timing_statistics_sender_;
791
792 // Protects access to any internal state after Run() is called. Designed
793 // assuming that only one node is actually executing in replay.
794 // Threading design:
795 // * The worker passed to message_queuer_ has full ownership over all
796 // the log-reading code, timestamp filters, last_queued_message_, etc.
797 // * The main thread should only have exclusive access to the replay
798 // event loop and associated features (mainly senders).
799 // It will pop an item out of the queue (which does maintain a shared_ptr
800 // reference which may also be being used by the message_queuer_ thread,
801 // but having shared_ptr's accessing the same memory from
802 // separate threads is permissible).
803 // Enabling this in simulation is currently infeasible due to a lack of
804 // synchronization in the MultiNodeNoncausalOffsetEstimator. Essentially,
805 // when the message_queuer_ thread attempts to read/pop messages from the
806 // timestamp_mapper_, it will end up calling callbacks that update the
807 // internal state of the MultiNodeNoncausalOffsetEstimator. Simultaneously,
808 // the event scheduler that is running in the main thread to orchestrate the
809 // simulation will be querying the estimator to know what the clocks on the
810 // various nodes are at, leading to potential issues.
811 ThreadedBuffering threading_;
812 std::optional<BootTimestamp> last_queued_message_;
813 std::optional<util::ThreadedQueue<TimestampedMessage, BootTimestamp>>
814 message_queuer_;
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700815
816 // If a ReplayChannels was passed to LogReader, this will hold the
817 // indices of the channels to replay for the Node represented by
818 // the instance of LogReader::State.
Naman Guptacf6d4422023-03-01 11:41:00 -0800819 std::unique_ptr<const ReplayChannelIndices> replay_channel_indices_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800820 };
821
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700822 // If a ReplayChannels was passed to LogReader then creates a
Naman Guptacf6d4422023-03-01 11:41:00 -0800823 // ReplayChannelIndices for the given node. Otherwise, returns a nullptr.
824 std::unique_ptr<const ReplayChannelIndices> MaybeMakeReplayChannelIndices(
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700825 const Node *node);
826
Austin Schuh8bd96322020-02-13 21:18:22 -0800827 // Node index -> State.
828 std::vector<std::unique_ptr<State>> states_;
829
830 // Creates the requested filter if it doesn't exist, regardless of whether
831 // these nodes can actually communicate directly. The second return value
832 // reports if this is the primary direction or not.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700833 message_bridge::NoncausalOffsetEstimator *GetFilter(const Node *node_a,
834 const Node *node_b);
Austin Schuh8bd96322020-02-13 21:18:22 -0800835
Austin Schuh8bd96322020-02-13 21:18:22 -0800836 // List of filters for a connection. The pointer to the first node will be
837 // less than the second node.
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800838 std::unique_ptr<message_bridge::MultiNodeNoncausalOffsetEstimator> filters_;
Austin Schuh8bd96322020-02-13 21:18:22 -0800839
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800840 std::unique_ptr<FlatbufferDetachedBuffer<Configuration>>
841 remapped_configuration_buffer_;
842
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800843 std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_unique_ptr_;
844 SimulatedEventLoopFactory *event_loop_factory_ = nullptr;
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800845
846 // Map of channel indices to new name. The channel index will be an index into
847 // logged_configuration(), and the string key will be the name of the channel
848 // to send on instead of the logged channel name.
Austin Schuh0de30f32020-12-06 12:44:28 -0800849 struct RemappedChannel {
850 std::string remapped_name;
851 std::string new_type;
852 };
853 std::map<size_t, RemappedChannel> remapped_channels_;
Austin Schuh01b4c352020-09-21 23:09:39 -0700854 std::vector<MapT> maps_;
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800855
Austin Schuh6f3babe2020-01-26 20:34:50 -0800856 // Number of nodes which still have data to send. This is used to figure out
857 // when to exit.
858 size_t live_nodes_ = 0;
859
James Kuszmaulb11a1502022-07-01 16:02:25 -0700860 // Similar counter to live_nodes_, but for tracking which individual nodes are
861 // running and have yet to hit the realtime end time, if any.
862 size_t live_nodes_with_realtime_time_end_ = 0;
863
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800864 const Configuration *remapped_configuration_ = nullptr;
865 const Configuration *replay_configuration_ = nullptr;
Austin Schuhcde938c2020-02-02 17:30:07 -0800866
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700867 // If a ReplayChannels was passed to LogReader, this will hold the
868 // name and type of channels to replay which is used when creating States.
869 const ReplayChannels *replay_channels_ = nullptr;
870
Austin Schuhcde938c2020-02-02 17:30:07 -0800871 // If true, the replay timer will ignore any missing data. This is used
872 // during startup when we are bootstrapping everything and trying to get to
873 // the start of all the log files.
874 bool ignore_missing_data_ = false;
James Kuszmaul71a81932020-12-15 21:08:01 -0800875
876 // Whether to exit the SimulatedEventLoop when we finish reading the logs.
877 bool exit_on_finish_ = true;
Austin Schuhe33c08d2022-02-03 18:15:21 -0800878
879 realtime_clock::time_point start_time_ = realtime_clock::min_time;
880 realtime_clock::time_point end_time_ = realtime_clock::max_time;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800881};
882
883} // namespace logger
884} // namespace aos
885
Austin Schuhb06f03b2021-02-17 22:00:37 -0800886#endif // AOS_EVENTS_LOGGING_LOG_READER_H_