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