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 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 317 | // True after we have seen a message after the start of the log. The |
| 318 | // guarentees on logging essentially are that all data from before the |
| 319 | // starting time of the log may be arbitrarily out of order, but once we get |
| 320 | // max_out_of_order_duration past the start, everything will remain within |
| 321 | // max_out_of_order_duration. We shouldn't see anything before the start |
| 322 | // after we've seen a message that is at least max_out_of_order_duration after |
| 323 | // the start. |
| 324 | bool after_start_ = false; |
| 325 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 326 | monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time; |
| 327 | }; |
| 328 | |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 329 | // Struct to hold a message as it gets sorted on a single node. |
| 330 | struct Message { |
| 331 | // The channel. |
| 332 | uint32_t channel_index = 0xffffffff; |
| 333 | // The local queue index. |
| 334 | uint32_t queue_index = 0xffffffff; |
| 335 | // The local timestamp on the monotonic clock. |
| 336 | monotonic_clock::time_point timestamp = monotonic_clock::min_time; |
| 337 | // The data (either a timestamp header, or a data header). |
| 338 | SizePrefixedFlatbufferVector<MessageHeader> data; |
| 339 | |
| 340 | bool operator<(const Message &m2) const; |
| 341 | bool operator>=(const Message &m2) const; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 342 | bool operator==(const Message &m2) const; |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | std::ostream &operator<<(std::ostream &os, const Message &m); |
| 346 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 347 | // Structure to hold a full message and all the timestamps, which may or may not |
| 348 | // have been sent from a remote node. The remote_queue_index will be invalid if |
| 349 | // this message is from the point of view of the node which sent it. |
| 350 | struct TimestampedMessage { |
| 351 | uint32_t channel_index = 0xffffffff; |
| 352 | |
| 353 | uint32_t queue_index = 0xffffffff; |
| 354 | monotonic_clock::time_point monotonic_event_time = monotonic_clock::min_time; |
| 355 | realtime_clock::time_point realtime_event_time = realtime_clock::min_time; |
| 356 | |
| 357 | uint32_t remote_queue_index = 0xffffffff; |
| 358 | monotonic_clock::time_point monotonic_remote_time = monotonic_clock::min_time; |
| 359 | realtime_clock::time_point realtime_remote_time = realtime_clock::min_time; |
| 360 | |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 361 | monotonic_clock::time_point monotonic_timestamp_time = |
| 362 | monotonic_clock::min_time; |
| 363 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 364 | SizePrefixedFlatbufferVector<MessageHeader> data; |
| 365 | }; |
| 366 | |
| 367 | std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m); |
| 368 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 369 | // Class to sort the resulting messages from a PartsMessageReader. |
| 370 | class LogPartsSorter { |
| 371 | public: |
| 372 | LogPartsSorter(LogParts log_parts); |
| 373 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 374 | // Returns the parts that this is sorting messages from. |
| 375 | const LogParts &parts() const { return parts_message_reader_.parts(); } |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 376 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 377 | monotonic_clock::time_point monotonic_start_time() const { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 378 | return parts().monotonic_start_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 379 | } |
| 380 | realtime_clock::time_point realtime_start_time() const { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 381 | return parts().realtime_start_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 384 | // The time this data is sorted until. |
| 385 | monotonic_clock::time_point sorted_until() const { return sorted_until_; } |
| 386 | |
| 387 | // Returns the next sorted message from the log file. It is safe to call |
| 388 | // std::move() on the result to move the data flatbuffer from it. |
| 389 | Message *Front(); |
| 390 | // Pops the front message. This should only be called after a call to |
| 391 | // Front(). |
| 392 | void PopFront(); |
| 393 | |
| 394 | // Returns a debug string representing the contents of this sorter. |
| 395 | std::string DebugString() const; |
| 396 | |
| 397 | private: |
| 398 | // Log parts reader we are wrapping. |
| 399 | PartsMessageReader parts_message_reader_; |
| 400 | // Cache of the time we are sorted until. |
| 401 | aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time; |
| 402 | |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 403 | // Timestamp of the last message returned. Used to make sure nothing goes |
| 404 | // backwards. |
| 405 | monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time; |
| 406 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 407 | // Set used for efficient sorting of messages. We can benchmark and evaluate |
| 408 | // other data structures if this proves to be the bottleneck. |
| 409 | absl::btree_set<Message> messages_; |
| 410 | }; |
| 411 | |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 412 | // Class to run merge sort on the messages from multiple LogPartsSorter |
| 413 | // instances. |
| 414 | class NodeMerger { |
| 415 | public: |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 416 | NodeMerger(std::vector<LogParts> parts); |
| 417 | |
| 418 | // Node index in the configuration of this node. |
| 419 | int node() const { return node_; } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 420 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 421 | // List of parts being sorted together. |
| 422 | std::vector<const LogParts *> Parts() const; |
| 423 | |
| 424 | const Configuration *configuration() const { |
| 425 | return parts_sorters_[0].parts().config.get(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | monotonic_clock::time_point monotonic_start_time() const { |
| 429 | return monotonic_start_time_; |
| 430 | } |
| 431 | realtime_clock::time_point realtime_start_time() const { |
| 432 | return realtime_start_time_; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // The time this data is sorted until. |
| 436 | monotonic_clock::time_point sorted_until() const { return sorted_until_; } |
| 437 | |
| 438 | // Returns the next sorted message from the set of log files. It is safe to |
| 439 | // call std::move() on the result to move the data flatbuffer from it. |
| 440 | Message *Front(); |
| 441 | // Pops the front message. This should only be called after a call to |
| 442 | // Front(). |
| 443 | void PopFront(); |
| 444 | |
| 445 | private: |
| 446 | // Unsorted list of all parts sorters. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 447 | std::vector<LogPartsSorter> parts_sorters_; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 448 | // Pointer to the parts sorter holding the current Front message if one |
| 449 | // exists, or nullptr if a new one needs to be found. |
| 450 | LogPartsSorter *current_ = nullptr; |
| 451 | // Cached sorted_until value. |
| 452 | aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 453 | |
| 454 | // Cached node. |
| 455 | int node_; |
| 456 | |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 457 | // Timestamp of the last message returned. Used to make sure nothing goes |
| 458 | // backwards. |
| 459 | monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time; |
| 460 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 461 | realtime_clock::time_point realtime_start_time_ = realtime_clock::max_time; |
| 462 | monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::max_time; |
| 463 | }; |
| 464 | |
| 465 | // Class to match timestamps with the corresponding data from other nodes. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 466 | // |
| 467 | // This class also buffers data for the node it represents, and supports |
| 468 | // notifying when new data is queued as well as queueing until a point in time. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 469 | class TimestampMapper { |
| 470 | public: |
| 471 | TimestampMapper(std::vector<LogParts> file); |
| 472 | |
| 473 | // Copying and moving will mess up the internal raw pointers. Just don't do |
| 474 | // it. |
| 475 | TimestampMapper(TimestampMapper const &) = delete; |
| 476 | TimestampMapper(TimestampMapper &&) = delete; |
| 477 | void operator=(TimestampMapper const &) = delete; |
| 478 | void operator=(TimestampMapper &&) = delete; |
| 479 | |
| 480 | // TODO(austin): It would be super helpful to provide a way to queue up to |
| 481 | // time X without matching timestamps, and to then be able to pull the |
| 482 | // timestamps out of this queue. This lets us bootstrap time estimation |
| 483 | // without exploding memory usage worst case. |
| 484 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 485 | std::vector<const LogParts *> Parts() const { return node_merger_.Parts(); } |
| 486 | |
| 487 | const Configuration *configuration() const { return configuration_.get(); } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 488 | |
| 489 | // Returns which node this is sorting for. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 490 | size_t node() const { return node_merger_.node(); } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 491 | |
| 492 | // The start time of this log. |
| 493 | monotonic_clock::time_point monotonic_start_time() const { |
| 494 | return node_merger_.monotonic_start_time(); |
| 495 | } |
| 496 | realtime_clock::time_point realtime_start_time() const { |
| 497 | return node_merger_.realtime_start_time(); |
| 498 | } |
| 499 | |
| 500 | // Uses timestamp_mapper as the peer for its node. Only one mapper may be set |
| 501 | // for each node. Peers are used to look up the data for timestamps on this |
| 502 | // node. |
| 503 | void AddPeer(TimestampMapper *timestamp_mapper); |
| 504 | |
| 505 | // Time that we are sorted until internally. |
| 506 | monotonic_clock::time_point sorted_until() const { |
| 507 | return node_merger_.sorted_until(); |
| 508 | } |
| 509 | |
| 510 | // Returns the next message for this node. |
| 511 | TimestampedMessage *Front(); |
| 512 | // Pops the next message. Front must be called first. |
| 513 | void PopFront(); |
| 514 | |
| 515 | // Returns debug information about this node. |
| 516 | std::string DebugString() const; |
| 517 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 518 | // Queues data the provided time. |
| 519 | void QueueUntil(monotonic_clock::time_point queue_time); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 520 | // Queues until we have time_estimation_buffer of data in the queue. |
| 521 | void QueueFor(std::chrono::nanoseconds time_estimation_buffer); |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 522 | |
Austin Schuh | 0660122 | 2021-01-26 17:02:50 -0800 | [diff] [blame] | 523 | // Queues until the condition is met. |
| 524 | template <typename T> |
| 525 | void QueueUntilCondition(T fn) { |
| 526 | while (true) { |
| 527 | if (fn()) { |
| 528 | break; |
| 529 | } |
| 530 | if (!QueueMatched()) { |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 536 | // Sets a callback to be called whenever a full message is queued. |
| 537 | void set_timestamp_callback(std::function<void(TimestampedMessage *)> fn) { |
| 538 | timestamp_callback_ = fn; |
| 539 | } |
| 540 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 541 | private: |
| 542 | // The state for a remote node. This holds the data that needs to be matched |
| 543 | // with the remote node's timestamps. |
| 544 | struct NodeData { |
| 545 | // True if we should save data here. This should be true if any of the |
| 546 | // bools in delivered below are true. |
| 547 | bool any_delivered = false; |
| 548 | |
| 549 | // Peer pointer. This node is only to be considered if a peer is set. |
| 550 | TimestampMapper *peer = nullptr; |
| 551 | |
| 552 | struct ChannelData { |
| 553 | // Deque per channel. This contains the data from the outside |
| 554 | // TimestampMapper node which is relevant for the node this NodeData |
| 555 | // points to. |
| 556 | std::deque<Message> messages; |
| 557 | // Bool tracking per channel if a message is delivered to the node this |
| 558 | // NodeData represents. |
| 559 | bool delivered = false; |
| 560 | }; |
| 561 | |
| 562 | // Vector with per channel data. |
| 563 | std::vector<ChannelData> channels; |
| 564 | }; |
| 565 | |
| 566 | // Returns (and forgets about) the data for the provided timestamp message |
| 567 | // showing when it was delivered to this node. |
| 568 | Message MatchingMessageFor(const Message &message); |
| 569 | |
| 570 | // Queues up a single message into our message queue, and any nodes that this |
| 571 | // message is delivered to. Returns true if one was available, false |
| 572 | // otherwise. |
| 573 | bool Queue(); |
| 574 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 575 | // Queues up a single matched message into our matched message queue. Returns |
| 576 | // true if one was queued, and false otherwise. |
| 577 | bool QueueMatched(); |
| 578 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 579 | // Queues up data until we have at least one message >= to time t. |
| 580 | // Useful for triggering a remote node to read enough data to have the |
| 581 | // timestamp you care about available. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 582 | void QueueUnmatchedUntil(monotonic_clock::time_point t); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 583 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 584 | // Queues m into matched_messages_. |
| 585 | void QueueMessage(Message *m); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 586 | |
| 587 | // The node merger to source messages from. |
| 588 | NodeMerger node_merger_; |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 589 | |
| 590 | std::shared_ptr<const Configuration> configuration_; |
| 591 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 592 | // The buffer of messages for this node. These are not matched with any |
| 593 | // remote data. |
| 594 | std::deque<Message> messages_; |
| 595 | // The node index for the source node for each channel. |
| 596 | std::vector<size_t> source_node_; |
| 597 | |
| 598 | // Vector per node. Not all nodes will have anything. |
| 599 | std::vector<NodeData> nodes_data_; |
| 600 | |
| 601 | // Latest message to return. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 602 | std::deque<TimestampedMessage> matched_messages_; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 603 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 604 | // Tracks the state of the first message in matched_messages_. Do we need to |
| 605 | // update it, is it valid, or should we return nullptr? |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 606 | enum class FirstMessage { |
| 607 | kNeedsUpdate, |
| 608 | kInMessage, |
| 609 | kNullptr, |
| 610 | }; |
| 611 | FirstMessage first_message_ = FirstMessage::kNeedsUpdate; |
| 612 | |
| 613 | // Timestamp of the last message returned. Used to make sure nothing goes |
| 614 | // backwards. |
| 615 | monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time; |
| 616 | // Time this node is queued up until. Used for caching. |
| 617 | monotonic_clock::time_point queued_until_ = monotonic_clock::min_time; |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 618 | |
| 619 | std::function<void(TimestampedMessage *)> timestamp_callback_; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 620 | }; |
| 621 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 622 | // Returns the node name with a trailing space, or an empty string if we are on |
| 623 | // a single node. |
| 624 | std::string MaybeNodeName(const Node *); |
| 625 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 626 | } // namespace aos::logger |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 627 | |
| 628 | #endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_ |