blob: e5e01758a7319a974289922158f80122e565242e [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 Schuh05b70472020-01-01 17:11:17 -08006#include <deque>
7#include <optional>
Austin Schuhfa895892020-01-07 20:07:41 -08008#include <string>
Austin Schuha36c8902019-12-30 18:07:15 -08009#include <string_view>
10#include <vector>
11
Austin Schuh05b70472020-01-01 17:11:17 -080012#include "absl/types/span.h"
Austin Schuha36c8902019-12-30 18:07:15 -080013#include "aos/events/event_loop.h"
14#include "aos/events/logging/logger_generated.h"
15#include "flatbuffers/flatbuffers.h"
16
17namespace aos {
18namespace logger {
19
20enum class LogType : uint8_t {
21 // The message originated on this node and should be logged here.
22 kLogMessage,
23 // The message originated on another node, but only the delivery times are
24 // logged here.
25 kLogDeliveryTimeOnly,
26 // The message originated on another node. Log it and the delivery times
27 // together. The message_gateway is responsible for logging any messages
28 // which didn't get delivered.
Austin Schuh6f3babe2020-01-26 20:34:50 -080029 kLogMessageAndDeliveryTime,
30 // The message originated on the other node and should be logged on this node.
31 kLogRemoteMessage
Austin Schuha36c8902019-12-30 18:07:15 -080032};
33
Austin Schuha36c8902019-12-30 18:07:15 -080034// This class manages efficiently writing a sequence of detached buffers to a
35// file. It queues them up and batches the write operation.
36class DetachedBufferWriter {
37 public:
38 DetachedBufferWriter(std::string_view filename);
39 ~DetachedBufferWriter();
40
Austin Schuh6f3babe2020-01-26 20:34:50 -080041 std::string_view filename() const { return filename_; }
42
Austin Schuha36c8902019-12-30 18:07:15 -080043 // TODO(austin): Snappy compress the log file if it ends with .snappy!
44
45 // Queues up a finished FlatBufferBuilder to be written. Steals the detached
46 // buffer from it.
47 void QueueSizedFlatbuffer(flatbuffers::FlatBufferBuilder *fbb);
48 // Queues up a detached buffer directly.
49 void QueueSizedFlatbuffer(flatbuffers::DetachedBuffer &&buffer);
Austin Schuhde031b72020-01-10 19:34:41 -080050 // Writes a Span. This is not terribly optimized right now.
51 void WriteSizedFlatbuffer(absl::Span<const uint8_t> span);
Austin Schuha36c8902019-12-30 18:07:15 -080052
53 // Triggers data to be provided to the kernel and written.
54 void Flush();
55
56 private:
Austin Schuh6f3babe2020-01-26 20:34:50 -080057 const std::string filename_;
58
Austin Schuha36c8902019-12-30 18:07:15 -080059 int fd_ = -1;
60
61 // Size of all the data in the queue.
62 size_t queued_size_ = 0;
63
64 // List of buffers to flush.
65 std::vector<flatbuffers::DetachedBuffer> queue_;
66 // List of iovecs to use with writev. This is a member variable to avoid
67 // churn.
68 std::vector<struct iovec> iovec_;
69};
70
71// Packes a message pointed to by the context into a MessageHeader.
72flatbuffers::Offset<MessageHeader> PackMessage(
73 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
74 int channel_index, LogType log_type);
75
Austin Schuh6f3babe2020-01-26 20:34:50 -080076FlatbufferVector<LogFileHeader> ReadHeader(std::string_view filename);
77
Austin Schuh05b70472020-01-01 17:11:17 -080078// Class to read chunks out of a log file.
79class SpanReader {
80 public:
81 SpanReader(std::string_view filename);
Austin Schuha36c8902019-12-30 18:07:15 -080082
Austin Schuh05b70472020-01-01 17:11:17 -080083 ~SpanReader() { close(fd_); }
84
Austin Schuh6f3babe2020-01-26 20:34:50 -080085 std::string_view filename() const { return filename_; }
86
Austin Schuh05b70472020-01-01 17:11:17 -080087 // Returns a span with the data for a message from the log file, excluding
88 // the size.
89 absl::Span<const uint8_t> ReadMessage();
90
91 // Returns true if there is a full message available in the buffer, or if we
92 // will have to read more data from disk.
93 bool MessageAvailable();
94
95 private:
96 // TODO(austin): Optimization:
97 // Allocate the 256k blocks like we do today. But, refcount them with
98 // shared_ptr pointed to by the messageheader that is returned. This avoids
99 // the copy. Need to do more benchmarking.
100
101 // Reads a chunk of data into data_. Returns false if no data was read.
102 bool ReadBlock();
103
Austin Schuh6f3babe2020-01-26 20:34:50 -0800104 const std::string filename_;
105
Austin Schuh05b70472020-01-01 17:11:17 -0800106 // File descriptor for the log file.
107 int fd_ = -1;
108
109 // Allocator which doesn't zero initialize memory.
110 template <typename T>
111 struct DefaultInitAllocator {
112 typedef T value_type;
113
114 template <typename U>
115 void construct(U *p) {
116 ::new (static_cast<void *>(p)) U;
117 }
118
119 template <typename U, typename... Args>
120 void construct(U *p, Args &&... args) {
121 ::new (static_cast<void *>(p)) U(std::forward<Args>(args)...);
122 }
123
124 T *allocate(std::size_t n) {
125 return reinterpret_cast<T *>(::operator new(sizeof(T) * n));
126 }
127
128 template <typename U>
129 void deallocate(U *p, std::size_t /*n*/) {
130 ::operator delete(static_cast<void *>(p));
131 }
132 };
133
134 // Vector to read into. This uses an allocator which doesn't zero
135 // initialize the memory.
136 std::vector<uint8_t, DefaultInitAllocator<uint8_t>> data_;
137
138 // Amount of data consumed already in data_.
139 size_t consumed_data_ = 0;
140
141 // Cached bit for if we have reached the end of the file. Otherwise we will
142 // hammer on the kernel asking for more data each time we send.
143 bool end_of_file_ = false;
144};
145
146// Class which handles reading the header and messages from the log file. This
147// handles any per-file state left before merging below.
148class MessageReader {
149 public:
150 MessageReader(std::string_view filename);
151
Austin Schuh6f3babe2020-01-26 20:34:50 -0800152 std::string_view filename() const { return span_reader_.filename(); }
153
Austin Schuh05b70472020-01-01 17:11:17 -0800154 // Returns the header from the log file.
155 const LogFileHeader *log_file_header() const {
156 return flatbuffers::GetSizePrefixedRoot<LogFileHeader>(
157 configuration_.data());
158 }
159
160 // Returns the minimum maount of data needed to queue up for sorting before
161 // ware guarenteed to not see data out of order.
162 std::chrono::nanoseconds max_out_of_order_duration() const {
163 return max_out_of_order_duration_;
164 }
165
Austin Schuhcde938c2020-02-02 17:30:07 -0800166 // Returns the newest timestamp read out of the log file.
Austin Schuh05b70472020-01-01 17:11:17 -0800167 monotonic_clock::time_point newest_timestamp() const {
168 return newest_timestamp_;
169 }
170
171 // Returns the next message if there is one.
172 std::optional<FlatbufferVector<MessageHeader>> ReadMessage();
173
174 // The time at which we need to read another chunk from the logfile.
175 monotonic_clock::time_point queue_data_time() const {
176 return newest_timestamp() - max_out_of_order_duration();
177 }
178
179 private:
180 // Log chunk reader.
181 SpanReader span_reader_;
182
183 // Vector holding the data for the configuration.
184 std::vector<uint8_t> configuration_;
185
186 // Minimum amount of data to queue up for sorting before we are guarenteed
187 // to not see data out of order.
188 std::chrono::nanoseconds max_out_of_order_duration_;
189
190 // Timestamp of the newest message in a channel queue.
191 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
192};
193
Austin Schuh6f3babe2020-01-26 20:34:50 -0800194class TimestampMerger;
Austin Schuh05b70472020-01-01 17:11:17 -0800195
Austin Schuh6f3babe2020-01-26 20:34:50 -0800196// A design requirement is that the relevant data for a channel is not more than
197// max_out_of_order_duration out of order. We approach sorting in layers.
198//
199// 1) Split each (maybe chunked) log file into one queue per channel. Read this
200// log file looking for data pertaining to a specific node.
201// (SplitMessageReader)
202// 2) Merge all the data per channel from the different log files into a sorted
203// list of timestamps and messages. (TimestampMerger)
204// 3) Combine the timestamps and messages. (TimestampMerger)
205// 4) Merge all the channels to produce the next message on a node.
206// (ChannelMerger)
207// 5) Duplicate this entire stack per node.
208
209// This class splits messages and timestamps up into a queue per channel, and
210// handles reading data from multiple chunks.
211class SplitMessageReader {
212 public:
213 SplitMessageReader(const std::vector<std::string> &filenames);
214
215 // Sets the TimestampMerger that gets notified for each channel. The node
216 // that the TimestampMerger is merging as needs to be passed in.
217 void SetTimestampMerger(TimestampMerger *timestamp_merger, int channel,
218 const Node *target_node);
219
220 // Returns the (timestamp, queue_idex) for the oldest message in a channel, or
221 // max_time if there is nothing in the channel.
Austin Schuhcde938c2020-02-02 17:30:07 -0800222 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
223 oldest_message(int channel) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800224 return channels_[channel].data.front_timestamp();
225 }
226
227 // Returns the (timestamp, queue_index) for the oldest delivery time in a
228 // channel, or max_time if there is nothing in the channel.
Austin Schuhcde938c2020-02-02 17:30:07 -0800229 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
230 oldest_message(int channel, int destination_node) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800231 return channels_[channel].timestamps[destination_node].front_timestamp();
232 }
233
234 // Returns the timestamp, queue_index, and message for the oldest data on a
235 // channel. Requeues data as needed.
236 std::tuple<monotonic_clock::time_point, uint32_t,
237 FlatbufferVector<MessageHeader>>
238 PopOldest(int channel_index);
239
240 // Returns the timestamp, queue_index, and message for the oldest timestamp on
241 // a channel delivered to a node. Requeues data as needed.
242 std::tuple<monotonic_clock::time_point, uint32_t,
243 FlatbufferVector<MessageHeader>>
244 PopOldest(int channel, int node_index);
245
246 // Returns the header for the log files.
Austin Schuh05b70472020-01-01 17:11:17 -0800247 const LogFileHeader *log_file_header() const {
Austin Schuhfa895892020-01-07 20:07:41 -0800248 return &log_file_header_.message();
Austin Schuh05b70472020-01-01 17:11:17 -0800249 }
250
Austin Schuh6f3babe2020-01-26 20:34:50 -0800251 // Returns the starting time for this set of log files.
Austin Schuh05b70472020-01-01 17:11:17 -0800252 monotonic_clock::time_point monotonic_start_time() {
253 return monotonic_clock::time_point(
254 std::chrono::nanoseconds(log_file_header()->monotonic_start_time()));
255 }
256 realtime_clock::time_point realtime_start_time() {
257 return realtime_clock::time_point(
258 std::chrono::nanoseconds(log_file_header()->realtime_start_time()));
259 }
260
Austin Schuh6f3babe2020-01-26 20:34:50 -0800261 // Returns the configuration from the log file header.
262 const Configuration *configuration() const {
263 return log_file_header()->configuration();
264 }
265
Austin Schuh05b70472020-01-01 17:11:17 -0800266 // Returns the node who's point of view this log file is from. Make sure this
267 // is a pointer in the configuration() nodes list so it can be consumed
268 // elsewhere.
269 const Node *node() const {
270 if (configuration()->has_nodes()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800271 return configuration::GetNodeOrDie(configuration(),
272 log_file_header()->node());
Austin Schuh05b70472020-01-01 17:11:17 -0800273 } else {
274 CHECK(!log_file_header()->has_node());
275 return nullptr;
276 }
277 }
278
Austin Schuh6f3babe2020-01-26 20:34:50 -0800279 // Returns the timestamp of the newest message read from the log file, and the
280 // timestamp that we need to re-queue data.
281 monotonic_clock::time_point newest_timestamp() const {
Austin Schuhcde938c2020-02-02 17:30:07 -0800282 return newest_timestamp_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800283 }
284
Austin Schuhcde938c2020-02-02 17:30:07 -0800285 // Returns the next time to trigger a requeue.
286 monotonic_clock::time_point time_to_queue() const { return time_to_queue_; }
287
288 // Returns the minimum amount of data needed to queue up for sorting before
289 // ware guarenteed to not see data out of order.
290 std::chrono::nanoseconds max_out_of_order_duration() const {
291 return message_reader_->max_out_of_order_duration();
292 }
293
294 std::string_view filename() const { return message_reader_->filename(); }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800295
296 // Adds more messages to the sorted list. This reads enough data such that
297 // oldest_message_time can be replayed safely. Returns false if the log file
298 // has all been read.
299 bool QueueMessages(monotonic_clock::time_point oldest_message_time);
Austin Schuh05b70472020-01-01 17:11:17 -0800300
Austin Schuhcde938c2020-02-02 17:30:07 -0800301 // Returns debug strings for a channel, and timestamps for a node.
302 std::string DebugString(int channel) const;
303 std::string DebugString(int channel, int node_index) const;
304
Austin Schuh05b70472020-01-01 17:11:17 -0800305 private:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800306 // TODO(austin): Need to copy or refcount the message instead of running
307 // multiple copies of the reader. Or maybe have a "as_node" index and hide it
308 // inside.
309
Austin Schuhfa895892020-01-07 20:07:41 -0800310 // Moves to the next log file in the list.
311 bool NextLogFile();
312
Austin Schuh6f3babe2020-01-26 20:34:50 -0800313 // Filenames of the log files.
314 std::vector<std::string> filenames_;
315 // And the index of the next file to open.
316 size_t next_filename_index_ = 0;
Austin Schuh05b70472020-01-01 17:11:17 -0800317
Austin Schuh6f3babe2020-01-26 20:34:50 -0800318 // Log file header to report. This is a copy.
319 FlatbufferDetachedBuffer<LogFileHeader> log_file_header_;
320 // Current log file being read.
321 std::unique_ptr<MessageReader> message_reader_;
Austin Schuh05b70472020-01-01 17:11:17 -0800322
323 // Datastructure to hold the list of messages, cached timestamp for the
324 // oldest message, and sender to send with.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800325 struct MessageHeaderQueue {
326 // If true, this is a timestamp queue.
327 bool timestamps = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800328
Austin Schuh6f3babe2020-01-26 20:34:50 -0800329 // Returns a reference to the the oldest message.
330 FlatbufferVector<MessageHeader> &front() {
331 CHECK_GT(data_.size(), 0u);
332 return data_.front();
Austin Schuh05b70472020-01-01 17:11:17 -0800333 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800334
Austin Schuhcde938c2020-02-02 17:30:07 -0800335 // Adds a message to the back of the queue. Returns true if it was actually
336 // emplaced.
337 bool emplace_back(FlatbufferVector<MessageHeader> &&msg);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800338
339 // Drops the front message. Invalidates the front() reference.
340 void pop_front();
341
342 // The size of the queue.
343 size_t size() { return data_.size(); }
344
Austin Schuhcde938c2020-02-02 17:30:07 -0800345 // Returns a debug string with info about each message in the queue.
346 std::string DebugString() const;
347
Austin Schuh6f3babe2020-01-26 20:34:50 -0800348 // Returns the (timestamp, queue_index) for the oldest message.
Austin Schuhcde938c2020-02-02 17:30:07 -0800349 const std::tuple<monotonic_clock::time_point, uint32_t,
350 const MessageHeader *>
351 front_timestamp() {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800352 CHECK_GT(data_.size(), 0u);
353 return std::make_tuple(
354 monotonic_clock::time_point(std::chrono::nanoseconds(
355 front().message().monotonic_sent_time())),
Austin Schuhcde938c2020-02-02 17:30:07 -0800356 front().message().queue_index(), &front().message());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800357 }
358
359 // Pointer to the timestamp merger for this queue if available.
360 TimestampMerger *timestamp_merger = nullptr;
361 // Pointer to the reader which feeds this queue.
362 SplitMessageReader *split_reader = nullptr;
363
364 private:
365 // The data.
366 std::deque<FlatbufferVector<MessageHeader>> data_;
Austin Schuh05b70472020-01-01 17:11:17 -0800367 };
368
Austin Schuh6f3babe2020-01-26 20:34:50 -0800369 // All the queues needed for a channel. There isn't going to be data in all
370 // of these.
371 struct ChannelData {
372 // The data queue for the channel.
373 MessageHeaderQueue data;
374 // Queues for timestamps for each node.
375 std::vector<MessageHeaderQueue> timestamps;
376 };
Austin Schuhfa895892020-01-07 20:07:41 -0800377
Austin Schuh6f3babe2020-01-26 20:34:50 -0800378 // Data for all the channels.
Austin Schuh05b70472020-01-01 17:11:17 -0800379 std::vector<ChannelData> channels_;
380
Austin Schuh6f3babe2020-01-26 20:34:50 -0800381 // Once we know the node that this SplitMessageReader will be writing as,
382 // there will be only one MessageHeaderQueue that a specific channel matches.
383 // Precompute this here for efficiency.
384 std::vector<MessageHeaderQueue *> channels_to_write_;
385
Austin Schuhcde938c2020-02-02 17:30:07 -0800386 monotonic_clock::time_point time_to_queue_ = monotonic_clock::min_time;
387
388 // Latches true when we hit the end of the last log file and there is no sense
389 // poking it further.
390 bool at_end_ = false;
391
392 // Timestamp of the newest message that was read and actually queued. We want
393 // to track this independently from the log file because we need the
394 // timestamps here to be timestamps of messages that are queued.
395 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800396};
397
398class ChannelMerger;
399
400// Sorts channels (and timestamps) from multiple log files for a single channel.
401class TimestampMerger {
402 public:
403 TimestampMerger(const Configuration *configuration,
404 std::vector<SplitMessageReader *> split_message_readers,
405 int channel_index, const Node *target_node,
406 ChannelMerger *channel_merger);
407
408 // Metadata used to schedule the message.
409 struct DeliveryTimestamp {
410 monotonic_clock::time_point monotonic_event_time =
411 monotonic_clock::min_time;
412 realtime_clock::time_point realtime_event_time = realtime_clock::min_time;
413
414 monotonic_clock::time_point monotonic_remote_time =
415 monotonic_clock::min_time;
416 realtime_clock::time_point realtime_remote_time = realtime_clock::min_time;
417 uint32_t remote_queue_index = 0xffffffff;
418 };
419
420 // Pushes SplitMessageReader onto the timestamp heap. This should only be
421 // called when timestamps are placed in the channel this class is merging for
422 // the reader.
423 void UpdateTimestamp(
424 SplitMessageReader *split_message_reader,
Austin Schuhcde938c2020-02-02 17:30:07 -0800425 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
426 oldest_message_time) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800427 PushTimestampHeap(oldest_message_time, split_message_reader);
428 }
429 // Pushes SplitMessageReader onto the message heap. This should only be
430 // called when data is placed in the channel this class is merging for the
431 // reader.
432 void Update(
433 SplitMessageReader *split_message_reader,
Austin Schuhcde938c2020-02-02 17:30:07 -0800434 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
435 oldest_message_time) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800436 PushMessageHeap(oldest_message_time, split_message_reader);
437 }
438
Austin Schuhcde938c2020-02-02 17:30:07 -0800439 // Returns the oldest combined timestamp and data for this channel. If there
440 // isn't a matching piece of data, returns only the timestamp with no data.
441 // The caller can determine what the appropriate action is to recover.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800442 std::tuple<DeliveryTimestamp, FlatbufferVector<MessageHeader>> PopOldest();
443
444 // Tracks if the channel merger has pushed this onto it's heap or not.
445 bool pushed() { return pushed_; }
446 // Sets if this has been pushed to the channel merger heap. Should only be
447 // called by the channel merger.
448 void set_pushed(bool pushed) { pushed_ = pushed; }
449
Austin Schuhcde938c2020-02-02 17:30:07 -0800450 // Returns a debug string with the heaps printed out.
451 std::string DebugString() const;
452
Austin Schuh6f3babe2020-01-26 20:34:50 -0800453 private:
454 // Pushes messages and timestamps to the corresponding heaps.
455 void PushMessageHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800456 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
457 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800458 SplitMessageReader *split_message_reader);
459 void PushTimestampHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800460 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
461 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800462 SplitMessageReader *split_message_reader);
463
464 // Pops a message from the message heap. This automatically triggers the
465 // split message reader to re-fetch any new data.
466 std::tuple<monotonic_clock::time_point, uint32_t,
467 FlatbufferVector<MessageHeader>>
468 PopMessageHeap();
Austin Schuhcde938c2020-02-02 17:30:07 -0800469
470 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
471 oldest_message() const;
472 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
473 oldest_timestamp() const;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800474 // Pops a message from the timestamp heap. This automatically triggers the
475 // split message reader to re-fetch any new data.
476 std::tuple<monotonic_clock::time_point, uint32_t,
477 FlatbufferVector<MessageHeader>>
478 PopTimestampHeap();
479
480 const Configuration *configuration_;
481
482 // If true, this is a forwarded channel and timestamps should be matched.
483 bool has_timestamps_ = false;
484
485 // Tracks if the ChannelMerger has pushed this onto it's queue.
486 bool pushed_ = false;
487
488 // The split message readers used for source data.
489 std::vector<SplitMessageReader *> split_message_readers_;
490
491 // The channel to merge.
492 int channel_index_;
493
494 // Our node.
495 int node_index_;
496
497 // Heaps for messages and timestamps.
498 std::vector<
499 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
500 message_heap_;
501 std::vector<
502 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
503 timestamp_heap_;
504
505 // Parent channel merger.
506 ChannelMerger *channel_merger_;
507};
508
509// This class handles constructing all the split message readers, channel
510// mergers, and combining the results.
511class ChannelMerger {
512 public:
513 // Builds a ChannelMerger around a set of log files. These are of the format:
514 // {
515 // {log1_part0, log1_part1, ...},
516 // {log2}
517 // }
518 // The inner vector is a list of log file chunks which form up a log file.
519 // The outer vector is a list of log files with subsets of the messages, or
520 // messages from different nodes.
521 ChannelMerger(const std::vector<std::vector<std::string>> &filenames);
522
523 // Returns the nodes that we know how to merge.
524 const std::vector<const Node *> nodes() const;
525 // Sets the node that we will return messages as. Returns true if the node
526 // has log files and will produce data. This can only be called once, and
527 // will likely corrupt state if called a second time.
528 bool SetNode(const Node *target_node);
529
530 // Everything else needs the node set before it works.
531
532 // Returns a timestamp for the oldest message in this group of logfiles.
533 monotonic_clock::time_point OldestMessage() const;
534 // Pops the oldest message.
535 std::tuple<TimestampMerger::DeliveryTimestamp, int,
536 FlatbufferVector<MessageHeader>>
537 PopOldest();
538
539 // Returns the config for this set of log files.
540 const Configuration *configuration() const {
541 return log_file_header()->configuration();
542 }
543
544 const LogFileHeader *log_file_header() const {
545 return &log_file_header_.message();
546 }
547
548 // Returns the start times for the configured node's log files.
Austin Schuhcde938c2020-02-02 17:30:07 -0800549 monotonic_clock::time_point monotonic_start_time() const {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800550 return monotonic_clock::time_point(
551 std::chrono::nanoseconds(log_file_header()->monotonic_start_time()));
552 }
Austin Schuhcde938c2020-02-02 17:30:07 -0800553 realtime_clock::time_point realtime_start_time() const {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800554 return realtime_clock::time_point(
555 std::chrono::nanoseconds(log_file_header()->realtime_start_time()));
556 }
557
558 // Returns the node set by SetNode above.
559 const Node *node() const { return node_; }
560
561 // Called by the TimestampMerger when new data is available with the provided
562 // timestamp and channel_index.
563 void Update(monotonic_clock::time_point timestamp, int channel_index) {
564 PushChannelHeap(timestamp, channel_index);
565 }
566
Austin Schuhcde938c2020-02-02 17:30:07 -0800567 // Returns a debug string with all the heaps in it. Generally only useful for
568 // debugging what went wrong.
569 std::string DebugString() const;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800570
Austin Schuhcde938c2020-02-02 17:30:07 -0800571 private:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800572 // Pushes the timestamp for new data on the provided channel.
573 void PushChannelHeap(monotonic_clock::time_point timestamp,
574 int channel_index);
575
576 // All the message readers.
577 std::vector<std::unique_ptr<SplitMessageReader>> split_message_readers_;
578
579 // The log header we are claiming to be.
580 FlatbufferDetachedBuffer<LogFileHeader> log_file_header_;
581
582 // The timestamp mergers which combine data from the split message readers.
583 std::vector<TimestampMerger> timestamp_mergers_;
584
585 // A heap of the channel readers and timestamps for the oldest data in each.
Austin Schuh05b70472020-01-01 17:11:17 -0800586 std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap_;
587
Austin Schuh6f3babe2020-01-26 20:34:50 -0800588 // Configured node.
589 const Node *node_;
590
591 // Cached copy of the list of nodes.
592 std::vector<const Node *> nodes_;
Austin Schuh05b70472020-01-01 17:11:17 -0800593};
Austin Schuha36c8902019-12-30 18:07:15 -0800594
595} // namespace logger
596} // namespace aos
597
598#endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_