blob: 5d5d3448f8d62ede0ad7e07cff6fea6aa6ba6351 [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"
Philipp Schrader790cb542023-07-05 21:06:52 -070019#include "flatbuffers/flatbuffers.h"
20
Brian Silvermanf51499a2020-09-21 12:49:08 -070021#include "aos/containers/resizeable_buffer.h"
Austin Schuha36c8902019-12-30 18:07:15 -080022#include "aos/events/event_loop.h"
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070023#include "aos/events/logging/boot_timestamp.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070024#include "aos/events/logging/buffer_encoder.h"
Alexei Strots01395492023-03-20 13:59:56 -070025#include "aos/events/logging/log_backend.h"
Austin Schuhc41603c2020-10-11 16:17:37 -070026#include "aos/events/logging/logfile_sorting.h"
Austin Schuha36c8902019-12-30 18:07:15 -080027#include "aos/events/logging/logger_generated.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070028#include "aos/flatbuffers.h"
Austin Schuhf2d0e682022-10-16 14:20:58 -070029#include "aos/network/remote_message_generated.h"
Austin Schuha36c8902019-12-30 18:07:15 -080030
Brian Silvermanf51499a2020-09-21 12:49:08 -070031namespace aos::logger {
Austin Schuha36c8902019-12-30 18:07:15 -080032
33enum class LogType : uint8_t {
34 // The message originated on this node and should be logged here.
35 kLogMessage,
36 // The message originated on another node, but only the delivery times are
37 // logged here.
38 kLogDeliveryTimeOnly,
39 // The message originated on another node. Log it and the delivery times
40 // together. The message_gateway is responsible for logging any messages
41 // which didn't get delivered.
Austin Schuh6f3babe2020-01-26 20:34:50 -080042 kLogMessageAndDeliveryTime,
43 // The message originated on the other node and should be logged on this node.
44 kLogRemoteMessage
Austin Schuha36c8902019-12-30 18:07:15 -080045};
46
Austin Schuha36c8902019-12-30 18:07:15 -080047// This class manages efficiently writing a sequence of detached buffers to a
Brian Silvermanf51499a2020-09-21 12:49:08 -070048// file. It encodes them, queues them up, and batches the write operation.
Alexei Strots01395492023-03-20 13:59:56 -070049
Austin Schuha36c8902019-12-30 18:07:15 -080050class DetachedBufferWriter {
51 public:
Brian Silvermana9f2ec92020-10-06 18:00:53 -070052 // Marker struct for one of our constructor overloads.
53 struct already_out_of_space_t {};
54
Alexei Strotsbc082d82023-05-03 08:43:42 -070055 DetachedBufferWriter(std::unique_ptr<LogSink> log_sink,
Austin Schuh48d10d62022-10-16 22:19:23 -070056 std::unique_ptr<DataEncoder> encoder);
Brian Silvermana9f2ec92020-10-06 18:00:53 -070057 // Creates a dummy instance which won't even open a file. It will act as if
58 // opening the file ran out of space immediately.
59 DetachedBufferWriter(already_out_of_space_t) : ran_out_of_space_(true) {}
Austin Schuh2f8fd752020-09-01 22:38:28 -070060 DetachedBufferWriter(DetachedBufferWriter &&other);
61 DetachedBufferWriter(const DetachedBufferWriter &) = delete;
62
Austin Schuha36c8902019-12-30 18:07:15 -080063 ~DetachedBufferWriter();
64
Austin Schuh2f8fd752020-09-01 22:38:28 -070065 DetachedBufferWriter &operator=(DetachedBufferWriter &&other);
Brian Silverman98360e22020-04-28 16:51:20 -070066 DetachedBufferWriter &operator=(const DetachedBufferWriter &) = delete;
67
Alexei Strotsbc082d82023-05-03 08:43:42 -070068 std::string_view name() const { return log_sink_->name(); }
Austin Schuh6f3babe2020-01-26 20:34:50 -080069
Brian Silvermana9f2ec92020-10-06 18:00:53 -070070 // This will be true until Close() is called, unless the file couldn't be
71 // created due to running out of space.
Alexei Strotsbc082d82023-05-03 08:43:42 -070072 bool is_open() const { return log_sink_->is_open(); }
Brian Silvermana9f2ec92020-10-06 18:00:53 -070073
Brian Silvermanf51499a2020-09-21 12:49:08 -070074 // Queues up a finished FlatBufferBuilder to be encoded and written.
75 //
76 // Triggers a flush if there's enough data queued up.
77 //
78 // Steals the detached buffer from it.
Austin Schuh48d10d62022-10-16 22:19:23 -070079 void CopyMessage(DataEncoder::Copier *coppier,
80 aos::monotonic_clock::time_point now);
Austin Schuha36c8902019-12-30 18:07:15 -080081
Brian Silverman0465fcf2020-09-24 00:29:18 -070082 // Indicates we got ENOSPC when trying to write. After this returns true, no
83 // further data is written.
84 bool ran_out_of_space() const { return ran_out_of_space_; }
85
86 // To avoid silently failing to write logfiles, you must call this before
87 // destruction if ran_out_of_space() is true and the situation has been
88 // handled.
89 void acknowledge_out_of_space() {
90 CHECK(ran_out_of_space_);
91 acknowledge_ran_out_of_space_ = true;
92 }
93
94 // Fully flushes and closes the underlying file now. No additional data may be
95 // enqueued after calling this.
96 //
97 // This will be performed in the destructor automatically.
98 //
99 // Note that this may set ran_out_of_space().
100 void Close();
101
Brian Silvermanf51499a2020-09-21 12:49:08 -0700102 // Returns the total number of bytes written and currently queued.
Austin Schuha426f1f2021-03-31 22:27:41 -0700103 size_t total_bytes() const {
104 if (!encoder_) {
105 return 0;
106 }
107 return encoder_->total_bytes();
108 }
Austin Schuha36c8902019-12-30 18:07:15 -0800109
Alexei Strotsbc082d82023-05-03 08:43:42 -0700110 WriteStats *WriteStatistics() const { return log_sink_->WriteStatistics(); }
Brian Silverman98360e22020-04-28 16:51:20 -0700111
Austin Schuha36c8902019-12-30 18:07:15 -0800112 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700113 // Performs a single writev call with as much of the data we have queued up as
Austin Schuh8bdfc492023-02-11 12:53:13 -0800114 // possible. now is the time we flushed at, to be recorded in
115 // last_flush_time_.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700116 //
117 // This will normally take all of the data we have queued up, unless an
118 // encoder has spit out a big enough chunk all at once that we can't manage
119 // all of it.
Austin Schuh8bdfc492023-02-11 12:53:13 -0800120 void Flush(aos::monotonic_clock::time_point now);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700121
Brian Silvermanf51499a2020-09-21 12:49:08 -0700122 // Flushes data if we've reached the threshold to do that as part of normal
Austin Schuhbd06ae42021-03-31 22:48:21 -0700123 // operation either due to the outstanding queued data, or because we have
124 // passed our flush period. now is the current time to save some CPU grabbing
125 // the current time. It just needs to be close.
126 void FlushAtThreshold(aos::monotonic_clock::time_point now);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700127
Alexei Strotsbc082d82023-05-03 08:43:42 -0700128 std::unique_ptr<LogSink> log_sink_;
Austin Schuh48d10d62022-10-16 22:19:23 -0700129 std::unique_ptr<DataEncoder> encoder_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800130
Brian Silverman0465fcf2020-09-24 00:29:18 -0700131 bool ran_out_of_space_ = false;
132 bool acknowledge_ran_out_of_space_ = false;
Austin Schuha36c8902019-12-30 18:07:15 -0800133
Austin Schuhbd06ae42021-03-31 22:48:21 -0700134 aos::monotonic_clock::time_point last_flush_time_ =
135 aos::monotonic_clock::min_time;
Austin Schuha36c8902019-12-30 18:07:15 -0800136};
137
Austin Schuhf2d0e682022-10-16 14:20:58 -0700138// Repacks the provided RemoteMessage into fbb.
139flatbuffers::Offset<MessageHeader> PackRemoteMessage(
140 flatbuffers::FlatBufferBuilder *fbb,
141 const message_bridge::RemoteMessage *msg, int channel_index,
142 const aos::monotonic_clock::time_point monotonic_timestamp_time);
143
144constexpr flatbuffers::uoffset_t PackRemoteMessageSize() { return 96u; }
145size_t PackRemoteMessageInline(
146 uint8_t *data, const message_bridge::RemoteMessage *msg, int channel_index,
Austin Schuh71a40d42023-02-04 21:22:22 -0800147 const aos::monotonic_clock::time_point monotonic_timestamp_time,
148 size_t start_byte, size_t end_byte);
Austin Schuhf2d0e682022-10-16 14:20:58 -0700149
Austin Schuha36c8902019-12-30 18:07:15 -0800150// Packes a message pointed to by the context into a MessageHeader.
151flatbuffers::Offset<MessageHeader> PackMessage(
152 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
153 int channel_index, LogType log_type);
154
Austin Schuhfa30c352022-10-16 11:12:02 -0700155// Returns the size that the packed message from PackMessage or
156// PackMessageInline will be.
Austin Schuh48d10d62022-10-16 22:19:23 -0700157flatbuffers::uoffset_t PackMessageSize(LogType log_type, size_t data_size);
Austin Schuhfa30c352022-10-16 11:12:02 -0700158
159// Packs the provided message pointed to by context into the provided buffer.
160// This is equivalent to PackMessage, but doesn't require allocating a
161// FlatBufferBuilder underneath.
162size_t PackMessageInline(uint8_t *data, const Context &contex,
Austin Schuh71a40d42023-02-04 21:22:22 -0800163 int channel_index, LogType log_type, size_t start_byte,
164 size_t end_byte);
Austin Schuhfa30c352022-10-16 11:12:02 -0700165
Austin Schuh05b70472020-01-01 17:11:17 -0800166// Class to read chunks out of a log file.
167class SpanReader {
168 public:
Austin Schuhcd368422021-11-22 21:23:29 -0800169 SpanReader(std::string_view filename, bool quiet = false);
Austin Schuha36c8902019-12-30 18:07:15 -0800170
Austin Schuh6f3babe2020-01-26 20:34:50 -0800171 std::string_view filename() const { return filename_; }
172
Brian Smarttea913d42021-12-10 15:02:38 -0800173 size_t TotalRead() const { return total_read_; }
174 size_t TotalConsumed() const { return total_consumed_; }
Austin Schuh60e77942022-05-16 17:48:24 -0700175 bool IsIncomplete() const {
176 return is_finished_ && total_consumed_ < total_read_;
177 }
Brian Smarttea913d42021-12-10 15:02:38 -0800178
Austin Schuhcf5f6442021-07-06 10:43:28 -0700179 // Returns a span with the data for the next message from the log file,
180 // including the size. The result is only guarenteed to be valid until
181 // ReadMessage() or PeekMessage() is called again.
Austin Schuh05b70472020-01-01 17:11:17 -0800182 absl::Span<const uint8_t> ReadMessage();
183
Austin Schuhcf5f6442021-07-06 10:43:28 -0700184 // Returns a span with the data for the next message without consuming it.
185 // Multiple calls to PeekMessage return the same data. ReadMessage or
186 // ConsumeMessage must be called to get the next message.
187 absl::Span<const uint8_t> PeekMessage();
188 // Consumes the message so the next call to ReadMessage or PeekMessage returns
189 // new data. This does not invalidate the data.
190 void ConsumeMessage();
191
Austin Schuh05b70472020-01-01 17:11:17 -0800192 private:
193 // TODO(austin): Optimization:
194 // Allocate the 256k blocks like we do today. But, refcount them with
195 // shared_ptr pointed to by the messageheader that is returned. This avoids
196 // the copy. Need to do more benchmarking.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700197 // And (Brian): Consider just mmapping the file and handing out refcounted
198 // pointers into that too.
Austin Schuh05b70472020-01-01 17:11:17 -0800199
200 // Reads a chunk of data into data_. Returns false if no data was read.
201 bool ReadBlock();
202
Austin Schuhc41603c2020-10-11 16:17:37 -0700203 std::string filename_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800204
Brian Silvermanf51499a2020-09-21 12:49:08 -0700205 // File reader and data decoder.
206 std::unique_ptr<DataDecoder> decoder_;
Austin Schuh05b70472020-01-01 17:11:17 -0800207
Brian Silvermanf51499a2020-09-21 12:49:08 -0700208 // Vector to read into.
209 ResizeableBuffer data_;
Austin Schuh05b70472020-01-01 17:11:17 -0800210
211 // Amount of data consumed already in data_.
212 size_t consumed_data_ = 0;
Brian Smarttea913d42021-12-10 15:02:38 -0800213
214 // Accumulates the total volume of bytes read from filename_
215 size_t total_read_ = 0;
216
217 // Accumulates the total volume of read bytes that were 'consumed' into
218 // messages. May be less than total_read_, if the last message (span) is
219 // either truncated or somehow corrupt.
220 size_t total_consumed_ = 0;
221
222 // Reached the end, no more readable messages.
223 bool is_finished_ = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800224};
225
Brian Silvermanfee16972021-09-14 12:06:38 -0700226// Reads the last header from a log file. This handles any duplicate headers
227// that were written.
228std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
229 SpanReader *span_reader);
230std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
231 std::string_view filename);
232// Reads the Nth message from a log file, excluding the header. Note: this
233// doesn't handle duplicate headers.
234std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage(
235 std::string_view filename, size_t n);
236
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700237class UnpackedMessageHeader;
238
Austin Schuh05b70472020-01-01 17:11:17 -0800239// Class which handles reading the header and messages from the log file. This
240// handles any per-file state left before merging below.
241class MessageReader {
242 public:
243 MessageReader(std::string_view filename);
244
Austin Schuh6f3babe2020-01-26 20:34:50 -0800245 std::string_view filename() const { return span_reader_.filename(); }
246
Austin Schuh05b70472020-01-01 17:11:17 -0800247 // Returns the header from the log file.
248 const LogFileHeader *log_file_header() const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700249 return &raw_log_file_header_.message();
250 }
251
252 // Returns the raw data of the header from the log file.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800253 const SizePrefixedFlatbufferVector<LogFileHeader> &raw_log_file_header()
254 const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700255 return raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800256 }
257
258 // Returns the minimum maount of data needed to queue up for sorting before
259 // ware guarenteed to not see data out of order.
260 std::chrono::nanoseconds max_out_of_order_duration() const {
261 return max_out_of_order_duration_;
262 }
263
Austin Schuhcde938c2020-02-02 17:30:07 -0800264 // Returns the newest timestamp read out of the log file.
Austin Schuh05b70472020-01-01 17:11:17 -0800265 monotonic_clock::time_point newest_timestamp() const {
266 return newest_timestamp_;
267 }
268
269 // Returns the next message if there is one.
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700270 std::shared_ptr<UnpackedMessageHeader> ReadMessage();
Austin Schuh05b70472020-01-01 17:11:17 -0800271
272 // The time at which we need to read another chunk from the logfile.
273 monotonic_clock::time_point queue_data_time() const {
274 return newest_timestamp() - max_out_of_order_duration();
275 }
276
Brian Smarttea913d42021-12-10 15:02:38 -0800277 // Flag value setters for testing
278 void set_crash_on_corrupt_message_flag(bool b) {
279 crash_on_corrupt_message_flag_ = b;
280 }
281 void set_ignore_corrupt_messages_flag(bool b) {
282 ignore_corrupt_messages_flag_ = b;
283 }
284
Austin Schuh05b70472020-01-01 17:11:17 -0800285 private:
286 // Log chunk reader.
287 SpanReader span_reader_;
288
Austin Schuh97789fc2020-08-01 14:42:45 -0700289 // Vector holding the raw data for the log file header.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800290 SizePrefixedFlatbufferVector<LogFileHeader> raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800291
292 // Minimum amount of data to queue up for sorting before we are guarenteed
293 // to not see data out of order.
294 std::chrono::nanoseconds max_out_of_order_duration_;
295
296 // Timestamp of the newest message in a channel queue.
297 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Brian Smarttea913d42021-12-10 15:02:38 -0800298
299 // Total volume of verifiable messages from the beginning of the file.
300 // TODO - are message counts also useful?
301 size_t total_verified_before_ = 0;
302
303 // Total volume of messages with corrupted flatbuffer formatting, if any.
304 // Excludes corrupted message content.
305 // TODO - if the layout included something as simple as a CRC (relatively
306 // fast and robust enough) for each span, then corrupted content could be
307 // included in this check.
308 size_t total_corrupted_ = 0;
309
310 // Total volume of verifiable messages intermixed with corrupted messages,
311 // if any. Will be == 0 if total_corrupted_ == 0.
312 size_t total_verified_during_ = 0;
313
314 // Total volume of verifiable messages found after the last corrupted one,
315 // if any. Will be == 0 if total_corrupted_ == 0.
316 size_t total_verified_after_ = 0;
317
318 bool is_corrupted() const { return total_corrupted_ > 0; }
319
320 bool crash_on_corrupt_message_flag_ = true;
321 bool ignore_corrupt_messages_flag_ = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800322};
323
Austin Schuhc41603c2020-10-11 16:17:37 -0700324// A class to seamlessly read messages from a list of part files.
325class PartsMessageReader {
326 public:
327 PartsMessageReader(LogParts log_parts);
328
329 std::string_view filename() const { return message_reader_.filename(); }
330
Austin Schuhd2f96102020-12-01 20:27:29 -0800331 // Returns the LogParts that holds the filenames we are reading.
332 const LogParts &parts() const { return parts_; }
333
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800334 const LogFileHeader *log_file_header() const {
335 return message_reader_.log_file_header();
336 }
337
Austin Schuhc41603c2020-10-11 16:17:37 -0700338 // Returns the minimum amount of data needed to queue up for sorting before
339 // we are guarenteed to not see data out of order.
340 std::chrono::nanoseconds max_out_of_order_duration() const {
341 return message_reader_.max_out_of_order_duration();
342 }
343
344 // Returns the newest timestamp read out of the log file.
345 monotonic_clock::time_point newest_timestamp() const {
346 return newest_timestamp_;
347 }
348
349 // Returns the next message if there is one, or nullopt if we have reached the
350 // end of all the files.
351 // Note: reading the next message may change the max_out_of_order_duration().
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700352 std::shared_ptr<UnpackedMessageHeader> ReadMessage();
Austin Schuhc41603c2020-10-11 16:17:37 -0700353
Austin Schuh48507722021-07-17 17:29:24 -0700354 // Returns the boot count for the requested node, or std::nullopt if we don't
355 // know.
356 std::optional<size_t> boot_count(size_t node_index) const {
357 CHECK_GE(node_index, 0u);
358 CHECK_LT(node_index, boot_counts_.size());
359 return boot_counts_[node_index];
360 }
361
Austin Schuhc41603c2020-10-11 16:17:37 -0700362 private:
363 // Opens the next log and updates message_reader_. Sets done_ if there is
364 // nothing more to do.
365 void NextLog();
Austin Schuh48507722021-07-17 17:29:24 -0700366 void ComputeBootCounts();
Austin Schuhc41603c2020-10-11 16:17:37 -0700367
368 const LogParts parts_;
369 size_t next_part_index_ = 1u;
370 bool done_ = false;
371 MessageReader message_reader_;
Brian Silvermanfee16972021-09-14 12:06:38 -0700372 // We instantiate the next one early, to allow implementations to prefetch.
373 // TODO(Brian): To get optimal performance when downloading, this needs more
374 // communication with the implementation to prioritize the next part and add
375 // more parallelism when it helps. Maybe some kind of a queue of parts in
376 // order, and the implementation gets to pull however many make sense off the
377 // front?
378 std::optional<MessageReader> next_message_reader_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700379
Austin Schuh315b96b2020-12-11 21:21:12 -0800380 // True after we have seen a message after the start of the log. The
381 // guarentees on logging essentially are that all data from before the
382 // starting time of the log may be arbitrarily out of order, but once we get
383 // max_out_of_order_duration past the start, everything will remain within
384 // max_out_of_order_duration. We shouldn't see anything before the start
385 // after we've seen a message that is at least max_out_of_order_duration after
386 // the start.
387 bool after_start_ = false;
388
Austin Schuhc41603c2020-10-11 16:17:37 -0700389 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Austin Schuh48507722021-07-17 17:29:24 -0700390
391 // Per node boot counts.
392 std::vector<std::optional<size_t>> boot_counts_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700393};
394
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700395// Stores MessageHeader as a flat header and inline, aligned block of data.
396class UnpackedMessageHeader {
397 public:
James Kuszmaul9776b392023-01-14 14:08:08 -0800398 UnpackedMessageHeader(
399 uint32_t channel_index, monotonic_clock::time_point monotonic_sent_time,
400 realtime_clock::time_point realtime_sent_time, uint32_t queue_index,
401 std::optional<monotonic_clock::time_point> monotonic_remote_time,
402 std::optional<realtime_clock::time_point> realtime_remote_time,
403 std::optional<uint32_t> remote_queue_index,
404 monotonic_clock::time_point monotonic_timestamp_time,
405 bool has_monotonic_timestamp_time, absl::Span<const uint8_t> span)
406 : channel_index(channel_index),
407 monotonic_sent_time(monotonic_sent_time),
408 realtime_sent_time(realtime_sent_time),
409 queue_index(queue_index),
410 monotonic_remote_time(monotonic_remote_time),
411 realtime_remote_time(realtime_remote_time),
412 remote_queue_index(remote_queue_index),
413 monotonic_timestamp_time(monotonic_timestamp_time),
414 has_monotonic_timestamp_time(has_monotonic_timestamp_time),
415 span(span) {}
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700416 UnpackedMessageHeader(const UnpackedMessageHeader &) = delete;
417 UnpackedMessageHeader &operator=(const UnpackedMessageHeader &) = delete;
418
419 // The channel.
420 uint32_t channel_index = 0xffffffff;
421
422 monotonic_clock::time_point monotonic_sent_time;
423 realtime_clock::time_point realtime_sent_time;
424
425 // The local queue index.
426 uint32_t queue_index = 0xffffffff;
427
Austin Schuh826e6ce2021-11-18 20:33:10 -0800428 std::optional<aos::monotonic_clock::time_point> monotonic_remote_time;
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700429
430 std::optional<realtime_clock::time_point> realtime_remote_time;
431 std::optional<uint32_t> remote_queue_index;
432
433 // This field is defaulted in the flatbuffer, so we need to store both the
434 // possibly defaulted value and whether it is defaulted.
435 monotonic_clock::time_point monotonic_timestamp_time;
436 bool has_monotonic_timestamp_time;
437
438 static std::shared_ptr<UnpackedMessageHeader> MakeMessage(
439 const MessageHeader &message);
440
441 // Note: we are storing a span here because we need something to put in the
442 // SharedSpan pointer that RawSender takes. We are using the aliasing
443 // constructor of shared_ptr to avoid the allocation, and it needs a nice
444 // pointer to track.
445 absl::Span<const uint8_t> span;
446
447 char actual_data[];
448
449 private:
450 ~UnpackedMessageHeader() {}
451
452 static void DestroyAndFree(UnpackedMessageHeader *p) {
453 p->~UnpackedMessageHeader();
454 free(p);
455 }
456};
457
458std::ostream &operator<<(std::ostream &os,
459 const UnpackedMessageHeader &message);
460
Austin Schuh1be0ce42020-11-29 22:43:26 -0800461// Struct to hold a message as it gets sorted on a single node.
462struct Message {
463 // The channel.
464 uint32_t channel_index = 0xffffffff;
465 // The local queue index.
Austin Schuh58646e22021-08-23 23:51:46 -0700466 // TODO(austin): Technically the boot inside queue_index is redundant with
467 // timestamp. In practice, it is less error-prone to duplicate it. Maybe a
468 // function to return the combined struct?
469 BootQueueIndex queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700470 // The local timestamp.
471 BootTimestamp timestamp;
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700472
Austin Schuh48507722021-07-17 17:29:24 -0700473 // Remote boot when this is a timestamp.
474 size_t monotonic_remote_boot = 0xffffff;
475
476 size_t monotonic_timestamp_boot = 0xffffff;
477
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700478 std::shared_ptr<UnpackedMessageHeader> data;
Austin Schuh1be0ce42020-11-29 22:43:26 -0800479
480 bool operator<(const Message &m2) const;
481 bool operator>=(const Message &m2) const;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800482 bool operator==(const Message &m2) const;
Austin Schuh1be0ce42020-11-29 22:43:26 -0800483};
484
485std::ostream &operator<<(std::ostream &os, const Message &m);
486
Austin Schuhd2f96102020-12-01 20:27:29 -0800487// Structure to hold a full message and all the timestamps, which may or may not
488// have been sent from a remote node. The remote_queue_index will be invalid if
489// this message is from the point of view of the node which sent it.
490struct TimestampedMessage {
491 uint32_t channel_index = 0xffffffff;
492
Austin Schuh58646e22021-08-23 23:51:46 -0700493 BootQueueIndex queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700494 BootTimestamp monotonic_event_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800495 realtime_clock::time_point realtime_event_time = realtime_clock::min_time;
496
Austin Schuh58646e22021-08-23 23:51:46 -0700497 BootQueueIndex remote_queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700498 BootTimestamp monotonic_remote_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800499 realtime_clock::time_point realtime_remote_time = realtime_clock::min_time;
500
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700501 BootTimestamp monotonic_timestamp_time;
Austin Schuh8bf1e632021-01-02 22:41:04 -0800502
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700503 std::shared_ptr<UnpackedMessageHeader> data;
Austin Schuhd2f96102020-12-01 20:27:29 -0800504};
505
506std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m);
507
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800508// Class to sort the resulting messages from a PartsMessageReader.
509class LogPartsSorter {
510 public:
511 LogPartsSorter(LogParts log_parts);
512
Austin Schuh0ca51f32020-12-25 21:51:45 -0800513 // Returns the parts that this is sorting messages from.
514 const LogParts &parts() const { return parts_message_reader_.parts(); }
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800515
Austin Schuhd2f96102020-12-01 20:27:29 -0800516 monotonic_clock::time_point monotonic_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800517 return parts().monotonic_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800518 }
519 realtime_clock::time_point realtime_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800520 return parts().realtime_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800521 }
522
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800523 // The time this data is sorted until.
524 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
525
526 // Returns the next sorted message from the log file. It is safe to call
527 // std::move() on the result to move the data flatbuffer from it.
528 Message *Front();
529 // Pops the front message. This should only be called after a call to
530 // Front().
531 void PopFront();
532
533 // Returns a debug string representing the contents of this sorter.
534 std::string DebugString() const;
535
536 private:
537 // Log parts reader we are wrapping.
538 PartsMessageReader parts_message_reader_;
539 // Cache of the time we are sorted until.
540 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
541
Austin Schuhb000de62020-12-03 22:00:40 -0800542 // Timestamp of the last message returned. Used to make sure nothing goes
543 // backwards.
544 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
545
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800546 // Set used for efficient sorting of messages. We can benchmark and evaluate
547 // other data structures if this proves to be the bottleneck.
548 absl::btree_set<Message> messages_;
Austin Schuh48507722021-07-17 17:29:24 -0700549
550 // Mapping from channel to source node.
551 // TODO(austin): Should we put this in Boots so it can be cached for everyone?
552 std::vector<size_t> source_node_index_;
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800553};
554
Austin Schuh8f52ed52020-11-30 23:12:39 -0800555// Class to run merge sort on the messages from multiple LogPartsSorter
556// instances.
557class NodeMerger {
558 public:
Austin Schuhd2f96102020-12-01 20:27:29 -0800559 NodeMerger(std::vector<LogParts> parts);
560
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700561 // Copying and moving will mess up the internal raw pointers. Just don't do
562 // it.
563 NodeMerger(NodeMerger const &) = delete;
564 NodeMerger(NodeMerger &&) = delete;
565 void operator=(NodeMerger const &) = delete;
566 void operator=(NodeMerger &&) = delete;
567
Austin Schuhd2f96102020-12-01 20:27:29 -0800568 // Node index in the configuration of this node.
569 int node() const { return node_; }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800570
Austin Schuh0ca51f32020-12-25 21:51:45 -0800571 // List of parts being sorted together.
572 std::vector<const LogParts *> Parts() const;
573
574 const Configuration *configuration() const {
575 return parts_sorters_[0].parts().config.get();
Austin Schuhd2f96102020-12-01 20:27:29 -0800576 }
577
578 monotonic_clock::time_point monotonic_start_time() const {
579 return monotonic_start_time_;
580 }
581 realtime_clock::time_point realtime_start_time() const {
582 return realtime_start_time_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800583 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800584 monotonic_clock::time_point monotonic_oldest_time() const {
585 return monotonic_oldest_time_;
586 }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800587
588 // The time this data is sorted until.
589 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
590
591 // Returns the next sorted message from the set of log files. It is safe to
592 // call std::move() on the result to move the data flatbuffer from it.
593 Message *Front();
594 // Pops the front message. This should only be called after a call to
595 // Front().
596 void PopFront();
597
598 private:
599 // Unsorted list of all parts sorters.
Austin Schuhd2f96102020-12-01 20:27:29 -0800600 std::vector<LogPartsSorter> parts_sorters_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800601 // Pointer to the parts sorter holding the current Front message if one
602 // exists, or nullptr if a new one needs to be found.
603 LogPartsSorter *current_ = nullptr;
604 // Cached sorted_until value.
605 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800606
607 // Cached node.
608 int node_;
609
Austin Schuhb000de62020-12-03 22:00:40 -0800610 // Timestamp of the last message returned. Used to make sure nothing goes
611 // backwards.
612 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
613
Austin Schuhd2f96102020-12-01 20:27:29 -0800614 realtime_clock::time_point realtime_start_time_ = realtime_clock::max_time;
615 monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::max_time;
Austin Schuh60e77942022-05-16 17:48:24 -0700616 monotonic_clock::time_point monotonic_oldest_time_ =
617 monotonic_clock::max_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800618};
619
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700620// Class to concatenate multiple boots worth of logs into a single per-node
621// stream.
622class BootMerger {
623 public:
624 BootMerger(std::vector<LogParts> file);
625
626 // Copying and moving will mess up the internal raw pointers. Just don't do
627 // it.
628 BootMerger(BootMerger const &) = delete;
629 BootMerger(BootMerger &&) = delete;
630 void operator=(BootMerger const &) = delete;
631 void operator=(BootMerger &&) = delete;
632
633 // Node index in the configuration of this node.
634 int node() const { return node_mergers_[0]->node(); }
635
636 // List of parts being sorted together.
637 std::vector<const LogParts *> Parts() const;
638
639 const Configuration *configuration() const {
640 return node_mergers_[0]->configuration();
641 }
642
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700643 monotonic_clock::time_point monotonic_start_time(size_t boot) const {
644 CHECK_LT(boot, node_mergers_.size());
645 return node_mergers_[boot]->monotonic_start_time();
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700646 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700647 realtime_clock::time_point realtime_start_time(size_t boot) const {
648 CHECK_LT(boot, node_mergers_.size());
649 return node_mergers_[boot]->realtime_start_time();
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700650 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800651 monotonic_clock::time_point monotonic_oldest_time(size_t boot) const {
652 CHECK_LT(boot, node_mergers_.size());
653 return node_mergers_[boot]->monotonic_oldest_time();
654 }
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700655
656 bool started() const {
657 return node_mergers_[index_]->sorted_until() != monotonic_clock::min_time ||
658 index_ != 0;
659 }
660
661 // Returns the next sorted message from the set of log files. It is safe to
662 // call std::move() on the result to move the data flatbuffer from it.
663 Message *Front();
664 // Pops the front message. This should only be called after a call to
665 // Front().
666 void PopFront();
667
668 private:
669 int index_ = 0;
670
671 // TODO(austin): Sanjay points out this is pretty inefficient. Don't keep so
672 // many things open.
673 std::vector<std::unique_ptr<NodeMerger>> node_mergers_;
674};
675
Austin Schuhd2f96102020-12-01 20:27:29 -0800676// Class to match timestamps with the corresponding data from other nodes.
Austin Schuh79b30942021-01-24 22:32:21 -0800677//
678// This class also buffers data for the node it represents, and supports
679// notifying when new data is queued as well as queueing until a point in time.
Austin Schuhd2f96102020-12-01 20:27:29 -0800680class TimestampMapper {
681 public:
682 TimestampMapper(std::vector<LogParts> file);
683
684 // Copying and moving will mess up the internal raw pointers. Just don't do
685 // it.
686 TimestampMapper(TimestampMapper const &) = delete;
687 TimestampMapper(TimestampMapper &&) = delete;
688 void operator=(TimestampMapper const &) = delete;
689 void operator=(TimestampMapper &&) = delete;
690
691 // TODO(austin): It would be super helpful to provide a way to queue up to
692 // time X without matching timestamps, and to then be able to pull the
693 // timestamps out of this queue. This lets us bootstrap time estimation
694 // without exploding memory usage worst case.
695
Austin Schuh0ca51f32020-12-25 21:51:45 -0800696 const Configuration *configuration() const { return configuration_.get(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800697
698 // Returns which node this is sorting for.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700699 size_t node() const { return boot_merger_.node(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800700
701 // The start time of this log.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700702 monotonic_clock::time_point monotonic_start_time(size_t boot) const {
703 return boot_merger_.monotonic_start_time(boot);
Austin Schuhd2f96102020-12-01 20:27:29 -0800704 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700705 realtime_clock::time_point realtime_start_time(size_t boot) const {
706 return boot_merger_.realtime_start_time(boot);
Austin Schuhd2f96102020-12-01 20:27:29 -0800707 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800708 // Returns the oldest timestamp on a message on this boot.
709 monotonic_clock::time_point monotonic_oldest_time(size_t boot) const {
710 return boot_merger_.monotonic_oldest_time(boot);
711 }
Austin Schuhd2f96102020-12-01 20:27:29 -0800712
713 // Uses timestamp_mapper as the peer for its node. Only one mapper may be set
714 // for each node. Peers are used to look up the data for timestamps on this
715 // node.
716 void AddPeer(TimestampMapper *timestamp_mapper);
717
Austin Schuh24bf4972021-06-29 22:09:08 -0700718 // Returns true if anything has been queued up.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700719 bool started() const { return boot_merger_.started(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800720
721 // Returns the next message for this node.
722 TimestampedMessage *Front();
723 // Pops the next message. Front must be called first.
724 void PopFront();
725
726 // Returns debug information about this node.
727 std::string DebugString() const;
728
Austin Schuh79b30942021-01-24 22:32:21 -0800729 // Queues data the provided time.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700730 void QueueUntil(BootTimestamp queue_time);
Austin Schuhe639ea12021-01-25 13:00:22 -0800731 // Queues until we have time_estimation_buffer of data in the queue.
732 void QueueFor(std::chrono::nanoseconds time_estimation_buffer);
Austin Schuh79b30942021-01-24 22:32:21 -0800733
Austin Schuh06601222021-01-26 17:02:50 -0800734 // Queues until the condition is met.
735 template <typename T>
736 void QueueUntilCondition(T fn) {
737 while (true) {
738 if (fn()) {
739 break;
740 }
741 if (!QueueMatched()) {
742 break;
743 }
744 }
745 }
746
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700747 // Sets the callback that can be used to skip messages.
748 void set_replay_channels_callback(
749 std::function<bool(const TimestampedMessage &)> fn) {
750 replay_channels_callback_ = fn;
751 }
752
Austin Schuh79b30942021-01-24 22:32:21 -0800753 // Sets a callback to be called whenever a full message is queued.
754 void set_timestamp_callback(std::function<void(TimestampedMessage *)> fn) {
755 timestamp_callback_ = fn;
756 }
757
Austin Schuhd2f96102020-12-01 20:27:29 -0800758 private:
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700759 // Result of MaybeQueueMatched
760 enum class MatchResult : uint8_t {
761 kEndOfFile, // End of the log file being read
762 kQueued, // Message was queued
763 kSkipped // Message was skipped over
764 };
765
Austin Schuhd2f96102020-12-01 20:27:29 -0800766 // The state for a remote node. This holds the data that needs to be matched
767 // with the remote node's timestamps.
768 struct NodeData {
769 // True if we should save data here. This should be true if any of the
770 // bools in delivered below are true.
771 bool any_delivered = false;
772
Austin Schuh36c00932021-07-19 18:13:21 -0700773 // True if we have a peer and therefore should be saving data for it.
774 bool save_for_peer = false;
775
Austin Schuhd2f96102020-12-01 20:27:29 -0800776 // Peer pointer. This node is only to be considered if a peer is set.
777 TimestampMapper *peer = nullptr;
778
779 struct ChannelData {
780 // Deque per channel. This contains the data from the outside
781 // TimestampMapper node which is relevant for the node this NodeData
782 // points to.
783 std::deque<Message> messages;
784 // Bool tracking per channel if a message is delivered to the node this
785 // NodeData represents.
786 bool delivered = false;
Austin Schuh6a7358f2021-11-18 22:40:40 -0800787 // The TTL for delivery.
788 std::chrono::nanoseconds time_to_live = std::chrono::nanoseconds(0);
Austin Schuhd2f96102020-12-01 20:27:29 -0800789 };
790
791 // Vector with per channel data.
792 std::vector<ChannelData> channels;
793 };
794
795 // Returns (and forgets about) the data for the provided timestamp message
796 // showing when it was delivered to this node.
797 Message MatchingMessageFor(const Message &message);
798
799 // Queues up a single message into our message queue, and any nodes that this
800 // message is delivered to. Returns true if one was available, false
801 // otherwise.
802 bool Queue();
803
Austin Schuh79b30942021-01-24 22:32:21 -0800804 // Queues up a single matched message into our matched message queue. Returns
805 // true if one was queued, and false otherwise.
806 bool QueueMatched();
807
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700808 // Queues a message if the replay_channels_callback is passed and the end of
809 // the log file has not been reached.
810 MatchResult MaybeQueueMatched();
811
Austin Schuhd2f96102020-12-01 20:27:29 -0800812 // Queues up data until we have at least one message >= to time t.
813 // Useful for triggering a remote node to read enough data to have the
814 // timestamp you care about available.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700815 void QueueUnmatchedUntil(BootTimestamp t);
Austin Schuhd2f96102020-12-01 20:27:29 -0800816
Austin Schuh79b30942021-01-24 22:32:21 -0800817 // Queues m into matched_messages_.
818 void QueueMessage(Message *m);
Austin Schuhd2f96102020-12-01 20:27:29 -0800819
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700820 // If a replay_channels_callback was set and the callback returns false, a
821 // matched message is popped and true is returned. Otherwise false is
822 // returned.
823 bool CheckReplayChannelsAndMaybePop(const TimestampedMessage &message);
824
Austin Schuh58646e22021-08-23 23:51:46 -0700825 // Returns the name of the node this class is sorting for.
826 std::string_view node_name() const {
827 return configuration_->has_nodes() ? configuration_->nodes()
828 ->Get(boot_merger_.node())
829 ->name()
830 ->string_view()
831 : "(single node)";
832 }
833
Austin Schuhd2f96102020-12-01 20:27:29 -0800834 // The node merger to source messages from.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700835 BootMerger boot_merger_;
Austin Schuh0ca51f32020-12-25 21:51:45 -0800836
837 std::shared_ptr<const Configuration> configuration_;
838
Austin Schuhd2f96102020-12-01 20:27:29 -0800839 // The buffer of messages for this node. These are not matched with any
840 // remote data.
841 std::deque<Message> messages_;
842 // The node index for the source node for each channel.
843 std::vector<size_t> source_node_;
844
845 // Vector per node. Not all nodes will have anything.
846 std::vector<NodeData> nodes_data_;
847
848 // Latest message to return.
Austin Schuh79b30942021-01-24 22:32:21 -0800849 std::deque<TimestampedMessage> matched_messages_;
Austin Schuhd2f96102020-12-01 20:27:29 -0800850
Austin Schuh79b30942021-01-24 22:32:21 -0800851 // Tracks the state of the first message in matched_messages_. Do we need to
852 // update it, is it valid, or should we return nullptr?
Austin Schuhd2f96102020-12-01 20:27:29 -0800853 enum class FirstMessage {
854 kNeedsUpdate,
855 kInMessage,
856 kNullptr,
857 };
858 FirstMessage first_message_ = FirstMessage::kNeedsUpdate;
859
860 // Timestamp of the last message returned. Used to make sure nothing goes
861 // backwards.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700862 BootTimestamp last_message_time_ = BootTimestamp::min_time();
Austin Schuh6a7358f2021-11-18 22:40:40 -0800863 BootTimestamp last_popped_message_time_ = BootTimestamp::min_time();
Austin Schuhd2f96102020-12-01 20:27:29 -0800864 // Time this node is queued up until. Used for caching.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700865 BootTimestamp queued_until_ = BootTimestamp::min_time();
Austin Schuh79b30942021-01-24 22:32:21 -0800866
867 std::function<void(TimestampedMessage *)> timestamp_callback_;
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700868 std::function<bool(TimestampedMessage &)> replay_channels_callback_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800869};
870
Austin Schuhee711052020-08-24 16:06:09 -0700871// Returns the node name with a trailing space, or an empty string if we are on
872// a single node.
873std::string MaybeNodeName(const Node *);
874
Austin Schuh71a40d42023-02-04 21:22:22 -0800875// Class to copy a RemoteMessage into the provided buffer.
876class RemoteMessageCopier : public DataEncoder::Copier {
877 public:
878 RemoteMessageCopier(const message_bridge::RemoteMessage *message,
879 int channel_index,
880 aos::monotonic_clock::time_point monotonic_timestamp_time,
881 EventLoop *event_loop)
882 : DataEncoder::Copier(PackRemoteMessageSize()),
883 message_(message),
884 channel_index_(channel_index),
885 monotonic_timestamp_time_(monotonic_timestamp_time),
886 event_loop_(event_loop) {}
887
888 monotonic_clock::time_point end_time() const { return end_time_; }
889
890 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
891 size_t result = PackRemoteMessageInline(data, message_, channel_index_,
892 monotonic_timestamp_time_,
893 start_byte, end_byte);
894 end_time_ = event_loop_->monotonic_now();
895 return result;
896 }
897
898 private:
899 const message_bridge::RemoteMessage *message_;
900 int channel_index_;
901 aos::monotonic_clock::time_point monotonic_timestamp_time_;
902 EventLoop *event_loop_;
903 monotonic_clock::time_point end_time_;
904};
905
906// Class to copy a context into the provided buffer.
907class ContextDataCopier : public DataEncoder::Copier {
908 public:
909 ContextDataCopier(const Context &context, int channel_index, LogType log_type,
910 EventLoop *event_loop)
911 : DataEncoder::Copier(PackMessageSize(log_type, context.size)),
912 context_(context),
913 channel_index_(channel_index),
914 log_type_(log_type),
915 event_loop_(event_loop) {}
916
917 monotonic_clock::time_point end_time() const { return end_time_; }
918
919 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
920 size_t result = PackMessageInline(data, context_, channel_index_, log_type_,
921 start_byte, end_byte);
922 end_time_ = event_loop_->monotonic_now();
923 return result;
924 }
925
926 private:
927 const Context &context_;
928 const int channel_index_;
929 const LogType log_type_;
930 EventLoop *event_loop_;
931 monotonic_clock::time_point end_time_;
932};
933
Brian Silvermanf51499a2020-09-21 12:49:08 -0700934} // namespace aos::logger
Austin Schuha36c8902019-12-30 18:07:15 -0800935
936#endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_