blob: 0bf60a095b728a8677b7e8821f9e53d8b1295d1e [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"
Alexei Strots01395492023-03-20 13:59:56 -070023#include "aos/events/logging/log_backend.h"
Austin Schuhc41603c2020-10-11 16:17:37 -070024#include "aos/events/logging/logfile_sorting.h"
Austin Schuha36c8902019-12-30 18:07:15 -080025#include "aos/events/logging/logger_generated.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070026#include "aos/flatbuffers.h"
Austin Schuhf2d0e682022-10-16 14:20:58 -070027#include "aos/network/remote_message_generated.h"
Austin Schuha36c8902019-12-30 18:07:15 -080028#include "flatbuffers/flatbuffers.h"
29
Brian Silvermanf51499a2020-09-21 12:49:08 -070030namespace aos::logger {
Austin Schuha36c8902019-12-30 18:07:15 -080031
32enum class LogType : uint8_t {
33 // The message originated on this node and should be logged here.
34 kLogMessage,
35 // The message originated on another node, but only the delivery times are
36 // logged here.
37 kLogDeliveryTimeOnly,
38 // The message originated on another node. Log it and the delivery times
39 // together. The message_gateway is responsible for logging any messages
40 // which didn't get delivered.
Austin Schuh6f3babe2020-01-26 20:34:50 -080041 kLogMessageAndDeliveryTime,
42 // The message originated on the other node and should be logged on this node.
43 kLogRemoteMessage
Austin Schuha36c8902019-12-30 18:07:15 -080044};
45
Austin Schuha36c8902019-12-30 18:07:15 -080046// This class manages efficiently writing a sequence of detached buffers to a
Brian Silvermanf51499a2020-09-21 12:49:08 -070047// file. It encodes them, queues them up, and batches the write operation.
Alexei Strots01395492023-03-20 13:59:56 -070048
Austin Schuha36c8902019-12-30 18:07:15 -080049class DetachedBufferWriter {
50 public:
Brian Silvermana9f2ec92020-10-06 18:00:53 -070051 // Marker struct for one of our constructor overloads.
52 struct already_out_of_space_t {};
53
Alexei Strotsbc082d82023-05-03 08:43:42 -070054 DetachedBufferWriter(std::unique_ptr<LogSink> log_sink,
Austin Schuh48d10d62022-10-16 22:19:23 -070055 std::unique_ptr<DataEncoder> encoder);
Brian Silvermana9f2ec92020-10-06 18:00:53 -070056 // Creates a dummy instance which won't even open a file. It will act as if
57 // opening the file ran out of space immediately.
58 DetachedBufferWriter(already_out_of_space_t) : ran_out_of_space_(true) {}
Austin Schuh2f8fd752020-09-01 22:38:28 -070059 DetachedBufferWriter(DetachedBufferWriter &&other);
60 DetachedBufferWriter(const DetachedBufferWriter &) = delete;
61
Austin Schuha36c8902019-12-30 18:07:15 -080062 ~DetachedBufferWriter();
63
Austin Schuh2f8fd752020-09-01 22:38:28 -070064 DetachedBufferWriter &operator=(DetachedBufferWriter &&other);
Brian Silverman98360e22020-04-28 16:51:20 -070065 DetachedBufferWriter &operator=(const DetachedBufferWriter &) = delete;
66
Alexei Strotsbc082d82023-05-03 08:43:42 -070067 std::string_view name() const { return log_sink_->name(); }
Austin Schuh6f3babe2020-01-26 20:34:50 -080068
Brian Silvermana9f2ec92020-10-06 18:00:53 -070069 // This will be true until Close() is called, unless the file couldn't be
70 // created due to running out of space.
Alexei Strotsbc082d82023-05-03 08:43:42 -070071 bool is_open() const { return log_sink_->is_open(); }
Brian Silvermana9f2ec92020-10-06 18:00:53 -070072
Brian Silvermanf51499a2020-09-21 12:49:08 -070073 // Queues up a finished FlatBufferBuilder to be encoded and written.
74 //
75 // Triggers a flush if there's enough data queued up.
76 //
77 // Steals the detached buffer from it.
Austin Schuh48d10d62022-10-16 22:19:23 -070078 void CopyMessage(DataEncoder::Copier *coppier,
79 aos::monotonic_clock::time_point now);
Austin Schuha36c8902019-12-30 18:07:15 -080080
Brian Silverman0465fcf2020-09-24 00:29:18 -070081 // Indicates we got ENOSPC when trying to write. After this returns true, no
82 // further data is written.
83 bool ran_out_of_space() const { return ran_out_of_space_; }
84
85 // To avoid silently failing to write logfiles, you must call this before
86 // destruction if ran_out_of_space() is true and the situation has been
87 // handled.
88 void acknowledge_out_of_space() {
89 CHECK(ran_out_of_space_);
90 acknowledge_ran_out_of_space_ = true;
91 }
92
93 // Fully flushes and closes the underlying file now. No additional data may be
94 // enqueued after calling this.
95 //
96 // This will be performed in the destructor automatically.
97 //
98 // Note that this may set ran_out_of_space().
99 void Close();
100
Brian Silvermanf51499a2020-09-21 12:49:08 -0700101 // Returns the total number of bytes written and currently queued.
Austin Schuha426f1f2021-03-31 22:27:41 -0700102 size_t total_bytes() const {
103 if (!encoder_) {
104 return 0;
105 }
106 return encoder_->total_bytes();
107 }
Austin Schuha36c8902019-12-30 18:07:15 -0800108
Alexei Strotsbc082d82023-05-03 08:43:42 -0700109 WriteStats *WriteStatistics() const { return log_sink_->WriteStatistics(); }
Brian Silverman98360e22020-04-28 16:51:20 -0700110
Austin Schuha36c8902019-12-30 18:07:15 -0800111 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700112 // Performs a single writev call with as much of the data we have queued up as
Austin Schuh8bdfc492023-02-11 12:53:13 -0800113 // possible. now is the time we flushed at, to be recorded in
114 // last_flush_time_.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700115 //
116 // This will normally take all of the data we have queued up, unless an
117 // encoder has spit out a big enough chunk all at once that we can't manage
118 // all of it.
Austin Schuh8bdfc492023-02-11 12:53:13 -0800119 void Flush(aos::monotonic_clock::time_point now);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700120
Brian Silvermanf51499a2020-09-21 12:49:08 -0700121 // Flushes data if we've reached the threshold to do that as part of normal
Austin Schuhbd06ae42021-03-31 22:48:21 -0700122 // operation either due to the outstanding queued data, or because we have
123 // passed our flush period. now is the current time to save some CPU grabbing
124 // the current time. It just needs to be close.
125 void FlushAtThreshold(aos::monotonic_clock::time_point now);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700126
Alexei Strotsbc082d82023-05-03 08:43:42 -0700127 std::unique_ptr<LogSink> log_sink_;
Austin Schuh48d10d62022-10-16 22:19:23 -0700128 std::unique_ptr<DataEncoder> encoder_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800129
Brian Silverman0465fcf2020-09-24 00:29:18 -0700130 bool ran_out_of_space_ = false;
131 bool acknowledge_ran_out_of_space_ = false;
Austin Schuha36c8902019-12-30 18:07:15 -0800132
Austin Schuhbd06ae42021-03-31 22:48:21 -0700133 aos::monotonic_clock::time_point last_flush_time_ =
134 aos::monotonic_clock::min_time;
Austin Schuha36c8902019-12-30 18:07:15 -0800135};
136
Austin Schuhf2d0e682022-10-16 14:20:58 -0700137// Repacks the provided RemoteMessage into fbb.
138flatbuffers::Offset<MessageHeader> PackRemoteMessage(
139 flatbuffers::FlatBufferBuilder *fbb,
140 const message_bridge::RemoteMessage *msg, int channel_index,
141 const aos::monotonic_clock::time_point monotonic_timestamp_time);
142
143constexpr flatbuffers::uoffset_t PackRemoteMessageSize() { return 96u; }
144size_t PackRemoteMessageInline(
145 uint8_t *data, const message_bridge::RemoteMessage *msg, int channel_index,
Austin Schuh71a40d42023-02-04 21:22:22 -0800146 const aos::monotonic_clock::time_point monotonic_timestamp_time,
147 size_t start_byte, size_t end_byte);
Austin Schuhf2d0e682022-10-16 14:20:58 -0700148
Austin Schuha36c8902019-12-30 18:07:15 -0800149// Packes a message pointed to by the context into a MessageHeader.
150flatbuffers::Offset<MessageHeader> PackMessage(
151 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
152 int channel_index, LogType log_type);
153
Austin Schuhfa30c352022-10-16 11:12:02 -0700154// Returns the size that the packed message from PackMessage or
155// PackMessageInline will be.
Austin Schuh48d10d62022-10-16 22:19:23 -0700156flatbuffers::uoffset_t PackMessageSize(LogType log_type, size_t data_size);
Austin Schuhfa30c352022-10-16 11:12:02 -0700157
158// Packs the provided message pointed to by context into the provided buffer.
159// This is equivalent to PackMessage, but doesn't require allocating a
160// FlatBufferBuilder underneath.
161size_t PackMessageInline(uint8_t *data, const Context &contex,
Austin Schuh71a40d42023-02-04 21:22:22 -0800162 int channel_index, LogType log_type, size_t start_byte,
163 size_t end_byte);
Austin Schuhfa30c352022-10-16 11:12:02 -0700164
Austin Schuh05b70472020-01-01 17:11:17 -0800165// Class to read chunks out of a log file.
166class SpanReader {
167 public:
Austin Schuhcd368422021-11-22 21:23:29 -0800168 SpanReader(std::string_view filename, bool quiet = false);
Austin Schuha36c8902019-12-30 18:07:15 -0800169
Austin Schuh6f3babe2020-01-26 20:34:50 -0800170 std::string_view filename() const { return filename_; }
171
Brian Smarttea913d42021-12-10 15:02:38 -0800172 size_t TotalRead() const { return total_read_; }
173 size_t TotalConsumed() const { return total_consumed_; }
Austin Schuh60e77942022-05-16 17:48:24 -0700174 bool IsIncomplete() const {
175 return is_finished_ && total_consumed_ < total_read_;
176 }
Brian Smarttea913d42021-12-10 15:02:38 -0800177
Austin Schuhcf5f6442021-07-06 10:43:28 -0700178 // Returns a span with the data for the next message from the log file,
179 // including the size. The result is only guarenteed to be valid until
180 // ReadMessage() or PeekMessage() is called again.
Austin Schuh05b70472020-01-01 17:11:17 -0800181 absl::Span<const uint8_t> ReadMessage();
182
Austin Schuhcf5f6442021-07-06 10:43:28 -0700183 // Returns a span with the data for the next message without consuming it.
184 // Multiple calls to PeekMessage return the same data. ReadMessage or
185 // ConsumeMessage must be called to get the next message.
186 absl::Span<const uint8_t> PeekMessage();
187 // Consumes the message so the next call to ReadMessage or PeekMessage returns
188 // new data. This does not invalidate the data.
189 void ConsumeMessage();
190
Austin Schuh05b70472020-01-01 17:11:17 -0800191 private:
192 // TODO(austin): Optimization:
193 // Allocate the 256k blocks like we do today. But, refcount them with
194 // shared_ptr pointed to by the messageheader that is returned. This avoids
195 // the copy. Need to do more benchmarking.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700196 // And (Brian): Consider just mmapping the file and handing out refcounted
197 // pointers into that too.
Austin Schuh05b70472020-01-01 17:11:17 -0800198
199 // Reads a chunk of data into data_. Returns false if no data was read.
200 bool ReadBlock();
201
Austin Schuhc41603c2020-10-11 16:17:37 -0700202 std::string filename_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800203
Brian Silvermanf51499a2020-09-21 12:49:08 -0700204 // File reader and data decoder.
205 std::unique_ptr<DataDecoder> decoder_;
Austin Schuh05b70472020-01-01 17:11:17 -0800206
Brian Silvermanf51499a2020-09-21 12:49:08 -0700207 // Vector to read into.
208 ResizeableBuffer data_;
Austin Schuh05b70472020-01-01 17:11:17 -0800209
210 // Amount of data consumed already in data_.
211 size_t consumed_data_ = 0;
Brian Smarttea913d42021-12-10 15:02:38 -0800212
213 // Accumulates the total volume of bytes read from filename_
214 size_t total_read_ = 0;
215
216 // Accumulates the total volume of read bytes that were 'consumed' into
217 // messages. May be less than total_read_, if the last message (span) is
218 // either truncated or somehow corrupt.
219 size_t total_consumed_ = 0;
220
221 // Reached the end, no more readable messages.
222 bool is_finished_ = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800223};
224
Brian Silvermanfee16972021-09-14 12:06:38 -0700225// Reads the last header from a log file. This handles any duplicate headers
226// that were written.
227std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
228 SpanReader *span_reader);
229std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
230 std::string_view filename);
231// Reads the Nth message from a log file, excluding the header. Note: this
232// doesn't handle duplicate headers.
233std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage(
234 std::string_view filename, size_t n);
235
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700236class UnpackedMessageHeader;
237
Austin Schuh05b70472020-01-01 17:11:17 -0800238// Class which handles reading the header and messages from the log file. This
239// handles any per-file state left before merging below.
240class MessageReader {
241 public:
242 MessageReader(std::string_view filename);
243
Austin Schuh6f3babe2020-01-26 20:34:50 -0800244 std::string_view filename() const { return span_reader_.filename(); }
245
Austin Schuh05b70472020-01-01 17:11:17 -0800246 // Returns the header from the log file.
247 const LogFileHeader *log_file_header() const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700248 return &raw_log_file_header_.message();
249 }
250
251 // Returns the raw data of the header from the log file.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800252 const SizePrefixedFlatbufferVector<LogFileHeader> &raw_log_file_header()
253 const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700254 return raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800255 }
256
257 // Returns the minimum maount of data needed to queue up for sorting before
258 // ware guarenteed to not see data out of order.
259 std::chrono::nanoseconds max_out_of_order_duration() const {
260 return max_out_of_order_duration_;
261 }
262
Austin Schuhcde938c2020-02-02 17:30:07 -0800263 // Returns the newest timestamp read out of the log file.
Austin Schuh05b70472020-01-01 17:11:17 -0800264 monotonic_clock::time_point newest_timestamp() const {
265 return newest_timestamp_;
266 }
267
268 // Returns the next message if there is one.
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700269 std::shared_ptr<UnpackedMessageHeader> ReadMessage();
Austin Schuh05b70472020-01-01 17:11:17 -0800270
271 // The time at which we need to read another chunk from the logfile.
272 monotonic_clock::time_point queue_data_time() const {
273 return newest_timestamp() - max_out_of_order_duration();
274 }
275
Brian Smarttea913d42021-12-10 15:02:38 -0800276 // Flag value setters for testing
277 void set_crash_on_corrupt_message_flag(bool b) {
278 crash_on_corrupt_message_flag_ = b;
279 }
280 void set_ignore_corrupt_messages_flag(bool b) {
281 ignore_corrupt_messages_flag_ = b;
282 }
283
Austin Schuh05b70472020-01-01 17:11:17 -0800284 private:
285 // Log chunk reader.
286 SpanReader span_reader_;
287
Austin Schuh97789fc2020-08-01 14:42:45 -0700288 // Vector holding the raw data for the log file header.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800289 SizePrefixedFlatbufferVector<LogFileHeader> raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800290
291 // Minimum amount of data to queue up for sorting before we are guarenteed
292 // to not see data out of order.
293 std::chrono::nanoseconds max_out_of_order_duration_;
294
295 // Timestamp of the newest message in a channel queue.
296 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Brian Smarttea913d42021-12-10 15:02:38 -0800297
298 // Total volume of verifiable messages from the beginning of the file.
299 // TODO - are message counts also useful?
300 size_t total_verified_before_ = 0;
301
302 // Total volume of messages with corrupted flatbuffer formatting, if any.
303 // Excludes corrupted message content.
304 // TODO - if the layout included something as simple as a CRC (relatively
305 // fast and robust enough) for each span, then corrupted content could be
306 // included in this check.
307 size_t total_corrupted_ = 0;
308
309 // Total volume of verifiable messages intermixed with corrupted messages,
310 // if any. Will be == 0 if total_corrupted_ == 0.
311 size_t total_verified_during_ = 0;
312
313 // Total volume of verifiable messages found after the last corrupted one,
314 // if any. Will be == 0 if total_corrupted_ == 0.
315 size_t total_verified_after_ = 0;
316
317 bool is_corrupted() const { return total_corrupted_ > 0; }
318
319 bool crash_on_corrupt_message_flag_ = true;
320 bool ignore_corrupt_messages_flag_ = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800321};
322
Austin Schuhc41603c2020-10-11 16:17:37 -0700323// A class to seamlessly read messages from a list of part files.
324class PartsMessageReader {
325 public:
326 PartsMessageReader(LogParts log_parts);
327
328 std::string_view filename() const { return message_reader_.filename(); }
329
Austin Schuhd2f96102020-12-01 20:27:29 -0800330 // Returns the LogParts that holds the filenames we are reading.
331 const LogParts &parts() const { return parts_; }
332
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800333 const LogFileHeader *log_file_header() const {
334 return message_reader_.log_file_header();
335 }
336
Austin Schuhc41603c2020-10-11 16:17:37 -0700337 // Returns the minimum amount of data needed to queue up for sorting before
338 // we are guarenteed to not see data out of order.
339 std::chrono::nanoseconds max_out_of_order_duration() const {
340 return message_reader_.max_out_of_order_duration();
341 }
342
343 // Returns the newest timestamp read out of the log file.
344 monotonic_clock::time_point newest_timestamp() const {
345 return newest_timestamp_;
346 }
347
348 // Returns the next message if there is one, or nullopt if we have reached the
349 // end of all the files.
350 // Note: reading the next message may change the max_out_of_order_duration().
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700351 std::shared_ptr<UnpackedMessageHeader> ReadMessage();
Austin Schuhc41603c2020-10-11 16:17:37 -0700352
Austin Schuh48507722021-07-17 17:29:24 -0700353 // Returns the boot count for the requested node, or std::nullopt if we don't
354 // know.
355 std::optional<size_t> boot_count(size_t node_index) const {
356 CHECK_GE(node_index, 0u);
357 CHECK_LT(node_index, boot_counts_.size());
358 return boot_counts_[node_index];
359 }
360
Austin Schuhc41603c2020-10-11 16:17:37 -0700361 private:
362 // Opens the next log and updates message_reader_. Sets done_ if there is
363 // nothing more to do.
364 void NextLog();
Austin Schuh48507722021-07-17 17:29:24 -0700365 void ComputeBootCounts();
Austin Schuhc41603c2020-10-11 16:17:37 -0700366
367 const LogParts parts_;
368 size_t next_part_index_ = 1u;
369 bool done_ = false;
370 MessageReader message_reader_;
Brian Silvermanfee16972021-09-14 12:06:38 -0700371 // We instantiate the next one early, to allow implementations to prefetch.
372 // TODO(Brian): To get optimal performance when downloading, this needs more
373 // communication with the implementation to prioritize the next part and add
374 // more parallelism when it helps. Maybe some kind of a queue of parts in
375 // order, and the implementation gets to pull however many make sense off the
376 // front?
377 std::optional<MessageReader> next_message_reader_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700378
Austin Schuh315b96b2020-12-11 21:21:12 -0800379 // True after we have seen a message after the start of the log. The
380 // guarentees on logging essentially are that all data from before the
381 // starting time of the log may be arbitrarily out of order, but once we get
382 // max_out_of_order_duration past the start, everything will remain within
383 // max_out_of_order_duration. We shouldn't see anything before the start
384 // after we've seen a message that is at least max_out_of_order_duration after
385 // the start.
386 bool after_start_ = false;
387
Austin Schuhc41603c2020-10-11 16:17:37 -0700388 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Austin Schuh48507722021-07-17 17:29:24 -0700389
390 // Per node boot counts.
391 std::vector<std::optional<size_t>> boot_counts_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700392};
393
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700394// Stores MessageHeader as a flat header and inline, aligned block of data.
395class UnpackedMessageHeader {
396 public:
James Kuszmaul9776b392023-01-14 14:08:08 -0800397 UnpackedMessageHeader(
398 uint32_t channel_index, monotonic_clock::time_point monotonic_sent_time,
399 realtime_clock::time_point realtime_sent_time, uint32_t queue_index,
400 std::optional<monotonic_clock::time_point> monotonic_remote_time,
401 std::optional<realtime_clock::time_point> realtime_remote_time,
402 std::optional<uint32_t> remote_queue_index,
403 monotonic_clock::time_point monotonic_timestamp_time,
404 bool has_monotonic_timestamp_time, absl::Span<const uint8_t> span)
405 : channel_index(channel_index),
406 monotonic_sent_time(monotonic_sent_time),
407 realtime_sent_time(realtime_sent_time),
408 queue_index(queue_index),
409 monotonic_remote_time(monotonic_remote_time),
410 realtime_remote_time(realtime_remote_time),
411 remote_queue_index(remote_queue_index),
412 monotonic_timestamp_time(monotonic_timestamp_time),
413 has_monotonic_timestamp_time(has_monotonic_timestamp_time),
414 span(span) {}
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700415 UnpackedMessageHeader(const UnpackedMessageHeader &) = delete;
416 UnpackedMessageHeader &operator=(const UnpackedMessageHeader &) = delete;
417
418 // The channel.
419 uint32_t channel_index = 0xffffffff;
420
421 monotonic_clock::time_point monotonic_sent_time;
422 realtime_clock::time_point realtime_sent_time;
423
424 // The local queue index.
425 uint32_t queue_index = 0xffffffff;
426
Austin Schuh826e6ce2021-11-18 20:33:10 -0800427 std::optional<aos::monotonic_clock::time_point> monotonic_remote_time;
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700428
429 std::optional<realtime_clock::time_point> realtime_remote_time;
430 std::optional<uint32_t> remote_queue_index;
431
432 // This field is defaulted in the flatbuffer, so we need to store both the
433 // possibly defaulted value and whether it is defaulted.
434 monotonic_clock::time_point monotonic_timestamp_time;
435 bool has_monotonic_timestamp_time;
436
437 static std::shared_ptr<UnpackedMessageHeader> MakeMessage(
438 const MessageHeader &message);
439
440 // Note: we are storing a span here because we need something to put in the
441 // SharedSpan pointer that RawSender takes. We are using the aliasing
442 // constructor of shared_ptr to avoid the allocation, and it needs a nice
443 // pointer to track.
444 absl::Span<const uint8_t> span;
445
446 char actual_data[];
447
448 private:
449 ~UnpackedMessageHeader() {}
450
451 static void DestroyAndFree(UnpackedMessageHeader *p) {
452 p->~UnpackedMessageHeader();
453 free(p);
454 }
455};
456
457std::ostream &operator<<(std::ostream &os,
458 const UnpackedMessageHeader &message);
459
Austin Schuh1be0ce42020-11-29 22:43:26 -0800460// Struct to hold a message as it gets sorted on a single node.
461struct Message {
462 // The channel.
463 uint32_t channel_index = 0xffffffff;
464 // The local queue index.
Austin Schuh58646e22021-08-23 23:51:46 -0700465 // TODO(austin): Technically the boot inside queue_index is redundant with
466 // timestamp. In practice, it is less error-prone to duplicate it. Maybe a
467 // function to return the combined struct?
468 BootQueueIndex queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700469 // The local timestamp.
470 BootTimestamp timestamp;
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700471
Austin Schuh48507722021-07-17 17:29:24 -0700472 // Remote boot when this is a timestamp.
473 size_t monotonic_remote_boot = 0xffffff;
474
475 size_t monotonic_timestamp_boot = 0xffffff;
476
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700477 std::shared_ptr<UnpackedMessageHeader> data;
Austin Schuh1be0ce42020-11-29 22:43:26 -0800478
479 bool operator<(const Message &m2) const;
480 bool operator>=(const Message &m2) const;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800481 bool operator==(const Message &m2) const;
Austin Schuh1be0ce42020-11-29 22:43:26 -0800482};
483
484std::ostream &operator<<(std::ostream &os, const Message &m);
485
Austin Schuhd2f96102020-12-01 20:27:29 -0800486// Structure to hold a full message and all the timestamps, which may or may not
487// have been sent from a remote node. The remote_queue_index will be invalid if
488// this message is from the point of view of the node which sent it.
489struct TimestampedMessage {
490 uint32_t channel_index = 0xffffffff;
491
Austin Schuh58646e22021-08-23 23:51:46 -0700492 BootQueueIndex queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700493 BootTimestamp monotonic_event_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800494 realtime_clock::time_point realtime_event_time = realtime_clock::min_time;
495
Austin Schuh58646e22021-08-23 23:51:46 -0700496 BootQueueIndex remote_queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700497 BootTimestamp monotonic_remote_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800498 realtime_clock::time_point realtime_remote_time = realtime_clock::min_time;
499
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700500 BootTimestamp monotonic_timestamp_time;
Austin Schuh8bf1e632021-01-02 22:41:04 -0800501
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700502 std::shared_ptr<UnpackedMessageHeader> data;
Austin Schuhd2f96102020-12-01 20:27:29 -0800503};
504
505std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m);
506
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800507// Class to sort the resulting messages from a PartsMessageReader.
508class LogPartsSorter {
509 public:
510 LogPartsSorter(LogParts log_parts);
511
Austin Schuh0ca51f32020-12-25 21:51:45 -0800512 // Returns the parts that this is sorting messages from.
513 const LogParts &parts() const { return parts_message_reader_.parts(); }
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800514
Austin Schuhd2f96102020-12-01 20:27:29 -0800515 monotonic_clock::time_point monotonic_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800516 return parts().monotonic_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800517 }
518 realtime_clock::time_point realtime_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800519 return parts().realtime_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800520 }
521
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800522 // The time this data is sorted until.
523 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
524
525 // Returns the next sorted message from the log file. It is safe to call
526 // std::move() on the result to move the data flatbuffer from it.
527 Message *Front();
528 // Pops the front message. This should only be called after a call to
529 // Front().
530 void PopFront();
531
532 // Returns a debug string representing the contents of this sorter.
533 std::string DebugString() const;
534
535 private:
536 // Log parts reader we are wrapping.
537 PartsMessageReader parts_message_reader_;
538 // Cache of the time we are sorted until.
539 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
540
Austin Schuhb000de62020-12-03 22:00:40 -0800541 // Timestamp of the last message returned. Used to make sure nothing goes
542 // backwards.
543 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
544
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800545 // Set used for efficient sorting of messages. We can benchmark and evaluate
546 // other data structures if this proves to be the bottleneck.
547 absl::btree_set<Message> messages_;
Austin Schuh48507722021-07-17 17:29:24 -0700548
549 // Mapping from channel to source node.
550 // TODO(austin): Should we put this in Boots so it can be cached for everyone?
551 std::vector<size_t> source_node_index_;
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800552};
553
Austin Schuh8f52ed52020-11-30 23:12:39 -0800554// Class to run merge sort on the messages from multiple LogPartsSorter
555// instances.
556class NodeMerger {
557 public:
Austin Schuhd2f96102020-12-01 20:27:29 -0800558 NodeMerger(std::vector<LogParts> parts);
559
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700560 // Copying and moving will mess up the internal raw pointers. Just don't do
561 // it.
562 NodeMerger(NodeMerger const &) = delete;
563 NodeMerger(NodeMerger &&) = delete;
564 void operator=(NodeMerger const &) = delete;
565 void operator=(NodeMerger &&) = delete;
566
Austin Schuhd2f96102020-12-01 20:27:29 -0800567 // Node index in the configuration of this node.
568 int node() const { return node_; }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800569
Austin Schuh0ca51f32020-12-25 21:51:45 -0800570 // List of parts being sorted together.
571 std::vector<const LogParts *> Parts() const;
572
573 const Configuration *configuration() const {
574 return parts_sorters_[0].parts().config.get();
Austin Schuhd2f96102020-12-01 20:27:29 -0800575 }
576
577 monotonic_clock::time_point monotonic_start_time() const {
578 return monotonic_start_time_;
579 }
580 realtime_clock::time_point realtime_start_time() const {
581 return realtime_start_time_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800582 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800583 monotonic_clock::time_point monotonic_oldest_time() const {
584 return monotonic_oldest_time_;
585 }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800586
587 // The time this data is sorted until.
588 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
589
590 // Returns the next sorted message from the set of log files. It is safe to
591 // call std::move() on the result to move the data flatbuffer from it.
592 Message *Front();
593 // Pops the front message. This should only be called after a call to
594 // Front().
595 void PopFront();
596
597 private:
598 // Unsorted list of all parts sorters.
Austin Schuhd2f96102020-12-01 20:27:29 -0800599 std::vector<LogPartsSorter> parts_sorters_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800600 // Pointer to the parts sorter holding the current Front message if one
601 // exists, or nullptr if a new one needs to be found.
602 LogPartsSorter *current_ = nullptr;
603 // Cached sorted_until value.
604 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800605
606 // Cached node.
607 int node_;
608
Austin Schuhb000de62020-12-03 22:00:40 -0800609 // Timestamp of the last message returned. Used to make sure nothing goes
610 // backwards.
611 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
612
Austin Schuhd2f96102020-12-01 20:27:29 -0800613 realtime_clock::time_point realtime_start_time_ = realtime_clock::max_time;
614 monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::max_time;
Austin Schuh60e77942022-05-16 17:48:24 -0700615 monotonic_clock::time_point monotonic_oldest_time_ =
616 monotonic_clock::max_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800617};
618
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700619// Class to concatenate multiple boots worth of logs into a single per-node
620// stream.
621class BootMerger {
622 public:
623 BootMerger(std::vector<LogParts> file);
624
625 // Copying and moving will mess up the internal raw pointers. Just don't do
626 // it.
627 BootMerger(BootMerger const &) = delete;
628 BootMerger(BootMerger &&) = delete;
629 void operator=(BootMerger const &) = delete;
630 void operator=(BootMerger &&) = delete;
631
632 // Node index in the configuration of this node.
633 int node() const { return node_mergers_[0]->node(); }
634
635 // List of parts being sorted together.
636 std::vector<const LogParts *> Parts() const;
637
638 const Configuration *configuration() const {
639 return node_mergers_[0]->configuration();
640 }
641
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700642 monotonic_clock::time_point monotonic_start_time(size_t boot) const {
643 CHECK_LT(boot, node_mergers_.size());
644 return node_mergers_[boot]->monotonic_start_time();
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700645 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700646 realtime_clock::time_point realtime_start_time(size_t boot) const {
647 CHECK_LT(boot, node_mergers_.size());
648 return node_mergers_[boot]->realtime_start_time();
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700649 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800650 monotonic_clock::time_point monotonic_oldest_time(size_t boot) const {
651 CHECK_LT(boot, node_mergers_.size());
652 return node_mergers_[boot]->monotonic_oldest_time();
653 }
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700654
655 bool started() const {
656 return node_mergers_[index_]->sorted_until() != monotonic_clock::min_time ||
657 index_ != 0;
658 }
659
660 // Returns the next sorted message from the set of log files. It is safe to
661 // call std::move() on the result to move the data flatbuffer from it.
662 Message *Front();
663 // Pops the front message. This should only be called after a call to
664 // Front().
665 void PopFront();
666
667 private:
668 int index_ = 0;
669
670 // TODO(austin): Sanjay points out this is pretty inefficient. Don't keep so
671 // many things open.
672 std::vector<std::unique_ptr<NodeMerger>> node_mergers_;
673};
674
Austin Schuhd2f96102020-12-01 20:27:29 -0800675// Class to match timestamps with the corresponding data from other nodes.
Austin Schuh79b30942021-01-24 22:32:21 -0800676//
677// This class also buffers data for the node it represents, and supports
678// notifying when new data is queued as well as queueing until a point in time.
Austin Schuhd2f96102020-12-01 20:27:29 -0800679class TimestampMapper {
680 public:
681 TimestampMapper(std::vector<LogParts> file);
682
683 // Copying and moving will mess up the internal raw pointers. Just don't do
684 // it.
685 TimestampMapper(TimestampMapper const &) = delete;
686 TimestampMapper(TimestampMapper &&) = delete;
687 void operator=(TimestampMapper const &) = delete;
688 void operator=(TimestampMapper &&) = delete;
689
690 // TODO(austin): It would be super helpful to provide a way to queue up to
691 // time X without matching timestamps, and to then be able to pull the
692 // timestamps out of this queue. This lets us bootstrap time estimation
693 // without exploding memory usage worst case.
694
Austin Schuh0ca51f32020-12-25 21:51:45 -0800695 const Configuration *configuration() const { return configuration_.get(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800696
697 // Returns which node this is sorting for.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700698 size_t node() const { return boot_merger_.node(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800699
700 // The start time of this log.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700701 monotonic_clock::time_point monotonic_start_time(size_t boot) const {
702 return boot_merger_.monotonic_start_time(boot);
Austin Schuhd2f96102020-12-01 20:27:29 -0800703 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700704 realtime_clock::time_point realtime_start_time(size_t boot) const {
705 return boot_merger_.realtime_start_time(boot);
Austin Schuhd2f96102020-12-01 20:27:29 -0800706 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800707 // Returns the oldest timestamp on a message on this boot.
708 monotonic_clock::time_point monotonic_oldest_time(size_t boot) const {
709 return boot_merger_.monotonic_oldest_time(boot);
710 }
Austin Schuhd2f96102020-12-01 20:27:29 -0800711
712 // Uses timestamp_mapper as the peer for its node. Only one mapper may be set
713 // for each node. Peers are used to look up the data for timestamps on this
714 // node.
715 void AddPeer(TimestampMapper *timestamp_mapper);
716
Austin Schuh24bf4972021-06-29 22:09:08 -0700717 // Returns true if anything has been queued up.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700718 bool started() const { return boot_merger_.started(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800719
720 // Returns the next message for this node.
721 TimestampedMessage *Front();
722 // Pops the next message. Front must be called first.
723 void PopFront();
724
725 // Returns debug information about this node.
726 std::string DebugString() const;
727
Austin Schuh79b30942021-01-24 22:32:21 -0800728 // Queues data the provided time.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700729 void QueueUntil(BootTimestamp queue_time);
Austin Schuhe639ea12021-01-25 13:00:22 -0800730 // Queues until we have time_estimation_buffer of data in the queue.
731 void QueueFor(std::chrono::nanoseconds time_estimation_buffer);
Austin Schuh79b30942021-01-24 22:32:21 -0800732
Austin Schuh06601222021-01-26 17:02:50 -0800733 // Queues until the condition is met.
734 template <typename T>
735 void QueueUntilCondition(T fn) {
736 while (true) {
737 if (fn()) {
738 break;
739 }
740 if (!QueueMatched()) {
741 break;
742 }
743 }
744 }
745
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700746 // Sets the callback that can be used to skip messages.
747 void set_replay_channels_callback(
748 std::function<bool(const TimestampedMessage &)> fn) {
749 replay_channels_callback_ = fn;
750 }
751
Austin Schuh79b30942021-01-24 22:32:21 -0800752 // Sets a callback to be called whenever a full message is queued.
753 void set_timestamp_callback(std::function<void(TimestampedMessage *)> fn) {
754 timestamp_callback_ = fn;
755 }
756
Austin Schuhd2f96102020-12-01 20:27:29 -0800757 private:
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700758 // Result of MaybeQueueMatched
759 enum class MatchResult : uint8_t {
760 kEndOfFile, // End of the log file being read
761 kQueued, // Message was queued
762 kSkipped // Message was skipped over
763 };
764
Austin Schuhd2f96102020-12-01 20:27:29 -0800765 // The state for a remote node. This holds the data that needs to be matched
766 // with the remote node's timestamps.
767 struct NodeData {
768 // True if we should save data here. This should be true if any of the
769 // bools in delivered below are true.
770 bool any_delivered = false;
771
Austin Schuh36c00932021-07-19 18:13:21 -0700772 // True if we have a peer and therefore should be saving data for it.
773 bool save_for_peer = false;
774
Austin Schuhd2f96102020-12-01 20:27:29 -0800775 // Peer pointer. This node is only to be considered if a peer is set.
776 TimestampMapper *peer = nullptr;
777
778 struct ChannelData {
779 // Deque per channel. This contains the data from the outside
780 // TimestampMapper node which is relevant for the node this NodeData
781 // points to.
782 std::deque<Message> messages;
783 // Bool tracking per channel if a message is delivered to the node this
784 // NodeData represents.
785 bool delivered = false;
Austin Schuh6a7358f2021-11-18 22:40:40 -0800786 // The TTL for delivery.
787 std::chrono::nanoseconds time_to_live = std::chrono::nanoseconds(0);
Austin Schuhd2f96102020-12-01 20:27:29 -0800788 };
789
790 // Vector with per channel data.
791 std::vector<ChannelData> channels;
792 };
793
794 // Returns (and forgets about) the data for the provided timestamp message
795 // showing when it was delivered to this node.
796 Message MatchingMessageFor(const Message &message);
797
798 // Queues up a single message into our message queue, and any nodes that this
799 // message is delivered to. Returns true if one was available, false
800 // otherwise.
801 bool Queue();
802
Austin Schuh79b30942021-01-24 22:32:21 -0800803 // Queues up a single matched message into our matched message queue. Returns
804 // true if one was queued, and false otherwise.
805 bool QueueMatched();
806
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700807 // Queues a message if the replay_channels_callback is passed and the end of
808 // the log file has not been reached.
809 MatchResult MaybeQueueMatched();
810
Austin Schuhd2f96102020-12-01 20:27:29 -0800811 // Queues up data until we have at least one message >= to time t.
812 // Useful for triggering a remote node to read enough data to have the
813 // timestamp you care about available.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700814 void QueueUnmatchedUntil(BootTimestamp t);
Austin Schuhd2f96102020-12-01 20:27:29 -0800815
Austin Schuh79b30942021-01-24 22:32:21 -0800816 // Queues m into matched_messages_.
817 void QueueMessage(Message *m);
Austin Schuhd2f96102020-12-01 20:27:29 -0800818
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700819 // If a replay_channels_callback was set and the callback returns false, a
820 // matched message is popped and true is returned. Otherwise false is
821 // returned.
822 bool CheckReplayChannelsAndMaybePop(const TimestampedMessage &message);
823
Austin Schuh58646e22021-08-23 23:51:46 -0700824 // Returns the name of the node this class is sorting for.
825 std::string_view node_name() const {
826 return configuration_->has_nodes() ? configuration_->nodes()
827 ->Get(boot_merger_.node())
828 ->name()
829 ->string_view()
830 : "(single node)";
831 }
832
Austin Schuhd2f96102020-12-01 20:27:29 -0800833 // The node merger to source messages from.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700834 BootMerger boot_merger_;
Austin Schuh0ca51f32020-12-25 21:51:45 -0800835
836 std::shared_ptr<const Configuration> configuration_;
837
Austin Schuhd2f96102020-12-01 20:27:29 -0800838 // The buffer of messages for this node. These are not matched with any
839 // remote data.
840 std::deque<Message> messages_;
841 // The node index for the source node for each channel.
842 std::vector<size_t> source_node_;
843
844 // Vector per node. Not all nodes will have anything.
845 std::vector<NodeData> nodes_data_;
846
847 // Latest message to return.
Austin Schuh79b30942021-01-24 22:32:21 -0800848 std::deque<TimestampedMessage> matched_messages_;
Austin Schuhd2f96102020-12-01 20:27:29 -0800849
Austin Schuh79b30942021-01-24 22:32:21 -0800850 // Tracks the state of the first message in matched_messages_. Do we need to
851 // update it, is it valid, or should we return nullptr?
Austin Schuhd2f96102020-12-01 20:27:29 -0800852 enum class FirstMessage {
853 kNeedsUpdate,
854 kInMessage,
855 kNullptr,
856 };
857 FirstMessage first_message_ = FirstMessage::kNeedsUpdate;
858
859 // Timestamp of the last message returned. Used to make sure nothing goes
860 // backwards.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700861 BootTimestamp last_message_time_ = BootTimestamp::min_time();
Austin Schuh6a7358f2021-11-18 22:40:40 -0800862 BootTimestamp last_popped_message_time_ = BootTimestamp::min_time();
Austin Schuhd2f96102020-12-01 20:27:29 -0800863 // Time this node is queued up until. Used for caching.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700864 BootTimestamp queued_until_ = BootTimestamp::min_time();
Austin Schuh79b30942021-01-24 22:32:21 -0800865
866 std::function<void(TimestampedMessage *)> timestamp_callback_;
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700867 std::function<bool(TimestampedMessage &)> replay_channels_callback_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800868};
869
Austin Schuhee711052020-08-24 16:06:09 -0700870// Returns the node name with a trailing space, or an empty string if we are on
871// a single node.
872std::string MaybeNodeName(const Node *);
873
Austin Schuh71a40d42023-02-04 21:22:22 -0800874// Class to copy a RemoteMessage into the provided buffer.
875class RemoteMessageCopier : public DataEncoder::Copier {
876 public:
877 RemoteMessageCopier(const message_bridge::RemoteMessage *message,
878 int channel_index,
879 aos::monotonic_clock::time_point monotonic_timestamp_time,
880 EventLoop *event_loop)
881 : DataEncoder::Copier(PackRemoteMessageSize()),
882 message_(message),
883 channel_index_(channel_index),
884 monotonic_timestamp_time_(monotonic_timestamp_time),
885 event_loop_(event_loop) {}
886
887 monotonic_clock::time_point end_time() const { return end_time_; }
888
889 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
890 size_t result = PackRemoteMessageInline(data, message_, channel_index_,
891 monotonic_timestamp_time_,
892 start_byte, end_byte);
893 end_time_ = event_loop_->monotonic_now();
894 return result;
895 }
896
897 private:
898 const message_bridge::RemoteMessage *message_;
899 int channel_index_;
900 aos::monotonic_clock::time_point monotonic_timestamp_time_;
901 EventLoop *event_loop_;
902 monotonic_clock::time_point end_time_;
903};
904
905// Class to copy a context into the provided buffer.
906class ContextDataCopier : public DataEncoder::Copier {
907 public:
908 ContextDataCopier(const Context &context, int channel_index, LogType log_type,
909 EventLoop *event_loop)
910 : DataEncoder::Copier(PackMessageSize(log_type, context.size)),
911 context_(context),
912 channel_index_(channel_index),
913 log_type_(log_type),
914 event_loop_(event_loop) {}
915
916 monotonic_clock::time_point end_time() const { return end_time_; }
917
918 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
919 size_t result = PackMessageInline(data, context_, channel_index_, log_type_,
920 start_byte, end_byte);
921 end_time_ = event_loop_->monotonic_now();
922 return result;
923 }
924
925 private:
926 const Context &context_;
927 const int channel_index_;
928 const LogType log_type_;
929 EventLoop *event_loop_;
930 monotonic_clock::time_point end_time_;
931};
932
Brian Silvermanf51499a2020-09-21 12:49:08 -0700933} // namespace aos::logger
Austin Schuha36c8902019-12-30 18:07:15 -0800934
935#endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_