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" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 19 | #include "flatbuffers/flatbuffers.h" |
| 20 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 21 | #include "aos/containers/resizeable_buffer.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 22 | #include "aos/events/event_loop.h" |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 23 | #include "aos/events/logging/boot_timestamp.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 24 | #include "aos/events/logging/buffer_encoder.h" |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 25 | #include "aos/events/logging/log_backend.h" |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 26 | #include "aos/events/logging/logfile_sorting.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 27 | #include "aos/events/logging/logger_generated.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 28 | #include "aos/flatbuffers.h" |
Austin Schuh | f2d0e68 | 2022-10-16 14:20:58 -0700 | [diff] [blame] | 29 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 30 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 31 | namespace aos::logger { |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 32 | |
| 33 | enum class LogType : uint8_t { |
| 34 | // The message originated on this node and should be logged here. |
| 35 | kLogMessage, |
| 36 | // The message originated on another node, but only the delivery times are |
| 37 | // logged here. |
| 38 | kLogDeliveryTimeOnly, |
| 39 | // The message originated on another node. Log it and the delivery times |
| 40 | // together. The message_gateway is responsible for logging any messages |
| 41 | // which didn't get delivered. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 42 | kLogMessageAndDeliveryTime, |
| 43 | // The message originated on the other node and should be logged on this node. |
| 44 | kLogRemoteMessage |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 47 | // This class manages efficiently writing a sequence of detached buffers to a |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 48 | // file. It encodes them, queues them up, and batches the write operation. |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 49 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 50 | class DetachedBufferWriter { |
| 51 | public: |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 52 | // Marker struct for one of our constructor overloads. |
| 53 | struct already_out_of_space_t {}; |
| 54 | |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 55 | DetachedBufferWriter(std::unique_ptr<LogSink> log_sink, |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 56 | std::unique_ptr<DataEncoder> encoder); |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 57 | // Creates a dummy instance which won't even open a file. It will act as if |
| 58 | // opening the file ran out of space immediately. |
| 59 | DetachedBufferWriter(already_out_of_space_t) : ran_out_of_space_(true) {} |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 60 | DetachedBufferWriter(DetachedBufferWriter &&other); |
| 61 | DetachedBufferWriter(const DetachedBufferWriter &) = delete; |
| 62 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 63 | ~DetachedBufferWriter(); |
| 64 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 65 | DetachedBufferWriter &operator=(DetachedBufferWriter &&other); |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 66 | DetachedBufferWriter &operator=(const DetachedBufferWriter &) = delete; |
| 67 | |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 68 | std::string_view name() const { return log_sink_->name(); } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 69 | |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 70 | // This will be true until Close() is called, unless the file couldn't be |
| 71 | // created due to running out of space. |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 72 | bool is_open() const { return log_sink_->is_open(); } |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 73 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 74 | // Queues up a finished FlatBufferBuilder to be encoded and written. |
| 75 | // |
| 76 | // Triggers a flush if there's enough data queued up. |
| 77 | // |
| 78 | // Steals the detached buffer from it. |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 79 | void CopyMessage(DataEncoder::Copier *coppier, |
| 80 | aos::monotonic_clock::time_point now); |
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 | |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 110 | WriteStats *WriteStatistics() const { return log_sink_->WriteStatistics(); } |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 111 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 112 | private: |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 113 | // Performs a single writev call with as much of the data we have queued up as |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 114 | // possible. now is the time we flushed at, to be recorded in |
| 115 | // last_flush_time_. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 116 | // |
| 117 | // This will normally take all of the data we have queued up, unless an |
| 118 | // encoder has spit out a big enough chunk all at once that we can't manage |
| 119 | // all of it. |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 120 | void Flush(aos::monotonic_clock::time_point now); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 121 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 122 | // 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] | 123 | // operation either due to the outstanding queued data, or because we have |
| 124 | // passed our flush period. now is the current time to save some CPU grabbing |
| 125 | // the current time. It just needs to be close. |
| 126 | void FlushAtThreshold(aos::monotonic_clock::time_point now); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 127 | |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 128 | std::unique_ptr<LogSink> log_sink_; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 129 | std::unique_ptr<DataEncoder> encoder_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 130 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 131 | bool ran_out_of_space_ = false; |
| 132 | bool acknowledge_ran_out_of_space_ = false; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 133 | |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 134 | aos::monotonic_clock::time_point last_flush_time_ = |
| 135 | aos::monotonic_clock::min_time; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
Austin Schuh | f2d0e68 | 2022-10-16 14:20:58 -0700 | [diff] [blame] | 138 | // Repacks the provided RemoteMessage into fbb. |
| 139 | flatbuffers::Offset<MessageHeader> PackRemoteMessage( |
| 140 | flatbuffers::FlatBufferBuilder *fbb, |
| 141 | const message_bridge::RemoteMessage *msg, int channel_index, |
| 142 | const aos::monotonic_clock::time_point monotonic_timestamp_time); |
| 143 | |
| 144 | constexpr flatbuffers::uoffset_t PackRemoteMessageSize() { return 96u; } |
| 145 | size_t PackRemoteMessageInline( |
| 146 | uint8_t *data, const message_bridge::RemoteMessage *msg, int channel_index, |
Austin Schuh | 71a40d4 | 2023-02-04 21:22:22 -0800 | [diff] [blame] | 147 | const aos::monotonic_clock::time_point monotonic_timestamp_time, |
| 148 | size_t start_byte, size_t end_byte); |
Austin Schuh | f2d0e68 | 2022-10-16 14:20:58 -0700 | [diff] [blame] | 149 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 150 | // Packes a message pointed to by the context into a MessageHeader. |
| 151 | flatbuffers::Offset<MessageHeader> PackMessage( |
| 152 | flatbuffers::FlatBufferBuilder *fbb, const Context &context, |
| 153 | int channel_index, LogType log_type); |
| 154 | |
Austin Schuh | fa30c35 | 2022-10-16 11:12:02 -0700 | [diff] [blame] | 155 | // Returns the size that the packed message from PackMessage or |
| 156 | // PackMessageInline will be. |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 157 | flatbuffers::uoffset_t PackMessageSize(LogType log_type, size_t data_size); |
Austin Schuh | fa30c35 | 2022-10-16 11:12:02 -0700 | [diff] [blame] | 158 | |
| 159 | // Packs the provided message pointed to by context into the provided buffer. |
| 160 | // This is equivalent to PackMessage, but doesn't require allocating a |
| 161 | // FlatBufferBuilder underneath. |
| 162 | size_t PackMessageInline(uint8_t *data, const Context &contex, |
Austin Schuh | 71a40d4 | 2023-02-04 21:22:22 -0800 | [diff] [blame] | 163 | int channel_index, LogType log_type, size_t start_byte, |
| 164 | size_t end_byte); |
Austin Schuh | fa30c35 | 2022-10-16 11:12:02 -0700 | [diff] [blame] | 165 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 166 | // Class to read chunks out of a log file. |
| 167 | class SpanReader { |
| 168 | public: |
Alexei Strots | cee7b37 | 2023-04-21 11:57:54 -0700 | [diff] [blame] | 169 | // It creates a reader and makes proper decoder based on information encoded |
| 170 | // in the filename. |
Austin Schuh | cd36842 | 2021-11-22 21:23:29 -0800 | [diff] [blame] | 171 | SpanReader(std::string_view filename, bool quiet = false); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 172 | |
Alexei Strots | cee7b37 | 2023-04-21 11:57:54 -0700 | [diff] [blame] | 173 | // Opens new reader from provided decoder. |
| 174 | SpanReader(std::string_view filename, std::unique_ptr<DataDecoder> decoder); |
| 175 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 176 | std::string_view filename() const { return filename_; } |
| 177 | |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 178 | size_t TotalRead() const { return total_read_; } |
| 179 | size_t TotalConsumed() const { return total_consumed_; } |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 180 | bool IsIncomplete() const { |
| 181 | return is_finished_ && total_consumed_ < total_read_; |
| 182 | } |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 183 | |
Austin Schuh | cf5f644 | 2021-07-06 10:43:28 -0700 | [diff] [blame] | 184 | // Returns a span with the data for the next message from the log file, |
| 185 | // including the size. The result is only guarenteed to be valid until |
| 186 | // ReadMessage() or PeekMessage() is called again. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 187 | absl::Span<const uint8_t> ReadMessage(); |
| 188 | |
Austin Schuh | cf5f644 | 2021-07-06 10:43:28 -0700 | [diff] [blame] | 189 | // Returns a span with the data for the next message without consuming it. |
| 190 | // Multiple calls to PeekMessage return the same data. ReadMessage or |
| 191 | // ConsumeMessage must be called to get the next message. |
| 192 | absl::Span<const uint8_t> PeekMessage(); |
| 193 | // Consumes the message so the next call to ReadMessage or PeekMessage returns |
| 194 | // new data. This does not invalidate the data. |
| 195 | void ConsumeMessage(); |
| 196 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 197 | private: |
| 198 | // TODO(austin): Optimization: |
| 199 | // Allocate the 256k blocks like we do today. But, refcount them with |
| 200 | // shared_ptr pointed to by the messageheader that is returned. This avoids |
| 201 | // the copy. Need to do more benchmarking. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 202 | // And (Brian): Consider just mmapping the file and handing out refcounted |
| 203 | // pointers into that too. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 204 | |
| 205 | // Reads a chunk of data into data_. Returns false if no data was read. |
| 206 | bool ReadBlock(); |
| 207 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 208 | std::string filename_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 209 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 210 | // File reader and data decoder. |
| 211 | std::unique_ptr<DataDecoder> decoder_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 212 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 213 | // Vector to read into. |
| 214 | ResizeableBuffer data_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 215 | |
| 216 | // Amount of data consumed already in data_. |
| 217 | size_t consumed_data_ = 0; |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 218 | |
| 219 | // Accumulates the total volume of bytes read from filename_ |
| 220 | size_t total_read_ = 0; |
| 221 | |
| 222 | // Accumulates the total volume of read bytes that were 'consumed' into |
| 223 | // messages. May be less than total_read_, if the last message (span) is |
| 224 | // either truncated or somehow corrupt. |
| 225 | size_t total_consumed_ = 0; |
| 226 | |
| 227 | // Reached the end, no more readable messages. |
| 228 | bool is_finished_ = false; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 229 | }; |
| 230 | |
Alexei Strots | a319471 | 2023-04-21 23:30:50 -0700 | [diff] [blame] | 231 | // Class to borrow log readers from pool based on their ids. This is used as a |
| 232 | // factory and helps with performance when construction or descrution of |
| 233 | // decoders are not free. For instance,, S3 fetchers are slow to destroy. |
| 234 | class ReadersPool { |
| 235 | public: |
| 236 | virtual ~ReadersPool() = default; |
| 237 | |
| 238 | // Borrow reader from pool based on the id. |
| 239 | virtual SpanReader *BorrowReader(std::string_view id) = 0; |
| 240 | }; |
| 241 | |
| 242 | class LogReadersPool : public ReadersPool { |
| 243 | public: |
| 244 | explicit LogReadersPool(const LogSource *log_source = nullptr, |
| 245 | size_t pool_size = 50); |
| 246 | |
| 247 | SpanReader *BorrowReader(std::string_view id) override; |
| 248 | |
| 249 | private: |
| 250 | const LogSource *log_source_; |
| 251 | std::vector<SpanReader> part_readers_; |
| 252 | const size_t pool_size_; |
| 253 | }; |
| 254 | |
Brian Silverman | fee1697 | 2021-09-14 12:06:38 -0700 | [diff] [blame] | 255 | // Reads the last header from a log file. This handles any duplicate headers |
| 256 | // that were written. |
| 257 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader( |
| 258 | SpanReader *span_reader); |
| 259 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader( |
| 260 | std::string_view filename); |
| 261 | // Reads the Nth message from a log file, excluding the header. Note: this |
| 262 | // doesn't handle duplicate headers. |
| 263 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage( |
| 264 | std::string_view filename, size_t n); |
| 265 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 266 | class UnpackedMessageHeader; |
| 267 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 268 | // Class which handles reading the header and messages from the log file. This |
| 269 | // handles any per-file state left before merging below. |
| 270 | class MessageReader { |
| 271 | public: |
Alexei Strots | 5801740 | 2023-05-03 22:05:06 -0700 | [diff] [blame^] | 272 | // TODO (Alexei): it's deprecated and needs to be removed. |
| 273 | explicit MessageReader(std::string_view filename) |
| 274 | : MessageReader(SpanReader(filename)) {} |
| 275 | |
| 276 | explicit MessageReader(SpanReader span_reader); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 277 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 278 | std::string_view filename() const { return span_reader_.filename(); } |
| 279 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 280 | // Returns the header from the log file. |
| 281 | const LogFileHeader *log_file_header() const { |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 282 | return &raw_log_file_header_.message(); |
| 283 | } |
| 284 | |
| 285 | // Returns the raw data of the header from the log file. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 286 | const SizePrefixedFlatbufferVector<LogFileHeader> &raw_log_file_header() |
| 287 | const { |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 288 | return raw_log_file_header_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // Returns the minimum maount of data needed to queue up for sorting before |
| 292 | // ware guarenteed to not see data out of order. |
| 293 | std::chrono::nanoseconds max_out_of_order_duration() const { |
| 294 | return max_out_of_order_duration_; |
| 295 | } |
| 296 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 297 | // Returns the newest timestamp read out of the log file. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 298 | monotonic_clock::time_point newest_timestamp() const { |
| 299 | return newest_timestamp_; |
| 300 | } |
| 301 | |
| 302 | // Returns the next message if there is one. |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 303 | std::shared_ptr<UnpackedMessageHeader> ReadMessage(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 304 | |
| 305 | // The time at which we need to read another chunk from the logfile. |
| 306 | monotonic_clock::time_point queue_data_time() const { |
| 307 | return newest_timestamp() - max_out_of_order_duration(); |
| 308 | } |
| 309 | |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 310 | // Flag value setters for testing |
| 311 | void set_crash_on_corrupt_message_flag(bool b) { |
| 312 | crash_on_corrupt_message_flag_ = b; |
| 313 | } |
| 314 | void set_ignore_corrupt_messages_flag(bool b) { |
| 315 | ignore_corrupt_messages_flag_ = b; |
| 316 | } |
| 317 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 318 | private: |
| 319 | // Log chunk reader. |
| 320 | SpanReader span_reader_; |
| 321 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 322 | // Vector holding the raw data for the log file header. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 323 | SizePrefixedFlatbufferVector<LogFileHeader> raw_log_file_header_; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 324 | |
| 325 | // Minimum amount of data to queue up for sorting before we are guarenteed |
| 326 | // to not see data out of order. |
| 327 | std::chrono::nanoseconds max_out_of_order_duration_; |
| 328 | |
| 329 | // Timestamp of the newest message in a channel queue. |
| 330 | monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time; |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 331 | |
| 332 | // Total volume of verifiable messages from the beginning of the file. |
| 333 | // TODO - are message counts also useful? |
| 334 | size_t total_verified_before_ = 0; |
| 335 | |
| 336 | // Total volume of messages with corrupted flatbuffer formatting, if any. |
| 337 | // Excludes corrupted message content. |
| 338 | // TODO - if the layout included something as simple as a CRC (relatively |
| 339 | // fast and robust enough) for each span, then corrupted content could be |
| 340 | // included in this check. |
| 341 | size_t total_corrupted_ = 0; |
| 342 | |
| 343 | // Total volume of verifiable messages intermixed with corrupted messages, |
| 344 | // if any. Will be == 0 if total_corrupted_ == 0. |
| 345 | size_t total_verified_during_ = 0; |
| 346 | |
| 347 | // Total volume of verifiable messages found after the last corrupted one, |
| 348 | // if any. Will be == 0 if total_corrupted_ == 0. |
| 349 | size_t total_verified_after_ = 0; |
| 350 | |
| 351 | bool is_corrupted() const { return total_corrupted_ > 0; } |
| 352 | |
| 353 | bool crash_on_corrupt_message_flag_ = true; |
| 354 | bool ignore_corrupt_messages_flag_ = false; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 355 | }; |
| 356 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 357 | // A class to seamlessly read messages from a list of part files. |
| 358 | class PartsMessageReader { |
| 359 | public: |
Alexei Strots | 5801740 | 2023-05-03 22:05:06 -0700 | [diff] [blame^] | 360 | // TODO (Alexei): it's deprecated, need to removed. |
| 361 | explicit PartsMessageReader(LogParts log_parts) |
| 362 | : PartsMessageReader(LogPartsAccess(std::nullopt, std::move(log_parts))) { |
| 363 | } |
| 364 | |
| 365 | explicit PartsMessageReader(LogPartsAccess log_parts_access); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 366 | |
| 367 | std::string_view filename() const { return message_reader_.filename(); } |
| 368 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 369 | // Returns the LogParts that holds the filenames we are reading. |
Alexei Strots | 5801740 | 2023-05-03 22:05:06 -0700 | [diff] [blame^] | 370 | const LogParts &parts() const { return log_parts_access_.parts(); } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 371 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 372 | const LogFileHeader *log_file_header() const { |
| 373 | return message_reader_.log_file_header(); |
| 374 | } |
| 375 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 376 | // Returns the minimum amount of data needed to queue up for sorting before |
| 377 | // we are guarenteed to not see data out of order. |
| 378 | std::chrono::nanoseconds max_out_of_order_duration() const { |
| 379 | return message_reader_.max_out_of_order_duration(); |
| 380 | } |
| 381 | |
| 382 | // Returns the newest timestamp read out of the log file. |
| 383 | monotonic_clock::time_point newest_timestamp() const { |
| 384 | return newest_timestamp_; |
| 385 | } |
| 386 | |
| 387 | // Returns the next message if there is one, or nullopt if we have reached the |
| 388 | // end of all the files. |
| 389 | // 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] | 390 | std::shared_ptr<UnpackedMessageHeader> ReadMessage(); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 391 | |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 392 | // Returns the boot count for the requested node, or std::nullopt if we don't |
| 393 | // know. |
| 394 | std::optional<size_t> boot_count(size_t node_index) const { |
| 395 | CHECK_GE(node_index, 0u); |
| 396 | CHECK_LT(node_index, boot_counts_.size()); |
| 397 | return boot_counts_[node_index]; |
| 398 | } |
| 399 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 400 | private: |
Alexei Strots | 5801740 | 2023-05-03 22:05:06 -0700 | [diff] [blame^] | 401 | static SpanReader MakeSpanReader(const LogPartsAccess &log_parts_access, |
| 402 | size_t part_number); |
| 403 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 404 | // Opens the next log and updates message_reader_. Sets done_ if there is |
| 405 | // nothing more to do. |
| 406 | void NextLog(); |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 407 | void ComputeBootCounts(); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 408 | |
Alexei Strots | 5801740 | 2023-05-03 22:05:06 -0700 | [diff] [blame^] | 409 | const LogPartsAccess log_parts_access_; |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 410 | size_t next_part_index_ = 1u; |
| 411 | bool done_ = false; |
Alexei Strots | 036d84e | 2023-05-03 16:05:12 -0700 | [diff] [blame] | 412 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 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 | |
Eric Schmiedeberg | ae00e73 | 2023-04-12 15:53:17 -0600 | [diff] [blame] | 489 | // Used to be able to mutate the data in the span. This is only used for |
| 490 | // mutating the message inside of LogReader for the Before Send Callback. It |
| 491 | // is safe in this case since there is only one caller to Send, and the data |
| 492 | // is not mutated after Send is called. |
| 493 | uint8_t *mutable_data() { return const_cast<uint8_t *>(span.data()); } |
| 494 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 495 | char actual_data[]; |
| 496 | |
| 497 | private: |
| 498 | ~UnpackedMessageHeader() {} |
| 499 | |
| 500 | static void DestroyAndFree(UnpackedMessageHeader *p) { |
| 501 | p->~UnpackedMessageHeader(); |
| 502 | free(p); |
| 503 | } |
| 504 | }; |
| 505 | |
| 506 | std::ostream &operator<<(std::ostream &os, |
| 507 | const UnpackedMessageHeader &message); |
| 508 | |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 509 | // Struct to hold a message as it gets sorted on a single node. |
| 510 | struct Message { |
| 511 | // The channel. |
| 512 | uint32_t channel_index = 0xffffffff; |
| 513 | // The local queue index. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 514 | // TODO(austin): Technically the boot inside queue_index is redundant with |
| 515 | // timestamp. In practice, it is less error-prone to duplicate it. Maybe a |
| 516 | // function to return the combined struct? |
| 517 | BootQueueIndex queue_index; |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 518 | // The local timestamp. |
| 519 | BootTimestamp timestamp; |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 520 | |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 521 | // Remote boot when this is a timestamp. |
| 522 | size_t monotonic_remote_boot = 0xffffff; |
| 523 | |
| 524 | size_t monotonic_timestamp_boot = 0xffffff; |
| 525 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 526 | std::shared_ptr<UnpackedMessageHeader> data; |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 527 | |
| 528 | bool operator<(const Message &m2) const; |
| 529 | bool operator>=(const Message &m2) const; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 530 | bool operator==(const Message &m2) const; |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 531 | }; |
| 532 | |
| 533 | std::ostream &operator<<(std::ostream &os, const Message &m); |
| 534 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 535 | // Structure to hold a full message and all the timestamps, which may or may not |
| 536 | // have been sent from a remote node. The remote_queue_index will be invalid if |
| 537 | // this message is from the point of view of the node which sent it. |
| 538 | struct TimestampedMessage { |
| 539 | uint32_t channel_index = 0xffffffff; |
| 540 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 541 | BootQueueIndex queue_index; |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 542 | BootTimestamp monotonic_event_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 543 | realtime_clock::time_point realtime_event_time = realtime_clock::min_time; |
| 544 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 545 | BootQueueIndex remote_queue_index; |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 546 | BootTimestamp monotonic_remote_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 547 | realtime_clock::time_point realtime_remote_time = realtime_clock::min_time; |
| 548 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 549 | BootTimestamp monotonic_timestamp_time; |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 550 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 551 | std::shared_ptr<UnpackedMessageHeader> data; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 552 | }; |
| 553 | |
| 554 | std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m); |
| 555 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 556 | // Class to sort the resulting messages from a PartsMessageReader. |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 557 | class MessageSorter { |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 558 | public: |
Alexei Strots | 5801740 | 2023-05-03 22:05:06 -0700 | [diff] [blame^] | 559 | // TODO (Alexei): it's deperecated and need to be removed. |
| 560 | explicit MessageSorter(LogParts log_parts) |
| 561 | : MessageSorter(LogPartsAccess(std::nullopt, std::move(log_parts))) {} |
| 562 | |
| 563 | explicit MessageSorter(const LogPartsAccess log_parts_access); |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 564 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 565 | // Returns the parts that this is sorting messages from. |
| 566 | const LogParts &parts() const { return parts_message_reader_.parts(); } |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 567 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 568 | monotonic_clock::time_point monotonic_start_time() const { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 569 | return parts().monotonic_start_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 570 | } |
| 571 | realtime_clock::time_point realtime_start_time() const { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 572 | return parts().realtime_start_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 575 | // The time this data is sorted until. |
| 576 | monotonic_clock::time_point sorted_until() const { return sorted_until_; } |
| 577 | |
| 578 | // Returns the next sorted message from the log file. It is safe to call |
| 579 | // std::move() on the result to move the data flatbuffer from it. |
| 580 | Message *Front(); |
| 581 | // Pops the front message. This should only be called after a call to |
| 582 | // Front(). |
| 583 | void PopFront(); |
| 584 | |
| 585 | // Returns a debug string representing the contents of this sorter. |
| 586 | std::string DebugString() const; |
| 587 | |
| 588 | private: |
| 589 | // Log parts reader we are wrapping. |
| 590 | PartsMessageReader parts_message_reader_; |
| 591 | // Cache of the time we are sorted until. |
| 592 | aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time; |
| 593 | |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 594 | // Timestamp of the last message returned. Used to make sure nothing goes |
| 595 | // backwards. |
| 596 | monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time; |
| 597 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 598 | // Set used for efficient sorting of messages. We can benchmark and evaluate |
| 599 | // other data structures if this proves to be the bottleneck. |
| 600 | absl::btree_set<Message> messages_; |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 601 | |
| 602 | // Mapping from channel to source node. |
| 603 | // TODO(austin): Should we put this in Boots so it can be cached for everyone? |
| 604 | std::vector<size_t> source_node_index_; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 605 | }; |
| 606 | |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 607 | // Class to run merge sort on the messages associated with specific node and |
| 608 | // boot. |
| 609 | class PartsMerger { |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 610 | public: |
Alexei Strots | 1f51ac7 | 2023-05-15 10:14:54 -0700 | [diff] [blame] | 611 | PartsMerger(std::string_view node_name, size_t boot_count, |
| 612 | const LogFilesContainer &log_files); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 613 | |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 614 | // Copying and moving will mess up the internal raw pointers. Just don't do |
| 615 | // it. |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 616 | PartsMerger(PartsMerger const &) = delete; |
| 617 | PartsMerger(PartsMerger &&) = delete; |
| 618 | void operator=(PartsMerger const &) = delete; |
| 619 | void operator=(PartsMerger &&) = delete; |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 620 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 621 | // Node index in the configuration of this node. |
| 622 | int node() const { return node_; } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 623 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 624 | // List of parts being sorted together. |
| 625 | std::vector<const LogParts *> Parts() const; |
| 626 | |
| 627 | const Configuration *configuration() const { |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 628 | return message_sorters_[0].parts().config.get(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | monotonic_clock::time_point monotonic_start_time() const { |
| 632 | return monotonic_start_time_; |
| 633 | } |
| 634 | realtime_clock::time_point realtime_start_time() const { |
| 635 | return realtime_start_time_; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 636 | } |
Austin Schuh | 5dd2284 | 2021-11-17 16:09:39 -0800 | [diff] [blame] | 637 | monotonic_clock::time_point monotonic_oldest_time() const { |
| 638 | return monotonic_oldest_time_; |
| 639 | } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 640 | |
| 641 | // The time this data is sorted until. |
| 642 | monotonic_clock::time_point sorted_until() const { return sorted_until_; } |
| 643 | |
| 644 | // Returns the next sorted message from the set of log files. It is safe to |
| 645 | // call std::move() on the result to move the data flatbuffer from it. |
| 646 | Message *Front(); |
| 647 | // Pops the front message. This should only be called after a call to |
| 648 | // Front(). |
| 649 | void PopFront(); |
| 650 | |
| 651 | private: |
| 652 | // Unsorted list of all parts sorters. |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 653 | std::vector<MessageSorter> message_sorters_; |
Alexei Strots | 5801740 | 2023-05-03 22:05:06 -0700 | [diff] [blame^] | 654 | |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 655 | // Pointer to the parts sorter holding the current Front message if one |
| 656 | // exists, or nullptr if a new one needs to be found. |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 657 | MessageSorter *current_ = nullptr; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 658 | // Cached sorted_until value. |
| 659 | aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 660 | |
| 661 | // Cached node. |
| 662 | int node_; |
| 663 | |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 664 | // Timestamp of the last message returned. Used to make sure nothing goes |
| 665 | // backwards. |
| 666 | monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time; |
| 667 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 668 | realtime_clock::time_point realtime_start_time_ = realtime_clock::max_time; |
| 669 | monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::max_time; |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 670 | monotonic_clock::time_point monotonic_oldest_time_ = |
| 671 | monotonic_clock::max_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 672 | }; |
| 673 | |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 674 | // Class to concatenate multiple boots worth of logs into a single per-node |
| 675 | // stream. |
| 676 | class BootMerger { |
| 677 | public: |
Alexei Strots | 1f51ac7 | 2023-05-15 10:14:54 -0700 | [diff] [blame] | 678 | BootMerger(std::string_view node_name, const LogFilesContainer &log_files); |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 679 | |
| 680 | // Copying and moving will mess up the internal raw pointers. Just don't do |
| 681 | // it. |
| 682 | BootMerger(BootMerger const &) = delete; |
| 683 | BootMerger(BootMerger &&) = delete; |
| 684 | void operator=(BootMerger const &) = delete; |
| 685 | void operator=(BootMerger &&) = delete; |
| 686 | |
| 687 | // Node index in the configuration of this node. |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 688 | int node() const { return parts_mergers_[0]->node(); } |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 689 | |
| 690 | // List of parts being sorted together. |
| 691 | std::vector<const LogParts *> Parts() const; |
| 692 | |
| 693 | const Configuration *configuration() const { |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 694 | return parts_mergers_[0]->configuration(); |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 697 | monotonic_clock::time_point monotonic_start_time(size_t boot) const { |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 698 | CHECK_LT(boot, parts_mergers_.size()); |
| 699 | return parts_mergers_[boot]->monotonic_start_time(); |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 700 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 701 | realtime_clock::time_point realtime_start_time(size_t boot) const { |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 702 | CHECK_LT(boot, parts_mergers_.size()); |
| 703 | return parts_mergers_[boot]->realtime_start_time(); |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 704 | } |
Austin Schuh | 5dd2284 | 2021-11-17 16:09:39 -0800 | [diff] [blame] | 705 | monotonic_clock::time_point monotonic_oldest_time(size_t boot) const { |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 706 | CHECK_LT(boot, parts_mergers_.size()); |
| 707 | return parts_mergers_[boot]->monotonic_oldest_time(); |
Austin Schuh | 5dd2284 | 2021-11-17 16:09:39 -0800 | [diff] [blame] | 708 | } |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 709 | |
| 710 | bool started() const { |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 711 | return parts_mergers_[index_]->sorted_until() != |
| 712 | monotonic_clock::min_time || |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 713 | index_ != 0; |
| 714 | } |
| 715 | |
| 716 | // Returns the next sorted message from the set of log files. It is safe to |
| 717 | // call std::move() on the result to move the data flatbuffer from it. |
| 718 | Message *Front(); |
| 719 | // Pops the front message. This should only be called after a call to |
| 720 | // Front(). |
| 721 | void PopFront(); |
| 722 | |
| 723 | private: |
| 724 | int index_ = 0; |
| 725 | |
| 726 | // TODO(austin): Sanjay points out this is pretty inefficient. Don't keep so |
| 727 | // many things open. |
Alexei Strots | a8dadd1 | 2023-04-28 15:19:23 -0700 | [diff] [blame] | 728 | std::vector<std::unique_ptr<PartsMerger>> parts_mergers_; |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 729 | }; |
| 730 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 731 | // Class to match timestamps with the corresponding data from other nodes. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 732 | // |
| 733 | // This class also buffers data for the node it represents, and supports |
| 734 | // 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] | 735 | class TimestampMapper { |
| 736 | public: |
Alexei Strots | 1f51ac7 | 2023-05-15 10:14:54 -0700 | [diff] [blame] | 737 | TimestampMapper(std::string_view node_name, |
| 738 | const LogFilesContainer &log_files); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 739 | |
| 740 | // Copying and moving will mess up the internal raw pointers. Just don't do |
| 741 | // it. |
| 742 | TimestampMapper(TimestampMapper const &) = delete; |
| 743 | TimestampMapper(TimestampMapper &&) = delete; |
| 744 | void operator=(TimestampMapper const &) = delete; |
| 745 | void operator=(TimestampMapper &&) = delete; |
| 746 | |
| 747 | // TODO(austin): It would be super helpful to provide a way to queue up to |
| 748 | // time X without matching timestamps, and to then be able to pull the |
| 749 | // timestamps out of this queue. This lets us bootstrap time estimation |
| 750 | // without exploding memory usage worst case. |
| 751 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 752 | const Configuration *configuration() const { return configuration_.get(); } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 753 | |
| 754 | // Returns which node this is sorting for. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 755 | size_t node() const { return boot_merger_.node(); } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 756 | |
| 757 | // The start time of this log. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 758 | monotonic_clock::time_point monotonic_start_time(size_t boot) const { |
| 759 | return boot_merger_.monotonic_start_time(boot); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 760 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 761 | realtime_clock::time_point realtime_start_time(size_t boot) const { |
| 762 | return boot_merger_.realtime_start_time(boot); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 763 | } |
Austin Schuh | 5dd2284 | 2021-11-17 16:09:39 -0800 | [diff] [blame] | 764 | // Returns the oldest timestamp on a message on this boot. |
| 765 | monotonic_clock::time_point monotonic_oldest_time(size_t boot) const { |
| 766 | return boot_merger_.monotonic_oldest_time(boot); |
| 767 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 768 | |
| 769 | // Uses timestamp_mapper as the peer for its node. Only one mapper may be set |
| 770 | // for each node. Peers are used to look up the data for timestamps on this |
| 771 | // node. |
| 772 | void AddPeer(TimestampMapper *timestamp_mapper); |
| 773 | |
Austin Schuh | 24bf497 | 2021-06-29 22:09:08 -0700 | [diff] [blame] | 774 | // Returns true if anything has been queued up. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 775 | bool started() const { return boot_merger_.started(); } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 776 | |
| 777 | // Returns the next message for this node. |
| 778 | TimestampedMessage *Front(); |
| 779 | // Pops the next message. Front must be called first. |
| 780 | void PopFront(); |
| 781 | |
| 782 | // Returns debug information about this node. |
| 783 | std::string DebugString() const; |
| 784 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 785 | // Queues data the provided time. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 786 | void QueueUntil(BootTimestamp queue_time); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 787 | // Queues until we have time_estimation_buffer of data in the queue. |
| 788 | void QueueFor(std::chrono::nanoseconds time_estimation_buffer); |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 789 | |
Austin Schuh | 0660122 | 2021-01-26 17:02:50 -0800 | [diff] [blame] | 790 | // Queues until the condition is met. |
| 791 | template <typename T> |
| 792 | void QueueUntilCondition(T fn) { |
| 793 | while (true) { |
| 794 | if (fn()) { |
| 795 | break; |
| 796 | } |
| 797 | if (!QueueMatched()) { |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 803 | // Sets the callback that can be used to skip messages. |
| 804 | void set_replay_channels_callback( |
| 805 | std::function<bool(const TimestampedMessage &)> fn) { |
| 806 | replay_channels_callback_ = fn; |
| 807 | } |
| 808 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 809 | // Sets a callback to be called whenever a full message is queued. |
| 810 | void set_timestamp_callback(std::function<void(TimestampedMessage *)> fn) { |
| 811 | timestamp_callback_ = fn; |
| 812 | } |
| 813 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 814 | private: |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 815 | // Result of MaybeQueueMatched |
| 816 | enum class MatchResult : uint8_t { |
| 817 | kEndOfFile, // End of the log file being read |
| 818 | kQueued, // Message was queued |
| 819 | kSkipped // Message was skipped over |
| 820 | }; |
| 821 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 822 | // The state for a remote node. This holds the data that needs to be matched |
| 823 | // with the remote node's timestamps. |
| 824 | struct NodeData { |
| 825 | // True if we should save data here. This should be true if any of the |
| 826 | // bools in delivered below are true. |
| 827 | bool any_delivered = false; |
| 828 | |
Austin Schuh | 36c0093 | 2021-07-19 18:13:21 -0700 | [diff] [blame] | 829 | // True if we have a peer and therefore should be saving data for it. |
| 830 | bool save_for_peer = false; |
| 831 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 832 | // Peer pointer. This node is only to be considered if a peer is set. |
| 833 | TimestampMapper *peer = nullptr; |
| 834 | |
| 835 | struct ChannelData { |
| 836 | // Deque per channel. This contains the data from the outside |
| 837 | // TimestampMapper node which is relevant for the node this NodeData |
| 838 | // points to. |
| 839 | std::deque<Message> messages; |
| 840 | // Bool tracking per channel if a message is delivered to the node this |
| 841 | // NodeData represents. |
| 842 | bool delivered = false; |
Austin Schuh | 6a7358f | 2021-11-18 22:40:40 -0800 | [diff] [blame] | 843 | // The TTL for delivery. |
| 844 | std::chrono::nanoseconds time_to_live = std::chrono::nanoseconds(0); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 845 | }; |
| 846 | |
| 847 | // Vector with per channel data. |
| 848 | std::vector<ChannelData> channels; |
| 849 | }; |
| 850 | |
| 851 | // Returns (and forgets about) the data for the provided timestamp message |
| 852 | // showing when it was delivered to this node. |
| 853 | Message MatchingMessageFor(const Message &message); |
| 854 | |
| 855 | // Queues up a single message into our message queue, and any nodes that this |
| 856 | // message is delivered to. Returns true if one was available, false |
| 857 | // otherwise. |
| 858 | bool Queue(); |
| 859 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 860 | // Queues up a single matched message into our matched message queue. Returns |
| 861 | // true if one was queued, and false otherwise. |
| 862 | bool QueueMatched(); |
| 863 | |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 864 | // Queues a message if the replay_channels_callback is passed and the end of |
| 865 | // the log file has not been reached. |
| 866 | MatchResult MaybeQueueMatched(); |
| 867 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 868 | // Queues up data until we have at least one message >= to time t. |
| 869 | // Useful for triggering a remote node to read enough data to have the |
| 870 | // timestamp you care about available. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 871 | void QueueUnmatchedUntil(BootTimestamp t); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 872 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 873 | // Queues m into matched_messages_. |
| 874 | void QueueMessage(Message *m); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 875 | |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 876 | // If a replay_channels_callback was set and the callback returns false, a |
| 877 | // matched message is popped and true is returned. Otherwise false is |
| 878 | // returned. |
| 879 | bool CheckReplayChannelsAndMaybePop(const TimestampedMessage &message); |
| 880 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 881 | // Returns the name of the node this class is sorting for. |
| 882 | std::string_view node_name() const { |
| 883 | return configuration_->has_nodes() ? configuration_->nodes() |
| 884 | ->Get(boot_merger_.node()) |
| 885 | ->name() |
| 886 | ->string_view() |
| 887 | : "(single node)"; |
| 888 | } |
| 889 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 890 | // The node merger to source messages from. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 891 | BootMerger boot_merger_; |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 892 | |
| 893 | std::shared_ptr<const Configuration> configuration_; |
| 894 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 895 | // The buffer of messages for this node. These are not matched with any |
| 896 | // remote data. |
| 897 | std::deque<Message> messages_; |
| 898 | // The node index for the source node for each channel. |
| 899 | std::vector<size_t> source_node_; |
| 900 | |
| 901 | // Vector per node. Not all nodes will have anything. |
| 902 | std::vector<NodeData> nodes_data_; |
| 903 | |
| 904 | // Latest message to return. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 905 | std::deque<TimestampedMessage> matched_messages_; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 906 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 907 | // Tracks the state of the first message in matched_messages_. Do we need to |
| 908 | // update it, is it valid, or should we return nullptr? |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 909 | enum class FirstMessage { |
| 910 | kNeedsUpdate, |
| 911 | kInMessage, |
| 912 | kNullptr, |
| 913 | }; |
| 914 | FirstMessage first_message_ = FirstMessage::kNeedsUpdate; |
| 915 | |
| 916 | // Timestamp of the last message returned. Used to make sure nothing goes |
| 917 | // backwards. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 918 | BootTimestamp last_message_time_ = BootTimestamp::min_time(); |
Austin Schuh | 6a7358f | 2021-11-18 22:40:40 -0800 | [diff] [blame] | 919 | BootTimestamp last_popped_message_time_ = BootTimestamp::min_time(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 920 | // Time this node is queued up until. Used for caching. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 921 | BootTimestamp queued_until_ = BootTimestamp::min_time(); |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 922 | |
| 923 | std::function<void(TimestampedMessage *)> timestamp_callback_; |
Eric Schmiedeberg | b38477e | 2022-12-02 16:08:04 -0700 | [diff] [blame] | 924 | std::function<bool(TimestampedMessage &)> replay_channels_callback_; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 925 | }; |
| 926 | |
Alexei Strots | 036d84e | 2023-05-03 16:05:12 -0700 | [diff] [blame] | 927 | // Returns the node name, or an empty string if we are a single node. |
| 928 | inline std::string_view MaybeNodeName(const Node *node) { |
| 929 | if (node != nullptr) { |
| 930 | return node->name()->string_view(); |
| 931 | } |
| 932 | return ""; |
| 933 | } |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 934 | |
Austin Schuh | 71a40d4 | 2023-02-04 21:22:22 -0800 | [diff] [blame] | 935 | // Class to copy a RemoteMessage into the provided buffer. |
| 936 | class RemoteMessageCopier : public DataEncoder::Copier { |
| 937 | public: |
| 938 | RemoteMessageCopier(const message_bridge::RemoteMessage *message, |
| 939 | int channel_index, |
| 940 | aos::monotonic_clock::time_point monotonic_timestamp_time, |
| 941 | EventLoop *event_loop) |
| 942 | : DataEncoder::Copier(PackRemoteMessageSize()), |
| 943 | message_(message), |
| 944 | channel_index_(channel_index), |
| 945 | monotonic_timestamp_time_(monotonic_timestamp_time), |
| 946 | event_loop_(event_loop) {} |
| 947 | |
| 948 | monotonic_clock::time_point end_time() const { return end_time_; } |
| 949 | |
| 950 | size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final { |
| 951 | size_t result = PackRemoteMessageInline(data, message_, channel_index_, |
| 952 | monotonic_timestamp_time_, |
| 953 | start_byte, end_byte); |
| 954 | end_time_ = event_loop_->monotonic_now(); |
| 955 | return result; |
| 956 | } |
| 957 | |
| 958 | private: |
| 959 | const message_bridge::RemoteMessage *message_; |
| 960 | int channel_index_; |
| 961 | aos::monotonic_clock::time_point monotonic_timestamp_time_; |
| 962 | EventLoop *event_loop_; |
| 963 | monotonic_clock::time_point end_time_; |
| 964 | }; |
| 965 | |
| 966 | // Class to copy a context into the provided buffer. |
| 967 | class ContextDataCopier : public DataEncoder::Copier { |
| 968 | public: |
| 969 | ContextDataCopier(const Context &context, int channel_index, LogType log_type, |
| 970 | EventLoop *event_loop) |
| 971 | : DataEncoder::Copier(PackMessageSize(log_type, context.size)), |
| 972 | context_(context), |
| 973 | channel_index_(channel_index), |
| 974 | log_type_(log_type), |
| 975 | event_loop_(event_loop) {} |
| 976 | |
| 977 | monotonic_clock::time_point end_time() const { return end_time_; } |
| 978 | |
| 979 | size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final { |
| 980 | size_t result = PackMessageInline(data, context_, channel_index_, log_type_, |
| 981 | start_byte, end_byte); |
| 982 | end_time_ = event_loop_->monotonic_now(); |
| 983 | return result; |
| 984 | } |
| 985 | |
| 986 | private: |
| 987 | const Context &context_; |
| 988 | const int channel_index_; |
| 989 | const LogType log_type_; |
| 990 | EventLoop *event_loop_; |
| 991 | monotonic_clock::time_point end_time_; |
| 992 | }; |
| 993 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 994 | } // namespace aos::logger |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 995 | |
| 996 | #endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_ |