blob: 44381f3a087b1177e18319755549a8b31cb8fddc [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 Schuh8bd96322020-02-13 21:18:22 -0800294 VLOG(2) << "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 Schuh8bd96322020-02-13 21:18:22 -0800438 VLOG(1) << "End of log file " << filenames_.back();
Austin Schuhcde938c2020-02-02 17:30:07 -0800439 at_end_ = true;
Austin Schuh8bd96322020-02-13 21:18:22 -0800440 for (MessageHeaderQueue *queue : channels_to_write_) {
441 if (queue == nullptr || queue->timestamp_merger == nullptr) {
442 continue;
443 }
444 queue->timestamp_merger->NoticeAtEnd();
445 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800446 return false;
447 }
448 }
Austin Schuh05b70472020-01-01 17:11:17 -0800449 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800450}
451
452void SplitMessageReader::SetTimestampMerger(TimestampMerger *timestamp_merger,
453 int channel_index,
454 const Node *target_node) {
455 const Node *reinterpreted_target_node =
456 configuration::GetNodeOrDie(configuration(), target_node);
457 const Channel *const channel =
458 configuration()->channels()->Get(channel_index);
459
Austin Schuhcde938c2020-02-02 17:30:07 -0800460 VLOG(1) << " Configuring merger " << this << " for channel " << channel_index
461 << " "
462 << configuration::CleanedChannelToString(
463 configuration()->channels()->Get(channel_index));
464
Austin Schuh6f3babe2020-01-26 20:34:50 -0800465 MessageHeaderQueue *message_header_queue = nullptr;
466
467 // Figure out if this log file is from our point of view, or the other node's
468 // point of view.
469 if (node() == reinterpreted_target_node) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800470 VLOG(1) << " Replaying as logged node " << filename();
471
472 if (configuration::ChannelIsSendableOnNode(channel, node())) {
473 VLOG(1) << " Data on node";
474 message_header_queue = &(channels_[channel_index].data);
475 } else if (configuration::ChannelIsReadableOnNode(channel, node())) {
476 VLOG(1) << " Timestamps on node";
477 message_header_queue =
478 &(channels_[channel_index].timestamps[configuration::GetNodeIndex(
479 configuration(), node())]);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800480 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800481 VLOG(1) << " Dropping";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800482 }
483 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800484 VLOG(1) << " Replaying as other node " << filename();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800485 // We are replaying from another node's point of view. The only interesting
Austin Schuhcde938c2020-02-02 17:30:07 -0800486 // data is data that is sent from our node and received on theirs.
487 if (configuration::ChannelIsReadableOnNode(channel,
488 reinterpreted_target_node) &&
489 configuration::ChannelIsSendableOnNode(channel, node())) {
490 VLOG(1) << " Readable on target node";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800491 // Data from another node.
492 message_header_queue = &(channels_[channel_index].data);
493 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800494 VLOG(1) << " Dropping";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800495 // This is either not sendable on the other node, or is a timestamp and
496 // therefore not interesting.
497 }
498 }
499
500 // If we found one, write it down. This will be nullptr when there is nothing
501 // relevant on this channel on this node for the target node. In that case,
502 // we want to drop the message instead of queueing it.
503 if (message_header_queue != nullptr) {
504 message_header_queue->timestamp_merger = timestamp_merger;
505 }
506}
507
508std::tuple<monotonic_clock::time_point, uint32_t,
509 FlatbufferVector<MessageHeader>>
510SplitMessageReader::PopOldest(int channel_index) {
511 CHECK_GT(channels_[channel_index].data.size(), 0u);
Austin Schuhcde938c2020-02-02 17:30:07 -0800512 const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
513 timestamp = channels_[channel_index].data.front_timestamp();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800514 FlatbufferVector<MessageHeader> front =
515 std::move(channels_[channel_index].data.front());
516 channels_[channel_index].data.pop_front();
Austin Schuhcde938c2020-02-02 17:30:07 -0800517
518 VLOG(1) << "Popped " << this << " " << std::get<0>(timestamp);
519
520 QueueMessages(std::get<0>(timestamp));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800521
522 return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp),
523 std::move(front));
524}
525
526std::tuple<monotonic_clock::time_point, uint32_t,
527 FlatbufferVector<MessageHeader>>
528SplitMessageReader::PopOldest(int channel, int node_index) {
529 CHECK_GT(channels_[channel].timestamps[node_index].size(), 0u);
Austin Schuhcde938c2020-02-02 17:30:07 -0800530 const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
531 timestamp = channels_[channel].timestamps[node_index].front_timestamp();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800532 FlatbufferVector<MessageHeader> front =
533 std::move(channels_[channel].timestamps[node_index].front());
534 channels_[channel].timestamps[node_index].pop_front();
Austin Schuhcde938c2020-02-02 17:30:07 -0800535
536 VLOG(1) << "Popped " << this << " " << std::get<0>(timestamp);
537
538 QueueMessages(std::get<0>(timestamp));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800539
540 return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp),
541 std::move(front));
542}
543
Austin Schuhcde938c2020-02-02 17:30:07 -0800544bool SplitMessageReader::MessageHeaderQueue::emplace_back(
Austin Schuh6f3babe2020-01-26 20:34:50 -0800545 FlatbufferVector<MessageHeader> &&msg) {
546 CHECK(split_reader != nullptr);
547
548 // If there is no timestamp merger for this queue, nobody is listening. Drop
549 // the message. This happens when a log file from another node is replayed,
550 // and the timestamp mergers down stream just don't care.
551 if (timestamp_merger == nullptr) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800552 return false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800553 }
554
555 CHECK(timestamps != msg.message().has_data())
556 << ": Got timestamps and data mixed up on a node. "
557 << FlatbufferToJson(msg);
558
559 data_.emplace_back(std::move(msg));
560
561 if (data_.size() == 1u) {
562 // Yup, new data. Notify.
563 if (timestamps) {
564 timestamp_merger->UpdateTimestamp(split_reader, front_timestamp());
565 } else {
566 timestamp_merger->Update(split_reader, front_timestamp());
567 }
568 }
Austin Schuhcde938c2020-02-02 17:30:07 -0800569
570 return true;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800571}
572
573void SplitMessageReader::MessageHeaderQueue::pop_front() {
574 data_.pop_front();
575 if (data_.size() != 0u) {
576 // Yup, new data.
577 if (timestamps) {
578 timestamp_merger->UpdateTimestamp(split_reader, front_timestamp());
579 } else {
580 timestamp_merger->Update(split_reader, front_timestamp());
581 }
582 }
Austin Schuh05b70472020-01-01 17:11:17 -0800583}
584
585namespace {
586
Austin Schuh6f3babe2020-01-26 20:34:50 -0800587bool SplitMessageReaderHeapCompare(
588 const std::tuple<monotonic_clock::time_point, uint32_t,
589 SplitMessageReader *>
590 first,
591 const std::tuple<monotonic_clock::time_point, uint32_t,
592 SplitMessageReader *>
593 second) {
594 if (std::get<0>(first) > std::get<0>(second)) {
595 return true;
596 } else if (std::get<0>(first) == std::get<0>(second)) {
597 if (std::get<1>(first) > std::get<1>(second)) {
598 return true;
599 } else if (std::get<1>(first) == std::get<1>(second)) {
600 return std::get<2>(first) > std::get<2>(second);
601 } else {
602 return false;
603 }
604 } else {
605 return false;
606 }
607}
608
Austin Schuh05b70472020-01-01 17:11:17 -0800609bool ChannelHeapCompare(
610 const std::pair<monotonic_clock::time_point, int> first,
611 const std::pair<monotonic_clock::time_point, int> second) {
612 if (first.first > second.first) {
613 return true;
614 } else if (first.first == second.first) {
615 return first.second > second.second;
616 } else {
617 return false;
618 }
619}
620
621} // namespace
622
Austin Schuh6f3babe2020-01-26 20:34:50 -0800623TimestampMerger::TimestampMerger(
624 const Configuration *configuration,
625 std::vector<SplitMessageReader *> split_message_readers, int channel_index,
626 const Node *target_node, ChannelMerger *channel_merger)
627 : configuration_(configuration),
628 split_message_readers_(std::move(split_message_readers)),
629 channel_index_(channel_index),
630 node_index_(configuration::MultiNode(configuration)
631 ? configuration::GetNodeIndex(configuration, target_node)
632 : -1),
633 channel_merger_(channel_merger) {
634 // Tell the readers we care so they know who to notify.
Austin Schuhcde938c2020-02-02 17:30:07 -0800635 VLOG(1) << "Configuring channel " << channel_index << " target node "
636 << FlatbufferToJson(target_node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800637 for (SplitMessageReader *reader : split_message_readers_) {
638 reader->SetTimestampMerger(this, channel_index, target_node);
639 }
640
641 // And then determine if we need to track timestamps.
642 const Channel *channel = configuration->channels()->Get(channel_index);
643 if (!configuration::ChannelIsSendableOnNode(channel, target_node) &&
644 configuration::ChannelIsReadableOnNode(channel, target_node)) {
645 has_timestamps_ = true;
646 }
647}
648
649void TimestampMerger::PushMessageHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800650 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
651 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800652 SplitMessageReader *split_message_reader) {
653 DCHECK(std::find_if(message_heap_.begin(), message_heap_.end(),
654 [split_message_reader](
655 const std::tuple<monotonic_clock::time_point,
656 uint32_t, SplitMessageReader *>
657 x) {
658 return std::get<2>(x) == split_message_reader;
659 }) == message_heap_.end())
660 << ": Pushing message when it is already in the heap.";
661
662 message_heap_.push_back(std::make_tuple(
663 std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader));
664
665 std::push_heap(message_heap_.begin(), message_heap_.end(),
666 &SplitMessageReaderHeapCompare);
667
668 // If we are just a data merger, don't wait for timestamps.
669 if (!has_timestamps_) {
670 channel_merger_->Update(std::get<0>(timestamp), channel_index_);
671 pushed_ = true;
672 }
673}
674
Austin Schuhcde938c2020-02-02 17:30:07 -0800675std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
676TimestampMerger::oldest_message() const {
677 CHECK_GT(message_heap_.size(), 0u);
678 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
679 oldest_message_reader = message_heap_.front();
680 return std::get<2>(oldest_message_reader)->oldest_message(channel_index_);
681}
682
683std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
684TimestampMerger::oldest_timestamp() const {
685 CHECK_GT(timestamp_heap_.size(), 0u);
686 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
687 oldest_message_reader = timestamp_heap_.front();
688 return std::get<2>(oldest_message_reader)
689 ->oldest_message(channel_index_, node_index_);
690}
691
Austin Schuh6f3babe2020-01-26 20:34:50 -0800692void TimestampMerger::PushTimestampHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800693 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
694 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800695 SplitMessageReader *split_message_reader) {
696 DCHECK(std::find_if(timestamp_heap_.begin(), timestamp_heap_.end(),
697 [split_message_reader](
698 const std::tuple<monotonic_clock::time_point,
699 uint32_t, SplitMessageReader *>
700 x) {
701 return std::get<2>(x) == split_message_reader;
702 }) == timestamp_heap_.end())
703 << ": Pushing timestamp when it is already in the heap.";
704
705 timestamp_heap_.push_back(std::make_tuple(
706 std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader));
707
708 std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
709 SplitMessageReaderHeapCompare);
710
711 // If we are a timestamp merger, don't wait for data. Missing data will be
712 // caught at read time.
713 if (has_timestamps_) {
714 channel_merger_->Update(std::get<0>(timestamp), channel_index_);
715 pushed_ = true;
716 }
717}
718
719std::tuple<monotonic_clock::time_point, uint32_t,
720 FlatbufferVector<MessageHeader>>
721TimestampMerger::PopMessageHeap() {
722 // Pop the oldest message reader pointer off the heap.
723 CHECK_GT(message_heap_.size(), 0u);
724 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
725 oldest_message_reader = message_heap_.front();
726
727 std::pop_heap(message_heap_.begin(), message_heap_.end(),
728 &SplitMessageReaderHeapCompare);
729 message_heap_.pop_back();
730
731 // Pop the oldest message. This re-pushes any messages from the reader to the
732 // message heap.
733 std::tuple<monotonic_clock::time_point, uint32_t,
734 FlatbufferVector<MessageHeader>>
735 oldest_message =
736 std::get<2>(oldest_message_reader)->PopOldest(channel_index_);
737
738 // Confirm that the time and queue_index we have recorded matches.
739 CHECK_EQ(std::get<0>(oldest_message), std::get<0>(oldest_message_reader));
740 CHECK_EQ(std::get<1>(oldest_message), std::get<1>(oldest_message_reader));
741
742 // Now, keep reading until we have found all duplicates.
743 while (message_heap_.size() > 0u) {
744 // See if it is a duplicate.
745 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
746 next_oldest_message_reader = message_heap_.front();
747
Austin Schuhcde938c2020-02-02 17:30:07 -0800748 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
749 next_oldest_message_time = std::get<2>(next_oldest_message_reader)
750 ->oldest_message(channel_index_);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800751
752 if (std::get<0>(next_oldest_message_time) == std::get<0>(oldest_message) &&
753 std::get<1>(next_oldest_message_time) == std::get<1>(oldest_message)) {
754 // Pop the message reader pointer.
755 std::pop_heap(message_heap_.begin(), message_heap_.end(),
756 &SplitMessageReaderHeapCompare);
757 message_heap_.pop_back();
758
759 // Pop the next oldest message. This re-pushes any messages from the
760 // reader.
761 std::tuple<monotonic_clock::time_point, uint32_t,
762 FlatbufferVector<MessageHeader>>
763 next_oldest_message = std::get<2>(next_oldest_message_reader)
764 ->PopOldest(channel_index_);
765
766 // And make sure the message matches in it's entirety.
767 CHECK(std::get<2>(oldest_message).span() ==
768 std::get<2>(next_oldest_message).span())
769 << ": Data at the same timestamp doesn't match.";
770 } else {
771 break;
772 }
773 }
774
775 return oldest_message;
776}
777
778std::tuple<monotonic_clock::time_point, uint32_t,
779 FlatbufferVector<MessageHeader>>
780TimestampMerger::PopTimestampHeap() {
781 // Pop the oldest message reader pointer off the heap.
782 CHECK_GT(timestamp_heap_.size(), 0u);
783
784 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
785 oldest_timestamp_reader = timestamp_heap_.front();
786
787 std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
788 &SplitMessageReaderHeapCompare);
789 timestamp_heap_.pop_back();
790
791 CHECK(node_index_ != -1) << ": Timestamps in a single node environment";
792
793 // Pop the oldest message. This re-pushes any timestamps from the reader to
794 // the timestamp heap.
795 std::tuple<monotonic_clock::time_point, uint32_t,
796 FlatbufferVector<MessageHeader>>
797 oldest_timestamp = std::get<2>(oldest_timestamp_reader)
798 ->PopOldest(channel_index_, node_index_);
799
800 // Confirm that the time we have recorded matches.
801 CHECK_EQ(std::get<0>(oldest_timestamp), std::get<0>(oldest_timestamp_reader));
802 CHECK_EQ(std::get<1>(oldest_timestamp), std::get<1>(oldest_timestamp_reader));
803
804 // TODO(austin): What if we get duplicate timestamps?
805
806 return oldest_timestamp;
807}
808
Austin Schuh8bd96322020-02-13 21:18:22 -0800809TimestampMerger::DeliveryTimestamp TimestampMerger::OldestTimestamp() const {
810 if (!has_timestamps_ || timestamp_heap_.size() == 0u) {
811 return TimestampMerger::DeliveryTimestamp{};
812 }
813
814 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
815 oldest_timestamp_reader = timestamp_heap_.front();
816
817 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
818 oldest_timestamp = std::get<2>(oldest_timestamp_reader)
819 ->oldest_message(channel_index_, node_index_);
820
821 TimestampMerger::DeliveryTimestamp timestamp;
822 timestamp.monotonic_event_time =
823 monotonic_clock::time_point(chrono::nanoseconds(
824 std::get<2>(oldest_timestamp)->monotonic_sent_time()));
825 timestamp.realtime_event_time = realtime_clock::time_point(
826 chrono::nanoseconds(std::get<2>(oldest_timestamp)->realtime_sent_time()));
827
828 timestamp.monotonic_remote_time =
829 monotonic_clock::time_point(chrono::nanoseconds(
830 std::get<2>(oldest_timestamp)->monotonic_remote_time()));
831 timestamp.realtime_remote_time =
832 realtime_clock::time_point(chrono::nanoseconds(
833 std::get<2>(oldest_timestamp)->realtime_remote_time()));
834
835 timestamp.remote_queue_index = std::get<2>(oldest_timestamp)->queue_index();
836 return timestamp;
837}
838
Austin Schuh6f3babe2020-01-26 20:34:50 -0800839std::tuple<TimestampMerger::DeliveryTimestamp, FlatbufferVector<MessageHeader>>
840TimestampMerger::PopOldest() {
841 if (has_timestamps_) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800842 // Read the timestamps.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800843 std::tuple<monotonic_clock::time_point, uint32_t,
844 FlatbufferVector<MessageHeader>>
845 oldest_timestamp = PopTimestampHeap();
846
847 TimestampMerger::DeliveryTimestamp timestamp;
848 timestamp.monotonic_event_time =
849 monotonic_clock::time_point(chrono::nanoseconds(
850 std::get<2>(oldest_timestamp).message().monotonic_sent_time()));
851 timestamp.realtime_event_time =
852 realtime_clock::time_point(chrono::nanoseconds(
853 std::get<2>(oldest_timestamp).message().realtime_sent_time()));
854
855 // Consistency check.
856 CHECK_EQ(timestamp.monotonic_event_time, std::get<0>(oldest_timestamp));
857 CHECK_EQ(std::get<2>(oldest_timestamp).message().queue_index(),
858 std::get<1>(oldest_timestamp));
859
860 monotonic_clock::time_point remote_timestamp_monotonic_time(
861 chrono::nanoseconds(
862 std::get<2>(oldest_timestamp).message().monotonic_remote_time()));
863
Austin Schuh8bd96322020-02-13 21:18:22 -0800864 // See if we have any data. If not, pass the problem up the chain.
865 if (message_heap_.size() == 0u) {
866 VLOG(1) << "No data to match timestamp on "
867 << configuration::CleanedChannelToString(
868 configuration_->channels()->Get(channel_index_));
869 return std::make_tuple(timestamp,
870 std::move(std::get<2>(oldest_timestamp)));
871 }
872
Austin Schuh6f3babe2020-01-26 20:34:50 -0800873 while (true) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800874 {
875 // Ok, now try grabbing data until we find one which matches.
876 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
877 oldest_message_ref = oldest_message();
878
879 // Time at which the message was sent (this message is written from the
880 // sending node's perspective.
881 monotonic_clock::time_point remote_monotonic_time(chrono::nanoseconds(
882 std::get<2>(oldest_message_ref)->monotonic_sent_time()));
883
884 if (remote_monotonic_time < remote_timestamp_monotonic_time) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800885 VLOG(1) << "Undelivered message, skipping. Remote time is "
886 << remote_monotonic_time << " timestamp is "
887 << remote_timestamp_monotonic_time << " on channel "
888 << channel_index_;
Austin Schuhcde938c2020-02-02 17:30:07 -0800889 PopMessageHeap();
890 continue;
891 } else if (remote_monotonic_time > remote_timestamp_monotonic_time) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800892 VLOG(1) << "Data not found. Remote time should be "
893 << remote_timestamp_monotonic_time << " on channel "
894 << channel_index_;
Austin Schuhcde938c2020-02-02 17:30:07 -0800895 return std::make_tuple(timestamp,
896 std::move(std::get<2>(oldest_timestamp)));
897 }
898
899 timestamp.monotonic_remote_time = remote_monotonic_time;
900 }
901
Austin Schuh6f3babe2020-01-26 20:34:50 -0800902 std::tuple<monotonic_clock::time_point, uint32_t,
903 FlatbufferVector<MessageHeader>>
904 oldest_message = PopMessageHeap();
905
Austin Schuh6f3babe2020-01-26 20:34:50 -0800906 timestamp.realtime_remote_time =
907 realtime_clock::time_point(chrono::nanoseconds(
908 std::get<2>(oldest_message).message().realtime_sent_time()));
909 timestamp.remote_queue_index =
910 std::get<2>(oldest_message).message().queue_index();
911
Austin Schuhcde938c2020-02-02 17:30:07 -0800912 CHECK_EQ(timestamp.monotonic_remote_time,
913 remote_timestamp_monotonic_time);
914
915 CHECK_EQ(timestamp.remote_queue_index,
916 std::get<2>(oldest_timestamp).message().remote_queue_index())
917 << ": " << FlatbufferToJson(&std::get<2>(oldest_timestamp).message())
918 << " data "
919 << FlatbufferToJson(&std::get<2>(oldest_message).message());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800920
921 return std::make_tuple(timestamp, std::get<2>(oldest_message));
922 }
923 } else {
924 std::tuple<monotonic_clock::time_point, uint32_t,
925 FlatbufferVector<MessageHeader>>
926 oldest_message = PopMessageHeap();
927
928 TimestampMerger::DeliveryTimestamp timestamp;
929 timestamp.monotonic_event_time =
930 monotonic_clock::time_point(chrono::nanoseconds(
931 std::get<2>(oldest_message).message().monotonic_sent_time()));
932 timestamp.realtime_event_time =
933 realtime_clock::time_point(chrono::nanoseconds(
934 std::get<2>(oldest_message).message().realtime_sent_time()));
935 timestamp.remote_queue_index = 0xffffffff;
936
937 CHECK_EQ(std::get<0>(oldest_message), timestamp.monotonic_event_time);
938 CHECK_EQ(std::get<1>(oldest_message),
939 std::get<2>(oldest_message).message().queue_index());
940
941 return std::make_tuple(timestamp, std::get<2>(oldest_message));
942 }
943}
944
Austin Schuh8bd96322020-02-13 21:18:22 -0800945void TimestampMerger::NoticeAtEnd() { channel_merger_->NoticeAtEnd(); }
946
Austin Schuh6f3babe2020-01-26 20:34:50 -0800947namespace {
948std::vector<std::unique_ptr<SplitMessageReader>> MakeSplitMessageReaders(
949 const std::vector<std::vector<std::string>> &filenames) {
950 CHECK_GT(filenames.size(), 0u);
951 // Build up all the SplitMessageReaders.
952 std::vector<std::unique_ptr<SplitMessageReader>> result;
953 for (const std::vector<std::string> &filenames : filenames) {
954 result.emplace_back(std::make_unique<SplitMessageReader>(filenames));
955 }
956 return result;
957}
958} // namespace
959
960ChannelMerger::ChannelMerger(
961 const std::vector<std::vector<std::string>> &filenames)
962 : split_message_readers_(MakeSplitMessageReaders(filenames)),
963 log_file_header_(
964 CopyFlatBuffer(split_message_readers_[0]->log_file_header())) {
965 // Now, confirm that the configuration matches for each and pick a start time.
966 // Also return the list of possible nodes.
967 for (const std::unique_ptr<SplitMessageReader> &reader :
968 split_message_readers_) {
969 CHECK(CompareFlatBuffer(log_file_header_.message().configuration(),
970 reader->log_file_header()->configuration()))
971 << ": Replaying log files with different configurations isn't "
972 "supported";
973 }
974
975 nodes_ = configuration::GetNodes(configuration());
976}
977
978bool ChannelMerger::SetNode(const Node *target_node) {
979 std::vector<SplitMessageReader *> split_message_readers;
980 for (const std::unique_ptr<SplitMessageReader> &reader :
981 split_message_readers_) {
982 split_message_readers.emplace_back(reader.get());
983 }
984
985 // Go find a log_file_header for this node.
986 {
987 bool found_node = false;
988
989 for (const std::unique_ptr<SplitMessageReader> &reader :
990 split_message_readers_) {
991 if (CompareFlatBuffer(reader->node(), target_node)) {
992 if (!found_node) {
993 found_node = true;
994 log_file_header_ = CopyFlatBuffer(reader->log_file_header());
Austin Schuhcde938c2020-02-02 17:30:07 -0800995 VLOG(1) << "Found log file " << reader->filename() << " with node "
996 << FlatbufferToJson(reader->node()) << " start_time "
997 << monotonic_start_time();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800998 } else {
999 // And then make sure all the other files have matching headers.
Austin Schuhcde938c2020-02-02 17:30:07 -08001000 CHECK(CompareFlatBuffer(log_file_header(), reader->log_file_header()))
1001 << ": " << FlatbufferToJson(log_file_header()) << " reader "
1002 << FlatbufferToJson(reader->log_file_header());
Austin Schuh6f3babe2020-01-26 20:34:50 -08001003 }
1004 }
1005 }
1006
1007 if (!found_node) {
1008 LOG(WARNING) << "Failed to find log file for node "
1009 << FlatbufferToJson(target_node);
1010 return false;
1011 }
1012 }
1013
1014 // Build up all the timestamp mergers. This connects up all the
1015 // SplitMessageReaders.
1016 timestamp_mergers_.reserve(configuration()->channels()->size());
1017 for (size_t channel_index = 0;
1018 channel_index < configuration()->channels()->size(); ++channel_index) {
1019 timestamp_mergers_.emplace_back(
1020 configuration(), split_message_readers, channel_index,
1021 configuration::GetNode(configuration(), target_node), this);
1022 }
1023
1024 // And prime everything.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001025 for (std::unique_ptr<SplitMessageReader> &split_message_reader :
1026 split_message_readers_) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001027 split_message_reader->QueueMessages(
1028 split_message_reader->monotonic_start_time());
Austin Schuh6f3babe2020-01-26 20:34:50 -08001029 }
1030
1031 node_ = configuration::GetNodeOrDie(configuration(), target_node);
1032 return true;
1033}
1034
1035monotonic_clock::time_point ChannelMerger::OldestMessage() const {
1036 if (channel_heap_.size() == 0u) {
1037 return monotonic_clock::max_time;
1038 }
1039 return channel_heap_.front().first;
1040}
1041
Austin Schuh8bd96322020-02-13 21:18:22 -08001042TimestampMerger::DeliveryTimestamp ChannelMerger::OldestTimestamp() const {
1043 if (timestamp_heap_.size() == 0u) {
1044 return TimestampMerger::DeliveryTimestamp{};
1045 }
1046 return timestamp_mergers_[timestamp_heap_.front().second].OldestTimestamp();
1047}
1048
1049TimestampMerger::DeliveryTimestamp ChannelMerger::OldestTimestampForChannel(
1050 int channel) const {
1051 return timestamp_mergers_[channel].OldestTimestamp();
1052}
1053
Austin Schuh6f3babe2020-01-26 20:34:50 -08001054void ChannelMerger::PushChannelHeap(monotonic_clock::time_point timestamp,
1055 int channel_index) {
1056 // Pop and recreate the heap if it has already been pushed. And since we are
1057 // pushing again, we don't need to clear pushed.
1058 if (timestamp_mergers_[channel_index].pushed()) {
1059 channel_heap_.erase(std::find_if(
1060 channel_heap_.begin(), channel_heap_.end(),
1061 [channel_index](const std::pair<monotonic_clock::time_point, int> x) {
1062 return x.second == channel_index;
1063 }));
1064 std::make_heap(channel_heap_.begin(), channel_heap_.end(),
1065 ChannelHeapCompare);
Austin Schuh8bd96322020-02-13 21:18:22 -08001066
1067 if (timestamp_mergers_[channel_index].has_timestamps()) {
1068 timestamp_heap_.erase(std::find_if(
1069 timestamp_heap_.begin(), timestamp_heap_.end(),
1070 [channel_index](const std::pair<monotonic_clock::time_point, int> x) {
1071 return x.second == channel_index;
1072 }));
1073 std::make_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
1074 ChannelHeapCompare);
1075 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001076 }
1077
Austin Schuh05b70472020-01-01 17:11:17 -08001078 channel_heap_.push_back(std::make_pair(timestamp, channel_index));
1079
1080 // The default sort puts the newest message first. Use a custom comparator to
1081 // put the oldest message first.
1082 std::push_heap(channel_heap_.begin(), channel_heap_.end(),
1083 ChannelHeapCompare);
Austin Schuh8bd96322020-02-13 21:18:22 -08001084
1085 if (timestamp_mergers_[channel_index].has_timestamps()) {
1086 timestamp_heap_.push_back(std::make_pair(timestamp, channel_index));
1087 std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
1088 ChannelHeapCompare);
1089 }
Austin Schuh05b70472020-01-01 17:11:17 -08001090}
1091
Austin Schuh6f3babe2020-01-26 20:34:50 -08001092std::tuple<TimestampMerger::DeliveryTimestamp, int,
1093 FlatbufferVector<MessageHeader>>
1094ChannelMerger::PopOldest() {
Austin Schuh8bd96322020-02-13 21:18:22 -08001095 CHECK_GT(channel_heap_.size(), 0u);
Austin Schuh05b70472020-01-01 17:11:17 -08001096 std::pair<monotonic_clock::time_point, int> oldest_channel_data =
1097 channel_heap_.front();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001098 int channel_index = oldest_channel_data.second;
Austin Schuh05b70472020-01-01 17:11:17 -08001099 std::pop_heap(channel_heap_.begin(), channel_heap_.end(),
1100 &ChannelHeapCompare);
1101 channel_heap_.pop_back();
Austin Schuh8bd96322020-02-13 21:18:22 -08001102
Austin Schuh6f3babe2020-01-26 20:34:50 -08001103 timestamp_mergers_[channel_index].set_pushed(false);
Austin Schuh05b70472020-01-01 17:11:17 -08001104
Austin Schuh6f3babe2020-01-26 20:34:50 -08001105 TimestampMerger *merger = &timestamp_mergers_[channel_index];
Austin Schuh05b70472020-01-01 17:11:17 -08001106
Austin Schuh8bd96322020-02-13 21:18:22 -08001107 if (merger->has_timestamps()) {
1108 CHECK_GT(timestamp_heap_.size(), 0u);
1109 std::pair<monotonic_clock::time_point, int> oldest_timestamp_data =
1110 timestamp_heap_.front();
1111 CHECK(oldest_timestamp_data == oldest_channel_data)
1112 << ": Timestamp heap out of sync.";
1113 std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
1114 &ChannelHeapCompare);
1115 timestamp_heap_.pop_back();
1116 }
1117
Austin Schuhcde938c2020-02-02 17:30:07 -08001118 // Merger handles any queueing needed from here.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001119 std::tuple<TimestampMerger::DeliveryTimestamp,
1120 FlatbufferVector<MessageHeader>>
1121 message = merger->PopOldest();
Austin Schuh05b70472020-01-01 17:11:17 -08001122
Austin Schuh6f3babe2020-01-26 20:34:50 -08001123 return std::make_tuple(std::get<0>(message), channel_index,
1124 std::move(std::get<1>(message)));
1125}
1126
Austin Schuhcde938c2020-02-02 17:30:07 -08001127std::string SplitMessageReader::MessageHeaderQueue::DebugString() const {
1128 std::stringstream ss;
1129 for (size_t i = 0; i < data_.size(); ++i) {
1130 if (timestamps) {
1131 ss << " msg: ";
1132 } else {
1133 ss << " timestamp: ";
Austin Schuh6f3babe2020-01-26 20:34:50 -08001134 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001135 ss << monotonic_clock::time_point(std::chrono::nanoseconds(
1136 data_[i].message().monotonic_sent_time()))
1137 << " ("
1138 << realtime_clock::time_point(
1139 std::chrono::nanoseconds(data_[i].message().realtime_sent_time()))
1140 << ") " << data_[i].message().queue_index();
1141 if (timestamps) {
1142 ss << " <- remote "
1143 << monotonic_clock::time_point(std::chrono::nanoseconds(
1144 data_[i].message().monotonic_remote_time()))
1145 << " ("
1146 << realtime_clock::time_point(std::chrono::nanoseconds(
1147 data_[i].message().realtime_remote_time()))
1148 << ")";
Austin Schuh6f3babe2020-01-26 20:34:50 -08001149 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001150 ss << "\n";
1151 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001152
Austin Schuhcde938c2020-02-02 17:30:07 -08001153 return ss.str();
1154}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001155
Austin Schuhcde938c2020-02-02 17:30:07 -08001156std::string SplitMessageReader::DebugString(int channel) const {
1157 std::stringstream ss;
1158 ss << "[\n";
1159 ss << channels_[channel].data.DebugString();
1160 ss << " ]";
1161 return ss.str();
1162}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001163
Austin Schuhcde938c2020-02-02 17:30:07 -08001164std::string SplitMessageReader::DebugString(int channel, int node_index) const {
1165 std::stringstream ss;
1166 ss << "[\n";
1167 ss << channels_[channel].timestamps[node_index].DebugString();
1168 ss << " ]";
1169 return ss.str();
1170}
1171
1172std::string TimestampMerger::DebugString() const {
1173 std::stringstream ss;
1174
1175 if (timestamp_heap_.size() > 0) {
1176 ss << " timestamp_heap {\n";
1177 std::vector<
1178 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
1179 timestamp_heap = timestamp_heap_;
1180 while (timestamp_heap.size() > 0u) {
1181 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
1182 oldest_timestamp_reader = timestamp_heap.front();
1183
1184 ss << " " << std::get<2>(oldest_timestamp_reader) << " "
1185 << std::get<0>(oldest_timestamp_reader) << " queue_index ("
1186 << std::get<1>(oldest_timestamp_reader) << ") ttq "
1187 << std::get<2>(oldest_timestamp_reader)->time_to_queue() << " "
1188 << std::get<2>(oldest_timestamp_reader)->filename() << " -> "
1189 << std::get<2>(oldest_timestamp_reader)
1190 ->DebugString(channel_index_, node_index_)
1191 << "\n";
1192
1193 std::pop_heap(timestamp_heap.begin(), timestamp_heap.end(),
1194 &SplitMessageReaderHeapCompare);
1195 timestamp_heap.pop_back();
1196 }
1197 ss << " }\n";
1198 }
1199
1200 ss << " message_heap {\n";
1201 {
1202 std::vector<
1203 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
1204 message_heap = message_heap_;
1205 while (message_heap.size() > 0u) {
1206 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
1207 oldest_message_reader = message_heap.front();
1208
1209 ss << " " << std::get<2>(oldest_message_reader) << " "
1210 << std::get<0>(oldest_message_reader) << " queue_index ("
1211 << std::get<1>(oldest_message_reader) << ") ttq "
1212 << std::get<2>(oldest_message_reader)->time_to_queue() << " "
1213 << std::get<2>(oldest_message_reader)->filename() << " -> "
1214 << std::get<2>(oldest_message_reader)->DebugString(channel_index_)
1215 << "\n";
1216
1217 std::pop_heap(message_heap.begin(), message_heap.end(),
1218 &SplitMessageReaderHeapCompare);
1219 message_heap.pop_back();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001220 }
Austin Schuh05b70472020-01-01 17:11:17 -08001221 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001222 ss << " }";
1223
1224 return ss.str();
1225}
1226
1227std::string ChannelMerger::DebugString() const {
1228 std::stringstream ss;
1229 ss << "start_time " << realtime_start_time() << " " << monotonic_start_time()
1230 << "\n";
1231 ss << "channel_heap {\n";
1232 std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap =
1233 channel_heap_;
1234 while (channel_heap.size() > 0u) {
1235 std::tuple<monotonic_clock::time_point, int> channel = channel_heap.front();
1236 ss << " " << std::get<0>(channel) << " (" << std::get<1>(channel) << ") "
1237 << configuration::CleanedChannelToString(
1238 configuration()->channels()->Get(std::get<1>(channel)))
1239 << "\n";
1240
1241 ss << timestamp_mergers_[std::get<1>(channel)].DebugString() << "\n";
1242
1243 std::pop_heap(channel_heap.begin(), channel_heap.end(),
1244 &ChannelHeapCompare);
1245 channel_heap.pop_back();
1246 }
1247 ss << "}";
1248
1249 return ss.str();
Austin Schuh05b70472020-01-01 17:11:17 -08001250}
1251
Austin Schuha36c8902019-12-30 18:07:15 -08001252} // namespace logger
1253} // namespace aos