blob: 3a312bc1b7693d513d7ed49b2fe98efd4a42d7fd [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 Schuh05b70472020-01-01 17:11:17 -080017#include "absl/types/span.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070018#include "aos/containers/resizeable_buffer.h"
Austin Schuha36c8902019-12-30 18:07:15 -080019#include "aos/events/event_loop.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070020#include "aos/events/logging/buffer_encoder.h"
Austin Schuha36c8902019-12-30 18:07:15 -080021#include "aos/events/logging/logger_generated.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070022#include "aos/flatbuffers.h"
Austin Schuha36c8902019-12-30 18:07:15 -080023#include "flatbuffers/flatbuffers.h"
24
Brian Silvermanf51499a2020-09-21 12:49:08 -070025namespace aos::logger {
Austin Schuha36c8902019-12-30 18:07:15 -080026
27enum class LogType : uint8_t {
28 // The message originated on this node and should be logged here.
29 kLogMessage,
30 // The message originated on another node, but only the delivery times are
31 // logged here.
32 kLogDeliveryTimeOnly,
33 // The message originated on another node. Log it and the delivery times
34 // together. The message_gateway is responsible for logging any messages
35 // which didn't get delivered.
Austin Schuh6f3babe2020-01-26 20:34:50 -080036 kLogMessageAndDeliveryTime,
37 // The message originated on the other node and should be logged on this node.
38 kLogRemoteMessage
Austin Schuha36c8902019-12-30 18:07:15 -080039};
40
Austin Schuha36c8902019-12-30 18:07:15 -080041// This class manages efficiently writing a sequence of detached buffers to a
Brian Silvermanf51499a2020-09-21 12:49:08 -070042// file. It encodes them, queues them up, and batches the write operation.
Austin Schuha36c8902019-12-30 18:07:15 -080043class DetachedBufferWriter {
44 public:
Brian Silvermanf51499a2020-09-21 12:49:08 -070045 DetachedBufferWriter(std::string_view filename,
46 std::unique_ptr<DetachedBufferEncoder> encoder);
Austin Schuh2f8fd752020-09-01 22:38:28 -070047 DetachedBufferWriter(DetachedBufferWriter &&other);
48 DetachedBufferWriter(const DetachedBufferWriter &) = delete;
49
Austin Schuha36c8902019-12-30 18:07:15 -080050 ~DetachedBufferWriter();
51
Austin Schuh2f8fd752020-09-01 22:38:28 -070052 DetachedBufferWriter &operator=(DetachedBufferWriter &&other);
Brian Silverman98360e22020-04-28 16:51:20 -070053 DetachedBufferWriter &operator=(const DetachedBufferWriter &) = delete;
54
Austin Schuh6f3babe2020-01-26 20:34:50 -080055 std::string_view filename() const { return filename_; }
56
Austin Schuh2f8fd752020-09-01 22:38:28 -070057 // Rewrites a location in a file (relative to the start) to have new data in
58 // it. The main use case is updating start times after a log file starts.
59 void RewriteLocation(off64_t offset, absl::Span<const uint8_t> data);
60
Brian Silvermanf51499a2020-09-21 12:49:08 -070061 // Queues up a finished FlatBufferBuilder to be encoded and written.
62 //
63 // Triggers a flush if there's enough data queued up.
64 //
65 // Steals the detached buffer from it.
66 void QueueSizedFlatbuffer(flatbuffers::FlatBufferBuilder *fbb) {
67 QueueSizedFlatbuffer(fbb->Release());
68 }
69 // May steal the backing storage of buffer, or may leave it alone.
70 void QueueSizedFlatbuffer(flatbuffers::DetachedBuffer &&buffer) {
71 encoder_->Encode(std::move(buffer));
72 FlushAtThreshold();
73 }
Austin Schuha36c8902019-12-30 18:07:15 -080074
Brian Silvermanf51499a2020-09-21 12:49:08 -070075 // Queues up data in span. May copy or may write it to disk immediately.
76 void QueueSpan(absl::Span<const uint8_t> span);
Austin Schuha36c8902019-12-30 18:07:15 -080077
Brian Silverman0465fcf2020-09-24 00:29:18 -070078 // Indicates we got ENOSPC when trying to write. After this returns true, no
79 // further data is written.
80 bool ran_out_of_space() const { return ran_out_of_space_; }
81
82 // To avoid silently failing to write logfiles, you must call this before
83 // destruction if ran_out_of_space() is true and the situation has been
84 // handled.
85 void acknowledge_out_of_space() {
86 CHECK(ran_out_of_space_);
87 acknowledge_ran_out_of_space_ = true;
88 }
89
90 // Fully flushes and closes the underlying file now. No additional data may be
91 // enqueued after calling this.
92 //
93 // This will be performed in the destructor automatically.
94 //
95 // Note that this may set ran_out_of_space().
96 void Close();
97
Brian Silvermanf51499a2020-09-21 12:49:08 -070098 // Returns the total number of bytes written and currently queued.
99 size_t total_bytes() const { return encoder_->total_bytes(); }
Austin Schuha36c8902019-12-30 18:07:15 -0800100
Brian Silvermanf51499a2020-09-21 12:49:08 -0700101 // The maximum time for a single write call, or 0 if none have been performed.
102 std::chrono::nanoseconds max_write_time() const { return max_write_time_; }
103 // The number of bytes in the longest write call, or -1 if none have been
104 // performed.
105 int max_write_time_bytes() const { return max_write_time_bytes_; }
106 // The number of buffers in the longest write call, or -1 if none have been
107 // performed.
108 int max_write_time_messages() const { return max_write_time_messages_; }
109 // The total time spent in write calls.
110 std::chrono::nanoseconds total_write_time() const {
111 return total_write_time_;
112 }
113 // The total number of writes which have been performed.
114 int total_write_count() const { return total_write_count_; }
115 // The total number of messages which have been written.
116 int total_write_messages() const { return total_write_messages_; }
117 // The total number of bytes which have been written.
118 int total_write_bytes() const { return total_write_bytes_; }
119 void ResetStatistics() {
120 max_write_time_ = std::chrono::nanoseconds::zero();
121 max_write_time_bytes_ = -1;
122 max_write_time_messages_ = -1;
123 total_write_time_ = std::chrono::nanoseconds::zero();
124 total_write_count_ = 0;
125 total_write_messages_ = 0;
126 total_write_bytes_ = 0;
127 }
Brian Silverman98360e22020-04-28 16:51:20 -0700128
Austin Schuha36c8902019-12-30 18:07:15 -0800129 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700130 // Performs a single writev call with as much of the data we have queued up as
131 // possible.
132 //
133 // This will normally take all of the data we have queued up, unless an
134 // encoder has spit out a big enough chunk all at once that we can't manage
135 // all of it.
136 void Flush();
137
Brian Silverman0465fcf2020-09-24 00:29:18 -0700138 // write_return is what write(2) or writev(2) returned. write_size is the
139 // number of bytes we expected it to write.
140 void HandleWriteReturn(ssize_t write_return, size_t write_size);
141
Brian Silvermanf51499a2020-09-21 12:49:08 -0700142 void UpdateStatsForWrite(aos::monotonic_clock::duration duration,
143 ssize_t written, int iovec_size);
144
145 // Flushes data if we've reached the threshold to do that as part of normal
146 // operation.
147 void FlushAtThreshold();
148
Austin Schuh2f8fd752020-09-01 22:38:28 -0700149 std::string filename_;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700150 std::unique_ptr<DetachedBufferEncoder> encoder_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800151
Austin Schuha36c8902019-12-30 18:07:15 -0800152 int fd_ = -1;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700153 bool ran_out_of_space_ = false;
154 bool acknowledge_ran_out_of_space_ = false;
Austin Schuha36c8902019-12-30 18:07:15 -0800155
Austin Schuha36c8902019-12-30 18:07:15 -0800156 // List of iovecs to use with writev. This is a member variable to avoid
157 // churn.
158 std::vector<struct iovec> iovec_;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700159
160 std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero();
161 int max_write_time_bytes_ = -1;
162 int max_write_time_messages_ = -1;
163 std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero();
164 int total_write_count_ = 0;
165 int total_write_messages_ = 0;
166 int total_write_bytes_ = 0;
Austin Schuha36c8902019-12-30 18:07:15 -0800167};
168
169// Packes a message pointed to by the context into a MessageHeader.
170flatbuffers::Offset<MessageHeader> PackMessage(
171 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
172 int channel_index, LogType log_type);
173
Austin Schuh6f3babe2020-01-26 20:34:50 -0800174FlatbufferVector<LogFileHeader> ReadHeader(std::string_view filename);
Austin Schuh5212cad2020-09-09 23:12:09 -0700175FlatbufferVector<MessageHeader> ReadNthMessage(std::string_view filename,
176 size_t n);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800177
Austin Schuh05b70472020-01-01 17:11:17 -0800178// Class to read chunks out of a log file.
179class SpanReader {
180 public:
181 SpanReader(std::string_view filename);
Austin Schuha36c8902019-12-30 18:07:15 -0800182
Austin Schuh6f3babe2020-01-26 20:34:50 -0800183 std::string_view filename() const { return filename_; }
184
Austin Schuh05b70472020-01-01 17:11:17 -0800185 // Returns a span with the data for a message from the log file, excluding
186 // the size.
187 absl::Span<const uint8_t> ReadMessage();
188
189 // Returns true if there is a full message available in the buffer, or if we
190 // will have to read more data from disk.
191 bool MessageAvailable();
192
193 private:
194 // TODO(austin): Optimization:
195 // Allocate the 256k blocks like we do today. But, refcount them with
196 // shared_ptr pointed to by the messageheader that is returned. This avoids
197 // the copy. Need to do more benchmarking.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700198 // And (Brian): Consider just mmapping the file and handing out refcounted
199 // pointers into that too.
Austin Schuh05b70472020-01-01 17:11:17 -0800200
201 // Reads a chunk of data into data_. Returns false if no data was read.
202 bool ReadBlock();
203
Austin Schuh6f3babe2020-01-26 20:34:50 -0800204 const std::string filename_;
205
Brian Silvermanf51499a2020-09-21 12:49:08 -0700206 // File reader and data decoder.
207 std::unique_ptr<DataDecoder> decoder_;
Austin Schuh05b70472020-01-01 17:11:17 -0800208
Brian Silvermanf51499a2020-09-21 12:49:08 -0700209 // Vector to read into.
210 ResizeableBuffer data_;
Austin Schuh05b70472020-01-01 17:11:17 -0800211
212 // Amount of data consumed already in data_.
213 size_t consumed_data_ = 0;
Austin Schuh05b70472020-01-01 17:11:17 -0800214};
215
216// Class which handles reading the header and messages from the log file. This
217// handles any per-file state left before merging below.
218class MessageReader {
219 public:
220 MessageReader(std::string_view filename);
221
Austin Schuh6f3babe2020-01-26 20:34:50 -0800222 std::string_view filename() const { return span_reader_.filename(); }
223
Austin Schuh05b70472020-01-01 17:11:17 -0800224 // Returns the header from the log file.
225 const LogFileHeader *log_file_header() const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700226 return &raw_log_file_header_.message();
227 }
228
229 // Returns the raw data of the header from the log file.
230 const FlatbufferVector<LogFileHeader> &raw_log_file_header() const {
231 return raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800232 }
233
234 // Returns the minimum maount of data needed to queue up for sorting before
235 // ware guarenteed to not see data out of order.
236 std::chrono::nanoseconds max_out_of_order_duration() const {
237 return max_out_of_order_duration_;
238 }
239
Austin Schuhcde938c2020-02-02 17:30:07 -0800240 // Returns the newest timestamp read out of the log file.
Austin Schuh05b70472020-01-01 17:11:17 -0800241 monotonic_clock::time_point newest_timestamp() const {
242 return newest_timestamp_;
243 }
244
245 // Returns the next message if there is one.
246 std::optional<FlatbufferVector<MessageHeader>> ReadMessage();
247
248 // The time at which we need to read another chunk from the logfile.
249 monotonic_clock::time_point queue_data_time() const {
250 return newest_timestamp() - max_out_of_order_duration();
251 }
252
253 private:
254 // Log chunk reader.
255 SpanReader span_reader_;
256
Austin Schuh97789fc2020-08-01 14:42:45 -0700257 // Vector holding the raw data for the log file header.
258 FlatbufferVector<LogFileHeader> raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800259
260 // Minimum amount of data to queue up for sorting before we are guarenteed
261 // to not see data out of order.
262 std::chrono::nanoseconds max_out_of_order_duration_;
263
264 // Timestamp of the newest message in a channel queue.
265 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
266};
267
Austin Schuh6f3babe2020-01-26 20:34:50 -0800268class TimestampMerger;
Austin Schuh05b70472020-01-01 17:11:17 -0800269
Austin Schuh6f3babe2020-01-26 20:34:50 -0800270// A design requirement is that the relevant data for a channel is not more than
271// max_out_of_order_duration out of order. We approach sorting in layers.
272//
273// 1) Split each (maybe chunked) log file into one queue per channel. Read this
274// log file looking for data pertaining to a specific node.
275// (SplitMessageReader)
276// 2) Merge all the data per channel from the different log files into a sorted
277// list of timestamps and messages. (TimestampMerger)
278// 3) Combine the timestamps and messages. (TimestampMerger)
279// 4) Merge all the channels to produce the next message on a node.
280// (ChannelMerger)
281// 5) Duplicate this entire stack per node.
282
283// This class splits messages and timestamps up into a queue per channel, and
284// handles reading data from multiple chunks.
285class SplitMessageReader {
286 public:
287 SplitMessageReader(const std::vector<std::string> &filenames);
288
289 // Sets the TimestampMerger that gets notified for each channel. The node
290 // that the TimestampMerger is merging as needs to be passed in.
291 void SetTimestampMerger(TimestampMerger *timestamp_merger, int channel,
292 const Node *target_node);
293
Austin Schuh2f8fd752020-09-01 22:38:28 -0700294 // Returns the (timestamp, queue_index, message_header) for the oldest message
295 // in a channel, or max_time if there is nothing in the channel.
Austin Schuhcde938c2020-02-02 17:30:07 -0800296 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
297 oldest_message(int channel) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800298 return channels_[channel].data.front_timestamp();
299 }
300
Austin Schuh2f8fd752020-09-01 22:38:28 -0700301 // Returns the (timestamp, queue_index, message_header) for the oldest
302 // delivery time in a channel, or max_time if there is nothing in the channel.
Austin Schuhcde938c2020-02-02 17:30:07 -0800303 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
304 oldest_message(int channel, int destination_node) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800305 return channels_[channel].timestamps[destination_node].front_timestamp();
306 }
307
308 // Returns the timestamp, queue_index, and message for the oldest data on a
309 // channel. Requeues data as needed.
310 std::tuple<monotonic_clock::time_point, uint32_t,
311 FlatbufferVector<MessageHeader>>
312 PopOldest(int channel_index);
313
314 // Returns the timestamp, queue_index, and message for the oldest timestamp on
315 // a channel delivered to a node. Requeues data as needed.
316 std::tuple<monotonic_clock::time_point, uint32_t,
317 FlatbufferVector<MessageHeader>>
Austin Schuh2f8fd752020-09-01 22:38:28 -0700318 PopOldestTimestamp(int channel, int node_index);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800319
320 // Returns the header for the log files.
Austin Schuh05b70472020-01-01 17:11:17 -0800321 const LogFileHeader *log_file_header() const {
Austin Schuhfa895892020-01-07 20:07:41 -0800322 return &log_file_header_.message();
Austin Schuh05b70472020-01-01 17:11:17 -0800323 }
324
Austin Schuh97789fc2020-08-01 14:42:45 -0700325 const FlatbufferVector<LogFileHeader> &raw_log_file_header() const {
326 return log_file_header_;
327 }
328
Austin Schuh6f3babe2020-01-26 20:34:50 -0800329 // Returns the starting time for this set of log files.
Austin Schuh05b70472020-01-01 17:11:17 -0800330 monotonic_clock::time_point monotonic_start_time() {
331 return monotonic_clock::time_point(
332 std::chrono::nanoseconds(log_file_header()->monotonic_start_time()));
333 }
334 realtime_clock::time_point realtime_start_time() {
335 return realtime_clock::time_point(
336 std::chrono::nanoseconds(log_file_header()->realtime_start_time()));
337 }
338
Austin Schuh6f3babe2020-01-26 20:34:50 -0800339 // Returns the configuration from the log file header.
340 const Configuration *configuration() const {
341 return log_file_header()->configuration();
342 }
343
Austin Schuh05b70472020-01-01 17:11:17 -0800344 // Returns the node who's point of view this log file is from. Make sure this
345 // is a pointer in the configuration() nodes list so it can be consumed
346 // elsewhere.
347 const Node *node() const {
348 if (configuration()->has_nodes()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800349 return configuration::GetNodeOrDie(configuration(),
350 log_file_header()->node());
Austin Schuh05b70472020-01-01 17:11:17 -0800351 } else {
352 CHECK(!log_file_header()->has_node());
353 return nullptr;
354 }
355 }
356
Austin Schuh6f3babe2020-01-26 20:34:50 -0800357 // Returns the timestamp of the newest message read from the log file, and the
358 // timestamp that we need to re-queue data.
359 monotonic_clock::time_point newest_timestamp() const {
Austin Schuhcde938c2020-02-02 17:30:07 -0800360 return newest_timestamp_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800361 }
362
Austin Schuhcde938c2020-02-02 17:30:07 -0800363 // Returns the next time to trigger a requeue.
364 monotonic_clock::time_point time_to_queue() const { return time_to_queue_; }
365
366 // Returns the minimum amount of data needed to queue up for sorting before
367 // ware guarenteed to not see data out of order.
368 std::chrono::nanoseconds max_out_of_order_duration() const {
369 return message_reader_->max_out_of_order_duration();
370 }
371
372 std::string_view filename() const { return message_reader_->filename(); }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800373
374 // Adds more messages to the sorted list. This reads enough data such that
375 // oldest_message_time can be replayed safely. Returns false if the log file
376 // has all been read.
377 bool QueueMessages(monotonic_clock::time_point oldest_message_time);
Austin Schuh05b70472020-01-01 17:11:17 -0800378
Austin Schuhcde938c2020-02-02 17:30:07 -0800379 // Returns debug strings for a channel, and timestamps for a node.
380 std::string DebugString(int channel) const;
381 std::string DebugString(int channel, int node_index) const;
382
Austin Schuh8bd96322020-02-13 21:18:22 -0800383 // Returns true if all the messages have been queued from the last log file in
384 // the list of log files chunks.
385 bool at_end() const { return at_end_; }
386
Austin Schuh05b70472020-01-01 17:11:17 -0800387 private:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800388 // TODO(austin): Need to copy or refcount the message instead of running
389 // multiple copies of the reader. Or maybe have a "as_node" index and hide it
390 // inside.
391
Austin Schuhfa895892020-01-07 20:07:41 -0800392 // Moves to the next log file in the list.
393 bool NextLogFile();
394
Austin Schuh6f3babe2020-01-26 20:34:50 -0800395 // Filenames of the log files.
396 std::vector<std::string> filenames_;
397 // And the index of the next file to open.
398 size_t next_filename_index_ = 0;
Austin Schuh05b70472020-01-01 17:11:17 -0800399
Austin Schuhee711052020-08-24 16:06:09 -0700400 // Node we are reading as.
401 const Node *target_node_ = nullptr;
402
Austin Schuh6f3babe2020-01-26 20:34:50 -0800403 // Log file header to report. This is a copy.
Austin Schuh97789fc2020-08-01 14:42:45 -0700404 FlatbufferVector<LogFileHeader> log_file_header_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800405 // Current log file being read.
406 std::unique_ptr<MessageReader> message_reader_;
Austin Schuh05b70472020-01-01 17:11:17 -0800407
408 // Datastructure to hold the list of messages, cached timestamp for the
409 // oldest message, and sender to send with.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800410 struct MessageHeaderQueue {
411 // If true, this is a timestamp queue.
412 bool timestamps = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800413
Austin Schuh6f3babe2020-01-26 20:34:50 -0800414 // Returns a reference to the the oldest message.
415 FlatbufferVector<MessageHeader> &front() {
416 CHECK_GT(data_.size(), 0u);
417 return data_.front();
Austin Schuh05b70472020-01-01 17:11:17 -0800418 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800419
Austin Schuhcde938c2020-02-02 17:30:07 -0800420 // Adds a message to the back of the queue. Returns true if it was actually
421 // emplaced.
422 bool emplace_back(FlatbufferVector<MessageHeader> &&msg);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800423
424 // Drops the front message. Invalidates the front() reference.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700425 void PopFront();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800426
427 // The size of the queue.
428 size_t size() { return data_.size(); }
429
Austin Schuhcde938c2020-02-02 17:30:07 -0800430 // Returns a debug string with info about each message in the queue.
431 std::string DebugString() const;
432
Austin Schuh2f8fd752020-09-01 22:38:28 -0700433 // Returns the (timestamp, queue_index, message_header) for the oldest
434 // message.
Austin Schuhcde938c2020-02-02 17:30:07 -0800435 const std::tuple<monotonic_clock::time_point, uint32_t,
436 const MessageHeader *>
437 front_timestamp() {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700438 const MessageHeader &message = front().message();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800439 return std::make_tuple(
Austin Schuh2f8fd752020-09-01 22:38:28 -0700440 monotonic_clock::time_point(
441 std::chrono::nanoseconds(message.monotonic_sent_time())),
442 message.queue_index(), &message);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800443 }
444
445 // Pointer to the timestamp merger for this queue if available.
446 TimestampMerger *timestamp_merger = nullptr;
447 // Pointer to the reader which feeds this queue.
448 SplitMessageReader *split_reader = nullptr;
449
450 private:
451 // The data.
452 std::deque<FlatbufferVector<MessageHeader>> data_;
Austin Schuh05b70472020-01-01 17:11:17 -0800453 };
454
Austin Schuh6f3babe2020-01-26 20:34:50 -0800455 // All the queues needed for a channel. There isn't going to be data in all
456 // of these.
457 struct ChannelData {
458 // The data queue for the channel.
459 MessageHeaderQueue data;
460 // Queues for timestamps for each node.
461 std::vector<MessageHeaderQueue> timestamps;
462 };
Austin Schuhfa895892020-01-07 20:07:41 -0800463
Austin Schuh6f3babe2020-01-26 20:34:50 -0800464 // Data for all the channels.
Austin Schuh05b70472020-01-01 17:11:17 -0800465 std::vector<ChannelData> channels_;
466
Austin Schuh6f3babe2020-01-26 20:34:50 -0800467 // Once we know the node that this SplitMessageReader will be writing as,
468 // there will be only one MessageHeaderQueue that a specific channel matches.
469 // Precompute this here for efficiency.
470 std::vector<MessageHeaderQueue *> channels_to_write_;
471
Austin Schuhcde938c2020-02-02 17:30:07 -0800472 monotonic_clock::time_point time_to_queue_ = monotonic_clock::min_time;
473
474 // Latches true when we hit the end of the last log file and there is no sense
475 // poking it further.
476 bool at_end_ = false;
477
478 // Timestamp of the newest message that was read and actually queued. We want
479 // to track this independently from the log file because we need the
480 // timestamps here to be timestamps of messages that are queued.
481 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800482};
483
484class ChannelMerger;
485
486// Sorts channels (and timestamps) from multiple log files for a single channel.
487class TimestampMerger {
488 public:
489 TimestampMerger(const Configuration *configuration,
490 std::vector<SplitMessageReader *> split_message_readers,
491 int channel_index, const Node *target_node,
492 ChannelMerger *channel_merger);
493
494 // Metadata used to schedule the message.
495 struct DeliveryTimestamp {
496 monotonic_clock::time_point monotonic_event_time =
497 monotonic_clock::min_time;
498 realtime_clock::time_point realtime_event_time = realtime_clock::min_time;
499
500 monotonic_clock::time_point monotonic_remote_time =
501 monotonic_clock::min_time;
502 realtime_clock::time_point realtime_remote_time = realtime_clock::min_time;
503 uint32_t remote_queue_index = 0xffffffff;
504 };
505
506 // Pushes SplitMessageReader onto the timestamp heap. This should only be
507 // called when timestamps are placed in the channel this class is merging for
508 // the reader.
509 void UpdateTimestamp(
510 SplitMessageReader *split_message_reader,
Austin Schuhcde938c2020-02-02 17:30:07 -0800511 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
512 oldest_message_time) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800513 PushTimestampHeap(oldest_message_time, split_message_reader);
514 }
515 // Pushes SplitMessageReader onto the message heap. This should only be
516 // called when data is placed in the channel this class is merging for the
517 // reader.
518 void Update(
519 SplitMessageReader *split_message_reader,
Austin Schuhcde938c2020-02-02 17:30:07 -0800520 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
521 oldest_message_time) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800522 PushMessageHeap(oldest_message_time, split_message_reader);
523 }
524
Austin Schuhcde938c2020-02-02 17:30:07 -0800525 // Returns the oldest combined timestamp and data for this channel. If there
526 // isn't a matching piece of data, returns only the timestamp with no data.
527 // The caller can determine what the appropriate action is to recover.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800528 std::tuple<DeliveryTimestamp, FlatbufferVector<MessageHeader>> PopOldest();
529
530 // Tracks if the channel merger has pushed this onto it's heap or not.
531 bool pushed() { return pushed_; }
532 // Sets if this has been pushed to the channel merger heap. Should only be
533 // called by the channel merger.
534 void set_pushed(bool pushed) { pushed_ = pushed; }
535
Austin Schuhcde938c2020-02-02 17:30:07 -0800536 // Returns a debug string with the heaps printed out.
537 std::string DebugString() const;
538
Austin Schuh8bd96322020-02-13 21:18:22 -0800539 // Returns true if we have timestamps.
540 bool has_timestamps() const { return has_timestamps_; }
541
542 // Records that one of the log files ran out of data. This should only be
543 // called by a SplitMessageReader.
544 void NoticeAtEnd();
545
Austin Schuh2f8fd752020-09-01 22:38:28 -0700546 aos::monotonic_clock::time_point channel_merger_time() {
547 if (has_timestamps_) {
548 return std::get<0>(timestamp_heap_[0]);
549 } else {
550 return std::get<0>(message_heap_[0]);
551 }
552 }
553
Austin Schuh6f3babe2020-01-26 20:34:50 -0800554 private:
555 // Pushes messages and timestamps to the corresponding heaps.
556 void PushMessageHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800557 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
558 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800559 SplitMessageReader *split_message_reader);
560 void PushTimestampHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800561 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
562 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800563 SplitMessageReader *split_message_reader);
564
565 // Pops a message from the message heap. This automatically triggers the
566 // split message reader to re-fetch any new data.
567 std::tuple<monotonic_clock::time_point, uint32_t,
568 FlatbufferVector<MessageHeader>>
569 PopMessageHeap();
Austin Schuhcde938c2020-02-02 17:30:07 -0800570
571 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
572 oldest_message() const;
573 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
574 oldest_timestamp() const;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800575 // Pops a message from the timestamp heap. This automatically triggers the
576 // split message reader to re-fetch any new data.
577 std::tuple<monotonic_clock::time_point, uint32_t,
578 FlatbufferVector<MessageHeader>>
579 PopTimestampHeap();
580
581 const Configuration *configuration_;
582
583 // If true, this is a forwarded channel and timestamps should be matched.
584 bool has_timestamps_ = false;
585
586 // Tracks if the ChannelMerger has pushed this onto it's queue.
587 bool pushed_ = false;
588
589 // The split message readers used for source data.
590 std::vector<SplitMessageReader *> split_message_readers_;
591
592 // The channel to merge.
593 int channel_index_;
594
595 // Our node.
596 int node_index_;
597
598 // Heaps for messages and timestamps.
599 std::vector<
600 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
601 message_heap_;
602 std::vector<
603 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
604 timestamp_heap_;
605
606 // Parent channel merger.
607 ChannelMerger *channel_merger_;
608};
609
610// This class handles constructing all the split message readers, channel
611// mergers, and combining the results.
612class ChannelMerger {
613 public:
614 // Builds a ChannelMerger around a set of log files. These are of the format:
615 // {
616 // {log1_part0, log1_part1, ...},
617 // {log2}
618 // }
619 // The inner vector is a list of log file chunks which form up a log file.
620 // The outer vector is a list of log files with subsets of the messages, or
621 // messages from different nodes.
622 ChannelMerger(const std::vector<std::vector<std::string>> &filenames);
623
624 // Returns the nodes that we know how to merge.
625 const std::vector<const Node *> nodes() const;
626 // Sets the node that we will return messages as. Returns true if the node
627 // has log files and will produce data. This can only be called once, and
628 // will likely corrupt state if called a second time.
629 bool SetNode(const Node *target_node);
630
631 // Everything else needs the node set before it works.
632
633 // Returns a timestamp for the oldest message in this group of logfiles.
Austin Schuh858c9f32020-08-31 16:56:12 -0700634 monotonic_clock::time_point OldestMessageTime() const;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800635 // Pops the oldest message.
636 std::tuple<TimestampMerger::DeliveryTimestamp, int,
637 FlatbufferVector<MessageHeader>>
638 PopOldest();
639
640 // Returns the config for this set of log files.
641 const Configuration *configuration() const {
642 return log_file_header()->configuration();
643 }
644
645 const LogFileHeader *log_file_header() const {
646 return &log_file_header_.message();
647 }
648
649 // Returns the start times for the configured node's log files.
Austin Schuhcde938c2020-02-02 17:30:07 -0800650 monotonic_clock::time_point monotonic_start_time() const {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800651 return monotonic_clock::time_point(
652 std::chrono::nanoseconds(log_file_header()->monotonic_start_time()));
653 }
Austin Schuhcde938c2020-02-02 17:30:07 -0800654 realtime_clock::time_point realtime_start_time() const {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800655 return realtime_clock::time_point(
656 std::chrono::nanoseconds(log_file_header()->realtime_start_time()));
657 }
658
659 // Returns the node set by SetNode above.
660 const Node *node() const { return node_; }
661
662 // Called by the TimestampMerger when new data is available with the provided
663 // timestamp and channel_index.
664 void Update(monotonic_clock::time_point timestamp, int channel_index) {
665 PushChannelHeap(timestamp, channel_index);
666 }
667
Austin Schuhcde938c2020-02-02 17:30:07 -0800668 // Returns a debug string with all the heaps in it. Generally only useful for
669 // debugging what went wrong.
670 std::string DebugString() const;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800671
Austin Schuh8bd96322020-02-13 21:18:22 -0800672 // Returns true if one of the log files has finished reading everything. When
673 // log file chunks are involved, this means that the last chunk in a log file
674 // has been read. It is acceptable to be missing data at this point in time.
675 bool at_end() const { return at_end_; }
676
677 // Marks that one of the log files is at the end. This should only be called
678 // by timestamp mergers.
679 void NoticeAtEnd() { at_end_ = true; }
680
Austin Schuhcde938c2020-02-02 17:30:07 -0800681 private:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800682 // Pushes the timestamp for new data on the provided channel.
683 void PushChannelHeap(monotonic_clock::time_point timestamp,
684 int channel_index);
685
Austin Schuh2f8fd752020-09-01 22:38:28 -0700686 // CHECKs that channel_heap_ and timestamp_heap_ are valid heaps.
687 void VerifyHeaps();
688
Austin Schuh6f3babe2020-01-26 20:34:50 -0800689 // All the message readers.
690 std::vector<std::unique_ptr<SplitMessageReader>> split_message_readers_;
691
692 // The log header we are claiming to be.
Austin Schuh97789fc2020-08-01 14:42:45 -0700693 FlatbufferVector<LogFileHeader> log_file_header_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800694
695 // The timestamp mergers which combine data from the split message readers.
696 std::vector<TimestampMerger> timestamp_mergers_;
697
698 // A heap of the channel readers and timestamps for the oldest data in each.
Austin Schuh05b70472020-01-01 17:11:17 -0800699 std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap_;
700
Austin Schuh6f3babe2020-01-26 20:34:50 -0800701 // Configured node.
702 const Node *node_;
703
Austin Schuh8bd96322020-02-13 21:18:22 -0800704 bool at_end_ = false;
705
Austin Schuh6f3babe2020-01-26 20:34:50 -0800706 // Cached copy of the list of nodes.
707 std::vector<const Node *> nodes_;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700708
709 // Last time popped. Used to detect events being returned out of order.
710 monotonic_clock::time_point last_popped_time_ = monotonic_clock::min_time;
Austin Schuh05b70472020-01-01 17:11:17 -0800711};
Austin Schuha36c8902019-12-30 18:07:15 -0800712
Austin Schuhee711052020-08-24 16:06:09 -0700713// Returns the node name with a trailing space, or an empty string if we are on
714// a single node.
715std::string MaybeNodeName(const Node *);
716
Brian Silvermanf51499a2020-09-21 12:49:08 -0700717} // namespace aos::logger
Austin Schuha36c8902019-12-30 18:07:15 -0800718
719#endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_