blob: b2a7f5dd573ca7cc660f70cc8c5de52683bdd8e3 [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"
Brian Silvermanf51499a2020-09-21 12:49:08 -070021#include "aos/events/logging/buffer_encoder.h"
Austin Schuhc41603c2020-10-11 16:17:37 -070022#include "aos/events/logging/logfile_sorting.h"
Austin Schuha36c8902019-12-30 18:07:15 -080023#include "aos/events/logging/logger_generated.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070024#include "aos/flatbuffers.h"
Austin Schuha36c8902019-12-30 18:07:15 -080025#include "flatbuffers/flatbuffers.h"
26
Brian Silvermanf51499a2020-09-21 12:49:08 -070027namespace aos::logger {
Austin Schuha36c8902019-12-30 18:07:15 -080028
29enum class LogType : uint8_t {
30 // The message originated on this node and should be logged here.
31 kLogMessage,
32 // The message originated on another node, but only the delivery times are
33 // logged here.
34 kLogDeliveryTimeOnly,
35 // The message originated on another node. Log it and the delivery times
36 // together. The message_gateway is responsible for logging any messages
37 // which didn't get delivered.
Austin Schuh6f3babe2020-01-26 20:34:50 -080038 kLogMessageAndDeliveryTime,
39 // The message originated on the other node and should be logged on this node.
40 kLogRemoteMessage
Austin Schuha36c8902019-12-30 18:07:15 -080041};
42
Austin Schuha36c8902019-12-30 18:07:15 -080043// This class manages efficiently writing a sequence of detached buffers to a
Brian Silvermanf51499a2020-09-21 12:49:08 -070044// file. It encodes them, queues them up, and batches the write operation.
Austin Schuha36c8902019-12-30 18:07:15 -080045class DetachedBufferWriter {
46 public:
Brian Silvermana9f2ec92020-10-06 18:00:53 -070047 // Marker struct for one of our constructor overloads.
48 struct already_out_of_space_t {};
49
Brian Silvermanf51499a2020-09-21 12:49:08 -070050 DetachedBufferWriter(std::string_view filename,
51 std::unique_ptr<DetachedBufferEncoder> encoder);
Brian Silvermana9f2ec92020-10-06 18:00:53 -070052 // Creates a dummy instance which won't even open a file. It will act as if
53 // opening the file ran out of space immediately.
54 DetachedBufferWriter(already_out_of_space_t) : ran_out_of_space_(true) {}
Austin Schuh2f8fd752020-09-01 22:38:28 -070055 DetachedBufferWriter(DetachedBufferWriter &&other);
56 DetachedBufferWriter(const DetachedBufferWriter &) = delete;
57
Austin Schuha36c8902019-12-30 18:07:15 -080058 ~DetachedBufferWriter();
59
Austin Schuh2f8fd752020-09-01 22:38:28 -070060 DetachedBufferWriter &operator=(DetachedBufferWriter &&other);
Brian Silverman98360e22020-04-28 16:51:20 -070061 DetachedBufferWriter &operator=(const DetachedBufferWriter &) = delete;
62
Austin Schuh6f3babe2020-01-26 20:34:50 -080063 std::string_view filename() const { return filename_; }
64
Brian Silvermana9f2ec92020-10-06 18:00:53 -070065 // This will be true until Close() is called, unless the file couldn't be
66 // created due to running out of space.
67 bool is_open() const { return fd_ != -1; }
68
Brian Silvermanf51499a2020-09-21 12:49:08 -070069 // Queues up a finished FlatBufferBuilder to be encoded and written.
70 //
71 // Triggers a flush if there's enough data queued up.
72 //
73 // Steals the detached buffer from it.
Austin Schuhbd06ae42021-03-31 22:48:21 -070074 void QueueSizedFlatbuffer(flatbuffers::FlatBufferBuilder *fbb,
75 aos::monotonic_clock::time_point now) {
76 QueueSizedFlatbuffer(fbb->Release(), now);
Brian Silvermanf51499a2020-09-21 12:49:08 -070077 }
78 // May steal the backing storage of buffer, or may leave it alone.
Austin Schuhbd06ae42021-03-31 22:48:21 -070079 void QueueSizedFlatbuffer(flatbuffers::DetachedBuffer &&buffer,
80 aos::monotonic_clock::time_point now) {
81 QueueSizedFlatbuffer(std::move(buffer));
82 FlushAtThreshold(now);
83 }
84 // Unconditionally queues the buffer.
Brian Silvermanf51499a2020-09-21 12:49:08 -070085 void QueueSizedFlatbuffer(flatbuffers::DetachedBuffer &&buffer) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -070086 if (ran_out_of_space_) {
87 return;
88 }
Brian Silvermanf51499a2020-09-21 12:49:08 -070089 encoder_->Encode(std::move(buffer));
Brian Silvermanf51499a2020-09-21 12:49:08 -070090 }
Austin Schuha36c8902019-12-30 18:07:15 -080091
Brian Silvermanf51499a2020-09-21 12:49:08 -070092 // Queues up data in span. May copy or may write it to disk immediately.
93 void QueueSpan(absl::Span<const uint8_t> span);
Austin Schuha36c8902019-12-30 18:07:15 -080094
Brian Silverman0465fcf2020-09-24 00:29:18 -070095 // Indicates we got ENOSPC when trying to write. After this returns true, no
96 // further data is written.
97 bool ran_out_of_space() const { return ran_out_of_space_; }
98
99 // To avoid silently failing to write logfiles, you must call this before
100 // destruction if ran_out_of_space() is true and the situation has been
101 // handled.
102 void acknowledge_out_of_space() {
103 CHECK(ran_out_of_space_);
104 acknowledge_ran_out_of_space_ = true;
105 }
106
107 // Fully flushes and closes the underlying file now. No additional data may be
108 // enqueued after calling this.
109 //
110 // This will be performed in the destructor automatically.
111 //
112 // Note that this may set ran_out_of_space().
113 void Close();
114
Brian Silvermanf51499a2020-09-21 12:49:08 -0700115 // Returns the total number of bytes written and currently queued.
Austin Schuha426f1f2021-03-31 22:27:41 -0700116 size_t total_bytes() const {
117 if (!encoder_) {
118 return 0;
119 }
120 return encoder_->total_bytes();
121 }
Austin Schuha36c8902019-12-30 18:07:15 -0800122
Brian Silvermanf51499a2020-09-21 12:49:08 -0700123 // The maximum time for a single write call, or 0 if none have been performed.
124 std::chrono::nanoseconds max_write_time() const { return max_write_time_; }
125 // The number of bytes in the longest write call, or -1 if none have been
126 // performed.
127 int max_write_time_bytes() const { return max_write_time_bytes_; }
128 // The number of buffers in the longest write call, or -1 if none have been
129 // performed.
130 int max_write_time_messages() const { return max_write_time_messages_; }
131 // The total time spent in write calls.
132 std::chrono::nanoseconds total_write_time() const {
133 return total_write_time_;
134 }
135 // The total number of writes which have been performed.
136 int total_write_count() const { return total_write_count_; }
137 // The total number of messages which have been written.
138 int total_write_messages() const { return total_write_messages_; }
139 // The total number of bytes which have been written.
140 int total_write_bytes() const { return total_write_bytes_; }
141 void ResetStatistics() {
142 max_write_time_ = std::chrono::nanoseconds::zero();
143 max_write_time_bytes_ = -1;
144 max_write_time_messages_ = -1;
145 total_write_time_ = std::chrono::nanoseconds::zero();
146 total_write_count_ = 0;
147 total_write_messages_ = 0;
148 total_write_bytes_ = 0;
149 }
Brian Silverman98360e22020-04-28 16:51:20 -0700150
Austin Schuha36c8902019-12-30 18:07:15 -0800151 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700152 // Performs a single writev call with as much of the data we have queued up as
153 // possible.
154 //
155 // This will normally take all of the data we have queued up, unless an
156 // encoder has spit out a big enough chunk all at once that we can't manage
157 // all of it.
158 void Flush();
159
Brian Silverman0465fcf2020-09-24 00:29:18 -0700160 // write_return is what write(2) or writev(2) returned. write_size is the
161 // number of bytes we expected it to write.
162 void HandleWriteReturn(ssize_t write_return, size_t write_size);
163
Brian Silvermanf51499a2020-09-21 12:49:08 -0700164 void UpdateStatsForWrite(aos::monotonic_clock::duration duration,
165 ssize_t written, int iovec_size);
166
167 // Flushes data if we've reached the threshold to do that as part of normal
Austin Schuhbd06ae42021-03-31 22:48:21 -0700168 // operation either due to the outstanding queued data, or because we have
169 // passed our flush period. now is the current time to save some CPU grabbing
170 // the current time. It just needs to be close.
171 void FlushAtThreshold(aos::monotonic_clock::time_point now);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700172
Austin Schuh2f8fd752020-09-01 22:38:28 -0700173 std::string filename_;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700174 std::unique_ptr<DetachedBufferEncoder> encoder_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800175
Austin Schuha36c8902019-12-30 18:07:15 -0800176 int fd_ = -1;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700177 bool ran_out_of_space_ = false;
178 bool acknowledge_ran_out_of_space_ = false;
Austin Schuha36c8902019-12-30 18:07:15 -0800179
Austin Schuha36c8902019-12-30 18:07:15 -0800180 // List of iovecs to use with writev. This is a member variable to avoid
181 // churn.
182 std::vector<struct iovec> iovec_;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700183
184 std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero();
185 int max_write_time_bytes_ = -1;
186 int max_write_time_messages_ = -1;
187 std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero();
188 int total_write_count_ = 0;
189 int total_write_messages_ = 0;
190 int total_write_bytes_ = 0;
Austin Schuhbd06ae42021-03-31 22:48:21 -0700191
192 aos::monotonic_clock::time_point last_flush_time_ =
193 aos::monotonic_clock::min_time;
Austin Schuha36c8902019-12-30 18:07:15 -0800194};
195
196// Packes a message pointed to by the context into a MessageHeader.
197flatbuffers::Offset<MessageHeader> PackMessage(
198 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
199 int channel_index, LogType log_type);
200
Austin Schuhadd6eb32020-11-09 21:24:26 -0800201std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
Austin Schuh3bd4c402020-11-06 18:19:06 -0800202 std::string_view filename);
Austin Schuhadd6eb32020-11-09 21:24:26 -0800203std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage(
Austin Schuh3bd4c402020-11-06 18:19:06 -0800204 std::string_view filename, size_t n);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800205
Austin Schuh05b70472020-01-01 17:11:17 -0800206// Class to read chunks out of a log file.
207class SpanReader {
208 public:
209 SpanReader(std::string_view filename);
Austin Schuha36c8902019-12-30 18:07:15 -0800210
Austin Schuh6f3babe2020-01-26 20:34:50 -0800211 std::string_view filename() const { return filename_; }
212
Austin Schuh05b70472020-01-01 17:11:17 -0800213 // Returns a span with the data for a message from the log file, excluding
214 // the size.
215 absl::Span<const uint8_t> ReadMessage();
216
Austin Schuh05b70472020-01-01 17:11:17 -0800217 private:
218 // TODO(austin): Optimization:
219 // Allocate the 256k blocks like we do today. But, refcount them with
220 // shared_ptr pointed to by the messageheader that is returned. This avoids
221 // the copy. Need to do more benchmarking.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700222 // And (Brian): Consider just mmapping the file and handing out refcounted
223 // pointers into that too.
Austin Schuh05b70472020-01-01 17:11:17 -0800224
225 // Reads a chunk of data into data_. Returns false if no data was read.
226 bool ReadBlock();
227
Austin Schuhc41603c2020-10-11 16:17:37 -0700228 std::string filename_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800229
Brian Silvermanf51499a2020-09-21 12:49:08 -0700230 // File reader and data decoder.
231 std::unique_ptr<DataDecoder> decoder_;
Austin Schuh05b70472020-01-01 17:11:17 -0800232
Brian Silvermanf51499a2020-09-21 12:49:08 -0700233 // Vector to read into.
234 ResizeableBuffer data_;
Austin Schuh05b70472020-01-01 17:11:17 -0800235
236 // Amount of data consumed already in data_.
237 size_t consumed_data_ = 0;
Austin Schuh05b70472020-01-01 17:11:17 -0800238};
239
240// Class which handles reading the header and messages from the log file. This
241// handles any per-file state left before merging below.
242class MessageReader {
243 public:
244 MessageReader(std::string_view filename);
245
Austin Schuh6f3babe2020-01-26 20:34:50 -0800246 std::string_view filename() const { return span_reader_.filename(); }
247
Austin Schuh05b70472020-01-01 17:11:17 -0800248 // Returns the header from the log file.
249 const LogFileHeader *log_file_header() const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700250 return &raw_log_file_header_.message();
251 }
252
253 // Returns the raw data of the header from the log file.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800254 const SizePrefixedFlatbufferVector<LogFileHeader> &raw_log_file_header()
255 const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700256 return raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800257 }
258
259 // Returns the minimum maount of data needed to queue up for sorting before
260 // ware guarenteed to not see data out of order.
261 std::chrono::nanoseconds max_out_of_order_duration() const {
262 return max_out_of_order_duration_;
263 }
264
Austin Schuhcde938c2020-02-02 17:30:07 -0800265 // Returns the newest timestamp read out of the log file.
Austin Schuh05b70472020-01-01 17:11:17 -0800266 monotonic_clock::time_point newest_timestamp() const {
267 return newest_timestamp_;
268 }
269
270 // Returns the next message if there is one.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800271 std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadMessage();
Austin Schuh05b70472020-01-01 17:11:17 -0800272
273 // The time at which we need to read another chunk from the logfile.
274 monotonic_clock::time_point queue_data_time() const {
275 return newest_timestamp() - max_out_of_order_duration();
276 }
277
278 private:
279 // Log chunk reader.
280 SpanReader span_reader_;
281
Austin Schuh97789fc2020-08-01 14:42:45 -0700282 // Vector holding the raw data for the log file header.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800283 SizePrefixedFlatbufferVector<LogFileHeader> raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800284
285 // Minimum amount of data to queue up for sorting before we are guarenteed
286 // to not see data out of order.
287 std::chrono::nanoseconds max_out_of_order_duration_;
288
289 // Timestamp of the newest message in a channel queue.
290 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
291};
292
Austin Schuhc41603c2020-10-11 16:17:37 -0700293// A class to seamlessly read messages from a list of part files.
294class PartsMessageReader {
295 public:
296 PartsMessageReader(LogParts log_parts);
297
298 std::string_view filename() const { return message_reader_.filename(); }
299
Austin Schuhd2f96102020-12-01 20:27:29 -0800300 // Returns the LogParts that holds the filenames we are reading.
301 const LogParts &parts() const { return parts_; }
302
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800303 const LogFileHeader *log_file_header() const {
304 return message_reader_.log_file_header();
305 }
306
Austin Schuhc41603c2020-10-11 16:17:37 -0700307 // Returns the minimum amount of data needed to queue up for sorting before
308 // we are guarenteed to not see data out of order.
309 std::chrono::nanoseconds max_out_of_order_duration() const {
310 return message_reader_.max_out_of_order_duration();
311 }
312
313 // Returns the newest timestamp read out of the log file.
314 monotonic_clock::time_point newest_timestamp() const {
315 return newest_timestamp_;
316 }
317
318 // Returns the next message if there is one, or nullopt if we have reached the
319 // end of all the files.
320 // Note: reading the next message may change the max_out_of_order_duration().
Austin Schuhadd6eb32020-11-09 21:24:26 -0800321 std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadMessage();
Austin Schuhc41603c2020-10-11 16:17:37 -0700322
323 private:
324 // Opens the next log and updates message_reader_. Sets done_ if there is
325 // nothing more to do.
326 void NextLog();
327
328 const LogParts parts_;
329 size_t next_part_index_ = 1u;
330 bool done_ = false;
331 MessageReader message_reader_;
332
Austin Schuh315b96b2020-12-11 21:21:12 -0800333 // True after we have seen a message after the start of the log. The
334 // guarentees on logging essentially are that all data from before the
335 // starting time of the log may be arbitrarily out of order, but once we get
336 // max_out_of_order_duration past the start, everything will remain within
337 // max_out_of_order_duration. We shouldn't see anything before the start
338 // after we've seen a message that is at least max_out_of_order_duration after
339 // the start.
340 bool after_start_ = false;
341
Austin Schuhc41603c2020-10-11 16:17:37 -0700342 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
343};
344
Austin Schuh1be0ce42020-11-29 22:43:26 -0800345// Struct to hold a message as it gets sorted on a single node.
346struct Message {
347 // The channel.
348 uint32_t channel_index = 0xffffffff;
349 // The local queue index.
350 uint32_t queue_index = 0xffffffff;
351 // The local timestamp on the monotonic clock.
352 monotonic_clock::time_point timestamp = monotonic_clock::min_time;
353 // The data (either a timestamp header, or a data header).
354 SizePrefixedFlatbufferVector<MessageHeader> data;
355
356 bool operator<(const Message &m2) const;
357 bool operator>=(const Message &m2) const;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800358 bool operator==(const Message &m2) const;
Austin Schuh1be0ce42020-11-29 22:43:26 -0800359};
360
361std::ostream &operator<<(std::ostream &os, const Message &m);
362
Austin Schuhd2f96102020-12-01 20:27:29 -0800363// Structure to hold a full message and all the timestamps, which may or may not
364// have been sent from a remote node. The remote_queue_index will be invalid if
365// this message is from the point of view of the node which sent it.
366struct TimestampedMessage {
367 uint32_t channel_index = 0xffffffff;
368
369 uint32_t queue_index = 0xffffffff;
370 monotonic_clock::time_point monotonic_event_time = monotonic_clock::min_time;
371 realtime_clock::time_point realtime_event_time = realtime_clock::min_time;
372
373 uint32_t remote_queue_index = 0xffffffff;
374 monotonic_clock::time_point monotonic_remote_time = monotonic_clock::min_time;
375 realtime_clock::time_point realtime_remote_time = realtime_clock::min_time;
376
Austin Schuh8bf1e632021-01-02 22:41:04 -0800377 monotonic_clock::time_point monotonic_timestamp_time =
378 monotonic_clock::min_time;
379
Austin Schuhd2f96102020-12-01 20:27:29 -0800380 SizePrefixedFlatbufferVector<MessageHeader> data;
381};
382
383std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m);
384
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800385// Class to sort the resulting messages from a PartsMessageReader.
386class LogPartsSorter {
387 public:
388 LogPartsSorter(LogParts log_parts);
389
Austin Schuh0ca51f32020-12-25 21:51:45 -0800390 // Returns the parts that this is sorting messages from.
391 const LogParts &parts() const { return parts_message_reader_.parts(); }
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800392
Austin Schuhd2f96102020-12-01 20:27:29 -0800393 monotonic_clock::time_point monotonic_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800394 return parts().monotonic_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800395 }
396 realtime_clock::time_point realtime_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800397 return parts().realtime_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800398 }
399
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800400 // The time this data is sorted until.
401 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
402
403 // Returns the next sorted message from the log file. It is safe to call
404 // std::move() on the result to move the data flatbuffer from it.
405 Message *Front();
406 // Pops the front message. This should only be called after a call to
407 // Front().
408 void PopFront();
409
410 // Returns a debug string representing the contents of this sorter.
411 std::string DebugString() const;
412
413 private:
414 // Log parts reader we are wrapping.
415 PartsMessageReader parts_message_reader_;
416 // Cache of the time we are sorted until.
417 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
418
Austin Schuhb000de62020-12-03 22:00:40 -0800419 // Timestamp of the last message returned. Used to make sure nothing goes
420 // backwards.
421 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
422
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800423 // Set used for efficient sorting of messages. We can benchmark and evaluate
424 // other data structures if this proves to be the bottleneck.
425 absl::btree_set<Message> messages_;
426};
427
Austin Schuh8f52ed52020-11-30 23:12:39 -0800428// Class to run merge sort on the messages from multiple LogPartsSorter
429// instances.
430class NodeMerger {
431 public:
Austin Schuhd2f96102020-12-01 20:27:29 -0800432 NodeMerger(std::vector<LogParts> parts);
433
434 // Node index in the configuration of this node.
435 int node() const { return node_; }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800436
Austin Schuh0ca51f32020-12-25 21:51:45 -0800437 // List of parts being sorted together.
438 std::vector<const LogParts *> Parts() const;
439
440 const Configuration *configuration() const {
441 return parts_sorters_[0].parts().config.get();
Austin Schuhd2f96102020-12-01 20:27:29 -0800442 }
443
444 monotonic_clock::time_point monotonic_start_time() const {
445 return monotonic_start_time_;
446 }
447 realtime_clock::time_point realtime_start_time() const {
448 return realtime_start_time_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800449 }
450
451 // The time this data is sorted until.
452 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
453
454 // Returns the next sorted message from the set of log files. It is safe to
455 // call std::move() on the result to move the data flatbuffer from it.
456 Message *Front();
457 // Pops the front message. This should only be called after a call to
458 // Front().
459 void PopFront();
460
461 private:
462 // Unsorted list of all parts sorters.
Austin Schuhd2f96102020-12-01 20:27:29 -0800463 std::vector<LogPartsSorter> parts_sorters_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800464 // Pointer to the parts sorter holding the current Front message if one
465 // exists, or nullptr if a new one needs to be found.
466 LogPartsSorter *current_ = nullptr;
467 // Cached sorted_until value.
468 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800469
470 // Cached node.
471 int node_;
472
Austin Schuhb000de62020-12-03 22:00:40 -0800473 // Timestamp of the last message returned. Used to make sure nothing goes
474 // backwards.
475 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
476
Austin Schuhd2f96102020-12-01 20:27:29 -0800477 realtime_clock::time_point realtime_start_time_ = realtime_clock::max_time;
478 monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::max_time;
479};
480
481// Class to match timestamps with the corresponding data from other nodes.
Austin Schuh79b30942021-01-24 22:32:21 -0800482//
483// This class also buffers data for the node it represents, and supports
484// notifying when new data is queued as well as queueing until a point in time.
Austin Schuhd2f96102020-12-01 20:27:29 -0800485class TimestampMapper {
486 public:
487 TimestampMapper(std::vector<LogParts> file);
488
489 // Copying and moving will mess up the internal raw pointers. Just don't do
490 // it.
491 TimestampMapper(TimestampMapper const &) = delete;
492 TimestampMapper(TimestampMapper &&) = delete;
493 void operator=(TimestampMapper const &) = delete;
494 void operator=(TimestampMapper &&) = delete;
495
496 // TODO(austin): It would be super helpful to provide a way to queue up to
497 // time X without matching timestamps, and to then be able to pull the
498 // timestamps out of this queue. This lets us bootstrap time estimation
499 // without exploding memory usage worst case.
500
Austin Schuh0ca51f32020-12-25 21:51:45 -0800501 std::vector<const LogParts *> Parts() const { return node_merger_.Parts(); }
502
503 const Configuration *configuration() const { return configuration_.get(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800504
505 // Returns which node this is sorting for.
Austin Schuh287d43d2020-12-04 20:19:33 -0800506 size_t node() const { return node_merger_.node(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800507
508 // The start time of this log.
509 monotonic_clock::time_point monotonic_start_time() const {
510 return node_merger_.monotonic_start_time();
511 }
512 realtime_clock::time_point realtime_start_time() const {
513 return node_merger_.realtime_start_time();
514 }
515
516 // Uses timestamp_mapper as the peer for its node. Only one mapper may be set
517 // for each node. Peers are used to look up the data for timestamps on this
518 // node.
519 void AddPeer(TimestampMapper *timestamp_mapper);
520
Austin Schuh24bf4972021-06-29 22:09:08 -0700521 // Returns true if anything has been queued up.
522 bool started() const {
523 return node_merger_.sorted_until() != monotonic_clock::min_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800524 }
525
526 // Returns the next message for this node.
527 TimestampedMessage *Front();
528 // Pops the next message. Front must be called first.
529 void PopFront();
530
531 // Returns debug information about this node.
532 std::string DebugString() const;
533
Austin Schuh79b30942021-01-24 22:32:21 -0800534 // Queues data the provided time.
535 void QueueUntil(monotonic_clock::time_point queue_time);
Austin Schuhe639ea12021-01-25 13:00:22 -0800536 // Queues until we have time_estimation_buffer of data in the queue.
537 void QueueFor(std::chrono::nanoseconds time_estimation_buffer);
Austin Schuh79b30942021-01-24 22:32:21 -0800538
Austin Schuh06601222021-01-26 17:02:50 -0800539 // Queues until the condition is met.
540 template <typename T>
541 void QueueUntilCondition(T fn) {
542 while (true) {
543 if (fn()) {
544 break;
545 }
546 if (!QueueMatched()) {
547 break;
548 }
549 }
550 }
551
Austin Schuh79b30942021-01-24 22:32:21 -0800552 // Sets a callback to be called whenever a full message is queued.
553 void set_timestamp_callback(std::function<void(TimestampedMessage *)> fn) {
554 timestamp_callback_ = fn;
555 }
556
Austin Schuhd2f96102020-12-01 20:27:29 -0800557 private:
558 // The state for a remote node. This holds the data that needs to be matched
559 // with the remote node's timestamps.
560 struct NodeData {
561 // True if we should save data here. This should be true if any of the
562 // bools in delivered below are true.
563 bool any_delivered = false;
564
565 // Peer pointer. This node is only to be considered if a peer is set.
566 TimestampMapper *peer = nullptr;
567
568 struct ChannelData {
569 // Deque per channel. This contains the data from the outside
570 // TimestampMapper node which is relevant for the node this NodeData
571 // points to.
572 std::deque<Message> messages;
573 // Bool tracking per channel if a message is delivered to the node this
574 // NodeData represents.
575 bool delivered = false;
576 };
577
578 // Vector with per channel data.
579 std::vector<ChannelData> channels;
580 };
581
582 // Returns (and forgets about) the data for the provided timestamp message
583 // showing when it was delivered to this node.
584 Message MatchingMessageFor(const Message &message);
585
586 // Queues up a single message into our message queue, and any nodes that this
587 // message is delivered to. Returns true if one was available, false
588 // otherwise.
589 bool Queue();
590
Austin Schuh79b30942021-01-24 22:32:21 -0800591 // Queues up a single matched message into our matched message queue. Returns
592 // true if one was queued, and false otherwise.
593 bool QueueMatched();
594
Austin Schuhd2f96102020-12-01 20:27:29 -0800595 // Queues up data until we have at least one message >= to time t.
596 // Useful for triggering a remote node to read enough data to have the
597 // timestamp you care about available.
Austin Schuh79b30942021-01-24 22:32:21 -0800598 void QueueUnmatchedUntil(monotonic_clock::time_point t);
Austin Schuhd2f96102020-12-01 20:27:29 -0800599
Austin Schuh79b30942021-01-24 22:32:21 -0800600 // Queues m into matched_messages_.
601 void QueueMessage(Message *m);
Austin Schuhd2f96102020-12-01 20:27:29 -0800602
603 // The node merger to source messages from.
604 NodeMerger node_merger_;
Austin Schuh0ca51f32020-12-25 21:51:45 -0800605
606 std::shared_ptr<const Configuration> configuration_;
607
Austin Schuhd2f96102020-12-01 20:27:29 -0800608 // The buffer of messages for this node. These are not matched with any
609 // remote data.
610 std::deque<Message> messages_;
611 // The node index for the source node for each channel.
612 std::vector<size_t> source_node_;
613
614 // Vector per node. Not all nodes will have anything.
615 std::vector<NodeData> nodes_data_;
616
617 // Latest message to return.
Austin Schuh79b30942021-01-24 22:32:21 -0800618 std::deque<TimestampedMessage> matched_messages_;
Austin Schuhd2f96102020-12-01 20:27:29 -0800619
Austin Schuh79b30942021-01-24 22:32:21 -0800620 // Tracks the state of the first message in matched_messages_. Do we need to
621 // update it, is it valid, or should we return nullptr?
Austin Schuhd2f96102020-12-01 20:27:29 -0800622 enum class FirstMessage {
623 kNeedsUpdate,
624 kInMessage,
625 kNullptr,
626 };
627 FirstMessage first_message_ = FirstMessage::kNeedsUpdate;
628
629 // Timestamp of the last message returned. Used to make sure nothing goes
630 // backwards.
631 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
632 // Time this node is queued up until. Used for caching.
633 monotonic_clock::time_point queued_until_ = monotonic_clock::min_time;
Austin Schuh79b30942021-01-24 22:32:21 -0800634
635 std::function<void(TimestampedMessage *)> timestamp_callback_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800636};
637
Austin Schuhee711052020-08-24 16:06:09 -0700638// Returns the node name with a trailing space, or an empty string if we are on
639// a single node.
640std::string MaybeNodeName(const Node *);
641
Brian Silvermanf51499a2020-09-21 12:49:08 -0700642} // namespace aos::logger
Austin Schuha36c8902019-12-30 18:07:15 -0800643
644#endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_