Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 1 | #include "aos/events/logging/logfile_utils.h" |
| 2 | |
| 3 | #include <fcntl.h> |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 4 | #include <sys/stat.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/uio.h> |
| 7 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | #include <climits> |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 10 | |
Austin Schuh | e4fca83 | 2020-03-07 16:58:53 -0800 | [diff] [blame] | 11 | #include "absl/strings/escaping.h" |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 12 | #include "aos/configuration.h" |
James Kuszmaul | dd0a504 | 2021-10-28 23:38:04 -0700 | [diff] [blame] | 13 | #include "aos/events/logging/snappy_encoder.h" |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 14 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 15 | #include "aos/util/file.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 16 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 17 | #include "gflags/gflags.h" |
| 18 | #include "glog/logging.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 19 | |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 20 | #if defined(__x86_64__) |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 21 | #define ENABLE_LZMA (!__has_feature(memory_sanitizer)) |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 22 | #elif defined(__aarch64__) |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 23 | #define ENABLE_LZMA (!__has_feature(memory_sanitizer)) |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 24 | #else |
| 25 | #define ENABLE_LZMA 0 |
| 26 | #endif |
| 27 | |
| 28 | #if ENABLE_LZMA |
| 29 | #include "aos/events/logging/lzma_encoder.h" |
| 30 | #endif |
| 31 | |
Austin Schuh | 7fbf5a7 | 2020-09-21 16:28:13 -0700 | [diff] [blame] | 32 | DEFINE_int32(flush_size, 128000, |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 33 | "Number of outstanding bytes to allow before flushing to disk."); |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 34 | DEFINE_double( |
| 35 | flush_period, 5.0, |
| 36 | "Max time to let data sit in the queue before flushing in seconds."); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 37 | |
Austin Schuh | a040c3f | 2021-02-13 16:09:07 -0800 | [diff] [blame] | 38 | DEFINE_double( |
Austin Schuh | 6a7358f | 2021-11-18 22:40:40 -0800 | [diff] [blame] | 39 | max_network_delay, 1.0, |
| 40 | "Max time to assume a message takes to cross the network before we are " |
| 41 | "willing to drop it from our buffers and assume it didn't make it. " |
| 42 | "Increasing this number can increase memory usage depending on the packet " |
| 43 | "loss of your network or if the timestamps aren't logged for a message."); |
| 44 | |
| 45 | DEFINE_double( |
Austin Schuh | a040c3f | 2021-02-13 16:09:07 -0800 | [diff] [blame] | 46 | max_out_of_order, -1, |
| 47 | "If set, this overrides the max out of order duration for a log file."); |
| 48 | |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 49 | DEFINE_bool(workaround_double_headers, true, |
| 50 | "Some old log files have two headers at the beginning. Use the " |
| 51 | "last header as the actual header."); |
| 52 | |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 53 | DEFINE_bool(crash_on_corrupt_message, true, |
| 54 | "When true, MessageReader will crash the first time a message " |
| 55 | "with corrupted format is found. When false, the crash will be " |
| 56 | "suppressed, and any remaining readable messages will be " |
| 57 | "evaluated to present verified vs corrupted stats."); |
| 58 | |
| 59 | DEFINE_bool(ignore_corrupt_messages, false, |
| 60 | "When true, and crash_on_corrupt_message is false, then any " |
| 61 | "corrupt message found by MessageReader be silently ignored, " |
| 62 | "providing access to all uncorrupted messages in a logfile."); |
| 63 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 64 | namespace aos::logger { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 65 | namespace { |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 66 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 67 | namespace chrono = std::chrono; |
| 68 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 69 | template <typename T> |
| 70 | void PrintOptionalOrNull(std::ostream *os, const std::optional<T> &t) { |
| 71 | if (t.has_value()) { |
| 72 | *os << *t; |
| 73 | } else { |
| 74 | *os << "null"; |
| 75 | } |
| 76 | } |
| 77 | } // namespace |
| 78 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 79 | DetachedBufferWriter::DetachedBufferWriter( |
| 80 | std::string_view filename, std::unique_ptr<DetachedBufferEncoder> encoder) |
| 81 | : filename_(filename), encoder_(std::move(encoder)) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 82 | if (!util::MkdirPIfSpace(filename, 0777)) { |
| 83 | ran_out_of_space_ = true; |
| 84 | } else { |
| 85 | fd_ = open(std::string(filename).c_str(), |
| 86 | O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 0774); |
| 87 | if (fd_ == -1 && errno == ENOSPC) { |
| 88 | ran_out_of_space_ = true; |
| 89 | } else { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 90 | PCHECK(fd_ != -1) << ": Failed to open " << this->filename() |
| 91 | << " for writing"; |
| 92 | VLOG(1) << "Opened " << this->filename() << " for writing"; |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | DetachedBufferWriter::~DetachedBufferWriter() { |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 98 | Close(); |
| 99 | if (ran_out_of_space_) { |
| 100 | CHECK(acknowledge_ran_out_of_space_) |
| 101 | << ": Unacknowledged out of disk space, log file was not completed"; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 102 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 105 | DetachedBufferWriter::DetachedBufferWriter(DetachedBufferWriter &&other) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 106 | *this = std::move(other); |
| 107 | } |
| 108 | |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 109 | // When other is destroyed "soon" (which it should be because we're getting an |
| 110 | // rvalue reference to it), it will flush etc all the data we have queued up |
| 111 | // (because that data will then be its data). |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 112 | DetachedBufferWriter &DetachedBufferWriter::operator=( |
| 113 | DetachedBufferWriter &&other) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 114 | std::swap(filename_, other.filename_); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 115 | std::swap(encoder_, other.encoder_); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 116 | std::swap(fd_, other.fd_); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 117 | std::swap(ran_out_of_space_, other.ran_out_of_space_); |
| 118 | std::swap(acknowledge_ran_out_of_space_, other.acknowledge_ran_out_of_space_); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 119 | std::swap(iovec_, other.iovec_); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 120 | std::swap(max_write_time_, other.max_write_time_); |
| 121 | std::swap(max_write_time_bytes_, other.max_write_time_bytes_); |
| 122 | std::swap(max_write_time_messages_, other.max_write_time_messages_); |
| 123 | std::swap(total_write_time_, other.total_write_time_); |
| 124 | std::swap(total_write_count_, other.total_write_count_); |
| 125 | std::swap(total_write_messages_, other.total_write_messages_); |
| 126 | std::swap(total_write_bytes_, other.total_write_bytes_); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 127 | return *this; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 130 | void DetachedBufferWriter::QueueSpan(absl::Span<const uint8_t> span) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 131 | if (ran_out_of_space_) { |
| 132 | // We don't want any later data to be written after space becomes |
| 133 | // available, so refuse to write anything more once we've dropped data |
| 134 | // because we ran out of space. |
| 135 | VLOG(1) << "Ignoring span: " << span.size(); |
| 136 | return; |
| 137 | } |
| 138 | |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 139 | aos::monotonic_clock::time_point now; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 140 | if (encoder_->may_bypass() && span.size() > 4096u) { |
| 141 | // Over this threshold, we'll assume it's cheaper to add an extra |
| 142 | // syscall to write the data immediately instead of copying it to |
| 143 | // enqueue. |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 144 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 145 | // First, flush everything. |
| 146 | while (encoder_->queue_size() > 0u) { |
| 147 | Flush(); |
| 148 | } |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 149 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 150 | // Then, write it directly. |
| 151 | const auto start = aos::monotonic_clock::now(); |
| 152 | const ssize_t written = write(fd_, span.data(), span.size()); |
| 153 | const auto end = aos::monotonic_clock::now(); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 154 | HandleWriteReturn(written, span.size()); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 155 | UpdateStatsForWrite(end - start, written, 1); |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 156 | now = end; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 157 | } else { |
| 158 | encoder_->Encode(CopySpanAsDetachedBuffer(span)); |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 159 | now = aos::monotonic_clock::now(); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 160 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 161 | |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 162 | FlushAtThreshold(now); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 165 | void DetachedBufferWriter::Close() { |
| 166 | if (fd_ == -1) { |
| 167 | return; |
| 168 | } |
| 169 | encoder_->Finish(); |
| 170 | while (encoder_->queue_size() > 0) { |
| 171 | Flush(); |
| 172 | } |
| 173 | if (close(fd_) == -1) { |
| 174 | if (errno == ENOSPC) { |
| 175 | ran_out_of_space_ = true; |
| 176 | } else { |
| 177 | PLOG(ERROR) << "Closing log file failed"; |
| 178 | } |
| 179 | } |
| 180 | fd_ = -1; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 181 | VLOG(1) << "Closed " << filename(); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 184 | void DetachedBufferWriter::Flush() { |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 185 | if (ran_out_of_space_) { |
| 186 | // We don't want any later data to be written after space becomes available, |
| 187 | // so refuse to write anything more once we've dropped data because we ran |
| 188 | // out of space. |
Austin Schuh | a426f1f | 2021-03-31 22:27:41 -0700 | [diff] [blame] | 189 | if (encoder_) { |
| 190 | VLOG(1) << "Ignoring queue: " << encoder_->queue().size(); |
| 191 | encoder_->Clear(encoder_->queue().size()); |
| 192 | } else { |
| 193 | VLOG(1) << "No queue to ignore"; |
| 194 | } |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | const auto queue = encoder_->queue(); |
| 199 | if (queue.empty()) { |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 200 | return; |
| 201 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 202 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 203 | iovec_.clear(); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 204 | const size_t iovec_size = std::min<size_t>(queue.size(), IOV_MAX); |
| 205 | iovec_.resize(iovec_size); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 206 | size_t counted_size = 0; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 207 | for (size_t i = 0; i < iovec_size; ++i) { |
| 208 | iovec_[i].iov_base = const_cast<uint8_t *>(queue[i].data()); |
| 209 | iovec_[i].iov_len = queue[i].size(); |
| 210 | counted_size += iovec_[i].iov_len; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 211 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 212 | |
| 213 | const auto start = aos::monotonic_clock::now(); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 214 | const ssize_t written = writev(fd_, iovec_.data(), iovec_.size()); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 215 | const auto end = aos::monotonic_clock::now(); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 216 | HandleWriteReturn(written, counted_size); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 217 | |
| 218 | encoder_->Clear(iovec_size); |
| 219 | |
| 220 | UpdateStatsForWrite(end - start, written, iovec_size); |
| 221 | } |
| 222 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 223 | void DetachedBufferWriter::HandleWriteReturn(ssize_t write_return, |
| 224 | size_t write_size) { |
| 225 | if (write_return == -1 && errno == ENOSPC) { |
| 226 | ran_out_of_space_ = true; |
| 227 | return; |
| 228 | } |
| 229 | PCHECK(write_return >= 0) << ": write failed"; |
| 230 | if (write_return < static_cast<ssize_t>(write_size)) { |
| 231 | // Sometimes this happens instead of ENOSPC. On a real filesystem, this |
| 232 | // never seems to happen in any other case. If we ever want to log to a |
| 233 | // socket, this will happen more often. However, until we get there, we'll |
| 234 | // just assume it means we ran out of space. |
| 235 | ran_out_of_space_ = true; |
| 236 | return; |
| 237 | } |
| 238 | } |
| 239 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 240 | void DetachedBufferWriter::UpdateStatsForWrite( |
| 241 | aos::monotonic_clock::duration duration, ssize_t written, int iovec_size) { |
| 242 | if (duration > max_write_time_) { |
| 243 | max_write_time_ = duration; |
| 244 | max_write_time_bytes_ = written; |
| 245 | max_write_time_messages_ = iovec_size; |
| 246 | } |
| 247 | total_write_time_ += duration; |
| 248 | ++total_write_count_; |
| 249 | total_write_messages_ += iovec_size; |
| 250 | total_write_bytes_ += written; |
| 251 | } |
| 252 | |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 253 | void DetachedBufferWriter::FlushAtThreshold( |
| 254 | aos::monotonic_clock::time_point now) { |
Austin Schuh | a426f1f | 2021-03-31 22:27:41 -0700 | [diff] [blame] | 255 | if (ran_out_of_space_) { |
| 256 | // We don't want any later data to be written after space becomes available, |
| 257 | // so refuse to write anything more once we've dropped data because we ran |
| 258 | // out of space. |
| 259 | if (encoder_) { |
| 260 | VLOG(1) << "Ignoring queue: " << encoder_->queue().size(); |
| 261 | encoder_->Clear(encoder_->queue().size()); |
| 262 | } else { |
| 263 | VLOG(1) << "No queue to ignore"; |
| 264 | } |
| 265 | return; |
| 266 | } |
| 267 | |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 268 | // We don't want to flush the first time through. Otherwise we will flush as |
| 269 | // the log file header might be compressing, defeating any parallelism and |
| 270 | // queueing there. |
| 271 | if (last_flush_time_ == aos::monotonic_clock::min_time) { |
| 272 | last_flush_time_ = now; |
| 273 | } |
| 274 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 275 | // Flush if we are at the max number of iovs per writev, because there's no |
| 276 | // point queueing up any more data in memory. Also flush once we have enough |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 277 | // data queued up or if it has been long enough. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 278 | while (encoder_->queued_bytes() > static_cast<size_t>(FLAGS_flush_size) || |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 279 | encoder_->queue_size() >= IOV_MAX || |
| 280 | now > last_flush_time_ + |
| 281 | chrono::duration_cast<chrono::nanoseconds>( |
| 282 | chrono::duration<double>(FLAGS_flush_period))) { |
| 283 | last_flush_time_ = now; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 284 | Flush(); |
| 285 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Austin Schuh | f2d0e68 | 2022-10-16 14:20:58 -0700 | [diff] [blame^] | 288 | // Do the magic dance to convert the endianness of the data and append it to the |
| 289 | // buffer. |
| 290 | namespace { |
| 291 | |
| 292 | // TODO(austin): Look at the generated code to see if building the header is |
| 293 | // efficient or not. |
| 294 | template <typename T> |
| 295 | uint8_t *Push(uint8_t *buffer, const T data) { |
| 296 | const T endian_data = flatbuffers::EndianScalar<T>(data); |
| 297 | std::memcpy(buffer, &endian_data, sizeof(T)); |
| 298 | return buffer + sizeof(T); |
| 299 | } |
| 300 | |
| 301 | uint8_t *PushBytes(uint8_t *buffer, const void *data, size_t size) { |
| 302 | std::memcpy(buffer, data, size); |
| 303 | return buffer + size; |
| 304 | } |
| 305 | |
| 306 | uint8_t *Pad(uint8_t *buffer, size_t padding) { |
| 307 | std::memset(buffer, 0, padding); |
| 308 | return buffer + padding; |
| 309 | } |
| 310 | } // namespace |
| 311 | |
| 312 | flatbuffers::Offset<MessageHeader> PackRemoteMessage( |
| 313 | flatbuffers::FlatBufferBuilder *fbb, |
| 314 | const message_bridge::RemoteMessage *msg, int channel_index, |
| 315 | const aos::monotonic_clock::time_point monotonic_timestamp_time) { |
| 316 | logger::MessageHeader::Builder message_header_builder(*fbb); |
| 317 | // Note: this must match the same order as MessageBridgeServer and |
| 318 | // PackMessage. We want identical headers to have identical |
| 319 | // on-the-wire formats to make comparing them easier. |
| 320 | |
| 321 | message_header_builder.add_channel_index(channel_index); |
| 322 | |
| 323 | message_header_builder.add_queue_index(msg->queue_index()); |
| 324 | message_header_builder.add_monotonic_sent_time(msg->monotonic_sent_time()); |
| 325 | message_header_builder.add_realtime_sent_time(msg->realtime_sent_time()); |
| 326 | |
| 327 | message_header_builder.add_monotonic_remote_time( |
| 328 | msg->monotonic_remote_time()); |
| 329 | message_header_builder.add_realtime_remote_time(msg->realtime_remote_time()); |
| 330 | message_header_builder.add_remote_queue_index(msg->remote_queue_index()); |
| 331 | |
| 332 | message_header_builder.add_monotonic_timestamp_time( |
| 333 | monotonic_timestamp_time.time_since_epoch().count()); |
| 334 | |
| 335 | return message_header_builder.Finish(); |
| 336 | } |
| 337 | |
| 338 | size_t PackRemoteMessageInline( |
| 339 | uint8_t *buffer, const message_bridge::RemoteMessage *msg, |
| 340 | int channel_index, |
| 341 | const aos::monotonic_clock::time_point monotonic_timestamp_time) { |
| 342 | const flatbuffers::uoffset_t message_size = PackRemoteMessageSize(); |
| 343 | |
| 344 | // clang-format off |
| 345 | // header: |
| 346 | // +0x00 | 5C 00 00 00 | UOffset32 | 0x0000005C (92) Loc: +0x5C | size prefix |
| 347 | buffer = Push<flatbuffers::uoffset_t>( |
| 348 | buffer, message_size - sizeof(flatbuffers::uoffset_t)); |
| 349 | // +0x04 | 20 00 00 00 | UOffset32 | 0x00000020 (32) Loc: +0x24 | offset to root table `aos.logger.MessageHeader` |
| 350 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x20); |
| 351 | // |
| 352 | // padding: |
| 353 | // +0x08 | 00 00 00 00 00 00 | uint8_t[6] | ...... | padding |
| 354 | buffer = Pad(buffer, 6); |
| 355 | // |
| 356 | // vtable (aos.logger.MessageHeader): |
| 357 | // +0x0E | 16 00 | uint16_t | 0x0016 (22) | size of this vtable |
| 358 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x16); |
| 359 | // +0x10 | 3C 00 | uint16_t | 0x003C (60) | size of referring table |
| 360 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x3c); |
| 361 | // +0x12 | 38 00 | VOffset16 | 0x0038 (56) | offset to field `channel_index` (id: 0) |
| 362 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x38); |
| 363 | // +0x14 | 2C 00 | VOffset16 | 0x002C (44) | offset to field `monotonic_sent_time` (id: 1) |
| 364 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x2c); |
| 365 | // +0x16 | 24 00 | VOffset16 | 0x0024 (36) | offset to field `realtime_sent_time` (id: 2) |
| 366 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x24); |
| 367 | // +0x18 | 34 00 | VOffset16 | 0x0034 (52) | offset to field `queue_index` (id: 3) |
| 368 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x34); |
| 369 | // +0x1A | 00 00 | VOffset16 | 0x0000 (0) | offset to field `data` (id: 4) <null> (Vector) |
| 370 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x00); |
| 371 | // +0x1C | 1C 00 | VOffset16 | 0x001C (28) | offset to field `monotonic_remote_time` (id: 5) |
| 372 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x1c); |
| 373 | // +0x1E | 14 00 | VOffset16 | 0x0014 (20) | offset to field `realtime_remote_time` (id: 6) |
| 374 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x14); |
| 375 | // +0x20 | 10 00 | VOffset16 | 0x0010 (16) | offset to field `remote_queue_index` (id: 7) |
| 376 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x10); |
| 377 | // +0x22 | 04 00 | VOffset16 | 0x0004 (4) | offset to field `monotonic_timestamp_time` (id: 8) |
| 378 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x04); |
| 379 | // |
| 380 | // root_table (aos.logger.MessageHeader): |
| 381 | // +0x24 | 16 00 00 00 | SOffset32 | 0x00000016 (22) Loc: +0x0E | offset to vtable |
| 382 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x16); |
| 383 | // +0x28 | F6 0B D8 11 A4 A8 B1 71 | int64_t | 0x71B1A8A411D80BF6 (8192514619791117302) | table field `monotonic_timestamp_time` (Long) |
| 384 | buffer = Push<int64_t>(buffer, |
| 385 | monotonic_timestamp_time.time_since_epoch().count()); |
| 386 | // +0x30 | 00 00 00 00 | uint8_t[4] | .... | padding |
| 387 | // TODO(austin): Can we re-arrange the order to ditch the padding? |
| 388 | // (Answer is yes, but what is the impact elsewhere? It will change the |
| 389 | // binary format) |
| 390 | buffer = Pad(buffer, 4); |
| 391 | // +0x34 | 75 00 00 00 | uint32_t | 0x00000075 (117) | table field `remote_queue_index` (UInt) |
| 392 | buffer = Push<uint32_t>(buffer, msg->remote_queue_index()); |
| 393 | // +0x38 | AA B0 43 0A 35 BE FA D2 | int64_t | 0xD2FABE350A43B0AA (-3244071446552268630) | table field `realtime_remote_time` (Long) |
| 394 | buffer = Push<int64_t>(buffer, msg->realtime_remote_time()); |
| 395 | // +0x40 | D5 40 30 F3 C1 A7 26 1D | int64_t | 0x1D26A7C1F33040D5 (2100550727665467605) | table field `monotonic_remote_time` (Long) |
| 396 | buffer = Push<int64_t>(buffer, msg->monotonic_remote_time()); |
| 397 | // +0x48 | 5B 25 32 A1 4A E8 46 CA | int64_t | 0xCA46E84AA132255B (-3871151422448720549) | table field `realtime_sent_time` (Long) |
| 398 | buffer = Push<int64_t>(buffer, msg->realtime_sent_time()); |
| 399 | // +0x50 | 49 7D 45 1F 8C 36 6B A3 | int64_t | 0xA36B368C1F457D49 (-6671178447571288759) | table field `monotonic_sent_time` (Long) |
| 400 | buffer = Push<int64_t>(buffer, msg->monotonic_sent_time()); |
| 401 | // +0x58 | 33 00 00 00 | uint32_t | 0x00000033 (51) | table field `queue_index` (UInt) |
| 402 | buffer = Push<uint32_t>(buffer, msg->queue_index()); |
| 403 | // +0x5C | 76 00 00 00 | uint32_t | 0x00000076 (118) | table field `channel_index` (UInt) |
| 404 | buffer = Push<uint32_t>(buffer, channel_index); |
| 405 | // clang-format on |
| 406 | |
| 407 | return message_size; |
| 408 | } |
| 409 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 410 | flatbuffers::Offset<MessageHeader> PackMessage( |
| 411 | flatbuffers::FlatBufferBuilder *fbb, const Context &context, |
| 412 | int channel_index, LogType log_type) { |
| 413 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset; |
| 414 | |
| 415 | switch (log_type) { |
| 416 | case LogType::kLogMessage: |
| 417 | case LogType::kLogMessageAndDeliveryTime: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 418 | case LogType::kLogRemoteMessage: |
Austin Schuh | fa30c35 | 2022-10-16 11:12:02 -0700 | [diff] [blame] | 419 | // Since the timestamps are 8 byte aligned, we are going to end up adding |
| 420 | // padding in the middle of the message to pad everything out to 8 byte |
| 421 | // alignment. That's rather wasteful. To make things efficient to mmap |
| 422 | // while reading uncompressed logs, we'd actually rather the message be |
| 423 | // aligned. So, force 8 byte alignment (enough to preserve alignment |
| 424 | // inside the nested message so that we can read it without moving it) |
| 425 | // here. |
| 426 | fbb->ForceVectorAlignment(context.size, sizeof(uint8_t), 8); |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 427 | data_offset = fbb->CreateVector( |
| 428 | static_cast<const uint8_t *>(context.data), context.size); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 429 | break; |
| 430 | |
| 431 | case LogType::kLogDeliveryTimeOnly: |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | MessageHeader::Builder message_header_builder(*fbb); |
| 436 | message_header_builder.add_channel_index(channel_index); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 437 | |
Austin Schuh | fa30c35 | 2022-10-16 11:12:02 -0700 | [diff] [blame] | 438 | // These are split out into very explicit serialization calls because the |
| 439 | // order here changes the order things are written out on the wire, and we |
| 440 | // want to control and understand it here. Changing the order can increase |
| 441 | // the amount of padding bytes in the middle. |
| 442 | // |
| 443 | // It is also easier to follow... And doesn't actually make things much bigger. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 444 | switch (log_type) { |
| 445 | case LogType::kLogRemoteMessage: |
| 446 | message_header_builder.add_queue_index(context.remote_queue_index); |
Austin Schuh | fa30c35 | 2022-10-16 11:12:02 -0700 | [diff] [blame] | 447 | message_header_builder.add_data(data_offset); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 448 | message_header_builder.add_monotonic_sent_time( |
| 449 | context.monotonic_remote_time.time_since_epoch().count()); |
| 450 | message_header_builder.add_realtime_sent_time( |
| 451 | context.realtime_remote_time.time_since_epoch().count()); |
| 452 | break; |
| 453 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 454 | case LogType::kLogDeliveryTimeOnly: |
| 455 | message_header_builder.add_queue_index(context.queue_index); |
| 456 | message_header_builder.add_monotonic_sent_time( |
| 457 | context.monotonic_event_time.time_since_epoch().count()); |
| 458 | message_header_builder.add_realtime_sent_time( |
| 459 | context.realtime_event_time.time_since_epoch().count()); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 460 | message_header_builder.add_monotonic_remote_time( |
| 461 | context.monotonic_remote_time.time_since_epoch().count()); |
| 462 | message_header_builder.add_realtime_remote_time( |
| 463 | context.realtime_remote_time.time_since_epoch().count()); |
| 464 | message_header_builder.add_remote_queue_index(context.remote_queue_index); |
| 465 | break; |
Austin Schuh | fa30c35 | 2022-10-16 11:12:02 -0700 | [diff] [blame] | 466 | |
| 467 | case LogType::kLogMessage: |
| 468 | message_header_builder.add_queue_index(context.queue_index); |
| 469 | message_header_builder.add_data(data_offset); |
| 470 | message_header_builder.add_monotonic_sent_time( |
| 471 | context.monotonic_event_time.time_since_epoch().count()); |
| 472 | message_header_builder.add_realtime_sent_time( |
| 473 | context.realtime_event_time.time_since_epoch().count()); |
| 474 | break; |
| 475 | |
| 476 | case LogType::kLogMessageAndDeliveryTime: |
| 477 | message_header_builder.add_queue_index(context.queue_index); |
| 478 | message_header_builder.add_remote_queue_index(context.remote_queue_index); |
| 479 | message_header_builder.add_monotonic_sent_time( |
| 480 | context.monotonic_event_time.time_since_epoch().count()); |
| 481 | message_header_builder.add_realtime_sent_time( |
| 482 | context.realtime_event_time.time_since_epoch().count()); |
| 483 | message_header_builder.add_monotonic_remote_time( |
| 484 | context.monotonic_remote_time.time_since_epoch().count()); |
| 485 | message_header_builder.add_realtime_remote_time( |
| 486 | context.realtime_remote_time.time_since_epoch().count()); |
| 487 | message_header_builder.add_data(data_offset); |
| 488 | break; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | return message_header_builder.Finish(); |
| 492 | } |
| 493 | |
Austin Schuh | fa30c35 | 2022-10-16 11:12:02 -0700 | [diff] [blame] | 494 | flatbuffers::uoffset_t PackMessageHeaderSize(LogType log_type) { |
| 495 | switch (log_type) { |
| 496 | case LogType::kLogMessage: |
| 497 | return |
| 498 | // Root table size + offset. |
| 499 | sizeof(flatbuffers::uoffset_t) * 2 + |
| 500 | // 6 padding bytes to pad the header out properly. |
| 501 | 6 + |
| 502 | // vtable header (size + size of table) |
| 503 | sizeof(flatbuffers::voffset_t) * 2 + |
| 504 | // offsets to all the fields. |
| 505 | sizeof(flatbuffers::voffset_t) * 5 + |
| 506 | // pointer to vtable |
| 507 | sizeof(flatbuffers::soffset_t) + |
| 508 | // pointer to data |
| 509 | sizeof(flatbuffers::uoffset_t) + |
| 510 | // realtime_sent_time, monotonic_sent_time |
| 511 | sizeof(int64_t) * 2 + |
| 512 | // queue_index, channel_index |
| 513 | sizeof(uint32_t) * 2; |
| 514 | |
| 515 | case LogType::kLogDeliveryTimeOnly: |
| 516 | return |
| 517 | // Root table size + offset. |
| 518 | sizeof(flatbuffers::uoffset_t) * 2 + |
| 519 | // 6 padding bytes to pad the header out properly. |
| 520 | 4 + |
| 521 | // vtable header (size + size of table) |
| 522 | sizeof(flatbuffers::voffset_t) * 2 + |
| 523 | // offsets to all the fields. |
| 524 | sizeof(flatbuffers::voffset_t) * 8 + |
| 525 | // pointer to vtable |
| 526 | sizeof(flatbuffers::soffset_t) + |
| 527 | // remote_queue_index |
| 528 | sizeof(uint32_t) + |
| 529 | // realtime_remote_time, monotonic_remote_time, realtime_sent_time, |
| 530 | // monotonic_sent_time |
| 531 | sizeof(int64_t) * 4 + |
| 532 | // queue_index, channel_index |
| 533 | sizeof(uint32_t) * 2; |
| 534 | |
| 535 | case LogType::kLogMessageAndDeliveryTime: |
| 536 | return |
| 537 | // Root table size + offset. |
| 538 | sizeof(flatbuffers::uoffset_t) * 2 + |
| 539 | // 4 padding bytes to pad the header out properly. |
| 540 | 4 + |
| 541 | // vtable header (size + size of table) |
| 542 | sizeof(flatbuffers::voffset_t) * 2 + |
| 543 | // offsets to all the fields. |
| 544 | sizeof(flatbuffers::voffset_t) * 8 + |
| 545 | // pointer to vtable |
| 546 | sizeof(flatbuffers::soffset_t) + |
| 547 | // pointer to data |
| 548 | sizeof(flatbuffers::uoffset_t) + |
| 549 | // realtime_remote_time, monotonic_remote_time, realtime_sent_time, |
| 550 | // monotonic_sent_time |
| 551 | sizeof(int64_t) * 4 + |
| 552 | // remote_queue_index, queue_index, channel_index |
| 553 | sizeof(uint32_t) * 3; |
| 554 | |
| 555 | case LogType::kLogRemoteMessage: |
| 556 | return |
| 557 | // Root table size + offset. |
| 558 | sizeof(flatbuffers::uoffset_t) * 2 + |
| 559 | // 6 padding bytes to pad the header out properly. |
| 560 | 6 + |
| 561 | // vtable header (size + size of table) |
| 562 | sizeof(flatbuffers::voffset_t) * 2 + |
| 563 | // offsets to all the fields. |
| 564 | sizeof(flatbuffers::voffset_t) * 5 + |
| 565 | // pointer to vtable |
| 566 | sizeof(flatbuffers::soffset_t) + |
| 567 | // realtime_sent_time, monotonic_sent_time |
| 568 | sizeof(int64_t) * 2 + |
| 569 | // pointer to data |
| 570 | sizeof(flatbuffers::uoffset_t) + |
| 571 | // queue_index, channel_index |
| 572 | sizeof(uint32_t) * 2; |
| 573 | } |
| 574 | LOG(FATAL); |
| 575 | } |
| 576 | |
| 577 | flatbuffers::uoffset_t PackMessageSize(LogType log_type, |
| 578 | const Context &context) { |
| 579 | static_assert(sizeof(flatbuffers::uoffset_t) == 4u, |
| 580 | "Update size logic please."); |
| 581 | const flatbuffers::uoffset_t aligned_data_length = |
| 582 | ((context.size + 7) & 0xfffffff8u); |
| 583 | switch (log_type) { |
| 584 | case LogType::kLogDeliveryTimeOnly: |
| 585 | return PackMessageHeaderSize(log_type); |
| 586 | |
| 587 | case LogType::kLogMessage: |
| 588 | case LogType::kLogMessageAndDeliveryTime: |
| 589 | case LogType::kLogRemoteMessage: |
| 590 | return PackMessageHeaderSize(log_type) + |
| 591 | // Vector... |
| 592 | sizeof(flatbuffers::uoffset_t) + aligned_data_length; |
| 593 | } |
| 594 | LOG(FATAL); |
| 595 | } |
| 596 | |
Austin Schuh | fa30c35 | 2022-10-16 11:12:02 -0700 | [diff] [blame] | 597 | size_t PackMessageInline(uint8_t *buffer, const Context &context, |
| 598 | int channel_index, LogType log_type) { |
| 599 | const flatbuffers::uoffset_t message_size = |
| 600 | PackMessageSize(log_type, context); |
| 601 | |
| 602 | buffer = Push<flatbuffers::uoffset_t>( |
| 603 | buffer, message_size - sizeof(flatbuffers::uoffset_t)); |
| 604 | |
| 605 | // Pack all the data in. This is brittle but easy to change. Use the |
| 606 | // InlinePackMessage.Equivilent unit test to verify everything matches. |
| 607 | switch (log_type) { |
| 608 | case LogType::kLogMessage: |
| 609 | // clang-format off |
| 610 | // header: |
| 611 | // +0x00 | 4C 00 00 00 | UOffset32 | 0x0000004C (76) Loc: +0x4C | size prefix |
| 612 | // +0x04 | 18 00 00 00 | UOffset32 | 0x00000018 (24) Loc: +0x1C | offset to root table `aos.logger.MessageHeader` |
| 613 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x18); |
| 614 | // |
| 615 | // padding: |
| 616 | // +0x08 | 00 00 00 00 00 00 | uint8_t[6] | ...... | padding |
| 617 | buffer = Pad(buffer, 6); |
| 618 | // |
| 619 | // vtable (aos.logger.MessageHeader): |
| 620 | // +0x0E | 0E 00 | uint16_t | 0x000E (14) | size of this vtable |
| 621 | buffer = Push<flatbuffers::voffset_t>(buffer, 0xe); |
| 622 | // +0x10 | 20 00 | uint16_t | 0x0020 (32) | size of referring table |
| 623 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x20); |
| 624 | // +0x12 | 1C 00 | VOffset16 | 0x001C (28) | offset to field `channel_index` (id: 0) |
| 625 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x1c); |
| 626 | // +0x14 | 0C 00 | VOffset16 | 0x000C (12) | offset to field `monotonic_sent_time` (id: 1) |
| 627 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x0c); |
| 628 | // +0x16 | 04 00 | VOffset16 | 0x0004 (4) | offset to field `realtime_sent_time` (id: 2) |
| 629 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x04); |
| 630 | // +0x18 | 18 00 | VOffset16 | 0x0018 (24) | offset to field `queue_index` (id: 3) |
| 631 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x18); |
| 632 | // +0x1A | 14 00 | VOffset16 | 0x0014 (20) | offset to field `data` (id: 4) |
| 633 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x14); |
| 634 | // |
| 635 | // root_table (aos.logger.MessageHeader): |
| 636 | // +0x1C | 0E 00 00 00 | SOffset32 | 0x0000000E (14) Loc: +0x0E | offset to vtable |
| 637 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x0e); |
| 638 | // +0x20 | B2 E4 EF 89 19 7D 7F 6F | int64_t | 0x6F7F7D1989EFE4B2 (8034277808894108850) | table field `realtime_sent_time` (Long) |
| 639 | buffer = Push<int64_t>(buffer, context.realtime_event_time.time_since_epoch().count()); |
| 640 | // +0x28 | 86 8D 92 65 FC 79 74 2B | int64_t | 0x2B7479FC65928D86 (3131261765872160134) | table field `monotonic_sent_time` (Long) |
| 641 | buffer = Push<int64_t>(buffer, context.monotonic_event_time.time_since_epoch().count()); |
| 642 | // +0x30 | 0C 00 00 00 | UOffset32 | 0x0000000C (12) Loc: +0x3C | offset to field `data` (vector) |
| 643 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x0c); |
| 644 | // +0x34 | 86 00 00 00 | uint32_t | 0x00000086 (134) | table field `queue_index` (UInt) |
| 645 | buffer = Push<uint32_t>(buffer, context.queue_index); |
| 646 | // +0x38 | 71 00 00 00 | uint32_t | 0x00000071 (113) | table field `channel_index` (UInt) |
| 647 | buffer = Push<uint32_t>(buffer, channel_index); |
| 648 | // |
| 649 | // vector (aos.logger.MessageHeader.data): |
| 650 | // +0x3C | 0E 00 00 00 | uint32_t | 0x0000000E (14) | length of vector (# items) |
| 651 | buffer = Push<flatbuffers::uoffset_t>(buffer, context.size); |
| 652 | // +0x40 | FF | uint8_t | 0xFF (255) | value[0] |
| 653 | // +0x41 | B8 | uint8_t | 0xB8 (184) | value[1] |
| 654 | // +0x42 | EE | uint8_t | 0xEE (238) | value[2] |
| 655 | // +0x43 | 00 | uint8_t | 0x00 (0) | value[3] |
| 656 | // +0x44 | 20 | uint8_t | 0x20 (32) | value[4] |
| 657 | // +0x45 | 4D | uint8_t | 0x4D (77) | value[5] |
| 658 | // +0x46 | FF | uint8_t | 0xFF (255) | value[6] |
| 659 | // +0x47 | 25 | uint8_t | 0x25 (37) | value[7] |
| 660 | // +0x48 | 3C | uint8_t | 0x3C (60) | value[8] |
| 661 | // +0x49 | 17 | uint8_t | 0x17 (23) | value[9] |
| 662 | // +0x4A | 65 | uint8_t | 0x65 (101) | value[10] |
| 663 | // +0x4B | 2F | uint8_t | 0x2F (47) | value[11] |
| 664 | // +0x4C | 63 | uint8_t | 0x63 (99) | value[12] |
| 665 | // +0x4D | 58 | uint8_t | 0x58 (88) | value[13] |
| 666 | buffer = PushBytes(buffer, context.data, context.size); |
| 667 | // |
| 668 | // padding: |
| 669 | // +0x4E | 00 00 | uint8_t[2] | .. | padding |
| 670 | buffer = Pad(buffer, ((context.size + 7) & 0xfffffff8u) - context.size); |
| 671 | // clang-format on |
| 672 | break; |
| 673 | |
| 674 | case LogType::kLogDeliveryTimeOnly: |
| 675 | // clang-format off |
| 676 | // header: |
| 677 | // +0x00 | 4C 00 00 00 | UOffset32 | 0x0000004C (76) Loc: +0x4C | size prefix |
| 678 | // +0x04 | 1C 00 00 00 | UOffset32 | 0x0000001C (28) Loc: +0x20 | offset to root table `aos.logger.MessageHeader` |
| 679 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x1c); |
| 680 | // |
| 681 | // padding: |
| 682 | // +0x08 | 00 00 00 00 | uint8_t[4] | .... | padding |
| 683 | buffer = Pad(buffer, 4); |
| 684 | // |
| 685 | // vtable (aos.logger.MessageHeader): |
| 686 | // +0x0C | 14 00 | uint16_t | 0x0014 (20) | size of this vtable |
| 687 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x14); |
| 688 | // +0x0E | 30 00 | uint16_t | 0x0030 (48) | size of referring table |
| 689 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x30); |
| 690 | // +0x10 | 2C 00 | VOffset16 | 0x002C (44) | offset to field `channel_index` (id: 0) |
| 691 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x2c); |
| 692 | // +0x12 | 20 00 | VOffset16 | 0x0020 (32) | offset to field `monotonic_sent_time` (id: 1) |
| 693 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x20); |
| 694 | // +0x14 | 18 00 | VOffset16 | 0x0018 (24) | offset to field `realtime_sent_time` (id: 2) |
| 695 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x18); |
| 696 | // +0x16 | 28 00 | VOffset16 | 0x0028 (40) | offset to field `queue_index` (id: 3) |
| 697 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x28); |
| 698 | // +0x18 | 00 00 | VOffset16 | 0x0000 (0) | offset to field `data` (id: 4) <null> (Vector) |
| 699 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x00); |
| 700 | // +0x1A | 10 00 | VOffset16 | 0x0010 (16) | offset to field `monotonic_remote_time` (id: 5) |
| 701 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x10); |
| 702 | // +0x1C | 08 00 | VOffset16 | 0x0008 (8) | offset to field `realtime_remote_time` (id: 6) |
| 703 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x08); |
| 704 | // +0x1E | 04 00 | VOffset16 | 0x0004 (4) | offset to field `remote_queue_index` (id: 7) |
| 705 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x04); |
| 706 | // |
| 707 | // root_table (aos.logger.MessageHeader): |
| 708 | // +0x20 | 14 00 00 00 | SOffset32 | 0x00000014 (20) Loc: +0x0C | offset to vtable |
| 709 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x14); |
| 710 | // +0x24 | 69 00 00 00 | uint32_t | 0x00000069 (105) | table field `remote_queue_index` (UInt) |
| 711 | buffer = Push<uint32_t>(buffer, context.remote_queue_index); |
| 712 | // +0x28 | C6 85 F1 AB 83 B5 CD EB | int64_t | 0xEBCDB583ABF185C6 (-1455307527440726586) | table field `realtime_remote_time` (Long) |
| 713 | buffer = Push<int64_t>(buffer, context.realtime_remote_time.time_since_epoch().count()); |
| 714 | // +0x30 | 47 24 D3 97 1E 42 2D 99 | int64_t | 0x992D421E97D32447 (-7409193112790948793) | table field `monotonic_remote_time` (Long) |
| 715 | buffer = Push<int64_t>(buffer, context.monotonic_remote_time.time_since_epoch().count()); |
| 716 | // +0x38 | C8 B9 A7 AB 79 F2 CD 60 | int64_t | 0x60CDF279ABA7B9C8 (6975498002251626952) | table field `realtime_sent_time` (Long) |
| 717 | buffer = Push<int64_t>(buffer, context.realtime_event_time.time_since_epoch().count()); |
| 718 | // +0x40 | EA 8F 2A 0F AF 01 7A AB | int64_t | 0xAB7A01AF0F2A8FEA (-6090553694679822358) | table field `monotonic_sent_time` (Long) |
| 719 | buffer = Push<int64_t>(buffer, context.monotonic_event_time.time_since_epoch().count()); |
| 720 | // +0x48 | F5 00 00 00 | uint32_t | 0x000000F5 (245) | table field `queue_index` (UInt) |
| 721 | buffer = Push<uint32_t>(buffer, context.queue_index); |
| 722 | // +0x4C | 88 00 00 00 | uint32_t | 0x00000088 (136) | table field `channel_index` (UInt) |
| 723 | buffer = Push<uint32_t>(buffer, channel_index); |
| 724 | |
| 725 | // clang-format on |
| 726 | break; |
| 727 | |
| 728 | case LogType::kLogMessageAndDeliveryTime: |
| 729 | // clang-format off |
| 730 | // header: |
| 731 | // +0x00 | 5C 00 00 00 | UOffset32 | 0x0000005C (92) Loc: +0x5C | size prefix |
| 732 | // +0x04 | 1C 00 00 00 | UOffset32 | 0x0000001C (28) Loc: +0x20 | offset to root table `aos.logger.MessageHeader` |
| 733 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x1c); |
| 734 | // |
| 735 | // padding: |
| 736 | // +0x08 | 00 00 00 00 | uint8_t[4] | .... | padding |
| 737 | buffer = Pad(buffer, 4); |
| 738 | // |
| 739 | // vtable (aos.logger.MessageHeader): |
| 740 | // +0x0C | 14 00 | uint16_t | 0x0014 (20) | size of this vtable |
| 741 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x14); |
| 742 | // +0x0E | 34 00 | uint16_t | 0x0034 (52) | size of referring table |
| 743 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x34); |
| 744 | // +0x10 | 30 00 | VOffset16 | 0x0030 (48) | offset to field `channel_index` (id: 0) |
| 745 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x30); |
| 746 | // +0x12 | 20 00 | VOffset16 | 0x0020 (32) | offset to field `monotonic_sent_time` (id: 1) |
| 747 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x20); |
| 748 | // +0x14 | 18 00 | VOffset16 | 0x0018 (24) | offset to field `realtime_sent_time` (id: 2) |
| 749 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x18); |
| 750 | // +0x16 | 2C 00 | VOffset16 | 0x002C (44) | offset to field `queue_index` (id: 3) |
| 751 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x2c); |
| 752 | // +0x18 | 04 00 | VOffset16 | 0x0004 (4) | offset to field `data` (id: 4) |
| 753 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x04); |
| 754 | // +0x1A | 10 00 | VOffset16 | 0x0010 (16) | offset to field `monotonic_remote_time` (id: 5) |
| 755 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x10); |
| 756 | // +0x1C | 08 00 | VOffset16 | 0x0008 (8) | offset to field `realtime_remote_time` (id: 6) |
| 757 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x08); |
| 758 | // +0x1E | 28 00 | VOffset16 | 0x0028 (40) | offset to field `remote_queue_index` (id: 7) |
| 759 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x28); |
| 760 | // |
| 761 | // root_table (aos.logger.MessageHeader): |
| 762 | // +0x20 | 14 00 00 00 | SOffset32 | 0x00000014 (20) Loc: +0x0C | offset to vtable |
| 763 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x14); |
| 764 | // +0x24 | 30 00 00 00 | UOffset32 | 0x00000030 (48) Loc: +0x54 | offset to field `data` (vector) |
| 765 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x30); |
| 766 | // +0x28 | C4 C8 87 BF 40 6C 1F 29 | int64_t | 0x291F6C40BF87C8C4 (2963206105180129476) | table field `realtime_remote_time` (Long) |
| 767 | buffer = Push<int64_t>(buffer, context.realtime_remote_time.time_since_epoch().count()); |
| 768 | // +0x30 | 0F 00 26 FD D2 6D C0 1F | int64_t | 0x1FC06DD2FD26000F (2287949363661897743) | table field `monotonic_remote_time` (Long) |
| 769 | buffer = Push<int64_t>(buffer, context.monotonic_remote_time.time_since_epoch().count()); |
| 770 | // +0x38 | 29 75 09 C0 73 73 BF 88 | int64_t | 0x88BF7373C0097529 (-8593022623019338455) | table field `realtime_sent_time` (Long) |
| 771 | buffer = Push<int64_t>(buffer, context.realtime_event_time.time_since_epoch().count()); |
| 772 | // +0x40 | 6D 8A AE 04 50 25 9C E9 | int64_t | 0xE99C255004AE8A6D (-1613373540899321235) | table field `monotonic_sent_time` (Long) |
| 773 | buffer = Push<int64_t>(buffer, context.monotonic_event_time.time_since_epoch().count()); |
| 774 | // +0x48 | 47 00 00 00 | uint32_t | 0x00000047 (71) | table field `remote_queue_index` (UInt) |
| 775 | buffer = Push<uint32_t>(buffer, context.remote_queue_index); |
| 776 | // +0x4C | 4C 00 00 00 | uint32_t | 0x0000004C (76) | table field `queue_index` (UInt) |
| 777 | buffer = Push<uint32_t>(buffer, context.queue_index); |
| 778 | // +0x50 | 72 00 00 00 | uint32_t | 0x00000072 (114) | table field `channel_index` (UInt) |
| 779 | buffer = Push<uint32_t>(buffer, channel_index); |
| 780 | // |
| 781 | // vector (aos.logger.MessageHeader.data): |
| 782 | // +0x54 | 07 00 00 00 | uint32_t | 0x00000007 (7) | length of vector (# items) |
| 783 | buffer = Push<flatbuffers::uoffset_t>(buffer, context.size); |
| 784 | // +0x58 | B1 | uint8_t | 0xB1 (177) | value[0] |
| 785 | // +0x59 | 4A | uint8_t | 0x4A (74) | value[1] |
| 786 | // +0x5A | 50 | uint8_t | 0x50 (80) | value[2] |
| 787 | // +0x5B | 24 | uint8_t | 0x24 (36) | value[3] |
| 788 | // +0x5C | AF | uint8_t | 0xAF (175) | value[4] |
| 789 | // +0x5D | C8 | uint8_t | 0xC8 (200) | value[5] |
| 790 | // +0x5E | D5 | uint8_t | 0xD5 (213) | value[6] |
| 791 | buffer = PushBytes(buffer, context.data, context.size); |
| 792 | // |
| 793 | // padding: |
| 794 | // +0x5F | 00 | uint8_t[1] | . | padding |
| 795 | buffer = Pad(buffer, ((context.size + 7) & 0xfffffff8u) - context.size); |
| 796 | // clang-format on |
| 797 | |
| 798 | break; |
| 799 | |
| 800 | case LogType::kLogRemoteMessage: |
| 801 | // This is the message we need to recreate. |
| 802 | // |
| 803 | // clang-format off |
| 804 | // header: |
| 805 | // +0x00 | 5C 00 00 00 | UOffset32 | 0x0000005C (92) Loc: +0x5C | size prefix |
| 806 | // +0x04 | 18 00 00 00 | UOffset32 | 0x00000018 (24) Loc: +0x1C | offset to root table `aos.logger.MessageHeader` |
| 807 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x18); |
| 808 | // |
| 809 | // padding: |
| 810 | // +0x08 | 00 00 00 00 00 00 | uint8_t[6] | ...... | padding |
| 811 | buffer = Pad(buffer, 6); |
| 812 | // |
| 813 | // vtable (aos.logger.MessageHeader): |
| 814 | // +0x0E | 0E 00 | uint16_t | 0x000E (14) | size of this vtable |
| 815 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x0e); |
| 816 | // +0x10 | 20 00 | uint16_t | 0x0020 (32) | size of referring table |
| 817 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x20); |
| 818 | // +0x12 | 1C 00 | VOffset16 | 0x001C (28) | offset to field `channel_index` (id: 0) |
| 819 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x1c); |
| 820 | // +0x14 | 0C 00 | VOffset16 | 0x000C (12) | offset to field `monotonic_sent_time` (id: 1) |
| 821 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x0c); |
| 822 | // +0x16 | 04 00 | VOffset16 | 0x0004 (4) | offset to field `realtime_sent_time` (id: 2) |
| 823 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x04); |
| 824 | // +0x18 | 18 00 | VOffset16 | 0x0018 (24) | offset to field `queue_index` (id: 3) |
| 825 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x18); |
| 826 | // +0x1A | 14 00 | VOffset16 | 0x0014 (20) | offset to field `data` (id: 4) |
| 827 | buffer = Push<flatbuffers::voffset_t>(buffer, 0x14); |
| 828 | // |
| 829 | // root_table (aos.logger.MessageHeader): |
| 830 | // +0x1C | 0E 00 00 00 | SOffset32 | 0x0000000E (14) Loc: +0x0E | offset to vtable |
| 831 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x0E); |
| 832 | // +0x20 | D8 96 32 1A A0 D3 23 BB | int64_t | 0xBB23D3A01A3296D8 (-4961889679844403496) | table field `realtime_sent_time` (Long) |
| 833 | buffer = Push<int64_t>(buffer, context.realtime_remote_time.time_since_epoch().count()); |
| 834 | // +0x28 | 2E 5D 23 B3 BE 84 CF C2 | int64_t | 0xC2CF84BEB3235D2E (-4409159555588334290) | table field `monotonic_sent_time` (Long) |
| 835 | buffer = Push<int64_t>(buffer, context.monotonic_remote_time.time_since_epoch().count()); |
| 836 | // +0x30 | 0C 00 00 00 | UOffset32 | 0x0000000C (12) Loc: +0x3C | offset to field `data` (vector) |
| 837 | buffer = Push<flatbuffers::uoffset_t>(buffer, 0x0C); |
| 838 | // +0x34 | 69 00 00 00 | uint32_t | 0x00000069 (105) | table field `queue_index` (UInt) |
| 839 | buffer = Push<uint32_t>(buffer, context.remote_queue_index); |
| 840 | // +0x38 | F3 00 00 00 | uint32_t | 0x000000F3 (243) | table field `channel_index` (UInt) |
| 841 | buffer = Push<uint32_t>(buffer, channel_index); |
| 842 | // |
| 843 | // vector (aos.logger.MessageHeader.data): |
| 844 | // +0x3C | 1A 00 00 00 | uint32_t | 0x0000001A (26) | length of vector (# items) |
| 845 | buffer = Push<flatbuffers::uoffset_t>(buffer, context.size); |
| 846 | // +0x40 | 38 | uint8_t | 0x38 (56) | value[0] |
| 847 | // +0x41 | 1A | uint8_t | 0x1A (26) | value[1] |
| 848 | // ... |
| 849 | // +0x58 | 90 | uint8_t | 0x90 (144) | value[24] |
| 850 | // +0x59 | 92 | uint8_t | 0x92 (146) | value[25] |
| 851 | buffer = PushBytes(buffer, context.data, context.size); |
| 852 | // |
| 853 | // padding: |
| 854 | // +0x5A | 00 00 00 00 00 00 | uint8_t[6] | ...... | padding |
| 855 | buffer = Pad(buffer, ((context.size + 7) & 0xfffffff8u) - context.size); |
| 856 | // clang-format on |
| 857 | } |
| 858 | |
| 859 | return message_size; |
| 860 | } |
| 861 | |
Austin Schuh | cd36842 | 2021-11-22 21:23:29 -0800 | [diff] [blame] | 862 | SpanReader::SpanReader(std::string_view filename, bool quiet) |
| 863 | : filename_(filename) { |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 864 | decoder_ = std::make_unique<DummyDecoder>(filename); |
| 865 | |
| 866 | static constexpr std::string_view kXz = ".xz"; |
James Kuszmaul | dd0a504 | 2021-10-28 23:38:04 -0700 | [diff] [blame] | 867 | static constexpr std::string_view kSnappy = SnappyDecoder::kExtension; |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 868 | if (filename.substr(filename.size() - kXz.size()) == kXz) { |
| 869 | #if ENABLE_LZMA |
Austin Schuh | cd36842 | 2021-11-22 21:23:29 -0800 | [diff] [blame] | 870 | decoder_ = |
| 871 | std::make_unique<ThreadedLzmaDecoder>(std::move(decoder_), quiet); |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 872 | #else |
Austin Schuh | cd36842 | 2021-11-22 21:23:29 -0800 | [diff] [blame] | 873 | (void)quiet; |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 874 | LOG(FATAL) << "Reading xz-compressed files not supported on this platform"; |
| 875 | #endif |
James Kuszmaul | dd0a504 | 2021-10-28 23:38:04 -0700 | [diff] [blame] | 876 | } else if (filename.substr(filename.size() - kSnappy.size()) == kSnappy) { |
| 877 | decoder_ = std::make_unique<SnappyDecoder>(std::move(decoder_)); |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 878 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Austin Schuh | cf5f644 | 2021-07-06 10:43:28 -0700 | [diff] [blame] | 881 | absl::Span<const uint8_t> SpanReader::PeekMessage() { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 882 | // Make sure we have enough for the size. |
| 883 | if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) { |
| 884 | if (!ReadBlock()) { |
| 885 | return absl::Span<const uint8_t>(); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | // Now make sure we have enough for the message. |
| 890 | const size_t data_size = |
| 891 | flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) + |
| 892 | sizeof(flatbuffers::uoffset_t); |
Austin Schuh | e4fca83 | 2020-03-07 16:58:53 -0800 | [diff] [blame] | 893 | if (data_size == sizeof(flatbuffers::uoffset_t)) { |
| 894 | LOG(ERROR) << "Size of data is zero. Log file end is corrupted, skipping."; |
| 895 | LOG(ERROR) << " Rest of log file is " |
| 896 | << absl::BytesToHexString(std::string_view( |
| 897 | reinterpret_cast<const char *>(data_.data() + |
| 898 | consumed_data_), |
| 899 | data_.size() - consumed_data_)); |
| 900 | return absl::Span<const uint8_t>(); |
| 901 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 902 | while (data_.size() < consumed_data_ + data_size) { |
| 903 | if (!ReadBlock()) { |
| 904 | return absl::Span<const uint8_t>(); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | // And return it, consuming the data. |
| 909 | const uint8_t *data_ptr = data_.data() + consumed_data_; |
| 910 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 911 | return absl::Span<const uint8_t>(data_ptr, data_size); |
| 912 | } |
| 913 | |
Austin Schuh | cf5f644 | 2021-07-06 10:43:28 -0700 | [diff] [blame] | 914 | void SpanReader::ConsumeMessage() { |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 915 | size_t consumed_size = |
Austin Schuh | cf5f644 | 2021-07-06 10:43:28 -0700 | [diff] [blame] | 916 | flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) + |
| 917 | sizeof(flatbuffers::uoffset_t); |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 918 | consumed_data_ += consumed_size; |
| 919 | total_consumed_ += consumed_size; |
Austin Schuh | cf5f644 | 2021-07-06 10:43:28 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | absl::Span<const uint8_t> SpanReader::ReadMessage() { |
| 923 | absl::Span<const uint8_t> result = PeekMessage(); |
| 924 | if (result != absl::Span<const uint8_t>()) { |
| 925 | ConsumeMessage(); |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 926 | } else { |
| 927 | is_finished_ = true; |
Austin Schuh | cf5f644 | 2021-07-06 10:43:28 -0700 | [diff] [blame] | 928 | } |
| 929 | return result; |
| 930 | } |
| 931 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 932 | bool SpanReader::ReadBlock() { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 933 | // This is the amount of data we grab at a time. Doing larger chunks minimizes |
| 934 | // syscalls and helps decompressors batch things more efficiently. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 935 | constexpr size_t kReadSize = 256 * 1024; |
| 936 | |
| 937 | // Strip off any unused data at the front. |
| 938 | if (consumed_data_ != 0) { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 939 | data_.erase_front(consumed_data_); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 940 | consumed_data_ = 0; |
| 941 | } |
| 942 | |
| 943 | const size_t starting_size = data_.size(); |
| 944 | |
| 945 | // This should automatically grow the backing store. It won't shrink if we |
| 946 | // get a small chunk later. This reduces allocations when we want to append |
| 947 | // more data. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 948 | data_.resize(starting_size + kReadSize); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 949 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 950 | const size_t count = |
| 951 | decoder_->Read(data_.begin() + starting_size, data_.end()); |
| 952 | data_.resize(starting_size + count); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 953 | if (count == 0) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 954 | return false; |
| 955 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 956 | |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 957 | total_read_ += count; |
| 958 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 959 | return true; |
| 960 | } |
| 961 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 962 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader( |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 963 | SpanReader *span_reader) { |
| 964 | absl::Span<const uint8_t> config_data = span_reader->ReadMessage(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 965 | |
| 966 | // Make sure something was read. |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 967 | if (config_data == absl::Span<const uint8_t>()) { |
| 968 | return std::nullopt; |
| 969 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 970 | |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 971 | // And copy the config so we have it forever, removing the size prefix. |
Austin Schuh | b929c4e | 2021-07-12 15:32:53 -0700 | [diff] [blame] | 972 | SizePrefixedFlatbufferVector<LogFileHeader> result(config_data); |
Austin Schuh | e09beb1 | 2020-12-11 20:04:27 -0800 | [diff] [blame] | 973 | if (!result.Verify()) { |
| 974 | return std::nullopt; |
| 975 | } |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 976 | |
| 977 | if (FLAGS_workaround_double_headers) { |
| 978 | while (true) { |
| 979 | absl::Span<const uint8_t> maybe_header_data = span_reader->PeekMessage(); |
| 980 | if (maybe_header_data == absl::Span<const uint8_t>()) { |
| 981 | break; |
| 982 | } |
| 983 | |
| 984 | aos::SizePrefixedFlatbufferSpan<aos::logger::LogFileHeader> maybe_header( |
| 985 | maybe_header_data); |
| 986 | if (maybe_header.Verify()) { |
| 987 | LOG(WARNING) << "Found duplicate LogFileHeader in " |
| 988 | << span_reader->filename(); |
| 989 | ResizeableBuffer header_data_copy; |
| 990 | header_data_copy.resize(maybe_header_data.size()); |
| 991 | memcpy(header_data_copy.data(), maybe_header_data.begin(), |
| 992 | header_data_copy.size()); |
| 993 | result = SizePrefixedFlatbufferVector<LogFileHeader>( |
| 994 | std::move(header_data_copy)); |
| 995 | |
| 996 | span_reader->ConsumeMessage(); |
| 997 | } else { |
| 998 | break; |
| 999 | } |
| 1000 | } |
| 1001 | } |
Austin Schuh | e09beb1 | 2020-12-11 20:04:27 -0800 | [diff] [blame] | 1002 | return result; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 1005 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader( |
| 1006 | std::string_view filename) { |
| 1007 | SpanReader span_reader(filename); |
| 1008 | return ReadHeader(&span_reader); |
| 1009 | } |
| 1010 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 1011 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage( |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1012 | std::string_view filename, size_t n) { |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 1013 | SpanReader span_reader(filename); |
| 1014 | absl::Span<const uint8_t> data_span = span_reader.ReadMessage(); |
| 1015 | for (size_t i = 0; i < n + 1; ++i) { |
| 1016 | data_span = span_reader.ReadMessage(); |
| 1017 | |
| 1018 | // Make sure something was read. |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1019 | if (data_span == absl::Span<const uint8_t>()) { |
| 1020 | return std::nullopt; |
| 1021 | } |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 1024 | // And copy the config so we have it forever, removing the size prefix. |
Austin Schuh | b929c4e | 2021-07-12 15:32:53 -0700 | [diff] [blame] | 1025 | SizePrefixedFlatbufferVector<MessageHeader> result(data_span); |
Austin Schuh | e09beb1 | 2020-12-11 20:04:27 -0800 | [diff] [blame] | 1026 | if (!result.Verify()) { |
| 1027 | return std::nullopt; |
| 1028 | } |
| 1029 | return result; |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1032 | MessageReader::MessageReader(std::string_view filename) |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 1033 | : span_reader_(filename), |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 1034 | raw_log_file_header_( |
| 1035 | SizePrefixedFlatbufferVector<LogFileHeader>::Empty()) { |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 1036 | set_crash_on_corrupt_message_flag(FLAGS_crash_on_corrupt_message); |
| 1037 | set_ignore_corrupt_messages_flag(FLAGS_ignore_corrupt_messages); |
| 1038 | |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 1039 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> |
| 1040 | raw_log_file_header = ReadHeader(&span_reader_); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1041 | |
| 1042 | // Make sure something was read. |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 1043 | CHECK(raw_log_file_header) << ": Failed to read header from: " << filename; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1044 | |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 1045 | raw_log_file_header_ = std::move(*raw_log_file_header); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1046 | |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 1047 | CHECK(raw_log_file_header_.Verify()) << "Log file header is corrupted"; |
| 1048 | |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 1049 | total_verified_before_ = span_reader_.TotalConsumed(); |
| 1050 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1051 | max_out_of_order_duration_ = |
Austin Schuh | a040c3f | 2021-02-13 16:09:07 -0800 | [diff] [blame] | 1052 | FLAGS_max_out_of_order > 0 |
| 1053 | ? chrono::duration_cast<chrono::nanoseconds>( |
| 1054 | chrono::duration<double>(FLAGS_max_out_of_order)) |
| 1055 | : chrono::nanoseconds(log_file_header()->max_out_of_order_duration()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1056 | |
| 1057 | VLOG(1) << "Opened " << filename << " as node " |
| 1058 | << FlatbufferToJson(log_file_header()->node()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1061 | std::shared_ptr<UnpackedMessageHeader> MessageReader::ReadMessage() { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1062 | absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage(); |
| 1063 | if (msg_data == absl::Span<const uint8_t>()) { |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 1064 | if (is_corrupted()) { |
| 1065 | LOG(ERROR) << "Total corrupted volumes: before = " |
| 1066 | << total_verified_before_ |
| 1067 | << " | corrupted = " << total_corrupted_ |
| 1068 | << " | during = " << total_verified_during_ |
| 1069 | << " | after = " << total_verified_after_ << std::endl; |
| 1070 | } |
| 1071 | |
| 1072 | if (span_reader_.IsIncomplete()) { |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 1073 | LOG(ERROR) << "Unable to access some messages in " << filename() << " : " |
| 1074 | << span_reader_.TotalRead() << " bytes read, " |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 1075 | << span_reader_.TotalConsumed() << " bytes usable." |
| 1076 | << std::endl; |
| 1077 | } |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1078 | return nullptr; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1081 | SizePrefixedFlatbufferSpan<MessageHeader> msg(msg_data); |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 1082 | |
| 1083 | if (crash_on_corrupt_message_flag_) { |
| 1084 | CHECK(msg.Verify()) << "Corrupted message at offset " |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 1085 | << total_verified_before_ << " found within " |
| 1086 | << filename() |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 1087 | << "; set --nocrash_on_corrupt_message to see summary;" |
| 1088 | << " also set --ignore_corrupt_messages to process" |
| 1089 | << " anyway"; |
| 1090 | |
| 1091 | } else if (!msg.Verify()) { |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 1092 | LOG(ERROR) << "Corrupted message at offset " << total_verified_before_ |
Brian Smartt | ea913d4 | 2021-12-10 15:02:38 -0800 | [diff] [blame] | 1093 | << " from " << filename() << std::endl; |
| 1094 | |
| 1095 | total_corrupted_ += msg_data.size(); |
| 1096 | |
| 1097 | while (true) { |
| 1098 | absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage(); |
| 1099 | |
| 1100 | if (msg_data == absl::Span<const uint8_t>()) { |
| 1101 | if (!ignore_corrupt_messages_flag_) { |
| 1102 | LOG(ERROR) << "Total corrupted volumes: before = " |
| 1103 | << total_verified_before_ |
| 1104 | << " | corrupted = " << total_corrupted_ |
| 1105 | << " | during = " << total_verified_during_ |
| 1106 | << " | after = " << total_verified_after_ << std::endl; |
| 1107 | |
| 1108 | if (span_reader_.IsIncomplete()) { |
| 1109 | LOG(ERROR) << "Unable to access some messages in " << filename() |
| 1110 | << " : " << span_reader_.TotalRead() << " bytes read, " |
| 1111 | << span_reader_.TotalConsumed() << " bytes usable." |
| 1112 | << std::endl; |
| 1113 | } |
| 1114 | return nullptr; |
| 1115 | } |
| 1116 | break; |
| 1117 | } |
| 1118 | |
| 1119 | SizePrefixedFlatbufferSpan<MessageHeader> next_msg(msg_data); |
| 1120 | |
| 1121 | if (!next_msg.Verify()) { |
| 1122 | total_corrupted_ += msg_data.size(); |
| 1123 | total_verified_during_ += total_verified_after_; |
| 1124 | total_verified_after_ = 0; |
| 1125 | |
| 1126 | } else { |
| 1127 | total_verified_after_ += msg_data.size(); |
| 1128 | if (ignore_corrupt_messages_flag_) { |
| 1129 | msg = next_msg; |
| 1130 | break; |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | if (is_corrupted()) { |
| 1137 | total_verified_after_ += msg_data.size(); |
| 1138 | } else { |
| 1139 | total_verified_before_ += msg_data.size(); |
| 1140 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1141 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1142 | auto result = UnpackedMessageHeader::MakeMessage(msg.message()); |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 1143 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1144 | const monotonic_clock::time_point timestamp = result->monotonic_sent_time; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1145 | |
| 1146 | newest_timestamp_ = std::max(newest_timestamp_, timestamp); |
Austin Schuh | d187329 | 2021-11-18 15:35:30 -0800 | [diff] [blame] | 1147 | |
| 1148 | if (VLOG_IS_ON(3)) { |
| 1149 | VLOG(3) << "Read from " << filename() << " data " << FlatbufferToJson(msg); |
| 1150 | } else if (VLOG_IS_ON(2)) { |
| 1151 | SizePrefixedFlatbufferVector<MessageHeader> msg_copy = msg; |
| 1152 | msg_copy.mutable_message()->clear_data(); |
| 1153 | VLOG(2) << "Read from " << filename() << " data " |
| 1154 | << FlatbufferToJson(msg_copy); |
| 1155 | } |
| 1156 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1157 | return result; |
| 1158 | } |
| 1159 | |
| 1160 | std::shared_ptr<UnpackedMessageHeader> UnpackedMessageHeader::MakeMessage( |
| 1161 | const MessageHeader &message) { |
| 1162 | const size_t data_size = message.has_data() ? message.data()->size() : 0; |
| 1163 | |
| 1164 | UnpackedMessageHeader *const unpacked_message = |
| 1165 | reinterpret_cast<UnpackedMessageHeader *>( |
| 1166 | malloc(sizeof(UnpackedMessageHeader) + data_size + |
| 1167 | kChannelDataAlignment - 1)); |
| 1168 | |
| 1169 | CHECK(message.has_channel_index()); |
| 1170 | CHECK(message.has_monotonic_sent_time()); |
| 1171 | |
| 1172 | absl::Span<uint8_t> span; |
| 1173 | if (data_size > 0) { |
| 1174 | span = |
| 1175 | absl::Span<uint8_t>(reinterpret_cast<uint8_t *>(RoundChannelData( |
| 1176 | &unpacked_message->actual_data[0], data_size)), |
| 1177 | data_size); |
| 1178 | } |
| 1179 | |
Austin Schuh | 826e6ce | 2021-11-18 20:33:10 -0800 | [diff] [blame] | 1180 | std::optional<aos::monotonic_clock::time_point> monotonic_remote_time; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1181 | if (message.has_monotonic_remote_time()) { |
Austin Schuh | 826e6ce | 2021-11-18 20:33:10 -0800 | [diff] [blame] | 1182 | monotonic_remote_time = aos::monotonic_clock::time_point( |
| 1183 | std::chrono::nanoseconds(message.monotonic_remote_time())); |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1184 | } |
| 1185 | std::optional<realtime_clock::time_point> realtime_remote_time; |
| 1186 | if (message.has_realtime_remote_time()) { |
| 1187 | realtime_remote_time = realtime_clock::time_point( |
| 1188 | chrono::nanoseconds(message.realtime_remote_time())); |
| 1189 | } |
| 1190 | |
| 1191 | std::optional<uint32_t> remote_queue_index; |
| 1192 | if (message.has_remote_queue_index()) { |
| 1193 | remote_queue_index = message.remote_queue_index(); |
| 1194 | } |
| 1195 | |
| 1196 | new (unpacked_message) UnpackedMessageHeader{ |
| 1197 | .channel_index = message.channel_index(), |
| 1198 | .monotonic_sent_time = monotonic_clock::time_point( |
| 1199 | chrono::nanoseconds(message.monotonic_sent_time())), |
| 1200 | .realtime_sent_time = realtime_clock::time_point( |
| 1201 | chrono::nanoseconds(message.realtime_sent_time())), |
| 1202 | .queue_index = message.queue_index(), |
| 1203 | .monotonic_remote_time = monotonic_remote_time, |
| 1204 | .realtime_remote_time = realtime_remote_time, |
| 1205 | .remote_queue_index = remote_queue_index, |
| 1206 | .monotonic_timestamp_time = monotonic_clock::time_point( |
| 1207 | std::chrono::nanoseconds(message.monotonic_timestamp_time())), |
| 1208 | .has_monotonic_timestamp_time = message.has_monotonic_timestamp_time(), |
| 1209 | .span = span}; |
| 1210 | |
| 1211 | if (data_size > 0) { |
| 1212 | memcpy(span.data(), message.data()->data(), data_size); |
| 1213 | } |
| 1214 | |
| 1215 | return std::shared_ptr<UnpackedMessageHeader>(unpacked_message, |
| 1216 | &DestroyAndFree); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 1219 | PartsMessageReader::PartsMessageReader(LogParts log_parts) |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1220 | : parts_(std::move(log_parts)), message_reader_(parts_.parts[0]) { |
Brian Silverman | fee1697 | 2021-09-14 12:06:38 -0700 | [diff] [blame] | 1221 | if (parts_.parts.size() >= 2) { |
| 1222 | next_message_reader_.emplace(parts_.parts[1]); |
| 1223 | } |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1224 | ComputeBootCounts(); |
| 1225 | } |
| 1226 | |
| 1227 | void PartsMessageReader::ComputeBootCounts() { |
| 1228 | boot_counts_.assign(configuration::NodesCount(parts_.config.get()), |
| 1229 | std::nullopt); |
| 1230 | |
| 1231 | // We have 3 vintages of log files with different amounts of information. |
| 1232 | if (log_file_header()->has_boot_uuids()) { |
| 1233 | // The new hotness with the boots explicitly listed out. We can use the log |
| 1234 | // file header to compute the boot count of all relevant nodes. |
| 1235 | CHECK_EQ(log_file_header()->boot_uuids()->size(), boot_counts_.size()); |
| 1236 | size_t node_index = 0; |
| 1237 | for (const flatbuffers::String *boot_uuid : |
| 1238 | *log_file_header()->boot_uuids()) { |
| 1239 | CHECK(parts_.boots); |
| 1240 | if (boot_uuid->size() != 0) { |
| 1241 | auto it = parts_.boots->boot_count_map.find(boot_uuid->str()); |
| 1242 | if (it != parts_.boots->boot_count_map.end()) { |
| 1243 | boot_counts_[node_index] = it->second; |
| 1244 | } |
| 1245 | } else if (parts().boots->boots[node_index].size() == 1u) { |
| 1246 | boot_counts_[node_index] = 0; |
| 1247 | } |
| 1248 | ++node_index; |
| 1249 | } |
| 1250 | } else { |
| 1251 | // Older multi-node logs which are guarenteed to have UUIDs logged, or |
| 1252 | // single node log files with boot UUIDs in the header. We only know how to |
| 1253 | // order certain boots in certain circumstances. |
| 1254 | if (configuration::MultiNode(parts_.config.get()) || parts_.boots) { |
| 1255 | for (size_t node_index = 0; node_index < boot_counts_.size(); |
| 1256 | ++node_index) { |
| 1257 | CHECK(parts_.boots); |
| 1258 | if (parts().boots->boots[node_index].size() == 1u) { |
| 1259 | boot_counts_[node_index] = 0; |
| 1260 | } |
| 1261 | } |
| 1262 | } else { |
| 1263 | // Really old single node logs without any UUIDs. They can't reboot. |
| 1264 | CHECK_EQ(boot_counts_.size(), 1u); |
| 1265 | boot_counts_[0] = 0u; |
| 1266 | } |
| 1267 | } |
| 1268 | } |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 1269 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1270 | std::shared_ptr<UnpackedMessageHeader> PartsMessageReader::ReadMessage() { |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 1271 | while (!done_) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1272 | std::shared_ptr<UnpackedMessageHeader> message = |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 1273 | message_reader_.ReadMessage(); |
| 1274 | if (message) { |
| 1275 | newest_timestamp_ = message_reader_.newest_timestamp(); |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1276 | const monotonic_clock::time_point monotonic_sent_time = |
| 1277 | message->monotonic_sent_time; |
| 1278 | |
| 1279 | // TODO(austin): Does this work with startup? Might need to use the |
| 1280 | // start time. |
| 1281 | // TODO(austin): Does this work with startup when we don't know the |
| 1282 | // remote start time too? Look at one of those logs to compare. |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1283 | if (monotonic_sent_time > |
| 1284 | parts_.monotonic_start_time + max_out_of_order_duration()) { |
| 1285 | after_start_ = true; |
| 1286 | } |
| 1287 | if (after_start_) { |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 1288 | CHECK_GE(monotonic_sent_time, |
| 1289 | newest_timestamp_ - max_out_of_order_duration()) |
Austin Schuh | a040c3f | 2021-02-13 16:09:07 -0800 | [diff] [blame] | 1290 | << ": Max out of order of " << max_out_of_order_duration().count() |
| 1291 | << "ns exceeded. " << parts_ << ", start time is " |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1292 | << parts_.monotonic_start_time << " currently reading " |
| 1293 | << filename(); |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 1294 | } |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 1295 | return message; |
| 1296 | } |
| 1297 | NextLog(); |
| 1298 | } |
Austin Schuh | 32f6849 | 2020-11-08 21:45:51 -0800 | [diff] [blame] | 1299 | newest_timestamp_ = monotonic_clock::max_time; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1300 | return nullptr; |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | void PartsMessageReader::NextLog() { |
| 1304 | if (next_part_index_ == parts_.parts.size()) { |
Brian Silverman | fee1697 | 2021-09-14 12:06:38 -0700 | [diff] [blame] | 1305 | CHECK(!next_message_reader_); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 1306 | done_ = true; |
| 1307 | return; |
| 1308 | } |
Brian Silverman | fee1697 | 2021-09-14 12:06:38 -0700 | [diff] [blame] | 1309 | CHECK(next_message_reader_); |
| 1310 | message_reader_ = std::move(*next_message_reader_); |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1311 | ComputeBootCounts(); |
Brian Silverman | fee1697 | 2021-09-14 12:06:38 -0700 | [diff] [blame] | 1312 | if (next_part_index_ + 1 < parts_.parts.size()) { |
| 1313 | next_message_reader_.emplace(parts_.parts[next_part_index_ + 1]); |
| 1314 | } else { |
| 1315 | next_message_reader_.reset(); |
| 1316 | } |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 1317 | ++next_part_index_; |
| 1318 | } |
| 1319 | |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 1320 | bool Message::operator<(const Message &m2) const { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1321 | CHECK_EQ(this->timestamp.boot, m2.timestamp.boot); |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1322 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1323 | if (this->timestamp.time < m2.timestamp.time) { |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 1324 | return true; |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1325 | } else if (this->timestamp.time > m2.timestamp.time) { |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 1326 | return false; |
| 1327 | } |
| 1328 | |
| 1329 | if (this->channel_index < m2.channel_index) { |
| 1330 | return true; |
| 1331 | } else if (this->channel_index > m2.channel_index) { |
| 1332 | return false; |
| 1333 | } |
| 1334 | |
| 1335 | return this->queue_index < m2.queue_index; |
| 1336 | } |
| 1337 | |
| 1338 | bool Message::operator>=(const Message &m2) const { return !(*this < m2); } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 1339 | bool Message::operator==(const Message &m2) const { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1340 | CHECK_EQ(this->timestamp.boot, m2.timestamp.boot); |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1341 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1342 | return timestamp.time == m2.timestamp.time && |
| 1343 | channel_index == m2.channel_index && queue_index == m2.queue_index; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 1344 | } |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 1345 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1346 | std::ostream &operator<<(std::ostream &os, const UnpackedMessageHeader &m) { |
| 1347 | os << "{.channel_index=" << m.channel_index |
| 1348 | << ", .monotonic_sent_time=" << m.monotonic_sent_time |
| 1349 | << ", .realtime_sent_time=" << m.realtime_sent_time |
| 1350 | << ", .queue_index=" << m.queue_index; |
| 1351 | if (m.monotonic_remote_time) { |
Austin Schuh | 826e6ce | 2021-11-18 20:33:10 -0800 | [diff] [blame] | 1352 | os << ", .monotonic_remote_time=" << *m.monotonic_remote_time; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1353 | } |
| 1354 | os << ", .realtime_remote_time="; |
| 1355 | PrintOptionalOrNull(&os, m.realtime_remote_time); |
| 1356 | os << ", .remote_queue_index="; |
| 1357 | PrintOptionalOrNull(&os, m.remote_queue_index); |
| 1358 | if (m.has_monotonic_timestamp_time) { |
| 1359 | os << ", .monotonic_timestamp_time=" << m.monotonic_timestamp_time; |
| 1360 | } |
Austin Schuh | 22cf786 | 2022-09-19 19:09:42 -0700 | [diff] [blame] | 1361 | os << "}"; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1362 | return os; |
| 1363 | } |
| 1364 | |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 1365 | std::ostream &operator<<(std::ostream &os, const Message &m) { |
| 1366 | os << "{.channel_index=" << m.channel_index |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1367 | << ", .queue_index=" << m.queue_index << ", .timestamp=" << m.timestamp; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1368 | if (m.data != nullptr) { |
Austin Schuh | 826e6ce | 2021-11-18 20:33:10 -0800 | [diff] [blame] | 1369 | if (m.data->remote_queue_index.has_value()) { |
| 1370 | os << ", .remote_queue_index=" << *m.data->remote_queue_index; |
| 1371 | } |
| 1372 | if (m.data->monotonic_remote_time.has_value()) { |
| 1373 | os << ", .monotonic_remote_time=" << *m.data->monotonic_remote_time; |
| 1374 | } |
Austin Schuh | fb1b329 | 2021-11-16 21:20:15 -0800 | [diff] [blame] | 1375 | os << ", .data=" << m.data; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1376 | } |
| 1377 | os << "}"; |
| 1378 | return os; |
| 1379 | } |
| 1380 | |
| 1381 | std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m) { |
| 1382 | os << "{.channel_index=" << m.channel_index |
| 1383 | << ", .queue_index=" << m.queue_index |
| 1384 | << ", .monotonic_event_time=" << m.monotonic_event_time |
| 1385 | << ", .realtime_event_time=" << m.realtime_event_time; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1386 | if (m.remote_queue_index != BootQueueIndex::Invalid()) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1387 | os << ", .remote_queue_index=" << m.remote_queue_index; |
| 1388 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1389 | if (m.monotonic_remote_time != BootTimestamp::min_time()) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1390 | os << ", .monotonic_remote_time=" << m.monotonic_remote_time; |
| 1391 | } |
| 1392 | if (m.realtime_remote_time != realtime_clock::min_time) { |
| 1393 | os << ", .realtime_remote_time=" << m.realtime_remote_time; |
| 1394 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1395 | if (m.monotonic_timestamp_time != BootTimestamp::min_time()) { |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 1396 | os << ", .monotonic_timestamp_time=" << m.monotonic_timestamp_time; |
| 1397 | } |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1398 | if (m.data != nullptr) { |
| 1399 | os << ", .data=" << *m.data; |
Austin Schuh | 22cf786 | 2022-09-19 19:09:42 -0700 | [diff] [blame] | 1400 | } else { |
| 1401 | os << ", .data=nullptr"; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1402 | } |
| 1403 | os << "}"; |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 1404 | return os; |
| 1405 | } |
| 1406 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 1407 | LogPartsSorter::LogPartsSorter(LogParts log_parts) |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1408 | : parts_message_reader_(log_parts), |
| 1409 | source_node_index_(configuration::SourceNodeIndex(parts().config.get())) { |
| 1410 | } |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 1411 | |
| 1412 | Message *LogPartsSorter::Front() { |
| 1413 | // Queue up data until enough data has been queued that the front message is |
| 1414 | // sorted enough to be safe to pop. This may do nothing, so we should make |
| 1415 | // sure the nothing path is checked quickly. |
| 1416 | if (sorted_until() != monotonic_clock::max_time) { |
| 1417 | while (true) { |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1418 | if (!messages_.empty() && |
| 1419 | messages_.begin()->timestamp.time < sorted_until() && |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 1420 | sorted_until() >= monotonic_start_time()) { |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 1421 | break; |
| 1422 | } |
| 1423 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1424 | std::shared_ptr<UnpackedMessageHeader> m = |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 1425 | parts_message_reader_.ReadMessage(); |
| 1426 | // No data left, sorted forever, work through what is left. |
| 1427 | if (!m) { |
| 1428 | sorted_until_ = monotonic_clock::max_time; |
| 1429 | break; |
| 1430 | } |
| 1431 | |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1432 | size_t monotonic_timestamp_boot = 0; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1433 | if (m->has_monotonic_timestamp_time) { |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1434 | monotonic_timestamp_boot = parts().logger_boot_count; |
| 1435 | } |
| 1436 | size_t monotonic_remote_boot = 0xffffff; |
| 1437 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1438 | if (m->monotonic_remote_time.has_value()) { |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 1439 | const Node *node = |
| 1440 | parts().config->nodes()->Get(source_node_index_[m->channel_index]); |
milind-u | a50344f | 2021-08-25 18:22:20 -0700 | [diff] [blame] | 1441 | |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1442 | std::optional<size_t> boot = parts_message_reader_.boot_count( |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1443 | source_node_index_[m->channel_index]); |
milind-u | a50344f | 2021-08-25 18:22:20 -0700 | [diff] [blame] | 1444 | CHECK(boot) << ": Failed to find boot for node " << MaybeNodeName(node) |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 1445 | << ", with index " << source_node_index_[m->channel_index]; |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1446 | monotonic_remote_boot = *boot; |
| 1447 | } |
| 1448 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1449 | messages_.insert( |
| 1450 | Message{.channel_index = m->channel_index, |
| 1451 | .queue_index = BootQueueIndex{.boot = parts().boot_count, |
| 1452 | .index = m->queue_index}, |
| 1453 | .timestamp = BootTimestamp{.boot = parts().boot_count, |
| 1454 | .time = m->monotonic_sent_time}, |
| 1455 | .monotonic_remote_boot = monotonic_remote_boot, |
| 1456 | .monotonic_timestamp_boot = monotonic_timestamp_boot, |
| 1457 | .data = std::move(m)}); |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 1458 | |
| 1459 | // Now, update sorted_until_ to match the new message. |
| 1460 | if (parts_message_reader_.newest_timestamp() > |
| 1461 | monotonic_clock::min_time + |
| 1462 | parts_message_reader_.max_out_of_order_duration()) { |
| 1463 | sorted_until_ = parts_message_reader_.newest_timestamp() - |
| 1464 | parts_message_reader_.max_out_of_order_duration(); |
| 1465 | } else { |
| 1466 | sorted_until_ = monotonic_clock::min_time; |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | // Now that we have enough data queued, return a pointer to the oldest piece |
| 1472 | // of data if it exists. |
| 1473 | if (messages_.empty()) { |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 1474 | last_message_time_ = monotonic_clock::max_time; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 1475 | return nullptr; |
| 1476 | } |
| 1477 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1478 | CHECK_GE(messages_.begin()->timestamp.time, last_message_time_) |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1479 | << DebugString() << " reading " << parts_message_reader_.filename(); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1480 | last_message_time_ = messages_.begin()->timestamp.time; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 1481 | return &(*messages_.begin()); |
| 1482 | } |
| 1483 | |
| 1484 | void LogPartsSorter::PopFront() { messages_.erase(messages_.begin()); } |
| 1485 | |
| 1486 | std::string LogPartsSorter::DebugString() const { |
| 1487 | std::stringstream ss; |
| 1488 | ss << "messages: [\n"; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1489 | int count = 0; |
| 1490 | bool no_dots = true; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 1491 | for (const Message &m : messages_) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1492 | if (count < 15 || count > static_cast<int>(messages_.size()) - 15) { |
| 1493 | ss << m << "\n"; |
| 1494 | } else if (no_dots) { |
| 1495 | ss << "...\n"; |
| 1496 | no_dots = false; |
| 1497 | } |
| 1498 | ++count; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 1499 | } |
| 1500 | ss << "] <- " << parts_message_reader_.filename(); |
| 1501 | return ss.str(); |
| 1502 | } |
| 1503 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1504 | NodeMerger::NodeMerger(std::vector<LogParts> parts) { |
| 1505 | CHECK_GE(parts.size(), 1u); |
Austin Schuh | 715adc1 | 2021-06-29 22:07:39 -0700 | [diff] [blame] | 1506 | // Enforce that we are sorting things only from a single node from a single |
| 1507 | // boot. |
| 1508 | const std::string_view part0_node = parts[0].node; |
| 1509 | const std::string_view part0_source_boot_uuid = parts[0].source_boot_uuid; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1510 | for (size_t i = 1; i < parts.size(); ++i) { |
| 1511 | CHECK_EQ(part0_node, parts[i].node) << ": Can't merge different nodes."; |
Austin Schuh | 715adc1 | 2021-06-29 22:07:39 -0700 | [diff] [blame] | 1512 | CHECK_EQ(part0_source_boot_uuid, parts[i].source_boot_uuid) |
| 1513 | << ": Can't merge different boots."; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1514 | } |
Austin Schuh | 715adc1 | 2021-06-29 22:07:39 -0700 | [diff] [blame] | 1515 | |
| 1516 | node_ = configuration::GetNodeIndex(parts[0].config.get(), part0_node); |
| 1517 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1518 | for (LogParts &part : parts) { |
| 1519 | parts_sorters_.emplace_back(std::move(part)); |
| 1520 | } |
| 1521 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1522 | monotonic_start_time_ = monotonic_clock::max_time; |
Austin Schuh | 9dc4261 | 2021-09-20 20:41:29 -0700 | [diff] [blame] | 1523 | realtime_start_time_ = realtime_clock::min_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1524 | for (const LogPartsSorter &parts_sorter : parts_sorters_) { |
Sanjay Narayanan | 9896c75 | 2021-09-01 16:16:48 -0700 | [diff] [blame] | 1525 | // We want to capture the earliest meaningful start time here. The start |
| 1526 | // time defaults to min_time when there's no meaningful value to report, so |
| 1527 | // let's ignore those. |
Austin Schuh | 9dc4261 | 2021-09-20 20:41:29 -0700 | [diff] [blame] | 1528 | if (parts_sorter.monotonic_start_time() != monotonic_clock::min_time) { |
| 1529 | bool accept = false; |
| 1530 | // We want to prioritize start times from the logger node. Really, we |
| 1531 | // want to prioritize start times with a valid realtime_clock time. So, |
| 1532 | // if we have a start time without a RT clock, prefer a start time with a |
| 1533 | // RT clock, even it if is later. |
| 1534 | if (parts_sorter.realtime_start_time() != realtime_clock::min_time) { |
| 1535 | // We've got a good one. See if the current start time has a good RT |
| 1536 | // clock, or if we should use this one instead. |
| 1537 | if (parts_sorter.monotonic_start_time() < monotonic_start_time_) { |
| 1538 | accept = true; |
| 1539 | } else if (realtime_start_time_ == realtime_clock::min_time) { |
| 1540 | // The previous start time doesn't have a good RT time, so it is very |
| 1541 | // likely the start time from a remote part file. We just found a |
| 1542 | // better start time with a real RT time, so switch to that instead. |
| 1543 | accept = true; |
| 1544 | } |
| 1545 | } else if (realtime_start_time_ == realtime_clock::min_time) { |
| 1546 | // We don't have a RT time, so take the oldest. |
| 1547 | if (parts_sorter.monotonic_start_time() < monotonic_start_time_) { |
| 1548 | accept = true; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | if (accept) { |
| 1553 | monotonic_start_time_ = parts_sorter.monotonic_start_time(); |
| 1554 | realtime_start_time_ = parts_sorter.realtime_start_time(); |
| 1555 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1556 | } |
| 1557 | } |
Sanjay Narayanan | 9896c75 | 2021-09-01 16:16:48 -0700 | [diff] [blame] | 1558 | |
| 1559 | // If there was no meaningful start time reported, just use min_time. |
| 1560 | if (monotonic_start_time_ == monotonic_clock::max_time) { |
| 1561 | monotonic_start_time_ = monotonic_clock::min_time; |
| 1562 | realtime_start_time_ = realtime_clock::min_time; |
| 1563 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1564 | } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 1565 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 1566 | std::vector<const LogParts *> NodeMerger::Parts() const { |
| 1567 | std::vector<const LogParts *> p; |
| 1568 | p.reserve(parts_sorters_.size()); |
| 1569 | for (const LogPartsSorter &parts_sorter : parts_sorters_) { |
| 1570 | p.emplace_back(&parts_sorter.parts()); |
| 1571 | } |
| 1572 | return p; |
| 1573 | } |
| 1574 | |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 1575 | Message *NodeMerger::Front() { |
| 1576 | // Return the current Front if we have one, otherwise go compute one. |
| 1577 | if (current_ != nullptr) { |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 1578 | Message *result = current_->Front(); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1579 | CHECK_GE(result->timestamp.time, last_message_time_); |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 1580 | return result; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | // Otherwise, do a simple search for the oldest message, deduplicating any |
| 1584 | // duplicates. |
| 1585 | Message *oldest = nullptr; |
| 1586 | sorted_until_ = monotonic_clock::max_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1587 | for (LogPartsSorter &parts_sorter : parts_sorters_) { |
| 1588 | Message *m = parts_sorter.Front(); |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 1589 | if (!m) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1590 | sorted_until_ = std::min(sorted_until_, parts_sorter.sorted_until()); |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 1591 | continue; |
| 1592 | } |
| 1593 | if (oldest == nullptr || *m < *oldest) { |
| 1594 | oldest = m; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1595 | current_ = &parts_sorter; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 1596 | } else if (*m == *oldest) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1597 | // Found a duplicate. If there is a choice, we want the one which has |
| 1598 | // the timestamp time. |
| 1599 | if (!m->data->has_monotonic_timestamp_time) { |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 1600 | parts_sorter.PopFront(); |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1601 | } else if (!oldest->data->has_monotonic_timestamp_time) { |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 1602 | current_->PopFront(); |
| 1603 | current_ = &parts_sorter; |
| 1604 | oldest = m; |
| 1605 | } else { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1606 | CHECK_EQ(m->data->monotonic_timestamp_time, |
| 1607 | oldest->data->monotonic_timestamp_time); |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 1608 | parts_sorter.PopFront(); |
| 1609 | } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | // PopFront may change this, so compute it down here. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1613 | sorted_until_ = std::min(sorted_until_, parts_sorter.sorted_until()); |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 1614 | } |
| 1615 | |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 1616 | if (oldest) { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1617 | CHECK_GE(oldest->timestamp.time, last_message_time_); |
| 1618 | last_message_time_ = oldest->timestamp.time; |
Austin Schuh | 5dd2284 | 2021-11-17 16:09:39 -0800 | [diff] [blame] | 1619 | monotonic_oldest_time_ = |
| 1620 | std::min(monotonic_oldest_time_, oldest->timestamp.time); |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 1621 | } else { |
| 1622 | last_message_time_ = monotonic_clock::max_time; |
| 1623 | } |
| 1624 | |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 1625 | // Return the oldest message found. This will be nullptr if nothing was |
| 1626 | // found, indicating there is nothing left. |
| 1627 | return oldest; |
| 1628 | } |
| 1629 | |
| 1630 | void NodeMerger::PopFront() { |
| 1631 | CHECK(current_ != nullptr) << "Popping before calling Front()"; |
| 1632 | current_->PopFront(); |
| 1633 | current_ = nullptr; |
| 1634 | } |
| 1635 | |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1636 | BootMerger::BootMerger(std::vector<LogParts> files) { |
| 1637 | std::vector<std::vector<LogParts>> boots; |
| 1638 | |
| 1639 | // Now, we need to split things out by boot. |
| 1640 | for (size_t i = 0; i < files.size(); ++i) { |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1641 | const size_t boot_count = files[i].boot_count; |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1642 | if (boot_count + 1 > boots.size()) { |
| 1643 | boots.resize(boot_count + 1); |
| 1644 | } |
| 1645 | boots[boot_count].emplace_back(std::move(files[i])); |
| 1646 | } |
| 1647 | |
| 1648 | node_mergers_.reserve(boots.size()); |
| 1649 | for (size_t i = 0; i < boots.size(); ++i) { |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1650 | VLOG(2) << "Boot " << i; |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1651 | for (auto &p : boots[i]) { |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1652 | VLOG(2) << "Part " << p; |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1653 | } |
| 1654 | node_mergers_.emplace_back( |
| 1655 | std::make_unique<NodeMerger>(std::move(boots[i]))); |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | Message *BootMerger::Front() { |
| 1660 | Message *result = node_mergers_[index_]->Front(); |
| 1661 | |
| 1662 | if (result != nullptr) { |
| 1663 | return result; |
| 1664 | } |
| 1665 | |
| 1666 | if (index_ + 1u == node_mergers_.size()) { |
| 1667 | // At the end of the last node merger, just return. |
| 1668 | return nullptr; |
| 1669 | } else { |
| 1670 | ++index_; |
| 1671 | return Front(); |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | void BootMerger::PopFront() { node_mergers_[index_]->PopFront(); } |
| 1676 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1677 | std::vector<const LogParts *> BootMerger::Parts() const { |
| 1678 | std::vector<const LogParts *> results; |
| 1679 | for (const std::unique_ptr<NodeMerger> &node_merger : node_mergers_) { |
| 1680 | std::vector<const LogParts *> node_parts = node_merger->Parts(); |
| 1681 | |
| 1682 | results.insert(results.end(), std::make_move_iterator(node_parts.begin()), |
| 1683 | std::make_move_iterator(node_parts.end())); |
| 1684 | } |
| 1685 | |
| 1686 | return results; |
| 1687 | } |
| 1688 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1689 | TimestampMapper::TimestampMapper(std::vector<LogParts> parts) |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1690 | : boot_merger_(std::move(parts)), |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1691 | timestamp_callback_([](TimestampedMessage *) {}) { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1692 | for (const LogParts *part : boot_merger_.Parts()) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 1693 | if (!configuration_) { |
| 1694 | configuration_ = part->config; |
| 1695 | } else { |
| 1696 | CHECK_EQ(configuration_.get(), part->config.get()); |
| 1697 | } |
| 1698 | } |
| 1699 | const Configuration *config = configuration_.get(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1700 | // Only fill out nodes_data_ if there are nodes. Otherwise everything gets |
| 1701 | // pretty simple. |
| 1702 | if (configuration::MultiNode(config)) { |
| 1703 | nodes_data_.resize(config->nodes()->size()); |
| 1704 | const Node *my_node = config->nodes()->Get(node()); |
| 1705 | for (size_t node_index = 0; node_index < nodes_data_.size(); ++node_index) { |
| 1706 | const Node *node = config->nodes()->Get(node_index); |
| 1707 | NodeData *node_data = &nodes_data_[node_index]; |
| 1708 | node_data->channels.resize(config->channels()->size()); |
| 1709 | // We should save the channel if it is delivered to the node represented |
| 1710 | // by the NodeData, but not sent by that node. That combo means it is |
| 1711 | // forwarded. |
| 1712 | size_t channel_index = 0; |
| 1713 | node_data->any_delivered = false; |
| 1714 | for (const Channel *channel : *config->channels()) { |
| 1715 | node_data->channels[channel_index].delivered = |
| 1716 | configuration::ChannelIsReadableOnNode(channel, node) && |
Austin Schuh | b3dbb6d | 2021-01-02 17:29:35 -0800 | [diff] [blame] | 1717 | configuration::ChannelIsSendableOnNode(channel, my_node) && |
| 1718 | (my_node != node); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1719 | node_data->any_delivered = node_data->any_delivered || |
| 1720 | node_data->channels[channel_index].delivered; |
Austin Schuh | 6a7358f | 2021-11-18 22:40:40 -0800 | [diff] [blame] | 1721 | if (node_data->channels[channel_index].delivered) { |
| 1722 | const Connection *connection = |
| 1723 | configuration::ConnectionToNode(channel, node); |
| 1724 | node_data->channels[channel_index].time_to_live = |
| 1725 | chrono::nanoseconds(connection->time_to_live()); |
| 1726 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1727 | ++channel_index; |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | for (const Channel *channel : *config->channels()) { |
| 1732 | source_node_.emplace_back(configuration::GetNodeIndex( |
| 1733 | config, channel->source_node()->string_view())); |
| 1734 | } |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | void TimestampMapper::AddPeer(TimestampMapper *timestamp_mapper) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 1739 | CHECK(configuration::MultiNode(configuration())); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1740 | CHECK_NE(timestamp_mapper->node(), node()); |
| 1741 | CHECK_LT(timestamp_mapper->node(), nodes_data_.size()); |
| 1742 | |
| 1743 | NodeData *node_data = &nodes_data_[timestamp_mapper->node()]; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1744 | // Only set it if this node delivers to the peer timestamp_mapper. Otherwise |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1745 | // we could needlessly save data. |
| 1746 | if (node_data->any_delivered) { |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1747 | VLOG(1) << "Registering on node " << node() << " for peer node " |
| 1748 | << timestamp_mapper->node(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1749 | CHECK(timestamp_mapper->nodes_data_[node()].peer == nullptr); |
| 1750 | |
| 1751 | timestamp_mapper->nodes_data_[node()].peer = this; |
Austin Schuh | 36c0093 | 2021-07-19 18:13:21 -0700 | [diff] [blame] | 1752 | |
| 1753 | node_data->save_for_peer = true; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1754 | } |
| 1755 | } |
| 1756 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1757 | void TimestampMapper::QueueMessage(Message *m) { |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 1758 | matched_messages_.emplace_back( |
| 1759 | TimestampedMessage{.channel_index = m->channel_index, |
| 1760 | .queue_index = m->queue_index, |
| 1761 | .monotonic_event_time = m->timestamp, |
| 1762 | .realtime_event_time = m->data->realtime_sent_time, |
| 1763 | .remote_queue_index = BootQueueIndex::Invalid(), |
| 1764 | .monotonic_remote_time = BootTimestamp::min_time(), |
| 1765 | .realtime_remote_time = realtime_clock::min_time, |
| 1766 | .monotonic_timestamp_time = BootTimestamp::min_time(), |
| 1767 | .data = std::move(m->data)}); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | TimestampedMessage *TimestampMapper::Front() { |
| 1771 | // No need to fetch anything new. A previous message still exists. |
| 1772 | switch (first_message_) { |
| 1773 | case FirstMessage::kNeedsUpdate: |
| 1774 | break; |
| 1775 | case FirstMessage::kInMessage: |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1776 | return &matched_messages_.front(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1777 | case FirstMessage::kNullptr: |
| 1778 | return nullptr; |
| 1779 | } |
| 1780 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1781 | if (matched_messages_.empty()) { |
| 1782 | if (!QueueMatched()) { |
| 1783 | first_message_ = FirstMessage::kNullptr; |
| 1784 | return nullptr; |
| 1785 | } |
| 1786 | } |
| 1787 | first_message_ = FirstMessage::kInMessage; |
| 1788 | return &matched_messages_.front(); |
| 1789 | } |
| 1790 | |
| 1791 | bool TimestampMapper::QueueMatched() { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1792 | if (nodes_data_.empty()) { |
| 1793 | // Simple path. We are single node, so there are no timestamps to match! |
| 1794 | CHECK_EQ(messages_.size(), 0u); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1795 | Message *m = boot_merger_.Front(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1796 | if (!m) { |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1797 | return false; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1798 | } |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1799 | // Enqueue this message into matched_messages_ so we have a place to |
| 1800 | // associate remote timestamps, and return it. |
| 1801 | QueueMessage(m); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1802 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1803 | CHECK_GE(matched_messages_.back().monotonic_event_time, last_message_time_); |
| 1804 | last_message_time_ = matched_messages_.back().monotonic_event_time; |
| 1805 | |
| 1806 | // We are thin wrapper around node_merger. Call it directly. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1807 | boot_merger_.PopFront(); |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1808 | timestamp_callback_(&matched_messages_.back()); |
| 1809 | return true; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1810 | } |
| 1811 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1812 | // We need to only add messages to the list so they get processed for |
| 1813 | // messages which are delivered. Reuse the flow below which uses messages_ |
| 1814 | // by just adding the new message to messages_ and continuing. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1815 | if (messages_.empty()) { |
| 1816 | if (!Queue()) { |
| 1817 | // Found nothing to add, we are out of data! |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1818 | return false; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1819 | } |
| 1820 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1821 | // Now that it has been added (and cannibalized), forget about it |
| 1822 | // upstream. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1823 | boot_merger_.PopFront(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | Message *m = &(messages_.front()); |
| 1827 | |
| 1828 | if (source_node_[m->channel_index] == node()) { |
| 1829 | // From us, just forward it on, filling the remote data in as invalid. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1830 | QueueMessage(m); |
| 1831 | CHECK_GE(matched_messages_.back().monotonic_event_time, last_message_time_); |
| 1832 | last_message_time_ = matched_messages_.back().monotonic_event_time; |
| 1833 | messages_.pop_front(); |
| 1834 | timestamp_callback_(&matched_messages_.back()); |
| 1835 | return true; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1836 | } else { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1837 | // Got a timestamp, find the matching remote data, match it, and return |
| 1838 | // it. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1839 | Message data = MatchingMessageFor(*m); |
| 1840 | |
| 1841 | // Return the data from the remote. The local message only has timestamp |
| 1842 | // info which isn't relevant anymore once extracted. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1843 | matched_messages_.emplace_back(TimestampedMessage{ |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1844 | .channel_index = m->channel_index, |
| 1845 | .queue_index = m->queue_index, |
| 1846 | .monotonic_event_time = m->timestamp, |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1847 | .realtime_event_time = m->data->realtime_sent_time, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1848 | .remote_queue_index = |
| 1849 | BootQueueIndex{.boot = m->monotonic_remote_boot, |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1850 | .index = m->data->remote_queue_index.value()}, |
| 1851 | .monotonic_remote_time = {m->monotonic_remote_boot, |
Austin Schuh | 826e6ce | 2021-11-18 20:33:10 -0800 | [diff] [blame] | 1852 | m->data->monotonic_remote_time.value()}, |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1853 | .realtime_remote_time = m->data->realtime_remote_time.value(), |
| 1854 | .monotonic_timestamp_time = {m->monotonic_timestamp_boot, |
| 1855 | m->data->monotonic_timestamp_time}, |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1856 | .data = std::move(data.data)}); |
| 1857 | CHECK_GE(matched_messages_.back().monotonic_event_time, last_message_time_); |
| 1858 | last_message_time_ = matched_messages_.back().monotonic_event_time; |
| 1859 | // Since messages_ holds the data, drop it. |
| 1860 | messages_.pop_front(); |
| 1861 | timestamp_callback_(&matched_messages_.back()); |
| 1862 | return true; |
| 1863 | } |
| 1864 | } |
| 1865 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1866 | void TimestampMapper::QueueUntil(BootTimestamp queue_time) { |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1867 | while (last_message_time_ <= queue_time) { |
| 1868 | if (!QueueMatched()) { |
| 1869 | return; |
| 1870 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1874 | void TimestampMapper::QueueFor(chrono::nanoseconds time_estimation_buffer) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1875 | // Note: queueing for time doesn't really work well across boots. So we |
| 1876 | // just assume that if you are using this, you only care about the current |
| 1877 | // boot. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1878 | // |
| 1879 | // TODO(austin): Is that the right concept? |
| 1880 | // |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1881 | // Make sure we have something queued first. This makes the end time |
| 1882 | // calculation simpler, and is typically what folks want regardless. |
| 1883 | if (matched_messages_.empty()) { |
| 1884 | if (!QueueMatched()) { |
| 1885 | return; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | const aos::monotonic_clock::time_point end_queue_time = |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1890 | std::max(monotonic_start_time( |
| 1891 | matched_messages_.front().monotonic_event_time.boot), |
| 1892 | matched_messages_.front().monotonic_event_time.time) + |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1893 | time_estimation_buffer; |
| 1894 | |
| 1895 | // Place sorted messages on the list until we have |
| 1896 | // --time_estimation_buffer_seconds seconds queued up (but queue at least |
| 1897 | // until the log starts). |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1898 | while (end_queue_time >= last_message_time_.time) { |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1899 | if (!QueueMatched()) { |
| 1900 | return; |
| 1901 | } |
| 1902 | } |
| 1903 | } |
| 1904 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1905 | void TimestampMapper::PopFront() { |
| 1906 | CHECK(first_message_ != FirstMessage::kNeedsUpdate); |
Austin Schuh | 6a7358f | 2021-11-18 22:40:40 -0800 | [diff] [blame] | 1907 | last_popped_message_time_ = Front()->monotonic_event_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1908 | first_message_ = FirstMessage::kNeedsUpdate; |
| 1909 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1910 | matched_messages_.pop_front(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | Message TimestampMapper::MatchingMessageFor(const Message &message) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1914 | // Figure out what queue index we are looking for. |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1915 | CHECK_NOTNULL(message.data); |
| 1916 | CHECK(message.data->remote_queue_index.has_value()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1917 | const BootQueueIndex remote_queue_index = |
| 1918 | BootQueueIndex{.boot = message.monotonic_remote_boot, |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1919 | .index = *message.data->remote_queue_index}; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1920 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1921 | CHECK(message.data->monotonic_remote_time.has_value()); |
| 1922 | CHECK(message.data->realtime_remote_time.has_value()); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1923 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1924 | const BootTimestamp monotonic_remote_time{ |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1925 | .boot = message.monotonic_remote_boot, |
Austin Schuh | 826e6ce | 2021-11-18 20:33:10 -0800 | [diff] [blame] | 1926 | .time = message.data->monotonic_remote_time.value()}; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1927 | const realtime_clock::time_point realtime_remote_time = |
| 1928 | *message.data->realtime_remote_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1929 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1930 | TimestampMapper *peer = |
| 1931 | nodes_data_[source_node_[message.data->channel_index]].peer; |
Austin Schuh | fecf1d8 | 2020-12-19 16:57:28 -0800 | [diff] [blame] | 1932 | |
| 1933 | // We only register the peers which we have data for. So, if we are being |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1934 | // asked to pull a timestamp from a peer which doesn't exist, return an |
| 1935 | // empty message. |
Austin Schuh | fecf1d8 | 2020-12-19 16:57:28 -0800 | [diff] [blame] | 1936 | if (peer == nullptr) { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1937 | // TODO(austin): Make sure the tests hit all these paths with a boot count |
| 1938 | // of 1... |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1939 | return Message{.channel_index = message.channel_index, |
| 1940 | .queue_index = remote_queue_index, |
| 1941 | .timestamp = monotonic_remote_time, |
| 1942 | .monotonic_remote_boot = 0xffffff, |
| 1943 | .monotonic_timestamp_boot = 0xffffff, |
| 1944 | .data = nullptr}; |
Austin Schuh | fecf1d8 | 2020-12-19 16:57:28 -0800 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | // The queue which will have the matching data, if available. |
| 1948 | std::deque<Message> *data_queue = |
| 1949 | &peer->nodes_data_[node()].channels[message.channel_index].messages; |
| 1950 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1951 | peer->QueueUnmatchedUntil(monotonic_remote_time); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1952 | |
| 1953 | if (data_queue->empty()) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1954 | return Message{.channel_index = message.channel_index, |
| 1955 | .queue_index = remote_queue_index, |
| 1956 | .timestamp = monotonic_remote_time, |
| 1957 | .monotonic_remote_boot = 0xffffff, |
| 1958 | .monotonic_timestamp_boot = 0xffffff, |
| 1959 | .data = nullptr}; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1960 | } |
| 1961 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1962 | if (remote_queue_index < data_queue->front().queue_index || |
| 1963 | remote_queue_index > data_queue->back().queue_index) { |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 1964 | return Message{.channel_index = message.channel_index, |
| 1965 | .queue_index = remote_queue_index, |
| 1966 | .timestamp = monotonic_remote_time, |
| 1967 | .monotonic_remote_boot = 0xffffff, |
| 1968 | .monotonic_timestamp_boot = 0xffffff, |
| 1969 | .data = nullptr}; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1970 | } |
| 1971 | |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1972 | // The algorithm below is constant time with some assumptions. We need there |
| 1973 | // to be no missing messages in the data stream. This also assumes a queue |
| 1974 | // hasn't wrapped. That is conservative, but should let us get started. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1975 | if (data_queue->back().queue_index.boot == |
| 1976 | data_queue->front().queue_index.boot && |
| 1977 | (data_queue->back().queue_index.index - |
| 1978 | data_queue->front().queue_index.index + 1u == |
| 1979 | data_queue->size())) { |
| 1980 | CHECK_EQ(remote_queue_index.boot, data_queue->front().queue_index.boot); |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1981 | // Pull the data out and confirm that the timestamps match as expected. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1982 | // |
| 1983 | // TODO(austin): Move if not reliable. |
| 1984 | Message result = (*data_queue)[remote_queue_index.index - |
| 1985 | data_queue->front().queue_index.index]; |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1986 | |
| 1987 | CHECK_EQ(result.timestamp, monotonic_remote_time) |
| 1988 | << ": Queue index matches, but timestamp doesn't. Please investigate!"; |
Austin Schuh | 6a7358f | 2021-11-18 22:40:40 -0800 | [diff] [blame] | 1989 | CHECK_EQ(result.data->realtime_sent_time, realtime_remote_time) |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1990 | << ": Queue index matches, but timestamp doesn't. Please investigate!"; |
| 1991 | // Now drop the data off the front. We have deduplicated timestamps, so we |
| 1992 | // are done. And all the data is in order. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1993 | data_queue->erase( |
| 1994 | data_queue->begin(), |
| 1995 | data_queue->begin() + |
| 1996 | (remote_queue_index.index - data_queue->front().queue_index.index)); |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1997 | return result; |
| 1998 | } else { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1999 | // TODO(austin): Binary search. |
| 2000 | auto it = std::find_if( |
| 2001 | data_queue->begin(), data_queue->end(), |
| 2002 | [remote_queue_index, |
| 2003 | remote_boot = monotonic_remote_time.boot](const Message &m) { |
| 2004 | return m.queue_index == remote_queue_index && |
| 2005 | m.timestamp.boot == remote_boot; |
| 2006 | }); |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 2007 | if (it == data_queue->end()) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 2008 | return Message{.channel_index = message.channel_index, |
| 2009 | .queue_index = remote_queue_index, |
| 2010 | .timestamp = monotonic_remote_time, |
| 2011 | .monotonic_remote_boot = 0xffffff, |
| 2012 | .monotonic_timestamp_boot = 0xffffff, |
| 2013 | .data = nullptr}; |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | Message result = std::move(*it); |
| 2017 | |
| 2018 | CHECK_EQ(result.timestamp, monotonic_remote_time) |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 2019 | << ": Queue index matches, but timestamp doesn't. Please " |
| 2020 | "investigate!"; |
| 2021 | CHECK_EQ(result.data->realtime_sent_time, realtime_remote_time) |
| 2022 | << ": Queue index matches, but timestamp doesn't. Please " |
| 2023 | "investigate!"; |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 2024 | |
Austin Schuh | d6b1f4c | 2021-11-18 20:29:00 -0800 | [diff] [blame] | 2025 | // Erase everything up to this message. We want to keep 1 message in the |
| 2026 | // queue so we can handle reliable messages forwarded across boots. |
| 2027 | data_queue->erase(data_queue->begin(), it); |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 2028 | |
| 2029 | return result; |
| 2030 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 2031 | } |
| 2032 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 2033 | void TimestampMapper::QueueUnmatchedUntil(BootTimestamp t) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 2034 | if (queued_until_ > t) { |
| 2035 | return; |
| 2036 | } |
| 2037 | while (true) { |
| 2038 | if (!messages_.empty() && messages_.back().timestamp > t) { |
| 2039 | queued_until_ = std::max(queued_until_, messages_.back().timestamp); |
| 2040 | return; |
| 2041 | } |
| 2042 | |
| 2043 | if (!Queue()) { |
| 2044 | // Found nothing to add, we are out of data! |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 2045 | queued_until_ = BootTimestamp::max_time(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 2046 | return; |
| 2047 | } |
| 2048 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 2049 | // Now that it has been added (and cannibalized), forget about it |
| 2050 | // upstream. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 2051 | boot_merger_.PopFront(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | bool TimestampMapper::Queue() { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 2056 | Message *m = boot_merger_.Front(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 2057 | if (m == nullptr) { |
| 2058 | return false; |
| 2059 | } |
| 2060 | for (NodeData &node_data : nodes_data_) { |
| 2061 | if (!node_data.any_delivered) continue; |
Austin Schuh | 36c0093 | 2021-07-19 18:13:21 -0700 | [diff] [blame] | 2062 | if (!node_data.save_for_peer) continue; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 2063 | if (node_data.channels[m->channel_index].delivered) { |
Austin Schuh | 6a7358f | 2021-11-18 22:40:40 -0800 | [diff] [blame] | 2064 | // If we have data but no timestamps (logs where the timestamps didn't get |
| 2065 | // logged are classic), we can grow this indefinitely. We don't need to |
| 2066 | // keep anything that is older than the last message returned. |
| 2067 | |
| 2068 | // We have the time on the source node. |
| 2069 | // We care to wait until we have the time on the destination node. |
| 2070 | std::deque<Message> &messages = |
| 2071 | node_data.channels[m->channel_index].messages; |
| 2072 | // Max delay over the network is the TTL, so let's take the queue time and |
| 2073 | // add TTL to it. Don't forget any messages which are reliable until |
| 2074 | // someone can come up with a good reason to forget those too. |
| 2075 | if (node_data.channels[m->channel_index].time_to_live > |
| 2076 | chrono::nanoseconds(0)) { |
| 2077 | // We need to make *some* assumptions about network delay for this to |
| 2078 | // work. We want to only look at the RX side. This means we need to |
| 2079 | // track the last time a message was popped from any channel from the |
| 2080 | // node sending this message, and compare that to the max time we expect |
| 2081 | // that a message will take to be delivered across the network. This |
| 2082 | // assumes that messages are popped in time order as a proxy for |
| 2083 | // measuring the distributed time at this layer. |
| 2084 | // |
| 2085 | // Leave at least 1 message in here so we can handle reboots and |
| 2086 | // messages getting sent twice. |
| 2087 | while (messages.size() > 1u && |
| 2088 | messages.begin()->timestamp + |
| 2089 | node_data.channels[m->channel_index].time_to_live + |
| 2090 | chrono::duration_cast<chrono::nanoseconds>( |
| 2091 | chrono::duration<double>(FLAGS_max_network_delay)) < |
| 2092 | last_popped_message_time_) { |
| 2093 | messages.pop_front(); |
| 2094 | } |
| 2095 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 2096 | node_data.channels[m->channel_index].messages.emplace_back(*m); |
| 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | messages_.emplace_back(std::move(*m)); |
| 2101 | return true; |
| 2102 | } |
| 2103 | |
| 2104 | std::string TimestampMapper::DebugString() const { |
| 2105 | std::stringstream ss; |
Austin Schuh | 6e014b8 | 2021-09-14 17:46:33 -0700 | [diff] [blame] | 2106 | ss << "node " << node() << " (" << node_name() << ") [\n"; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 2107 | for (const Message &message : messages_) { |
| 2108 | ss << " " << message << "\n"; |
| 2109 | } |
| 2110 | ss << "] queued_until " << queued_until_; |
| 2111 | for (const NodeData &ns : nodes_data_) { |
| 2112 | if (ns.peer == nullptr) continue; |
| 2113 | ss << "\nnode " << ns.peer->node() << " remote_data [\n"; |
| 2114 | size_t channel_index = 0; |
| 2115 | for (const NodeData::ChannelData &channel_data : |
| 2116 | ns.peer->nodes_data_[node()].channels) { |
| 2117 | if (channel_data.messages.empty()) { |
| 2118 | continue; |
| 2119 | } |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 2120 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 2121 | ss << " channel " << channel_index << " [\n"; |
| 2122 | for (const Message &m : channel_data.messages) { |
| 2123 | ss << " " << m << "\n"; |
| 2124 | } |
| 2125 | ss << " ]\n"; |
| 2126 | ++channel_index; |
| 2127 | } |
| 2128 | ss << "] queued_until " << ns.peer->queued_until_; |
| 2129 | } |
| 2130 | return ss.str(); |
| 2131 | } |
| 2132 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 2133 | std::string MaybeNodeName(const Node *node) { |
| 2134 | if (node != nullptr) { |
| 2135 | return node->name()->str() + " "; |
| 2136 | } |
| 2137 | return ""; |
| 2138 | } |
| 2139 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 2140 | } // namespace aos::logger |