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