blob: 0c0ef7fd2c7a4c5c8c1290051244308bf5489ac1 [file] [log] [blame]
Austin Schuha36c8902019-12-30 18:07:15 -08001#ifndef AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_
2#define AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_
3
4#include <sys/uio.h>
5
Austin Schuh97789fc2020-08-01 14:42:45 -07006#include <chrono>
Austin Schuh05b70472020-01-01 17:11:17 -08007#include <deque>
Austin Schuh97789fc2020-08-01 14:42:45 -07008#include <limits>
9#include <memory>
Austin Schuh05b70472020-01-01 17:11:17 -080010#include <optional>
Austin Schuhfa895892020-01-07 20:07:41 -080011#include <string>
Austin Schuha36c8902019-12-30 18:07:15 -080012#include <string_view>
Brian Silverman98360e22020-04-28 16:51:20 -070013#include <tuple>
Austin Schuh97789fc2020-08-01 14:42:45 -070014#include <utility>
Austin Schuha36c8902019-12-30 18:07:15 -080015#include <vector>
16
Austin Schuh4b5c22a2020-11-30 22:58:43 -080017#include "absl/container/btree_set.h"
Austin Schuh05b70472020-01-01 17:11:17 -080018#include "absl/types/span.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070019#include "aos/containers/resizeable_buffer.h"
Austin Schuha36c8902019-12-30 18:07:15 -080020#include "aos/events/event_loop.h"
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070021#include "aos/events/logging/boot_timestamp.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070022#include "aos/events/logging/buffer_encoder.h"
Austin Schuhc41603c2020-10-11 16:17:37 -070023#include "aos/events/logging/logfile_sorting.h"
Austin Schuha36c8902019-12-30 18:07:15 -080024#include "aos/events/logging/logger_generated.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070025#include "aos/flatbuffers.h"
Austin Schuhf2d0e682022-10-16 14:20:58 -070026#include "aos/network/remote_message_generated.h"
Austin Schuha36c8902019-12-30 18:07:15 -080027#include "flatbuffers/flatbuffers.h"
28
Brian Silvermanf51499a2020-09-21 12:49:08 -070029namespace aos::logger {
Austin Schuha36c8902019-12-30 18:07:15 -080030
31enum class LogType : uint8_t {
32 // The message originated on this node and should be logged here.
33 kLogMessage,
34 // The message originated on another node, but only the delivery times are
35 // logged here.
36 kLogDeliveryTimeOnly,
37 // The message originated on another node. Log it and the delivery times
38 // together. The message_gateway is responsible for logging any messages
39 // which didn't get delivered.
Austin Schuh6f3babe2020-01-26 20:34:50 -080040 kLogMessageAndDeliveryTime,
41 // The message originated on the other node and should be logged on this node.
42 kLogRemoteMessage
Austin Schuha36c8902019-12-30 18:07:15 -080043};
44
Austin Schuha36c8902019-12-30 18:07:15 -080045// This class manages efficiently writing a sequence of detached buffers to a
Brian Silvermanf51499a2020-09-21 12:49:08 -070046// file. It encodes them, queues them up, and batches the write operation.
Austin Schuh4c3cdb72023-02-11 15:05:26 -080047//
48// There are a couple over-arching constraints on writing to keep track of.
49// 1) The kernel is both faster and more efficient at writing large, aligned
50// chunks with O_DIRECT set on the file. The alignment needed is specified
51// by kSector and is file system dependent.
52// 2) Not all encoders support generating round multiples of kSector of data.
53// Rather than burden the API for detecting when that is the case, we want
54// DetachedBufferWriter to be as efficient as it can at writing what given.
55// 3) Some files are small and not updated frequently. They need to be
56// flushed or we will lose data on power off. It is most efficient to write
57// as much as we can aligned by kSector and then fall back to the non direct
58// method when it has been flushed.
59// 4) Not all filesystems support O_DIRECT, and different sizes may be optimal
60// for different machines. The defaults should work decently anywhere and
61// be tuneable for faster systems.
Austin Schuha36c8902019-12-30 18:07:15 -080062class DetachedBufferWriter {
63 public:
Brian Silvermana9f2ec92020-10-06 18:00:53 -070064 // Marker struct for one of our constructor overloads.
65 struct already_out_of_space_t {};
66
Austin Schuh4c3cdb72023-02-11 15:05:26 -080067 // Size of an aligned sector used to detect when the data is aligned enough to
68 // use O_DIRECT instead.
69 static constexpr size_t kSector = 512u;
70
Brian Silvermanf51499a2020-09-21 12:49:08 -070071 DetachedBufferWriter(std::string_view filename,
Austin Schuh48d10d62022-10-16 22:19:23 -070072 std::unique_ptr<DataEncoder> encoder);
Brian Silvermana9f2ec92020-10-06 18:00:53 -070073 // Creates a dummy instance which won't even open a file. It will act as if
74 // opening the file ran out of space immediately.
75 DetachedBufferWriter(already_out_of_space_t) : ran_out_of_space_(true) {}
Austin Schuh2f8fd752020-09-01 22:38:28 -070076 DetachedBufferWriter(DetachedBufferWriter &&other);
77 DetachedBufferWriter(const DetachedBufferWriter &) = delete;
78
Austin Schuha36c8902019-12-30 18:07:15 -080079 ~DetachedBufferWriter();
80
Austin Schuh2f8fd752020-09-01 22:38:28 -070081 DetachedBufferWriter &operator=(DetachedBufferWriter &&other);
Brian Silverman98360e22020-04-28 16:51:20 -070082 DetachedBufferWriter &operator=(const DetachedBufferWriter &) = delete;
83
Austin Schuh6f3babe2020-01-26 20:34:50 -080084 std::string_view filename() const { return filename_; }
85
Brian Silvermana9f2ec92020-10-06 18:00:53 -070086 // This will be true until Close() is called, unless the file couldn't be
87 // created due to running out of space.
88 bool is_open() const { return fd_ != -1; }
89
Brian Silvermanf51499a2020-09-21 12:49:08 -070090 // Queues up a finished FlatBufferBuilder to be encoded and written.
91 //
92 // Triggers a flush if there's enough data queued up.
93 //
94 // Steals the detached buffer from it.
Austin Schuh48d10d62022-10-16 22:19:23 -070095 void CopyMessage(DataEncoder::Copier *coppier,
96 aos::monotonic_clock::time_point now);
Austin Schuha36c8902019-12-30 18:07:15 -080097
Brian Silverman0465fcf2020-09-24 00:29:18 -070098 // Indicates we got ENOSPC when trying to write. After this returns true, no
99 // further data is written.
100 bool ran_out_of_space() const { return ran_out_of_space_; }
101
102 // To avoid silently failing to write logfiles, you must call this before
103 // destruction if ran_out_of_space() is true and the situation has been
104 // handled.
105 void acknowledge_out_of_space() {
106 CHECK(ran_out_of_space_);
107 acknowledge_ran_out_of_space_ = true;
108 }
109
110 // Fully flushes and closes the underlying file now. No additional data may be
111 // enqueued after calling this.
112 //
113 // This will be performed in the destructor automatically.
114 //
115 // Note that this may set ran_out_of_space().
116 void Close();
117
Brian Silvermanf51499a2020-09-21 12:49:08 -0700118 // Returns the total number of bytes written and currently queued.
Austin Schuha426f1f2021-03-31 22:27:41 -0700119 size_t total_bytes() const {
120 if (!encoder_) {
121 return 0;
122 }
123 return encoder_->total_bytes();
124 }
Austin Schuha36c8902019-12-30 18:07:15 -0800125
Brian Silvermanf51499a2020-09-21 12:49:08 -0700126 // The maximum time for a single write call, or 0 if none have been performed.
127 std::chrono::nanoseconds max_write_time() const { return max_write_time_; }
128 // The number of bytes in the longest write call, or -1 if none have been
129 // performed.
130 int max_write_time_bytes() const { return max_write_time_bytes_; }
131 // The number of buffers in the longest write call, or -1 if none have been
132 // performed.
133 int max_write_time_messages() const { return max_write_time_messages_; }
134 // The total time spent in write calls.
135 std::chrono::nanoseconds total_write_time() const {
136 return total_write_time_;
137 }
138 // The total number of writes which have been performed.
139 int total_write_count() const { return total_write_count_; }
140 // The total number of messages which have been written.
141 int total_write_messages() const { return total_write_messages_; }
142 // The total number of bytes which have been written.
143 int total_write_bytes() const { return total_write_bytes_; }
144 void ResetStatistics() {
145 max_write_time_ = std::chrono::nanoseconds::zero();
146 max_write_time_bytes_ = -1;
147 max_write_time_messages_ = -1;
148 total_write_time_ = std::chrono::nanoseconds::zero();
149 total_write_count_ = 0;
150 total_write_messages_ = 0;
151 total_write_bytes_ = 0;
152 }
Brian Silverman98360e22020-04-28 16:51:20 -0700153
Austin Schuha36c8902019-12-30 18:07:15 -0800154 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700155 // Performs a single writev call with as much of the data we have queued up as
Austin Schuh8bdfc492023-02-11 12:53:13 -0800156 // possible. now is the time we flushed at, to be recorded in
157 // last_flush_time_.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700158 //
159 // This will normally take all of the data we have queued up, unless an
160 // encoder has spit out a big enough chunk all at once that we can't manage
161 // all of it.
Austin Schuh8bdfc492023-02-11 12:53:13 -0800162 void Flush(aos::monotonic_clock::time_point now);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700163
Brian Silverman0465fcf2020-09-24 00:29:18 -0700164 // write_return is what write(2) or writev(2) returned. write_size is the
165 // number of bytes we expected it to write.
166 void HandleWriteReturn(ssize_t write_return, size_t write_size);
167
Brian Silvermanf51499a2020-09-21 12:49:08 -0700168 void UpdateStatsForWrite(aos::monotonic_clock::duration duration,
169 ssize_t written, int iovec_size);
170
171 // Flushes data if we've reached the threshold to do that as part of normal
Austin Schuhbd06ae42021-03-31 22:48:21 -0700172 // operation either due to the outstanding queued data, or because we have
173 // passed our flush period. now is the current time to save some CPU grabbing
174 // the current time. It just needs to be close.
175 void FlushAtThreshold(aos::monotonic_clock::time_point now);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700176
Austin Schuh4c3cdb72023-02-11 15:05:26 -0800177 // Enables O_DIRECT on the open file if it is supported. Cheap to call if it
178 // is already enabled.
179 void EnableDirect();
180 // Disables O_DIRECT on the open file if it is supported. Cheap to call if it
181 // is already disabld.
182 void DisableDirect();
183
184 // Writes a chunk of iovecs. aligned is true if all the data is kSector byte
185 // aligned and multiples of it in length, and counted_size is the sum of the
186 // sizes of all the chunks of data. Returns the size of data written.
187 size_t WriteV(struct iovec *iovec_data, size_t iovec_size, bool aligned,
188 size_t counted_size);
189
190 bool ODirectEnabled() { return !!(flags_ & O_DIRECT); }
191
Austin Schuh2f8fd752020-09-01 22:38:28 -0700192 std::string filename_;
Austin Schuh48d10d62022-10-16 22:19:23 -0700193 std::unique_ptr<DataEncoder> encoder_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800194
Austin Schuha36c8902019-12-30 18:07:15 -0800195 int fd_ = -1;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700196 bool ran_out_of_space_ = false;
197 bool acknowledge_ran_out_of_space_ = false;
Austin Schuha36c8902019-12-30 18:07:15 -0800198
Austin Schuha36c8902019-12-30 18:07:15 -0800199 // List of iovecs to use with writev. This is a member variable to avoid
200 // churn.
201 std::vector<struct iovec> iovec_;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700202
203 std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero();
204 int max_write_time_bytes_ = -1;
205 int max_write_time_messages_ = -1;
206 std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero();
207 int total_write_count_ = 0;
208 int total_write_messages_ = 0;
209 int total_write_bytes_ = 0;
Austin Schuh4c3cdb72023-02-11 15:05:26 -0800210 int last_synced_bytes_ = 0;
211
212 bool supports_odirect_ = true;
213 int flags_ = 0;
Austin Schuhbd06ae42021-03-31 22:48:21 -0700214
Austin Schuh313d1ba2023-03-24 15:06:30 -0700215 size_t file_written_bytes_ = 0;
216
Austin Schuhbd06ae42021-03-31 22:48:21 -0700217 aos::monotonic_clock::time_point last_flush_time_ =
218 aos::monotonic_clock::min_time;
Austin Schuha36c8902019-12-30 18:07:15 -0800219};
220
Austin Schuhf2d0e682022-10-16 14:20:58 -0700221// Repacks the provided RemoteMessage into fbb.
222flatbuffers::Offset<MessageHeader> PackRemoteMessage(
223 flatbuffers::FlatBufferBuilder *fbb,
224 const message_bridge::RemoteMessage *msg, int channel_index,
225 const aos::monotonic_clock::time_point monotonic_timestamp_time);
226
227constexpr flatbuffers::uoffset_t PackRemoteMessageSize() { return 96u; }
228size_t PackRemoteMessageInline(
229 uint8_t *data, const message_bridge::RemoteMessage *msg, int channel_index,
Austin Schuh71a40d42023-02-04 21:22:22 -0800230 const aos::monotonic_clock::time_point monotonic_timestamp_time,
231 size_t start_byte, size_t end_byte);
Austin Schuhf2d0e682022-10-16 14:20:58 -0700232
Austin Schuha36c8902019-12-30 18:07:15 -0800233// Packes a message pointed to by the context into a MessageHeader.
234flatbuffers::Offset<MessageHeader> PackMessage(
235 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
236 int channel_index, LogType log_type);
237
Austin Schuhfa30c352022-10-16 11:12:02 -0700238// Returns the size that the packed message from PackMessage or
239// PackMessageInline will be.
Austin Schuh48d10d62022-10-16 22:19:23 -0700240flatbuffers::uoffset_t PackMessageSize(LogType log_type, size_t data_size);
Austin Schuhfa30c352022-10-16 11:12:02 -0700241
242// Packs the provided message pointed to by context into the provided buffer.
243// This is equivalent to PackMessage, but doesn't require allocating a
244// FlatBufferBuilder underneath.
245size_t PackMessageInline(uint8_t *data, const Context &contex,
Austin Schuh71a40d42023-02-04 21:22:22 -0800246 int channel_index, LogType log_type, size_t start_byte,
247 size_t end_byte);
Austin Schuhfa30c352022-10-16 11:12:02 -0700248
Austin Schuh05b70472020-01-01 17:11:17 -0800249// Class to read chunks out of a log file.
250class SpanReader {
251 public:
Austin Schuhcd368422021-11-22 21:23:29 -0800252 SpanReader(std::string_view filename, bool quiet = false);
Austin Schuha36c8902019-12-30 18:07:15 -0800253
Austin Schuh6f3babe2020-01-26 20:34:50 -0800254 std::string_view filename() const { return filename_; }
255
Brian Smarttea913d42021-12-10 15:02:38 -0800256 size_t TotalRead() const { return total_read_; }
257 size_t TotalConsumed() const { return total_consumed_; }
Austin Schuh60e77942022-05-16 17:48:24 -0700258 bool IsIncomplete() const {
259 return is_finished_ && total_consumed_ < total_read_;
260 }
Brian Smarttea913d42021-12-10 15:02:38 -0800261
Austin Schuhcf5f6442021-07-06 10:43:28 -0700262 // Returns a span with the data for the next message from the log file,
263 // including the size. The result is only guarenteed to be valid until
264 // ReadMessage() or PeekMessage() is called again.
Austin Schuh05b70472020-01-01 17:11:17 -0800265 absl::Span<const uint8_t> ReadMessage();
266
Austin Schuhcf5f6442021-07-06 10:43:28 -0700267 // Returns a span with the data for the next message without consuming it.
268 // Multiple calls to PeekMessage return the same data. ReadMessage or
269 // ConsumeMessage must be called to get the next message.
270 absl::Span<const uint8_t> PeekMessage();
271 // Consumes the message so the next call to ReadMessage or PeekMessage returns
272 // new data. This does not invalidate the data.
273 void ConsumeMessage();
274
Austin Schuh05b70472020-01-01 17:11:17 -0800275 private:
276 // TODO(austin): Optimization:
277 // Allocate the 256k blocks like we do today. But, refcount them with
278 // shared_ptr pointed to by the messageheader that is returned. This avoids
279 // the copy. Need to do more benchmarking.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700280 // And (Brian): Consider just mmapping the file and handing out refcounted
281 // pointers into that too.
Austin Schuh05b70472020-01-01 17:11:17 -0800282
283 // Reads a chunk of data into data_. Returns false if no data was read.
284 bool ReadBlock();
285
Austin Schuhc41603c2020-10-11 16:17:37 -0700286 std::string filename_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800287
Brian Silvermanf51499a2020-09-21 12:49:08 -0700288 // File reader and data decoder.
289 std::unique_ptr<DataDecoder> decoder_;
Austin Schuh05b70472020-01-01 17:11:17 -0800290
Brian Silvermanf51499a2020-09-21 12:49:08 -0700291 // Vector to read into.
292 ResizeableBuffer data_;
Austin Schuh05b70472020-01-01 17:11:17 -0800293
294 // Amount of data consumed already in data_.
295 size_t consumed_data_ = 0;
Brian Smarttea913d42021-12-10 15:02:38 -0800296
297 // Accumulates the total volume of bytes read from filename_
298 size_t total_read_ = 0;
299
300 // Accumulates the total volume of read bytes that were 'consumed' into
301 // messages. May be less than total_read_, if the last message (span) is
302 // either truncated or somehow corrupt.
303 size_t total_consumed_ = 0;
304
305 // Reached the end, no more readable messages.
306 bool is_finished_ = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800307};
308
Brian Silvermanfee16972021-09-14 12:06:38 -0700309// Reads the last header from a log file. This handles any duplicate headers
310// that were written.
311std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
312 SpanReader *span_reader);
313std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
314 std::string_view filename);
315// Reads the Nth message from a log file, excluding the header. Note: this
316// doesn't handle duplicate headers.
317std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage(
318 std::string_view filename, size_t n);
319
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700320class UnpackedMessageHeader;
321
Austin Schuh05b70472020-01-01 17:11:17 -0800322// Class which handles reading the header and messages from the log file. This
323// handles any per-file state left before merging below.
324class MessageReader {
325 public:
326 MessageReader(std::string_view filename);
327
Austin Schuh6f3babe2020-01-26 20:34:50 -0800328 std::string_view filename() const { return span_reader_.filename(); }
329
Austin Schuh05b70472020-01-01 17:11:17 -0800330 // Returns the header from the log file.
331 const LogFileHeader *log_file_header() const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700332 return &raw_log_file_header_.message();
333 }
334
335 // Returns the raw data of the header from the log file.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800336 const SizePrefixedFlatbufferVector<LogFileHeader> &raw_log_file_header()
337 const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700338 return raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800339 }
340
341 // Returns the minimum maount of data needed to queue up for sorting before
342 // ware guarenteed to not see data out of order.
343 std::chrono::nanoseconds max_out_of_order_duration() const {
344 return max_out_of_order_duration_;
345 }
346
Austin Schuhcde938c2020-02-02 17:30:07 -0800347 // Returns the newest timestamp read out of the log file.
Austin Schuh05b70472020-01-01 17:11:17 -0800348 monotonic_clock::time_point newest_timestamp() const {
349 return newest_timestamp_;
350 }
351
352 // Returns the next message if there is one.
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700353 std::shared_ptr<UnpackedMessageHeader> ReadMessage();
Austin Schuh05b70472020-01-01 17:11:17 -0800354
355 // The time at which we need to read another chunk from the logfile.
356 monotonic_clock::time_point queue_data_time() const {
357 return newest_timestamp() - max_out_of_order_duration();
358 }
359
Brian Smarttea913d42021-12-10 15:02:38 -0800360 // Flag value setters for testing
361 void set_crash_on_corrupt_message_flag(bool b) {
362 crash_on_corrupt_message_flag_ = b;
363 }
364 void set_ignore_corrupt_messages_flag(bool b) {
365 ignore_corrupt_messages_flag_ = b;
366 }
367
Austin Schuh05b70472020-01-01 17:11:17 -0800368 private:
369 // Log chunk reader.
370 SpanReader span_reader_;
371
Austin Schuh97789fc2020-08-01 14:42:45 -0700372 // Vector holding the raw data for the log file header.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800373 SizePrefixedFlatbufferVector<LogFileHeader> raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800374
375 // Minimum amount of data to queue up for sorting before we are guarenteed
376 // to not see data out of order.
377 std::chrono::nanoseconds max_out_of_order_duration_;
378
379 // Timestamp of the newest message in a channel queue.
380 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Brian Smarttea913d42021-12-10 15:02:38 -0800381
382 // Total volume of verifiable messages from the beginning of the file.
383 // TODO - are message counts also useful?
384 size_t total_verified_before_ = 0;
385
386 // Total volume of messages with corrupted flatbuffer formatting, if any.
387 // Excludes corrupted message content.
388 // TODO - if the layout included something as simple as a CRC (relatively
389 // fast and robust enough) for each span, then corrupted content could be
390 // included in this check.
391 size_t total_corrupted_ = 0;
392
393 // Total volume of verifiable messages intermixed with corrupted messages,
394 // if any. Will be == 0 if total_corrupted_ == 0.
395 size_t total_verified_during_ = 0;
396
397 // Total volume of verifiable messages found after the last corrupted one,
398 // if any. Will be == 0 if total_corrupted_ == 0.
399 size_t total_verified_after_ = 0;
400
401 bool is_corrupted() const { return total_corrupted_ > 0; }
402
403 bool crash_on_corrupt_message_flag_ = true;
404 bool ignore_corrupt_messages_flag_ = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800405};
406
Austin Schuhc41603c2020-10-11 16:17:37 -0700407// A class to seamlessly read messages from a list of part files.
408class PartsMessageReader {
409 public:
410 PartsMessageReader(LogParts log_parts);
411
412 std::string_view filename() const { return message_reader_.filename(); }
413
Austin Schuhd2f96102020-12-01 20:27:29 -0800414 // Returns the LogParts that holds the filenames we are reading.
415 const LogParts &parts() const { return parts_; }
416
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800417 const LogFileHeader *log_file_header() const {
418 return message_reader_.log_file_header();
419 }
420
Austin Schuhc41603c2020-10-11 16:17:37 -0700421 // Returns the minimum amount of data needed to queue up for sorting before
422 // we are guarenteed to not see data out of order.
423 std::chrono::nanoseconds max_out_of_order_duration() const {
424 return message_reader_.max_out_of_order_duration();
425 }
426
427 // Returns the newest timestamp read out of the log file.
428 monotonic_clock::time_point newest_timestamp() const {
429 return newest_timestamp_;
430 }
431
432 // Returns the next message if there is one, or nullopt if we have reached the
433 // end of all the files.
434 // Note: reading the next message may change the max_out_of_order_duration().
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700435 std::shared_ptr<UnpackedMessageHeader> ReadMessage();
Austin Schuhc41603c2020-10-11 16:17:37 -0700436
Austin Schuh48507722021-07-17 17:29:24 -0700437 // Returns the boot count for the requested node, or std::nullopt if we don't
438 // know.
439 std::optional<size_t> boot_count(size_t node_index) const {
440 CHECK_GE(node_index, 0u);
441 CHECK_LT(node_index, boot_counts_.size());
442 return boot_counts_[node_index];
443 }
444
Austin Schuhc41603c2020-10-11 16:17:37 -0700445 private:
446 // Opens the next log and updates message_reader_. Sets done_ if there is
447 // nothing more to do.
448 void NextLog();
Austin Schuh48507722021-07-17 17:29:24 -0700449 void ComputeBootCounts();
Austin Schuhc41603c2020-10-11 16:17:37 -0700450
451 const LogParts parts_;
452 size_t next_part_index_ = 1u;
453 bool done_ = false;
454 MessageReader message_reader_;
Brian Silvermanfee16972021-09-14 12:06:38 -0700455 // We instantiate the next one early, to allow implementations to prefetch.
456 // TODO(Brian): To get optimal performance when downloading, this needs more
457 // communication with the implementation to prioritize the next part and add
458 // more parallelism when it helps. Maybe some kind of a queue of parts in
459 // order, and the implementation gets to pull however many make sense off the
460 // front?
461 std::optional<MessageReader> next_message_reader_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700462
Austin Schuh315b96b2020-12-11 21:21:12 -0800463 // True after we have seen a message after the start of the log. The
464 // guarentees on logging essentially are that all data from before the
465 // starting time of the log may be arbitrarily out of order, but once we get
466 // max_out_of_order_duration past the start, everything will remain within
467 // max_out_of_order_duration. We shouldn't see anything before the start
468 // after we've seen a message that is at least max_out_of_order_duration after
469 // the start.
470 bool after_start_ = false;
471
Austin Schuhc41603c2020-10-11 16:17:37 -0700472 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Austin Schuh48507722021-07-17 17:29:24 -0700473
474 // Per node boot counts.
475 std::vector<std::optional<size_t>> boot_counts_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700476};
477
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700478// Stores MessageHeader as a flat header and inline, aligned block of data.
479class UnpackedMessageHeader {
480 public:
James Kuszmaul9776b392023-01-14 14:08:08 -0800481 UnpackedMessageHeader(
482 uint32_t channel_index, monotonic_clock::time_point monotonic_sent_time,
483 realtime_clock::time_point realtime_sent_time, uint32_t queue_index,
484 std::optional<monotonic_clock::time_point> monotonic_remote_time,
485 std::optional<realtime_clock::time_point> realtime_remote_time,
486 std::optional<uint32_t> remote_queue_index,
487 monotonic_clock::time_point monotonic_timestamp_time,
488 bool has_monotonic_timestamp_time, absl::Span<const uint8_t> span)
489 : channel_index(channel_index),
490 monotonic_sent_time(monotonic_sent_time),
491 realtime_sent_time(realtime_sent_time),
492 queue_index(queue_index),
493 monotonic_remote_time(monotonic_remote_time),
494 realtime_remote_time(realtime_remote_time),
495 remote_queue_index(remote_queue_index),
496 monotonic_timestamp_time(monotonic_timestamp_time),
497 has_monotonic_timestamp_time(has_monotonic_timestamp_time),
498 span(span) {}
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700499 UnpackedMessageHeader(const UnpackedMessageHeader &) = delete;
500 UnpackedMessageHeader &operator=(const UnpackedMessageHeader &) = delete;
501
502 // The channel.
503 uint32_t channel_index = 0xffffffff;
504
505 monotonic_clock::time_point monotonic_sent_time;
506 realtime_clock::time_point realtime_sent_time;
507
508 // The local queue index.
509 uint32_t queue_index = 0xffffffff;
510
Austin Schuh826e6ce2021-11-18 20:33:10 -0800511 std::optional<aos::monotonic_clock::time_point> monotonic_remote_time;
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700512
513 std::optional<realtime_clock::time_point> realtime_remote_time;
514 std::optional<uint32_t> remote_queue_index;
515
516 // This field is defaulted in the flatbuffer, so we need to store both the
517 // possibly defaulted value and whether it is defaulted.
518 monotonic_clock::time_point monotonic_timestamp_time;
519 bool has_monotonic_timestamp_time;
520
521 static std::shared_ptr<UnpackedMessageHeader> MakeMessage(
522 const MessageHeader &message);
523
524 // Note: we are storing a span here because we need something to put in the
525 // SharedSpan pointer that RawSender takes. We are using the aliasing
526 // constructor of shared_ptr to avoid the allocation, and it needs a nice
527 // pointer to track.
528 absl::Span<const uint8_t> span;
529
530 char actual_data[];
531
532 private:
533 ~UnpackedMessageHeader() {}
534
535 static void DestroyAndFree(UnpackedMessageHeader *p) {
536 p->~UnpackedMessageHeader();
537 free(p);
538 }
539};
540
541std::ostream &operator<<(std::ostream &os,
542 const UnpackedMessageHeader &message);
543
Austin Schuh1be0ce42020-11-29 22:43:26 -0800544// Struct to hold a message as it gets sorted on a single node.
545struct Message {
546 // The channel.
547 uint32_t channel_index = 0xffffffff;
548 // The local queue index.
Austin Schuh58646e22021-08-23 23:51:46 -0700549 // TODO(austin): Technically the boot inside queue_index is redundant with
550 // timestamp. In practice, it is less error-prone to duplicate it. Maybe a
551 // function to return the combined struct?
552 BootQueueIndex queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700553 // The local timestamp.
554 BootTimestamp timestamp;
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700555
Austin Schuh48507722021-07-17 17:29:24 -0700556 // Remote boot when this is a timestamp.
557 size_t monotonic_remote_boot = 0xffffff;
558
559 size_t monotonic_timestamp_boot = 0xffffff;
560
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700561 std::shared_ptr<UnpackedMessageHeader> data;
Austin Schuh1be0ce42020-11-29 22:43:26 -0800562
563 bool operator<(const Message &m2) const;
564 bool operator>=(const Message &m2) const;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800565 bool operator==(const Message &m2) const;
Austin Schuh1be0ce42020-11-29 22:43:26 -0800566};
567
568std::ostream &operator<<(std::ostream &os, const Message &m);
569
Austin Schuhd2f96102020-12-01 20:27:29 -0800570// Structure to hold a full message and all the timestamps, which may or may not
571// have been sent from a remote node. The remote_queue_index will be invalid if
572// this message is from the point of view of the node which sent it.
573struct TimestampedMessage {
574 uint32_t channel_index = 0xffffffff;
575
Austin Schuh58646e22021-08-23 23:51:46 -0700576 BootQueueIndex queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700577 BootTimestamp monotonic_event_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800578 realtime_clock::time_point realtime_event_time = realtime_clock::min_time;
579
Austin Schuh58646e22021-08-23 23:51:46 -0700580 BootQueueIndex remote_queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700581 BootTimestamp monotonic_remote_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800582 realtime_clock::time_point realtime_remote_time = realtime_clock::min_time;
583
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700584 BootTimestamp monotonic_timestamp_time;
Austin Schuh8bf1e632021-01-02 22:41:04 -0800585
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700586 std::shared_ptr<UnpackedMessageHeader> data;
Austin Schuhd2f96102020-12-01 20:27:29 -0800587};
588
589std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m);
590
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800591// Class to sort the resulting messages from a PartsMessageReader.
592class LogPartsSorter {
593 public:
594 LogPartsSorter(LogParts log_parts);
595
Austin Schuh0ca51f32020-12-25 21:51:45 -0800596 // Returns the parts that this is sorting messages from.
597 const LogParts &parts() const { return parts_message_reader_.parts(); }
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800598
Austin Schuhd2f96102020-12-01 20:27:29 -0800599 monotonic_clock::time_point monotonic_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800600 return parts().monotonic_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800601 }
602 realtime_clock::time_point realtime_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800603 return parts().realtime_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800604 }
605
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800606 // The time this data is sorted until.
607 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
608
609 // Returns the next sorted message from the log file. It is safe to call
610 // std::move() on the result to move the data flatbuffer from it.
611 Message *Front();
612 // Pops the front message. This should only be called after a call to
613 // Front().
614 void PopFront();
615
616 // Returns a debug string representing the contents of this sorter.
617 std::string DebugString() const;
618
619 private:
620 // Log parts reader we are wrapping.
621 PartsMessageReader parts_message_reader_;
622 // Cache of the time we are sorted until.
623 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
624
Austin Schuhb000de62020-12-03 22:00:40 -0800625 // Timestamp of the last message returned. Used to make sure nothing goes
626 // backwards.
627 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
628
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800629 // Set used for efficient sorting of messages. We can benchmark and evaluate
630 // other data structures if this proves to be the bottleneck.
631 absl::btree_set<Message> messages_;
Austin Schuh48507722021-07-17 17:29:24 -0700632
633 // Mapping from channel to source node.
634 // TODO(austin): Should we put this in Boots so it can be cached for everyone?
635 std::vector<size_t> source_node_index_;
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800636};
637
Austin Schuh8f52ed52020-11-30 23:12:39 -0800638// Class to run merge sort on the messages from multiple LogPartsSorter
639// instances.
640class NodeMerger {
641 public:
Austin Schuhd2f96102020-12-01 20:27:29 -0800642 NodeMerger(std::vector<LogParts> parts);
643
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700644 // Copying and moving will mess up the internal raw pointers. Just don't do
645 // it.
646 NodeMerger(NodeMerger const &) = delete;
647 NodeMerger(NodeMerger &&) = delete;
648 void operator=(NodeMerger const &) = delete;
649 void operator=(NodeMerger &&) = delete;
650
Austin Schuhd2f96102020-12-01 20:27:29 -0800651 // Node index in the configuration of this node.
652 int node() const { return node_; }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800653
Austin Schuh0ca51f32020-12-25 21:51:45 -0800654 // List of parts being sorted together.
655 std::vector<const LogParts *> Parts() const;
656
657 const Configuration *configuration() const {
658 return parts_sorters_[0].parts().config.get();
Austin Schuhd2f96102020-12-01 20:27:29 -0800659 }
660
661 monotonic_clock::time_point monotonic_start_time() const {
662 return monotonic_start_time_;
663 }
664 realtime_clock::time_point realtime_start_time() const {
665 return realtime_start_time_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800666 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800667 monotonic_clock::time_point monotonic_oldest_time() const {
668 return monotonic_oldest_time_;
669 }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800670
671 // The time this data is sorted until.
672 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
673
674 // Returns the next sorted message from the set of log files. It is safe to
675 // call std::move() on the result to move the data flatbuffer from it.
676 Message *Front();
677 // Pops the front message. This should only be called after a call to
678 // Front().
679 void PopFront();
680
681 private:
682 // Unsorted list of all parts sorters.
Austin Schuhd2f96102020-12-01 20:27:29 -0800683 std::vector<LogPartsSorter> parts_sorters_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800684 // Pointer to the parts sorter holding the current Front message if one
685 // exists, or nullptr if a new one needs to be found.
686 LogPartsSorter *current_ = nullptr;
687 // Cached sorted_until value.
688 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800689
690 // Cached node.
691 int node_;
692
Austin Schuhb000de62020-12-03 22:00:40 -0800693 // Timestamp of the last message returned. Used to make sure nothing goes
694 // backwards.
695 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
696
Austin Schuhd2f96102020-12-01 20:27:29 -0800697 realtime_clock::time_point realtime_start_time_ = realtime_clock::max_time;
698 monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::max_time;
Austin Schuh60e77942022-05-16 17:48:24 -0700699 monotonic_clock::time_point monotonic_oldest_time_ =
700 monotonic_clock::max_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800701};
702
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700703// Class to concatenate multiple boots worth of logs into a single per-node
704// stream.
705class BootMerger {
706 public:
707 BootMerger(std::vector<LogParts> file);
708
709 // Copying and moving will mess up the internal raw pointers. Just don't do
710 // it.
711 BootMerger(BootMerger const &) = delete;
712 BootMerger(BootMerger &&) = delete;
713 void operator=(BootMerger const &) = delete;
714 void operator=(BootMerger &&) = delete;
715
716 // Node index in the configuration of this node.
717 int node() const { return node_mergers_[0]->node(); }
718
719 // List of parts being sorted together.
720 std::vector<const LogParts *> Parts() const;
721
722 const Configuration *configuration() const {
723 return node_mergers_[0]->configuration();
724 }
725
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700726 monotonic_clock::time_point monotonic_start_time(size_t boot) const {
727 CHECK_LT(boot, node_mergers_.size());
728 return node_mergers_[boot]->monotonic_start_time();
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700729 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700730 realtime_clock::time_point realtime_start_time(size_t boot) const {
731 CHECK_LT(boot, node_mergers_.size());
732 return node_mergers_[boot]->realtime_start_time();
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700733 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800734 monotonic_clock::time_point monotonic_oldest_time(size_t boot) const {
735 CHECK_LT(boot, node_mergers_.size());
736 return node_mergers_[boot]->monotonic_oldest_time();
737 }
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700738
739 bool started() const {
740 return node_mergers_[index_]->sorted_until() != monotonic_clock::min_time ||
741 index_ != 0;
742 }
743
744 // Returns the next sorted message from the set of log files. It is safe to
745 // call std::move() on the result to move the data flatbuffer from it.
746 Message *Front();
747 // Pops the front message. This should only be called after a call to
748 // Front().
749 void PopFront();
750
751 private:
752 int index_ = 0;
753
754 // TODO(austin): Sanjay points out this is pretty inefficient. Don't keep so
755 // many things open.
756 std::vector<std::unique_ptr<NodeMerger>> node_mergers_;
757};
758
Austin Schuhd2f96102020-12-01 20:27:29 -0800759// Class to match timestamps with the corresponding data from other nodes.
Austin Schuh79b30942021-01-24 22:32:21 -0800760//
761// This class also buffers data for the node it represents, and supports
762// notifying when new data is queued as well as queueing until a point in time.
Austin Schuhd2f96102020-12-01 20:27:29 -0800763class TimestampMapper {
764 public:
765 TimestampMapper(std::vector<LogParts> file);
766
767 // Copying and moving will mess up the internal raw pointers. Just don't do
768 // it.
769 TimestampMapper(TimestampMapper const &) = delete;
770 TimestampMapper(TimestampMapper &&) = delete;
771 void operator=(TimestampMapper const &) = delete;
772 void operator=(TimestampMapper &&) = delete;
773
774 // TODO(austin): It would be super helpful to provide a way to queue up to
775 // time X without matching timestamps, and to then be able to pull the
776 // timestamps out of this queue. This lets us bootstrap time estimation
777 // without exploding memory usage worst case.
778
Austin Schuh0ca51f32020-12-25 21:51:45 -0800779 const Configuration *configuration() const { return configuration_.get(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800780
781 // Returns which node this is sorting for.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700782 size_t node() const { return boot_merger_.node(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800783
784 // The start time of this log.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700785 monotonic_clock::time_point monotonic_start_time(size_t boot) const {
786 return boot_merger_.monotonic_start_time(boot);
Austin Schuhd2f96102020-12-01 20:27:29 -0800787 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700788 realtime_clock::time_point realtime_start_time(size_t boot) const {
789 return boot_merger_.realtime_start_time(boot);
Austin Schuhd2f96102020-12-01 20:27:29 -0800790 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800791 // Returns the oldest timestamp on a message on this boot.
792 monotonic_clock::time_point monotonic_oldest_time(size_t boot) const {
793 return boot_merger_.monotonic_oldest_time(boot);
794 }
Austin Schuhd2f96102020-12-01 20:27:29 -0800795
796 // Uses timestamp_mapper as the peer for its node. Only one mapper may be set
797 // for each node. Peers are used to look up the data for timestamps on this
798 // node.
799 void AddPeer(TimestampMapper *timestamp_mapper);
800
Austin Schuh24bf4972021-06-29 22:09:08 -0700801 // Returns true if anything has been queued up.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700802 bool started() const { return boot_merger_.started(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800803
804 // Returns the next message for this node.
805 TimestampedMessage *Front();
806 // Pops the next message. Front must be called first.
807 void PopFront();
808
809 // Returns debug information about this node.
810 std::string DebugString() const;
811
Austin Schuh79b30942021-01-24 22:32:21 -0800812 // Queues data the provided time.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700813 void QueueUntil(BootTimestamp queue_time);
Austin Schuhe639ea12021-01-25 13:00:22 -0800814 // Queues until we have time_estimation_buffer of data in the queue.
815 void QueueFor(std::chrono::nanoseconds time_estimation_buffer);
Austin Schuh79b30942021-01-24 22:32:21 -0800816
Austin Schuh06601222021-01-26 17:02:50 -0800817 // Queues until the condition is met.
818 template <typename T>
819 void QueueUntilCondition(T fn) {
820 while (true) {
821 if (fn()) {
822 break;
823 }
824 if (!QueueMatched()) {
825 break;
826 }
827 }
828 }
829
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700830 // Sets the callback that can be used to skip messages.
831 void set_replay_channels_callback(
832 std::function<bool(const TimestampedMessage &)> fn) {
833 replay_channels_callback_ = fn;
834 }
835
Austin Schuh79b30942021-01-24 22:32:21 -0800836 // Sets a callback to be called whenever a full message is queued.
837 void set_timestamp_callback(std::function<void(TimestampedMessage *)> fn) {
838 timestamp_callback_ = fn;
839 }
840
Austin Schuhd2f96102020-12-01 20:27:29 -0800841 private:
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700842 // Result of MaybeQueueMatched
843 enum class MatchResult : uint8_t {
844 kEndOfFile, // End of the log file being read
845 kQueued, // Message was queued
846 kSkipped // Message was skipped over
847 };
848
Austin Schuhd2f96102020-12-01 20:27:29 -0800849 // The state for a remote node. This holds the data that needs to be matched
850 // with the remote node's timestamps.
851 struct NodeData {
852 // True if we should save data here. This should be true if any of the
853 // bools in delivered below are true.
854 bool any_delivered = false;
855
Austin Schuh36c00932021-07-19 18:13:21 -0700856 // True if we have a peer and therefore should be saving data for it.
857 bool save_for_peer = false;
858
Austin Schuhd2f96102020-12-01 20:27:29 -0800859 // Peer pointer. This node is only to be considered if a peer is set.
860 TimestampMapper *peer = nullptr;
861
862 struct ChannelData {
863 // Deque per channel. This contains the data from the outside
864 // TimestampMapper node which is relevant for the node this NodeData
865 // points to.
866 std::deque<Message> messages;
867 // Bool tracking per channel if a message is delivered to the node this
868 // NodeData represents.
869 bool delivered = false;
Austin Schuh6a7358f2021-11-18 22:40:40 -0800870 // The TTL for delivery.
871 std::chrono::nanoseconds time_to_live = std::chrono::nanoseconds(0);
Austin Schuhd2f96102020-12-01 20:27:29 -0800872 };
873
874 // Vector with per channel data.
875 std::vector<ChannelData> channels;
876 };
877
878 // Returns (and forgets about) the data for the provided timestamp message
879 // showing when it was delivered to this node.
880 Message MatchingMessageFor(const Message &message);
881
882 // Queues up a single message into our message queue, and any nodes that this
883 // message is delivered to. Returns true if one was available, false
884 // otherwise.
885 bool Queue();
886
Austin Schuh79b30942021-01-24 22:32:21 -0800887 // Queues up a single matched message into our matched message queue. Returns
888 // true if one was queued, and false otherwise.
889 bool QueueMatched();
890
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700891 // Queues a message if the replay_channels_callback is passed and the end of
892 // the log file has not been reached.
893 MatchResult MaybeQueueMatched();
894
Austin Schuhd2f96102020-12-01 20:27:29 -0800895 // Queues up data until we have at least one message >= to time t.
896 // Useful for triggering a remote node to read enough data to have the
897 // timestamp you care about available.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700898 void QueueUnmatchedUntil(BootTimestamp t);
Austin Schuhd2f96102020-12-01 20:27:29 -0800899
Austin Schuh79b30942021-01-24 22:32:21 -0800900 // Queues m into matched_messages_.
901 void QueueMessage(Message *m);
Austin Schuhd2f96102020-12-01 20:27:29 -0800902
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700903 // If a replay_channels_callback was set and the callback returns false, a
904 // matched message is popped and true is returned. Otherwise false is
905 // returned.
906 bool CheckReplayChannelsAndMaybePop(const TimestampedMessage &message);
907
Austin Schuh58646e22021-08-23 23:51:46 -0700908 // Returns the name of the node this class is sorting for.
909 std::string_view node_name() const {
910 return configuration_->has_nodes() ? configuration_->nodes()
911 ->Get(boot_merger_.node())
912 ->name()
913 ->string_view()
914 : "(single node)";
915 }
916
Austin Schuhd2f96102020-12-01 20:27:29 -0800917 // The node merger to source messages from.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700918 BootMerger boot_merger_;
Austin Schuh0ca51f32020-12-25 21:51:45 -0800919
920 std::shared_ptr<const Configuration> configuration_;
921
Austin Schuhd2f96102020-12-01 20:27:29 -0800922 // The buffer of messages for this node. These are not matched with any
923 // remote data.
924 std::deque<Message> messages_;
925 // The node index for the source node for each channel.
926 std::vector<size_t> source_node_;
927
928 // Vector per node. Not all nodes will have anything.
929 std::vector<NodeData> nodes_data_;
930
931 // Latest message to return.
Austin Schuh79b30942021-01-24 22:32:21 -0800932 std::deque<TimestampedMessage> matched_messages_;
Austin Schuhd2f96102020-12-01 20:27:29 -0800933
Austin Schuh79b30942021-01-24 22:32:21 -0800934 // Tracks the state of the first message in matched_messages_. Do we need to
935 // update it, is it valid, or should we return nullptr?
Austin Schuhd2f96102020-12-01 20:27:29 -0800936 enum class FirstMessage {
937 kNeedsUpdate,
938 kInMessage,
939 kNullptr,
940 };
941 FirstMessage first_message_ = FirstMessage::kNeedsUpdate;
942
943 // Timestamp of the last message returned. Used to make sure nothing goes
944 // backwards.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700945 BootTimestamp last_message_time_ = BootTimestamp::min_time();
Austin Schuh6a7358f2021-11-18 22:40:40 -0800946 BootTimestamp last_popped_message_time_ = BootTimestamp::min_time();
Austin Schuhd2f96102020-12-01 20:27:29 -0800947 // Time this node is queued up until. Used for caching.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700948 BootTimestamp queued_until_ = BootTimestamp::min_time();
Austin Schuh79b30942021-01-24 22:32:21 -0800949
950 std::function<void(TimestampedMessage *)> timestamp_callback_;
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700951 std::function<bool(TimestampedMessage &)> replay_channels_callback_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800952};
953
Austin Schuhee711052020-08-24 16:06:09 -0700954// Returns the node name with a trailing space, or an empty string if we are on
955// a single node.
956std::string MaybeNodeName(const Node *);
957
Austin Schuh71a40d42023-02-04 21:22:22 -0800958// Class to copy a RemoteMessage into the provided buffer.
959class RemoteMessageCopier : public DataEncoder::Copier {
960 public:
961 RemoteMessageCopier(const message_bridge::RemoteMessage *message,
962 int channel_index,
963 aos::monotonic_clock::time_point monotonic_timestamp_time,
964 EventLoop *event_loop)
965 : DataEncoder::Copier(PackRemoteMessageSize()),
966 message_(message),
967 channel_index_(channel_index),
968 monotonic_timestamp_time_(monotonic_timestamp_time),
969 event_loop_(event_loop) {}
970
971 monotonic_clock::time_point end_time() const { return end_time_; }
972
973 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
974 size_t result = PackRemoteMessageInline(data, message_, channel_index_,
975 monotonic_timestamp_time_,
976 start_byte, end_byte);
977 end_time_ = event_loop_->monotonic_now();
978 return result;
979 }
980
981 private:
982 const message_bridge::RemoteMessage *message_;
983 int channel_index_;
984 aos::monotonic_clock::time_point monotonic_timestamp_time_;
985 EventLoop *event_loop_;
986 monotonic_clock::time_point end_time_;
987};
988
989// Class to copy a context into the provided buffer.
990class ContextDataCopier : public DataEncoder::Copier {
991 public:
992 ContextDataCopier(const Context &context, int channel_index, LogType log_type,
993 EventLoop *event_loop)
994 : DataEncoder::Copier(PackMessageSize(log_type, context.size)),
995 context_(context),
996 channel_index_(channel_index),
997 log_type_(log_type),
998 event_loop_(event_loop) {}
999
1000 monotonic_clock::time_point end_time() const { return end_time_; }
1001
1002 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
1003 size_t result = PackMessageInline(data, context_, channel_index_, log_type_,
1004 start_byte, end_byte);
1005 end_time_ = event_loop_->monotonic_now();
1006 return result;
1007 }
1008
1009 private:
1010 const Context &context_;
1011 const int channel_index_;
1012 const LogType log_type_;
1013 EventLoop *event_loop_;
1014 monotonic_clock::time_point end_time_;
1015};
1016
Brian Silvermanf51499a2020-09-21 12:49:08 -07001017} // namespace aos::logger
Austin Schuha36c8902019-12-30 18:07:15 -08001018
1019#endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_