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