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