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