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( |
| 39 | max_out_of_order, -1, |
| 40 | "If set, this overrides the max out of order duration for a log file."); |
| 41 | |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 42 | DEFINE_bool(workaround_double_headers, true, |
| 43 | "Some old log files have two headers at the beginning. Use the " |
| 44 | "last header as the actual header."); |
| 45 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 46 | namespace aos::logger { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 47 | namespace { |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 48 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 49 | namespace chrono = std::chrono; |
| 50 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 51 | template <typename T> |
| 52 | void PrintOptionalOrNull(std::ostream *os, const std::optional<T> &t) { |
| 53 | if (t.has_value()) { |
| 54 | *os << *t; |
| 55 | } else { |
| 56 | *os << "null"; |
| 57 | } |
| 58 | } |
| 59 | } // namespace |
| 60 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 61 | DetachedBufferWriter::DetachedBufferWriter( |
| 62 | std::string_view filename, std::unique_ptr<DetachedBufferEncoder> encoder) |
| 63 | : filename_(filename), encoder_(std::move(encoder)) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 64 | if (!util::MkdirPIfSpace(filename, 0777)) { |
| 65 | ran_out_of_space_ = true; |
| 66 | } else { |
| 67 | fd_ = open(std::string(filename).c_str(), |
| 68 | O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 0774); |
| 69 | if (fd_ == -1 && errno == ENOSPC) { |
| 70 | ran_out_of_space_ = true; |
| 71 | } else { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 72 | PCHECK(fd_ != -1) << ": Failed to open " << this->filename() |
| 73 | << " for writing"; |
| 74 | VLOG(1) << "Opened " << this->filename() << " for writing"; |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | DetachedBufferWriter::~DetachedBufferWriter() { |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 80 | Close(); |
| 81 | if (ran_out_of_space_) { |
| 82 | CHECK(acknowledge_ran_out_of_space_) |
| 83 | << ": Unacknowledged out of disk space, log file was not completed"; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 84 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 87 | DetachedBufferWriter::DetachedBufferWriter(DetachedBufferWriter &&other) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 88 | *this = std::move(other); |
| 89 | } |
| 90 | |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 91 | // When other is destroyed "soon" (which it should be because we're getting an |
| 92 | // rvalue reference to it), it will flush etc all the data we have queued up |
| 93 | // (because that data will then be its data). |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 94 | DetachedBufferWriter &DetachedBufferWriter::operator=( |
| 95 | DetachedBufferWriter &&other) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 96 | std::swap(filename_, other.filename_); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 97 | std::swap(encoder_, other.encoder_); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 98 | std::swap(fd_, other.fd_); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 99 | std::swap(ran_out_of_space_, other.ran_out_of_space_); |
| 100 | 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] | 101 | std::swap(iovec_, other.iovec_); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 102 | std::swap(max_write_time_, other.max_write_time_); |
| 103 | std::swap(max_write_time_bytes_, other.max_write_time_bytes_); |
| 104 | std::swap(max_write_time_messages_, other.max_write_time_messages_); |
| 105 | std::swap(total_write_time_, other.total_write_time_); |
| 106 | std::swap(total_write_count_, other.total_write_count_); |
| 107 | std::swap(total_write_messages_, other.total_write_messages_); |
| 108 | std::swap(total_write_bytes_, other.total_write_bytes_); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 109 | return *this; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 112 | void DetachedBufferWriter::QueueSpan(absl::Span<const uint8_t> span) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 113 | if (ran_out_of_space_) { |
| 114 | // We don't want any later data to be written after space becomes |
| 115 | // available, so refuse to write anything more once we've dropped data |
| 116 | // because we ran out of space. |
| 117 | VLOG(1) << "Ignoring span: " << span.size(); |
| 118 | return; |
| 119 | } |
| 120 | |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 121 | aos::monotonic_clock::time_point now; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 122 | if (encoder_->may_bypass() && span.size() > 4096u) { |
| 123 | // Over this threshold, we'll assume it's cheaper to add an extra |
| 124 | // syscall to write the data immediately instead of copying it to |
| 125 | // enqueue. |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 126 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 127 | // First, flush everything. |
| 128 | while (encoder_->queue_size() > 0u) { |
| 129 | Flush(); |
| 130 | } |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 131 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 132 | // Then, write it directly. |
| 133 | const auto start = aos::monotonic_clock::now(); |
| 134 | const ssize_t written = write(fd_, span.data(), span.size()); |
| 135 | const auto end = aos::monotonic_clock::now(); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 136 | HandleWriteReturn(written, span.size()); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 137 | UpdateStatsForWrite(end - start, written, 1); |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 138 | now = end; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 139 | } else { |
| 140 | encoder_->Encode(CopySpanAsDetachedBuffer(span)); |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 141 | now = aos::monotonic_clock::now(); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 142 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 143 | |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 144 | FlushAtThreshold(now); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 147 | void DetachedBufferWriter::Close() { |
| 148 | if (fd_ == -1) { |
| 149 | return; |
| 150 | } |
| 151 | encoder_->Finish(); |
| 152 | while (encoder_->queue_size() > 0) { |
| 153 | Flush(); |
| 154 | } |
| 155 | if (close(fd_) == -1) { |
| 156 | if (errno == ENOSPC) { |
| 157 | ran_out_of_space_ = true; |
| 158 | } else { |
| 159 | PLOG(ERROR) << "Closing log file failed"; |
| 160 | } |
| 161 | } |
| 162 | fd_ = -1; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 163 | VLOG(1) << "Closed " << filename(); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 166 | void DetachedBufferWriter::Flush() { |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 167 | if (ran_out_of_space_) { |
| 168 | // We don't want any later data to be written after space becomes available, |
| 169 | // so refuse to write anything more once we've dropped data because we ran |
| 170 | // out of space. |
Austin Schuh | a426f1f | 2021-03-31 22:27:41 -0700 | [diff] [blame] | 171 | if (encoder_) { |
| 172 | VLOG(1) << "Ignoring queue: " << encoder_->queue().size(); |
| 173 | encoder_->Clear(encoder_->queue().size()); |
| 174 | } else { |
| 175 | VLOG(1) << "No queue to ignore"; |
| 176 | } |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | const auto queue = encoder_->queue(); |
| 181 | if (queue.empty()) { |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 182 | return; |
| 183 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 184 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 185 | iovec_.clear(); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 186 | const size_t iovec_size = std::min<size_t>(queue.size(), IOV_MAX); |
| 187 | iovec_.resize(iovec_size); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 188 | size_t counted_size = 0; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 189 | for (size_t i = 0; i < iovec_size; ++i) { |
| 190 | iovec_[i].iov_base = const_cast<uint8_t *>(queue[i].data()); |
| 191 | iovec_[i].iov_len = queue[i].size(); |
| 192 | counted_size += iovec_[i].iov_len; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 193 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 194 | |
| 195 | const auto start = aos::monotonic_clock::now(); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 196 | const ssize_t written = writev(fd_, iovec_.data(), iovec_.size()); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 197 | const auto end = aos::monotonic_clock::now(); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 198 | HandleWriteReturn(written, counted_size); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 199 | |
| 200 | encoder_->Clear(iovec_size); |
| 201 | |
| 202 | UpdateStatsForWrite(end - start, written, iovec_size); |
| 203 | } |
| 204 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 205 | void DetachedBufferWriter::HandleWriteReturn(ssize_t write_return, |
| 206 | size_t write_size) { |
| 207 | if (write_return == -1 && errno == ENOSPC) { |
| 208 | ran_out_of_space_ = true; |
| 209 | return; |
| 210 | } |
| 211 | PCHECK(write_return >= 0) << ": write failed"; |
| 212 | if (write_return < static_cast<ssize_t>(write_size)) { |
| 213 | // Sometimes this happens instead of ENOSPC. On a real filesystem, this |
| 214 | // never seems to happen in any other case. If we ever want to log to a |
| 215 | // socket, this will happen more often. However, until we get there, we'll |
| 216 | // just assume it means we ran out of space. |
| 217 | ran_out_of_space_ = true; |
| 218 | return; |
| 219 | } |
| 220 | } |
| 221 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 222 | void DetachedBufferWriter::UpdateStatsForWrite( |
| 223 | aos::monotonic_clock::duration duration, ssize_t written, int iovec_size) { |
| 224 | if (duration > max_write_time_) { |
| 225 | max_write_time_ = duration; |
| 226 | max_write_time_bytes_ = written; |
| 227 | max_write_time_messages_ = iovec_size; |
| 228 | } |
| 229 | total_write_time_ += duration; |
| 230 | ++total_write_count_; |
| 231 | total_write_messages_ += iovec_size; |
| 232 | total_write_bytes_ += written; |
| 233 | } |
| 234 | |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 235 | void DetachedBufferWriter::FlushAtThreshold( |
| 236 | aos::monotonic_clock::time_point now) { |
Austin Schuh | a426f1f | 2021-03-31 22:27:41 -0700 | [diff] [blame] | 237 | if (ran_out_of_space_) { |
| 238 | // We don't want any later data to be written after space becomes available, |
| 239 | // so refuse to write anything more once we've dropped data because we ran |
| 240 | // out of space. |
| 241 | if (encoder_) { |
| 242 | VLOG(1) << "Ignoring queue: " << encoder_->queue().size(); |
| 243 | encoder_->Clear(encoder_->queue().size()); |
| 244 | } else { |
| 245 | VLOG(1) << "No queue to ignore"; |
| 246 | } |
| 247 | return; |
| 248 | } |
| 249 | |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 250 | // We don't want to flush the first time through. Otherwise we will flush as |
| 251 | // the log file header might be compressing, defeating any parallelism and |
| 252 | // queueing there. |
| 253 | if (last_flush_time_ == aos::monotonic_clock::min_time) { |
| 254 | last_flush_time_ = now; |
| 255 | } |
| 256 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 257 | // Flush if we are at the max number of iovs per writev, because there's no |
| 258 | // 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] | 259 | // data queued up or if it has been long enough. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 260 | while (encoder_->queued_bytes() > static_cast<size_t>(FLAGS_flush_size) || |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 261 | encoder_->queue_size() >= IOV_MAX || |
| 262 | now > last_flush_time_ + |
| 263 | chrono::duration_cast<chrono::nanoseconds>( |
| 264 | chrono::duration<double>(FLAGS_flush_period))) { |
| 265 | last_flush_time_ = now; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 266 | Flush(); |
| 267 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | flatbuffers::Offset<MessageHeader> PackMessage( |
| 271 | flatbuffers::FlatBufferBuilder *fbb, const Context &context, |
| 272 | int channel_index, LogType log_type) { |
| 273 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset; |
| 274 | |
| 275 | switch (log_type) { |
| 276 | case LogType::kLogMessage: |
| 277 | case LogType::kLogMessageAndDeliveryTime: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 278 | case LogType::kLogRemoteMessage: |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 279 | data_offset = fbb->CreateVector( |
| 280 | static_cast<const uint8_t *>(context.data), context.size); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 281 | break; |
| 282 | |
| 283 | case LogType::kLogDeliveryTimeOnly: |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | MessageHeader::Builder message_header_builder(*fbb); |
| 288 | message_header_builder.add_channel_index(channel_index); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 289 | |
| 290 | switch (log_type) { |
| 291 | case LogType::kLogRemoteMessage: |
| 292 | message_header_builder.add_queue_index(context.remote_queue_index); |
| 293 | message_header_builder.add_monotonic_sent_time( |
| 294 | context.monotonic_remote_time.time_since_epoch().count()); |
| 295 | message_header_builder.add_realtime_sent_time( |
| 296 | context.realtime_remote_time.time_since_epoch().count()); |
| 297 | break; |
| 298 | |
| 299 | case LogType::kLogMessage: |
| 300 | case LogType::kLogMessageAndDeliveryTime: |
| 301 | case LogType::kLogDeliveryTimeOnly: |
| 302 | message_header_builder.add_queue_index(context.queue_index); |
| 303 | message_header_builder.add_monotonic_sent_time( |
| 304 | context.monotonic_event_time.time_since_epoch().count()); |
| 305 | message_header_builder.add_realtime_sent_time( |
| 306 | context.realtime_event_time.time_since_epoch().count()); |
| 307 | break; |
| 308 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 309 | |
| 310 | switch (log_type) { |
| 311 | case LogType::kLogMessage: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 312 | case LogType::kLogRemoteMessage: |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 313 | message_header_builder.add_data(data_offset); |
| 314 | break; |
| 315 | |
| 316 | case LogType::kLogMessageAndDeliveryTime: |
| 317 | message_header_builder.add_data(data_offset); |
| 318 | [[fallthrough]]; |
| 319 | |
| 320 | case LogType::kLogDeliveryTimeOnly: |
| 321 | message_header_builder.add_monotonic_remote_time( |
| 322 | context.monotonic_remote_time.time_since_epoch().count()); |
| 323 | message_header_builder.add_realtime_remote_time( |
| 324 | context.realtime_remote_time.time_since_epoch().count()); |
| 325 | message_header_builder.add_remote_queue_index(context.remote_queue_index); |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | return message_header_builder.Finish(); |
| 330 | } |
| 331 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 332 | SpanReader::SpanReader(std::string_view filename) : filename_(filename) { |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 333 | decoder_ = std::make_unique<DummyDecoder>(filename); |
| 334 | |
| 335 | static constexpr std::string_view kXz = ".xz"; |
James Kuszmaul | dd0a504 | 2021-10-28 23:38:04 -0700 | [diff] [blame] | 336 | static constexpr std::string_view kSnappy = SnappyDecoder::kExtension; |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 337 | if (filename.substr(filename.size() - kXz.size()) == kXz) { |
| 338 | #if ENABLE_LZMA |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 339 | decoder_ = std::make_unique<ThreadedLzmaDecoder>(std::move(decoder_)); |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 340 | #else |
| 341 | LOG(FATAL) << "Reading xz-compressed files not supported on this platform"; |
| 342 | #endif |
James Kuszmaul | dd0a504 | 2021-10-28 23:38:04 -0700 | [diff] [blame] | 343 | } else if (filename.substr(filename.size() - kSnappy.size()) == kSnappy) { |
| 344 | decoder_ = std::make_unique<SnappyDecoder>(std::move(decoder_)); |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 345 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Austin Schuh | cf5f644 | 2021-07-06 10:43:28 -0700 | [diff] [blame] | 348 | absl::Span<const uint8_t> SpanReader::PeekMessage() { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 349 | // Make sure we have enough for the size. |
| 350 | if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) { |
| 351 | if (!ReadBlock()) { |
| 352 | return absl::Span<const uint8_t>(); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Now make sure we have enough for the message. |
| 357 | const size_t data_size = |
| 358 | flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) + |
| 359 | sizeof(flatbuffers::uoffset_t); |
Austin Schuh | e4fca83 | 2020-03-07 16:58:53 -0800 | [diff] [blame] | 360 | if (data_size == sizeof(flatbuffers::uoffset_t)) { |
| 361 | LOG(ERROR) << "Size of data is zero. Log file end is corrupted, skipping."; |
| 362 | LOG(ERROR) << " Rest of log file is " |
| 363 | << absl::BytesToHexString(std::string_view( |
| 364 | reinterpret_cast<const char *>(data_.data() + |
| 365 | consumed_data_), |
| 366 | data_.size() - consumed_data_)); |
| 367 | return absl::Span<const uint8_t>(); |
| 368 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 369 | while (data_.size() < consumed_data_ + data_size) { |
| 370 | if (!ReadBlock()) { |
| 371 | return absl::Span<const uint8_t>(); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | // And return it, consuming the data. |
| 376 | const uint8_t *data_ptr = data_.data() + consumed_data_; |
| 377 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 378 | return absl::Span<const uint8_t>(data_ptr, data_size); |
| 379 | } |
| 380 | |
Austin Schuh | cf5f644 | 2021-07-06 10:43:28 -0700 | [diff] [blame] | 381 | void SpanReader::ConsumeMessage() { |
| 382 | consumed_data_ += |
| 383 | flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) + |
| 384 | sizeof(flatbuffers::uoffset_t); |
| 385 | } |
| 386 | |
| 387 | absl::Span<const uint8_t> SpanReader::ReadMessage() { |
| 388 | absl::Span<const uint8_t> result = PeekMessage(); |
| 389 | if (result != absl::Span<const uint8_t>()) { |
| 390 | ConsumeMessage(); |
| 391 | } |
| 392 | return result; |
| 393 | } |
| 394 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 395 | bool SpanReader::ReadBlock() { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 396 | // This is the amount of data we grab at a time. Doing larger chunks minimizes |
| 397 | // syscalls and helps decompressors batch things more efficiently. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 398 | constexpr size_t kReadSize = 256 * 1024; |
| 399 | |
| 400 | // Strip off any unused data at the front. |
| 401 | if (consumed_data_ != 0) { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 402 | data_.erase_front(consumed_data_); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 403 | consumed_data_ = 0; |
| 404 | } |
| 405 | |
| 406 | const size_t starting_size = data_.size(); |
| 407 | |
| 408 | // This should automatically grow the backing store. It won't shrink if we |
| 409 | // get a small chunk later. This reduces allocations when we want to append |
| 410 | // more data. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 411 | data_.resize(starting_size + kReadSize); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 412 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 413 | const size_t count = |
| 414 | decoder_->Read(data_.begin() + starting_size, data_.end()); |
| 415 | data_.resize(starting_size + count); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 416 | if (count == 0) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 417 | return false; |
| 418 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 419 | |
| 420 | return true; |
| 421 | } |
| 422 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 423 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader( |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 424 | SpanReader *span_reader) { |
| 425 | absl::Span<const uint8_t> config_data = span_reader->ReadMessage(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 426 | |
| 427 | // Make sure something was read. |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 428 | if (config_data == absl::Span<const uint8_t>()) { |
| 429 | return std::nullopt; |
| 430 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 431 | |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 432 | // 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] | 433 | SizePrefixedFlatbufferVector<LogFileHeader> result(config_data); |
Austin Schuh | e09beb1 | 2020-12-11 20:04:27 -0800 | [diff] [blame] | 434 | if (!result.Verify()) { |
| 435 | return std::nullopt; |
| 436 | } |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 437 | |
| 438 | if (FLAGS_workaround_double_headers) { |
| 439 | while (true) { |
| 440 | absl::Span<const uint8_t> maybe_header_data = span_reader->PeekMessage(); |
| 441 | if (maybe_header_data == absl::Span<const uint8_t>()) { |
| 442 | break; |
| 443 | } |
| 444 | |
| 445 | aos::SizePrefixedFlatbufferSpan<aos::logger::LogFileHeader> maybe_header( |
| 446 | maybe_header_data); |
| 447 | if (maybe_header.Verify()) { |
| 448 | LOG(WARNING) << "Found duplicate LogFileHeader in " |
| 449 | << span_reader->filename(); |
| 450 | ResizeableBuffer header_data_copy; |
| 451 | header_data_copy.resize(maybe_header_data.size()); |
| 452 | memcpy(header_data_copy.data(), maybe_header_data.begin(), |
| 453 | header_data_copy.size()); |
| 454 | result = SizePrefixedFlatbufferVector<LogFileHeader>( |
| 455 | std::move(header_data_copy)); |
| 456 | |
| 457 | span_reader->ConsumeMessage(); |
| 458 | } else { |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | } |
Austin Schuh | e09beb1 | 2020-12-11 20:04:27 -0800 | [diff] [blame] | 463 | return result; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 466 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader( |
| 467 | std::string_view filename) { |
| 468 | SpanReader span_reader(filename); |
| 469 | return ReadHeader(&span_reader); |
| 470 | } |
| 471 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 472 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage( |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 473 | std::string_view filename, size_t n) { |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 474 | SpanReader span_reader(filename); |
| 475 | absl::Span<const uint8_t> data_span = span_reader.ReadMessage(); |
| 476 | for (size_t i = 0; i < n + 1; ++i) { |
| 477 | data_span = span_reader.ReadMessage(); |
| 478 | |
| 479 | // Make sure something was read. |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 480 | if (data_span == absl::Span<const uint8_t>()) { |
| 481 | return std::nullopt; |
| 482 | } |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 485 | // 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] | 486 | SizePrefixedFlatbufferVector<MessageHeader> result(data_span); |
Austin Schuh | e09beb1 | 2020-12-11 20:04:27 -0800 | [diff] [blame] | 487 | if (!result.Verify()) { |
| 488 | return std::nullopt; |
| 489 | } |
| 490 | return result; |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 493 | MessageReader::MessageReader(std::string_view filename) |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 494 | : span_reader_(filename), |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 495 | raw_log_file_header_( |
| 496 | SizePrefixedFlatbufferVector<LogFileHeader>::Empty()) { |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 497 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> |
| 498 | raw_log_file_header = ReadHeader(&span_reader_); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 499 | |
| 500 | // Make sure something was read. |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 501 | CHECK(raw_log_file_header) << ": Failed to read header from: " << filename; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 502 | |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 503 | raw_log_file_header_ = std::move(*raw_log_file_header); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 504 | |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 505 | CHECK(raw_log_file_header_.Verify()) << "Log file header is corrupted"; |
| 506 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 507 | max_out_of_order_duration_ = |
Austin Schuh | a040c3f | 2021-02-13 16:09:07 -0800 | [diff] [blame] | 508 | FLAGS_max_out_of_order > 0 |
| 509 | ? chrono::duration_cast<chrono::nanoseconds>( |
| 510 | chrono::duration<double>(FLAGS_max_out_of_order)) |
| 511 | : chrono::nanoseconds(log_file_header()->max_out_of_order_duration()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 512 | |
| 513 | VLOG(1) << "Opened " << filename << " as node " |
| 514 | << FlatbufferToJson(log_file_header()->node()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 515 | } |
| 516 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 517 | std::shared_ptr<UnpackedMessageHeader> MessageReader::ReadMessage() { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 518 | absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage(); |
| 519 | if (msg_data == absl::Span<const uint8_t>()) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 520 | return nullptr; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 523 | SizePrefixedFlatbufferSpan<MessageHeader> msg(msg_data); |
| 524 | CHECK(msg.Verify()) << ": Corrupted message from " << filename(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 525 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 526 | auto result = UnpackedMessageHeader::MakeMessage(msg.message()); |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 527 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 528 | const monotonic_clock::time_point timestamp = result->monotonic_sent_time; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 529 | |
| 530 | newest_timestamp_ = std::max(newest_timestamp_, timestamp); |
Austin Schuh | d187329 | 2021-11-18 15:35:30 -0800 | [diff] [blame^] | 531 | |
| 532 | if (VLOG_IS_ON(3)) { |
| 533 | VLOG(3) << "Read from " << filename() << " data " << FlatbufferToJson(msg); |
| 534 | } else if (VLOG_IS_ON(2)) { |
| 535 | SizePrefixedFlatbufferVector<MessageHeader> msg_copy = msg; |
| 536 | msg_copy.mutable_message()->clear_data(); |
| 537 | VLOG(2) << "Read from " << filename() << " data " |
| 538 | << FlatbufferToJson(msg_copy); |
| 539 | } |
| 540 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 541 | return result; |
| 542 | } |
| 543 | |
| 544 | std::shared_ptr<UnpackedMessageHeader> UnpackedMessageHeader::MakeMessage( |
| 545 | const MessageHeader &message) { |
| 546 | const size_t data_size = message.has_data() ? message.data()->size() : 0; |
| 547 | |
| 548 | UnpackedMessageHeader *const unpacked_message = |
| 549 | reinterpret_cast<UnpackedMessageHeader *>( |
| 550 | malloc(sizeof(UnpackedMessageHeader) + data_size + |
| 551 | kChannelDataAlignment - 1)); |
| 552 | |
| 553 | CHECK(message.has_channel_index()); |
| 554 | CHECK(message.has_monotonic_sent_time()); |
| 555 | |
| 556 | absl::Span<uint8_t> span; |
| 557 | if (data_size > 0) { |
| 558 | span = |
| 559 | absl::Span<uint8_t>(reinterpret_cast<uint8_t *>(RoundChannelData( |
| 560 | &unpacked_message->actual_data[0], data_size)), |
| 561 | data_size); |
| 562 | } |
| 563 | |
| 564 | std::optional<std::chrono::nanoseconds> monotonic_remote_time; |
| 565 | if (message.has_monotonic_remote_time()) { |
| 566 | monotonic_remote_time = |
| 567 | std::chrono::nanoseconds(message.monotonic_remote_time()); |
| 568 | } |
| 569 | std::optional<realtime_clock::time_point> realtime_remote_time; |
| 570 | if (message.has_realtime_remote_time()) { |
| 571 | realtime_remote_time = realtime_clock::time_point( |
| 572 | chrono::nanoseconds(message.realtime_remote_time())); |
| 573 | } |
| 574 | |
| 575 | std::optional<uint32_t> remote_queue_index; |
| 576 | if (message.has_remote_queue_index()) { |
| 577 | remote_queue_index = message.remote_queue_index(); |
| 578 | } |
| 579 | |
| 580 | new (unpacked_message) UnpackedMessageHeader{ |
| 581 | .channel_index = message.channel_index(), |
| 582 | .monotonic_sent_time = monotonic_clock::time_point( |
| 583 | chrono::nanoseconds(message.monotonic_sent_time())), |
| 584 | .realtime_sent_time = realtime_clock::time_point( |
| 585 | chrono::nanoseconds(message.realtime_sent_time())), |
| 586 | .queue_index = message.queue_index(), |
| 587 | .monotonic_remote_time = monotonic_remote_time, |
| 588 | .realtime_remote_time = realtime_remote_time, |
| 589 | .remote_queue_index = remote_queue_index, |
| 590 | .monotonic_timestamp_time = monotonic_clock::time_point( |
| 591 | std::chrono::nanoseconds(message.monotonic_timestamp_time())), |
| 592 | .has_monotonic_timestamp_time = message.has_monotonic_timestamp_time(), |
| 593 | .span = span}; |
| 594 | |
| 595 | if (data_size > 0) { |
| 596 | memcpy(span.data(), message.data()->data(), data_size); |
| 597 | } |
| 598 | |
| 599 | return std::shared_ptr<UnpackedMessageHeader>(unpacked_message, |
| 600 | &DestroyAndFree); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 603 | PartsMessageReader::PartsMessageReader(LogParts log_parts) |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 604 | : parts_(std::move(log_parts)), message_reader_(parts_.parts[0]) { |
Brian Silverman | fee1697 | 2021-09-14 12:06:38 -0700 | [diff] [blame] | 605 | if (parts_.parts.size() >= 2) { |
| 606 | next_message_reader_.emplace(parts_.parts[1]); |
| 607 | } |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 608 | ComputeBootCounts(); |
| 609 | } |
| 610 | |
| 611 | void PartsMessageReader::ComputeBootCounts() { |
| 612 | boot_counts_.assign(configuration::NodesCount(parts_.config.get()), |
| 613 | std::nullopt); |
| 614 | |
| 615 | // We have 3 vintages of log files with different amounts of information. |
| 616 | if (log_file_header()->has_boot_uuids()) { |
| 617 | // The new hotness with the boots explicitly listed out. We can use the log |
| 618 | // file header to compute the boot count of all relevant nodes. |
| 619 | CHECK_EQ(log_file_header()->boot_uuids()->size(), boot_counts_.size()); |
| 620 | size_t node_index = 0; |
| 621 | for (const flatbuffers::String *boot_uuid : |
| 622 | *log_file_header()->boot_uuids()) { |
| 623 | CHECK(parts_.boots); |
| 624 | if (boot_uuid->size() != 0) { |
| 625 | auto it = parts_.boots->boot_count_map.find(boot_uuid->str()); |
| 626 | if (it != parts_.boots->boot_count_map.end()) { |
| 627 | boot_counts_[node_index] = it->second; |
| 628 | } |
| 629 | } else if (parts().boots->boots[node_index].size() == 1u) { |
| 630 | boot_counts_[node_index] = 0; |
| 631 | } |
| 632 | ++node_index; |
| 633 | } |
| 634 | } else { |
| 635 | // Older multi-node logs which are guarenteed to have UUIDs logged, or |
| 636 | // single node log files with boot UUIDs in the header. We only know how to |
| 637 | // order certain boots in certain circumstances. |
| 638 | if (configuration::MultiNode(parts_.config.get()) || parts_.boots) { |
| 639 | for (size_t node_index = 0; node_index < boot_counts_.size(); |
| 640 | ++node_index) { |
| 641 | CHECK(parts_.boots); |
| 642 | if (parts().boots->boots[node_index].size() == 1u) { |
| 643 | boot_counts_[node_index] = 0; |
| 644 | } |
| 645 | } |
| 646 | } else { |
| 647 | // Really old single node logs without any UUIDs. They can't reboot. |
| 648 | CHECK_EQ(boot_counts_.size(), 1u); |
| 649 | boot_counts_[0] = 0u; |
| 650 | } |
| 651 | } |
| 652 | } |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 653 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 654 | std::shared_ptr<UnpackedMessageHeader> PartsMessageReader::ReadMessage() { |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 655 | while (!done_) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 656 | std::shared_ptr<UnpackedMessageHeader> message = |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 657 | message_reader_.ReadMessage(); |
| 658 | if (message) { |
| 659 | newest_timestamp_ = message_reader_.newest_timestamp(); |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 660 | const monotonic_clock::time_point monotonic_sent_time = |
| 661 | message->monotonic_sent_time; |
| 662 | |
| 663 | // TODO(austin): Does this work with startup? Might need to use the |
| 664 | // start time. |
| 665 | // TODO(austin): Does this work with startup when we don't know the |
| 666 | // remote start time too? Look at one of those logs to compare. |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 667 | if (monotonic_sent_time > |
| 668 | parts_.monotonic_start_time + max_out_of_order_duration()) { |
| 669 | after_start_ = true; |
| 670 | } |
| 671 | if (after_start_) { |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 672 | CHECK_GE(monotonic_sent_time, |
| 673 | newest_timestamp_ - max_out_of_order_duration()) |
Austin Schuh | a040c3f | 2021-02-13 16:09:07 -0800 | [diff] [blame] | 674 | << ": Max out of order of " << max_out_of_order_duration().count() |
| 675 | << "ns exceeded. " << parts_ << ", start time is " |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 676 | << parts_.monotonic_start_time << " currently reading " |
| 677 | << filename(); |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 678 | } |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 679 | return message; |
| 680 | } |
| 681 | NextLog(); |
| 682 | } |
Austin Schuh | 32f6849 | 2020-11-08 21:45:51 -0800 | [diff] [blame] | 683 | newest_timestamp_ = monotonic_clock::max_time; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 684 | return nullptr; |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void PartsMessageReader::NextLog() { |
| 688 | if (next_part_index_ == parts_.parts.size()) { |
Brian Silverman | fee1697 | 2021-09-14 12:06:38 -0700 | [diff] [blame] | 689 | CHECK(!next_message_reader_); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 690 | done_ = true; |
| 691 | return; |
| 692 | } |
Brian Silverman | fee1697 | 2021-09-14 12:06:38 -0700 | [diff] [blame] | 693 | CHECK(next_message_reader_); |
| 694 | message_reader_ = std::move(*next_message_reader_); |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 695 | ComputeBootCounts(); |
Brian Silverman | fee1697 | 2021-09-14 12:06:38 -0700 | [diff] [blame] | 696 | if (next_part_index_ + 1 < parts_.parts.size()) { |
| 697 | next_message_reader_.emplace(parts_.parts[next_part_index_ + 1]); |
| 698 | } else { |
| 699 | next_message_reader_.reset(); |
| 700 | } |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 701 | ++next_part_index_; |
| 702 | } |
| 703 | |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 704 | bool Message::operator<(const Message &m2) const { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 705 | CHECK_EQ(this->timestamp.boot, m2.timestamp.boot); |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 706 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 707 | if (this->timestamp.time < m2.timestamp.time) { |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 708 | return true; |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 709 | } else if (this->timestamp.time > m2.timestamp.time) { |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 710 | return false; |
| 711 | } |
| 712 | |
| 713 | if (this->channel_index < m2.channel_index) { |
| 714 | return true; |
| 715 | } else if (this->channel_index > m2.channel_index) { |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | return this->queue_index < m2.queue_index; |
| 720 | } |
| 721 | |
| 722 | bool Message::operator>=(const Message &m2) const { return !(*this < m2); } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 723 | bool Message::operator==(const Message &m2) const { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 724 | CHECK_EQ(this->timestamp.boot, m2.timestamp.boot); |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 725 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 726 | return timestamp.time == m2.timestamp.time && |
| 727 | channel_index == m2.channel_index && queue_index == m2.queue_index; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 728 | } |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 729 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 730 | std::ostream &operator<<(std::ostream &os, const UnpackedMessageHeader &m) { |
| 731 | os << "{.channel_index=" << m.channel_index |
| 732 | << ", .monotonic_sent_time=" << m.monotonic_sent_time |
| 733 | << ", .realtime_sent_time=" << m.realtime_sent_time |
| 734 | << ", .queue_index=" << m.queue_index; |
| 735 | if (m.monotonic_remote_time) { |
| 736 | os << ", .monotonic_remote_time=" << m.monotonic_remote_time->count(); |
| 737 | } |
| 738 | os << ", .realtime_remote_time="; |
| 739 | PrintOptionalOrNull(&os, m.realtime_remote_time); |
| 740 | os << ", .remote_queue_index="; |
| 741 | PrintOptionalOrNull(&os, m.remote_queue_index); |
| 742 | if (m.has_monotonic_timestamp_time) { |
| 743 | os << ", .monotonic_timestamp_time=" << m.monotonic_timestamp_time; |
| 744 | } |
| 745 | return os; |
| 746 | } |
| 747 | |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 748 | std::ostream &operator<<(std::ostream &os, const Message &m) { |
| 749 | os << "{.channel_index=" << m.channel_index |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 750 | << ", .queue_index=" << m.queue_index << ", .timestamp=" << m.timestamp; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 751 | if (m.data != nullptr) { |
Austin Schuh | fb1b329 | 2021-11-16 21:20:15 -0800 | [diff] [blame] | 752 | os << ", .data=" << m.data; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 753 | } |
| 754 | os << "}"; |
| 755 | return os; |
| 756 | } |
| 757 | |
| 758 | std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m) { |
| 759 | os << "{.channel_index=" << m.channel_index |
| 760 | << ", .queue_index=" << m.queue_index |
| 761 | << ", .monotonic_event_time=" << m.monotonic_event_time |
| 762 | << ", .realtime_event_time=" << m.realtime_event_time; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 763 | if (m.remote_queue_index != BootQueueIndex::Invalid()) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 764 | os << ", .remote_queue_index=" << m.remote_queue_index; |
| 765 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 766 | if (m.monotonic_remote_time != BootTimestamp::min_time()) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 767 | os << ", .monotonic_remote_time=" << m.monotonic_remote_time; |
| 768 | } |
| 769 | if (m.realtime_remote_time != realtime_clock::min_time) { |
| 770 | os << ", .realtime_remote_time=" << m.realtime_remote_time; |
| 771 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 772 | if (m.monotonic_timestamp_time != BootTimestamp::min_time()) { |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 773 | os << ", .monotonic_timestamp_time=" << m.monotonic_timestamp_time; |
| 774 | } |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 775 | if (m.data != nullptr) { |
| 776 | os << ", .data=" << *m.data; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 777 | } |
| 778 | os << "}"; |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 779 | return os; |
| 780 | } |
| 781 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 782 | LogPartsSorter::LogPartsSorter(LogParts log_parts) |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 783 | : parts_message_reader_(log_parts), |
| 784 | source_node_index_(configuration::SourceNodeIndex(parts().config.get())) { |
| 785 | } |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 786 | |
| 787 | Message *LogPartsSorter::Front() { |
| 788 | // Queue up data until enough data has been queued that the front message is |
| 789 | // sorted enough to be safe to pop. This may do nothing, so we should make |
| 790 | // sure the nothing path is checked quickly. |
| 791 | if (sorted_until() != monotonic_clock::max_time) { |
| 792 | while (true) { |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 793 | if (!messages_.empty() && |
| 794 | messages_.begin()->timestamp.time < sorted_until() && |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 795 | sorted_until() >= monotonic_start_time()) { |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 796 | break; |
| 797 | } |
| 798 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 799 | std::shared_ptr<UnpackedMessageHeader> m = |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 800 | parts_message_reader_.ReadMessage(); |
| 801 | // No data left, sorted forever, work through what is left. |
| 802 | if (!m) { |
| 803 | sorted_until_ = monotonic_clock::max_time; |
| 804 | break; |
| 805 | } |
| 806 | |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 807 | size_t monotonic_timestamp_boot = 0; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 808 | if (m->has_monotonic_timestamp_time) { |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 809 | monotonic_timestamp_boot = parts().logger_boot_count; |
| 810 | } |
| 811 | size_t monotonic_remote_boot = 0xffffff; |
| 812 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 813 | if (m->monotonic_remote_time.has_value()) { |
milind-u | a50344f | 2021-08-25 18:22:20 -0700 | [diff] [blame] | 814 | const Node *node = parts().config->nodes()->Get( |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 815 | source_node_index_[m->channel_index]); |
milind-u | a50344f | 2021-08-25 18:22:20 -0700 | [diff] [blame] | 816 | |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 817 | std::optional<size_t> boot = parts_message_reader_.boot_count( |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 818 | source_node_index_[m->channel_index]); |
milind-u | a50344f | 2021-08-25 18:22:20 -0700 | [diff] [blame] | 819 | CHECK(boot) << ": Failed to find boot for node " << MaybeNodeName(node) |
| 820 | << ", with index " |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 821 | << source_node_index_[m->channel_index]; |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 822 | monotonic_remote_boot = *boot; |
| 823 | } |
| 824 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 825 | messages_.insert( |
| 826 | Message{.channel_index = m->channel_index, |
| 827 | .queue_index = BootQueueIndex{.boot = parts().boot_count, |
| 828 | .index = m->queue_index}, |
| 829 | .timestamp = BootTimestamp{.boot = parts().boot_count, |
| 830 | .time = m->monotonic_sent_time}, |
| 831 | .monotonic_remote_boot = monotonic_remote_boot, |
| 832 | .monotonic_timestamp_boot = monotonic_timestamp_boot, |
| 833 | .data = std::move(m)}); |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 834 | |
| 835 | // Now, update sorted_until_ to match the new message. |
| 836 | if (parts_message_reader_.newest_timestamp() > |
| 837 | monotonic_clock::min_time + |
| 838 | parts_message_reader_.max_out_of_order_duration()) { |
| 839 | sorted_until_ = parts_message_reader_.newest_timestamp() - |
| 840 | parts_message_reader_.max_out_of_order_duration(); |
| 841 | } else { |
| 842 | sorted_until_ = monotonic_clock::min_time; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | // Now that we have enough data queued, return a pointer to the oldest piece |
| 848 | // of data if it exists. |
| 849 | if (messages_.empty()) { |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 850 | last_message_time_ = monotonic_clock::max_time; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 851 | return nullptr; |
| 852 | } |
| 853 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 854 | CHECK_GE(messages_.begin()->timestamp.time, last_message_time_) |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 855 | << DebugString() << " reading " << parts_message_reader_.filename(); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 856 | last_message_time_ = messages_.begin()->timestamp.time; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 857 | return &(*messages_.begin()); |
| 858 | } |
| 859 | |
| 860 | void LogPartsSorter::PopFront() { messages_.erase(messages_.begin()); } |
| 861 | |
| 862 | std::string LogPartsSorter::DebugString() const { |
| 863 | std::stringstream ss; |
| 864 | ss << "messages: [\n"; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 865 | int count = 0; |
| 866 | bool no_dots = true; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 867 | for (const Message &m : messages_) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 868 | if (count < 15 || count > static_cast<int>(messages_.size()) - 15) { |
| 869 | ss << m << "\n"; |
| 870 | } else if (no_dots) { |
| 871 | ss << "...\n"; |
| 872 | no_dots = false; |
| 873 | } |
| 874 | ++count; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 875 | } |
| 876 | ss << "] <- " << parts_message_reader_.filename(); |
| 877 | return ss.str(); |
| 878 | } |
| 879 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 880 | NodeMerger::NodeMerger(std::vector<LogParts> parts) { |
| 881 | CHECK_GE(parts.size(), 1u); |
Austin Schuh | 715adc1 | 2021-06-29 22:07:39 -0700 | [diff] [blame] | 882 | // Enforce that we are sorting things only from a single node from a single |
| 883 | // boot. |
| 884 | const std::string_view part0_node = parts[0].node; |
| 885 | 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] | 886 | for (size_t i = 1; i < parts.size(); ++i) { |
| 887 | CHECK_EQ(part0_node, parts[i].node) << ": Can't merge different nodes."; |
Austin Schuh | 715adc1 | 2021-06-29 22:07:39 -0700 | [diff] [blame] | 888 | CHECK_EQ(part0_source_boot_uuid, parts[i].source_boot_uuid) |
| 889 | << ": Can't merge different boots."; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 890 | } |
Austin Schuh | 715adc1 | 2021-06-29 22:07:39 -0700 | [diff] [blame] | 891 | |
| 892 | node_ = configuration::GetNodeIndex(parts[0].config.get(), part0_node); |
| 893 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 894 | for (LogParts &part : parts) { |
| 895 | parts_sorters_.emplace_back(std::move(part)); |
| 896 | } |
| 897 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 898 | monotonic_start_time_ = monotonic_clock::max_time; |
Austin Schuh | 9dc4261 | 2021-09-20 20:41:29 -0700 | [diff] [blame] | 899 | realtime_start_time_ = realtime_clock::min_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 900 | for (const LogPartsSorter &parts_sorter : parts_sorters_) { |
Sanjay Narayanan | 9896c75 | 2021-09-01 16:16:48 -0700 | [diff] [blame] | 901 | // We want to capture the earliest meaningful start time here. The start |
| 902 | // time defaults to min_time when there's no meaningful value to report, so |
| 903 | // let's ignore those. |
Austin Schuh | 9dc4261 | 2021-09-20 20:41:29 -0700 | [diff] [blame] | 904 | if (parts_sorter.monotonic_start_time() != monotonic_clock::min_time) { |
| 905 | bool accept = false; |
| 906 | // We want to prioritize start times from the logger node. Really, we |
| 907 | // want to prioritize start times with a valid realtime_clock time. So, |
| 908 | // if we have a start time without a RT clock, prefer a start time with a |
| 909 | // RT clock, even it if is later. |
| 910 | if (parts_sorter.realtime_start_time() != realtime_clock::min_time) { |
| 911 | // We've got a good one. See if the current start time has a good RT |
| 912 | // clock, or if we should use this one instead. |
| 913 | if (parts_sorter.monotonic_start_time() < monotonic_start_time_) { |
| 914 | accept = true; |
| 915 | } else if (realtime_start_time_ == realtime_clock::min_time) { |
| 916 | // The previous start time doesn't have a good RT time, so it is very |
| 917 | // likely the start time from a remote part file. We just found a |
| 918 | // better start time with a real RT time, so switch to that instead. |
| 919 | accept = true; |
| 920 | } |
| 921 | } else if (realtime_start_time_ == realtime_clock::min_time) { |
| 922 | // We don't have a RT time, so take the oldest. |
| 923 | if (parts_sorter.monotonic_start_time() < monotonic_start_time_) { |
| 924 | accept = true; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | if (accept) { |
| 929 | monotonic_start_time_ = parts_sorter.monotonic_start_time(); |
| 930 | realtime_start_time_ = parts_sorter.realtime_start_time(); |
| 931 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 932 | } |
| 933 | } |
Sanjay Narayanan | 9896c75 | 2021-09-01 16:16:48 -0700 | [diff] [blame] | 934 | |
| 935 | // If there was no meaningful start time reported, just use min_time. |
| 936 | if (monotonic_start_time_ == monotonic_clock::max_time) { |
| 937 | monotonic_start_time_ = monotonic_clock::min_time; |
| 938 | realtime_start_time_ = realtime_clock::min_time; |
| 939 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 940 | } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 941 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 942 | std::vector<const LogParts *> NodeMerger::Parts() const { |
| 943 | std::vector<const LogParts *> p; |
| 944 | p.reserve(parts_sorters_.size()); |
| 945 | for (const LogPartsSorter &parts_sorter : parts_sorters_) { |
| 946 | p.emplace_back(&parts_sorter.parts()); |
| 947 | } |
| 948 | return p; |
| 949 | } |
| 950 | |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 951 | Message *NodeMerger::Front() { |
| 952 | // Return the current Front if we have one, otherwise go compute one. |
| 953 | if (current_ != nullptr) { |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 954 | Message *result = current_->Front(); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 955 | CHECK_GE(result->timestamp.time, last_message_time_); |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 956 | return result; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | // Otherwise, do a simple search for the oldest message, deduplicating any |
| 960 | // duplicates. |
| 961 | Message *oldest = nullptr; |
| 962 | sorted_until_ = monotonic_clock::max_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 963 | for (LogPartsSorter &parts_sorter : parts_sorters_) { |
| 964 | Message *m = parts_sorter.Front(); |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 965 | if (!m) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 966 | sorted_until_ = std::min(sorted_until_, parts_sorter.sorted_until()); |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 967 | continue; |
| 968 | } |
| 969 | if (oldest == nullptr || *m < *oldest) { |
| 970 | oldest = m; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 971 | current_ = &parts_sorter; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 972 | } else if (*m == *oldest) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 973 | // Found a duplicate. If there is a choice, we want the one which has |
| 974 | // the timestamp time. |
| 975 | if (!m->data->has_monotonic_timestamp_time) { |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 976 | parts_sorter.PopFront(); |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 977 | } else if (!oldest->data->has_monotonic_timestamp_time) { |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 978 | current_->PopFront(); |
| 979 | current_ = &parts_sorter; |
| 980 | oldest = m; |
| 981 | } else { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 982 | CHECK_EQ(m->data->monotonic_timestamp_time, |
| 983 | oldest->data->monotonic_timestamp_time); |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 984 | parts_sorter.PopFront(); |
| 985 | } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | // PopFront may change this, so compute it down here. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 989 | sorted_until_ = std::min(sorted_until_, parts_sorter.sorted_until()); |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 992 | if (oldest) { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 993 | CHECK_GE(oldest->timestamp.time, last_message_time_); |
| 994 | last_message_time_ = oldest->timestamp.time; |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 995 | } else { |
| 996 | last_message_time_ = monotonic_clock::max_time; |
| 997 | } |
| 998 | |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 999 | // Return the oldest message found. This will be nullptr if nothing was |
| 1000 | // found, indicating there is nothing left. |
| 1001 | return oldest; |
| 1002 | } |
| 1003 | |
| 1004 | void NodeMerger::PopFront() { |
| 1005 | CHECK(current_ != nullptr) << "Popping before calling Front()"; |
| 1006 | current_->PopFront(); |
| 1007 | current_ = nullptr; |
| 1008 | } |
| 1009 | |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1010 | BootMerger::BootMerger(std::vector<LogParts> files) { |
| 1011 | std::vector<std::vector<LogParts>> boots; |
| 1012 | |
| 1013 | // Now, we need to split things out by boot. |
| 1014 | for (size_t i = 0; i < files.size(); ++i) { |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1015 | const size_t boot_count = files[i].boot_count; |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1016 | if (boot_count + 1 > boots.size()) { |
| 1017 | boots.resize(boot_count + 1); |
| 1018 | } |
| 1019 | boots[boot_count].emplace_back(std::move(files[i])); |
| 1020 | } |
| 1021 | |
| 1022 | node_mergers_.reserve(boots.size()); |
| 1023 | for (size_t i = 0; i < boots.size(); ++i) { |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1024 | VLOG(2) << "Boot " << i; |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1025 | for (auto &p : boots[i]) { |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1026 | VLOG(2) << "Part " << p; |
Austin Schuh | f16ef6a | 2021-06-30 21:48:17 -0700 | [diff] [blame] | 1027 | } |
| 1028 | node_mergers_.emplace_back( |
| 1029 | std::make_unique<NodeMerger>(std::move(boots[i]))); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | Message *BootMerger::Front() { |
| 1034 | Message *result = node_mergers_[index_]->Front(); |
| 1035 | |
| 1036 | if (result != nullptr) { |
| 1037 | return result; |
| 1038 | } |
| 1039 | |
| 1040 | if (index_ + 1u == node_mergers_.size()) { |
| 1041 | // At the end of the last node merger, just return. |
| 1042 | return nullptr; |
| 1043 | } else { |
| 1044 | ++index_; |
| 1045 | return Front(); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | void BootMerger::PopFront() { node_mergers_[index_]->PopFront(); } |
| 1050 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1051 | std::vector<const LogParts *> BootMerger::Parts() const { |
| 1052 | std::vector<const LogParts *> results; |
| 1053 | for (const std::unique_ptr<NodeMerger> &node_merger : node_mergers_) { |
| 1054 | std::vector<const LogParts *> node_parts = node_merger->Parts(); |
| 1055 | |
| 1056 | results.insert(results.end(), std::make_move_iterator(node_parts.begin()), |
| 1057 | std::make_move_iterator(node_parts.end())); |
| 1058 | } |
| 1059 | |
| 1060 | return results; |
| 1061 | } |
| 1062 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1063 | TimestampMapper::TimestampMapper(std::vector<LogParts> parts) |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1064 | : boot_merger_(std::move(parts)), |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1065 | timestamp_callback_([](TimestampedMessage *) {}) { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1066 | for (const LogParts *part : boot_merger_.Parts()) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 1067 | if (!configuration_) { |
| 1068 | configuration_ = part->config; |
| 1069 | } else { |
| 1070 | CHECK_EQ(configuration_.get(), part->config.get()); |
| 1071 | } |
| 1072 | } |
| 1073 | const Configuration *config = configuration_.get(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1074 | // Only fill out nodes_data_ if there are nodes. Otherwise everything gets |
| 1075 | // pretty simple. |
| 1076 | if (configuration::MultiNode(config)) { |
| 1077 | nodes_data_.resize(config->nodes()->size()); |
| 1078 | const Node *my_node = config->nodes()->Get(node()); |
| 1079 | for (size_t node_index = 0; node_index < nodes_data_.size(); ++node_index) { |
| 1080 | const Node *node = config->nodes()->Get(node_index); |
| 1081 | NodeData *node_data = &nodes_data_[node_index]; |
| 1082 | node_data->channels.resize(config->channels()->size()); |
| 1083 | // We should save the channel if it is delivered to the node represented |
| 1084 | // by the NodeData, but not sent by that node. That combo means it is |
| 1085 | // forwarded. |
| 1086 | size_t channel_index = 0; |
| 1087 | node_data->any_delivered = false; |
| 1088 | for (const Channel *channel : *config->channels()) { |
| 1089 | node_data->channels[channel_index].delivered = |
| 1090 | configuration::ChannelIsReadableOnNode(channel, node) && |
Austin Schuh | b3dbb6d | 2021-01-02 17:29:35 -0800 | [diff] [blame] | 1091 | configuration::ChannelIsSendableOnNode(channel, my_node) && |
| 1092 | (my_node != node); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1093 | node_data->any_delivered = node_data->any_delivered || |
| 1094 | node_data->channels[channel_index].delivered; |
| 1095 | ++channel_index; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | for (const Channel *channel : *config->channels()) { |
| 1100 | source_node_.emplace_back(configuration::GetNodeIndex( |
| 1101 | config, channel->source_node()->string_view())); |
| 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | void TimestampMapper::AddPeer(TimestampMapper *timestamp_mapper) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 1107 | CHECK(configuration::MultiNode(configuration())); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1108 | CHECK_NE(timestamp_mapper->node(), node()); |
| 1109 | CHECK_LT(timestamp_mapper->node(), nodes_data_.size()); |
| 1110 | |
| 1111 | NodeData *node_data = &nodes_data_[timestamp_mapper->node()]; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1112 | // 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] | 1113 | // we could needlessly save data. |
| 1114 | if (node_data->any_delivered) { |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1115 | VLOG(1) << "Registering on node " << node() << " for peer node " |
| 1116 | << timestamp_mapper->node(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1117 | CHECK(timestamp_mapper->nodes_data_[node()].peer == nullptr); |
| 1118 | |
| 1119 | timestamp_mapper->nodes_data_[node()].peer = this; |
Austin Schuh | 36c0093 | 2021-07-19 18:13:21 -0700 | [diff] [blame] | 1120 | |
| 1121 | node_data->save_for_peer = true; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1125 | void TimestampMapper::QueueMessage(Message *m) { |
| 1126 | matched_messages_.emplace_back(TimestampedMessage{ |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1127 | .channel_index = m->channel_index, |
| 1128 | .queue_index = m->queue_index, |
| 1129 | .monotonic_event_time = m->timestamp, |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1130 | .realtime_event_time = m->data->realtime_sent_time, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1131 | .remote_queue_index = BootQueueIndex::Invalid(), |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1132 | .monotonic_remote_time = BootTimestamp::min_time(), |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1133 | .realtime_remote_time = realtime_clock::min_time, |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1134 | .monotonic_timestamp_time = BootTimestamp::min_time(), |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1135 | .data = std::move(m->data)}); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | TimestampedMessage *TimestampMapper::Front() { |
| 1139 | // No need to fetch anything new. A previous message still exists. |
| 1140 | switch (first_message_) { |
| 1141 | case FirstMessage::kNeedsUpdate: |
| 1142 | break; |
| 1143 | case FirstMessage::kInMessage: |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1144 | return &matched_messages_.front(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1145 | case FirstMessage::kNullptr: |
| 1146 | return nullptr; |
| 1147 | } |
| 1148 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1149 | if (matched_messages_.empty()) { |
| 1150 | if (!QueueMatched()) { |
| 1151 | first_message_ = FirstMessage::kNullptr; |
| 1152 | return nullptr; |
| 1153 | } |
| 1154 | } |
| 1155 | first_message_ = FirstMessage::kInMessage; |
| 1156 | return &matched_messages_.front(); |
| 1157 | } |
| 1158 | |
| 1159 | bool TimestampMapper::QueueMatched() { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1160 | if (nodes_data_.empty()) { |
| 1161 | // Simple path. We are single node, so there are no timestamps to match! |
| 1162 | CHECK_EQ(messages_.size(), 0u); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1163 | Message *m = boot_merger_.Front(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1164 | if (!m) { |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1165 | return false; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1166 | } |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1167 | // Enqueue this message into matched_messages_ so we have a place to |
| 1168 | // associate remote timestamps, and return it. |
| 1169 | QueueMessage(m); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1170 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1171 | CHECK_GE(matched_messages_.back().monotonic_event_time, last_message_time_); |
| 1172 | last_message_time_ = matched_messages_.back().monotonic_event_time; |
| 1173 | |
| 1174 | // We are thin wrapper around node_merger. Call it directly. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1175 | boot_merger_.PopFront(); |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1176 | timestamp_callback_(&matched_messages_.back()); |
| 1177 | return true; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1180 | // We need to only add messages to the list so they get processed for |
| 1181 | // messages which are delivered. Reuse the flow below which uses messages_ |
| 1182 | // by just adding the new message to messages_ and continuing. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1183 | if (messages_.empty()) { |
| 1184 | if (!Queue()) { |
| 1185 | // Found nothing to add, we are out of data! |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1186 | return false; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1189 | // Now that it has been added (and cannibalized), forget about it |
| 1190 | // upstream. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1191 | boot_merger_.PopFront(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | Message *m = &(messages_.front()); |
| 1195 | |
| 1196 | if (source_node_[m->channel_index] == node()) { |
| 1197 | // 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] | 1198 | QueueMessage(m); |
| 1199 | CHECK_GE(matched_messages_.back().monotonic_event_time, last_message_time_); |
| 1200 | last_message_time_ = matched_messages_.back().monotonic_event_time; |
| 1201 | messages_.pop_front(); |
| 1202 | timestamp_callback_(&matched_messages_.back()); |
| 1203 | return true; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1204 | } else { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1205 | // Got a timestamp, find the matching remote data, match it, and return |
| 1206 | // it. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1207 | Message data = MatchingMessageFor(*m); |
| 1208 | |
| 1209 | // Return the data from the remote. The local message only has timestamp |
| 1210 | // info which isn't relevant anymore once extracted. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1211 | matched_messages_.emplace_back(TimestampedMessage{ |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1212 | .channel_index = m->channel_index, |
| 1213 | .queue_index = m->queue_index, |
| 1214 | .monotonic_event_time = m->timestamp, |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1215 | .realtime_event_time = m->data->realtime_sent_time, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1216 | .remote_queue_index = |
| 1217 | BootQueueIndex{.boot = m->monotonic_remote_boot, |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1218 | .index = m->data->remote_queue_index.value()}, |
| 1219 | .monotonic_remote_time = {m->monotonic_remote_boot, |
| 1220 | monotonic_clock::time_point( |
| 1221 | m->data->monotonic_remote_time.value())}, |
| 1222 | .realtime_remote_time = m->data->realtime_remote_time.value(), |
| 1223 | .monotonic_timestamp_time = {m->monotonic_timestamp_boot, |
| 1224 | m->data->monotonic_timestamp_time}, |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1225 | .data = std::move(data.data)}); |
| 1226 | CHECK_GE(matched_messages_.back().monotonic_event_time, last_message_time_); |
| 1227 | last_message_time_ = matched_messages_.back().monotonic_event_time; |
| 1228 | // Since messages_ holds the data, drop it. |
| 1229 | messages_.pop_front(); |
| 1230 | timestamp_callback_(&matched_messages_.back()); |
| 1231 | return true; |
| 1232 | } |
| 1233 | } |
| 1234 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1235 | void TimestampMapper::QueueUntil(BootTimestamp queue_time) { |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1236 | while (last_message_time_ <= queue_time) { |
| 1237 | if (!QueueMatched()) { |
| 1238 | return; |
| 1239 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1243 | void TimestampMapper::QueueFor(chrono::nanoseconds time_estimation_buffer) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1244 | // Note: queueing for time doesn't really work well across boots. So we |
| 1245 | // just assume that if you are using this, you only care about the current |
| 1246 | // boot. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1247 | // |
| 1248 | // TODO(austin): Is that the right concept? |
| 1249 | // |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1250 | // Make sure we have something queued first. This makes the end time |
| 1251 | // calculation simpler, and is typically what folks want regardless. |
| 1252 | if (matched_messages_.empty()) { |
| 1253 | if (!QueueMatched()) { |
| 1254 | return; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | const aos::monotonic_clock::time_point end_queue_time = |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1259 | std::max(monotonic_start_time( |
| 1260 | matched_messages_.front().monotonic_event_time.boot), |
| 1261 | matched_messages_.front().monotonic_event_time.time) + |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1262 | time_estimation_buffer; |
| 1263 | |
| 1264 | // Place sorted messages on the list until we have |
| 1265 | // --time_estimation_buffer_seconds seconds queued up (but queue at least |
| 1266 | // until the log starts). |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1267 | while (end_queue_time >= last_message_time_.time) { |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1268 | if (!QueueMatched()) { |
| 1269 | return; |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1274 | void TimestampMapper::PopFront() { |
| 1275 | CHECK(first_message_ != FirstMessage::kNeedsUpdate); |
| 1276 | first_message_ = FirstMessage::kNeedsUpdate; |
| 1277 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1278 | matched_messages_.pop_front(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | Message TimestampMapper::MatchingMessageFor(const Message &message) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1282 | // Figure out what queue index we are looking for. |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1283 | CHECK_NOTNULL(message.data); |
| 1284 | CHECK(message.data->remote_queue_index.has_value()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1285 | const BootQueueIndex remote_queue_index = |
| 1286 | BootQueueIndex{.boot = message.monotonic_remote_boot, |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1287 | .index = *message.data->remote_queue_index}; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1288 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1289 | CHECK(message.data->monotonic_remote_time.has_value()); |
| 1290 | CHECK(message.data->realtime_remote_time.has_value()); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1291 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1292 | const BootTimestamp monotonic_remote_time{ |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1293 | .boot = message.monotonic_remote_boot, |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1294 | .time = monotonic_clock::time_point( |
| 1295 | message.data->monotonic_remote_time.value())}; |
| 1296 | const realtime_clock::time_point realtime_remote_time = |
| 1297 | *message.data->realtime_remote_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1298 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1299 | TimestampMapper *peer = |
| 1300 | nodes_data_[source_node_[message.data->channel_index]].peer; |
Austin Schuh | fecf1d8 | 2020-12-19 16:57:28 -0800 | [diff] [blame] | 1301 | |
| 1302 | // 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] | 1303 | // asked to pull a timestamp from a peer which doesn't exist, return an |
| 1304 | // empty message. |
Austin Schuh | fecf1d8 | 2020-12-19 16:57:28 -0800 | [diff] [blame] | 1305 | if (peer == nullptr) { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1306 | // TODO(austin): Make sure the tests hit all these paths with a boot count |
| 1307 | // of 1... |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1308 | return Message{.channel_index = message.channel_index, |
| 1309 | .queue_index = remote_queue_index, |
| 1310 | .timestamp = monotonic_remote_time, |
| 1311 | .monotonic_remote_boot = 0xffffff, |
| 1312 | .monotonic_timestamp_boot = 0xffffff, |
| 1313 | .data = nullptr}; |
Austin Schuh | fecf1d8 | 2020-12-19 16:57:28 -0800 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | // The queue which will have the matching data, if available. |
| 1317 | std::deque<Message> *data_queue = |
| 1318 | &peer->nodes_data_[node()].channels[message.channel_index].messages; |
| 1319 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame] | 1320 | peer->QueueUnmatchedUntil(monotonic_remote_time); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1321 | |
| 1322 | if (data_queue->empty()) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1323 | return Message{.channel_index = message.channel_index, |
| 1324 | .queue_index = remote_queue_index, |
| 1325 | .timestamp = monotonic_remote_time, |
| 1326 | .monotonic_remote_boot = 0xffffff, |
| 1327 | .monotonic_timestamp_boot = 0xffffff, |
| 1328 | .data = nullptr}; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1331 | if (remote_queue_index < data_queue->front().queue_index || |
| 1332 | remote_queue_index > data_queue->back().queue_index) { |
| 1333 | return Message{ |
| 1334 | .channel_index = message.channel_index, |
| 1335 | .queue_index = remote_queue_index, |
| 1336 | .timestamp = monotonic_remote_time, |
Austin Schuh | 4850772 | 2021-07-17 17:29:24 -0700 | [diff] [blame] | 1337 | .monotonic_remote_boot = 0xffffff, |
| 1338 | .monotonic_timestamp_boot = 0xffffff, |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1339 | .data = nullptr}; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1342 | // The algorithm below is constant time with some assumptions. We need there |
| 1343 | // to be no missing messages in the data stream. This also assumes a queue |
| 1344 | // hasn't wrapped. That is conservative, but should let us get started. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1345 | if (data_queue->back().queue_index.boot == |
| 1346 | data_queue->front().queue_index.boot && |
| 1347 | (data_queue->back().queue_index.index - |
| 1348 | data_queue->front().queue_index.index + 1u == |
| 1349 | data_queue->size())) { |
| 1350 | CHECK_EQ(remote_queue_index.boot, data_queue->front().queue_index.boot); |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1351 | // Pull the data out and confirm that the timestamps match as expected. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1352 | // |
| 1353 | // TODO(austin): Move if not reliable. |
| 1354 | Message result = (*data_queue)[remote_queue_index.index - |
| 1355 | data_queue->front().queue_index.index]; |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1356 | |
| 1357 | CHECK_EQ(result.timestamp, monotonic_remote_time) |
| 1358 | << ": Queue index matches, but timestamp doesn't. Please investigate!"; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1359 | CHECK_EQ(result.data->realtime_sent_time, |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1360 | realtime_remote_time) |
| 1361 | << ": Queue index matches, but timestamp doesn't. Please investigate!"; |
| 1362 | // Now drop the data off the front. We have deduplicated timestamps, so we |
| 1363 | // are done. And all the data is in order. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1364 | data_queue->erase( |
| 1365 | data_queue->begin(), |
| 1366 | data_queue->begin() + |
| 1367 | (remote_queue_index.index - data_queue->front().queue_index.index)); |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1368 | return result; |
| 1369 | } else { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1370 | // TODO(austin): Binary search. |
| 1371 | auto it = std::find_if( |
| 1372 | data_queue->begin(), data_queue->end(), |
| 1373 | [remote_queue_index, |
| 1374 | remote_boot = monotonic_remote_time.boot](const Message &m) { |
| 1375 | return m.queue_index == remote_queue_index && |
| 1376 | m.timestamp.boot == remote_boot; |
| 1377 | }); |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1378 | if (it == data_queue->end()) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1379 | return Message{.channel_index = message.channel_index, |
| 1380 | .queue_index = remote_queue_index, |
| 1381 | .timestamp = monotonic_remote_time, |
| 1382 | .monotonic_remote_boot = 0xffffff, |
| 1383 | .monotonic_timestamp_boot = 0xffffff, |
| 1384 | .data = nullptr}; |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | Message result = std::move(*it); |
| 1388 | |
| 1389 | CHECK_EQ(result.timestamp, monotonic_remote_time) |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1390 | << ": Queue index matches, but timestamp doesn't. Please " |
| 1391 | "investigate!"; |
| 1392 | CHECK_EQ(result.data->realtime_sent_time, realtime_remote_time) |
| 1393 | << ": Queue index matches, but timestamp doesn't. Please " |
| 1394 | "investigate!"; |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1395 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1396 | // TODO(austin): We still go in order, so we can erase from the beginning to |
| 1397 | // our iterator minus 1. That'll keep 1 in the queue. |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 1398 | data_queue->erase(it); |
| 1399 | |
| 1400 | return result; |
| 1401 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1402 | } |
| 1403 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1404 | void TimestampMapper::QueueUnmatchedUntil(BootTimestamp t) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1405 | if (queued_until_ > t) { |
| 1406 | return; |
| 1407 | } |
| 1408 | while (true) { |
| 1409 | if (!messages_.empty() && messages_.back().timestamp > t) { |
| 1410 | queued_until_ = std::max(queued_until_, messages_.back().timestamp); |
| 1411 | return; |
| 1412 | } |
| 1413 | |
| 1414 | if (!Queue()) { |
| 1415 | // Found nothing to add, we are out of data! |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1416 | queued_until_ = BootTimestamp::max_time(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1417 | return; |
| 1418 | } |
| 1419 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1420 | // Now that it has been added (and cannibalized), forget about it |
| 1421 | // upstream. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1422 | boot_merger_.PopFront(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | bool TimestampMapper::Queue() { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1427 | Message *m = boot_merger_.Front(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1428 | if (m == nullptr) { |
| 1429 | return false; |
| 1430 | } |
| 1431 | for (NodeData &node_data : nodes_data_) { |
| 1432 | if (!node_data.any_delivered) continue; |
Austin Schuh | 36c0093 | 2021-07-19 18:13:21 -0700 | [diff] [blame] | 1433 | if (!node_data.save_for_peer) continue; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1434 | if (node_data.channels[m->channel_index].delivered) { |
| 1435 | // TODO(austin): This copies the data... Probably not worth stressing |
| 1436 | // about yet. |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1437 | // TODO(austin): Bound how big this can get. We tend not to send |
| 1438 | // massive data, so we can probably ignore this for a bit. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1439 | node_data.channels[m->channel_index].messages.emplace_back(*m); |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | messages_.emplace_back(std::move(*m)); |
| 1444 | return true; |
| 1445 | } |
| 1446 | |
| 1447 | std::string TimestampMapper::DebugString() const { |
| 1448 | std::stringstream ss; |
Austin Schuh | 6e014b8 | 2021-09-14 17:46:33 -0700 | [diff] [blame] | 1449 | ss << "node " << node() << " (" << node_name() << ") [\n"; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1450 | for (const Message &message : messages_) { |
| 1451 | ss << " " << message << "\n"; |
| 1452 | } |
| 1453 | ss << "] queued_until " << queued_until_; |
| 1454 | for (const NodeData &ns : nodes_data_) { |
| 1455 | if (ns.peer == nullptr) continue; |
| 1456 | ss << "\nnode " << ns.peer->node() << " remote_data [\n"; |
| 1457 | size_t channel_index = 0; |
| 1458 | for (const NodeData::ChannelData &channel_data : |
| 1459 | ns.peer->nodes_data_[node()].channels) { |
| 1460 | if (channel_data.messages.empty()) { |
| 1461 | continue; |
| 1462 | } |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 1463 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1464 | ss << " channel " << channel_index << " [\n"; |
| 1465 | for (const Message &m : channel_data.messages) { |
| 1466 | ss << " " << m << "\n"; |
| 1467 | } |
| 1468 | ss << " ]\n"; |
| 1469 | ++channel_index; |
| 1470 | } |
| 1471 | ss << "] queued_until " << ns.peer->queued_until_; |
| 1472 | } |
| 1473 | return ss.str(); |
| 1474 | } |
| 1475 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1476 | std::string MaybeNodeName(const Node *node) { |
| 1477 | if (node != nullptr) { |
| 1478 | return node->name()->str() + " "; |
| 1479 | } |
| 1480 | return ""; |
| 1481 | } |
| 1482 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 1483 | } // namespace aos::logger |