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> |
| 4 | #include <limits.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/uio.h> |
| 8 | |
| 9 | #include <vector> |
| 10 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 11 | #include "aos/configuration.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 12 | #include "aos/events/logging/logger_generated.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 | |
| 19 | DEFINE_int32(flush_size, 1000000, |
| 20 | "Number of outstanding bytes to allow before flushing to disk."); |
| 21 | |
| 22 | namespace aos { |
| 23 | namespace logger { |
| 24 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 25 | namespace chrono = std::chrono; |
| 26 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 27 | DetachedBufferWriter::DetachedBufferWriter(std::string_view filename) |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 28 | : filename_(filename) { |
| 29 | util::MkdirP(filename, 0777); |
| 30 | fd_ = open(std::string(filename).c_str(), |
| 31 | O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 0774); |
| 32 | VLOG(1) << "Opened " << filename << " for writing"; |
| 33 | PCHECK(fd_ != -1) << ": Failed to open " << filename << " for writing"; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | DetachedBufferWriter::~DetachedBufferWriter() { |
| 37 | Flush(); |
| 38 | PLOG_IF(ERROR, close(fd_) == -1) << " Failed to close logfile"; |
| 39 | } |
| 40 | |
| 41 | void DetachedBufferWriter::QueueSizedFlatbuffer( |
| 42 | flatbuffers::FlatBufferBuilder *fbb) { |
| 43 | QueueSizedFlatbuffer(fbb->Release()); |
| 44 | } |
| 45 | |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 46 | void DetachedBufferWriter::WriteSizedFlatbuffer( |
| 47 | absl::Span<const uint8_t> span) { |
| 48 | // Cheat aggressively... Write out the queued up data, and then write this |
| 49 | // data once without buffering. It is hard to make a DetachedBuffer out of |
| 50 | // this data, and we don't want to worry about lifetimes. |
| 51 | Flush(); |
| 52 | iovec_.clear(); |
| 53 | iovec_.reserve(1); |
| 54 | |
| 55 | struct iovec n; |
| 56 | n.iov_base = const_cast<uint8_t *>(span.data()); |
| 57 | n.iov_len = span.size(); |
| 58 | iovec_.emplace_back(n); |
| 59 | |
| 60 | const ssize_t written = writev(fd_, iovec_.data(), iovec_.size()); |
| 61 | |
| 62 | PCHECK(written == static_cast<ssize_t>(n.iov_len)) |
| 63 | << ": Wrote " << written << " expected " << n.iov_len; |
| 64 | } |
| 65 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 66 | void DetachedBufferWriter::QueueSizedFlatbuffer( |
| 67 | flatbuffers::DetachedBuffer &&buffer) { |
| 68 | queued_size_ += buffer.size(); |
| 69 | queue_.emplace_back(std::move(buffer)); |
| 70 | |
| 71 | // Flush if we are at the max number of iovs per writev, or have written |
| 72 | // enough data. Otherwise writev will fail with an invalid argument. |
| 73 | if (queued_size_ > static_cast<size_t>(FLAGS_flush_size) || |
| 74 | queue_.size() == IOV_MAX) { |
| 75 | Flush(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void DetachedBufferWriter::Flush() { |
| 80 | if (queue_.size() == 0u) { |
| 81 | return; |
| 82 | } |
| 83 | iovec_.clear(); |
| 84 | iovec_.reserve(queue_.size()); |
| 85 | size_t counted_size = 0; |
| 86 | for (size_t i = 0; i < queue_.size(); ++i) { |
| 87 | struct iovec n; |
| 88 | n.iov_base = queue_[i].data(); |
| 89 | n.iov_len = queue_[i].size(); |
| 90 | counted_size += n.iov_len; |
| 91 | iovec_.emplace_back(std::move(n)); |
| 92 | } |
| 93 | CHECK_EQ(counted_size, queued_size_); |
| 94 | const ssize_t written = writev(fd_, iovec_.data(), iovec_.size()); |
| 95 | |
| 96 | PCHECK(written == static_cast<ssize_t>(queued_size_)) |
| 97 | << ": Wrote " << written << " expected " << queued_size_; |
| 98 | |
| 99 | queued_size_ = 0; |
| 100 | queue_.clear(); |
| 101 | // TODO(austin): Handle partial writes in some way other than crashing... |
| 102 | } |
| 103 | |
| 104 | flatbuffers::Offset<MessageHeader> PackMessage( |
| 105 | flatbuffers::FlatBufferBuilder *fbb, const Context &context, |
| 106 | int channel_index, LogType log_type) { |
| 107 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset; |
| 108 | |
| 109 | switch (log_type) { |
| 110 | case LogType::kLogMessage: |
| 111 | case LogType::kLogMessageAndDeliveryTime: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 112 | case LogType::kLogRemoteMessage: |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 113 | data_offset = |
| 114 | fbb->CreateVector(static_cast<uint8_t *>(context.data), context.size); |
| 115 | break; |
| 116 | |
| 117 | case LogType::kLogDeliveryTimeOnly: |
| 118 | break; |
| 119 | } |
| 120 | |
| 121 | MessageHeader::Builder message_header_builder(*fbb); |
| 122 | message_header_builder.add_channel_index(channel_index); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 123 | |
| 124 | switch (log_type) { |
| 125 | case LogType::kLogRemoteMessage: |
| 126 | message_header_builder.add_queue_index(context.remote_queue_index); |
| 127 | message_header_builder.add_monotonic_sent_time( |
| 128 | context.monotonic_remote_time.time_since_epoch().count()); |
| 129 | message_header_builder.add_realtime_sent_time( |
| 130 | context.realtime_remote_time.time_since_epoch().count()); |
| 131 | break; |
| 132 | |
| 133 | case LogType::kLogMessage: |
| 134 | case LogType::kLogMessageAndDeliveryTime: |
| 135 | case LogType::kLogDeliveryTimeOnly: |
| 136 | message_header_builder.add_queue_index(context.queue_index); |
| 137 | message_header_builder.add_monotonic_sent_time( |
| 138 | context.monotonic_event_time.time_since_epoch().count()); |
| 139 | message_header_builder.add_realtime_sent_time( |
| 140 | context.realtime_event_time.time_since_epoch().count()); |
| 141 | break; |
| 142 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 143 | |
| 144 | switch (log_type) { |
| 145 | case LogType::kLogMessage: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 146 | case LogType::kLogRemoteMessage: |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 147 | message_header_builder.add_data(data_offset); |
| 148 | break; |
| 149 | |
| 150 | case LogType::kLogMessageAndDeliveryTime: |
| 151 | message_header_builder.add_data(data_offset); |
| 152 | [[fallthrough]]; |
| 153 | |
| 154 | case LogType::kLogDeliveryTimeOnly: |
| 155 | message_header_builder.add_monotonic_remote_time( |
| 156 | context.monotonic_remote_time.time_since_epoch().count()); |
| 157 | message_header_builder.add_realtime_remote_time( |
| 158 | context.realtime_remote_time.time_since_epoch().count()); |
| 159 | message_header_builder.add_remote_queue_index(context.remote_queue_index); |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | return message_header_builder.Finish(); |
| 164 | } |
| 165 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 166 | SpanReader::SpanReader(std::string_view filename) |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 167 | : filename_(filename), |
| 168 | fd_(open(std::string(filename).c_str(), O_RDONLY | O_CLOEXEC)) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 169 | PCHECK(fd_ != -1) << ": Failed to open " << filename; |
| 170 | } |
| 171 | |
| 172 | absl::Span<const uint8_t> SpanReader::ReadMessage() { |
| 173 | // Make sure we have enough for the size. |
| 174 | if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) { |
| 175 | if (!ReadBlock()) { |
| 176 | return absl::Span<const uint8_t>(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Now make sure we have enough for the message. |
| 181 | const size_t data_size = |
| 182 | flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) + |
| 183 | sizeof(flatbuffers::uoffset_t); |
| 184 | while (data_.size() < consumed_data_ + data_size) { |
| 185 | if (!ReadBlock()) { |
| 186 | return absl::Span<const uint8_t>(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // And return it, consuming the data. |
| 191 | const uint8_t *data_ptr = data_.data() + consumed_data_; |
| 192 | |
| 193 | consumed_data_ += data_size; |
| 194 | |
| 195 | return absl::Span<const uint8_t>(data_ptr, data_size); |
| 196 | } |
| 197 | |
| 198 | bool SpanReader::MessageAvailable() { |
| 199 | // Are we big enough to read the size? |
| 200 | if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | // Then, are we big enough to read the full message? |
| 205 | const size_t data_size = |
| 206 | flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) + |
| 207 | sizeof(flatbuffers::uoffset_t); |
| 208 | if (data_.size() < consumed_data_ + data_size) { |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | bool SpanReader::ReadBlock() { |
| 216 | if (end_of_file_) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | // Appends 256k. This is enough that the read call is efficient. We don't |
| 221 | // want to spend too much time reading small chunks because the syscalls for |
| 222 | // that will be expensive. |
| 223 | constexpr size_t kReadSize = 256 * 1024; |
| 224 | |
| 225 | // Strip off any unused data at the front. |
| 226 | if (consumed_data_ != 0) { |
| 227 | data_.erase(data_.begin(), data_.begin() + consumed_data_); |
| 228 | consumed_data_ = 0; |
| 229 | } |
| 230 | |
| 231 | const size_t starting_size = data_.size(); |
| 232 | |
| 233 | // This should automatically grow the backing store. It won't shrink if we |
| 234 | // get a small chunk later. This reduces allocations when we want to append |
| 235 | // more data. |
| 236 | data_.resize(data_.size() + kReadSize); |
| 237 | |
| 238 | ssize_t count = read(fd_, &data_[starting_size], kReadSize); |
| 239 | data_.resize(starting_size + std::max(count, static_cast<ssize_t>(0))); |
| 240 | if (count == 0) { |
| 241 | end_of_file_ = true; |
| 242 | return false; |
| 243 | } |
| 244 | PCHECK(count > 0); |
| 245 | |
| 246 | return true; |
| 247 | } |
| 248 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 249 | FlatbufferVector<LogFileHeader> ReadHeader(std::string_view filename) { |
| 250 | SpanReader span_reader(filename); |
| 251 | // Make sure we have enough to read the size. |
| 252 | absl::Span<const uint8_t> config_data = span_reader.ReadMessage(); |
| 253 | |
| 254 | // Make sure something was read. |
| 255 | CHECK(config_data != absl::Span<const uint8_t>()); |
| 256 | |
| 257 | // And copy the config so we have it forever. |
| 258 | std::vector<uint8_t> data( |
| 259 | config_data.begin() + sizeof(flatbuffers::uoffset_t), config_data.end()); |
| 260 | return FlatbufferVector<LogFileHeader>(std::move(data)); |
| 261 | } |
| 262 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 263 | MessageReader::MessageReader(std::string_view filename) |
| 264 | : span_reader_(filename) { |
| 265 | // Make sure we have enough to read the size. |
| 266 | absl::Span<const uint8_t> config_data = span_reader_.ReadMessage(); |
| 267 | |
| 268 | // Make sure something was read. |
| 269 | CHECK(config_data != absl::Span<const uint8_t>()); |
| 270 | |
| 271 | // And copy the config so we have it forever. |
| 272 | configuration_ = std::vector<uint8_t>(config_data.begin(), config_data.end()); |
| 273 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 274 | max_out_of_order_duration_ = |
| 275 | std::chrono::nanoseconds(log_file_header()->max_out_of_order_duration()); |
| 276 | |
| 277 | VLOG(1) << "Opened " << filename << " as node " |
| 278 | << FlatbufferToJson(log_file_header()->node()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | std::optional<FlatbufferVector<MessageHeader>> MessageReader::ReadMessage() { |
| 282 | absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage(); |
| 283 | if (msg_data == absl::Span<const uint8_t>()) { |
| 284 | return std::nullopt; |
| 285 | } |
| 286 | |
| 287 | FlatbufferVector<MessageHeader> result{std::vector<uint8_t>( |
| 288 | msg_data.begin() + sizeof(flatbuffers::uoffset_t), msg_data.end())}; |
| 289 | |
| 290 | const monotonic_clock::time_point timestamp = monotonic_clock::time_point( |
| 291 | chrono::nanoseconds(result.message().monotonic_sent_time())); |
| 292 | |
| 293 | newest_timestamp_ = std::max(newest_timestamp_, timestamp); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 294 | VLOG(2) << "Read from " << filename() << " data " << FlatbufferToJson(result); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 295 | return std::move(result); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 298 | SplitMessageReader::SplitMessageReader( |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 299 | const std::vector<std::string> &filenames) |
| 300 | : filenames_(filenames), |
| 301 | log_file_header_(FlatbufferDetachedBuffer<LogFileHeader>::Empty()) { |
| 302 | CHECK(NextLogFile()) << ": filenames is empty. Need files to read."; |
| 303 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 304 | // Grab any log file header. They should all match (and we will check as we |
| 305 | // open more of them). |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 306 | log_file_header_ = CopyFlatBuffer(message_reader_->log_file_header()); |
| 307 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 308 | // Setup per channel state. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 309 | channels_.resize(configuration()->channels()->size()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 310 | for (ChannelData &channel_data : channels_) { |
| 311 | channel_data.data.split_reader = this; |
| 312 | // Build up the timestamp list. |
| 313 | if (configuration::MultiNode(configuration())) { |
| 314 | channel_data.timestamps.resize(configuration()->nodes()->size()); |
| 315 | for (MessageHeaderQueue &queue : channel_data.timestamps) { |
| 316 | queue.timestamps = true; |
| 317 | queue.split_reader = this; |
| 318 | } |
| 319 | } |
| 320 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 321 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 322 | // Build up channels_to_write_ as an optimization to make it fast to figure |
| 323 | // out which datastructure to place any new data from a channel on. |
| 324 | for (const Channel *channel : *configuration()->channels()) { |
| 325 | // This is the main case. We will only see data on this node. |
| 326 | if (configuration::ChannelIsSendableOnNode(channel, node())) { |
| 327 | channels_to_write_.emplace_back( |
| 328 | &channels_[channels_to_write_.size()].data); |
| 329 | } else |
| 330 | // If we can't send, but can receive, we should be able to see |
| 331 | // timestamps here. |
| 332 | if (configuration::ChannelIsReadableOnNode(channel, node())) { |
| 333 | channels_to_write_.emplace_back( |
| 334 | &(channels_[channels_to_write_.size()] |
| 335 | .timestamps[configuration::GetNodeIndex(configuration(), |
| 336 | node())])); |
| 337 | } else { |
| 338 | channels_to_write_.emplace_back(nullptr); |
| 339 | } |
| 340 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 343 | bool SplitMessageReader::NextLogFile() { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 344 | if (next_filename_index_ == filenames_.size()) { |
| 345 | return false; |
| 346 | } |
| 347 | message_reader_ = |
| 348 | std::make_unique<MessageReader>(filenames_[next_filename_index_]); |
| 349 | |
| 350 | // We can't support the config diverging between two log file headers. See if |
| 351 | // they are the same. |
| 352 | if (next_filename_index_ != 0) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 353 | CHECK(CompareFlatBuffer(&log_file_header_.message(), |
| 354 | message_reader_->log_file_header())) |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 355 | << ": Header is different between log file chunks " |
| 356 | << filenames_[next_filename_index_] << " and " |
| 357 | << filenames_[next_filename_index_ - 1] << ", this is not supported."; |
| 358 | } |
| 359 | |
| 360 | ++next_filename_index_; |
| 361 | return true; |
| 362 | } |
| 363 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 364 | bool SplitMessageReader::QueueMessages( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 365 | monotonic_clock::time_point last_dequeued_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 366 | // TODO(austin): Once we are happy that everything works, read a 256kb chunk |
| 367 | // to reduce the need to re-heap down below. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 368 | |
| 369 | // Special case no more data. Otherwise we blow up on the CHECK statement |
| 370 | // confirming that we have enough data queued. |
| 371 | if (at_end_) { |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | // If this isn't the first time around, confirm that we had enough data queued |
| 376 | // to follow the contract. |
| 377 | if (time_to_queue_ != monotonic_clock::min_time) { |
| 378 | CHECK_LE(last_dequeued_time, |
| 379 | newest_timestamp() - max_out_of_order_duration()) |
| 380 | << " node " << FlatbufferToJson(node()) << " on " << this; |
| 381 | |
| 382 | // Bail if there is enough data already queued. |
| 383 | if (last_dequeued_time < time_to_queue_) { |
| 384 | VLOG(1) << "All up to date on " << this << ", dequeued " |
| 385 | << last_dequeued_time << " queue time " << time_to_queue_; |
| 386 | return true; |
| 387 | } |
| 388 | } else { |
| 389 | // Startup takes a special dance. We want to queue up until the start time, |
| 390 | // but we then want to find the next message to read. The conservative |
| 391 | // answer is to immediately trigger a second requeue to get things moving. |
| 392 | time_to_queue_ = monotonic_start_time(); |
| 393 | QueueMessages(time_to_queue_); |
| 394 | } |
| 395 | |
| 396 | // If we are asked to queue, queue for at least max_out_of_order_duration past |
| 397 | // the last known time in the log file (ie the newest timestep read). As long |
| 398 | // as we requeue exactly when time_to_queue_ is dequeued and go no further, we |
| 399 | // are safe. And since we pop in order, that works. |
| 400 | // |
| 401 | // Special case the start of the log file. There should be at most 1 message |
| 402 | // from each channel at the start of the log file. So always force the start |
| 403 | // of the log file to just be read. |
| 404 | time_to_queue_ = std::max(time_to_queue_, newest_timestamp()); |
| 405 | VLOG(1) << "Queueing, going until " << time_to_queue_ << " " << filename(); |
| 406 | |
| 407 | bool was_emplaced = false; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 408 | while (true) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 409 | // Stop if we have enough. |
| 410 | if (newest_timestamp() > |
| 411 | time_to_queue_ + max_out_of_order_duration() && |
| 412 | was_emplaced) { |
| 413 | VLOG(1) << "Done queueing on " << this << ", queued to " |
| 414 | << newest_timestamp() << " with requeue time " << time_to_queue_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 415 | return true; |
| 416 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 417 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 418 | if (std::optional<FlatbufferVector<MessageHeader>> msg = |
| 419 | message_reader_->ReadMessage()) { |
| 420 | const MessageHeader &header = msg.value().message(); |
| 421 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 422 | const monotonic_clock::time_point timestamp = monotonic_clock::time_point( |
| 423 | chrono::nanoseconds(header.monotonic_sent_time())); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 424 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 425 | VLOG(1) << "Queued " << this << " " << filename() |
| 426 | << " ttq: " << time_to_queue_ << " now " |
| 427 | << newest_timestamp() << " start time " |
| 428 | << monotonic_start_time() << " " << FlatbufferToJson(&header); |
| 429 | |
| 430 | const int channel_index = header.channel_index(); |
| 431 | was_emplaced = channels_to_write_[channel_index]->emplace_back( |
| 432 | std::move(msg.value())); |
| 433 | if (was_emplaced) { |
| 434 | newest_timestamp_ = std::max(newest_timestamp_, timestamp); |
| 435 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 436 | } else { |
| 437 | if (!NextLogFile()) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 438 | VLOG(1) << "End of log file " << filenames_.back(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 439 | at_end_ = true; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 440 | for (MessageHeaderQueue *queue : channels_to_write_) { |
| 441 | if (queue == nullptr || queue->timestamp_merger == nullptr) { |
| 442 | continue; |
| 443 | } |
| 444 | queue->timestamp_merger->NoticeAtEnd(); |
| 445 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 446 | return false; |
| 447 | } |
| 448 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 449 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void SplitMessageReader::SetTimestampMerger(TimestampMerger *timestamp_merger, |
| 453 | int channel_index, |
| 454 | const Node *target_node) { |
| 455 | const Node *reinterpreted_target_node = |
| 456 | configuration::GetNodeOrDie(configuration(), target_node); |
| 457 | const Channel *const channel = |
| 458 | configuration()->channels()->Get(channel_index); |
| 459 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 460 | VLOG(1) << " Configuring merger " << this << " for channel " << channel_index |
| 461 | << " " |
| 462 | << configuration::CleanedChannelToString( |
| 463 | configuration()->channels()->Get(channel_index)); |
| 464 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 465 | MessageHeaderQueue *message_header_queue = nullptr; |
| 466 | |
| 467 | // Figure out if this log file is from our point of view, or the other node's |
| 468 | // point of view. |
| 469 | if (node() == reinterpreted_target_node) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 470 | VLOG(1) << " Replaying as logged node " << filename(); |
| 471 | |
| 472 | if (configuration::ChannelIsSendableOnNode(channel, node())) { |
| 473 | VLOG(1) << " Data on node"; |
| 474 | message_header_queue = &(channels_[channel_index].data); |
| 475 | } else if (configuration::ChannelIsReadableOnNode(channel, node())) { |
| 476 | VLOG(1) << " Timestamps on node"; |
| 477 | message_header_queue = |
| 478 | &(channels_[channel_index].timestamps[configuration::GetNodeIndex( |
| 479 | configuration(), node())]); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 480 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 481 | VLOG(1) << " Dropping"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 482 | } |
| 483 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 484 | VLOG(1) << " Replaying as other node " << filename(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 485 | // 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] | 486 | // data is data that is sent from our node and received on theirs. |
| 487 | if (configuration::ChannelIsReadableOnNode(channel, |
| 488 | reinterpreted_target_node) && |
| 489 | configuration::ChannelIsSendableOnNode(channel, node())) { |
| 490 | VLOG(1) << " Readable on target node"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 491 | // Data from another node. |
| 492 | message_header_queue = &(channels_[channel_index].data); |
| 493 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 494 | VLOG(1) << " Dropping"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 495 | // This is either not sendable on the other node, or is a timestamp and |
| 496 | // therefore not interesting. |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | // If we found one, write it down. This will be nullptr when there is nothing |
| 501 | // relevant on this channel on this node for the target node. In that case, |
| 502 | // we want to drop the message instead of queueing it. |
| 503 | if (message_header_queue != nullptr) { |
| 504 | message_header_queue->timestamp_merger = timestamp_merger; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 509 | FlatbufferVector<MessageHeader>> |
| 510 | SplitMessageReader::PopOldest(int channel_index) { |
| 511 | CHECK_GT(channels_[channel_index].data.size(), 0u); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 512 | const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 513 | timestamp = channels_[channel_index].data.front_timestamp(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 514 | FlatbufferVector<MessageHeader> front = |
| 515 | std::move(channels_[channel_index].data.front()); |
| 516 | channels_[channel_index].data.pop_front(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 517 | |
| 518 | VLOG(1) << "Popped " << this << " " << std::get<0>(timestamp); |
| 519 | |
| 520 | QueueMessages(std::get<0>(timestamp)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 521 | |
| 522 | return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp), |
| 523 | std::move(front)); |
| 524 | } |
| 525 | |
| 526 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 527 | FlatbufferVector<MessageHeader>> |
| 528 | SplitMessageReader::PopOldest(int channel, int node_index) { |
| 529 | CHECK_GT(channels_[channel].timestamps[node_index].size(), 0u); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 530 | const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 531 | timestamp = channels_[channel].timestamps[node_index].front_timestamp(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 532 | FlatbufferVector<MessageHeader> front = |
| 533 | std::move(channels_[channel].timestamps[node_index].front()); |
| 534 | channels_[channel].timestamps[node_index].pop_front(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 535 | |
| 536 | VLOG(1) << "Popped " << this << " " << std::get<0>(timestamp); |
| 537 | |
| 538 | QueueMessages(std::get<0>(timestamp)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 539 | |
| 540 | return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp), |
| 541 | std::move(front)); |
| 542 | } |
| 543 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 544 | bool SplitMessageReader::MessageHeaderQueue::emplace_back( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 545 | FlatbufferVector<MessageHeader> &&msg) { |
| 546 | CHECK(split_reader != nullptr); |
| 547 | |
| 548 | // If there is no timestamp merger for this queue, nobody is listening. Drop |
| 549 | // the message. This happens when a log file from another node is replayed, |
| 550 | // and the timestamp mergers down stream just don't care. |
| 551 | if (timestamp_merger == nullptr) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 552 | return false; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | CHECK(timestamps != msg.message().has_data()) |
| 556 | << ": Got timestamps and data mixed up on a node. " |
| 557 | << FlatbufferToJson(msg); |
| 558 | |
| 559 | data_.emplace_back(std::move(msg)); |
| 560 | |
| 561 | if (data_.size() == 1u) { |
| 562 | // Yup, new data. Notify. |
| 563 | if (timestamps) { |
| 564 | timestamp_merger->UpdateTimestamp(split_reader, front_timestamp()); |
| 565 | } else { |
| 566 | timestamp_merger->Update(split_reader, front_timestamp()); |
| 567 | } |
| 568 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 569 | |
| 570 | return true; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | void SplitMessageReader::MessageHeaderQueue::pop_front() { |
| 574 | data_.pop_front(); |
| 575 | if (data_.size() != 0u) { |
| 576 | // Yup, new data. |
| 577 | if (timestamps) { |
| 578 | timestamp_merger->UpdateTimestamp(split_reader, front_timestamp()); |
| 579 | } else { |
| 580 | timestamp_merger->Update(split_reader, front_timestamp()); |
| 581 | } |
| 582 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | namespace { |
| 586 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 587 | bool SplitMessageReaderHeapCompare( |
| 588 | const std::tuple<monotonic_clock::time_point, uint32_t, |
| 589 | SplitMessageReader *> |
| 590 | first, |
| 591 | const std::tuple<monotonic_clock::time_point, uint32_t, |
| 592 | SplitMessageReader *> |
| 593 | second) { |
| 594 | if (std::get<0>(first) > std::get<0>(second)) { |
| 595 | return true; |
| 596 | } else if (std::get<0>(first) == std::get<0>(second)) { |
| 597 | if (std::get<1>(first) > std::get<1>(second)) { |
| 598 | return true; |
| 599 | } else if (std::get<1>(first) == std::get<1>(second)) { |
| 600 | return std::get<2>(first) > std::get<2>(second); |
| 601 | } else { |
| 602 | return false; |
| 603 | } |
| 604 | } else { |
| 605 | return false; |
| 606 | } |
| 607 | } |
| 608 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 609 | bool ChannelHeapCompare( |
| 610 | const std::pair<monotonic_clock::time_point, int> first, |
| 611 | const std::pair<monotonic_clock::time_point, int> second) { |
| 612 | if (first.first > second.first) { |
| 613 | return true; |
| 614 | } else if (first.first == second.first) { |
| 615 | return first.second > second.second; |
| 616 | } else { |
| 617 | return false; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | } // namespace |
| 622 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 623 | TimestampMerger::TimestampMerger( |
| 624 | const Configuration *configuration, |
| 625 | std::vector<SplitMessageReader *> split_message_readers, int channel_index, |
| 626 | const Node *target_node, ChannelMerger *channel_merger) |
| 627 | : configuration_(configuration), |
| 628 | split_message_readers_(std::move(split_message_readers)), |
| 629 | channel_index_(channel_index), |
| 630 | node_index_(configuration::MultiNode(configuration) |
| 631 | ? configuration::GetNodeIndex(configuration, target_node) |
| 632 | : -1), |
| 633 | channel_merger_(channel_merger) { |
| 634 | // Tell the readers we care so they know who to notify. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 635 | VLOG(1) << "Configuring channel " << channel_index << " target node " |
| 636 | << FlatbufferToJson(target_node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 637 | for (SplitMessageReader *reader : split_message_readers_) { |
| 638 | reader->SetTimestampMerger(this, channel_index, target_node); |
| 639 | } |
| 640 | |
| 641 | // And then determine if we need to track timestamps. |
| 642 | const Channel *channel = configuration->channels()->Get(channel_index); |
| 643 | if (!configuration::ChannelIsSendableOnNode(channel, target_node) && |
| 644 | configuration::ChannelIsReadableOnNode(channel, target_node)) { |
| 645 | has_timestamps_ = true; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | void TimestampMerger::PushMessageHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 650 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 651 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 652 | SplitMessageReader *split_message_reader) { |
| 653 | DCHECK(std::find_if(message_heap_.begin(), message_heap_.end(), |
| 654 | [split_message_reader]( |
| 655 | const std::tuple<monotonic_clock::time_point, |
| 656 | uint32_t, SplitMessageReader *> |
| 657 | x) { |
| 658 | return std::get<2>(x) == split_message_reader; |
| 659 | }) == message_heap_.end()) |
| 660 | << ": Pushing message when it is already in the heap."; |
| 661 | |
| 662 | message_heap_.push_back(std::make_tuple( |
| 663 | std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader)); |
| 664 | |
| 665 | std::push_heap(message_heap_.begin(), message_heap_.end(), |
| 666 | &SplitMessageReaderHeapCompare); |
| 667 | |
| 668 | // If we are just a data merger, don't wait for timestamps. |
| 669 | if (!has_timestamps_) { |
| 670 | channel_merger_->Update(std::get<0>(timestamp), channel_index_); |
| 671 | pushed_ = true; |
| 672 | } |
| 673 | } |
| 674 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 675 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 676 | TimestampMerger::oldest_message() const { |
| 677 | CHECK_GT(message_heap_.size(), 0u); |
| 678 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 679 | oldest_message_reader = message_heap_.front(); |
| 680 | return std::get<2>(oldest_message_reader)->oldest_message(channel_index_); |
| 681 | } |
| 682 | |
| 683 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 684 | TimestampMerger::oldest_timestamp() const { |
| 685 | CHECK_GT(timestamp_heap_.size(), 0u); |
| 686 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 687 | oldest_message_reader = timestamp_heap_.front(); |
| 688 | return std::get<2>(oldest_message_reader) |
| 689 | ->oldest_message(channel_index_, node_index_); |
| 690 | } |
| 691 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 692 | void TimestampMerger::PushTimestampHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 693 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 694 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 695 | SplitMessageReader *split_message_reader) { |
| 696 | DCHECK(std::find_if(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 697 | [split_message_reader]( |
| 698 | const std::tuple<monotonic_clock::time_point, |
| 699 | uint32_t, SplitMessageReader *> |
| 700 | x) { |
| 701 | return std::get<2>(x) == split_message_reader; |
| 702 | }) == timestamp_heap_.end()) |
| 703 | << ": Pushing timestamp when it is already in the heap."; |
| 704 | |
| 705 | timestamp_heap_.push_back(std::make_tuple( |
| 706 | std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader)); |
| 707 | |
| 708 | std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 709 | SplitMessageReaderHeapCompare); |
| 710 | |
| 711 | // If we are a timestamp merger, don't wait for data. Missing data will be |
| 712 | // caught at read time. |
| 713 | if (has_timestamps_) { |
| 714 | channel_merger_->Update(std::get<0>(timestamp), channel_index_); |
| 715 | pushed_ = true; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 720 | FlatbufferVector<MessageHeader>> |
| 721 | TimestampMerger::PopMessageHeap() { |
| 722 | // Pop the oldest message reader pointer off the heap. |
| 723 | CHECK_GT(message_heap_.size(), 0u); |
| 724 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 725 | oldest_message_reader = message_heap_.front(); |
| 726 | |
| 727 | std::pop_heap(message_heap_.begin(), message_heap_.end(), |
| 728 | &SplitMessageReaderHeapCompare); |
| 729 | message_heap_.pop_back(); |
| 730 | |
| 731 | // Pop the oldest message. This re-pushes any messages from the reader to the |
| 732 | // message heap. |
| 733 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 734 | FlatbufferVector<MessageHeader>> |
| 735 | oldest_message = |
| 736 | std::get<2>(oldest_message_reader)->PopOldest(channel_index_); |
| 737 | |
| 738 | // Confirm that the time and queue_index we have recorded matches. |
| 739 | CHECK_EQ(std::get<0>(oldest_message), std::get<0>(oldest_message_reader)); |
| 740 | CHECK_EQ(std::get<1>(oldest_message), std::get<1>(oldest_message_reader)); |
| 741 | |
| 742 | // Now, keep reading until we have found all duplicates. |
| 743 | while (message_heap_.size() > 0u) { |
| 744 | // See if it is a duplicate. |
| 745 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 746 | next_oldest_message_reader = message_heap_.front(); |
| 747 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 748 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 749 | next_oldest_message_time = std::get<2>(next_oldest_message_reader) |
| 750 | ->oldest_message(channel_index_); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 751 | |
| 752 | if (std::get<0>(next_oldest_message_time) == std::get<0>(oldest_message) && |
| 753 | std::get<1>(next_oldest_message_time) == std::get<1>(oldest_message)) { |
| 754 | // Pop the message reader pointer. |
| 755 | std::pop_heap(message_heap_.begin(), message_heap_.end(), |
| 756 | &SplitMessageReaderHeapCompare); |
| 757 | message_heap_.pop_back(); |
| 758 | |
| 759 | // Pop the next oldest message. This re-pushes any messages from the |
| 760 | // reader. |
| 761 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 762 | FlatbufferVector<MessageHeader>> |
| 763 | next_oldest_message = std::get<2>(next_oldest_message_reader) |
| 764 | ->PopOldest(channel_index_); |
| 765 | |
| 766 | // And make sure the message matches in it's entirety. |
| 767 | CHECK(std::get<2>(oldest_message).span() == |
| 768 | std::get<2>(next_oldest_message).span()) |
| 769 | << ": Data at the same timestamp doesn't match."; |
| 770 | } else { |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | return oldest_message; |
| 776 | } |
| 777 | |
| 778 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 779 | FlatbufferVector<MessageHeader>> |
| 780 | TimestampMerger::PopTimestampHeap() { |
| 781 | // Pop the oldest message reader pointer off the heap. |
| 782 | CHECK_GT(timestamp_heap_.size(), 0u); |
| 783 | |
| 784 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 785 | oldest_timestamp_reader = timestamp_heap_.front(); |
| 786 | |
| 787 | std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 788 | &SplitMessageReaderHeapCompare); |
| 789 | timestamp_heap_.pop_back(); |
| 790 | |
| 791 | CHECK(node_index_ != -1) << ": Timestamps in a single node environment"; |
| 792 | |
| 793 | // Pop the oldest message. This re-pushes any timestamps from the reader to |
| 794 | // the timestamp heap. |
| 795 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 796 | FlatbufferVector<MessageHeader>> |
| 797 | oldest_timestamp = std::get<2>(oldest_timestamp_reader) |
| 798 | ->PopOldest(channel_index_, node_index_); |
| 799 | |
| 800 | // Confirm that the time we have recorded matches. |
| 801 | CHECK_EQ(std::get<0>(oldest_timestamp), std::get<0>(oldest_timestamp_reader)); |
| 802 | CHECK_EQ(std::get<1>(oldest_timestamp), std::get<1>(oldest_timestamp_reader)); |
| 803 | |
| 804 | // TODO(austin): What if we get duplicate timestamps? |
| 805 | |
| 806 | return oldest_timestamp; |
| 807 | } |
| 808 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 809 | TimestampMerger::DeliveryTimestamp TimestampMerger::OldestTimestamp() const { |
| 810 | if (!has_timestamps_ || timestamp_heap_.size() == 0u) { |
| 811 | return TimestampMerger::DeliveryTimestamp{}; |
| 812 | } |
| 813 | |
| 814 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 815 | oldest_timestamp_reader = timestamp_heap_.front(); |
| 816 | |
| 817 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 818 | oldest_timestamp = std::get<2>(oldest_timestamp_reader) |
| 819 | ->oldest_message(channel_index_, node_index_); |
| 820 | |
| 821 | TimestampMerger::DeliveryTimestamp timestamp; |
| 822 | timestamp.monotonic_event_time = |
| 823 | monotonic_clock::time_point(chrono::nanoseconds( |
| 824 | std::get<2>(oldest_timestamp)->monotonic_sent_time())); |
| 825 | timestamp.realtime_event_time = realtime_clock::time_point( |
| 826 | chrono::nanoseconds(std::get<2>(oldest_timestamp)->realtime_sent_time())); |
| 827 | |
| 828 | timestamp.monotonic_remote_time = |
| 829 | monotonic_clock::time_point(chrono::nanoseconds( |
| 830 | std::get<2>(oldest_timestamp)->monotonic_remote_time())); |
| 831 | timestamp.realtime_remote_time = |
| 832 | realtime_clock::time_point(chrono::nanoseconds( |
| 833 | std::get<2>(oldest_timestamp)->realtime_remote_time())); |
| 834 | |
| 835 | timestamp.remote_queue_index = std::get<2>(oldest_timestamp)->queue_index(); |
| 836 | return timestamp; |
| 837 | } |
| 838 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 839 | std::tuple<TimestampMerger::DeliveryTimestamp, FlatbufferVector<MessageHeader>> |
| 840 | TimestampMerger::PopOldest() { |
| 841 | if (has_timestamps_) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 842 | // Read the timestamps. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 843 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 844 | FlatbufferVector<MessageHeader>> |
| 845 | oldest_timestamp = PopTimestampHeap(); |
| 846 | |
| 847 | TimestampMerger::DeliveryTimestamp timestamp; |
| 848 | timestamp.monotonic_event_time = |
| 849 | monotonic_clock::time_point(chrono::nanoseconds( |
| 850 | std::get<2>(oldest_timestamp).message().monotonic_sent_time())); |
| 851 | timestamp.realtime_event_time = |
| 852 | realtime_clock::time_point(chrono::nanoseconds( |
| 853 | std::get<2>(oldest_timestamp).message().realtime_sent_time())); |
| 854 | |
| 855 | // Consistency check. |
| 856 | CHECK_EQ(timestamp.monotonic_event_time, std::get<0>(oldest_timestamp)); |
| 857 | CHECK_EQ(std::get<2>(oldest_timestamp).message().queue_index(), |
| 858 | std::get<1>(oldest_timestamp)); |
| 859 | |
| 860 | monotonic_clock::time_point remote_timestamp_monotonic_time( |
| 861 | chrono::nanoseconds( |
| 862 | std::get<2>(oldest_timestamp).message().monotonic_remote_time())); |
| 863 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 864 | // See if we have any data. If not, pass the problem up the chain. |
| 865 | if (message_heap_.size() == 0u) { |
| 866 | VLOG(1) << "No data to match timestamp on " |
| 867 | << configuration::CleanedChannelToString( |
| 868 | configuration_->channels()->Get(channel_index_)); |
| 869 | return std::make_tuple(timestamp, |
| 870 | std::move(std::get<2>(oldest_timestamp))); |
| 871 | } |
| 872 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 873 | while (true) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 874 | { |
| 875 | // Ok, now try grabbing data until we find one which matches. |
| 876 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 877 | oldest_message_ref = oldest_message(); |
| 878 | |
| 879 | // Time at which the message was sent (this message is written from the |
| 880 | // sending node's perspective. |
| 881 | monotonic_clock::time_point remote_monotonic_time(chrono::nanoseconds( |
| 882 | std::get<2>(oldest_message_ref)->monotonic_sent_time())); |
| 883 | |
| 884 | if (remote_monotonic_time < remote_timestamp_monotonic_time) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 885 | VLOG(1) << "Undelivered message, skipping. Remote time is " |
| 886 | << remote_monotonic_time << " timestamp is " |
| 887 | << remote_timestamp_monotonic_time << " on channel " |
| 888 | << channel_index_; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 889 | PopMessageHeap(); |
| 890 | continue; |
| 891 | } else if (remote_monotonic_time > remote_timestamp_monotonic_time) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 892 | VLOG(1) << "Data not found. Remote time should be " |
| 893 | << remote_timestamp_monotonic_time << " on channel " |
| 894 | << channel_index_; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 895 | return std::make_tuple(timestamp, |
| 896 | std::move(std::get<2>(oldest_timestamp))); |
| 897 | } |
| 898 | |
| 899 | timestamp.monotonic_remote_time = remote_monotonic_time; |
| 900 | } |
| 901 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 902 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 903 | FlatbufferVector<MessageHeader>> |
| 904 | oldest_message = PopMessageHeap(); |
| 905 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 906 | timestamp.realtime_remote_time = |
| 907 | realtime_clock::time_point(chrono::nanoseconds( |
| 908 | std::get<2>(oldest_message).message().realtime_sent_time())); |
| 909 | timestamp.remote_queue_index = |
| 910 | std::get<2>(oldest_message).message().queue_index(); |
| 911 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 912 | CHECK_EQ(timestamp.monotonic_remote_time, |
| 913 | remote_timestamp_monotonic_time); |
| 914 | |
| 915 | CHECK_EQ(timestamp.remote_queue_index, |
| 916 | std::get<2>(oldest_timestamp).message().remote_queue_index()) |
| 917 | << ": " << FlatbufferToJson(&std::get<2>(oldest_timestamp).message()) |
| 918 | << " data " |
| 919 | << FlatbufferToJson(&std::get<2>(oldest_message).message()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 920 | |
| 921 | return std::make_tuple(timestamp, std::get<2>(oldest_message)); |
| 922 | } |
| 923 | } else { |
| 924 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 925 | FlatbufferVector<MessageHeader>> |
| 926 | oldest_message = PopMessageHeap(); |
| 927 | |
| 928 | TimestampMerger::DeliveryTimestamp timestamp; |
| 929 | timestamp.monotonic_event_time = |
| 930 | monotonic_clock::time_point(chrono::nanoseconds( |
| 931 | std::get<2>(oldest_message).message().monotonic_sent_time())); |
| 932 | timestamp.realtime_event_time = |
| 933 | realtime_clock::time_point(chrono::nanoseconds( |
| 934 | std::get<2>(oldest_message).message().realtime_sent_time())); |
| 935 | timestamp.remote_queue_index = 0xffffffff; |
| 936 | |
| 937 | CHECK_EQ(std::get<0>(oldest_message), timestamp.monotonic_event_time); |
| 938 | CHECK_EQ(std::get<1>(oldest_message), |
| 939 | std::get<2>(oldest_message).message().queue_index()); |
| 940 | |
| 941 | return std::make_tuple(timestamp, std::get<2>(oldest_message)); |
| 942 | } |
| 943 | } |
| 944 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 945 | void TimestampMerger::NoticeAtEnd() { channel_merger_->NoticeAtEnd(); } |
| 946 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 947 | namespace { |
| 948 | std::vector<std::unique_ptr<SplitMessageReader>> MakeSplitMessageReaders( |
| 949 | const std::vector<std::vector<std::string>> &filenames) { |
| 950 | CHECK_GT(filenames.size(), 0u); |
| 951 | // Build up all the SplitMessageReaders. |
| 952 | std::vector<std::unique_ptr<SplitMessageReader>> result; |
| 953 | for (const std::vector<std::string> &filenames : filenames) { |
| 954 | result.emplace_back(std::make_unique<SplitMessageReader>(filenames)); |
| 955 | } |
| 956 | return result; |
| 957 | } |
| 958 | } // namespace |
| 959 | |
| 960 | ChannelMerger::ChannelMerger( |
| 961 | const std::vector<std::vector<std::string>> &filenames) |
| 962 | : split_message_readers_(MakeSplitMessageReaders(filenames)), |
| 963 | log_file_header_( |
| 964 | CopyFlatBuffer(split_message_readers_[0]->log_file_header())) { |
| 965 | // Now, confirm that the configuration matches for each and pick a start time. |
| 966 | // Also return the list of possible nodes. |
| 967 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 968 | split_message_readers_) { |
| 969 | CHECK(CompareFlatBuffer(log_file_header_.message().configuration(), |
| 970 | reader->log_file_header()->configuration())) |
| 971 | << ": Replaying log files with different configurations isn't " |
| 972 | "supported"; |
| 973 | } |
| 974 | |
| 975 | nodes_ = configuration::GetNodes(configuration()); |
| 976 | } |
| 977 | |
| 978 | bool ChannelMerger::SetNode(const Node *target_node) { |
| 979 | std::vector<SplitMessageReader *> split_message_readers; |
| 980 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 981 | split_message_readers_) { |
| 982 | split_message_readers.emplace_back(reader.get()); |
| 983 | } |
| 984 | |
| 985 | // Go find a log_file_header for this node. |
| 986 | { |
| 987 | bool found_node = false; |
| 988 | |
| 989 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 990 | split_message_readers_) { |
| 991 | if (CompareFlatBuffer(reader->node(), target_node)) { |
| 992 | if (!found_node) { |
| 993 | found_node = true; |
| 994 | log_file_header_ = CopyFlatBuffer(reader->log_file_header()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 995 | VLOG(1) << "Found log file " << reader->filename() << " with node " |
| 996 | << FlatbufferToJson(reader->node()) << " start_time " |
| 997 | << monotonic_start_time(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 998 | } else { |
| 999 | // And then make sure all the other files have matching headers. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1000 | CHECK(CompareFlatBuffer(log_file_header(), reader->log_file_header())) |
| 1001 | << ": " << FlatbufferToJson(log_file_header()) << " reader " |
| 1002 | << FlatbufferToJson(reader->log_file_header()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | if (!found_node) { |
| 1008 | LOG(WARNING) << "Failed to find log file for node " |
| 1009 | << FlatbufferToJson(target_node); |
| 1010 | return false; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | // Build up all the timestamp mergers. This connects up all the |
| 1015 | // SplitMessageReaders. |
| 1016 | timestamp_mergers_.reserve(configuration()->channels()->size()); |
| 1017 | for (size_t channel_index = 0; |
| 1018 | channel_index < configuration()->channels()->size(); ++channel_index) { |
| 1019 | timestamp_mergers_.emplace_back( |
| 1020 | configuration(), split_message_readers, channel_index, |
| 1021 | configuration::GetNode(configuration(), target_node), this); |
| 1022 | } |
| 1023 | |
| 1024 | // And prime everything. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1025 | for (std::unique_ptr<SplitMessageReader> &split_message_reader : |
| 1026 | split_message_readers_) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1027 | split_message_reader->QueueMessages( |
| 1028 | split_message_reader->monotonic_start_time()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | node_ = configuration::GetNodeOrDie(configuration(), target_node); |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
| 1035 | monotonic_clock::time_point ChannelMerger::OldestMessage() const { |
| 1036 | if (channel_heap_.size() == 0u) { |
| 1037 | return monotonic_clock::max_time; |
| 1038 | } |
| 1039 | return channel_heap_.front().first; |
| 1040 | } |
| 1041 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1042 | TimestampMerger::DeliveryTimestamp ChannelMerger::OldestTimestamp() const { |
| 1043 | if (timestamp_heap_.size() == 0u) { |
| 1044 | return TimestampMerger::DeliveryTimestamp{}; |
| 1045 | } |
| 1046 | return timestamp_mergers_[timestamp_heap_.front().second].OldestTimestamp(); |
| 1047 | } |
| 1048 | |
| 1049 | TimestampMerger::DeliveryTimestamp ChannelMerger::OldestTimestampForChannel( |
| 1050 | int channel) const { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1051 | // If we didn't find any data for this node, we won't have any mergers. Return |
| 1052 | // an invalid timestamp in that case. |
| 1053 | if (timestamp_mergers_.size() <= static_cast<size_t>(channel)) { |
| 1054 | TimestampMerger::DeliveryTimestamp result; |
| 1055 | return result; |
| 1056 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1057 | return timestamp_mergers_[channel].OldestTimestamp(); |
| 1058 | } |
| 1059 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1060 | void ChannelMerger::PushChannelHeap(monotonic_clock::time_point timestamp, |
| 1061 | int channel_index) { |
| 1062 | // Pop and recreate the heap if it has already been pushed. And since we are |
| 1063 | // pushing again, we don't need to clear pushed. |
| 1064 | if (timestamp_mergers_[channel_index].pushed()) { |
| 1065 | channel_heap_.erase(std::find_if( |
| 1066 | channel_heap_.begin(), channel_heap_.end(), |
| 1067 | [channel_index](const std::pair<monotonic_clock::time_point, int> x) { |
| 1068 | return x.second == channel_index; |
| 1069 | })); |
| 1070 | std::make_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1071 | ChannelHeapCompare); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1072 | |
| 1073 | if (timestamp_mergers_[channel_index].has_timestamps()) { |
| 1074 | timestamp_heap_.erase(std::find_if( |
| 1075 | timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1076 | [channel_index](const std::pair<monotonic_clock::time_point, int> x) { |
| 1077 | return x.second == channel_index; |
| 1078 | })); |
| 1079 | std::make_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1080 | ChannelHeapCompare); |
| 1081 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1084 | channel_heap_.push_back(std::make_pair(timestamp, channel_index)); |
| 1085 | |
| 1086 | // The default sort puts the newest message first. Use a custom comparator to |
| 1087 | // put the oldest message first. |
| 1088 | std::push_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1089 | ChannelHeapCompare); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1090 | |
| 1091 | if (timestamp_mergers_[channel_index].has_timestamps()) { |
| 1092 | timestamp_heap_.push_back(std::make_pair(timestamp, channel_index)); |
| 1093 | std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1094 | ChannelHeapCompare); |
| 1095 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1098 | std::tuple<TimestampMerger::DeliveryTimestamp, int, |
| 1099 | FlatbufferVector<MessageHeader>> |
| 1100 | ChannelMerger::PopOldest() { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1101 | CHECK_GT(channel_heap_.size(), 0u); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1102 | std::pair<monotonic_clock::time_point, int> oldest_channel_data = |
| 1103 | channel_heap_.front(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1104 | int channel_index = oldest_channel_data.second; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1105 | std::pop_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1106 | &ChannelHeapCompare); |
| 1107 | channel_heap_.pop_back(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1108 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1109 | timestamp_mergers_[channel_index].set_pushed(false); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1110 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1111 | TimestampMerger *merger = ×tamp_mergers_[channel_index]; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1112 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1113 | if (merger->has_timestamps()) { |
| 1114 | CHECK_GT(timestamp_heap_.size(), 0u); |
| 1115 | std::pair<monotonic_clock::time_point, int> oldest_timestamp_data = |
| 1116 | timestamp_heap_.front(); |
| 1117 | CHECK(oldest_timestamp_data == oldest_channel_data) |
| 1118 | << ": Timestamp heap out of sync."; |
| 1119 | std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1120 | &ChannelHeapCompare); |
| 1121 | timestamp_heap_.pop_back(); |
| 1122 | } |
| 1123 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1124 | // Merger handles any queueing needed from here. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1125 | std::tuple<TimestampMerger::DeliveryTimestamp, |
| 1126 | FlatbufferVector<MessageHeader>> |
| 1127 | message = merger->PopOldest(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1128 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1129 | return std::make_tuple(std::get<0>(message), channel_index, |
| 1130 | std::move(std::get<1>(message))); |
| 1131 | } |
| 1132 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1133 | std::string SplitMessageReader::MessageHeaderQueue::DebugString() const { |
| 1134 | std::stringstream ss; |
| 1135 | for (size_t i = 0; i < data_.size(); ++i) { |
| 1136 | if (timestamps) { |
| 1137 | ss << " msg: "; |
| 1138 | } else { |
| 1139 | ss << " timestamp: "; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1140 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1141 | ss << monotonic_clock::time_point(std::chrono::nanoseconds( |
| 1142 | data_[i].message().monotonic_sent_time())) |
| 1143 | << " (" |
| 1144 | << realtime_clock::time_point( |
| 1145 | std::chrono::nanoseconds(data_[i].message().realtime_sent_time())) |
| 1146 | << ") " << data_[i].message().queue_index(); |
| 1147 | if (timestamps) { |
| 1148 | ss << " <- remote " |
| 1149 | << monotonic_clock::time_point(std::chrono::nanoseconds( |
| 1150 | data_[i].message().monotonic_remote_time())) |
| 1151 | << " (" |
| 1152 | << realtime_clock::time_point(std::chrono::nanoseconds( |
| 1153 | data_[i].message().realtime_remote_time())) |
| 1154 | << ")"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1155 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1156 | ss << "\n"; |
| 1157 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1158 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1159 | return ss.str(); |
| 1160 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1161 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1162 | std::string SplitMessageReader::DebugString(int channel) const { |
| 1163 | std::stringstream ss; |
| 1164 | ss << "[\n"; |
| 1165 | ss << channels_[channel].data.DebugString(); |
| 1166 | ss << " ]"; |
| 1167 | return ss.str(); |
| 1168 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1169 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1170 | std::string SplitMessageReader::DebugString(int channel, int node_index) const { |
| 1171 | std::stringstream ss; |
| 1172 | ss << "[\n"; |
| 1173 | ss << channels_[channel].timestamps[node_index].DebugString(); |
| 1174 | ss << " ]"; |
| 1175 | return ss.str(); |
| 1176 | } |
| 1177 | |
| 1178 | std::string TimestampMerger::DebugString() const { |
| 1179 | std::stringstream ss; |
| 1180 | |
| 1181 | if (timestamp_heap_.size() > 0) { |
| 1182 | ss << " timestamp_heap {\n"; |
| 1183 | std::vector< |
| 1184 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 1185 | timestamp_heap = timestamp_heap_; |
| 1186 | while (timestamp_heap.size() > 0u) { |
| 1187 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1188 | oldest_timestamp_reader = timestamp_heap.front(); |
| 1189 | |
| 1190 | ss << " " << std::get<2>(oldest_timestamp_reader) << " " |
| 1191 | << std::get<0>(oldest_timestamp_reader) << " queue_index (" |
| 1192 | << std::get<1>(oldest_timestamp_reader) << ") ttq " |
| 1193 | << std::get<2>(oldest_timestamp_reader)->time_to_queue() << " " |
| 1194 | << std::get<2>(oldest_timestamp_reader)->filename() << " -> " |
| 1195 | << std::get<2>(oldest_timestamp_reader) |
| 1196 | ->DebugString(channel_index_, node_index_) |
| 1197 | << "\n"; |
| 1198 | |
| 1199 | std::pop_heap(timestamp_heap.begin(), timestamp_heap.end(), |
| 1200 | &SplitMessageReaderHeapCompare); |
| 1201 | timestamp_heap.pop_back(); |
| 1202 | } |
| 1203 | ss << " }\n"; |
| 1204 | } |
| 1205 | |
| 1206 | ss << " message_heap {\n"; |
| 1207 | { |
| 1208 | std::vector< |
| 1209 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 1210 | message_heap = message_heap_; |
| 1211 | while (message_heap.size() > 0u) { |
| 1212 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1213 | oldest_message_reader = message_heap.front(); |
| 1214 | |
| 1215 | ss << " " << std::get<2>(oldest_message_reader) << " " |
| 1216 | << std::get<0>(oldest_message_reader) << " queue_index (" |
| 1217 | << std::get<1>(oldest_message_reader) << ") ttq " |
| 1218 | << std::get<2>(oldest_message_reader)->time_to_queue() << " " |
| 1219 | << std::get<2>(oldest_message_reader)->filename() << " -> " |
| 1220 | << std::get<2>(oldest_message_reader)->DebugString(channel_index_) |
| 1221 | << "\n"; |
| 1222 | |
| 1223 | std::pop_heap(message_heap.begin(), message_heap.end(), |
| 1224 | &SplitMessageReaderHeapCompare); |
| 1225 | message_heap.pop_back(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1226 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1227 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1228 | ss << " }"; |
| 1229 | |
| 1230 | return ss.str(); |
| 1231 | } |
| 1232 | |
| 1233 | std::string ChannelMerger::DebugString() const { |
| 1234 | std::stringstream ss; |
| 1235 | ss << "start_time " << realtime_start_time() << " " << monotonic_start_time() |
| 1236 | << "\n"; |
| 1237 | ss << "channel_heap {\n"; |
| 1238 | std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap = |
| 1239 | channel_heap_; |
| 1240 | while (channel_heap.size() > 0u) { |
| 1241 | std::tuple<monotonic_clock::time_point, int> channel = channel_heap.front(); |
| 1242 | ss << " " << std::get<0>(channel) << " (" << std::get<1>(channel) << ") " |
| 1243 | << configuration::CleanedChannelToString( |
| 1244 | configuration()->channels()->Get(std::get<1>(channel))) |
| 1245 | << "\n"; |
| 1246 | |
| 1247 | ss << timestamp_mergers_[std::get<1>(channel)].DebugString() << "\n"; |
| 1248 | |
| 1249 | std::pop_heap(channel_heap.begin(), channel_heap.end(), |
| 1250 | &ChannelHeapCompare); |
| 1251 | channel_heap.pop_back(); |
| 1252 | } |
| 1253 | ss << "}"; |
| 1254 | |
| 1255 | return ss.str(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 1258 | } // namespace logger |
| 1259 | } // namespace aos |