Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_ |
| 2 | #define AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_ |
| 3 | |
| 4 | #include <sys/uio.h> |
| 5 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 6 | #include <chrono> |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 7 | #include <deque> |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 8 | #include <limits> |
| 9 | #include <memory> |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 10 | #include <optional> |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 11 | #include <string> |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 12 | #include <string_view> |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 13 | #include <tuple> |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 14 | #include <utility> |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 17 | #include "absl/types/span.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 18 | #include "aos/containers/resizeable_buffer.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 19 | #include "aos/events/event_loop.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 20 | #include "aos/events/logging/buffer_encoder.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 21 | #include "aos/events/logging/logger_generated.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 22 | #include "aos/flatbuffers.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 23 | #include "flatbuffers/flatbuffers.h" |
| 24 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 25 | namespace aos::logger { |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 26 | |
| 27 | enum 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 Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 36 | kLogMessageAndDeliveryTime, |
| 37 | // The message originated on the other node and should be logged on this node. |
| 38 | kLogRemoteMessage |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 41 | // This class manages efficiently writing a sequence of detached buffers to a |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 42 | // file. It encodes them, queues them up, and batches the write operation. |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 43 | class DetachedBufferWriter { |
| 44 | public: |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 45 | DetachedBufferWriter(std::string_view filename, |
| 46 | std::unique_ptr<DetachedBufferEncoder> encoder); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 47 | DetachedBufferWriter(DetachedBufferWriter &&other); |
| 48 | DetachedBufferWriter(const DetachedBufferWriter &) = delete; |
| 49 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 50 | ~DetachedBufferWriter(); |
| 51 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 52 | DetachedBufferWriter &operator=(DetachedBufferWriter &&other); |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 53 | DetachedBufferWriter &operator=(const DetachedBufferWriter &) = delete; |
| 54 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 55 | std::string_view filename() const { return filename_; } |
| 56 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 57 | // Queues up a finished FlatBufferBuilder to be encoded and written. |
| 58 | // |
| 59 | // Triggers a flush if there's enough data queued up. |
| 60 | // |
| 61 | // Steals the detached buffer from it. |
| 62 | void QueueSizedFlatbuffer(flatbuffers::FlatBufferBuilder *fbb) { |
| 63 | QueueSizedFlatbuffer(fbb->Release()); |
| 64 | } |
| 65 | // May steal the backing storage of buffer, or may leave it alone. |
| 66 | void QueueSizedFlatbuffer(flatbuffers::DetachedBuffer &&buffer) { |
| 67 | encoder_->Encode(std::move(buffer)); |
| 68 | FlushAtThreshold(); |
| 69 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 70 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 71 | // Queues up data in span. May copy or may write it to disk immediately. |
| 72 | void QueueSpan(absl::Span<const uint8_t> span); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 73 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 74 | // Indicates we got ENOSPC when trying to write. After this returns true, no |
| 75 | // further data is written. |
| 76 | bool ran_out_of_space() const { return ran_out_of_space_; } |
| 77 | |
| 78 | // To avoid silently failing to write logfiles, you must call this before |
| 79 | // destruction if ran_out_of_space() is true and the situation has been |
| 80 | // handled. |
| 81 | void acknowledge_out_of_space() { |
| 82 | CHECK(ran_out_of_space_); |
| 83 | acknowledge_ran_out_of_space_ = true; |
| 84 | } |
| 85 | |
| 86 | // Fully flushes and closes the underlying file now. No additional data may be |
| 87 | // enqueued after calling this. |
| 88 | // |
| 89 | // This will be performed in the destructor automatically. |
| 90 | // |
| 91 | // Note that this may set ran_out_of_space(). |
| 92 | void Close(); |
| 93 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 94 | // Returns the total number of bytes written and currently queued. |
| 95 | size_t total_bytes() const { return encoder_->total_bytes(); } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 96 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 97 | // The maximum time for a single write call, or 0 if none have been performed. |
| 98 | std::chrono::nanoseconds max_write_time() const { return max_write_time_; } |
| 99 | // The number of bytes in the longest write call, or -1 if none have been |
| 100 | // performed. |
| 101 | int max_write_time_bytes() const { return max_write_time_bytes_; } |
| 102 | // The number of buffers in the longest write call, or -1 if none have been |
| 103 | // performed. |
| 104 | int max_write_time_messages() const { return max_write_time_messages_; } |
| 105 | // The total time spent in write calls. |
| 106 | std::chrono::nanoseconds total_write_time() const { |
| 107 | return total_write_time_; |
| 108 | } |
| 109 | // The total number of writes which have been performed. |
| 110 | int total_write_count() const { return total_write_count_; } |
| 111 | // The total number of messages which have been written. |
| 112 | int total_write_messages() const { return total_write_messages_; } |
| 113 | // The total number of bytes which have been written. |
| 114 | int total_write_bytes() const { return total_write_bytes_; } |
| 115 | void ResetStatistics() { |
| 116 | max_write_time_ = std::chrono::nanoseconds::zero(); |
| 117 | max_write_time_bytes_ = -1; |
| 118 | max_write_time_messages_ = -1; |
| 119 | total_write_time_ = std::chrono::nanoseconds::zero(); |
| 120 | total_write_count_ = 0; |
| 121 | total_write_messages_ = 0; |
| 122 | total_write_bytes_ = 0; |
| 123 | } |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 124 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 125 | private: |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 126 | // Performs a single writev call with as much of the data we have queued up as |
| 127 | // possible. |
| 128 | // |
| 129 | // This will normally take all of the data we have queued up, unless an |
| 130 | // encoder has spit out a big enough chunk all at once that we can't manage |
| 131 | // all of it. |
| 132 | void Flush(); |
| 133 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 134 | // write_return is what write(2) or writev(2) returned. write_size is the |
| 135 | // number of bytes we expected it to write. |
| 136 | void HandleWriteReturn(ssize_t write_return, size_t write_size); |
| 137 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 138 | void UpdateStatsForWrite(aos::monotonic_clock::duration duration, |
| 139 | ssize_t written, int iovec_size); |
| 140 | |
| 141 | // Flushes data if we've reached the threshold to do that as part of normal |
| 142 | // operation. |
| 143 | void FlushAtThreshold(); |
| 144 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 145 | std::string filename_; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 146 | std::unique_ptr<DetachedBufferEncoder> encoder_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 147 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 148 | int fd_ = -1; |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 149 | bool ran_out_of_space_ = false; |
| 150 | bool acknowledge_ran_out_of_space_ = false; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 151 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 152 | // List of iovecs to use with writev. This is a member variable to avoid |
| 153 | // churn. |
| 154 | std::vector<struct iovec> iovec_; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 155 | |
| 156 | std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero(); |
| 157 | int max_write_time_bytes_ = -1; |
| 158 | int max_write_time_messages_ = -1; |
| 159 | std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero(); |
| 160 | int total_write_count_ = 0; |
| 161 | int total_write_messages_ = 0; |
| 162 | int total_write_bytes_ = 0; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | // Packes a message pointed to by the context into a MessageHeader. |
| 166 | flatbuffers::Offset<MessageHeader> PackMessage( |
| 167 | flatbuffers::FlatBufferBuilder *fbb, const Context &context, |
| 168 | int channel_index, LogType log_type); |
| 169 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 170 | FlatbufferVector<LogFileHeader> ReadHeader(std::string_view filename); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 171 | FlatbufferVector<MessageHeader> ReadNthMessage(std::string_view filename, |
| 172 | size_t n); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 173 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 174 | // Class to read chunks out of a log file. |
| 175 | class SpanReader { |
| 176 | public: |
| 177 | SpanReader(std::string_view filename); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 178 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 179 | std::string_view filename() const { return filename_; } |
| 180 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 181 | // Returns a span with the data for a message from the log file, excluding |
| 182 | // the size. |
| 183 | absl::Span<const uint8_t> ReadMessage(); |
| 184 | |
| 185 | // Returns true if there is a full message available in the buffer, or if we |
| 186 | // will have to read more data from disk. |
| 187 | bool MessageAvailable(); |
| 188 | |
| 189 | private: |
| 190 | // TODO(austin): Optimization: |
| 191 | // Allocate the 256k blocks like we do today. But, refcount them with |
| 192 | // shared_ptr pointed to by the messageheader that is returned. This avoids |
| 193 | // the copy. Need to do more benchmarking. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 194 | // And (Brian): Consider just mmapping the file and handing out refcounted |
| 195 | // pointers into that too. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 196 | |
| 197 | // Reads a chunk of data into data_. Returns false if no data was read. |
| 198 | bool ReadBlock(); |
| 199 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 200 | const std::string filename_; |
| 201 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 202 | // File reader and data decoder. |
| 203 | std::unique_ptr<DataDecoder> decoder_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 204 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 205 | // Vector to read into. |
| 206 | ResizeableBuffer data_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 207 | |
| 208 | // Amount of data consumed already in data_. |
| 209 | size_t consumed_data_ = 0; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | // Class which handles reading the header and messages from the log file. This |
| 213 | // handles any per-file state left before merging below. |
| 214 | class MessageReader { |
| 215 | public: |
| 216 | MessageReader(std::string_view filename); |
| 217 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 218 | std::string_view filename() const { return span_reader_.filename(); } |
| 219 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 220 | // Returns the header from the log file. |
| 221 | const LogFileHeader *log_file_header() const { |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 222 | return &raw_log_file_header_.message(); |
| 223 | } |
| 224 | |
| 225 | // Returns the raw data of the header from the log file. |
| 226 | const FlatbufferVector<LogFileHeader> &raw_log_file_header() const { |
| 227 | return raw_log_file_header_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Returns the minimum maount of data needed to queue up for sorting before |
| 231 | // ware guarenteed to not see data out of order. |
| 232 | std::chrono::nanoseconds max_out_of_order_duration() const { |
| 233 | return max_out_of_order_duration_; |
| 234 | } |
| 235 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 236 | // Returns the newest timestamp read out of the log file. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 237 | monotonic_clock::time_point newest_timestamp() const { |
| 238 | return newest_timestamp_; |
| 239 | } |
| 240 | |
| 241 | // Returns the next message if there is one. |
| 242 | std::optional<FlatbufferVector<MessageHeader>> ReadMessage(); |
| 243 | |
| 244 | // The time at which we need to read another chunk from the logfile. |
| 245 | monotonic_clock::time_point queue_data_time() const { |
| 246 | return newest_timestamp() - max_out_of_order_duration(); |
| 247 | } |
| 248 | |
| 249 | private: |
| 250 | // Log chunk reader. |
| 251 | SpanReader span_reader_; |
| 252 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 253 | // Vector holding the raw data for the log file header. |
| 254 | FlatbufferVector<LogFileHeader> raw_log_file_header_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 255 | |
| 256 | // Minimum amount of data to queue up for sorting before we are guarenteed |
| 257 | // to not see data out of order. |
| 258 | std::chrono::nanoseconds max_out_of_order_duration_; |
| 259 | |
| 260 | // Timestamp of the newest message in a channel queue. |
| 261 | monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time; |
| 262 | }; |
| 263 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 264 | class TimestampMerger; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 265 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 266 | // A design requirement is that the relevant data for a channel is not more than |
| 267 | // max_out_of_order_duration out of order. We approach sorting in layers. |
| 268 | // |
| 269 | // 1) Split each (maybe chunked) log file into one queue per channel. Read this |
| 270 | // log file looking for data pertaining to a specific node. |
| 271 | // (SplitMessageReader) |
| 272 | // 2) Merge all the data per channel from the different log files into a sorted |
| 273 | // list of timestamps and messages. (TimestampMerger) |
| 274 | // 3) Combine the timestamps and messages. (TimestampMerger) |
| 275 | // 4) Merge all the channels to produce the next message on a node. |
| 276 | // (ChannelMerger) |
| 277 | // 5) Duplicate this entire stack per node. |
| 278 | |
| 279 | // This class splits messages and timestamps up into a queue per channel, and |
| 280 | // handles reading data from multiple chunks. |
| 281 | class SplitMessageReader { |
| 282 | public: |
| 283 | SplitMessageReader(const std::vector<std::string> &filenames); |
| 284 | |
| 285 | // Sets the TimestampMerger that gets notified for each channel. The node |
| 286 | // that the TimestampMerger is merging as needs to be passed in. |
| 287 | void SetTimestampMerger(TimestampMerger *timestamp_merger, int channel, |
| 288 | const Node *target_node); |
| 289 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 290 | // Returns the (timestamp, queue_index, message_header) for the oldest message |
| 291 | // in a channel, or max_time if there is nothing in the channel. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 292 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 293 | oldest_message(int channel) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 294 | return channels_[channel].data.front_timestamp(); |
| 295 | } |
| 296 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 297 | // Returns the (timestamp, queue_index, message_header) for the oldest |
| 298 | // delivery time in a channel, or max_time if there is nothing in the channel. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 299 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 300 | oldest_message(int channel, int destination_node) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 301 | return channels_[channel].timestamps[destination_node].front_timestamp(); |
| 302 | } |
| 303 | |
| 304 | // Returns the timestamp, queue_index, and message for the oldest data on a |
| 305 | // channel. Requeues data as needed. |
| 306 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 307 | FlatbufferVector<MessageHeader>> |
| 308 | PopOldest(int channel_index); |
| 309 | |
| 310 | // Returns the timestamp, queue_index, and message for the oldest timestamp on |
| 311 | // a channel delivered to a node. Requeues data as needed. |
| 312 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 313 | FlatbufferVector<MessageHeader>> |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 314 | PopOldestTimestamp(int channel, int node_index); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 315 | |
| 316 | // Returns the header for the log files. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 317 | const LogFileHeader *log_file_header() const { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 318 | return &log_file_header_.message(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 321 | const FlatbufferVector<LogFileHeader> &raw_log_file_header() const { |
| 322 | return log_file_header_; |
| 323 | } |
| 324 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 325 | // Returns the starting time for this set of log files. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 326 | monotonic_clock::time_point monotonic_start_time() { |
| 327 | return monotonic_clock::time_point( |
| 328 | std::chrono::nanoseconds(log_file_header()->monotonic_start_time())); |
| 329 | } |
| 330 | realtime_clock::time_point realtime_start_time() { |
| 331 | return realtime_clock::time_point( |
| 332 | std::chrono::nanoseconds(log_file_header()->realtime_start_time())); |
| 333 | } |
| 334 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 335 | // Returns the configuration from the log file header. |
| 336 | const Configuration *configuration() const { |
| 337 | return log_file_header()->configuration(); |
| 338 | } |
| 339 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 340 | // Returns the node who's point of view this log file is from. Make sure this |
| 341 | // is a pointer in the configuration() nodes list so it can be consumed |
| 342 | // elsewhere. |
| 343 | const Node *node() const { |
| 344 | if (configuration()->has_nodes()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 345 | return configuration::GetNodeOrDie(configuration(), |
| 346 | log_file_header()->node()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 347 | } else { |
| 348 | CHECK(!log_file_header()->has_node()); |
| 349 | return nullptr; |
| 350 | } |
| 351 | } |
| 352 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 353 | // Returns the timestamp of the newest message read from the log file, and the |
| 354 | // timestamp that we need to re-queue data. |
| 355 | monotonic_clock::time_point newest_timestamp() const { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 356 | return newest_timestamp_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 359 | // Returns the next time to trigger a requeue. |
| 360 | monotonic_clock::time_point time_to_queue() const { return time_to_queue_; } |
| 361 | |
| 362 | // Returns the minimum amount of data needed to queue up for sorting before |
| 363 | // ware guarenteed to not see data out of order. |
| 364 | std::chrono::nanoseconds max_out_of_order_duration() const { |
| 365 | return message_reader_->max_out_of_order_duration(); |
| 366 | } |
| 367 | |
| 368 | std::string_view filename() const { return message_reader_->filename(); } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 369 | |
| 370 | // Adds more messages to the sorted list. This reads enough data such that |
| 371 | // oldest_message_time can be replayed safely. Returns false if the log file |
| 372 | // has all been read. |
| 373 | bool QueueMessages(monotonic_clock::time_point oldest_message_time); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 374 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 375 | // Returns debug strings for a channel, and timestamps for a node. |
| 376 | std::string DebugString(int channel) const; |
| 377 | std::string DebugString(int channel, int node_index) const; |
| 378 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 379 | // Returns true if all the messages have been queued from the last log file in |
| 380 | // the list of log files chunks. |
| 381 | bool at_end() const { return at_end_; } |
| 382 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 383 | private: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 384 | // TODO(austin): Need to copy or refcount the message instead of running |
| 385 | // multiple copies of the reader. Or maybe have a "as_node" index and hide it |
| 386 | // inside. |
| 387 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 388 | // Moves to the next log file in the list. |
| 389 | bool NextLogFile(); |
| 390 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 391 | // Filenames of the log files. |
| 392 | std::vector<std::string> filenames_; |
| 393 | // And the index of the next file to open. |
| 394 | size_t next_filename_index_ = 0; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 395 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 396 | // Node we are reading as. |
| 397 | const Node *target_node_ = nullptr; |
| 398 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 399 | // Log file header to report. This is a copy. |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 400 | FlatbufferVector<LogFileHeader> log_file_header_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 401 | // Current log file being read. |
| 402 | std::unique_ptr<MessageReader> message_reader_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 403 | |
| 404 | // Datastructure to hold the list of messages, cached timestamp for the |
| 405 | // oldest message, and sender to send with. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 406 | struct MessageHeaderQueue { |
| 407 | // If true, this is a timestamp queue. |
| 408 | bool timestamps = false; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 409 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 410 | // Returns a reference to the the oldest message. |
| 411 | FlatbufferVector<MessageHeader> &front() { |
| 412 | CHECK_GT(data_.size(), 0u); |
| 413 | return data_.front(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 414 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 415 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 416 | // Adds a message to the back of the queue. Returns true if it was actually |
| 417 | // emplaced. |
| 418 | bool emplace_back(FlatbufferVector<MessageHeader> &&msg); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 419 | |
| 420 | // Drops the front message. Invalidates the front() reference. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 421 | void PopFront(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 422 | |
| 423 | // The size of the queue. |
| 424 | size_t size() { return data_.size(); } |
| 425 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 426 | // Returns a debug string with info about each message in the queue. |
| 427 | std::string DebugString() const; |
| 428 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 429 | // Returns the (timestamp, queue_index, message_header) for the oldest |
| 430 | // message. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 431 | const std::tuple<monotonic_clock::time_point, uint32_t, |
| 432 | const MessageHeader *> |
| 433 | front_timestamp() { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 434 | const MessageHeader &message = front().message(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 435 | return std::make_tuple( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 436 | monotonic_clock::time_point( |
| 437 | std::chrono::nanoseconds(message.monotonic_sent_time())), |
| 438 | message.queue_index(), &message); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | // Pointer to the timestamp merger for this queue if available. |
| 442 | TimestampMerger *timestamp_merger = nullptr; |
| 443 | // Pointer to the reader which feeds this queue. |
| 444 | SplitMessageReader *split_reader = nullptr; |
| 445 | |
| 446 | private: |
| 447 | // The data. |
| 448 | std::deque<FlatbufferVector<MessageHeader>> data_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 449 | }; |
| 450 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 451 | // All the queues needed for a channel. There isn't going to be data in all |
| 452 | // of these. |
| 453 | struct ChannelData { |
| 454 | // The data queue for the channel. |
| 455 | MessageHeaderQueue data; |
| 456 | // Queues for timestamps for each node. |
| 457 | std::vector<MessageHeaderQueue> timestamps; |
| 458 | }; |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 459 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 460 | // Data for all the channels. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 461 | std::vector<ChannelData> channels_; |
| 462 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 463 | // Once we know the node that this SplitMessageReader will be writing as, |
| 464 | // there will be only one MessageHeaderQueue that a specific channel matches. |
| 465 | // Precompute this here for efficiency. |
| 466 | std::vector<MessageHeaderQueue *> channels_to_write_; |
| 467 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 468 | monotonic_clock::time_point time_to_queue_ = monotonic_clock::min_time; |
| 469 | |
| 470 | // Latches true when we hit the end of the last log file and there is no sense |
| 471 | // poking it further. |
| 472 | bool at_end_ = false; |
| 473 | |
| 474 | // Timestamp of the newest message that was read and actually queued. We want |
| 475 | // to track this independently from the log file because we need the |
| 476 | // timestamps here to be timestamps of messages that are queued. |
| 477 | monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 478 | }; |
| 479 | |
| 480 | class ChannelMerger; |
| 481 | |
| 482 | // Sorts channels (and timestamps) from multiple log files for a single channel. |
| 483 | class TimestampMerger { |
| 484 | public: |
| 485 | TimestampMerger(const Configuration *configuration, |
| 486 | std::vector<SplitMessageReader *> split_message_readers, |
| 487 | int channel_index, const Node *target_node, |
| 488 | ChannelMerger *channel_merger); |
| 489 | |
| 490 | // Metadata used to schedule the message. |
| 491 | struct DeliveryTimestamp { |
| 492 | monotonic_clock::time_point monotonic_event_time = |
| 493 | monotonic_clock::min_time; |
| 494 | realtime_clock::time_point realtime_event_time = realtime_clock::min_time; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame^] | 495 | uint32_t queue_index = 0xffffffff; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 496 | |
| 497 | monotonic_clock::time_point monotonic_remote_time = |
| 498 | monotonic_clock::min_time; |
| 499 | realtime_clock::time_point realtime_remote_time = realtime_clock::min_time; |
| 500 | uint32_t remote_queue_index = 0xffffffff; |
| 501 | }; |
| 502 | |
| 503 | // Pushes SplitMessageReader onto the timestamp heap. This should only be |
| 504 | // called when timestamps are placed in the channel this class is merging for |
| 505 | // the reader. |
| 506 | void UpdateTimestamp( |
| 507 | SplitMessageReader *split_message_reader, |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 508 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 509 | oldest_message_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 510 | PushTimestampHeap(oldest_message_time, split_message_reader); |
| 511 | } |
| 512 | // Pushes SplitMessageReader onto the message heap. This should only be |
| 513 | // called when data is placed in the channel this class is merging for the |
| 514 | // reader. |
| 515 | void Update( |
| 516 | SplitMessageReader *split_message_reader, |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 517 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 518 | oldest_message_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 519 | PushMessageHeap(oldest_message_time, split_message_reader); |
| 520 | } |
| 521 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 522 | // Returns the oldest combined timestamp and data for this channel. If there |
| 523 | // isn't a matching piece of data, returns only the timestamp with no data. |
| 524 | // The caller can determine what the appropriate action is to recover. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 525 | std::tuple<DeliveryTimestamp, FlatbufferVector<MessageHeader>> PopOldest(); |
| 526 | |
| 527 | // Tracks if the channel merger has pushed this onto it's heap or not. |
| 528 | bool pushed() { return pushed_; } |
| 529 | // Sets if this has been pushed to the channel merger heap. Should only be |
| 530 | // called by the channel merger. |
| 531 | void set_pushed(bool pushed) { pushed_ = pushed; } |
| 532 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 533 | // Returns a debug string with the heaps printed out. |
| 534 | std::string DebugString() const; |
| 535 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 536 | // Returns true if we have timestamps. |
| 537 | bool has_timestamps() const { return has_timestamps_; } |
| 538 | |
| 539 | // Records that one of the log files ran out of data. This should only be |
| 540 | // called by a SplitMessageReader. |
| 541 | void NoticeAtEnd(); |
| 542 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 543 | aos::monotonic_clock::time_point channel_merger_time() { |
| 544 | if (has_timestamps_) { |
| 545 | return std::get<0>(timestamp_heap_[0]); |
| 546 | } else { |
| 547 | return std::get<0>(message_heap_[0]); |
| 548 | } |
| 549 | } |
| 550 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 551 | private: |
| 552 | // Pushes messages and timestamps to the corresponding heaps. |
| 553 | void PushMessageHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 554 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 555 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 556 | SplitMessageReader *split_message_reader); |
| 557 | void PushTimestampHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 558 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 559 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 560 | SplitMessageReader *split_message_reader); |
| 561 | |
| 562 | // Pops a message from the message heap. This automatically triggers the |
| 563 | // split message reader to re-fetch any new data. |
| 564 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 565 | FlatbufferVector<MessageHeader>> |
| 566 | PopMessageHeap(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 567 | |
| 568 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 569 | oldest_message() const; |
| 570 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 571 | oldest_timestamp() const; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 572 | // Pops a message from the timestamp heap. This automatically triggers the |
| 573 | // split message reader to re-fetch any new data. |
| 574 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 575 | FlatbufferVector<MessageHeader>> |
| 576 | PopTimestampHeap(); |
| 577 | |
| 578 | const Configuration *configuration_; |
| 579 | |
| 580 | // If true, this is a forwarded channel and timestamps should be matched. |
| 581 | bool has_timestamps_ = false; |
| 582 | |
| 583 | // Tracks if the ChannelMerger has pushed this onto it's queue. |
| 584 | bool pushed_ = false; |
| 585 | |
| 586 | // The split message readers used for source data. |
| 587 | std::vector<SplitMessageReader *> split_message_readers_; |
| 588 | |
| 589 | // The channel to merge. |
| 590 | int channel_index_; |
| 591 | |
| 592 | // Our node. |
| 593 | int node_index_; |
| 594 | |
| 595 | // Heaps for messages and timestamps. |
| 596 | std::vector< |
| 597 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 598 | message_heap_; |
| 599 | std::vector< |
| 600 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 601 | timestamp_heap_; |
| 602 | |
| 603 | // Parent channel merger. |
| 604 | ChannelMerger *channel_merger_; |
| 605 | }; |
| 606 | |
| 607 | // This class handles constructing all the split message readers, channel |
| 608 | // mergers, and combining the results. |
| 609 | class ChannelMerger { |
| 610 | public: |
| 611 | // Builds a ChannelMerger around a set of log files. These are of the format: |
| 612 | // { |
| 613 | // {log1_part0, log1_part1, ...}, |
| 614 | // {log2} |
| 615 | // } |
| 616 | // The inner vector is a list of log file chunks which form up a log file. |
| 617 | // The outer vector is a list of log files with subsets of the messages, or |
| 618 | // messages from different nodes. |
| 619 | ChannelMerger(const std::vector<std::vector<std::string>> &filenames); |
| 620 | |
| 621 | // Returns the nodes that we know how to merge. |
| 622 | const std::vector<const Node *> nodes() const; |
| 623 | // Sets the node that we will return messages as. Returns true if the node |
| 624 | // has log files and will produce data. This can only be called once, and |
| 625 | // will likely corrupt state if called a second time. |
| 626 | bool SetNode(const Node *target_node); |
| 627 | |
| 628 | // Everything else needs the node set before it works. |
| 629 | |
| 630 | // Returns a timestamp for the oldest message in this group of logfiles. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 631 | monotonic_clock::time_point OldestMessageTime() const; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 632 | // Pops the oldest message. |
| 633 | std::tuple<TimestampMerger::DeliveryTimestamp, int, |
| 634 | FlatbufferVector<MessageHeader>> |
| 635 | PopOldest(); |
| 636 | |
| 637 | // Returns the config for this set of log files. |
| 638 | const Configuration *configuration() const { |
| 639 | return log_file_header()->configuration(); |
| 640 | } |
| 641 | |
| 642 | const LogFileHeader *log_file_header() const { |
| 643 | return &log_file_header_.message(); |
| 644 | } |
| 645 | |
| 646 | // Returns the start times for the configured node's log files. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 647 | monotonic_clock::time_point monotonic_start_time() const { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 648 | return monotonic_clock::time_point( |
| 649 | std::chrono::nanoseconds(log_file_header()->monotonic_start_time())); |
| 650 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 651 | realtime_clock::time_point realtime_start_time() const { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 652 | return realtime_clock::time_point( |
| 653 | std::chrono::nanoseconds(log_file_header()->realtime_start_time())); |
| 654 | } |
| 655 | |
| 656 | // Returns the node set by SetNode above. |
| 657 | const Node *node() const { return node_; } |
| 658 | |
| 659 | // Called by the TimestampMerger when new data is available with the provided |
| 660 | // timestamp and channel_index. |
| 661 | void Update(monotonic_clock::time_point timestamp, int channel_index) { |
| 662 | PushChannelHeap(timestamp, channel_index); |
| 663 | } |
| 664 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 665 | // Returns a debug string with all the heaps in it. Generally only useful for |
| 666 | // debugging what went wrong. |
| 667 | std::string DebugString() const; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 668 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 669 | // Returns true if one of the log files has finished reading everything. When |
| 670 | // log file chunks are involved, this means that the last chunk in a log file |
| 671 | // has been read. It is acceptable to be missing data at this point in time. |
| 672 | bool at_end() const { return at_end_; } |
| 673 | |
| 674 | // Marks that one of the log files is at the end. This should only be called |
| 675 | // by timestamp mergers. |
| 676 | void NoticeAtEnd() { at_end_ = true; } |
| 677 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 678 | private: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 679 | // Pushes the timestamp for new data on the provided channel. |
| 680 | void PushChannelHeap(monotonic_clock::time_point timestamp, |
| 681 | int channel_index); |
| 682 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 683 | // CHECKs that channel_heap_ and timestamp_heap_ are valid heaps. |
| 684 | void VerifyHeaps(); |
| 685 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 686 | // All the message readers. |
| 687 | std::vector<std::unique_ptr<SplitMessageReader>> split_message_readers_; |
| 688 | |
| 689 | // The log header we are claiming to be. |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 690 | FlatbufferVector<LogFileHeader> log_file_header_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 691 | |
| 692 | // The timestamp mergers which combine data from the split message readers. |
| 693 | std::vector<TimestampMerger> timestamp_mergers_; |
| 694 | |
| 695 | // A heap of the channel readers and timestamps for the oldest data in each. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 696 | std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap_; |
| 697 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 698 | // Configured node. |
| 699 | const Node *node_; |
| 700 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 701 | bool at_end_ = false; |
| 702 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 703 | // Cached copy of the list of nodes. |
| 704 | std::vector<const Node *> nodes_; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 705 | |
| 706 | // Last time popped. Used to detect events being returned out of order. |
| 707 | monotonic_clock::time_point last_popped_time_ = monotonic_clock::min_time; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 708 | }; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 709 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 710 | // Returns the node name with a trailing space, or an empty string if we are on |
| 711 | // a single node. |
| 712 | std::string MaybeNodeName(const Node *); |
| 713 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 714 | } // namespace aos::logger |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 715 | |
| 716 | #endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_ |