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