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