blob: 4e38feb3deac9620ffada0592591ebb794df82c4 [file] [log] [blame]
Austin Schuha36c8902019-12-30 18:07:15 -08001#ifndef AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_
2#define AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_
3
4#include <sys/uio.h>
5
Austin Schuh97789fc2020-08-01 14:42:45 -07006#include <chrono>
Austin Schuh05b70472020-01-01 17:11:17 -08007#include <deque>
Austin Schuh97789fc2020-08-01 14:42:45 -07008#include <limits>
9#include <memory>
Austin Schuh05b70472020-01-01 17:11:17 -080010#include <optional>
Austin Schuhfa895892020-01-07 20:07:41 -080011#include <string>
Austin Schuha36c8902019-12-30 18:07:15 -080012#include <string_view>
Brian Silverman98360e22020-04-28 16:51:20 -070013#include <tuple>
Austin Schuh97789fc2020-08-01 14:42:45 -070014#include <utility>
Austin Schuha36c8902019-12-30 18:07:15 -080015#include <vector>
16
Austin Schuh4b5c22a2020-11-30 22:58:43 -080017#include "absl/container/btree_set.h"
Austin Schuh05b70472020-01-01 17:11:17 -080018#include "absl/types/span.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070019#include "aos/containers/resizeable_buffer.h"
Austin Schuha36c8902019-12-30 18:07:15 -080020#include "aos/events/event_loop.h"
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070021#include "aos/events/logging/boot_timestamp.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070022#include "aos/events/logging/buffer_encoder.h"
Austin Schuhc41603c2020-10-11 16:17:37 -070023#include "aos/events/logging/logfile_sorting.h"
Austin Schuha36c8902019-12-30 18:07:15 -080024#include "aos/events/logging/logger_generated.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070025#include "aos/flatbuffers.h"
Austin Schuhf2d0e682022-10-16 14:20:58 -070026#include "aos/network/remote_message_generated.h"
Austin Schuha36c8902019-12-30 18:07:15 -080027#include "flatbuffers/flatbuffers.h"
28
Brian Silvermanf51499a2020-09-21 12:49:08 -070029namespace aos::logger {
Austin Schuha36c8902019-12-30 18:07:15 -080030
31enum class LogType : uint8_t {
32 // The message originated on this node and should be logged here.
33 kLogMessage,
34 // The message originated on another node, but only the delivery times are
35 // logged here.
36 kLogDeliveryTimeOnly,
37 // The message originated on another node. Log it and the delivery times
38 // together. The message_gateway is responsible for logging any messages
39 // which didn't get delivered.
Austin Schuh6f3babe2020-01-26 20:34:50 -080040 kLogMessageAndDeliveryTime,
41 // The message originated on the other node and should be logged on this node.
42 kLogRemoteMessage
Austin Schuha36c8902019-12-30 18:07:15 -080043};
44
Austin Schuha36c8902019-12-30 18:07:15 -080045// This class manages efficiently writing a sequence of detached buffers to a
Brian Silvermanf51499a2020-09-21 12:49:08 -070046// file. It encodes them, queues them up, and batches the write operation.
Austin Schuha36c8902019-12-30 18:07:15 -080047class DetachedBufferWriter {
48 public:
Brian Silvermana9f2ec92020-10-06 18:00:53 -070049 // Marker struct for one of our constructor overloads.
50 struct already_out_of_space_t {};
51
Brian Silvermanf51499a2020-09-21 12:49:08 -070052 DetachedBufferWriter(std::string_view filename,
Austin Schuh48d10d62022-10-16 22:19:23 -070053 std::unique_ptr<DataEncoder> encoder);
Brian Silvermana9f2ec92020-10-06 18:00:53 -070054 // Creates a dummy instance which won't even open a file. It will act as if
55 // opening the file ran out of space immediately.
56 DetachedBufferWriter(already_out_of_space_t) : ran_out_of_space_(true) {}
Austin Schuh2f8fd752020-09-01 22:38:28 -070057 DetachedBufferWriter(DetachedBufferWriter &&other);
58 DetachedBufferWriter(const DetachedBufferWriter &) = delete;
59
Austin Schuha36c8902019-12-30 18:07:15 -080060 ~DetachedBufferWriter();
61
Austin Schuh2f8fd752020-09-01 22:38:28 -070062 DetachedBufferWriter &operator=(DetachedBufferWriter &&other);
Brian Silverman98360e22020-04-28 16:51:20 -070063 DetachedBufferWriter &operator=(const DetachedBufferWriter &) = delete;
64
Austin Schuh6f3babe2020-01-26 20:34:50 -080065 std::string_view filename() const { return filename_; }
66
Brian Silvermana9f2ec92020-10-06 18:00:53 -070067 // This will be true until Close() is called, unless the file couldn't be
68 // created due to running out of space.
69 bool is_open() const { return fd_ != -1; }
70
Brian Silvermanf51499a2020-09-21 12:49:08 -070071 // Queues up a finished FlatBufferBuilder to be encoded and written.
72 //
73 // Triggers a flush if there's enough data queued up.
74 //
75 // Steals the detached buffer from it.
Austin Schuh48d10d62022-10-16 22:19:23 -070076 void CopyMessage(DataEncoder::Copier *coppier,
77 aos::monotonic_clock::time_point now);
Austin Schuha36c8902019-12-30 18:07:15 -080078
Brian Silverman0465fcf2020-09-24 00:29:18 -070079 // Indicates we got ENOSPC when trying to write. After this returns true, no
80 // further data is written.
81 bool ran_out_of_space() const { return ran_out_of_space_; }
82
83 // To avoid silently failing to write logfiles, you must call this before
84 // destruction if ran_out_of_space() is true and the situation has been
85 // handled.
86 void acknowledge_out_of_space() {
87 CHECK(ran_out_of_space_);
88 acknowledge_ran_out_of_space_ = true;
89 }
90
91 // Fully flushes and closes the underlying file now. No additional data may be
92 // enqueued after calling this.
93 //
94 // This will be performed in the destructor automatically.
95 //
96 // Note that this may set ran_out_of_space().
97 void Close();
98
Brian Silvermanf51499a2020-09-21 12:49:08 -070099 // Returns the total number of bytes written and currently queued.
Austin Schuha426f1f2021-03-31 22:27:41 -0700100 size_t total_bytes() const {
101 if (!encoder_) {
102 return 0;
103 }
104 return encoder_->total_bytes();
105 }
Austin Schuha36c8902019-12-30 18:07:15 -0800106
Brian Silvermanf51499a2020-09-21 12:49:08 -0700107 // The maximum time for a single write call, or 0 if none have been performed.
108 std::chrono::nanoseconds max_write_time() const { return max_write_time_; }
109 // The number of bytes in the longest write call, or -1 if none have been
110 // performed.
111 int max_write_time_bytes() const { return max_write_time_bytes_; }
112 // The number of buffers in the longest write call, or -1 if none have been
113 // performed.
114 int max_write_time_messages() const { return max_write_time_messages_; }
115 // The total time spent in write calls.
116 std::chrono::nanoseconds total_write_time() const {
117 return total_write_time_;
118 }
119 // The total number of writes which have been performed.
120 int total_write_count() const { return total_write_count_; }
121 // The total number of messages which have been written.
122 int total_write_messages() const { return total_write_messages_; }
123 // The total number of bytes which have been written.
124 int total_write_bytes() const { return total_write_bytes_; }
125 void ResetStatistics() {
126 max_write_time_ = std::chrono::nanoseconds::zero();
127 max_write_time_bytes_ = -1;
128 max_write_time_messages_ = -1;
129 total_write_time_ = std::chrono::nanoseconds::zero();
130 total_write_count_ = 0;
131 total_write_messages_ = 0;
132 total_write_bytes_ = 0;
133 }
Brian Silverman98360e22020-04-28 16:51:20 -0700134
Austin Schuha36c8902019-12-30 18:07:15 -0800135 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700136 // Performs a single writev call with as much of the data we have queued up as
137 // possible.
138 //
139 // This will normally take all of the data we have queued up, unless an
140 // encoder has spit out a big enough chunk all at once that we can't manage
141 // all of it.
142 void Flush();
143
Brian Silverman0465fcf2020-09-24 00:29:18 -0700144 // write_return is what write(2) or writev(2) returned. write_size is the
145 // number of bytes we expected it to write.
146 void HandleWriteReturn(ssize_t write_return, size_t write_size);
147
Brian Silvermanf51499a2020-09-21 12:49:08 -0700148 void UpdateStatsForWrite(aos::monotonic_clock::duration duration,
149 ssize_t written, int iovec_size);
150
151 // Flushes data if we've reached the threshold to do that as part of normal
Austin Schuhbd06ae42021-03-31 22:48:21 -0700152 // operation either due to the outstanding queued data, or because we have
153 // passed our flush period. now is the current time to save some CPU grabbing
154 // the current time. It just needs to be close.
155 void FlushAtThreshold(aos::monotonic_clock::time_point now);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700156
Austin Schuh2f8fd752020-09-01 22:38:28 -0700157 std::string filename_;
Austin Schuh48d10d62022-10-16 22:19:23 -0700158 std::unique_ptr<DataEncoder> encoder_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800159
Austin Schuha36c8902019-12-30 18:07:15 -0800160 int fd_ = -1;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700161 bool ran_out_of_space_ = false;
162 bool acknowledge_ran_out_of_space_ = false;
Austin Schuha36c8902019-12-30 18:07:15 -0800163
Austin Schuha36c8902019-12-30 18:07:15 -0800164 // List of iovecs to use with writev. This is a member variable to avoid
165 // churn.
166 std::vector<struct iovec> iovec_;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700167
168 std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero();
169 int max_write_time_bytes_ = -1;
170 int max_write_time_messages_ = -1;
171 std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero();
172 int total_write_count_ = 0;
173 int total_write_messages_ = 0;
174 int total_write_bytes_ = 0;
Austin Schuhbd06ae42021-03-31 22:48:21 -0700175
176 aos::monotonic_clock::time_point last_flush_time_ =
177 aos::monotonic_clock::min_time;
Austin Schuha36c8902019-12-30 18:07:15 -0800178};
179
Austin Schuhf2d0e682022-10-16 14:20:58 -0700180// Repacks the provided RemoteMessage into fbb.
181flatbuffers::Offset<MessageHeader> PackRemoteMessage(
182 flatbuffers::FlatBufferBuilder *fbb,
183 const message_bridge::RemoteMessage *msg, int channel_index,
184 const aos::monotonic_clock::time_point monotonic_timestamp_time);
185
186constexpr flatbuffers::uoffset_t PackRemoteMessageSize() { return 96u; }
187size_t PackRemoteMessageInline(
188 uint8_t *data, const message_bridge::RemoteMessage *msg, int channel_index,
Austin Schuh71a40d42023-02-04 21:22:22 -0800189 const aos::monotonic_clock::time_point monotonic_timestamp_time,
190 size_t start_byte, size_t end_byte);
Austin Schuhf2d0e682022-10-16 14:20:58 -0700191
Austin Schuha36c8902019-12-30 18:07:15 -0800192// Packes a message pointed to by the context into a MessageHeader.
193flatbuffers::Offset<MessageHeader> PackMessage(
194 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
195 int channel_index, LogType log_type);
196
Austin Schuhfa30c352022-10-16 11:12:02 -0700197// Returns the size that the packed message from PackMessage or
198// PackMessageInline will be.
Austin Schuh48d10d62022-10-16 22:19:23 -0700199flatbuffers::uoffset_t PackMessageSize(LogType log_type, size_t data_size);
Austin Schuhfa30c352022-10-16 11:12:02 -0700200
201// Packs the provided message pointed to by context into the provided buffer.
202// This is equivalent to PackMessage, but doesn't require allocating a
203// FlatBufferBuilder underneath.
204size_t PackMessageInline(uint8_t *data, const Context &contex,
Austin Schuh71a40d42023-02-04 21:22:22 -0800205 int channel_index, LogType log_type, size_t start_byte,
206 size_t end_byte);
Austin Schuhfa30c352022-10-16 11:12:02 -0700207
Austin Schuh05b70472020-01-01 17:11:17 -0800208// Class to read chunks out of a log file.
209class SpanReader {
210 public:
Austin Schuhcd368422021-11-22 21:23:29 -0800211 SpanReader(std::string_view filename, bool quiet = false);
Austin Schuha36c8902019-12-30 18:07:15 -0800212
Austin Schuh6f3babe2020-01-26 20:34:50 -0800213 std::string_view filename() const { return filename_; }
214
Brian Smarttea913d42021-12-10 15:02:38 -0800215 size_t TotalRead() const { return total_read_; }
216 size_t TotalConsumed() const { return total_consumed_; }
Austin Schuh60e77942022-05-16 17:48:24 -0700217 bool IsIncomplete() const {
218 return is_finished_ && total_consumed_ < total_read_;
219 }
Brian Smarttea913d42021-12-10 15:02:38 -0800220
Austin Schuhcf5f6442021-07-06 10:43:28 -0700221 // Returns a span with the data for the next message from the log file,
222 // including the size. The result is only guarenteed to be valid until
223 // ReadMessage() or PeekMessage() is called again.
Austin Schuh05b70472020-01-01 17:11:17 -0800224 absl::Span<const uint8_t> ReadMessage();
225
Austin Schuhcf5f6442021-07-06 10:43:28 -0700226 // Returns a span with the data for the next message without consuming it.
227 // Multiple calls to PeekMessage return the same data. ReadMessage or
228 // ConsumeMessage must be called to get the next message.
229 absl::Span<const uint8_t> PeekMessage();
230 // Consumes the message so the next call to ReadMessage or PeekMessage returns
231 // new data. This does not invalidate the data.
232 void ConsumeMessage();
233
Austin Schuh05b70472020-01-01 17:11:17 -0800234 private:
235 // TODO(austin): Optimization:
236 // Allocate the 256k blocks like we do today. But, refcount them with
237 // shared_ptr pointed to by the messageheader that is returned. This avoids
238 // the copy. Need to do more benchmarking.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700239 // And (Brian): Consider just mmapping the file and handing out refcounted
240 // pointers into that too.
Austin Schuh05b70472020-01-01 17:11:17 -0800241
242 // Reads a chunk of data into data_. Returns false if no data was read.
243 bool ReadBlock();
244
Austin Schuhc41603c2020-10-11 16:17:37 -0700245 std::string filename_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800246
Brian Silvermanf51499a2020-09-21 12:49:08 -0700247 // File reader and data decoder.
248 std::unique_ptr<DataDecoder> decoder_;
Austin Schuh05b70472020-01-01 17:11:17 -0800249
Brian Silvermanf51499a2020-09-21 12:49:08 -0700250 // Vector to read into.
251 ResizeableBuffer data_;
Austin Schuh05b70472020-01-01 17:11:17 -0800252
253 // Amount of data consumed already in data_.
254 size_t consumed_data_ = 0;
Brian Smarttea913d42021-12-10 15:02:38 -0800255
256 // Accumulates the total volume of bytes read from filename_
257 size_t total_read_ = 0;
258
259 // Accumulates the total volume of read bytes that were 'consumed' into
260 // messages. May be less than total_read_, if the last message (span) is
261 // either truncated or somehow corrupt.
262 size_t total_consumed_ = 0;
263
264 // Reached the end, no more readable messages.
265 bool is_finished_ = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800266};
267
Brian Silvermanfee16972021-09-14 12:06:38 -0700268// Reads the last header from a log file. This handles any duplicate headers
269// that were written.
270std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
271 SpanReader *span_reader);
272std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
273 std::string_view filename);
274// Reads the Nth message from a log file, excluding the header. Note: this
275// doesn't handle duplicate headers.
276std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage(
277 std::string_view filename, size_t n);
278
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700279class UnpackedMessageHeader;
280
Austin Schuh05b70472020-01-01 17:11:17 -0800281// Class which handles reading the header and messages from the log file. This
282// handles any per-file state left before merging below.
283class MessageReader {
284 public:
285 MessageReader(std::string_view filename);
286
Austin Schuh6f3babe2020-01-26 20:34:50 -0800287 std::string_view filename() const { return span_reader_.filename(); }
288
Austin Schuh05b70472020-01-01 17:11:17 -0800289 // Returns the header from the log file.
290 const LogFileHeader *log_file_header() const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700291 return &raw_log_file_header_.message();
292 }
293
294 // Returns the raw data of the header from the log file.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800295 const SizePrefixedFlatbufferVector<LogFileHeader> &raw_log_file_header()
296 const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700297 return raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800298 }
299
300 // Returns the minimum maount of data needed to queue up for sorting before
301 // ware guarenteed to not see data out of order.
302 std::chrono::nanoseconds max_out_of_order_duration() const {
303 return max_out_of_order_duration_;
304 }
305
Austin Schuhcde938c2020-02-02 17:30:07 -0800306 // Returns the newest timestamp read out of the log file.
Austin Schuh05b70472020-01-01 17:11:17 -0800307 monotonic_clock::time_point newest_timestamp() const {
308 return newest_timestamp_;
309 }
310
311 // Returns the next message if there is one.
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700312 std::shared_ptr<UnpackedMessageHeader> ReadMessage();
Austin Schuh05b70472020-01-01 17:11:17 -0800313
314 // The time at which we need to read another chunk from the logfile.
315 monotonic_clock::time_point queue_data_time() const {
316 return newest_timestamp() - max_out_of_order_duration();
317 }
318
Brian Smarttea913d42021-12-10 15:02:38 -0800319 // Flag value setters for testing
320 void set_crash_on_corrupt_message_flag(bool b) {
321 crash_on_corrupt_message_flag_ = b;
322 }
323 void set_ignore_corrupt_messages_flag(bool b) {
324 ignore_corrupt_messages_flag_ = b;
325 }
326
Austin Schuh05b70472020-01-01 17:11:17 -0800327 private:
328 // Log chunk reader.
329 SpanReader span_reader_;
330
Austin Schuh97789fc2020-08-01 14:42:45 -0700331 // Vector holding the raw data for the log file header.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800332 SizePrefixedFlatbufferVector<LogFileHeader> raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800333
334 // Minimum amount of data to queue up for sorting before we are guarenteed
335 // to not see data out of order.
336 std::chrono::nanoseconds max_out_of_order_duration_;
337
338 // Timestamp of the newest message in a channel queue.
339 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Brian Smarttea913d42021-12-10 15:02:38 -0800340
341 // Total volume of verifiable messages from the beginning of the file.
342 // TODO - are message counts also useful?
343 size_t total_verified_before_ = 0;
344
345 // Total volume of messages with corrupted flatbuffer formatting, if any.
346 // Excludes corrupted message content.
347 // TODO - if the layout included something as simple as a CRC (relatively
348 // fast and robust enough) for each span, then corrupted content could be
349 // included in this check.
350 size_t total_corrupted_ = 0;
351
352 // Total volume of verifiable messages intermixed with corrupted messages,
353 // if any. Will be == 0 if total_corrupted_ == 0.
354 size_t total_verified_during_ = 0;
355
356 // Total volume of verifiable messages found after the last corrupted one,
357 // if any. Will be == 0 if total_corrupted_ == 0.
358 size_t total_verified_after_ = 0;
359
360 bool is_corrupted() const { return total_corrupted_ > 0; }
361
362 bool crash_on_corrupt_message_flag_ = true;
363 bool ignore_corrupt_messages_flag_ = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800364};
365
Austin Schuhc41603c2020-10-11 16:17:37 -0700366// A class to seamlessly read messages from a list of part files.
367class PartsMessageReader {
368 public:
369 PartsMessageReader(LogParts log_parts);
370
371 std::string_view filename() const { return message_reader_.filename(); }
372
Austin Schuhd2f96102020-12-01 20:27:29 -0800373 // Returns the LogParts that holds the filenames we are reading.
374 const LogParts &parts() const { return parts_; }
375
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800376 const LogFileHeader *log_file_header() const {
377 return message_reader_.log_file_header();
378 }
379
Austin Schuhc41603c2020-10-11 16:17:37 -0700380 // Returns the minimum amount of data needed to queue up for sorting before
381 // we are guarenteed to not see data out of order.
382 std::chrono::nanoseconds max_out_of_order_duration() const {
383 return message_reader_.max_out_of_order_duration();
384 }
385
386 // Returns the newest timestamp read out of the log file.
387 monotonic_clock::time_point newest_timestamp() const {
388 return newest_timestamp_;
389 }
390
391 // Returns the next message if there is one, or nullopt if we have reached the
392 // end of all the files.
393 // Note: reading the next message may change the max_out_of_order_duration().
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700394 std::shared_ptr<UnpackedMessageHeader> ReadMessage();
Austin Schuhc41603c2020-10-11 16:17:37 -0700395
Austin Schuh48507722021-07-17 17:29:24 -0700396 // Returns the boot count for the requested node, or std::nullopt if we don't
397 // know.
398 std::optional<size_t> boot_count(size_t node_index) const {
399 CHECK_GE(node_index, 0u);
400 CHECK_LT(node_index, boot_counts_.size());
401 return boot_counts_[node_index];
402 }
403
Austin Schuhc41603c2020-10-11 16:17:37 -0700404 private:
405 // Opens the next log and updates message_reader_. Sets done_ if there is
406 // nothing more to do.
407 void NextLog();
Austin Schuh48507722021-07-17 17:29:24 -0700408 void ComputeBootCounts();
Austin Schuhc41603c2020-10-11 16:17:37 -0700409
410 const LogParts parts_;
411 size_t next_part_index_ = 1u;
412 bool done_ = false;
413 MessageReader message_reader_;
Brian Silvermanfee16972021-09-14 12:06:38 -0700414 // We instantiate the next one early, to allow implementations to prefetch.
415 // TODO(Brian): To get optimal performance when downloading, this needs more
416 // communication with the implementation to prioritize the next part and add
417 // more parallelism when it helps. Maybe some kind of a queue of parts in
418 // order, and the implementation gets to pull however many make sense off the
419 // front?
420 std::optional<MessageReader> next_message_reader_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700421
Austin Schuh315b96b2020-12-11 21:21:12 -0800422 // True after we have seen a message after the start of the log. The
423 // guarentees on logging essentially are that all data from before the
424 // starting time of the log may be arbitrarily out of order, but once we get
425 // max_out_of_order_duration past the start, everything will remain within
426 // max_out_of_order_duration. We shouldn't see anything before the start
427 // after we've seen a message that is at least max_out_of_order_duration after
428 // the start.
429 bool after_start_ = false;
430
Austin Schuhc41603c2020-10-11 16:17:37 -0700431 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Austin Schuh48507722021-07-17 17:29:24 -0700432
433 // Per node boot counts.
434 std::vector<std::optional<size_t>> boot_counts_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700435};
436
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700437// Stores MessageHeader as a flat header and inline, aligned block of data.
438class UnpackedMessageHeader {
439 public:
James Kuszmaul9776b392023-01-14 14:08:08 -0800440 UnpackedMessageHeader(
441 uint32_t channel_index, monotonic_clock::time_point monotonic_sent_time,
442 realtime_clock::time_point realtime_sent_time, uint32_t queue_index,
443 std::optional<monotonic_clock::time_point> monotonic_remote_time,
444 std::optional<realtime_clock::time_point> realtime_remote_time,
445 std::optional<uint32_t> remote_queue_index,
446 monotonic_clock::time_point monotonic_timestamp_time,
447 bool has_monotonic_timestamp_time, absl::Span<const uint8_t> span)
448 : channel_index(channel_index),
449 monotonic_sent_time(monotonic_sent_time),
450 realtime_sent_time(realtime_sent_time),
451 queue_index(queue_index),
452 monotonic_remote_time(monotonic_remote_time),
453 realtime_remote_time(realtime_remote_time),
454 remote_queue_index(remote_queue_index),
455 monotonic_timestamp_time(monotonic_timestamp_time),
456 has_monotonic_timestamp_time(has_monotonic_timestamp_time),
457 span(span) {}
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700458 UnpackedMessageHeader(const UnpackedMessageHeader &) = delete;
459 UnpackedMessageHeader &operator=(const UnpackedMessageHeader &) = delete;
460
461 // The channel.
462 uint32_t channel_index = 0xffffffff;
463
464 monotonic_clock::time_point monotonic_sent_time;
465 realtime_clock::time_point realtime_sent_time;
466
467 // The local queue index.
468 uint32_t queue_index = 0xffffffff;
469
Austin Schuh826e6ce2021-11-18 20:33:10 -0800470 std::optional<aos::monotonic_clock::time_point> monotonic_remote_time;
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700471
472 std::optional<realtime_clock::time_point> realtime_remote_time;
473 std::optional<uint32_t> remote_queue_index;
474
475 // This field is defaulted in the flatbuffer, so we need to store both the
476 // possibly defaulted value and whether it is defaulted.
477 monotonic_clock::time_point monotonic_timestamp_time;
478 bool has_monotonic_timestamp_time;
479
480 static std::shared_ptr<UnpackedMessageHeader> MakeMessage(
481 const MessageHeader &message);
482
483 // Note: we are storing a span here because we need something to put in the
484 // SharedSpan pointer that RawSender takes. We are using the aliasing
485 // constructor of shared_ptr to avoid the allocation, and it needs a nice
486 // pointer to track.
487 absl::Span<const uint8_t> span;
488
489 char actual_data[];
490
491 private:
492 ~UnpackedMessageHeader() {}
493
494 static void DestroyAndFree(UnpackedMessageHeader *p) {
495 p->~UnpackedMessageHeader();
496 free(p);
497 }
498};
499
500std::ostream &operator<<(std::ostream &os,
501 const UnpackedMessageHeader &message);
502
Austin Schuh1be0ce42020-11-29 22:43:26 -0800503// Struct to hold a message as it gets sorted on a single node.
504struct Message {
505 // The channel.
506 uint32_t channel_index = 0xffffffff;
507 // The local queue index.
Austin Schuh58646e22021-08-23 23:51:46 -0700508 // TODO(austin): Technically the boot inside queue_index is redundant with
509 // timestamp. In practice, it is less error-prone to duplicate it. Maybe a
510 // function to return the combined struct?
511 BootQueueIndex queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700512 // The local timestamp.
513 BootTimestamp timestamp;
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700514
Austin Schuh48507722021-07-17 17:29:24 -0700515 // Remote boot when this is a timestamp.
516 size_t monotonic_remote_boot = 0xffffff;
517
518 size_t monotonic_timestamp_boot = 0xffffff;
519
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700520 std::shared_ptr<UnpackedMessageHeader> data;
Austin Schuh1be0ce42020-11-29 22:43:26 -0800521
522 bool operator<(const Message &m2) const;
523 bool operator>=(const Message &m2) const;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800524 bool operator==(const Message &m2) const;
Austin Schuh1be0ce42020-11-29 22:43:26 -0800525};
526
527std::ostream &operator<<(std::ostream &os, const Message &m);
528
Austin Schuhd2f96102020-12-01 20:27:29 -0800529// Structure to hold a full message and all the timestamps, which may or may not
530// have been sent from a remote node. The remote_queue_index will be invalid if
531// this message is from the point of view of the node which sent it.
532struct TimestampedMessage {
533 uint32_t channel_index = 0xffffffff;
534
Austin Schuh58646e22021-08-23 23:51:46 -0700535 BootQueueIndex queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700536 BootTimestamp monotonic_event_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800537 realtime_clock::time_point realtime_event_time = realtime_clock::min_time;
538
Austin Schuh58646e22021-08-23 23:51:46 -0700539 BootQueueIndex remote_queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700540 BootTimestamp monotonic_remote_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800541 realtime_clock::time_point realtime_remote_time = realtime_clock::min_time;
542
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700543 BootTimestamp monotonic_timestamp_time;
Austin Schuh8bf1e632021-01-02 22:41:04 -0800544
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700545 std::shared_ptr<UnpackedMessageHeader> data;
Austin Schuhd2f96102020-12-01 20:27:29 -0800546};
547
548std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m);
549
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800550// Class to sort the resulting messages from a PartsMessageReader.
551class LogPartsSorter {
552 public:
553 LogPartsSorter(LogParts log_parts);
554
Austin Schuh0ca51f32020-12-25 21:51:45 -0800555 // Returns the parts that this is sorting messages from.
556 const LogParts &parts() const { return parts_message_reader_.parts(); }
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800557
Austin Schuhd2f96102020-12-01 20:27:29 -0800558 monotonic_clock::time_point monotonic_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800559 return parts().monotonic_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800560 }
561 realtime_clock::time_point realtime_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800562 return parts().realtime_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800563 }
564
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800565 // The time this data is sorted until.
566 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
567
568 // Returns the next sorted message from the log file. It is safe to call
569 // std::move() on the result to move the data flatbuffer from it.
570 Message *Front();
571 // Pops the front message. This should only be called after a call to
572 // Front().
573 void PopFront();
574
575 // Returns a debug string representing the contents of this sorter.
576 std::string DebugString() const;
577
578 private:
579 // Log parts reader we are wrapping.
580 PartsMessageReader parts_message_reader_;
581 // Cache of the time we are sorted until.
582 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
583
Austin Schuhb000de62020-12-03 22:00:40 -0800584 // Timestamp of the last message returned. Used to make sure nothing goes
585 // backwards.
586 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
587
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800588 // Set used for efficient sorting of messages. We can benchmark and evaluate
589 // other data structures if this proves to be the bottleneck.
590 absl::btree_set<Message> messages_;
Austin Schuh48507722021-07-17 17:29:24 -0700591
592 // Mapping from channel to source node.
593 // TODO(austin): Should we put this in Boots so it can be cached for everyone?
594 std::vector<size_t> source_node_index_;
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800595};
596
Austin Schuh8f52ed52020-11-30 23:12:39 -0800597// Class to run merge sort on the messages from multiple LogPartsSorter
598// instances.
599class NodeMerger {
600 public:
Austin Schuhd2f96102020-12-01 20:27:29 -0800601 NodeMerger(std::vector<LogParts> parts);
602
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700603 // Copying and moving will mess up the internal raw pointers. Just don't do
604 // it.
605 NodeMerger(NodeMerger const &) = delete;
606 NodeMerger(NodeMerger &&) = delete;
607 void operator=(NodeMerger const &) = delete;
608 void operator=(NodeMerger &&) = delete;
609
Austin Schuhd2f96102020-12-01 20:27:29 -0800610 // Node index in the configuration of this node.
611 int node() const { return node_; }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800612
Austin Schuh0ca51f32020-12-25 21:51:45 -0800613 // List of parts being sorted together.
614 std::vector<const LogParts *> Parts() const;
615
616 const Configuration *configuration() const {
617 return parts_sorters_[0].parts().config.get();
Austin Schuhd2f96102020-12-01 20:27:29 -0800618 }
619
620 monotonic_clock::time_point monotonic_start_time() const {
621 return monotonic_start_time_;
622 }
623 realtime_clock::time_point realtime_start_time() const {
624 return realtime_start_time_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800625 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800626 monotonic_clock::time_point monotonic_oldest_time() const {
627 return monotonic_oldest_time_;
628 }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800629
630 // The time this data is sorted until.
631 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
632
633 // Returns the next sorted message from the set of log files. It is safe to
634 // call std::move() on the result to move the data flatbuffer from it.
635 Message *Front();
636 // Pops the front message. This should only be called after a call to
637 // Front().
638 void PopFront();
639
640 private:
641 // Unsorted list of all parts sorters.
Austin Schuhd2f96102020-12-01 20:27:29 -0800642 std::vector<LogPartsSorter> parts_sorters_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800643 // Pointer to the parts sorter holding the current Front message if one
644 // exists, or nullptr if a new one needs to be found.
645 LogPartsSorter *current_ = nullptr;
646 // Cached sorted_until value.
647 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800648
649 // Cached node.
650 int node_;
651
Austin Schuhb000de62020-12-03 22:00:40 -0800652 // Timestamp of the last message returned. Used to make sure nothing goes
653 // backwards.
654 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
655
Austin Schuhd2f96102020-12-01 20:27:29 -0800656 realtime_clock::time_point realtime_start_time_ = realtime_clock::max_time;
657 monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::max_time;
Austin Schuh60e77942022-05-16 17:48:24 -0700658 monotonic_clock::time_point monotonic_oldest_time_ =
659 monotonic_clock::max_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800660};
661
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700662// Class to concatenate multiple boots worth of logs into a single per-node
663// stream.
664class BootMerger {
665 public:
666 BootMerger(std::vector<LogParts> file);
667
668 // Copying and moving will mess up the internal raw pointers. Just don't do
669 // it.
670 BootMerger(BootMerger const &) = delete;
671 BootMerger(BootMerger &&) = delete;
672 void operator=(BootMerger const &) = delete;
673 void operator=(BootMerger &&) = delete;
674
675 // Node index in the configuration of this node.
676 int node() const { return node_mergers_[0]->node(); }
677
678 // List of parts being sorted together.
679 std::vector<const LogParts *> Parts() const;
680
681 const Configuration *configuration() const {
682 return node_mergers_[0]->configuration();
683 }
684
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700685 monotonic_clock::time_point monotonic_start_time(size_t boot) const {
686 CHECK_LT(boot, node_mergers_.size());
687 return node_mergers_[boot]->monotonic_start_time();
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700688 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700689 realtime_clock::time_point realtime_start_time(size_t boot) const {
690 CHECK_LT(boot, node_mergers_.size());
691 return node_mergers_[boot]->realtime_start_time();
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700692 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800693 monotonic_clock::time_point monotonic_oldest_time(size_t boot) const {
694 CHECK_LT(boot, node_mergers_.size());
695 return node_mergers_[boot]->monotonic_oldest_time();
696 }
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700697
698 bool started() const {
699 return node_mergers_[index_]->sorted_until() != monotonic_clock::min_time ||
700 index_ != 0;
701 }
702
703 // Returns the next sorted message from the set of log files. It is safe to
704 // call std::move() on the result to move the data flatbuffer from it.
705 Message *Front();
706 // Pops the front message. This should only be called after a call to
707 // Front().
708 void PopFront();
709
710 private:
711 int index_ = 0;
712
713 // TODO(austin): Sanjay points out this is pretty inefficient. Don't keep so
714 // many things open.
715 std::vector<std::unique_ptr<NodeMerger>> node_mergers_;
716};
717
Austin Schuhd2f96102020-12-01 20:27:29 -0800718// Class to match timestamps with the corresponding data from other nodes.
Austin Schuh79b30942021-01-24 22:32:21 -0800719//
720// This class also buffers data for the node it represents, and supports
721// notifying when new data is queued as well as queueing until a point in time.
Austin Schuhd2f96102020-12-01 20:27:29 -0800722class TimestampMapper {
723 public:
724 TimestampMapper(std::vector<LogParts> file);
725
726 // Copying and moving will mess up the internal raw pointers. Just don't do
727 // it.
728 TimestampMapper(TimestampMapper const &) = delete;
729 TimestampMapper(TimestampMapper &&) = delete;
730 void operator=(TimestampMapper const &) = delete;
731 void operator=(TimestampMapper &&) = delete;
732
733 // TODO(austin): It would be super helpful to provide a way to queue up to
734 // time X without matching timestamps, and to then be able to pull the
735 // timestamps out of this queue. This lets us bootstrap time estimation
736 // without exploding memory usage worst case.
737
Austin Schuh0ca51f32020-12-25 21:51:45 -0800738 const Configuration *configuration() const { return configuration_.get(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800739
740 // Returns which node this is sorting for.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700741 size_t node() const { return boot_merger_.node(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800742
743 // The start time of this log.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700744 monotonic_clock::time_point monotonic_start_time(size_t boot) const {
745 return boot_merger_.monotonic_start_time(boot);
Austin Schuhd2f96102020-12-01 20:27:29 -0800746 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700747 realtime_clock::time_point realtime_start_time(size_t boot) const {
748 return boot_merger_.realtime_start_time(boot);
Austin Schuhd2f96102020-12-01 20:27:29 -0800749 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800750 // Returns the oldest timestamp on a message on this boot.
751 monotonic_clock::time_point monotonic_oldest_time(size_t boot) const {
752 return boot_merger_.monotonic_oldest_time(boot);
753 }
Austin Schuhd2f96102020-12-01 20:27:29 -0800754
755 // Uses timestamp_mapper as the peer for its node. Only one mapper may be set
756 // for each node. Peers are used to look up the data for timestamps on this
757 // node.
758 void AddPeer(TimestampMapper *timestamp_mapper);
759
Austin Schuh24bf4972021-06-29 22:09:08 -0700760 // Returns true if anything has been queued up.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700761 bool started() const { return boot_merger_.started(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800762
763 // Returns the next message for this node.
764 TimestampedMessage *Front();
765 // Pops the next message. Front must be called first.
766 void PopFront();
767
768 // Returns debug information about this node.
769 std::string DebugString() const;
770
Austin Schuh79b30942021-01-24 22:32:21 -0800771 // Queues data the provided time.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700772 void QueueUntil(BootTimestamp queue_time);
Austin Schuhe639ea12021-01-25 13:00:22 -0800773 // Queues until we have time_estimation_buffer of data in the queue.
774 void QueueFor(std::chrono::nanoseconds time_estimation_buffer);
Austin Schuh79b30942021-01-24 22:32:21 -0800775
Austin Schuh06601222021-01-26 17:02:50 -0800776 // Queues until the condition is met.
777 template <typename T>
778 void QueueUntilCondition(T fn) {
779 while (true) {
780 if (fn()) {
781 break;
782 }
783 if (!QueueMatched()) {
784 break;
785 }
786 }
787 }
788
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700789 // Sets the callback that can be used to skip messages.
790 void set_replay_channels_callback(
791 std::function<bool(const TimestampedMessage &)> fn) {
792 replay_channels_callback_ = fn;
793 }
794
Austin Schuh79b30942021-01-24 22:32:21 -0800795 // Sets a callback to be called whenever a full message is queued.
796 void set_timestamp_callback(std::function<void(TimestampedMessage *)> fn) {
797 timestamp_callback_ = fn;
798 }
799
Austin Schuhd2f96102020-12-01 20:27:29 -0800800 private:
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700801 // Result of MaybeQueueMatched
802 enum class MatchResult : uint8_t {
803 kEndOfFile, // End of the log file being read
804 kQueued, // Message was queued
805 kSkipped // Message was skipped over
806 };
807
Austin Schuhd2f96102020-12-01 20:27:29 -0800808 // The state for a remote node. This holds the data that needs to be matched
809 // with the remote node's timestamps.
810 struct NodeData {
811 // True if we should save data here. This should be true if any of the
812 // bools in delivered below are true.
813 bool any_delivered = false;
814
Austin Schuh36c00932021-07-19 18:13:21 -0700815 // True if we have a peer and therefore should be saving data for it.
816 bool save_for_peer = false;
817
Austin Schuhd2f96102020-12-01 20:27:29 -0800818 // Peer pointer. This node is only to be considered if a peer is set.
819 TimestampMapper *peer = nullptr;
820
821 struct ChannelData {
822 // Deque per channel. This contains the data from the outside
823 // TimestampMapper node which is relevant for the node this NodeData
824 // points to.
825 std::deque<Message> messages;
826 // Bool tracking per channel if a message is delivered to the node this
827 // NodeData represents.
828 bool delivered = false;
Austin Schuh6a7358f2021-11-18 22:40:40 -0800829 // The TTL for delivery.
830 std::chrono::nanoseconds time_to_live = std::chrono::nanoseconds(0);
Austin Schuhd2f96102020-12-01 20:27:29 -0800831 };
832
833 // Vector with per channel data.
834 std::vector<ChannelData> channels;
835 };
836
837 // Returns (and forgets about) the data for the provided timestamp message
838 // showing when it was delivered to this node.
839 Message MatchingMessageFor(const Message &message);
840
841 // Queues up a single message into our message queue, and any nodes that this
842 // message is delivered to. Returns true if one was available, false
843 // otherwise.
844 bool Queue();
845
Austin Schuh79b30942021-01-24 22:32:21 -0800846 // Queues up a single matched message into our matched message queue. Returns
847 // true if one was queued, and false otherwise.
848 bool QueueMatched();
849
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700850 // Queues a message if the replay_channels_callback is passed and the end of
851 // the log file has not been reached.
852 MatchResult MaybeQueueMatched();
853
Austin Schuhd2f96102020-12-01 20:27:29 -0800854 // Queues up data until we have at least one message >= to time t.
855 // Useful for triggering a remote node to read enough data to have the
856 // timestamp you care about available.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700857 void QueueUnmatchedUntil(BootTimestamp t);
Austin Schuhd2f96102020-12-01 20:27:29 -0800858
Austin Schuh79b30942021-01-24 22:32:21 -0800859 // Queues m into matched_messages_.
860 void QueueMessage(Message *m);
Austin Schuhd2f96102020-12-01 20:27:29 -0800861
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700862 // If a replay_channels_callback was set and the callback returns false, a
863 // matched message is popped and true is returned. Otherwise false is
864 // returned.
865 bool CheckReplayChannelsAndMaybePop(const TimestampedMessage &message);
866
Austin Schuh58646e22021-08-23 23:51:46 -0700867 // Returns the name of the node this class is sorting for.
868 std::string_view node_name() const {
869 return configuration_->has_nodes() ? configuration_->nodes()
870 ->Get(boot_merger_.node())
871 ->name()
872 ->string_view()
873 : "(single node)";
874 }
875
Austin Schuhd2f96102020-12-01 20:27:29 -0800876 // The node merger to source messages from.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700877 BootMerger boot_merger_;
Austin Schuh0ca51f32020-12-25 21:51:45 -0800878
879 std::shared_ptr<const Configuration> configuration_;
880
Austin Schuhd2f96102020-12-01 20:27:29 -0800881 // The buffer of messages for this node. These are not matched with any
882 // remote data.
883 std::deque<Message> messages_;
884 // The node index for the source node for each channel.
885 std::vector<size_t> source_node_;
886
887 // Vector per node. Not all nodes will have anything.
888 std::vector<NodeData> nodes_data_;
889
890 // Latest message to return.
Austin Schuh79b30942021-01-24 22:32:21 -0800891 std::deque<TimestampedMessage> matched_messages_;
Austin Schuhd2f96102020-12-01 20:27:29 -0800892
Austin Schuh79b30942021-01-24 22:32:21 -0800893 // Tracks the state of the first message in matched_messages_. Do we need to
894 // update it, is it valid, or should we return nullptr?
Austin Schuhd2f96102020-12-01 20:27:29 -0800895 enum class FirstMessage {
896 kNeedsUpdate,
897 kInMessage,
898 kNullptr,
899 };
900 FirstMessage first_message_ = FirstMessage::kNeedsUpdate;
901
902 // Timestamp of the last message returned. Used to make sure nothing goes
903 // backwards.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700904 BootTimestamp last_message_time_ = BootTimestamp::min_time();
Austin Schuh6a7358f2021-11-18 22:40:40 -0800905 BootTimestamp last_popped_message_time_ = BootTimestamp::min_time();
Austin Schuhd2f96102020-12-01 20:27:29 -0800906 // Time this node is queued up until. Used for caching.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700907 BootTimestamp queued_until_ = BootTimestamp::min_time();
Austin Schuh79b30942021-01-24 22:32:21 -0800908
909 std::function<void(TimestampedMessage *)> timestamp_callback_;
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700910 std::function<bool(TimestampedMessage &)> replay_channels_callback_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800911};
912
Austin Schuhee711052020-08-24 16:06:09 -0700913// Returns the node name with a trailing space, or an empty string if we are on
914// a single node.
915std::string MaybeNodeName(const Node *);
916
Austin Schuh71a40d42023-02-04 21:22:22 -0800917// Class to copy a RemoteMessage into the provided buffer.
918class RemoteMessageCopier : public DataEncoder::Copier {
919 public:
920 RemoteMessageCopier(const message_bridge::RemoteMessage *message,
921 int channel_index,
922 aos::monotonic_clock::time_point monotonic_timestamp_time,
923 EventLoop *event_loop)
924 : DataEncoder::Copier(PackRemoteMessageSize()),
925 message_(message),
926 channel_index_(channel_index),
927 monotonic_timestamp_time_(monotonic_timestamp_time),
928 event_loop_(event_loop) {}
929
930 monotonic_clock::time_point end_time() const { return end_time_; }
931
932 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
933 size_t result = PackRemoteMessageInline(data, message_, channel_index_,
934 monotonic_timestamp_time_,
935 start_byte, end_byte);
936 end_time_ = event_loop_->monotonic_now();
937 return result;
938 }
939
940 private:
941 const message_bridge::RemoteMessage *message_;
942 int channel_index_;
943 aos::monotonic_clock::time_point monotonic_timestamp_time_;
944 EventLoop *event_loop_;
945 monotonic_clock::time_point end_time_;
946};
947
948// Class to copy a context into the provided buffer.
949class ContextDataCopier : public DataEncoder::Copier {
950 public:
951 ContextDataCopier(const Context &context, int channel_index, LogType log_type,
952 EventLoop *event_loop)
953 : DataEncoder::Copier(PackMessageSize(log_type, context.size)),
954 context_(context),
955 channel_index_(channel_index),
956 log_type_(log_type),
957 event_loop_(event_loop) {}
958
959 monotonic_clock::time_point end_time() const { return end_time_; }
960
961 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
962 size_t result = PackMessageInline(data, context_, channel_index_, log_type_,
963 start_byte, end_byte);
964 end_time_ = event_loop_->monotonic_now();
965 return result;
966 }
967
968 private:
969 const Context &context_;
970 const int channel_index_;
971 const LogType log_type_;
972 EventLoop *event_loop_;
973 monotonic_clock::time_point end_time_;
974};
975
Brian Silvermanf51499a2020-09-21 12:49:08 -0700976} // namespace aos::logger
Austin Schuha36c8902019-12-30 18:07:15 -0800977
978#endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_