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."); |
| 33 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 34 | namespace aos::logger { |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 36 | namespace chrono = std::chrono; |
| 37 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 38 | DetachedBufferWriter::DetachedBufferWriter( |
| 39 | std::string_view filename, std::unique_ptr<DetachedBufferEncoder> encoder) |
| 40 | : filename_(filename), encoder_(std::move(encoder)) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 41 | if (!util::MkdirPIfSpace(filename, 0777)) { |
| 42 | ran_out_of_space_ = true; |
| 43 | } else { |
| 44 | fd_ = open(std::string(filename).c_str(), |
| 45 | O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 0774); |
| 46 | if (fd_ == -1 && errno == ENOSPC) { |
| 47 | ran_out_of_space_ = true; |
| 48 | } else { |
| 49 | PCHECK(fd_ != -1) << ": Failed to open " << filename << " for writing"; |
| 50 | VLOG(1) << "Opened " << filename << " for writing"; |
| 51 | } |
| 52 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | DetachedBufferWriter::~DetachedBufferWriter() { |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 56 | Close(); |
| 57 | if (ran_out_of_space_) { |
| 58 | CHECK(acknowledge_ran_out_of_space_) |
| 59 | << ": Unacknowledged out of disk space, log file was not completed"; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 60 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 63 | DetachedBufferWriter::DetachedBufferWriter(DetachedBufferWriter &&other) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 64 | *this = std::move(other); |
| 65 | } |
| 66 | |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 67 | // When other is destroyed "soon" (which it should be because we're getting an |
| 68 | // rvalue reference to it), it will flush etc all the data we have queued up |
| 69 | // (because that data will then be its data). |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 70 | DetachedBufferWriter &DetachedBufferWriter::operator=( |
| 71 | DetachedBufferWriter &&other) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 72 | std::swap(filename_, other.filename_); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 73 | std::swap(encoder_, other.encoder_); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 74 | std::swap(fd_, other.fd_); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 75 | std::swap(ran_out_of_space_, other.ran_out_of_space_); |
| 76 | 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] | 77 | std::swap(iovec_, other.iovec_); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 78 | std::swap(max_write_time_, other.max_write_time_); |
| 79 | std::swap(max_write_time_bytes_, other.max_write_time_bytes_); |
| 80 | std::swap(max_write_time_messages_, other.max_write_time_messages_); |
| 81 | std::swap(total_write_time_, other.total_write_time_); |
| 82 | std::swap(total_write_count_, other.total_write_count_); |
| 83 | std::swap(total_write_messages_, other.total_write_messages_); |
| 84 | std::swap(total_write_bytes_, other.total_write_bytes_); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 85 | return *this; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 88 | void DetachedBufferWriter::QueueSpan(absl::Span<const uint8_t> span) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 89 | if (ran_out_of_space_) { |
| 90 | // We don't want any later data to be written after space becomes |
| 91 | // available, so refuse to write anything more once we've dropped data |
| 92 | // because we ran out of space. |
| 93 | VLOG(1) << "Ignoring span: " << span.size(); |
| 94 | return; |
| 95 | } |
| 96 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 97 | if (encoder_->may_bypass() && span.size() > 4096u) { |
| 98 | // Over this threshold, we'll assume it's cheaper to add an extra |
| 99 | // syscall to write the data immediately instead of copying it to |
| 100 | // enqueue. |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 101 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 102 | // First, flush everything. |
| 103 | while (encoder_->queue_size() > 0u) { |
| 104 | Flush(); |
| 105 | } |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 106 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 107 | // Then, write it directly. |
| 108 | const auto start = aos::monotonic_clock::now(); |
| 109 | const ssize_t written = write(fd_, span.data(), span.size()); |
| 110 | const auto end = aos::monotonic_clock::now(); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 111 | HandleWriteReturn(written, span.size()); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 112 | UpdateStatsForWrite(end - start, written, 1); |
| 113 | } else { |
| 114 | encoder_->Encode(CopySpanAsDetachedBuffer(span)); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 115 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 116 | |
| 117 | FlushAtThreshold(); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 120 | void DetachedBufferWriter::Close() { |
| 121 | if (fd_ == -1) { |
| 122 | return; |
| 123 | } |
| 124 | encoder_->Finish(); |
| 125 | while (encoder_->queue_size() > 0) { |
| 126 | Flush(); |
| 127 | } |
| 128 | if (close(fd_) == -1) { |
| 129 | if (errno == ENOSPC) { |
| 130 | ran_out_of_space_ = true; |
| 131 | } else { |
| 132 | PLOG(ERROR) << "Closing log file failed"; |
| 133 | } |
| 134 | } |
| 135 | fd_ = -1; |
| 136 | VLOG(1) << "Closed " << filename_; |
| 137 | } |
| 138 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 139 | void DetachedBufferWriter::Flush() { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 140 | const auto queue = encoder_->queue(); |
| 141 | if (queue.empty()) { |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 142 | return; |
| 143 | } |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 144 | if (ran_out_of_space_) { |
| 145 | // We don't want any later data to be written after space becomes available, |
| 146 | // so refuse to write anything more once we've dropped data because we ran |
| 147 | // out of space. |
| 148 | VLOG(1) << "Ignoring queue: " << queue.size(); |
| 149 | encoder_->Clear(queue.size()); |
| 150 | return; |
| 151 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 152 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 153 | iovec_.clear(); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 154 | const size_t iovec_size = std::min<size_t>(queue.size(), IOV_MAX); |
| 155 | iovec_.resize(iovec_size); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 156 | size_t counted_size = 0; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 157 | for (size_t i = 0; i < iovec_size; ++i) { |
| 158 | iovec_[i].iov_base = const_cast<uint8_t *>(queue[i].data()); |
| 159 | iovec_[i].iov_len = queue[i].size(); |
| 160 | counted_size += iovec_[i].iov_len; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 161 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 162 | |
| 163 | const auto start = aos::monotonic_clock::now(); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 164 | const ssize_t written = writev(fd_, iovec_.data(), iovec_.size()); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 165 | const auto end = aos::monotonic_clock::now(); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 166 | HandleWriteReturn(written, counted_size); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 167 | |
| 168 | encoder_->Clear(iovec_size); |
| 169 | |
| 170 | UpdateStatsForWrite(end - start, written, iovec_size); |
| 171 | } |
| 172 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 173 | void DetachedBufferWriter::HandleWriteReturn(ssize_t write_return, |
| 174 | size_t write_size) { |
| 175 | if (write_return == -1 && errno == ENOSPC) { |
| 176 | ran_out_of_space_ = true; |
| 177 | return; |
| 178 | } |
| 179 | PCHECK(write_return >= 0) << ": write failed"; |
| 180 | if (write_return < static_cast<ssize_t>(write_size)) { |
| 181 | // Sometimes this happens instead of ENOSPC. On a real filesystem, this |
| 182 | // never seems to happen in any other case. If we ever want to log to a |
| 183 | // socket, this will happen more often. However, until we get there, we'll |
| 184 | // just assume it means we ran out of space. |
| 185 | ran_out_of_space_ = true; |
| 186 | return; |
| 187 | } |
| 188 | } |
| 189 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 190 | void DetachedBufferWriter::UpdateStatsForWrite( |
| 191 | aos::monotonic_clock::duration duration, ssize_t written, int iovec_size) { |
| 192 | if (duration > max_write_time_) { |
| 193 | max_write_time_ = duration; |
| 194 | max_write_time_bytes_ = written; |
| 195 | max_write_time_messages_ = iovec_size; |
| 196 | } |
| 197 | total_write_time_ += duration; |
| 198 | ++total_write_count_; |
| 199 | total_write_messages_ += iovec_size; |
| 200 | total_write_bytes_ += written; |
| 201 | } |
| 202 | |
| 203 | void DetachedBufferWriter::FlushAtThreshold() { |
| 204 | // Flush if we are at the max number of iovs per writev, because there's no |
| 205 | // point queueing up any more data in memory. Also flush once we have enough |
| 206 | // data queued up. |
| 207 | while (encoder_->queued_bytes() > static_cast<size_t>(FLAGS_flush_size) || |
| 208 | encoder_->queue_size() >= IOV_MAX) { |
| 209 | Flush(); |
| 210 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | flatbuffers::Offset<MessageHeader> PackMessage( |
| 214 | flatbuffers::FlatBufferBuilder *fbb, const Context &context, |
| 215 | int channel_index, LogType log_type) { |
| 216 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset; |
| 217 | |
| 218 | switch (log_type) { |
| 219 | case LogType::kLogMessage: |
| 220 | case LogType::kLogMessageAndDeliveryTime: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 221 | case LogType::kLogRemoteMessage: |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 222 | data_offset = fbb->CreateVector( |
| 223 | static_cast<const uint8_t *>(context.data), context.size); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 224 | break; |
| 225 | |
| 226 | case LogType::kLogDeliveryTimeOnly: |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | MessageHeader::Builder message_header_builder(*fbb); |
| 231 | message_header_builder.add_channel_index(channel_index); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 232 | |
| 233 | switch (log_type) { |
| 234 | case LogType::kLogRemoteMessage: |
| 235 | message_header_builder.add_queue_index(context.remote_queue_index); |
| 236 | message_header_builder.add_monotonic_sent_time( |
| 237 | context.monotonic_remote_time.time_since_epoch().count()); |
| 238 | message_header_builder.add_realtime_sent_time( |
| 239 | context.realtime_remote_time.time_since_epoch().count()); |
| 240 | break; |
| 241 | |
| 242 | case LogType::kLogMessage: |
| 243 | case LogType::kLogMessageAndDeliveryTime: |
| 244 | case LogType::kLogDeliveryTimeOnly: |
| 245 | message_header_builder.add_queue_index(context.queue_index); |
| 246 | message_header_builder.add_monotonic_sent_time( |
| 247 | context.monotonic_event_time.time_since_epoch().count()); |
| 248 | message_header_builder.add_realtime_sent_time( |
| 249 | context.realtime_event_time.time_since_epoch().count()); |
| 250 | break; |
| 251 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 252 | |
| 253 | switch (log_type) { |
| 254 | case LogType::kLogMessage: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 255 | case LogType::kLogRemoteMessage: |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 256 | message_header_builder.add_data(data_offset); |
| 257 | break; |
| 258 | |
| 259 | case LogType::kLogMessageAndDeliveryTime: |
| 260 | message_header_builder.add_data(data_offset); |
| 261 | [[fallthrough]]; |
| 262 | |
| 263 | case LogType::kLogDeliveryTimeOnly: |
| 264 | message_header_builder.add_monotonic_remote_time( |
| 265 | context.monotonic_remote_time.time_since_epoch().count()); |
| 266 | message_header_builder.add_realtime_remote_time( |
| 267 | context.realtime_remote_time.time_since_epoch().count()); |
| 268 | message_header_builder.add_remote_queue_index(context.remote_queue_index); |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | return message_header_builder.Finish(); |
| 273 | } |
| 274 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 275 | SpanReader::SpanReader(std::string_view filename) : filename_(filename) { |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 276 | static const std::string_view kXz = ".xz"; |
| 277 | if (filename.substr(filename.size() - kXz.size()) == kXz) { |
| 278 | #if ENABLE_LZMA |
| 279 | decoder_ = std::make_unique<LzmaDecoder>(filename); |
| 280 | #else |
| 281 | LOG(FATAL) << "Reading xz-compressed files not supported on this platform"; |
| 282 | #endif |
| 283 | } else { |
| 284 | decoder_ = std::make_unique<DummyDecoder>(filename); |
| 285 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | absl::Span<const uint8_t> SpanReader::ReadMessage() { |
| 289 | // Make sure we have enough for the size. |
| 290 | if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) { |
| 291 | if (!ReadBlock()) { |
| 292 | return absl::Span<const uint8_t>(); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Now make sure we have enough for the message. |
| 297 | const size_t data_size = |
| 298 | flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) + |
| 299 | sizeof(flatbuffers::uoffset_t); |
Austin Schuh | e4fca83 | 2020-03-07 16:58:53 -0800 | [diff] [blame] | 300 | if (data_size == sizeof(flatbuffers::uoffset_t)) { |
| 301 | LOG(ERROR) << "Size of data is zero. Log file end is corrupted, skipping."; |
| 302 | LOG(ERROR) << " Rest of log file is " |
| 303 | << absl::BytesToHexString(std::string_view( |
| 304 | reinterpret_cast<const char *>(data_.data() + |
| 305 | consumed_data_), |
| 306 | data_.size() - consumed_data_)); |
| 307 | return absl::Span<const uint8_t>(); |
| 308 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 309 | while (data_.size() < consumed_data_ + data_size) { |
| 310 | if (!ReadBlock()) { |
| 311 | return absl::Span<const uint8_t>(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // And return it, consuming the data. |
| 316 | const uint8_t *data_ptr = data_.data() + consumed_data_; |
| 317 | |
| 318 | consumed_data_ += data_size; |
| 319 | |
| 320 | return absl::Span<const uint8_t>(data_ptr, data_size); |
| 321 | } |
| 322 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 323 | bool SpanReader::ReadBlock() { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 324 | // This is the amount of data we grab at a time. Doing larger chunks minimizes |
| 325 | // syscalls and helps decompressors batch things more efficiently. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 326 | constexpr size_t kReadSize = 256 * 1024; |
| 327 | |
| 328 | // Strip off any unused data at the front. |
| 329 | if (consumed_data_ != 0) { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 330 | data_.erase_front(consumed_data_); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 331 | consumed_data_ = 0; |
| 332 | } |
| 333 | |
| 334 | const size_t starting_size = data_.size(); |
| 335 | |
| 336 | // This should automatically grow the backing store. It won't shrink if we |
| 337 | // get a small chunk later. This reduces allocations when we want to append |
| 338 | // more data. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 339 | data_.resize(starting_size + kReadSize); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 340 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 341 | const size_t count = |
| 342 | decoder_->Read(data_.begin() + starting_size, data_.end()); |
| 343 | data_.resize(starting_size + count); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 344 | if (count == 0) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 345 | return false; |
| 346 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 351 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader( |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 352 | std::string_view filename) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 353 | SpanReader span_reader(filename); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 354 | absl::Span<const uint8_t> config_data = span_reader.ReadMessage(); |
| 355 | |
| 356 | // Make sure something was read. |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 357 | if (config_data == absl::Span<const uint8_t>()) { |
| 358 | return std::nullopt; |
| 359 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 360 | |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 361 | // And copy the config so we have it forever, removing the size prefix. |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 362 | ResizeableBuffer data; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 363 | data.resize(config_data.size()); |
| 364 | memcpy(data.data(), config_data.begin(), data.size()); |
| 365 | return SizePrefixedFlatbufferVector<LogFileHeader>(std::move(data)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 368 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage( |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 369 | std::string_view filename, size_t n) { |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 370 | SpanReader span_reader(filename); |
| 371 | absl::Span<const uint8_t> data_span = span_reader.ReadMessage(); |
| 372 | for (size_t i = 0; i < n + 1; ++i) { |
| 373 | data_span = span_reader.ReadMessage(); |
| 374 | |
| 375 | // Make sure something was read. |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 376 | if (data_span == absl::Span<const uint8_t>()) { |
| 377 | return std::nullopt; |
| 378 | } |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 381 | // And copy the config so we have it forever, removing the size prefix. |
| 382 | ResizeableBuffer data; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 383 | data.resize(data_span.size()); |
| 384 | memcpy(data.data(), data_span.begin(), data.size()); |
| 385 | return SizePrefixedFlatbufferVector<MessageHeader>(std::move(data)); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 388 | MessageReader::MessageReader(std::string_view filename) |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 389 | : span_reader_(filename), |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 390 | raw_log_file_header_( |
| 391 | SizePrefixedFlatbufferVector<LogFileHeader>::Empty()) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 392 | // Make sure we have enough to read the size. |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 393 | absl::Span<const uint8_t> header_data = span_reader_.ReadMessage(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 394 | |
| 395 | // Make sure something was read. |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 396 | CHECK(header_data != absl::Span<const uint8_t>()) |
| 397 | << ": Failed to read header from: " << filename; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 398 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 399 | // And copy the header data so we have it forever. |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 400 | ResizeableBuffer header_data_copy; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 401 | header_data_copy.resize(header_data.size()); |
| 402 | memcpy(header_data_copy.data(), header_data.begin(), header_data_copy.size()); |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 403 | raw_log_file_header_ = |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 404 | SizePrefixedFlatbufferVector<LogFileHeader>(std::move(header_data_copy)); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 405 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 406 | max_out_of_order_duration_ = |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 407 | chrono::nanoseconds(log_file_header()->max_out_of_order_duration()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 408 | |
| 409 | VLOG(1) << "Opened " << filename << " as node " |
| 410 | << FlatbufferToJson(log_file_header()->node()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 413 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> |
| 414 | MessageReader::ReadMessage() { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 415 | absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage(); |
| 416 | if (msg_data == absl::Span<const uint8_t>()) { |
| 417 | return std::nullopt; |
| 418 | } |
| 419 | |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 420 | ResizeableBuffer result_buffer; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 421 | result_buffer.resize(msg_data.size()); |
| 422 | memcpy(result_buffer.data(), msg_data.begin(), result_buffer.size()); |
| 423 | SizePrefixedFlatbufferVector<MessageHeader> result(std::move(result_buffer)); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 424 | |
| 425 | const monotonic_clock::time_point timestamp = monotonic_clock::time_point( |
| 426 | chrono::nanoseconds(result.message().monotonic_sent_time())); |
| 427 | |
| 428 | newest_timestamp_ = std::max(newest_timestamp_, timestamp); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 429 | VLOG(2) << "Read from " << filename() << " data " << FlatbufferToJson(result); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 430 | return std::move(result); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 433 | PartsMessageReader::PartsMessageReader(LogParts log_parts) |
| 434 | : parts_(std::move(log_parts)), message_reader_(parts_.parts[0]) {} |
| 435 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 436 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 437 | PartsMessageReader::ReadMessage() { |
| 438 | while (!done_) { |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 439 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> message = |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 440 | message_reader_.ReadMessage(); |
| 441 | if (message) { |
| 442 | newest_timestamp_ = message_reader_.newest_timestamp(); |
Austin Schuh | 32f6849 | 2020-11-08 21:45:51 -0800 | [diff] [blame] | 443 | const monotonic_clock::time_point monotonic_sent_time( |
| 444 | chrono::nanoseconds(message->message().monotonic_sent_time())); |
| 445 | CHECK_GE(monotonic_sent_time, |
| 446 | newest_timestamp_ - max_out_of_order_duration()); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 447 | return message; |
| 448 | } |
| 449 | NextLog(); |
| 450 | } |
Austin Schuh | 32f6849 | 2020-11-08 21:45:51 -0800 | [diff] [blame] | 451 | newest_timestamp_ = monotonic_clock::max_time; |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 452 | return std::nullopt; |
| 453 | } |
| 454 | |
| 455 | void PartsMessageReader::NextLog() { |
| 456 | if (next_part_index_ == parts_.parts.size()) { |
| 457 | done_ = true; |
| 458 | return; |
| 459 | } |
| 460 | message_reader_ = MessageReader(parts_.parts[next_part_index_]); |
| 461 | ++next_part_index_; |
| 462 | } |
| 463 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 464 | SplitMessageReader::SplitMessageReader( |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 465 | const std::vector<std::string> &filenames) |
| 466 | : filenames_(filenames), |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 467 | log_file_header_(SizePrefixedFlatbufferVector<LogFileHeader>::Empty()) { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 468 | CHECK(NextLogFile()) << ": filenames is empty. Need files to read."; |
| 469 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 470 | // Grab any log file header. They should all match (and we will check as we |
| 471 | // open more of them). |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 472 | log_file_header_ = message_reader_->raw_log_file_header(); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 473 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 474 | for (size_t i = 1; i < filenames_.size(); ++i) { |
| 475 | MessageReader message_reader(filenames_[i]); |
| 476 | |
| 477 | const monotonic_clock::time_point new_monotonic_start_time( |
| 478 | chrono::nanoseconds( |
| 479 | message_reader.log_file_header()->monotonic_start_time())); |
| 480 | const realtime_clock::time_point new_realtime_start_time( |
| 481 | chrono::nanoseconds( |
| 482 | message_reader.log_file_header()->realtime_start_time())); |
| 483 | |
| 484 | // There are 2 types of part files. Part files from before time estimation |
| 485 | // has started, and part files after. We don't declare a log file "started" |
| 486 | // until time estimation is up. And once a log file starts, it should never |
| 487 | // stop again, and should remain constant. |
| 488 | // To compare both types of headers, we mutate our saved copy of the header |
| 489 | // to match the next chunk by updating time if we detect a stopped -> |
| 490 | // started transition. |
| 491 | if (monotonic_start_time() == monotonic_clock::min_time) { |
| 492 | CHECK_EQ(realtime_start_time(), realtime_clock::min_time); |
| 493 | // We should only be missing the monotonic start time when logging data |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 494 | // for remote nodes. We don't have a good way to determine the remote |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 495 | // realtime offset, so it shouldn't be filled out. |
| 496 | // TODO(austin): If we have a good way, feel free to fill it out. It |
| 497 | // probably won't be better than we could do in post though with the same |
| 498 | // data. |
| 499 | CHECK(!log_file_header_.mutable_message()->has_realtime_start_time()); |
| 500 | if (new_monotonic_start_time != monotonic_clock::min_time) { |
| 501 | // If we finally found our start time, update the header. Do this once |
| 502 | // because it should never change again. |
| 503 | log_file_header_.mutable_message()->mutate_monotonic_start_time( |
| 504 | new_monotonic_start_time.time_since_epoch().count()); |
| 505 | log_file_header_.mutable_message()->mutate_realtime_start_time( |
| 506 | new_realtime_start_time.time_since_epoch().count()); |
| 507 | } |
| 508 | } |
| 509 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 510 | // We don't have a good way to set the realtime start time on remote nodes. |
| 511 | // Confirm it remains consistent. |
| 512 | CHECK_EQ(log_file_header_.mutable_message()->has_realtime_start_time(), |
| 513 | message_reader.log_file_header()->has_realtime_start_time()); |
| 514 | |
| 515 | // Parts index will *not* match unless we set them to match. We only want |
| 516 | // to accept the start time and parts mismatching, so set them. |
| 517 | log_file_header_.mutable_message()->mutate_parts_index( |
| 518 | message_reader.log_file_header()->parts_index()); |
| 519 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 520 | // Now compare that the headers match. |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 521 | if (!CompareFlatBuffer(message_reader.raw_log_file_header(), |
| 522 | log_file_header_)) { |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 523 | if (message_reader.log_file_header()->has_log_event_uuid() && |
| 524 | log_file_header_.message().has_log_event_uuid() && |
| 525 | message_reader.log_file_header()->log_event_uuid()->string_view() != |
| 526 | log_file_header_.message().log_event_uuid()->string_view()) { |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 527 | LOG(FATAL) << "Logger UUIDs don't match between log file chunks " |
| 528 | << filenames_[0] << " and " << filenames_[i] |
| 529 | << ", this is not supported."; |
| 530 | } |
| 531 | if (message_reader.log_file_header()->has_parts_uuid() && |
| 532 | log_file_header_.message().has_parts_uuid() && |
| 533 | message_reader.log_file_header()->parts_uuid()->string_view() != |
| 534 | log_file_header_.message().parts_uuid()->string_view()) { |
| 535 | LOG(FATAL) << "Parts UUIDs don't match between log file chunks " |
| 536 | << filenames_[0] << " and " << filenames_[i] |
| 537 | << ", this is not supported."; |
| 538 | } |
| 539 | |
| 540 | LOG(FATAL) << "Header is different between log file chunks " |
| 541 | << filenames_[0] << " and " << filenames_[i] |
| 542 | << ", this is not supported."; |
| 543 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 544 | } |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 545 | // Put the parts index back to the first log file chunk. |
| 546 | log_file_header_.mutable_message()->mutate_parts_index( |
| 547 | message_reader_->log_file_header()->parts_index()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 548 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 549 | // Setup per channel state. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 550 | channels_.resize(configuration()->channels()->size()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 551 | for (ChannelData &channel_data : channels_) { |
| 552 | channel_data.data.split_reader = this; |
| 553 | // Build up the timestamp list. |
| 554 | if (configuration::MultiNode(configuration())) { |
| 555 | channel_data.timestamps.resize(configuration()->nodes()->size()); |
| 556 | for (MessageHeaderQueue &queue : channel_data.timestamps) { |
| 557 | queue.timestamps = true; |
| 558 | queue.split_reader = this; |
| 559 | } |
| 560 | } |
| 561 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 562 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 563 | // Build up channels_to_write_ as an optimization to make it fast to figure |
| 564 | // out which datastructure to place any new data from a channel on. |
| 565 | for (const Channel *channel : *configuration()->channels()) { |
| 566 | // This is the main case. We will only see data on this node. |
| 567 | if (configuration::ChannelIsSendableOnNode(channel, node())) { |
| 568 | channels_to_write_.emplace_back( |
| 569 | &channels_[channels_to_write_.size()].data); |
| 570 | } else |
| 571 | // If we can't send, but can receive, we should be able to see |
| 572 | // timestamps here. |
| 573 | if (configuration::ChannelIsReadableOnNode(channel, node())) { |
| 574 | channels_to_write_.emplace_back( |
| 575 | &(channels_[channels_to_write_.size()] |
| 576 | .timestamps[configuration::GetNodeIndex(configuration(), |
| 577 | node())])); |
| 578 | } else { |
| 579 | channels_to_write_.emplace_back(nullptr); |
| 580 | } |
| 581 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 584 | bool SplitMessageReader::NextLogFile() { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 585 | if (next_filename_index_ == filenames_.size()) { |
| 586 | return false; |
| 587 | } |
| 588 | message_reader_ = |
| 589 | std::make_unique<MessageReader>(filenames_[next_filename_index_]); |
| 590 | |
| 591 | // We can't support the config diverging between two log file headers. See if |
| 592 | // they are the same. |
| 593 | if (next_filename_index_ != 0) { |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 594 | // In order for the headers to identically compare, they need to have the |
| 595 | // same parts_index. Rewrite the saved header with the new parts_index, |
| 596 | // compare, and then restore. |
| 597 | const int32_t original_parts_index = |
| 598 | log_file_header_.message().parts_index(); |
| 599 | log_file_header_.mutable_message()->mutate_parts_index( |
| 600 | message_reader_->log_file_header()->parts_index()); |
| 601 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 602 | CHECK(CompareFlatBuffer(message_reader_->raw_log_file_header(), |
| 603 | log_file_header_)) |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 604 | << ": Header is different between log file chunks " |
| 605 | << filenames_[next_filename_index_] << " and " |
| 606 | << filenames_[next_filename_index_ - 1] << ", this is not supported."; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 607 | |
| 608 | log_file_header_.mutable_message()->mutate_parts_index( |
| 609 | original_parts_index); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | ++next_filename_index_; |
| 613 | return true; |
| 614 | } |
| 615 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 616 | bool SplitMessageReader::QueueMessages( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 617 | monotonic_clock::time_point last_dequeued_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 618 | // TODO(austin): Once we are happy that everything works, read a 256kb chunk |
| 619 | // to reduce the need to re-heap down below. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 620 | |
| 621 | // Special case no more data. Otherwise we blow up on the CHECK statement |
| 622 | // confirming that we have enough data queued. |
| 623 | if (at_end_) { |
| 624 | return false; |
| 625 | } |
| 626 | |
| 627 | // If this isn't the first time around, confirm that we had enough data queued |
| 628 | // to follow the contract. |
| 629 | if (time_to_queue_ != monotonic_clock::min_time) { |
| 630 | CHECK_LE(last_dequeued_time, |
| 631 | newest_timestamp() - max_out_of_order_duration()) |
| 632 | << " node " << FlatbufferToJson(node()) << " on " << this; |
| 633 | |
| 634 | // Bail if there is enough data already queued. |
| 635 | if (last_dequeued_time < time_to_queue_) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 636 | VLOG(1) << MaybeNodeName(target_node_) << "All up to date on " << this |
| 637 | << ", dequeued " << last_dequeued_time << " queue time " |
| 638 | << time_to_queue_; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 639 | return true; |
| 640 | } |
| 641 | } else { |
| 642 | // Startup takes a special dance. We want to queue up until the start time, |
| 643 | // but we then want to find the next message to read. The conservative |
| 644 | // answer is to immediately trigger a second requeue to get things moving. |
| 645 | time_to_queue_ = monotonic_start_time(); |
Austin Schuh | eeba029 | 2020-10-11 16:20:05 -0700 | [diff] [blame] | 646 | CHECK_NE(time_to_queue_, monotonic_clock::min_time); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 647 | QueueMessages(time_to_queue_); |
| 648 | } |
| 649 | |
| 650 | // If we are asked to queue, queue for at least max_out_of_order_duration past |
| 651 | // the last known time in the log file (ie the newest timestep read). As long |
| 652 | // as we requeue exactly when time_to_queue_ is dequeued and go no further, we |
| 653 | // are safe. And since we pop in order, that works. |
| 654 | // |
| 655 | // Special case the start of the log file. There should be at most 1 message |
| 656 | // from each channel at the start of the log file. So always force the start |
| 657 | // of the log file to just be read. |
| 658 | time_to_queue_ = std::max(time_to_queue_, newest_timestamp()); |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 659 | VLOG(1) << MaybeNodeName(target_node_) << "Queueing, going until " |
| 660 | << time_to_queue_ << " " << filename(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 661 | |
| 662 | bool was_emplaced = false; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 663 | while (true) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 664 | // Stop if we have enough. |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 665 | if (newest_timestamp() > time_to_queue_ + max_out_of_order_duration() && |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 666 | was_emplaced) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 667 | VLOG(1) << MaybeNodeName(target_node_) << "Done queueing on " << this |
| 668 | << ", queued to " << newest_timestamp() << " with requeue time " |
| 669 | << time_to_queue_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 670 | return true; |
| 671 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 672 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 673 | if (std::optional<SizePrefixedFlatbufferVector<MessageHeader>> msg = |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 674 | message_reader_->ReadMessage()) { |
| 675 | const MessageHeader &header = msg.value().message(); |
| 676 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 677 | const monotonic_clock::time_point timestamp = monotonic_clock::time_point( |
| 678 | chrono::nanoseconds(header.monotonic_sent_time())); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 679 | |
Austin Schuh | 0b5fd03 | 2020-03-28 17:36:49 -0700 | [diff] [blame] | 680 | if (VLOG_IS_ON(2)) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 681 | LOG(INFO) << MaybeNodeName(target_node_) << "Queued " << this << " " |
| 682 | << filename() << " ttq: " << time_to_queue_ << " now " |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 683 | << newest_timestamp() << " start time " |
| 684 | << monotonic_start_time() << " " << FlatbufferToJson(&header); |
Austin Schuh | 0b5fd03 | 2020-03-28 17:36:49 -0700 | [diff] [blame] | 685 | } else if (VLOG_IS_ON(1)) { |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 686 | SizePrefixedFlatbufferVector<MessageHeader> copy = msg.value(); |
Austin Schuh | 0b5fd03 | 2020-03-28 17:36:49 -0700 | [diff] [blame] | 687 | copy.mutable_message()->clear_data(); |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 688 | LOG(INFO) << MaybeNodeName(target_node_) << "Queued " << this << " " |
| 689 | << filename() << " ttq: " << time_to_queue_ << " now " |
| 690 | << newest_timestamp() << " start time " |
| 691 | << monotonic_start_time() << " " << FlatbufferToJson(copy); |
Austin Schuh | 0b5fd03 | 2020-03-28 17:36:49 -0700 | [diff] [blame] | 692 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 693 | |
| 694 | const int channel_index = header.channel_index(); |
| 695 | was_emplaced = channels_to_write_[channel_index]->emplace_back( |
| 696 | std::move(msg.value())); |
| 697 | if (was_emplaced) { |
| 698 | newest_timestamp_ = std::max(newest_timestamp_, timestamp); |
| 699 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 700 | } else { |
| 701 | if (!NextLogFile()) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 702 | VLOG(1) << MaybeNodeName(target_node_) << "No more files, last was " |
| 703 | << filenames_.back(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 704 | at_end_ = true; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 705 | for (MessageHeaderQueue *queue : channels_to_write_) { |
| 706 | if (queue == nullptr || queue->timestamp_merger == nullptr) { |
| 707 | continue; |
| 708 | } |
| 709 | queue->timestamp_merger->NoticeAtEnd(); |
| 710 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 711 | return false; |
| 712 | } |
| 713 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 714 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | void SplitMessageReader::SetTimestampMerger(TimestampMerger *timestamp_merger, |
| 718 | int channel_index, |
| 719 | const Node *target_node) { |
| 720 | const Node *reinterpreted_target_node = |
| 721 | configuration::GetNodeOrDie(configuration(), target_node); |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 722 | target_node_ = reinterpreted_target_node; |
| 723 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 724 | const Channel *const channel = |
| 725 | configuration()->channels()->Get(channel_index); |
| 726 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 727 | VLOG(1) << " Configuring merger " << this << " for channel " << channel_index |
| 728 | << " " |
| 729 | << configuration::CleanedChannelToString( |
| 730 | configuration()->channels()->Get(channel_index)); |
| 731 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 732 | MessageHeaderQueue *message_header_queue = nullptr; |
| 733 | |
| 734 | // Figure out if this log file is from our point of view, or the other node's |
| 735 | // point of view. |
| 736 | if (node() == reinterpreted_target_node) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 737 | VLOG(1) << " Replaying as logged node " << filename(); |
| 738 | |
| 739 | if (configuration::ChannelIsSendableOnNode(channel, node())) { |
| 740 | VLOG(1) << " Data on node"; |
| 741 | message_header_queue = &(channels_[channel_index].data); |
| 742 | } else if (configuration::ChannelIsReadableOnNode(channel, node())) { |
| 743 | VLOG(1) << " Timestamps on node"; |
| 744 | message_header_queue = |
| 745 | &(channels_[channel_index].timestamps[configuration::GetNodeIndex( |
| 746 | configuration(), node())]); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 747 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 748 | VLOG(1) << " Dropping"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 749 | } |
| 750 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 751 | VLOG(1) << " Replaying as other node " << filename(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 752 | // We are replaying from another node's point of view. The only interesting |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 753 | // data is data that is sent from our node and received on theirs. |
| 754 | if (configuration::ChannelIsReadableOnNode(channel, |
| 755 | reinterpreted_target_node) && |
| 756 | configuration::ChannelIsSendableOnNode(channel, node())) { |
| 757 | VLOG(1) << " Readable on target node"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 758 | // Data from another node. |
| 759 | message_header_queue = &(channels_[channel_index].data); |
| 760 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 761 | VLOG(1) << " Dropping"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 762 | // This is either not sendable on the other node, or is a timestamp and |
| 763 | // therefore not interesting. |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | // If we found one, write it down. This will be nullptr when there is nothing |
| 768 | // relevant on this channel on this node for the target node. In that case, |
| 769 | // we want to drop the message instead of queueing it. |
| 770 | if (message_header_queue != nullptr) { |
| 771 | message_header_queue->timestamp_merger = timestamp_merger; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 776 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 777 | SplitMessageReader::PopOldest(int channel_index) { |
| 778 | CHECK_GT(channels_[channel_index].data.size(), 0u); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 779 | const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 780 | timestamp = channels_[channel_index].data.front_timestamp(); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 781 | SizePrefixedFlatbufferVector<MessageHeader> front = |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 782 | std::move(channels_[channel_index].data.front()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 783 | channels_[channel_index].data.PopFront(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 784 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 785 | VLOG(1) << MaybeNodeName(target_node_) << "Popped Data " << this << " " |
| 786 | << std::get<0>(timestamp) << " for " |
| 787 | << configuration::StrippedChannelToString( |
| 788 | configuration()->channels()->Get(channel_index)) |
| 789 | << " (" << channel_index << ")"; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 790 | |
| 791 | QueueMessages(std::get<0>(timestamp)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 792 | |
| 793 | return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp), |
| 794 | std::move(front)); |
| 795 | } |
| 796 | |
| 797 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 798 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 799 | SplitMessageReader::PopOldestTimestamp(int channel, int node_index) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 800 | CHECK_GT(channels_[channel].timestamps[node_index].size(), 0u); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 801 | const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 802 | timestamp = channels_[channel].timestamps[node_index].front_timestamp(); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 803 | SizePrefixedFlatbufferVector<MessageHeader> front = |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 804 | std::move(channels_[channel].timestamps[node_index].front()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 805 | channels_[channel].timestamps[node_index].PopFront(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 806 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 807 | VLOG(1) << MaybeNodeName(target_node_) << "Popped timestamp " << this << " " |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 808 | << std::get<0>(timestamp) << " for " |
| 809 | << configuration::StrippedChannelToString( |
| 810 | configuration()->channels()->Get(channel)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 811 | << " on " |
| 812 | << configuration()->nodes()->Get(node_index)->name()->string_view() |
| 813 | << " (" << node_index << ")"; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 814 | |
| 815 | QueueMessages(std::get<0>(timestamp)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 816 | |
| 817 | return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp), |
| 818 | std::move(front)); |
| 819 | } |
| 820 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 821 | bool SplitMessageReader::MessageHeaderQueue::emplace_back( |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 822 | SizePrefixedFlatbufferVector<MessageHeader> &&msg) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 823 | CHECK(split_reader != nullptr); |
| 824 | |
| 825 | // If there is no timestamp merger for this queue, nobody is listening. Drop |
| 826 | // the message. This happens when a log file from another node is replayed, |
| 827 | // and the timestamp mergers down stream just don't care. |
| 828 | if (timestamp_merger == nullptr) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 829 | return false; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | CHECK(timestamps != msg.message().has_data()) |
| 833 | << ": Got timestamps and data mixed up on a node. " |
| 834 | << FlatbufferToJson(msg); |
| 835 | |
| 836 | data_.emplace_back(std::move(msg)); |
| 837 | |
| 838 | if (data_.size() == 1u) { |
| 839 | // Yup, new data. Notify. |
| 840 | if (timestamps) { |
| 841 | timestamp_merger->UpdateTimestamp(split_reader, front_timestamp()); |
| 842 | } else { |
| 843 | timestamp_merger->Update(split_reader, front_timestamp()); |
| 844 | } |
| 845 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 846 | |
| 847 | return true; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 848 | } |
| 849 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 850 | void SplitMessageReader::MessageHeaderQueue::PopFront() { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 851 | data_.pop_front(); |
| 852 | if (data_.size() != 0u) { |
| 853 | // Yup, new data. |
| 854 | if (timestamps) { |
| 855 | timestamp_merger->UpdateTimestamp(split_reader, front_timestamp()); |
| 856 | } else { |
| 857 | timestamp_merger->Update(split_reader, front_timestamp()); |
| 858 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 859 | } else { |
| 860 | // Poke anyways to update the heap. |
| 861 | if (timestamps) { |
| 862 | timestamp_merger->UpdateTimestamp( |
| 863 | nullptr, std::make_tuple(monotonic_clock::min_time, 0, nullptr)); |
| 864 | } else { |
| 865 | timestamp_merger->Update( |
| 866 | nullptr, std::make_tuple(monotonic_clock::min_time, 0, nullptr)); |
| 867 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 868 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | namespace { |
| 872 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 873 | bool SplitMessageReaderHeapCompare( |
| 874 | const std::tuple<monotonic_clock::time_point, uint32_t, |
| 875 | SplitMessageReader *> |
| 876 | first, |
| 877 | const std::tuple<monotonic_clock::time_point, uint32_t, |
| 878 | SplitMessageReader *> |
| 879 | second) { |
| 880 | if (std::get<0>(first) > std::get<0>(second)) { |
| 881 | return true; |
| 882 | } else if (std::get<0>(first) == std::get<0>(second)) { |
| 883 | if (std::get<1>(first) > std::get<1>(second)) { |
| 884 | return true; |
| 885 | } else if (std::get<1>(first) == std::get<1>(second)) { |
| 886 | return std::get<2>(first) > std::get<2>(second); |
| 887 | } else { |
| 888 | return false; |
| 889 | } |
| 890 | } else { |
| 891 | return false; |
| 892 | } |
| 893 | } |
| 894 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 895 | bool ChannelHeapCompare( |
| 896 | const std::pair<monotonic_clock::time_point, int> first, |
| 897 | const std::pair<monotonic_clock::time_point, int> second) { |
| 898 | if (first.first > second.first) { |
| 899 | return true; |
| 900 | } else if (first.first == second.first) { |
| 901 | return first.second > second.second; |
| 902 | } else { |
| 903 | return false; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | } // namespace |
| 908 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 909 | TimestampMerger::TimestampMerger( |
| 910 | const Configuration *configuration, |
| 911 | std::vector<SplitMessageReader *> split_message_readers, int channel_index, |
| 912 | const Node *target_node, ChannelMerger *channel_merger) |
| 913 | : configuration_(configuration), |
| 914 | split_message_readers_(std::move(split_message_readers)), |
| 915 | channel_index_(channel_index), |
| 916 | node_index_(configuration::MultiNode(configuration) |
| 917 | ? configuration::GetNodeIndex(configuration, target_node) |
| 918 | : -1), |
| 919 | channel_merger_(channel_merger) { |
| 920 | // Tell the readers we care so they know who to notify. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 921 | VLOG(1) << "Configuring channel " << channel_index << " target node " |
| 922 | << FlatbufferToJson(target_node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 923 | for (SplitMessageReader *reader : split_message_readers_) { |
| 924 | reader->SetTimestampMerger(this, channel_index, target_node); |
| 925 | } |
| 926 | |
| 927 | // And then determine if we need to track timestamps. |
| 928 | const Channel *channel = configuration->channels()->Get(channel_index); |
| 929 | if (!configuration::ChannelIsSendableOnNode(channel, target_node) && |
| 930 | configuration::ChannelIsReadableOnNode(channel, target_node)) { |
| 931 | has_timestamps_ = true; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | void TimestampMerger::PushMessageHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 936 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 937 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 938 | SplitMessageReader *split_message_reader) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 939 | if (split_message_reader != nullptr) { |
| 940 | DCHECK(std::find_if(message_heap_.begin(), message_heap_.end(), |
| 941 | [split_message_reader]( |
| 942 | const std::tuple<monotonic_clock::time_point, |
| 943 | uint32_t, SplitMessageReader *> |
| 944 | x) { |
| 945 | return std::get<2>(x) == split_message_reader; |
| 946 | }) == message_heap_.end()) |
| 947 | << ": Pushing message when it is already in the heap."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 948 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 949 | message_heap_.push_back(std::make_tuple( |
| 950 | std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 951 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 952 | std::push_heap(message_heap_.begin(), message_heap_.end(), |
| 953 | &SplitMessageReaderHeapCompare); |
| 954 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 955 | |
| 956 | // If we are just a data merger, don't wait for timestamps. |
| 957 | if (!has_timestamps_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 958 | if (!message_heap_.empty()) { |
| 959 | channel_merger_->Update(std::get<0>(message_heap_[0]), channel_index_); |
| 960 | pushed_ = true; |
| 961 | } else { |
| 962 | // Remove ourselves if we are empty. |
| 963 | channel_merger_->Update(monotonic_clock::min_time, channel_index_); |
| 964 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 968 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 969 | TimestampMerger::oldest_message() const { |
| 970 | CHECK_GT(message_heap_.size(), 0u); |
| 971 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 972 | oldest_message_reader = message_heap_.front(); |
| 973 | return std::get<2>(oldest_message_reader)->oldest_message(channel_index_); |
| 974 | } |
| 975 | |
| 976 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 977 | TimestampMerger::oldest_timestamp() const { |
| 978 | CHECK_GT(timestamp_heap_.size(), 0u); |
| 979 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 980 | oldest_message_reader = timestamp_heap_.front(); |
| 981 | return std::get<2>(oldest_message_reader) |
| 982 | ->oldest_message(channel_index_, node_index_); |
| 983 | } |
| 984 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 985 | void TimestampMerger::PushTimestampHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 986 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 987 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 988 | SplitMessageReader *split_message_reader) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 989 | if (split_message_reader != nullptr) { |
| 990 | DCHECK(std::find_if(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 991 | [split_message_reader]( |
| 992 | const std::tuple<monotonic_clock::time_point, |
| 993 | uint32_t, SplitMessageReader *> |
| 994 | x) { |
| 995 | return std::get<2>(x) == split_message_reader; |
| 996 | }) == timestamp_heap_.end()) |
| 997 | << ": Pushing timestamp when it is already in the heap."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 998 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 999 | timestamp_heap_.push_back(std::make_tuple( |
| 1000 | std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1001 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1002 | std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1003 | SplitMessageReaderHeapCompare); |
| 1004 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1005 | |
| 1006 | // If we are a timestamp merger, don't wait for data. Missing data will be |
| 1007 | // caught at read time. |
| 1008 | if (has_timestamps_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1009 | if (!timestamp_heap_.empty()) { |
| 1010 | channel_merger_->Update(std::get<0>(timestamp_heap_[0]), channel_index_); |
| 1011 | pushed_ = true; |
| 1012 | } else { |
| 1013 | // Remove ourselves if we are empty. |
| 1014 | channel_merger_->Update(monotonic_clock::min_time, channel_index_); |
| 1015 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1020 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1021 | TimestampMerger::PopMessageHeap() { |
| 1022 | // Pop the oldest message reader pointer off the heap. |
| 1023 | CHECK_GT(message_heap_.size(), 0u); |
| 1024 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1025 | oldest_message_reader = message_heap_.front(); |
| 1026 | |
| 1027 | std::pop_heap(message_heap_.begin(), message_heap_.end(), |
| 1028 | &SplitMessageReaderHeapCompare); |
| 1029 | message_heap_.pop_back(); |
| 1030 | |
| 1031 | // Pop the oldest message. This re-pushes any messages from the reader to the |
| 1032 | // message heap. |
| 1033 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1034 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1035 | oldest_message = |
| 1036 | std::get<2>(oldest_message_reader)->PopOldest(channel_index_); |
| 1037 | |
| 1038 | // Confirm that the time and queue_index we have recorded matches. |
| 1039 | CHECK_EQ(std::get<0>(oldest_message), std::get<0>(oldest_message_reader)); |
| 1040 | CHECK_EQ(std::get<1>(oldest_message), std::get<1>(oldest_message_reader)); |
| 1041 | |
| 1042 | // Now, keep reading until we have found all duplicates. |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1043 | while (!message_heap_.empty()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1044 | // See if it is a duplicate. |
| 1045 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1046 | next_oldest_message_reader = message_heap_.front(); |
| 1047 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1048 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 1049 | next_oldest_message_time = std::get<2>(next_oldest_message_reader) |
| 1050 | ->oldest_message(channel_index_); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1051 | |
| 1052 | if (std::get<0>(next_oldest_message_time) == std::get<0>(oldest_message) && |
| 1053 | std::get<1>(next_oldest_message_time) == std::get<1>(oldest_message)) { |
| 1054 | // Pop the message reader pointer. |
| 1055 | std::pop_heap(message_heap_.begin(), message_heap_.end(), |
| 1056 | &SplitMessageReaderHeapCompare); |
| 1057 | message_heap_.pop_back(); |
| 1058 | |
| 1059 | // Pop the next oldest message. This re-pushes any messages from the |
| 1060 | // reader. |
| 1061 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1062 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1063 | next_oldest_message = std::get<2>(next_oldest_message_reader) |
| 1064 | ->PopOldest(channel_index_); |
| 1065 | |
| 1066 | // And make sure the message matches in it's entirety. |
| 1067 | CHECK(std::get<2>(oldest_message).span() == |
| 1068 | std::get<2>(next_oldest_message).span()) |
| 1069 | << ": Data at the same timestamp doesn't match."; |
| 1070 | } else { |
| 1071 | break; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | return oldest_message; |
| 1076 | } |
| 1077 | |
| 1078 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1079 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1080 | TimestampMerger::PopTimestampHeap() { |
| 1081 | // Pop the oldest message reader pointer off the heap. |
| 1082 | CHECK_GT(timestamp_heap_.size(), 0u); |
| 1083 | |
| 1084 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1085 | oldest_timestamp_reader = timestamp_heap_.front(); |
| 1086 | |
| 1087 | std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1088 | &SplitMessageReaderHeapCompare); |
| 1089 | timestamp_heap_.pop_back(); |
| 1090 | |
| 1091 | CHECK(node_index_ != -1) << ": Timestamps in a single node environment"; |
| 1092 | |
| 1093 | // Pop the oldest message. This re-pushes any timestamps from the reader to |
| 1094 | // the timestamp heap. |
| 1095 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1096 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1097 | oldest_timestamp = std::get<2>(oldest_timestamp_reader) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1098 | ->PopOldestTimestamp(channel_index_, node_index_); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1099 | |
| 1100 | // Confirm that the time we have recorded matches. |
| 1101 | CHECK_EQ(std::get<0>(oldest_timestamp), std::get<0>(oldest_timestamp_reader)); |
| 1102 | CHECK_EQ(std::get<1>(oldest_timestamp), std::get<1>(oldest_timestamp_reader)); |
| 1103 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1104 | // Now, keep reading until we have found all duplicates. |
| 1105 | while (!timestamp_heap_.empty()) { |
| 1106 | // See if it is a duplicate. |
| 1107 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1108 | next_oldest_timestamp_reader = timestamp_heap_.front(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1109 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1110 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 1111 | next_oldest_timestamp_time = |
| 1112 | std::get<2>(next_oldest_timestamp_reader) |
| 1113 | ->oldest_message(channel_index_, node_index_); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1114 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1115 | if (std::get<0>(next_oldest_timestamp_time) == |
| 1116 | std::get<0>(oldest_timestamp) && |
| 1117 | std::get<1>(next_oldest_timestamp_time) == |
| 1118 | std::get<1>(oldest_timestamp)) { |
| 1119 | // Pop the timestamp reader pointer. |
| 1120 | std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1121 | &SplitMessageReaderHeapCompare); |
| 1122 | timestamp_heap_.pop_back(); |
| 1123 | |
| 1124 | // Pop the next oldest timestamp. This re-pushes any messages from the |
| 1125 | // reader. |
| 1126 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1127 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1128 | next_oldest_timestamp = |
| 1129 | std::get<2>(next_oldest_timestamp_reader) |
| 1130 | ->PopOldestTimestamp(channel_index_, node_index_); |
| 1131 | |
| 1132 | // And make sure the contents matches in it's entirety. |
| 1133 | CHECK(std::get<2>(oldest_timestamp).span() == |
| 1134 | std::get<2>(next_oldest_timestamp).span()) |
| 1135 | << ": Data at the same timestamp doesn't match, " |
| 1136 | << aos::FlatbufferToJson(std::get<2>(oldest_timestamp)) << " vs " |
| 1137 | << aos::FlatbufferToJson(std::get<2>(next_oldest_timestamp)) << " " |
| 1138 | << absl::BytesToHexString(std::string_view( |
| 1139 | reinterpret_cast<const char *>( |
| 1140 | std::get<2>(oldest_timestamp).span().data()), |
| 1141 | std::get<2>(oldest_timestamp).span().size())) |
| 1142 | << " vs " |
| 1143 | << absl::BytesToHexString(std::string_view( |
| 1144 | reinterpret_cast<const char *>( |
| 1145 | std::get<2>(next_oldest_timestamp).span().data()), |
| 1146 | std::get<2>(next_oldest_timestamp).span().size())); |
| 1147 | |
| 1148 | } else { |
| 1149 | break; |
| 1150 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1151 | } |
| 1152 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1153 | return oldest_timestamp; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1156 | std::tuple<TimestampMerger::DeliveryTimestamp, |
| 1157 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1158 | TimestampMerger::PopOldest() { |
| 1159 | if (has_timestamps_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1160 | VLOG(1) << "Looking for matching timestamp for " |
| 1161 | << configuration::StrippedChannelToString( |
| 1162 | configuration_->channels()->Get(channel_index_)) |
| 1163 | << " (" << channel_index_ << ") " |
| 1164 | << " at " << std::get<0>(oldest_timestamp()); |
| 1165 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1166 | // Read the timestamps. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1167 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1168 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1169 | oldest_timestamp = PopTimestampHeap(); |
| 1170 | |
| 1171 | TimestampMerger::DeliveryTimestamp timestamp; |
| 1172 | timestamp.monotonic_event_time = |
| 1173 | monotonic_clock::time_point(chrono::nanoseconds( |
| 1174 | std::get<2>(oldest_timestamp).message().monotonic_sent_time())); |
| 1175 | timestamp.realtime_event_time = |
| 1176 | realtime_clock::time_point(chrono::nanoseconds( |
| 1177 | std::get<2>(oldest_timestamp).message().realtime_sent_time())); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1178 | timestamp.queue_index = |
| 1179 | std::get<2>(oldest_timestamp).message().queue_index(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1180 | |
| 1181 | // Consistency check. |
| 1182 | CHECK_EQ(timestamp.monotonic_event_time, std::get<0>(oldest_timestamp)); |
| 1183 | CHECK_EQ(std::get<2>(oldest_timestamp).message().queue_index(), |
| 1184 | std::get<1>(oldest_timestamp)); |
| 1185 | |
| 1186 | monotonic_clock::time_point remote_timestamp_monotonic_time( |
| 1187 | chrono::nanoseconds( |
| 1188 | std::get<2>(oldest_timestamp).message().monotonic_remote_time())); |
| 1189 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1190 | // See if we have any data. If not, pass the problem up the chain. |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1191 | if (message_heap_.empty()) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1192 | LOG(WARNING) << MaybeNodeName(configuration_->nodes()->Get(node_index_)) |
| 1193 | << "No data to match timestamp on " |
| 1194 | << configuration::CleanedChannelToString( |
| 1195 | configuration_->channels()->Get(channel_index_)) |
| 1196 | << " (" << channel_index_ << ")"; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1197 | return std::make_tuple(timestamp, |
| 1198 | std::move(std::get<2>(oldest_timestamp))); |
| 1199 | } |
| 1200 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1201 | while (true) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1202 | { |
| 1203 | // Ok, now try grabbing data until we find one which matches. |
| 1204 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 1205 | oldest_message_ref = oldest_message(); |
| 1206 | |
| 1207 | // Time at which the message was sent (this message is written from the |
| 1208 | // sending node's perspective. |
| 1209 | monotonic_clock::time_point remote_monotonic_time(chrono::nanoseconds( |
| 1210 | std::get<2>(oldest_message_ref)->monotonic_sent_time())); |
| 1211 | |
| 1212 | if (remote_monotonic_time < remote_timestamp_monotonic_time) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1213 | LOG(WARNING) << configuration_->nodes() |
| 1214 | ->Get(node_index_) |
| 1215 | ->name() |
| 1216 | ->string_view() |
| 1217 | << " Undelivered message, skipping. Remote time is " |
| 1218 | << remote_monotonic_time << " timestamp is " |
| 1219 | << remote_timestamp_monotonic_time << " on channel " |
| 1220 | << configuration::StrippedChannelToString( |
| 1221 | configuration_->channels()->Get(channel_index_)) |
| 1222 | << " (" << channel_index_ << ")"; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1223 | PopMessageHeap(); |
| 1224 | continue; |
| 1225 | } else if (remote_monotonic_time > remote_timestamp_monotonic_time) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1226 | LOG(WARNING) << configuration_->nodes() |
| 1227 | ->Get(node_index_) |
| 1228 | ->name() |
| 1229 | ->string_view() |
| 1230 | << " Data not found. Remote time should be " |
| 1231 | << remote_timestamp_monotonic_time |
| 1232 | << ", message time is " << remote_monotonic_time |
| 1233 | << " on channel " |
| 1234 | << configuration::StrippedChannelToString( |
| 1235 | configuration_->channels()->Get(channel_index_)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1236 | << " (" << channel_index_ << ")" |
| 1237 | << (VLOG_IS_ON(1) ? DebugString() : ""); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1238 | return std::make_tuple(timestamp, |
| 1239 | std::move(std::get<2>(oldest_timestamp))); |
| 1240 | } |
| 1241 | |
| 1242 | timestamp.monotonic_remote_time = remote_monotonic_time; |
| 1243 | } |
| 1244 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1245 | VLOG(1) << "Found matching data " |
| 1246 | << configuration::StrippedChannelToString( |
| 1247 | configuration_->channels()->Get(channel_index_)) |
| 1248 | << " (" << channel_index_ << ")"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1249 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1250 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1251 | oldest_message = PopMessageHeap(); |
| 1252 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1253 | timestamp.realtime_remote_time = |
| 1254 | realtime_clock::time_point(chrono::nanoseconds( |
| 1255 | std::get<2>(oldest_message).message().realtime_sent_time())); |
| 1256 | timestamp.remote_queue_index = |
| 1257 | std::get<2>(oldest_message).message().queue_index(); |
| 1258 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1259 | CHECK_EQ(timestamp.monotonic_remote_time, |
| 1260 | remote_timestamp_monotonic_time); |
| 1261 | |
| 1262 | CHECK_EQ(timestamp.remote_queue_index, |
| 1263 | std::get<2>(oldest_timestamp).message().remote_queue_index()) |
| 1264 | << ": " << FlatbufferToJson(&std::get<2>(oldest_timestamp).message()) |
| 1265 | << " data " |
| 1266 | << FlatbufferToJson(&std::get<2>(oldest_message).message()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1267 | |
Austin Schuh | 30dd5c5 | 2020-08-01 14:43:44 -0700 | [diff] [blame] | 1268 | return std::make_tuple(timestamp, std::move(std::get<2>(oldest_message))); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1269 | } |
| 1270 | } else { |
| 1271 | std::tuple<monotonic_clock::time_point, uint32_t, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1272 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1273 | oldest_message = PopMessageHeap(); |
| 1274 | |
| 1275 | TimestampMerger::DeliveryTimestamp timestamp; |
| 1276 | timestamp.monotonic_event_time = |
| 1277 | monotonic_clock::time_point(chrono::nanoseconds( |
| 1278 | std::get<2>(oldest_message).message().monotonic_sent_time())); |
| 1279 | timestamp.realtime_event_time = |
| 1280 | realtime_clock::time_point(chrono::nanoseconds( |
| 1281 | std::get<2>(oldest_message).message().realtime_sent_time())); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1282 | timestamp.queue_index = std::get<2>(oldest_message).message().queue_index(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1283 | timestamp.remote_queue_index = 0xffffffff; |
| 1284 | |
| 1285 | CHECK_EQ(std::get<0>(oldest_message), timestamp.monotonic_event_time); |
| 1286 | CHECK_EQ(std::get<1>(oldest_message), |
| 1287 | std::get<2>(oldest_message).message().queue_index()); |
| 1288 | |
Austin Schuh | 30dd5c5 | 2020-08-01 14:43:44 -0700 | [diff] [blame] | 1289 | return std::make_tuple(timestamp, std::move(std::get<2>(oldest_message))); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1293 | void TimestampMerger::NoticeAtEnd() { channel_merger_->NoticeAtEnd(); } |
| 1294 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1295 | namespace { |
| 1296 | std::vector<std::unique_ptr<SplitMessageReader>> MakeSplitMessageReaders( |
| 1297 | const std::vector<std::vector<std::string>> &filenames) { |
| 1298 | CHECK_GT(filenames.size(), 0u); |
| 1299 | // Build up all the SplitMessageReaders. |
| 1300 | std::vector<std::unique_ptr<SplitMessageReader>> result; |
| 1301 | for (const std::vector<std::string> &filenames : filenames) { |
| 1302 | result.emplace_back(std::make_unique<SplitMessageReader>(filenames)); |
| 1303 | } |
| 1304 | return result; |
| 1305 | } |
| 1306 | } // namespace |
| 1307 | |
| 1308 | ChannelMerger::ChannelMerger( |
| 1309 | const std::vector<std::vector<std::string>> &filenames) |
| 1310 | : split_message_readers_(MakeSplitMessageReaders(filenames)), |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 1311 | log_file_header_(split_message_readers_[0]->raw_log_file_header()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1312 | // Now, confirm that the configuration matches for each and pick a start time. |
| 1313 | // Also return the list of possible nodes. |
| 1314 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 1315 | split_message_readers_) { |
| 1316 | CHECK(CompareFlatBuffer(log_file_header_.message().configuration(), |
| 1317 | reader->log_file_header()->configuration())) |
| 1318 | << ": Replaying log files with different configurations isn't " |
| 1319 | "supported"; |
| 1320 | } |
| 1321 | |
| 1322 | nodes_ = configuration::GetNodes(configuration()); |
| 1323 | } |
| 1324 | |
| 1325 | bool ChannelMerger::SetNode(const Node *target_node) { |
| 1326 | std::vector<SplitMessageReader *> split_message_readers; |
| 1327 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 1328 | split_message_readers_) { |
| 1329 | split_message_readers.emplace_back(reader.get()); |
| 1330 | } |
| 1331 | |
| 1332 | // Go find a log_file_header for this node. |
| 1333 | { |
| 1334 | bool found_node = false; |
| 1335 | |
| 1336 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 1337 | split_message_readers_) { |
James Kuszmaul | fc273dc | 2020-05-09 17:56:19 -0700 | [diff] [blame] | 1338 | // In order to identify which logfile(s) map to the target node, do a |
| 1339 | // logical comparison of the nodes, by confirming that we are either in a |
| 1340 | // single-node setup (where the nodes will both be nullptr) or that the |
| 1341 | // node names match (but the other node fields--e.g., hostname lists--may |
| 1342 | // not). |
| 1343 | const bool both_null = |
| 1344 | reader->node() == nullptr && target_node == nullptr; |
| 1345 | const bool both_have_name = |
| 1346 | (reader->node() != nullptr) && (target_node != nullptr) && |
| 1347 | (reader->node()->has_name() && target_node->has_name()); |
| 1348 | const bool node_names_identical = |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 1349 | both_have_name && (reader->node()->name()->string_view() == |
| 1350 | target_node->name()->string_view()); |
James Kuszmaul | fc273dc | 2020-05-09 17:56:19 -0700 | [diff] [blame] | 1351 | if (both_null || node_names_identical) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1352 | if (!found_node) { |
| 1353 | found_node = true; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1354 | log_file_header_ = reader->raw_log_file_header(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1355 | VLOG(1) << "Found log file " << reader->filename() << " with node " |
| 1356 | << FlatbufferToJson(reader->node()) << " start_time " |
| 1357 | << monotonic_start_time(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1358 | } else { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1359 | // Find the earliest start time. That way, if we get a full log file |
| 1360 | // directly from the node, and a partial later, we start with the |
| 1361 | // full. Update our header to match that. |
| 1362 | const monotonic_clock::time_point new_monotonic_start_time( |
| 1363 | chrono::nanoseconds( |
| 1364 | reader->log_file_header()->monotonic_start_time())); |
| 1365 | const realtime_clock::time_point new_realtime_start_time( |
| 1366 | chrono::nanoseconds( |
| 1367 | reader->log_file_header()->realtime_start_time())); |
| 1368 | |
| 1369 | if (monotonic_start_time() == monotonic_clock::min_time || |
| 1370 | (new_monotonic_start_time != monotonic_clock::min_time && |
| 1371 | new_monotonic_start_time < monotonic_start_time())) { |
| 1372 | log_file_header_.mutable_message()->mutate_monotonic_start_time( |
| 1373 | new_monotonic_start_time.time_since_epoch().count()); |
| 1374 | log_file_header_.mutable_message()->mutate_realtime_start_time( |
| 1375 | new_realtime_start_time.time_since_epoch().count()); |
| 1376 | VLOG(1) << "Updated log file " << reader->filename() |
| 1377 | << " with node " << FlatbufferToJson(reader->node()) |
| 1378 | << " start_time " << new_monotonic_start_time; |
| 1379 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | if (!found_node) { |
| 1385 | LOG(WARNING) << "Failed to find log file for node " |
| 1386 | << FlatbufferToJson(target_node); |
| 1387 | return false; |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | // Build up all the timestamp mergers. This connects up all the |
| 1392 | // SplitMessageReaders. |
| 1393 | timestamp_mergers_.reserve(configuration()->channels()->size()); |
| 1394 | for (size_t channel_index = 0; |
| 1395 | channel_index < configuration()->channels()->size(); ++channel_index) { |
| 1396 | timestamp_mergers_.emplace_back( |
| 1397 | configuration(), split_message_readers, channel_index, |
| 1398 | configuration::GetNode(configuration(), target_node), this); |
| 1399 | } |
| 1400 | |
| 1401 | // And prime everything. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1402 | for (std::unique_ptr<SplitMessageReader> &split_message_reader : |
| 1403 | split_message_readers_) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1404 | split_message_reader->QueueMessages( |
| 1405 | split_message_reader->monotonic_start_time()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | node_ = configuration::GetNodeOrDie(configuration(), target_node); |
| 1409 | return true; |
| 1410 | } |
| 1411 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1412 | monotonic_clock::time_point ChannelMerger::OldestMessageTime() const { |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1413 | if (channel_heap_.empty()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1414 | return monotonic_clock::max_time; |
| 1415 | } |
| 1416 | return channel_heap_.front().first; |
| 1417 | } |
| 1418 | |
| 1419 | void ChannelMerger::PushChannelHeap(monotonic_clock::time_point timestamp, |
| 1420 | int channel_index) { |
| 1421 | // Pop and recreate the heap if it has already been pushed. And since we are |
| 1422 | // pushing again, we don't need to clear pushed. |
| 1423 | if (timestamp_mergers_[channel_index].pushed()) { |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1424 | const auto channel_iterator = std::find_if( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1425 | channel_heap_.begin(), channel_heap_.end(), |
| 1426 | [channel_index](const std::pair<monotonic_clock::time_point, int> x) { |
| 1427 | return x.second == channel_index; |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1428 | }); |
| 1429 | DCHECK(channel_iterator != channel_heap_.end()); |
| 1430 | if (std::get<0>(*channel_iterator) == timestamp) { |
| 1431 | // It's already in the heap, in the correct spot, so nothing |
| 1432 | // more for us to do here. |
| 1433 | return; |
| 1434 | } |
| 1435 | channel_heap_.erase(channel_iterator); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1436 | std::make_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1437 | ChannelHeapCompare); |
| 1438 | } |
| 1439 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1440 | if (timestamp == monotonic_clock::min_time) { |
| 1441 | timestamp_mergers_[channel_index].set_pushed(false); |
| 1442 | return; |
| 1443 | } |
| 1444 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1445 | channel_heap_.push_back(std::make_pair(timestamp, channel_index)); |
| 1446 | |
| 1447 | // The default sort puts the newest message first. Use a custom comparator to |
| 1448 | // put the oldest message first. |
| 1449 | std::push_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1450 | ChannelHeapCompare); |
| 1451 | } |
| 1452 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1453 | void ChannelMerger::VerifyHeaps() { |
Austin Schuh | 661a8d8 | 2020-09-13 17:25:56 -0700 | [diff] [blame] | 1454 | std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap = |
| 1455 | channel_heap_; |
| 1456 | std::make_heap(channel_heap.begin(), channel_heap.end(), &ChannelHeapCompare); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1457 | |
Austin Schuh | 661a8d8 | 2020-09-13 17:25:56 -0700 | [diff] [blame] | 1458 | for (size_t i = 0; i < channel_heap_.size(); ++i) { |
| 1459 | CHECK(channel_heap_[i] == channel_heap[i]) << ": Heaps diverged..."; |
| 1460 | CHECK_EQ( |
| 1461 | std::get<0>(channel_heap[i]), |
| 1462 | timestamp_mergers_[std::get<1>(channel_heap[i])].channel_merger_time()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1463 | } |
| 1464 | } |
| 1465 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1466 | std::tuple<TimestampMerger::DeliveryTimestamp, int, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1467 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1468 | ChannelMerger::PopOldest() { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1469 | CHECK_GT(channel_heap_.size(), 0u); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1470 | std::pair<monotonic_clock::time_point, int> oldest_channel_data = |
| 1471 | channel_heap_.front(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1472 | int channel_index = oldest_channel_data.second; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1473 | std::pop_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1474 | &ChannelHeapCompare); |
| 1475 | channel_heap_.pop_back(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1476 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1477 | timestamp_mergers_[channel_index].set_pushed(false); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1478 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1479 | TimestampMerger *merger = ×tamp_mergers_[channel_index]; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1480 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1481 | // Merger handles any queueing needed from here. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1482 | std::tuple<TimestampMerger::DeliveryTimestamp, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 1483 | SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1484 | message = merger->PopOldest(); |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1485 | DCHECK_EQ(std::get<0>(message).monotonic_event_time, |
| 1486 | oldest_channel_data.first) |
| 1487 | << ": channel_heap_ was corrupted for " << channel_index << ": " |
| 1488 | << DebugString(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1489 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1490 | CHECK_GE(std::get<0>(message).monotonic_event_time, last_popped_time_) |
| 1491 | << ": " << MaybeNodeName(log_file_header()->node()) |
| 1492 | << "Messages came off the queue out of order. " << DebugString(); |
| 1493 | last_popped_time_ = std::get<0>(message).monotonic_event_time; |
| 1494 | |
| 1495 | VLOG(1) << "Popped " << last_popped_time_ << " " |
| 1496 | << configuration::StrippedChannelToString( |
| 1497 | configuration()->channels()->Get(channel_index)) |
| 1498 | << " (" << channel_index << ")"; |
| 1499 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1500 | return std::make_tuple(std::get<0>(message), channel_index, |
| 1501 | std::move(std::get<1>(message))); |
| 1502 | } |
| 1503 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1504 | std::string SplitMessageReader::MessageHeaderQueue::DebugString() const { |
| 1505 | std::stringstream ss; |
| 1506 | for (size_t i = 0; i < data_.size(); ++i) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1507 | if (i < 5 || i + 5 > data_.size()) { |
| 1508 | if (timestamps) { |
| 1509 | ss << " msg: "; |
| 1510 | } else { |
| 1511 | ss << " timestamp: "; |
| 1512 | } |
| 1513 | ss << monotonic_clock::time_point( |
| 1514 | chrono::nanoseconds(data_[i].message().monotonic_sent_time())) |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1515 | << " (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1516 | << realtime_clock::time_point( |
| 1517 | chrono::nanoseconds(data_[i].message().realtime_sent_time())) |
| 1518 | << ") " << data_[i].message().queue_index(); |
| 1519 | if (timestamps) { |
| 1520 | ss << " <- remote " |
| 1521 | << monotonic_clock::time_point(chrono::nanoseconds( |
| 1522 | data_[i].message().monotonic_remote_time())) |
| 1523 | << " (" |
| 1524 | << realtime_clock::time_point(chrono::nanoseconds( |
| 1525 | data_[i].message().realtime_remote_time())) |
| 1526 | << ")"; |
| 1527 | } |
| 1528 | ss << "\n"; |
| 1529 | } else if (i == 5) { |
| 1530 | ss << " ...\n"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1531 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1532 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1533 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1534 | return ss.str(); |
| 1535 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1536 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1537 | std::string SplitMessageReader::DebugString(int channel) const { |
| 1538 | std::stringstream ss; |
| 1539 | ss << "[\n"; |
| 1540 | ss << channels_[channel].data.DebugString(); |
| 1541 | ss << " ]"; |
| 1542 | return ss.str(); |
| 1543 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1544 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1545 | std::string SplitMessageReader::DebugString(int channel, int node_index) const { |
| 1546 | std::stringstream ss; |
| 1547 | ss << "[\n"; |
| 1548 | ss << channels_[channel].timestamps[node_index].DebugString(); |
| 1549 | ss << " ]"; |
| 1550 | return ss.str(); |
| 1551 | } |
| 1552 | |
| 1553 | std::string TimestampMerger::DebugString() const { |
| 1554 | std::stringstream ss; |
| 1555 | |
| 1556 | if (timestamp_heap_.size() > 0) { |
| 1557 | ss << " timestamp_heap {\n"; |
| 1558 | std::vector< |
| 1559 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 1560 | timestamp_heap = timestamp_heap_; |
| 1561 | while (timestamp_heap.size() > 0u) { |
| 1562 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1563 | oldest_timestamp_reader = timestamp_heap.front(); |
| 1564 | |
| 1565 | ss << " " << std::get<2>(oldest_timestamp_reader) << " " |
| 1566 | << std::get<0>(oldest_timestamp_reader) << " queue_index (" |
| 1567 | << std::get<1>(oldest_timestamp_reader) << ") ttq " |
| 1568 | << std::get<2>(oldest_timestamp_reader)->time_to_queue() << " " |
| 1569 | << std::get<2>(oldest_timestamp_reader)->filename() << " -> " |
| 1570 | << std::get<2>(oldest_timestamp_reader) |
| 1571 | ->DebugString(channel_index_, node_index_) |
| 1572 | << "\n"; |
| 1573 | |
| 1574 | std::pop_heap(timestamp_heap.begin(), timestamp_heap.end(), |
| 1575 | &SplitMessageReaderHeapCompare); |
| 1576 | timestamp_heap.pop_back(); |
| 1577 | } |
| 1578 | ss << " }\n"; |
| 1579 | } |
| 1580 | |
| 1581 | ss << " message_heap {\n"; |
| 1582 | { |
| 1583 | std::vector< |
| 1584 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 1585 | message_heap = message_heap_; |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1586 | while (!message_heap.empty()) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1587 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1588 | oldest_message_reader = message_heap.front(); |
| 1589 | |
| 1590 | ss << " " << std::get<2>(oldest_message_reader) << " " |
| 1591 | << std::get<0>(oldest_message_reader) << " queue_index (" |
| 1592 | << std::get<1>(oldest_message_reader) << ") ttq " |
| 1593 | << std::get<2>(oldest_message_reader)->time_to_queue() << " " |
| 1594 | << std::get<2>(oldest_message_reader)->filename() << " -> " |
| 1595 | << std::get<2>(oldest_message_reader)->DebugString(channel_index_) |
| 1596 | << "\n"; |
| 1597 | |
| 1598 | std::pop_heap(message_heap.begin(), message_heap.end(), |
| 1599 | &SplitMessageReaderHeapCompare); |
| 1600 | message_heap.pop_back(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1601 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1602 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1603 | ss << " }"; |
| 1604 | |
| 1605 | return ss.str(); |
| 1606 | } |
| 1607 | |
| 1608 | std::string ChannelMerger::DebugString() const { |
| 1609 | std::stringstream ss; |
| 1610 | ss << "start_time " << realtime_start_time() << " " << monotonic_start_time() |
| 1611 | << "\n"; |
| 1612 | ss << "channel_heap {\n"; |
| 1613 | std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap = |
| 1614 | channel_heap_; |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1615 | while (!channel_heap.empty()) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1616 | std::tuple<monotonic_clock::time_point, int> channel = channel_heap.front(); |
| 1617 | ss << " " << std::get<0>(channel) << " (" << std::get<1>(channel) << ") " |
| 1618 | << configuration::CleanedChannelToString( |
| 1619 | configuration()->channels()->Get(std::get<1>(channel))) |
| 1620 | << "\n"; |
| 1621 | |
| 1622 | ss << timestamp_mergers_[std::get<1>(channel)].DebugString() << "\n"; |
| 1623 | |
| 1624 | std::pop_heap(channel_heap.begin(), channel_heap.end(), |
| 1625 | &ChannelHeapCompare); |
| 1626 | channel_heap.pop_back(); |
| 1627 | } |
| 1628 | ss << "}"; |
| 1629 | |
| 1630 | return ss.str(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1631 | } |
| 1632 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1633 | std::string MaybeNodeName(const Node *node) { |
| 1634 | if (node != nullptr) { |
| 1635 | return node->name()->str() + " "; |
| 1636 | } |
| 1637 | return ""; |
| 1638 | } |
| 1639 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 1640 | } // namespace aos::logger |