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 | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 17 | #include "absl/container/btree_set.h" |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 18 | #include "absl/types/span.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 19 | #include "aos/containers/resizeable_buffer.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 20 | #include "aos/events/event_loop.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 21 | #include "aos/events/logging/buffer_encoder.h" |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 22 | #include "aos/events/logging/logfile_sorting.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 23 | #include "aos/events/logging/logger_generated.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 24 | #include "aos/flatbuffers.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 25 | #include "flatbuffers/flatbuffers.h" |
| 26 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 27 | namespace aos::logger { |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 28 | |
| 29 | enum class LogType : uint8_t { |
| 30 | // The message originated on this node and should be logged here. |
| 31 | kLogMessage, |
| 32 | // The message originated on another node, but only the delivery times are |
| 33 | // logged here. |
| 34 | kLogDeliveryTimeOnly, |
| 35 | // The message originated on another node. Log it and the delivery times |
| 36 | // together. The message_gateway is responsible for logging any messages |
| 37 | // which didn't get delivered. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 38 | kLogMessageAndDeliveryTime, |
| 39 | // The message originated on the other node and should be logged on this node. |
| 40 | kLogRemoteMessage |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 43 | // This class manages efficiently writing a sequence of detached buffers to a |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 44 | // file. It encodes them, queues them up, and batches the write operation. |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 45 | class DetachedBufferWriter { |
| 46 | public: |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 47 | // Marker struct for one of our constructor overloads. |
| 48 | struct already_out_of_space_t {}; |
| 49 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 50 | DetachedBufferWriter(std::string_view filename, |
| 51 | std::unique_ptr<DetachedBufferEncoder> encoder); |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 52 | // Creates a dummy instance which won't even open a file. It will act as if |
| 53 | // opening the file ran out of space immediately. |
| 54 | DetachedBufferWriter(already_out_of_space_t) : ran_out_of_space_(true) {} |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 55 | DetachedBufferWriter(DetachedBufferWriter &&other); |
| 56 | DetachedBufferWriter(const DetachedBufferWriter &) = delete; |
| 57 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 58 | ~DetachedBufferWriter(); |
| 59 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 60 | DetachedBufferWriter &operator=(DetachedBufferWriter &&other); |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 61 | DetachedBufferWriter &operator=(const DetachedBufferWriter &) = delete; |
| 62 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 63 | std::string_view filename() const { return filename_; } |
| 64 | |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 65 | // This will be true until Close() is called, unless the file couldn't be |
| 66 | // created due to running out of space. |
| 67 | bool is_open() const { return fd_ != -1; } |
| 68 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 69 | // Queues up a finished FlatBufferBuilder to be encoded and written. |
| 70 | // |
| 71 | // Triggers a flush if there's enough data queued up. |
| 72 | // |
| 73 | // Steals the detached buffer from it. |
| 74 | void QueueSizedFlatbuffer(flatbuffers::FlatBufferBuilder *fbb) { |
| 75 | QueueSizedFlatbuffer(fbb->Release()); |
| 76 | } |
| 77 | // May steal the backing storage of buffer, or may leave it alone. |
| 78 | void QueueSizedFlatbuffer(flatbuffers::DetachedBuffer &&buffer) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 79 | if (ran_out_of_space_) { |
| 80 | return; |
| 81 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 82 | encoder_->Encode(std::move(buffer)); |
| 83 | FlushAtThreshold(); |
| 84 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 85 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 86 | // Queues up data in span. May copy or may write it to disk immediately. |
| 87 | void QueueSpan(absl::Span<const uint8_t> span); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 88 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 89 | // Indicates we got ENOSPC when trying to write. After this returns true, no |
| 90 | // further data is written. |
| 91 | bool ran_out_of_space() const { return ran_out_of_space_; } |
| 92 | |
| 93 | // To avoid silently failing to write logfiles, you must call this before |
| 94 | // destruction if ran_out_of_space() is true and the situation has been |
| 95 | // handled. |
| 96 | void acknowledge_out_of_space() { |
| 97 | CHECK(ran_out_of_space_); |
| 98 | acknowledge_ran_out_of_space_ = true; |
| 99 | } |
| 100 | |
| 101 | // Fully flushes and closes the underlying file now. No additional data may be |
| 102 | // enqueued after calling this. |
| 103 | // |
| 104 | // This will be performed in the destructor automatically. |
| 105 | // |
| 106 | // Note that this may set ran_out_of_space(). |
| 107 | void Close(); |
| 108 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 109 | // Returns the total number of bytes written and currently queued. |
| 110 | size_t total_bytes() const { return encoder_->total_bytes(); } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 111 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 112 | // The maximum time for a single write call, or 0 if none have been performed. |
| 113 | std::chrono::nanoseconds max_write_time() const { return max_write_time_; } |
| 114 | // The number of bytes in the longest write call, or -1 if none have been |
| 115 | // performed. |
| 116 | int max_write_time_bytes() const { return max_write_time_bytes_; } |
| 117 | // The number of buffers in the longest write call, or -1 if none have been |
| 118 | // performed. |
| 119 | int max_write_time_messages() const { return max_write_time_messages_; } |
| 120 | // The total time spent in write calls. |
| 121 | std::chrono::nanoseconds total_write_time() const { |
| 122 | return total_write_time_; |
| 123 | } |
| 124 | // The total number of writes which have been performed. |
| 125 | int total_write_count() const { return total_write_count_; } |
| 126 | // The total number of messages which have been written. |
| 127 | int total_write_messages() const { return total_write_messages_; } |
| 128 | // The total number of bytes which have been written. |
| 129 | int total_write_bytes() const { return total_write_bytes_; } |
| 130 | void ResetStatistics() { |
| 131 | max_write_time_ = std::chrono::nanoseconds::zero(); |
| 132 | max_write_time_bytes_ = -1; |
| 133 | max_write_time_messages_ = -1; |
| 134 | total_write_time_ = std::chrono::nanoseconds::zero(); |
| 135 | total_write_count_ = 0; |
| 136 | total_write_messages_ = 0; |
| 137 | total_write_bytes_ = 0; |
| 138 | } |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 139 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 140 | private: |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 141 | // Performs a single writev call with as much of the data we have queued up as |
| 142 | // possible. |
| 143 | // |
| 144 | // This will normally take all of the data we have queued up, unless an |
| 145 | // encoder has spit out a big enough chunk all at once that we can't manage |
| 146 | // all of it. |
| 147 | void Flush(); |
| 148 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 149 | // write_return is what write(2) or writev(2) returned. write_size is the |
| 150 | // number of bytes we expected it to write. |
| 151 | void HandleWriteReturn(ssize_t write_return, size_t write_size); |
| 152 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 153 | void UpdateStatsForWrite(aos::monotonic_clock::duration duration, |
| 154 | ssize_t written, int iovec_size); |
| 155 | |
| 156 | // Flushes data if we've reached the threshold to do that as part of normal |
| 157 | // operation. |
| 158 | void FlushAtThreshold(); |
| 159 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 160 | std::string filename_; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 161 | std::unique_ptr<DetachedBufferEncoder> encoder_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 162 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 163 | int fd_ = -1; |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 164 | bool ran_out_of_space_ = false; |
| 165 | bool acknowledge_ran_out_of_space_ = false; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 166 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 167 | // List of iovecs to use with writev. This is a member variable to avoid |
| 168 | // churn. |
| 169 | std::vector<struct iovec> iovec_; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 170 | |
| 171 | std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero(); |
| 172 | int max_write_time_bytes_ = -1; |
| 173 | int max_write_time_messages_ = -1; |
| 174 | std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero(); |
| 175 | int total_write_count_ = 0; |
| 176 | int total_write_messages_ = 0; |
| 177 | int total_write_bytes_ = 0; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | // Packes a message pointed to by the context into a MessageHeader. |
| 181 | flatbuffers::Offset<MessageHeader> PackMessage( |
| 182 | flatbuffers::FlatBufferBuilder *fbb, const Context &context, |
| 183 | int channel_index, LogType log_type); |
| 184 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 185 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader( |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 186 | std::string_view filename); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 187 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage( |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 188 | std::string_view filename, size_t n); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 189 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 190 | // Class to read chunks out of a log file. |
| 191 | class SpanReader { |
| 192 | public: |
| 193 | SpanReader(std::string_view filename); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 194 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 195 | std::string_view filename() const { return filename_; } |
| 196 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 197 | // Returns a span with the data for a message from the log file, excluding |
| 198 | // the size. |
| 199 | absl::Span<const uint8_t> ReadMessage(); |
| 200 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 201 | private: |
| 202 | // TODO(austin): Optimization: |
| 203 | // Allocate the 256k blocks like we do today. But, refcount them with |
| 204 | // shared_ptr pointed to by the messageheader that is returned. This avoids |
| 205 | // the copy. Need to do more benchmarking. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 206 | // And (Brian): Consider just mmapping the file and handing out refcounted |
| 207 | // pointers into that too. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 208 | |
| 209 | // Reads a chunk of data into data_. Returns false if no data was read. |
| 210 | bool ReadBlock(); |
| 211 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 212 | std::string filename_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 213 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 214 | // File reader and data decoder. |
| 215 | std::unique_ptr<DataDecoder> decoder_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 216 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 217 | // Vector to read into. |
| 218 | ResizeableBuffer data_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 219 | |
| 220 | // Amount of data consumed already in data_. |
| 221 | size_t consumed_data_ = 0; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | // Class which handles reading the header and messages from the log file. This |
| 225 | // handles any per-file state left before merging below. |
| 226 | class MessageReader { |
| 227 | public: |
| 228 | MessageReader(std::string_view filename); |
| 229 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 230 | std::string_view filename() const { return span_reader_.filename(); } |
| 231 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 232 | // Returns the header from the log file. |
| 233 | const LogFileHeader *log_file_header() const { |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 234 | return &raw_log_file_header_.message(); |
| 235 | } |
| 236 | |
| 237 | // Returns the raw data of the header from the log file. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 238 | const SizePrefixedFlatbufferVector<LogFileHeader> &raw_log_file_header() |
| 239 | const { |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 240 | return raw_log_file_header_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // Returns the minimum maount of data needed to queue up for sorting before |
| 244 | // ware guarenteed to not see data out of order. |
| 245 | std::chrono::nanoseconds max_out_of_order_duration() const { |
| 246 | return max_out_of_order_duration_; |
| 247 | } |
| 248 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 249 | // Returns the newest timestamp read out of the log file. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 250 | monotonic_clock::time_point newest_timestamp() const { |
| 251 | return newest_timestamp_; |
| 252 | } |
| 253 | |
| 254 | // Returns the next message if there is one. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 255 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadMessage(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 256 | |
| 257 | // The time at which we need to read another chunk from the logfile. |
| 258 | monotonic_clock::time_point queue_data_time() const { |
| 259 | return newest_timestamp() - max_out_of_order_duration(); |
| 260 | } |
| 261 | |
| 262 | private: |
| 263 | // Log chunk reader. |
| 264 | SpanReader span_reader_; |
| 265 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 266 | // Vector holding the raw data for the log file header. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 267 | SizePrefixedFlatbufferVector<LogFileHeader> raw_log_file_header_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 268 | |
| 269 | // Minimum amount of data to queue up for sorting before we are guarenteed |
| 270 | // to not see data out of order. |
| 271 | std::chrono::nanoseconds max_out_of_order_duration_; |
| 272 | |
| 273 | // Timestamp of the newest message in a channel queue. |
| 274 | monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time; |
| 275 | }; |
| 276 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 277 | // A class to seamlessly read messages from a list of part files. |
| 278 | class PartsMessageReader { |
| 279 | public: |
| 280 | PartsMessageReader(LogParts log_parts); |
| 281 | |
| 282 | std::string_view filename() const { return message_reader_.filename(); } |
| 283 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 284 | // Returns the LogParts that holds the filenames we are reading. |
| 285 | const LogParts &parts() const { return parts_; } |
| 286 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 287 | const LogFileHeader *log_file_header() const { |
| 288 | return message_reader_.log_file_header(); |
| 289 | } |
| 290 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 291 | // Returns the minimum amount of data needed to queue up for sorting before |
| 292 | // we are guarenteed to not see data out of order. |
| 293 | std::chrono::nanoseconds max_out_of_order_duration() const { |
| 294 | return message_reader_.max_out_of_order_duration(); |
| 295 | } |
| 296 | |
| 297 | // Returns the newest timestamp read out of the log file. |
| 298 | monotonic_clock::time_point newest_timestamp() const { |
| 299 | return newest_timestamp_; |
| 300 | } |
| 301 | |
| 302 | // Returns the next message if there is one, or nullopt if we have reached the |
| 303 | // end of all the files. |
| 304 | // Note: reading the next message may change the max_out_of_order_duration(). |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 305 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadMessage(); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 306 | |
| 307 | private: |
| 308 | // Opens the next log and updates message_reader_. Sets done_ if there is |
| 309 | // nothing more to do. |
| 310 | void NextLog(); |
| 311 | |
| 312 | const LogParts parts_; |
| 313 | size_t next_part_index_ = 1u; |
| 314 | bool done_ = false; |
| 315 | MessageReader message_reader_; |
| 316 | |
| 317 | monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time; |
| 318 | }; |
| 319 | |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 320 | // Struct to hold a message as it gets sorted on a single node. |
| 321 | struct Message { |
| 322 | // The channel. |
| 323 | uint32_t channel_index = 0xffffffff; |
| 324 | // The local queue index. |
| 325 | uint32_t queue_index = 0xffffffff; |
| 326 | // The local timestamp on the monotonic clock. |
| 327 | monotonic_clock::time_point timestamp = monotonic_clock::min_time; |
| 328 | // The data (either a timestamp header, or a data header). |
| 329 | SizePrefixedFlatbufferVector<MessageHeader> data; |
| 330 | |
| 331 | bool operator<(const Message &m2) const; |
| 332 | bool operator>=(const Message &m2) const; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 333 | bool operator==(const Message &m2) const; |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | std::ostream &operator<<(std::ostream &os, const Message &m); |
| 337 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 338 | // Structure to hold a full message and all the timestamps, which may or may not |
| 339 | // have been sent from a remote node. The remote_queue_index will be invalid if |
| 340 | // this message is from the point of view of the node which sent it. |
| 341 | struct TimestampedMessage { |
| 342 | uint32_t channel_index = 0xffffffff; |
| 343 | |
| 344 | uint32_t queue_index = 0xffffffff; |
| 345 | monotonic_clock::time_point monotonic_event_time = monotonic_clock::min_time; |
| 346 | realtime_clock::time_point realtime_event_time = realtime_clock::min_time; |
| 347 | |
| 348 | uint32_t remote_queue_index = 0xffffffff; |
| 349 | monotonic_clock::time_point monotonic_remote_time = monotonic_clock::min_time; |
| 350 | realtime_clock::time_point realtime_remote_time = realtime_clock::min_time; |
| 351 | |
| 352 | SizePrefixedFlatbufferVector<MessageHeader> data; |
| 353 | }; |
| 354 | |
| 355 | std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m); |
| 356 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 357 | // Class to sort the resulting messages from a PartsMessageReader. |
| 358 | class LogPartsSorter { |
| 359 | public: |
| 360 | LogPartsSorter(LogParts log_parts); |
| 361 | |
| 362 | // Returns the current log file header. |
| 363 | // TODO(austin): Is this the header we want to report? Do we want a better |
| 364 | // start time? |
| 365 | // TODO(austin): Report a start time from the LogParts. Figure out how that |
| 366 | // all works. |
| 367 | const LogFileHeader *log_file_header() const { |
| 368 | return parts_message_reader_.log_file_header(); |
| 369 | } |
| 370 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 371 | monotonic_clock::time_point monotonic_start_time() const { |
| 372 | return parts_message_reader_.parts().monotonic_start_time; |
| 373 | } |
| 374 | realtime_clock::time_point realtime_start_time() const { |
| 375 | return parts_message_reader_.parts().realtime_start_time; |
| 376 | } |
| 377 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 378 | // The time this data is sorted until. |
| 379 | monotonic_clock::time_point sorted_until() const { return sorted_until_; } |
| 380 | |
| 381 | // Returns the next sorted message from the log file. It is safe to call |
| 382 | // std::move() on the result to move the data flatbuffer from it. |
| 383 | Message *Front(); |
| 384 | // Pops the front message. This should only be called after a call to |
| 385 | // Front(). |
| 386 | void PopFront(); |
| 387 | |
| 388 | // Returns a debug string representing the contents of this sorter. |
| 389 | std::string DebugString() const; |
| 390 | |
| 391 | private: |
| 392 | // Log parts reader we are wrapping. |
| 393 | PartsMessageReader parts_message_reader_; |
| 394 | // Cache of the time we are sorted until. |
| 395 | aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time; |
| 396 | |
| 397 | // Set used for efficient sorting of messages. We can benchmark and evaluate |
| 398 | // other data structures if this proves to be the bottleneck. |
| 399 | absl::btree_set<Message> messages_; |
| 400 | }; |
| 401 | |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 402 | // Class to run merge sort on the messages from multiple LogPartsSorter |
| 403 | // instances. |
| 404 | class NodeMerger { |
| 405 | public: |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 406 | NodeMerger(std::vector<LogParts> parts); |
| 407 | |
| 408 | // Node index in the configuration of this node. |
| 409 | int node() const { return node_; } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 410 | |
| 411 | // The log file header for one of the log files. |
| 412 | const LogFileHeader *log_file_header() const { |
| 413 | CHECK(!parts_sorters_.empty()); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 414 | return parts_sorters_[0].log_file_header(); |
| 415 | } |
| 416 | |
| 417 | monotonic_clock::time_point monotonic_start_time() const { |
| 418 | return monotonic_start_time_; |
| 419 | } |
| 420 | realtime_clock::time_point realtime_start_time() const { |
| 421 | return realtime_start_time_; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // The time this data is sorted until. |
| 425 | monotonic_clock::time_point sorted_until() const { return sorted_until_; } |
| 426 | |
| 427 | // Returns the next sorted message from the set of log files. It is safe to |
| 428 | // call std::move() on the result to move the data flatbuffer from it. |
| 429 | Message *Front(); |
| 430 | // Pops the front message. This should only be called after a call to |
| 431 | // Front(). |
| 432 | void PopFront(); |
| 433 | |
| 434 | private: |
| 435 | // Unsorted list of all parts sorters. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 436 | std::vector<LogPartsSorter> parts_sorters_; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 437 | // Pointer to the parts sorter holding the current Front message if one |
| 438 | // exists, or nullptr if a new one needs to be found. |
| 439 | LogPartsSorter *current_ = nullptr; |
| 440 | // Cached sorted_until value. |
| 441 | aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 442 | |
| 443 | // Cached node. |
| 444 | int node_; |
| 445 | |
| 446 | realtime_clock::time_point realtime_start_time_ = realtime_clock::max_time; |
| 447 | monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::max_time; |
| 448 | }; |
| 449 | |
| 450 | // Class to match timestamps with the corresponding data from other nodes. |
| 451 | class TimestampMapper { |
| 452 | public: |
| 453 | TimestampMapper(std::vector<LogParts> file); |
| 454 | |
| 455 | // Copying and moving will mess up the internal raw pointers. Just don't do |
| 456 | // it. |
| 457 | TimestampMapper(TimestampMapper const &) = delete; |
| 458 | TimestampMapper(TimestampMapper &&) = delete; |
| 459 | void operator=(TimestampMapper const &) = delete; |
| 460 | void operator=(TimestampMapper &&) = delete; |
| 461 | |
| 462 | // TODO(austin): It would be super helpful to provide a way to queue up to |
| 463 | // time X without matching timestamps, and to then be able to pull the |
| 464 | // timestamps out of this queue. This lets us bootstrap time estimation |
| 465 | // without exploding memory usage worst case. |
| 466 | |
| 467 | // Returns a log file header for this node. |
| 468 | const LogFileHeader *log_file_header() const { |
| 469 | return node_merger_.log_file_header(); |
| 470 | } |
| 471 | |
| 472 | // Returns which node this is sorting for. |
| 473 | size_t node() const { return node_; } |
| 474 | |
| 475 | // The start time of this log. |
| 476 | monotonic_clock::time_point monotonic_start_time() const { |
| 477 | return node_merger_.monotonic_start_time(); |
| 478 | } |
| 479 | realtime_clock::time_point realtime_start_time() const { |
| 480 | return node_merger_.realtime_start_time(); |
| 481 | } |
| 482 | |
| 483 | // Uses timestamp_mapper as the peer for its node. Only one mapper may be set |
| 484 | // for each node. Peers are used to look up the data for timestamps on this |
| 485 | // node. |
| 486 | void AddPeer(TimestampMapper *timestamp_mapper); |
| 487 | |
| 488 | // Time that we are sorted until internally. |
| 489 | monotonic_clock::time_point sorted_until() const { |
| 490 | return node_merger_.sorted_until(); |
| 491 | } |
| 492 | |
| 493 | // Returns the next message for this node. |
| 494 | TimestampedMessage *Front(); |
| 495 | // Pops the next message. Front must be called first. |
| 496 | void PopFront(); |
| 497 | |
| 498 | // Returns debug information about this node. |
| 499 | std::string DebugString() const; |
| 500 | |
| 501 | private: |
| 502 | // The state for a remote node. This holds the data that needs to be matched |
| 503 | // with the remote node's timestamps. |
| 504 | struct NodeData { |
| 505 | // True if we should save data here. This should be true if any of the |
| 506 | // bools in delivered below are true. |
| 507 | bool any_delivered = false; |
| 508 | |
| 509 | // Peer pointer. This node is only to be considered if a peer is set. |
| 510 | TimestampMapper *peer = nullptr; |
| 511 | |
| 512 | struct ChannelData { |
| 513 | // Deque per channel. This contains the data from the outside |
| 514 | // TimestampMapper node which is relevant for the node this NodeData |
| 515 | // points to. |
| 516 | std::deque<Message> messages; |
| 517 | // Bool tracking per channel if a message is delivered to the node this |
| 518 | // NodeData represents. |
| 519 | bool delivered = false; |
| 520 | }; |
| 521 | |
| 522 | // Vector with per channel data. |
| 523 | std::vector<ChannelData> channels; |
| 524 | }; |
| 525 | |
| 526 | // Returns (and forgets about) the data for the provided timestamp message |
| 527 | // showing when it was delivered to this node. |
| 528 | Message MatchingMessageFor(const Message &message); |
| 529 | |
| 530 | // Queues up a single message into our message queue, and any nodes that this |
| 531 | // message is delivered to. Returns true if one was available, false |
| 532 | // otherwise. |
| 533 | bool Queue(); |
| 534 | |
| 535 | // Queues up data until we have at least one message >= to time t. |
| 536 | // Useful for triggering a remote node to read enough data to have the |
| 537 | // timestamp you care about available. |
| 538 | void QueueUntil(monotonic_clock::time_point t); |
| 539 | |
| 540 | // Fills message_ with the contents of m. |
| 541 | void FillMessage(Message *m); |
| 542 | |
| 543 | // The node merger to source messages from. |
| 544 | NodeMerger node_merger_; |
| 545 | // Our node. |
| 546 | const size_t node_; |
| 547 | // The buffer of messages for this node. These are not matched with any |
| 548 | // remote data. |
| 549 | std::deque<Message> messages_; |
| 550 | // The node index for the source node for each channel. |
| 551 | std::vector<size_t> source_node_; |
| 552 | |
| 553 | // Vector per node. Not all nodes will have anything. |
| 554 | std::vector<NodeData> nodes_data_; |
| 555 | |
| 556 | // Latest message to return. |
| 557 | TimestampedMessage message_; |
| 558 | |
| 559 | // Tracks if the first message points to message_, nullptr (all done), or is |
| 560 | // invalid. |
| 561 | enum class FirstMessage { |
| 562 | kNeedsUpdate, |
| 563 | kInMessage, |
| 564 | kNullptr, |
| 565 | }; |
| 566 | FirstMessage first_message_ = FirstMessage::kNeedsUpdate; |
| 567 | |
| 568 | // Timestamp of the last message returned. Used to make sure nothing goes |
| 569 | // backwards. |
| 570 | monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time; |
| 571 | // Time this node is queued up until. Used for caching. |
| 572 | monotonic_clock::time_point queued_until_ = monotonic_clock::min_time; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 573 | }; |
| 574 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 575 | class TimestampMerger; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 576 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 577 | // A design requirement is that the relevant data for a channel is not more than |
| 578 | // max_out_of_order_duration out of order. We approach sorting in layers. |
| 579 | // |
| 580 | // 1) Split each (maybe chunked) log file into one queue per channel. Read this |
| 581 | // log file looking for data pertaining to a specific node. |
| 582 | // (SplitMessageReader) |
| 583 | // 2) Merge all the data per channel from the different log files into a sorted |
| 584 | // list of timestamps and messages. (TimestampMerger) |
| 585 | // 3) Combine the timestamps and messages. (TimestampMerger) |
| 586 | // 4) Merge all the channels to produce the next message on a node. |
| 587 | // (ChannelMerger) |
| 588 | // 5) Duplicate this entire stack per node. |
| 589 | |
| 590 | // This class splits messages and timestamps up into a queue per channel, and |
| 591 | // handles reading data from multiple chunks. |
| 592 | class SplitMessageReader { |
| 593 | public: |
| 594 | SplitMessageReader(const std::vector<std::string> &filenames); |
| 595 | |
| 596 | // Sets the TimestampMerger that gets notified for each channel. The node |
| 597 | // that the TimestampMerger is merging as needs to be passed in. |
| 598 | void SetTimestampMerger(TimestampMerger *timestamp_merger, int channel, |
| 599 | const Node *target_node); |
| 600 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 601 | // Returns the (timestamp, queue_index, message_header) for the oldest message |
| 602 | // 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] | 603 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 604 | oldest_message(int channel) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 605 | return channels_[channel].data.front_timestamp(); |
| 606 | } |
| 607 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 608 | // Returns the (timestamp, queue_index, message_header) for the oldest |
| 609 | // 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] | 610 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 611 | oldest_message(int channel, int destination_node) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 612 | return channels_[channel].timestamps[destination_node].front_timestamp(); |
| 613 | } |
| 614 | |
| 615 | // Returns the timestamp, queue_index, and message for the oldest data on a |
| 616 | // channel. Requeues data as needed. |
| 617 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 618 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 619 | PopOldest(int channel_index); |
| 620 | |
| 621 | // Returns the timestamp, queue_index, and message for the oldest timestamp on |
| 622 | // a channel delivered to a node. Requeues data as needed. |
| 623 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 624 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 625 | PopOldestTimestamp(int channel, int node_index); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 626 | |
| 627 | // Returns the header for the log files. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 628 | const LogFileHeader *log_file_header() const { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 629 | return &log_file_header_.message(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 632 | const SizePrefixedFlatbufferVector<LogFileHeader> &raw_log_file_header() |
| 633 | const { |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 634 | return log_file_header_; |
| 635 | } |
| 636 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 637 | // Returns the starting time for this set of log files. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 638 | monotonic_clock::time_point monotonic_start_time() { |
| 639 | return monotonic_clock::time_point( |
| 640 | std::chrono::nanoseconds(log_file_header()->monotonic_start_time())); |
| 641 | } |
| 642 | realtime_clock::time_point realtime_start_time() { |
| 643 | return realtime_clock::time_point( |
| 644 | std::chrono::nanoseconds(log_file_header()->realtime_start_time())); |
| 645 | } |
| 646 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 647 | // Returns the configuration from the log file header. |
| 648 | const Configuration *configuration() const { |
| 649 | return log_file_header()->configuration(); |
| 650 | } |
| 651 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 652 | // Returns the node who's point of view this log file is from. Make sure this |
| 653 | // is a pointer in the configuration() nodes list so it can be consumed |
| 654 | // elsewhere. |
| 655 | const Node *node() const { |
| 656 | if (configuration()->has_nodes()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 657 | return configuration::GetNodeOrDie(configuration(), |
| 658 | log_file_header()->node()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 659 | } else { |
| 660 | CHECK(!log_file_header()->has_node()); |
| 661 | return nullptr; |
| 662 | } |
| 663 | } |
| 664 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 665 | // Returns the timestamp of the newest message read from the log file, and the |
| 666 | // timestamp that we need to re-queue data. |
| 667 | monotonic_clock::time_point newest_timestamp() const { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 668 | return newest_timestamp_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 671 | // Returns the next time to trigger a requeue. |
| 672 | monotonic_clock::time_point time_to_queue() const { return time_to_queue_; } |
| 673 | |
| 674 | // Returns the minimum amount of data needed to queue up for sorting before |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 675 | // we are guarenteed to not see data out of order. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 676 | std::chrono::nanoseconds max_out_of_order_duration() const { |
| 677 | return message_reader_->max_out_of_order_duration(); |
| 678 | } |
| 679 | |
| 680 | std::string_view filename() const { return message_reader_->filename(); } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 681 | |
| 682 | // Adds more messages to the sorted list. This reads enough data such that |
| 683 | // oldest_message_time can be replayed safely. Returns false if the log file |
| 684 | // has all been read. |
| 685 | bool QueueMessages(monotonic_clock::time_point oldest_message_time); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 686 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 687 | // Returns debug strings for a channel, and timestamps for a node. |
| 688 | std::string DebugString(int channel) const; |
| 689 | std::string DebugString(int channel, int node_index) const; |
| 690 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 691 | // Returns true if all the messages have been queued from the last log file in |
| 692 | // the list of log files chunks. |
| 693 | bool at_end() const { return at_end_; } |
| 694 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 695 | private: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 696 | // TODO(austin): Need to copy or refcount the message instead of running |
| 697 | // multiple copies of the reader. Or maybe have a "as_node" index and hide it |
| 698 | // inside. |
| 699 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 700 | // Moves to the next log file in the list. |
| 701 | bool NextLogFile(); |
| 702 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 703 | // Filenames of the log files. |
| 704 | std::vector<std::string> filenames_; |
| 705 | // And the index of the next file to open. |
| 706 | size_t next_filename_index_ = 0; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 707 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 708 | // Node we are reading as. |
| 709 | const Node *target_node_ = nullptr; |
| 710 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 711 | // Log file header to report. This is a copy. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 712 | SizePrefixedFlatbufferVector<LogFileHeader> log_file_header_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 713 | // Current log file being read. |
| 714 | std::unique_ptr<MessageReader> message_reader_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 715 | |
| 716 | // Datastructure to hold the list of messages, cached timestamp for the |
| 717 | // oldest message, and sender to send with. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 718 | struct MessageHeaderQueue { |
| 719 | // If true, this is a timestamp queue. |
| 720 | bool timestamps = false; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 721 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 722 | // Returns a reference to the the oldest message. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 723 | SizePrefixedFlatbufferVector<MessageHeader> &front() { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 724 | CHECK_GT(data_.size(), 0u); |
| 725 | return data_.front(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 726 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 727 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 728 | // Adds a message to the back of the queue. Returns true if it was actually |
| 729 | // emplaced. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 730 | bool emplace_back(SizePrefixedFlatbufferVector<MessageHeader> &&msg); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 731 | |
| 732 | // Drops the front message. Invalidates the front() reference. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 733 | void PopFront(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 734 | |
| 735 | // The size of the queue. |
| 736 | size_t size() { return data_.size(); } |
| 737 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 738 | // Returns a debug string with info about each message in the queue. |
| 739 | std::string DebugString() const; |
| 740 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 741 | // Returns the (timestamp, queue_index, message_header) for the oldest |
| 742 | // message. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 743 | const std::tuple<monotonic_clock::time_point, uint32_t, |
| 744 | const MessageHeader *> |
| 745 | front_timestamp() { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 746 | const MessageHeader &message = front().message(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 747 | return std::make_tuple( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 748 | monotonic_clock::time_point( |
| 749 | std::chrono::nanoseconds(message.monotonic_sent_time())), |
| 750 | message.queue_index(), &message); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | // Pointer to the timestamp merger for this queue if available. |
| 754 | TimestampMerger *timestamp_merger = nullptr; |
| 755 | // Pointer to the reader which feeds this queue. |
| 756 | SplitMessageReader *split_reader = nullptr; |
| 757 | |
| 758 | private: |
| 759 | // The data. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 760 | std::deque<SizePrefixedFlatbufferVector<MessageHeader>> data_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 761 | }; |
| 762 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 763 | // All the queues needed for a channel. There isn't going to be data in all |
| 764 | // of these. |
| 765 | struct ChannelData { |
| 766 | // The data queue for the channel. |
| 767 | MessageHeaderQueue data; |
| 768 | // Queues for timestamps for each node. |
| 769 | std::vector<MessageHeaderQueue> timestamps; |
| 770 | }; |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 771 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 772 | // Data for all the channels. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 773 | std::vector<ChannelData> channels_; |
| 774 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 775 | // Once we know the node that this SplitMessageReader will be writing as, |
| 776 | // there will be only one MessageHeaderQueue that a specific channel matches. |
| 777 | // Precompute this here for efficiency. |
| 778 | std::vector<MessageHeaderQueue *> channels_to_write_; |
| 779 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 780 | monotonic_clock::time_point time_to_queue_ = monotonic_clock::min_time; |
| 781 | |
| 782 | // Latches true when we hit the end of the last log file and there is no sense |
| 783 | // poking it further. |
| 784 | bool at_end_ = false; |
| 785 | |
| 786 | // Timestamp of the newest message that was read and actually queued. We want |
| 787 | // to track this independently from the log file because we need the |
| 788 | // timestamps here to be timestamps of messages that are queued. |
| 789 | monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 790 | }; |
| 791 | |
| 792 | class ChannelMerger; |
| 793 | |
| 794 | // Sorts channels (and timestamps) from multiple log files for a single channel. |
| 795 | class TimestampMerger { |
| 796 | public: |
| 797 | TimestampMerger(const Configuration *configuration, |
| 798 | std::vector<SplitMessageReader *> split_message_readers, |
| 799 | int channel_index, const Node *target_node, |
| 800 | ChannelMerger *channel_merger); |
| 801 | |
| 802 | // Metadata used to schedule the message. |
| 803 | struct DeliveryTimestamp { |
| 804 | monotonic_clock::time_point monotonic_event_time = |
| 805 | monotonic_clock::min_time; |
| 806 | realtime_clock::time_point realtime_event_time = realtime_clock::min_time; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 807 | uint32_t queue_index = 0xffffffff; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 808 | |
| 809 | monotonic_clock::time_point monotonic_remote_time = |
| 810 | monotonic_clock::min_time; |
| 811 | realtime_clock::time_point realtime_remote_time = realtime_clock::min_time; |
| 812 | uint32_t remote_queue_index = 0xffffffff; |
| 813 | }; |
| 814 | |
| 815 | // Pushes SplitMessageReader onto the timestamp heap. This should only be |
| 816 | // called when timestamps are placed in the channel this class is merging for |
| 817 | // the reader. |
| 818 | void UpdateTimestamp( |
| 819 | SplitMessageReader *split_message_reader, |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 820 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 821 | oldest_message_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 822 | PushTimestampHeap(oldest_message_time, split_message_reader); |
| 823 | } |
| 824 | // Pushes SplitMessageReader onto the message heap. This should only be |
| 825 | // called when data is placed in the channel this class is merging for the |
| 826 | // reader. |
| 827 | void Update( |
| 828 | SplitMessageReader *split_message_reader, |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 829 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 830 | oldest_message_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 831 | PushMessageHeap(oldest_message_time, split_message_reader); |
| 832 | } |
| 833 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 834 | // Returns the oldest combined timestamp and data for this channel. If there |
| 835 | // isn't a matching piece of data, returns only the timestamp with no data. |
| 836 | // The caller can determine what the appropriate action is to recover. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 837 | std::tuple<DeliveryTimestamp, SizePrefixedFlatbufferVector<MessageHeader>> |
| 838 | PopOldest(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 839 | |
| 840 | // Tracks if the channel merger has pushed this onto it's heap or not. |
| 841 | bool pushed() { return pushed_; } |
| 842 | // Sets if this has been pushed to the channel merger heap. Should only be |
| 843 | // called by the channel merger. |
| 844 | void set_pushed(bool pushed) { pushed_ = pushed; } |
| 845 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 846 | // Returns a debug string with the heaps printed out. |
| 847 | std::string DebugString() const; |
| 848 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 849 | // Returns true if we have timestamps. |
| 850 | bool has_timestamps() const { return has_timestamps_; } |
| 851 | |
| 852 | // Records that one of the log files ran out of data. This should only be |
| 853 | // called by a SplitMessageReader. |
| 854 | void NoticeAtEnd(); |
| 855 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 856 | aos::monotonic_clock::time_point channel_merger_time() { |
| 857 | if (has_timestamps_) { |
| 858 | return std::get<0>(timestamp_heap_[0]); |
| 859 | } else { |
| 860 | return std::get<0>(message_heap_[0]); |
| 861 | } |
| 862 | } |
| 863 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 864 | private: |
| 865 | // Pushes messages and timestamps to the corresponding heaps. |
| 866 | void PushMessageHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 867 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 868 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 869 | SplitMessageReader *split_message_reader); |
| 870 | void PushTimestampHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 871 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 872 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 873 | SplitMessageReader *split_message_reader); |
| 874 | |
| 875 | // Pops a message from the message heap. This automatically triggers the |
| 876 | // split message reader to re-fetch any new data. |
| 877 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 878 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 879 | PopMessageHeap(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 880 | |
| 881 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 882 | oldest_message() const; |
| 883 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 884 | oldest_timestamp() const; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 885 | // Pops a message from the timestamp heap. This automatically triggers the |
| 886 | // split message reader to re-fetch any new data. |
| 887 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 888 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 889 | PopTimestampHeap(); |
| 890 | |
| 891 | const Configuration *configuration_; |
| 892 | |
| 893 | // If true, this is a forwarded channel and timestamps should be matched. |
| 894 | bool has_timestamps_ = false; |
| 895 | |
| 896 | // Tracks if the ChannelMerger has pushed this onto it's queue. |
| 897 | bool pushed_ = false; |
| 898 | |
| 899 | // The split message readers used for source data. |
| 900 | std::vector<SplitMessageReader *> split_message_readers_; |
| 901 | |
| 902 | // The channel to merge. |
| 903 | int channel_index_; |
| 904 | |
| 905 | // Our node. |
| 906 | int node_index_; |
| 907 | |
| 908 | // Heaps for messages and timestamps. |
| 909 | std::vector< |
| 910 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 911 | message_heap_; |
| 912 | std::vector< |
| 913 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 914 | timestamp_heap_; |
| 915 | |
| 916 | // Parent channel merger. |
| 917 | ChannelMerger *channel_merger_; |
| 918 | }; |
| 919 | |
| 920 | // This class handles constructing all the split message readers, channel |
| 921 | // mergers, and combining the results. |
| 922 | class ChannelMerger { |
| 923 | public: |
| 924 | // Builds a ChannelMerger around a set of log files. These are of the format: |
| 925 | // { |
| 926 | // {log1_part0, log1_part1, ...}, |
| 927 | // {log2} |
| 928 | // } |
| 929 | // The inner vector is a list of log file chunks which form up a log file. |
| 930 | // The outer vector is a list of log files with subsets of the messages, or |
| 931 | // messages from different nodes. |
| 932 | ChannelMerger(const std::vector<std::vector<std::string>> &filenames); |
| 933 | |
| 934 | // Returns the nodes that we know how to merge. |
| 935 | const std::vector<const Node *> nodes() const; |
| 936 | // Sets the node that we will return messages as. Returns true if the node |
| 937 | // has log files and will produce data. This can only be called once, and |
| 938 | // will likely corrupt state if called a second time. |
| 939 | bool SetNode(const Node *target_node); |
| 940 | |
| 941 | // Everything else needs the node set before it works. |
| 942 | |
| 943 | // Returns a timestamp for the oldest message in this group of logfiles. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 944 | monotonic_clock::time_point OldestMessageTime() const; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 945 | // Pops the oldest message. |
| 946 | std::tuple<TimestampMerger::DeliveryTimestamp, int, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 947 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 948 | PopOldest(); |
| 949 | |
| 950 | // Returns the config for this set of log files. |
| 951 | const Configuration *configuration() const { |
| 952 | return log_file_header()->configuration(); |
| 953 | } |
| 954 | |
| 955 | const LogFileHeader *log_file_header() const { |
| 956 | return &log_file_header_.message(); |
| 957 | } |
| 958 | |
| 959 | // Returns the start times for the configured node's log files. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 960 | monotonic_clock::time_point monotonic_start_time() const { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 961 | return monotonic_clock::time_point( |
| 962 | std::chrono::nanoseconds(log_file_header()->monotonic_start_time())); |
| 963 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 964 | realtime_clock::time_point realtime_start_time() const { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 965 | return realtime_clock::time_point( |
| 966 | std::chrono::nanoseconds(log_file_header()->realtime_start_time())); |
| 967 | } |
| 968 | |
| 969 | // Returns the node set by SetNode above. |
| 970 | const Node *node() const { return node_; } |
| 971 | |
| 972 | // Called by the TimestampMerger when new data is available with the provided |
| 973 | // timestamp and channel_index. |
| 974 | void Update(monotonic_clock::time_point timestamp, int channel_index) { |
| 975 | PushChannelHeap(timestamp, channel_index); |
| 976 | } |
| 977 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 978 | // Returns a debug string with all the heaps in it. Generally only useful for |
| 979 | // debugging what went wrong. |
| 980 | std::string DebugString() const; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 981 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 982 | // Returns true if one of the log files has finished reading everything. When |
| 983 | // log file chunks are involved, this means that the last chunk in a log file |
| 984 | // has been read. It is acceptable to be missing data at this point in time. |
| 985 | bool at_end() const { return at_end_; } |
| 986 | |
| 987 | // Marks that one of the log files is at the end. This should only be called |
| 988 | // by timestamp mergers. |
| 989 | void NoticeAtEnd() { at_end_ = true; } |
| 990 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 991 | private: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 992 | // Pushes the timestamp for new data on the provided channel. |
| 993 | void PushChannelHeap(monotonic_clock::time_point timestamp, |
| 994 | int channel_index); |
| 995 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 996 | // CHECKs that channel_heap_ and timestamp_heap_ are valid heaps. |
| 997 | void VerifyHeaps(); |
| 998 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 999 | // All the message readers. |
| 1000 | std::vector<std::unique_ptr<SplitMessageReader>> split_message_readers_; |
| 1001 | |
| 1002 | // The log header we are claiming to be. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 1003 | SizePrefixedFlatbufferVector<LogFileHeader> log_file_header_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1004 | |
| 1005 | // The timestamp mergers which combine data from the split message readers. |
| 1006 | std::vector<TimestampMerger> timestamp_mergers_; |
| 1007 | |
| 1008 | // 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] | 1009 | std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap_; |
| 1010 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1011 | // Configured node. |
| 1012 | const Node *node_; |
| 1013 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1014 | bool at_end_ = false; |
| 1015 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1016 | // Cached copy of the list of nodes. |
| 1017 | std::vector<const Node *> nodes_; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1018 | |
| 1019 | // Last time popped. Used to detect events being returned out of order. |
| 1020 | monotonic_clock::time_point last_popped_time_ = monotonic_clock::min_time; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1021 | }; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 1022 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1023 | // Returns the node name with a trailing space, or an empty string if we are on |
| 1024 | // a single node. |
| 1025 | std::string MaybeNodeName(const Node *); |
| 1026 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 1027 | } // namespace aos::logger |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 1028 | |
| 1029 | #endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_ |