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