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" |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 21 | #include "aos/events/logging/boot_timestamp.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 22 | #include "aos/events/logging/buffer_encoder.h" |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 23 | #include "aos/events/logging/logfile_sorting.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 24 | #include "aos/events/logging/logger_generated.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 25 | #include "aos/flatbuffers.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 26 | #include "flatbuffers/flatbuffers.h" |
| 27 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 28 | namespace aos::logger { |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 29 | |
| 30 | enum class LogType : uint8_t { |
| 31 | // The message originated on this node and should be logged here. |
| 32 | kLogMessage, |
| 33 | // The message originated on another node, but only the delivery times are |
| 34 | // logged here. |
| 35 | kLogDeliveryTimeOnly, |
| 36 | // The message originated on another node. Log it and the delivery times |
| 37 | // together. The message_gateway is responsible for logging any messages |
| 38 | // which didn't get delivered. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 39 | kLogMessageAndDeliveryTime, |
| 40 | // The message originated on the other node and should be logged on this node. |
| 41 | kLogRemoteMessage |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 44 | // This class manages efficiently writing a sequence of detached buffers to a |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 45 | // file. It encodes them, queues them up, and batches the write operation. |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 46 | class DetachedBufferWriter { |
| 47 | public: |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 48 | // Marker struct for one of our constructor overloads. |
| 49 | struct already_out_of_space_t {}; |
| 50 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 51 | DetachedBufferWriter(std::string_view filename, |
| 52 | std::unique_ptr<DetachedBufferEncoder> encoder); |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 53 | // Creates a dummy instance which won't even open a file. It will act as if |
| 54 | // opening the file ran out of space immediately. |
| 55 | DetachedBufferWriter(already_out_of_space_t) : ran_out_of_space_(true) {} |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 56 | DetachedBufferWriter(DetachedBufferWriter &&other); |
| 57 | DetachedBufferWriter(const DetachedBufferWriter &) = delete; |
| 58 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 59 | ~DetachedBufferWriter(); |
| 60 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 61 | DetachedBufferWriter &operator=(DetachedBufferWriter &&other); |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 62 | DetachedBufferWriter &operator=(const DetachedBufferWriter &) = delete; |
| 63 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 64 | std::string_view filename() const { return filename_; } |
| 65 | |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 66 | // This will be true until Close() is called, unless the file couldn't be |
| 67 | // created due to running out of space. |
| 68 | bool is_open() const { return fd_ != -1; } |
| 69 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 70 | // Queues up a finished FlatBufferBuilder to be encoded and written. |
| 71 | // |
| 72 | // Triggers a flush if there's enough data queued up. |
| 73 | // |
| 74 | // Steals the detached buffer from it. |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 75 | void QueueSizedFlatbuffer(flatbuffers::FlatBufferBuilder *fbb, |
| 76 | aos::monotonic_clock::time_point now) { |
| 77 | QueueSizedFlatbuffer(fbb->Release(), now); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 78 | } |
| 79 | // May steal the backing storage of buffer, or may leave it alone. |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 80 | void QueueSizedFlatbuffer(flatbuffers::DetachedBuffer &&buffer, |
| 81 | aos::monotonic_clock::time_point now) { |
| 82 | QueueSizedFlatbuffer(std::move(buffer)); |
| 83 | FlushAtThreshold(now); |
| 84 | } |
| 85 | // Unconditionally queues the buffer. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 86 | void QueueSizedFlatbuffer(flatbuffers::DetachedBuffer &&buffer) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 87 | if (ran_out_of_space_) { |
| 88 | return; |
| 89 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 90 | encoder_->Encode(std::move(buffer)); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 91 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 92 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 93 | // Queues up data in span. May copy or may write it to disk immediately. |
| 94 | void QueueSpan(absl::Span<const uint8_t> span); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 95 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 96 | // Indicates we got ENOSPC when trying to write. After this returns true, no |
| 97 | // further data is written. |
| 98 | bool ran_out_of_space() const { return ran_out_of_space_; } |
| 99 | |
| 100 | // To avoid silently failing to write logfiles, you must call this before |
| 101 | // destruction if ran_out_of_space() is true and the situation has been |
| 102 | // handled. |
| 103 | void acknowledge_out_of_space() { |
| 104 | CHECK(ran_out_of_space_); |
| 105 | acknowledge_ran_out_of_space_ = true; |
| 106 | } |
| 107 | |
| 108 | // Fully flushes and closes the underlying file now. No additional data may be |
| 109 | // enqueued after calling this. |
| 110 | // |
| 111 | // This will be performed in the destructor automatically. |
| 112 | // |
| 113 | // Note that this may set ran_out_of_space(). |
| 114 | void Close(); |
| 115 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 116 | // Returns the total number of bytes written and currently queued. |
Austin Schuh | a426f1f | 2021-03-31 22:27:41 -0700 | [diff] [blame] | 117 | size_t total_bytes() const { |
| 118 | if (!encoder_) { |
| 119 | return 0; |
| 120 | } |
| 121 | return encoder_->total_bytes(); |
| 122 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 123 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 124 | // The maximum time for a single write call, or 0 if none have been performed. |
| 125 | std::chrono::nanoseconds max_write_time() const { return max_write_time_; } |
| 126 | // The number of bytes in the longest write call, or -1 if none have been |
| 127 | // performed. |
| 128 | int max_write_time_bytes() const { return max_write_time_bytes_; } |
| 129 | // The number of buffers in the longest write call, or -1 if none have been |
| 130 | // performed. |
| 131 | int max_write_time_messages() const { return max_write_time_messages_; } |
| 132 | // The total time spent in write calls. |
| 133 | std::chrono::nanoseconds total_write_time() const { |
| 134 | return total_write_time_; |
| 135 | } |
| 136 | // The total number of writes which have been performed. |
| 137 | int total_write_count() const { return total_write_count_; } |
| 138 | // The total number of messages which have been written. |
| 139 | int total_write_messages() const { return total_write_messages_; } |
| 140 | // The total number of bytes which have been written. |
| 141 | int total_write_bytes() const { return total_write_bytes_; } |
| 142 | void ResetStatistics() { |
| 143 | max_write_time_ = std::chrono::nanoseconds::zero(); |
| 144 | max_write_time_bytes_ = -1; |
| 145 | max_write_time_messages_ = -1; |
| 146 | total_write_time_ = std::chrono::nanoseconds::zero(); |
| 147 | total_write_count_ = 0; |
| 148 | total_write_messages_ = 0; |
| 149 | total_write_bytes_ = 0; |
| 150 | } |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 151 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 152 | private: |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 153 | // Performs a single writev call with as much of the data we have queued up as |
| 154 | // possible. |
| 155 | // |
| 156 | // This will normally take all of the data we have queued up, unless an |
| 157 | // encoder has spit out a big enough chunk all at once that we can't manage |
| 158 | // all of it. |
| 159 | void Flush(); |
| 160 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 161 | // write_return is what write(2) or writev(2) returned. write_size is the |
| 162 | // number of bytes we expected it to write. |
| 163 | void HandleWriteReturn(ssize_t write_return, size_t write_size); |
| 164 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 165 | void UpdateStatsForWrite(aos::monotonic_clock::duration duration, |
| 166 | ssize_t written, int iovec_size); |
| 167 | |
| 168 | // Flushes data if we've reached the threshold to do that as part of normal |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 169 | // operation either due to the outstanding queued data, or because we have |
| 170 | // passed our flush period. now is the current time to save some CPU grabbing |
| 171 | // the current time. It just needs to be close. |
| 172 | void FlushAtThreshold(aos::monotonic_clock::time_point now); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 173 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 174 | std::string filename_; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 175 | std::unique_ptr<DetachedBufferEncoder> encoder_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 176 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 177 | int fd_ = -1; |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 178 | bool ran_out_of_space_ = false; |
| 179 | bool acknowledge_ran_out_of_space_ = false; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 180 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 181 | // List of iovecs to use with writev. This is a member variable to avoid |
| 182 | // churn. |
| 183 | std::vector<struct iovec> iovec_; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 184 | |
| 185 | std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero(); |
| 186 | int max_write_time_bytes_ = -1; |
| 187 | int max_write_time_messages_ = -1; |
| 188 | std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero(); |
| 189 | int total_write_count_ = 0; |
| 190 | int total_write_messages_ = 0; |
| 191 | int total_write_bytes_ = 0; |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 192 | |
| 193 | aos::monotonic_clock::time_point last_flush_time_ = |
| 194 | aos::monotonic_clock::min_time; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | // Packes a message pointed to by the context into a MessageHeader. |
| 198 | flatbuffers::Offset<MessageHeader> PackMessage( |
| 199 | flatbuffers::FlatBufferBuilder *fbb, const Context &context, |
| 200 | int channel_index, LogType log_type); |
| 201 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 202 | // Class to read chunks out of a log file. |
| 203 | class SpanReader { |
| 204 | public: |
Austin Schuh | cd36842 | 2021-11-22 21:23:29 -0800 | [diff] [blame] | 205 | SpanReader(std::string_view filename, bool quiet = false); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 206 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 207 | std::string_view filename() const { return filename_; } |
| 208 | |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 209 | size_t TotalRead() const { return total_read_; } |
| 210 | size_t TotalConsumed() const { return total_consumed_; } |
| 211 | bool IsIncomplete() const { return is_finished_ |
| 212 | && total_consumed_ < total_read_; } |
| 213 | |
Austin Schuh | cf5f644 | 2021-07-06 10:43:28 -0700 | [diff] [blame] | 214 | // Returns a span with the data for the next message from the log file, |
| 215 | // including the size. The result is only guarenteed to be valid until |
| 216 | // ReadMessage() or PeekMessage() is called again. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 217 | absl::Span<const uint8_t> ReadMessage(); |
| 218 | |
Austin Schuh | cf5f644 | 2021-07-06 10:43:28 -0700 | [diff] [blame] | 219 | // Returns a span with the data for the next message without consuming it. |
| 220 | // Multiple calls to PeekMessage return the same data. ReadMessage or |
| 221 | // ConsumeMessage must be called to get the next message. |
| 222 | absl::Span<const uint8_t> PeekMessage(); |
| 223 | // Consumes the message so the next call to ReadMessage or PeekMessage returns |
| 224 | // new data. This does not invalidate the data. |
| 225 | void ConsumeMessage(); |
| 226 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 227 | private: |
| 228 | // TODO(austin): Optimization: |
| 229 | // Allocate the 256k blocks like we do today. But, refcount them with |
| 230 | // shared_ptr pointed to by the messageheader that is returned. This avoids |
| 231 | // the copy. Need to do more benchmarking. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 232 | // And (Brian): Consider just mmapping the file and handing out refcounted |
| 233 | // pointers into that too. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 234 | |
| 235 | // Reads a chunk of data into data_. Returns false if no data was read. |
| 236 | bool ReadBlock(); |
| 237 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 238 | std::string filename_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 239 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 240 | // File reader and data decoder. |
| 241 | std::unique_ptr<DataDecoder> decoder_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 242 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 243 | // Vector to read into. |
| 244 | ResizeableBuffer data_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 245 | |
| 246 | // Amount of data consumed already in data_. |
| 247 | size_t consumed_data_ = 0; |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 248 | |
| 249 | // Accumulates the total volume of bytes read from filename_ |
| 250 | size_t total_read_ = 0; |
| 251 | |
| 252 | // Accumulates the total volume of read bytes that were 'consumed' into |
| 253 | // messages. May be less than total_read_, if the last message (span) is |
| 254 | // either truncated or somehow corrupt. |
| 255 | size_t total_consumed_ = 0; |
| 256 | |
| 257 | // Reached the end, no more readable messages. |
| 258 | bool is_finished_ = false; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 259 | }; |
| 260 | |
Brian Silverman | fee1697 | 2021-09-14 12:06:38 -0700 | [diff] [blame] | 261 | // Reads the last header from a log file. This handles any duplicate headers |
| 262 | // that were written. |
| 263 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader( |
| 264 | SpanReader *span_reader); |
| 265 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader( |
| 266 | std::string_view filename); |
| 267 | // Reads the Nth message from a log file, excluding the header. Note: this |
| 268 | // doesn't handle duplicate headers. |
| 269 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage( |
| 270 | std::string_view filename, size_t n); |
| 271 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 272 | class UnpackedMessageHeader; |
| 273 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 274 | // Class which handles reading the header and messages from the log file. This |
| 275 | // handles any per-file state left before merging below. |
| 276 | class MessageReader { |
| 277 | public: |
| 278 | MessageReader(std::string_view filename); |
| 279 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 280 | std::string_view filename() const { return span_reader_.filename(); } |
| 281 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 282 | // Returns the header from the log file. |
| 283 | const LogFileHeader *log_file_header() const { |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 284 | return &raw_log_file_header_.message(); |
| 285 | } |
| 286 | |
| 287 | // Returns the raw data of the header from the log file. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 288 | const SizePrefixedFlatbufferVector<LogFileHeader> &raw_log_file_header() |
| 289 | const { |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 290 | return raw_log_file_header_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // Returns the minimum maount of data needed to queue up for sorting before |
| 294 | // ware guarenteed to not see data out of order. |
| 295 | std::chrono::nanoseconds max_out_of_order_duration() const { |
| 296 | return max_out_of_order_duration_; |
| 297 | } |
| 298 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 299 | // Returns the newest timestamp read out of the log file. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 300 | monotonic_clock::time_point newest_timestamp() const { |
| 301 | return newest_timestamp_; |
| 302 | } |
| 303 | |
| 304 | // Returns the next message if there is one. |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 305 | std::shared_ptr<UnpackedMessageHeader> ReadMessage(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 306 | |
| 307 | // The time at which we need to read another chunk from the logfile. |
| 308 | monotonic_clock::time_point queue_data_time() const { |
| 309 | return newest_timestamp() - max_out_of_order_duration(); |
| 310 | } |
| 311 | |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 312 | // Flag value setters for testing |
| 313 | void set_crash_on_corrupt_message_flag(bool b) { |
| 314 | crash_on_corrupt_message_flag_ = b; |
| 315 | } |
| 316 | void set_ignore_corrupt_messages_flag(bool b) { |
| 317 | ignore_corrupt_messages_flag_ = b; |
| 318 | } |
| 319 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 320 | private: |
| 321 | // Log chunk reader. |
| 322 | SpanReader span_reader_; |
| 323 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 324 | // Vector holding the raw data for the log file header. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 325 | SizePrefixedFlatbufferVector<LogFileHeader> raw_log_file_header_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 326 | |
| 327 | // Minimum amount of data to queue up for sorting before we are guarenteed |
| 328 | // to not see data out of order. |
| 329 | std::chrono::nanoseconds max_out_of_order_duration_; |
| 330 | |
| 331 | // Timestamp of the newest message in a channel queue. |
| 332 | monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time; |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 333 | |
| 334 | // Total volume of verifiable messages from the beginning of the file. |
| 335 | // TODO - are message counts also useful? |
| 336 | size_t total_verified_before_ = 0; |
| 337 | |
| 338 | // Total volume of messages with corrupted flatbuffer formatting, if any. |
| 339 | // Excludes corrupted message content. |
| 340 | // TODO - if the layout included something as simple as a CRC (relatively |
| 341 | // fast and robust enough) for each span, then corrupted content could be |
| 342 | // included in this check. |
| 343 | size_t total_corrupted_ = 0; |
| 344 | |
| 345 | // Total volume of verifiable messages intermixed with corrupted messages, |
| 346 | // if any. Will be == 0 if total_corrupted_ == 0. |
| 347 | size_t total_verified_during_ = 0; |
| 348 | |
| 349 | // Total volume of verifiable messages found after the last corrupted one, |
| 350 | // if any. Will be == 0 if total_corrupted_ == 0. |
| 351 | size_t total_verified_after_ = 0; |
| 352 | |
| 353 | bool is_corrupted() const { return total_corrupted_ > 0; } |
| 354 | |
| 355 | bool crash_on_corrupt_message_flag_ = true; |
| 356 | bool ignore_corrupt_messages_flag_ = false; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 357 | }; |
| 358 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 359 | // A class to seamlessly read messages from a list of part files. |
| 360 | class PartsMessageReader { |
| 361 | public: |
| 362 | PartsMessageReader(LogParts log_parts); |
| 363 | |
| 364 | std::string_view filename() const { return message_reader_.filename(); } |
| 365 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 366 | // Returns the LogParts that holds the filenames we are reading. |
| 367 | const LogParts &parts() const { return parts_; } |
| 368 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 369 | const LogFileHeader *log_file_header() const { |
| 370 | return message_reader_.log_file_header(); |
| 371 | } |
| 372 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 373 | // Returns the minimum amount of data needed to queue up for sorting before |
| 374 | // we are guarenteed to not see data out of order. |
| 375 | std::chrono::nanoseconds max_out_of_order_duration() const { |
| 376 | return message_reader_.max_out_of_order_duration(); |
| 377 | } |
| 378 | |
| 379 | // Returns the newest timestamp read out of the log file. |
| 380 | monotonic_clock::time_point newest_timestamp() const { |
| 381 | return newest_timestamp_; |
| 382 | } |
| 383 | |
| 384 | // Returns the next message if there is one, or nullopt if we have reached the |
| 385 | // end of all the files. |
| 386 | // Note: reading the next message may change the max_out_of_order_duration(). |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 387 | std::shared_ptr<UnpackedMessageHeader> ReadMessage(); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 388 | |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 389 | // Returns the boot count for the requested node, or std::nullopt if we don't |
| 390 | // know. |
| 391 | std::optional<size_t> boot_count(size_t node_index) const { |
| 392 | CHECK_GE(node_index, 0u); |
| 393 | CHECK_LT(node_index, boot_counts_.size()); |
| 394 | return boot_counts_[node_index]; |
| 395 | } |
| 396 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 397 | private: |
| 398 | // Opens the next log and updates message_reader_. Sets done_ if there is |
| 399 | // nothing more to do. |
| 400 | void NextLog(); |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 401 | void ComputeBootCounts(); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 402 | |
| 403 | const LogParts parts_; |
| 404 | size_t next_part_index_ = 1u; |
| 405 | bool done_ = false; |
| 406 | MessageReader message_reader_; |
Brian Silverman | fee1697 | 2021-09-14 12:06:38 -0700 | [diff] [blame] | 407 | // We instantiate the next one early, to allow implementations to prefetch. |
| 408 | // TODO(Brian): To get optimal performance when downloading, this needs more |
| 409 | // communication with the implementation to prioritize the next part and add |
| 410 | // more parallelism when it helps. Maybe some kind of a queue of parts in |
| 411 | // order, and the implementation gets to pull however many make sense off the |
| 412 | // front? |
| 413 | std::optional<MessageReader> next_message_reader_; |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 414 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 415 | // True after we have seen a message after the start of the log. The |
| 416 | // guarentees on logging essentially are that all data from before the |
| 417 | // starting time of the log may be arbitrarily out of order, but once we get |
| 418 | // max_out_of_order_duration past the start, everything will remain within |
| 419 | // max_out_of_order_duration. We shouldn't see anything before the start |
| 420 | // after we've seen a message that is at least max_out_of_order_duration after |
| 421 | // the start. |
| 422 | bool after_start_ = false; |
| 423 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 424 | monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time; |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 425 | |
| 426 | // Per node boot counts. |
| 427 | std::vector<std::optional<size_t>> boot_counts_; |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 428 | }; |
| 429 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 430 | // Stores MessageHeader as a flat header and inline, aligned block of data. |
| 431 | class UnpackedMessageHeader { |
| 432 | public: |
| 433 | UnpackedMessageHeader(const UnpackedMessageHeader &) = delete; |
| 434 | UnpackedMessageHeader &operator=(const UnpackedMessageHeader &) = delete; |
| 435 | |
| 436 | // The channel. |
| 437 | uint32_t channel_index = 0xffffffff; |
| 438 | |
| 439 | monotonic_clock::time_point monotonic_sent_time; |
| 440 | realtime_clock::time_point realtime_sent_time; |
| 441 | |
| 442 | // The local queue index. |
| 443 | uint32_t queue_index = 0xffffffff; |
| 444 | |
Austin Schuh | 826e6ce | 2021-11-18 20:33:10 -0800 | [diff] [blame] | 445 | std::optional<aos::monotonic_clock::time_point> monotonic_remote_time; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 446 | |
| 447 | std::optional<realtime_clock::time_point> realtime_remote_time; |
| 448 | std::optional<uint32_t> remote_queue_index; |
| 449 | |
| 450 | // This field is defaulted in the flatbuffer, so we need to store both the |
| 451 | // possibly defaulted value and whether it is defaulted. |
| 452 | monotonic_clock::time_point monotonic_timestamp_time; |
| 453 | bool has_monotonic_timestamp_time; |
| 454 | |
| 455 | static std::shared_ptr<UnpackedMessageHeader> MakeMessage( |
| 456 | const MessageHeader &message); |
| 457 | |
| 458 | // Note: we are storing a span here because we need something to put in the |
| 459 | // SharedSpan pointer that RawSender takes. We are using the aliasing |
| 460 | // constructor of shared_ptr to avoid the allocation, and it needs a nice |
| 461 | // pointer to track. |
| 462 | absl::Span<const uint8_t> span; |
| 463 | |
| 464 | char actual_data[]; |
| 465 | |
| 466 | private: |
| 467 | ~UnpackedMessageHeader() {} |
| 468 | |
| 469 | static void DestroyAndFree(UnpackedMessageHeader *p) { |
| 470 | p->~UnpackedMessageHeader(); |
| 471 | free(p); |
| 472 | } |
| 473 | }; |
| 474 | |
| 475 | std::ostream &operator<<(std::ostream &os, |
| 476 | const UnpackedMessageHeader &message); |
| 477 | |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 478 | // Struct to hold a message as it gets sorted on a single node. |
| 479 | struct Message { |
| 480 | // The channel. |
| 481 | uint32_t channel_index = 0xffffffff; |
| 482 | // The local queue index. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 483 | // TODO(austin): Technically the boot inside queue_index is redundant with |
| 484 | // timestamp. In practice, it is less error-prone to duplicate it. Maybe a |
| 485 | // function to return the combined struct? |
| 486 | BootQueueIndex queue_index; |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 487 | // The local timestamp. |
| 488 | BootTimestamp timestamp; |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 489 | |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 490 | // Remote boot when this is a timestamp. |
| 491 | size_t monotonic_remote_boot = 0xffffff; |
| 492 | |
| 493 | size_t monotonic_timestamp_boot = 0xffffff; |
| 494 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 495 | std::shared_ptr<UnpackedMessageHeader> data; |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 496 | |
| 497 | bool operator<(const Message &m2) const; |
| 498 | bool operator>=(const Message &m2) const; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 499 | bool operator==(const Message &m2) const; |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 500 | }; |
| 501 | |
| 502 | std::ostream &operator<<(std::ostream &os, const Message &m); |
| 503 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 504 | // Structure to hold a full message and all the timestamps, which may or may not |
| 505 | // have been sent from a remote node. The remote_queue_index will be invalid if |
| 506 | // this message is from the point of view of the node which sent it. |
| 507 | struct TimestampedMessage { |
| 508 | uint32_t channel_index = 0xffffffff; |
| 509 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 510 | BootQueueIndex queue_index; |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 511 | BootTimestamp monotonic_event_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 512 | realtime_clock::time_point realtime_event_time = realtime_clock::min_time; |
| 513 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 514 | BootQueueIndex remote_queue_index; |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 515 | BootTimestamp monotonic_remote_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 516 | realtime_clock::time_point realtime_remote_time = realtime_clock::min_time; |
| 517 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 518 | BootTimestamp monotonic_timestamp_time; |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 519 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 520 | std::shared_ptr<UnpackedMessageHeader> data; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 521 | }; |
| 522 | |
| 523 | std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m); |
| 524 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 525 | // Class to sort the resulting messages from a PartsMessageReader. |
| 526 | class LogPartsSorter { |
| 527 | public: |
| 528 | LogPartsSorter(LogParts log_parts); |
| 529 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 530 | // Returns the parts that this is sorting messages from. |
| 531 | const LogParts &parts() const { return parts_message_reader_.parts(); } |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 532 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 533 | monotonic_clock::time_point monotonic_start_time() const { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 534 | return parts().monotonic_start_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 535 | } |
| 536 | realtime_clock::time_point realtime_start_time() const { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 537 | return parts().realtime_start_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 540 | // The time this data is sorted until. |
| 541 | monotonic_clock::time_point sorted_until() const { return sorted_until_; } |
| 542 | |
| 543 | // Returns the next sorted message from the log file. It is safe to call |
| 544 | // std::move() on the result to move the data flatbuffer from it. |
| 545 | Message *Front(); |
| 546 | // Pops the front message. This should only be called after a call to |
| 547 | // Front(). |
| 548 | void PopFront(); |
| 549 | |
| 550 | // Returns a debug string representing the contents of this sorter. |
| 551 | std::string DebugString() const; |
| 552 | |
| 553 | private: |
| 554 | // Log parts reader we are wrapping. |
| 555 | PartsMessageReader parts_message_reader_; |
| 556 | // Cache of the time we are sorted until. |
| 557 | aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time; |
| 558 | |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 559 | // Timestamp of the last message returned. Used to make sure nothing goes |
| 560 | // backwards. |
| 561 | monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time; |
| 562 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 563 | // Set used for efficient sorting of messages. We can benchmark and evaluate |
| 564 | // other data structures if this proves to be the bottleneck. |
| 565 | absl::btree_set<Message> messages_; |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 566 | |
| 567 | // Mapping from channel to source node. |
| 568 | // TODO(austin): Should we put this in Boots so it can be cached for everyone? |
| 569 | std::vector<size_t> source_node_index_; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 570 | }; |
| 571 | |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 572 | // Class to run merge sort on the messages from multiple LogPartsSorter |
| 573 | // instances. |
| 574 | class NodeMerger { |
| 575 | public: |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 576 | NodeMerger(std::vector<LogParts> parts); |
| 577 | |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 578 | // Copying and moving will mess up the internal raw pointers. Just don't do |
| 579 | // it. |
| 580 | NodeMerger(NodeMerger const &) = delete; |
| 581 | NodeMerger(NodeMerger &&) = delete; |
| 582 | void operator=(NodeMerger const &) = delete; |
| 583 | void operator=(NodeMerger &&) = delete; |
| 584 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 585 | // Node index in the configuration of this node. |
| 586 | int node() const { return node_; } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 587 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 588 | // List of parts being sorted together. |
| 589 | std::vector<const LogParts *> Parts() const; |
| 590 | |
| 591 | const Configuration *configuration() const { |
| 592 | return parts_sorters_[0].parts().config.get(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | monotonic_clock::time_point monotonic_start_time() const { |
| 596 | return monotonic_start_time_; |
| 597 | } |
| 598 | realtime_clock::time_point realtime_start_time() const { |
| 599 | return realtime_start_time_; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 600 | } |
Austin Schuh | 5dd2284 | 2021-11-17 16:09:39 -0800 | [diff] [blame] | 601 | monotonic_clock::time_point monotonic_oldest_time() const { |
| 602 | return monotonic_oldest_time_; |
| 603 | } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 604 | |
| 605 | // The time this data is sorted until. |
| 606 | monotonic_clock::time_point sorted_until() const { return sorted_until_; } |
| 607 | |
| 608 | // Returns the next sorted message from the set of log files. It is safe to |
| 609 | // call std::move() on the result to move the data flatbuffer from it. |
| 610 | Message *Front(); |
| 611 | // Pops the front message. This should only be called after a call to |
| 612 | // Front(). |
| 613 | void PopFront(); |
| 614 | |
| 615 | private: |
| 616 | // Unsorted list of all parts sorters. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 617 | std::vector<LogPartsSorter> parts_sorters_; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 618 | // Pointer to the parts sorter holding the current Front message if one |
| 619 | // exists, or nullptr if a new one needs to be found. |
| 620 | LogPartsSorter *current_ = nullptr; |
| 621 | // Cached sorted_until value. |
| 622 | aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 623 | |
| 624 | // Cached node. |
| 625 | int node_; |
| 626 | |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 627 | // Timestamp of the last message returned. Used to make sure nothing goes |
| 628 | // backwards. |
| 629 | monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time; |
| 630 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 631 | realtime_clock::time_point realtime_start_time_ = realtime_clock::max_time; |
| 632 | monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::max_time; |
Austin Schuh | 5dd2284 | 2021-11-17 16:09:39 -0800 | [diff] [blame] | 633 | monotonic_clock::time_point monotonic_oldest_time_ = monotonic_clock::max_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 634 | }; |
| 635 | |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 636 | // Class to concatenate multiple boots worth of logs into a single per-node |
| 637 | // stream. |
| 638 | class BootMerger { |
| 639 | public: |
| 640 | BootMerger(std::vector<LogParts> file); |
| 641 | |
| 642 | // Copying and moving will mess up the internal raw pointers. Just don't do |
| 643 | // it. |
| 644 | BootMerger(BootMerger const &) = delete; |
| 645 | BootMerger(BootMerger &&) = delete; |
| 646 | void operator=(BootMerger const &) = delete; |
| 647 | void operator=(BootMerger &&) = delete; |
| 648 | |
| 649 | // Node index in the configuration of this node. |
| 650 | int node() const { return node_mergers_[0]->node(); } |
| 651 | |
| 652 | // List of parts being sorted together. |
| 653 | std::vector<const LogParts *> Parts() const; |
| 654 | |
| 655 | const Configuration *configuration() const { |
| 656 | return node_mergers_[0]->configuration(); |
| 657 | } |
| 658 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 659 | monotonic_clock::time_point monotonic_start_time(size_t boot) const { |
| 660 | CHECK_LT(boot, node_mergers_.size()); |
| 661 | return node_mergers_[boot]->monotonic_start_time(); |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 662 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 663 | realtime_clock::time_point realtime_start_time(size_t boot) const { |
| 664 | CHECK_LT(boot, node_mergers_.size()); |
| 665 | return node_mergers_[boot]->realtime_start_time(); |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 666 | } |
Austin Schuh | 5dd2284 | 2021-11-17 16:09:39 -0800 | [diff] [blame] | 667 | monotonic_clock::time_point monotonic_oldest_time(size_t boot) const { |
| 668 | CHECK_LT(boot, node_mergers_.size()); |
| 669 | return node_mergers_[boot]->monotonic_oldest_time(); |
| 670 | } |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 671 | |
| 672 | bool started() const { |
| 673 | return node_mergers_[index_]->sorted_until() != monotonic_clock::min_time || |
| 674 | index_ != 0; |
| 675 | } |
| 676 | |
| 677 | // Returns the next sorted message from the set of log files. It is safe to |
| 678 | // call std::move() on the result to move the data flatbuffer from it. |
| 679 | Message *Front(); |
| 680 | // Pops the front message. This should only be called after a call to |
| 681 | // Front(). |
| 682 | void PopFront(); |
| 683 | |
| 684 | private: |
| 685 | int index_ = 0; |
| 686 | |
| 687 | // TODO(austin): Sanjay points out this is pretty inefficient. Don't keep so |
| 688 | // many things open. |
| 689 | std::vector<std::unique_ptr<NodeMerger>> node_mergers_; |
| 690 | }; |
| 691 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 692 | // Class to match timestamps with the corresponding data from other nodes. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 693 | // |
| 694 | // This class also buffers data for the node it represents, and supports |
| 695 | // 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] | 696 | class TimestampMapper { |
| 697 | public: |
| 698 | TimestampMapper(std::vector<LogParts> file); |
| 699 | |
| 700 | // Copying and moving will mess up the internal raw pointers. Just don't do |
| 701 | // it. |
| 702 | TimestampMapper(TimestampMapper const &) = delete; |
| 703 | TimestampMapper(TimestampMapper &&) = delete; |
| 704 | void operator=(TimestampMapper const &) = delete; |
| 705 | void operator=(TimestampMapper &&) = delete; |
| 706 | |
| 707 | // TODO(austin): It would be super helpful to provide a way to queue up to |
| 708 | // time X without matching timestamps, and to then be able to pull the |
| 709 | // timestamps out of this queue. This lets us bootstrap time estimation |
| 710 | // without exploding memory usage worst case. |
| 711 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 712 | const Configuration *configuration() const { return configuration_.get(); } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 713 | |
| 714 | // Returns which node this is sorting for. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 715 | size_t node() const { return boot_merger_.node(); } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 716 | |
| 717 | // The start time of this log. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 718 | monotonic_clock::time_point monotonic_start_time(size_t boot) const { |
| 719 | return boot_merger_.monotonic_start_time(boot); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 720 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 721 | realtime_clock::time_point realtime_start_time(size_t boot) const { |
| 722 | return boot_merger_.realtime_start_time(boot); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 723 | } |
Austin Schuh | 5dd2284 | 2021-11-17 16:09:39 -0800 | [diff] [blame] | 724 | // Returns the oldest timestamp on a message on this boot. |
| 725 | monotonic_clock::time_point monotonic_oldest_time(size_t boot) const { |
| 726 | return boot_merger_.monotonic_oldest_time(boot); |
| 727 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 728 | |
| 729 | // Uses timestamp_mapper as the peer for its node. Only one mapper may be set |
| 730 | // for each node. Peers are used to look up the data for timestamps on this |
| 731 | // node. |
| 732 | void AddPeer(TimestampMapper *timestamp_mapper); |
| 733 | |
Austin Schuh | 24bf497 | 2021-06-29 22:09:08 -0700 | [diff] [blame] | 734 | // Returns true if anything has been queued up. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 735 | bool started() const { return boot_merger_.started(); } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 736 | |
| 737 | // Returns the next message for this node. |
| 738 | TimestampedMessage *Front(); |
| 739 | // Pops the next message. Front must be called first. |
| 740 | void PopFront(); |
| 741 | |
| 742 | // Returns debug information about this node. |
| 743 | std::string DebugString() const; |
| 744 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 745 | // Queues data the provided time. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 746 | void QueueUntil(BootTimestamp queue_time); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 747 | // Queues until we have time_estimation_buffer of data in the queue. |
| 748 | void QueueFor(std::chrono::nanoseconds time_estimation_buffer); |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 749 | |
Austin Schuh | 0660122 | 2021-01-26 17:02:50 -0800 | [diff] [blame] | 750 | // Queues until the condition is met. |
| 751 | template <typename T> |
| 752 | void QueueUntilCondition(T fn) { |
| 753 | while (true) { |
| 754 | if (fn()) { |
| 755 | break; |
| 756 | } |
| 757 | if (!QueueMatched()) { |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 763 | // Sets a callback to be called whenever a full message is queued. |
| 764 | void set_timestamp_callback(std::function<void(TimestampedMessage *)> fn) { |
| 765 | timestamp_callback_ = fn; |
| 766 | } |
| 767 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 768 | private: |
| 769 | // The state for a remote node. This holds the data that needs to be matched |
| 770 | // with the remote node's timestamps. |
| 771 | struct NodeData { |
| 772 | // True if we should save data here. This should be true if any of the |
| 773 | // bools in delivered below are true. |
| 774 | bool any_delivered = false; |
| 775 | |
Austin Schuh | 36c0093 | 2021-07-19 18:13:21 -0700 | [diff] [blame] | 776 | // True if we have a peer and therefore should be saving data for it. |
| 777 | bool save_for_peer = false; |
| 778 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 779 | // Peer pointer. This node is only to be considered if a peer is set. |
| 780 | TimestampMapper *peer = nullptr; |
| 781 | |
| 782 | struct ChannelData { |
| 783 | // Deque per channel. This contains the data from the outside |
| 784 | // TimestampMapper node which is relevant for the node this NodeData |
| 785 | // points to. |
| 786 | std::deque<Message> messages; |
| 787 | // Bool tracking per channel if a message is delivered to the node this |
| 788 | // NodeData represents. |
| 789 | bool delivered = false; |
Austin Schuh | 6a7358f | 2021-11-18 22:40:40 -0800 | [diff] [blame] | 790 | // The TTL for delivery. |
| 791 | std::chrono::nanoseconds time_to_live = std::chrono::nanoseconds(0); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 792 | }; |
| 793 | |
| 794 | // Vector with per channel data. |
| 795 | std::vector<ChannelData> channels; |
| 796 | }; |
| 797 | |
| 798 | // Returns (and forgets about) the data for the provided timestamp message |
| 799 | // showing when it was delivered to this node. |
| 800 | Message MatchingMessageFor(const Message &message); |
| 801 | |
| 802 | // Queues up a single message into our message queue, and any nodes that this |
| 803 | // message is delivered to. Returns true if one was available, false |
| 804 | // otherwise. |
| 805 | bool Queue(); |
| 806 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 807 | // Queues up a single matched message into our matched message queue. Returns |
| 808 | // true if one was queued, and false otherwise. |
| 809 | bool QueueMatched(); |
| 810 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 811 | // Queues up data until we have at least one message >= to time t. |
| 812 | // Useful for triggering a remote node to read enough data to have the |
| 813 | // timestamp you care about available. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 814 | void QueueUnmatchedUntil(BootTimestamp t); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 815 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 816 | // Queues m into matched_messages_. |
| 817 | void QueueMessage(Message *m); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 818 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 819 | // Returns the name of the node this class is sorting for. |
| 820 | std::string_view node_name() const { |
| 821 | return configuration_->has_nodes() ? configuration_->nodes() |
| 822 | ->Get(boot_merger_.node()) |
| 823 | ->name() |
| 824 | ->string_view() |
| 825 | : "(single node)"; |
| 826 | } |
| 827 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 828 | // The node merger to source messages from. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 829 | BootMerger boot_merger_; |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 830 | |
| 831 | std::shared_ptr<const Configuration> configuration_; |
| 832 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 833 | // The buffer of messages for this node. These are not matched with any |
| 834 | // remote data. |
| 835 | std::deque<Message> messages_; |
| 836 | // The node index for the source node for each channel. |
| 837 | std::vector<size_t> source_node_; |
| 838 | |
| 839 | // Vector per node. Not all nodes will have anything. |
| 840 | std::vector<NodeData> nodes_data_; |
| 841 | |
| 842 | // Latest message to return. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 843 | std::deque<TimestampedMessage> matched_messages_; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 844 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 845 | // Tracks the state of the first message in matched_messages_. Do we need to |
| 846 | // update it, is it valid, or should we return nullptr? |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 847 | enum class FirstMessage { |
| 848 | kNeedsUpdate, |
| 849 | kInMessage, |
| 850 | kNullptr, |
| 851 | }; |
| 852 | FirstMessage first_message_ = FirstMessage::kNeedsUpdate; |
| 853 | |
| 854 | // Timestamp of the last message returned. Used to make sure nothing goes |
| 855 | // backwards. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 856 | BootTimestamp last_message_time_ = BootTimestamp::min_time(); |
Austin Schuh | 6a7358f | 2021-11-18 22:40:40 -0800 | [diff] [blame] | 857 | BootTimestamp last_popped_message_time_ = BootTimestamp::min_time(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 858 | // Time this node is queued up until. Used for caching. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 859 | BootTimestamp queued_until_ = BootTimestamp::min_time(); |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 860 | |
| 861 | std::function<void(TimestampedMessage *)> timestamp_callback_; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 862 | }; |
| 863 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 864 | // Returns the node name with a trailing space, or an empty string if we are on |
| 865 | // a single node. |
| 866 | std::string MaybeNodeName(const Node *); |
| 867 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 868 | } // namespace aos::logger |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 869 | |
| 870 | #endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_ |