blob: 59f478c075d83dba0a8be9253ee95fc4e294ba90 [file] [log] [blame]
Austin Schuha36c8902019-12-30 18:07:15 -08001#include "aos/events/logging/logfile_utils.h"
2
3#include <fcntl.h>
Austin Schuha36c8902019-12-30 18:07:15 -08004#include <sys/stat.h>
5#include <sys/types.h>
6#include <sys/uio.h>
7
Brian Silvermanf51499a2020-09-21 12:49:08 -07008#include <algorithm>
9#include <climits>
Austin Schuha36c8902019-12-30 18:07:15 -080010
Austin Schuhe4fca832020-03-07 16:58:53 -080011#include "absl/strings/escaping.h"
Austin Schuh05b70472020-01-01 17:11:17 -080012#include "aos/configuration.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
Brian Silvermanf59fe3f2020-09-22 21:04:09 -070019#if defined(__x86_64__)
20#define ENABLE_LZMA 1
21#elif defined(__aarch64__)
22#define ENABLE_LZMA 1
23#else
24#define ENABLE_LZMA 0
25#endif
26
27#if ENABLE_LZMA
28#include "aos/events/logging/lzma_encoder.h"
29#endif
30
Austin Schuh7fbf5a72020-09-21 16:28:13 -070031DEFINE_int32(flush_size, 128000,
Austin Schuha36c8902019-12-30 18:07:15 -080032 "Number of outstanding bytes to allow before flushing to disk.");
33
Brian Silvermanf51499a2020-09-21 12:49:08 -070034namespace aos::logger {
Austin Schuha36c8902019-12-30 18:07:15 -080035
Austin Schuh05b70472020-01-01 17:11:17 -080036namespace chrono = std::chrono;
37
Brian Silvermanf51499a2020-09-21 12:49:08 -070038DetachedBufferWriter::DetachedBufferWriter(
39 std::string_view filename, std::unique_ptr<DetachedBufferEncoder> encoder)
40 : filename_(filename), encoder_(std::move(encoder)) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -070041 if (!util::MkdirPIfSpace(filename, 0777)) {
42 ran_out_of_space_ = true;
43 } else {
44 fd_ = open(std::string(filename).c_str(),
45 O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 0774);
46 if (fd_ == -1 && errno == ENOSPC) {
47 ran_out_of_space_ = true;
48 } else {
49 PCHECK(fd_ != -1) << ": Failed to open " << filename << " for writing";
50 VLOG(1) << "Opened " << filename << " for writing";
51 }
52 }
Austin Schuha36c8902019-12-30 18:07:15 -080053}
54
55DetachedBufferWriter::~DetachedBufferWriter() {
Brian Silverman0465fcf2020-09-24 00:29:18 -070056 Close();
57 if (ran_out_of_space_) {
58 CHECK(acknowledge_ran_out_of_space_)
59 << ": Unacknowledged out of disk space, log file was not completed";
Brian Silvermanf51499a2020-09-21 12:49:08 -070060 }
Austin Schuh2f8fd752020-09-01 22:38:28 -070061}
62
Brian Silvermand90905f2020-09-23 14:42:56 -070063DetachedBufferWriter::DetachedBufferWriter(DetachedBufferWriter &&other) {
Austin Schuh2f8fd752020-09-01 22:38:28 -070064 *this = std::move(other);
65}
66
Brian Silverman87ac0402020-09-17 14:47:01 -070067// When other is destroyed "soon" (which it should be because we're getting an
68// rvalue reference to it), it will flush etc all the data we have queued up
69// (because that data will then be its data).
Austin Schuh2f8fd752020-09-01 22:38:28 -070070DetachedBufferWriter &DetachedBufferWriter::operator=(
71 DetachedBufferWriter &&other) {
Austin Schuh2f8fd752020-09-01 22:38:28 -070072 std::swap(filename_, other.filename_);
Brian Silvermanf51499a2020-09-21 12:49:08 -070073 std::swap(encoder_, other.encoder_);
Austin Schuh2f8fd752020-09-01 22:38:28 -070074 std::swap(fd_, other.fd_);
Brian Silverman0465fcf2020-09-24 00:29:18 -070075 std::swap(ran_out_of_space_, other.ran_out_of_space_);
76 std::swap(acknowledge_ran_out_of_space_, other.acknowledge_ran_out_of_space_);
Austin Schuh2f8fd752020-09-01 22:38:28 -070077 std::swap(iovec_, other.iovec_);
Brian Silvermanf51499a2020-09-21 12:49:08 -070078 std::swap(max_write_time_, other.max_write_time_);
79 std::swap(max_write_time_bytes_, other.max_write_time_bytes_);
80 std::swap(max_write_time_messages_, other.max_write_time_messages_);
81 std::swap(total_write_time_, other.total_write_time_);
82 std::swap(total_write_count_, other.total_write_count_);
83 std::swap(total_write_messages_, other.total_write_messages_);
84 std::swap(total_write_bytes_, other.total_write_bytes_);
Austin Schuh2f8fd752020-09-01 22:38:28 -070085 return *this;
Austin Schuha36c8902019-12-30 18:07:15 -080086}
87
Brian Silvermanf51499a2020-09-21 12:49:08 -070088void DetachedBufferWriter::QueueSpan(absl::Span<const uint8_t> span) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -070089 if (ran_out_of_space_) {
90 // We don't want any later data to be written after space becomes
91 // available, so refuse to write anything more once we've dropped data
92 // because we ran out of space.
93 VLOG(1) << "Ignoring span: " << span.size();
94 return;
95 }
96
Brian Silvermanf51499a2020-09-21 12:49:08 -070097 if (encoder_->may_bypass() && span.size() > 4096u) {
98 // Over this threshold, we'll assume it's cheaper to add an extra
99 // syscall to write the data immediately instead of copying it to
100 // enqueue.
Austin Schuha36c8902019-12-30 18:07:15 -0800101
Brian Silvermanf51499a2020-09-21 12:49:08 -0700102 // First, flush everything.
103 while (encoder_->queue_size() > 0u) {
104 Flush();
105 }
Austin Schuhde031b72020-01-10 19:34:41 -0800106
Brian Silvermanf51499a2020-09-21 12:49:08 -0700107 // Then, write it directly.
108 const auto start = aos::monotonic_clock::now();
109 const ssize_t written = write(fd_, span.data(), span.size());
110 const auto end = aos::monotonic_clock::now();
Brian Silverman0465fcf2020-09-24 00:29:18 -0700111 HandleWriteReturn(written, span.size());
Brian Silvermanf51499a2020-09-21 12:49:08 -0700112 UpdateStatsForWrite(end - start, written, 1);
113 } else {
114 encoder_->Encode(CopySpanAsDetachedBuffer(span));
Austin Schuha36c8902019-12-30 18:07:15 -0800115 }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700116
117 FlushAtThreshold();
Austin Schuha36c8902019-12-30 18:07:15 -0800118}
119
Brian Silverman0465fcf2020-09-24 00:29:18 -0700120void DetachedBufferWriter::Close() {
121 if (fd_ == -1) {
122 return;
123 }
124 encoder_->Finish();
125 while (encoder_->queue_size() > 0) {
126 Flush();
127 }
128 if (close(fd_) == -1) {
129 if (errno == ENOSPC) {
130 ran_out_of_space_ = true;
131 } else {
132 PLOG(ERROR) << "Closing log file failed";
133 }
134 }
135 fd_ = -1;
136 VLOG(1) << "Closed " << filename_;
137}
138
Austin Schuha36c8902019-12-30 18:07:15 -0800139void DetachedBufferWriter::Flush() {
Brian Silvermanf51499a2020-09-21 12:49:08 -0700140 const auto queue = encoder_->queue();
141 if (queue.empty()) {
Austin Schuha36c8902019-12-30 18:07:15 -0800142 return;
143 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700144 if (ran_out_of_space_) {
145 // We don't want any later data to be written after space becomes available,
146 // so refuse to write anything more once we've dropped data because we ran
147 // out of space.
148 VLOG(1) << "Ignoring queue: " << queue.size();
149 encoder_->Clear(queue.size());
150 return;
151 }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700152
Austin Schuha36c8902019-12-30 18:07:15 -0800153 iovec_.clear();
Brian Silvermanf51499a2020-09-21 12:49:08 -0700154 const size_t iovec_size = std::min<size_t>(queue.size(), IOV_MAX);
155 iovec_.resize(iovec_size);
Austin Schuha36c8902019-12-30 18:07:15 -0800156 size_t counted_size = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700157 for (size_t i = 0; i < iovec_size; ++i) {
158 iovec_[i].iov_base = const_cast<uint8_t *>(queue[i].data());
159 iovec_[i].iov_len = queue[i].size();
160 counted_size += iovec_[i].iov_len;
Austin Schuha36c8902019-12-30 18:07:15 -0800161 }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700162
163 const auto start = aos::monotonic_clock::now();
Austin Schuha36c8902019-12-30 18:07:15 -0800164 const ssize_t written = writev(fd_, iovec_.data(), iovec_.size());
Brian Silvermanf51499a2020-09-21 12:49:08 -0700165 const auto end = aos::monotonic_clock::now();
Brian Silverman0465fcf2020-09-24 00:29:18 -0700166 HandleWriteReturn(written, counted_size);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700167
168 encoder_->Clear(iovec_size);
169
170 UpdateStatsForWrite(end - start, written, iovec_size);
171}
172
Brian Silverman0465fcf2020-09-24 00:29:18 -0700173void DetachedBufferWriter::HandleWriteReturn(ssize_t write_return,
174 size_t write_size) {
175 if (write_return == -1 && errno == ENOSPC) {
176 ran_out_of_space_ = true;
177 return;
178 }
179 PCHECK(write_return >= 0) << ": write failed";
180 if (write_return < static_cast<ssize_t>(write_size)) {
181 // Sometimes this happens instead of ENOSPC. On a real filesystem, this
182 // never seems to happen in any other case. If we ever want to log to a
183 // socket, this will happen more often. However, until we get there, we'll
184 // just assume it means we ran out of space.
185 ran_out_of_space_ = true;
186 return;
187 }
188}
189
Brian Silvermanf51499a2020-09-21 12:49:08 -0700190void DetachedBufferWriter::UpdateStatsForWrite(
191 aos::monotonic_clock::duration duration, ssize_t written, int iovec_size) {
192 if (duration > max_write_time_) {
193 max_write_time_ = duration;
194 max_write_time_bytes_ = written;
195 max_write_time_messages_ = iovec_size;
196 }
197 total_write_time_ += duration;
198 ++total_write_count_;
199 total_write_messages_ += iovec_size;
200 total_write_bytes_ += written;
201}
202
203void DetachedBufferWriter::FlushAtThreshold() {
204 // Flush if we are at the max number of iovs per writev, because there's no
205 // point queueing up any more data in memory. Also flush once we have enough
206 // data queued up.
207 while (encoder_->queued_bytes() > static_cast<size_t>(FLAGS_flush_size) ||
208 encoder_->queue_size() >= IOV_MAX) {
209 Flush();
210 }
Austin Schuha36c8902019-12-30 18:07:15 -0800211}
212
213flatbuffers::Offset<MessageHeader> PackMessage(
214 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
215 int channel_index, LogType log_type) {
216 flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset;
217
218 switch (log_type) {
219 case LogType::kLogMessage:
220 case LogType::kLogMessageAndDeliveryTime:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800221 case LogType::kLogRemoteMessage:
Brian Silvermaneaa41d62020-07-08 19:47:35 -0700222 data_offset = fbb->CreateVector(
223 static_cast<const uint8_t *>(context.data), context.size);
Austin Schuha36c8902019-12-30 18:07:15 -0800224 break;
225
226 case LogType::kLogDeliveryTimeOnly:
227 break;
228 }
229
230 MessageHeader::Builder message_header_builder(*fbb);
231 message_header_builder.add_channel_index(channel_index);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800232
233 switch (log_type) {
234 case LogType::kLogRemoteMessage:
235 message_header_builder.add_queue_index(context.remote_queue_index);
236 message_header_builder.add_monotonic_sent_time(
237 context.monotonic_remote_time.time_since_epoch().count());
238 message_header_builder.add_realtime_sent_time(
239 context.realtime_remote_time.time_since_epoch().count());
240 break;
241
242 case LogType::kLogMessage:
243 case LogType::kLogMessageAndDeliveryTime:
244 case LogType::kLogDeliveryTimeOnly:
245 message_header_builder.add_queue_index(context.queue_index);
246 message_header_builder.add_monotonic_sent_time(
247 context.monotonic_event_time.time_since_epoch().count());
248 message_header_builder.add_realtime_sent_time(
249 context.realtime_event_time.time_since_epoch().count());
250 break;
251 }
Austin Schuha36c8902019-12-30 18:07:15 -0800252
253 switch (log_type) {
254 case LogType::kLogMessage:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800255 case LogType::kLogRemoteMessage:
Austin Schuha36c8902019-12-30 18:07:15 -0800256 message_header_builder.add_data(data_offset);
257 break;
258
259 case LogType::kLogMessageAndDeliveryTime:
260 message_header_builder.add_data(data_offset);
261 [[fallthrough]];
262
263 case LogType::kLogDeliveryTimeOnly:
264 message_header_builder.add_monotonic_remote_time(
265 context.monotonic_remote_time.time_since_epoch().count());
266 message_header_builder.add_realtime_remote_time(
267 context.realtime_remote_time.time_since_epoch().count());
268 message_header_builder.add_remote_queue_index(context.remote_queue_index);
269 break;
270 }
271
272 return message_header_builder.Finish();
273}
274
Brian Silvermanf51499a2020-09-21 12:49:08 -0700275SpanReader::SpanReader(std::string_view filename) : filename_(filename) {
Brian Silvermanf59fe3f2020-09-22 21:04:09 -0700276 static const std::string_view kXz = ".xz";
277 if (filename.substr(filename.size() - kXz.size()) == kXz) {
278#if ENABLE_LZMA
279 decoder_ = std::make_unique<LzmaDecoder>(filename);
280#else
281 LOG(FATAL) << "Reading xz-compressed files not supported on this platform";
282#endif
283 } else {
284 decoder_ = std::make_unique<DummyDecoder>(filename);
285 }
Austin Schuh05b70472020-01-01 17:11:17 -0800286}
287
288absl::Span<const uint8_t> SpanReader::ReadMessage() {
289 // Make sure we have enough for the size.
290 if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) {
291 if (!ReadBlock()) {
292 return absl::Span<const uint8_t>();
293 }
294 }
295
296 // Now make sure we have enough for the message.
297 const size_t data_size =
298 flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) +
299 sizeof(flatbuffers::uoffset_t);
Austin Schuhe4fca832020-03-07 16:58:53 -0800300 if (data_size == sizeof(flatbuffers::uoffset_t)) {
301 LOG(ERROR) << "Size of data is zero. Log file end is corrupted, skipping.";
302 LOG(ERROR) << " Rest of log file is "
303 << absl::BytesToHexString(std::string_view(
304 reinterpret_cast<const char *>(data_.data() +
305 consumed_data_),
306 data_.size() - consumed_data_));
307 return absl::Span<const uint8_t>();
308 }
Austin Schuh05b70472020-01-01 17:11:17 -0800309 while (data_.size() < consumed_data_ + data_size) {
310 if (!ReadBlock()) {
311 return absl::Span<const uint8_t>();
312 }
313 }
314
315 // And return it, consuming the data.
316 const uint8_t *data_ptr = data_.data() + consumed_data_;
317
318 consumed_data_ += data_size;
319
320 return absl::Span<const uint8_t>(data_ptr, data_size);
321}
322
Austin Schuh05b70472020-01-01 17:11:17 -0800323bool SpanReader::ReadBlock() {
Brian Silvermanf51499a2020-09-21 12:49:08 -0700324 // This is the amount of data we grab at a time. Doing larger chunks minimizes
325 // syscalls and helps decompressors batch things more efficiently.
Austin Schuh05b70472020-01-01 17:11:17 -0800326 constexpr size_t kReadSize = 256 * 1024;
327
328 // Strip off any unused data at the front.
329 if (consumed_data_ != 0) {
Brian Silvermanf51499a2020-09-21 12:49:08 -0700330 data_.erase_front(consumed_data_);
Austin Schuh05b70472020-01-01 17:11:17 -0800331 consumed_data_ = 0;
332 }
333
334 const size_t starting_size = data_.size();
335
336 // This should automatically grow the backing store. It won't shrink if we
337 // get a small chunk later. This reduces allocations when we want to append
338 // more data.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700339 data_.resize(starting_size + kReadSize);
Austin Schuh05b70472020-01-01 17:11:17 -0800340
Brian Silvermanf51499a2020-09-21 12:49:08 -0700341 const size_t count =
342 decoder_->Read(data_.begin() + starting_size, data_.end());
343 data_.resize(starting_size + count);
Austin Schuh05b70472020-01-01 17:11:17 -0800344 if (count == 0) {
Austin Schuh05b70472020-01-01 17:11:17 -0800345 return false;
346 }
Austin Schuh05b70472020-01-01 17:11:17 -0800347
348 return true;
349}
350
Austin Schuhadd6eb32020-11-09 21:24:26 -0800351std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
Austin Schuh3bd4c402020-11-06 18:19:06 -0800352 std::string_view filename) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800353 SpanReader span_reader(filename);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800354 absl::Span<const uint8_t> config_data = span_reader.ReadMessage();
355
356 // Make sure something was read.
Austin Schuh3bd4c402020-11-06 18:19:06 -0800357 if (config_data == absl::Span<const uint8_t>()) {
358 return std::nullopt;
359 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800360
Austin Schuh5212cad2020-09-09 23:12:09 -0700361 // And copy the config so we have it forever, removing the size prefix.
Brian Silverman354697a2020-09-22 21:06:32 -0700362 ResizeableBuffer data;
Austin Schuhadd6eb32020-11-09 21:24:26 -0800363 data.resize(config_data.size());
364 memcpy(data.data(), config_data.begin(), data.size());
Austin Schuhe09beb12020-12-11 20:04:27 -0800365 SizePrefixedFlatbufferVector<LogFileHeader> result(std::move(data));
366 if (!result.Verify()) {
367 return std::nullopt;
368 }
369 return result;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800370}
371
Austin Schuhadd6eb32020-11-09 21:24:26 -0800372std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage(
Austin Schuh3bd4c402020-11-06 18:19:06 -0800373 std::string_view filename, size_t n) {
Austin Schuh5212cad2020-09-09 23:12:09 -0700374 SpanReader span_reader(filename);
375 absl::Span<const uint8_t> data_span = span_reader.ReadMessage();
376 for (size_t i = 0; i < n + 1; ++i) {
377 data_span = span_reader.ReadMessage();
378
379 // Make sure something was read.
Austin Schuh3bd4c402020-11-06 18:19:06 -0800380 if (data_span == absl::Span<const uint8_t>()) {
381 return std::nullopt;
382 }
Austin Schuh5212cad2020-09-09 23:12:09 -0700383 }
384
Brian Silverman354697a2020-09-22 21:06:32 -0700385 // And copy the config so we have it forever, removing the size prefix.
386 ResizeableBuffer data;
Austin Schuhadd6eb32020-11-09 21:24:26 -0800387 data.resize(data_span.size());
388 memcpy(data.data(), data_span.begin(), data.size());
Austin Schuhe09beb12020-12-11 20:04:27 -0800389 SizePrefixedFlatbufferVector<MessageHeader> result(std::move(data));
390 if (!result.Verify()) {
391 return std::nullopt;
392 }
393 return result;
Austin Schuh5212cad2020-09-09 23:12:09 -0700394}
395
Austin Schuh05b70472020-01-01 17:11:17 -0800396MessageReader::MessageReader(std::string_view filename)
Austin Schuh97789fc2020-08-01 14:42:45 -0700397 : span_reader_(filename),
Austin Schuhadd6eb32020-11-09 21:24:26 -0800398 raw_log_file_header_(
399 SizePrefixedFlatbufferVector<LogFileHeader>::Empty()) {
Austin Schuh05b70472020-01-01 17:11:17 -0800400 // Make sure we have enough to read the size.
Austin Schuh97789fc2020-08-01 14:42:45 -0700401 absl::Span<const uint8_t> header_data = span_reader_.ReadMessage();
Austin Schuh05b70472020-01-01 17:11:17 -0800402
403 // Make sure something was read.
Austin Schuh97789fc2020-08-01 14:42:45 -0700404 CHECK(header_data != absl::Span<const uint8_t>())
405 << ": Failed to read header from: " << filename;
Austin Schuh05b70472020-01-01 17:11:17 -0800406
Austin Schuh97789fc2020-08-01 14:42:45 -0700407 // And copy the header data so we have it forever.
Brian Silverman354697a2020-09-22 21:06:32 -0700408 ResizeableBuffer header_data_copy;
Austin Schuhadd6eb32020-11-09 21:24:26 -0800409 header_data_copy.resize(header_data.size());
410 memcpy(header_data_copy.data(), header_data.begin(), header_data_copy.size());
Austin Schuh97789fc2020-08-01 14:42:45 -0700411 raw_log_file_header_ =
Austin Schuhadd6eb32020-11-09 21:24:26 -0800412 SizePrefixedFlatbufferVector<LogFileHeader>(std::move(header_data_copy));
Austin Schuh05b70472020-01-01 17:11:17 -0800413
Austin Schuhcde938c2020-02-02 17:30:07 -0800414 max_out_of_order_duration_ =
Austin Schuh2f8fd752020-09-01 22:38:28 -0700415 chrono::nanoseconds(log_file_header()->max_out_of_order_duration());
Austin Schuhcde938c2020-02-02 17:30:07 -0800416
417 VLOG(1) << "Opened " << filename << " as node "
418 << FlatbufferToJson(log_file_header()->node());
Austin Schuh05b70472020-01-01 17:11:17 -0800419}
420
Austin Schuhadd6eb32020-11-09 21:24:26 -0800421std::optional<SizePrefixedFlatbufferVector<MessageHeader>>
422MessageReader::ReadMessage() {
Austin Schuh05b70472020-01-01 17:11:17 -0800423 absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage();
424 if (msg_data == absl::Span<const uint8_t>()) {
425 return std::nullopt;
426 }
427
Brian Silverman354697a2020-09-22 21:06:32 -0700428 ResizeableBuffer result_buffer;
Austin Schuhadd6eb32020-11-09 21:24:26 -0800429 result_buffer.resize(msg_data.size());
430 memcpy(result_buffer.data(), msg_data.begin(), result_buffer.size());
431 SizePrefixedFlatbufferVector<MessageHeader> result(std::move(result_buffer));
Austin Schuh05b70472020-01-01 17:11:17 -0800432
433 const monotonic_clock::time_point timestamp = monotonic_clock::time_point(
434 chrono::nanoseconds(result.message().monotonic_sent_time()));
435
436 newest_timestamp_ = std::max(newest_timestamp_, timestamp);
Austin Schuh8bd96322020-02-13 21:18:22 -0800437 VLOG(2) << "Read from " << filename() << " data " << FlatbufferToJson(result);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800438 return std::move(result);
Austin Schuh05b70472020-01-01 17:11:17 -0800439}
440
Austin Schuhc41603c2020-10-11 16:17:37 -0700441PartsMessageReader::PartsMessageReader(LogParts log_parts)
442 : parts_(std::move(log_parts)), message_reader_(parts_.parts[0]) {}
443
Austin Schuhadd6eb32020-11-09 21:24:26 -0800444std::optional<SizePrefixedFlatbufferVector<MessageHeader>>
Austin Schuhc41603c2020-10-11 16:17:37 -0700445PartsMessageReader::ReadMessage() {
446 while (!done_) {
Austin Schuhadd6eb32020-11-09 21:24:26 -0800447 std::optional<SizePrefixedFlatbufferVector<MessageHeader>> message =
Austin Schuhc41603c2020-10-11 16:17:37 -0700448 message_reader_.ReadMessage();
449 if (message) {
450 newest_timestamp_ = message_reader_.newest_timestamp();
Austin Schuh32f68492020-11-08 21:45:51 -0800451 const monotonic_clock::time_point monotonic_sent_time(
452 chrono::nanoseconds(message->message().monotonic_sent_time()));
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800453 // TODO(austin): Does this work with startup? Might need to use the start
454 // time.
455 // TODO(austin): Does this work with startup when we don't know the remote
456 // start time too? Look at one of those logs to compare.
Austin Schuh315b96b2020-12-11 21:21:12 -0800457 if (monotonic_sent_time >
458 parts_.monotonic_start_time + max_out_of_order_duration()) {
459 after_start_ = true;
460 }
461 if (after_start_) {
Austin Schuhb000de62020-12-03 22:00:40 -0800462 CHECK_GE(monotonic_sent_time,
463 newest_timestamp_ - max_out_of_order_duration())
Austin Schuh315b96b2020-12-11 21:21:12 -0800464 << ": Max out of order exceeded. " << parts_ << ", start time is "
465 << parts_.monotonic_start_time << " currently reading "
466 << filename();
Austin Schuhb000de62020-12-03 22:00:40 -0800467 }
Austin Schuhc41603c2020-10-11 16:17:37 -0700468 return message;
469 }
470 NextLog();
471 }
Austin Schuh32f68492020-11-08 21:45:51 -0800472 newest_timestamp_ = monotonic_clock::max_time;
Austin Schuhc41603c2020-10-11 16:17:37 -0700473 return std::nullopt;
474}
475
476void PartsMessageReader::NextLog() {
477 if (next_part_index_ == parts_.parts.size()) {
478 done_ = true;
479 return;
480 }
481 message_reader_ = MessageReader(parts_.parts[next_part_index_]);
482 ++next_part_index_;
483}
484
Austin Schuh1be0ce42020-11-29 22:43:26 -0800485bool Message::operator<(const Message &m2) const {
486 if (this->timestamp < m2.timestamp) {
487 return true;
488 } else if (this->timestamp > m2.timestamp) {
489 return false;
490 }
491
492 if (this->channel_index < m2.channel_index) {
493 return true;
494 } else if (this->channel_index > m2.channel_index) {
495 return false;
496 }
497
498 return this->queue_index < m2.queue_index;
499}
500
501bool Message::operator>=(const Message &m2) const { return !(*this < m2); }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800502bool Message::operator==(const Message &m2) const {
503 return timestamp == m2.timestamp && channel_index == m2.channel_index &&
504 queue_index == m2.queue_index;
505}
Austin Schuh1be0ce42020-11-29 22:43:26 -0800506
507std::ostream &operator<<(std::ostream &os, const Message &m) {
508 os << "{.channel_index=" << m.channel_index
Austin Schuhd2f96102020-12-01 20:27:29 -0800509 << ", .queue_index=" << m.queue_index << ", .timestamp=" << m.timestamp;
510 if (m.data.Verify()) {
511 os << ", .data="
512 << aos::FlatbufferToJson(m.data,
513 {.multi_line = false, .max_vector_size = 1});
514 }
515 os << "}";
516 return os;
517}
518
519std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m) {
520 os << "{.channel_index=" << m.channel_index
521 << ", .queue_index=" << m.queue_index
522 << ", .monotonic_event_time=" << m.monotonic_event_time
523 << ", .realtime_event_time=" << m.realtime_event_time;
524 if (m.remote_queue_index != 0xffffffff) {
525 os << ", .remote_queue_index=" << m.remote_queue_index;
526 }
527 if (m.monotonic_remote_time != monotonic_clock::min_time) {
528 os << ", .monotonic_remote_time=" << m.monotonic_remote_time;
529 }
530 if (m.realtime_remote_time != realtime_clock::min_time) {
531 os << ", .realtime_remote_time=" << m.realtime_remote_time;
532 }
533 if (m.data.Verify()) {
534 os << ", .data="
535 << aos::FlatbufferToJson(m.data,
536 {.multi_line = false, .max_vector_size = 1});
537 }
538 os << "}";
Austin Schuh1be0ce42020-11-29 22:43:26 -0800539 return os;
540}
541
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800542LogPartsSorter::LogPartsSorter(LogParts log_parts)
543 : parts_message_reader_(log_parts) {}
544
545Message *LogPartsSorter::Front() {
546 // Queue up data until enough data has been queued that the front message is
547 // sorted enough to be safe to pop. This may do nothing, so we should make
548 // sure the nothing path is checked quickly.
549 if (sorted_until() != monotonic_clock::max_time) {
550 while (true) {
Austin Schuhb000de62020-12-03 22:00:40 -0800551 if (!messages_.empty() && messages_.begin()->timestamp < sorted_until() &&
552 sorted_until() >= monotonic_start_time()) {
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800553 break;
554 }
555
556 std::optional<SizePrefixedFlatbufferVector<MessageHeader>> m =
557 parts_message_reader_.ReadMessage();
558 // No data left, sorted forever, work through what is left.
559 if (!m) {
560 sorted_until_ = monotonic_clock::max_time;
561 break;
562 }
563
564 messages_.insert(
565 {.channel_index = m.value().message().channel_index(),
566 .queue_index = m.value().message().queue_index(),
567 .timestamp = monotonic_clock::time_point(std::chrono::nanoseconds(
568 m.value().message().monotonic_sent_time())),
569 .data = std::move(m.value())});
570
571 // Now, update sorted_until_ to match the new message.
572 if (parts_message_reader_.newest_timestamp() >
573 monotonic_clock::min_time +
574 parts_message_reader_.max_out_of_order_duration()) {
575 sorted_until_ = parts_message_reader_.newest_timestamp() -
576 parts_message_reader_.max_out_of_order_duration();
577 } else {
578 sorted_until_ = monotonic_clock::min_time;
579 }
580 }
581 }
582
583 // Now that we have enough data queued, return a pointer to the oldest piece
584 // of data if it exists.
585 if (messages_.empty()) {
Austin Schuhb000de62020-12-03 22:00:40 -0800586 last_message_time_ = monotonic_clock::max_time;
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800587 return nullptr;
588 }
589
Austin Schuh315b96b2020-12-11 21:21:12 -0800590 CHECK_GE(messages_.begin()->timestamp, last_message_time_)
591 << DebugString() << " reading " << parts_message_reader_.filename();
Austin Schuhb000de62020-12-03 22:00:40 -0800592 last_message_time_ = messages_.begin()->timestamp;
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800593 return &(*messages_.begin());
594}
595
596void LogPartsSorter::PopFront() { messages_.erase(messages_.begin()); }
597
598std::string LogPartsSorter::DebugString() const {
599 std::stringstream ss;
600 ss << "messages: [\n";
Austin Schuh315b96b2020-12-11 21:21:12 -0800601 int count = 0;
602 bool no_dots = true;
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800603 for (const Message &m : messages_) {
Austin Schuh315b96b2020-12-11 21:21:12 -0800604 if (count < 15 || count > static_cast<int>(messages_.size()) - 15) {
605 ss << m << "\n";
606 } else if (no_dots) {
607 ss << "...\n";
608 no_dots = false;
609 }
610 ++count;
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800611 }
612 ss << "] <- " << parts_message_reader_.filename();
613 return ss.str();
614}
615
Austin Schuhd2f96102020-12-01 20:27:29 -0800616NodeMerger::NodeMerger(std::vector<LogParts> parts) {
617 CHECK_GE(parts.size(), 1u);
618 const std::string part0_node = parts[0].node;
619 for (size_t i = 1; i < parts.size(); ++i) {
620 CHECK_EQ(part0_node, parts[i].node) << ": Can't merge different nodes.";
621 }
622 for (LogParts &part : parts) {
623 parts_sorters_.emplace_back(std::move(part));
624 }
625
Austin Schuh0ca51f32020-12-25 21:51:45 -0800626 node_ = configuration::GetNodeIndex(configuration(), part0_node);
Austin Schuhd2f96102020-12-01 20:27:29 -0800627
628 monotonic_start_time_ = monotonic_clock::max_time;
629 realtime_start_time_ = realtime_clock::max_time;
630 for (const LogPartsSorter &parts_sorter : parts_sorters_) {
631 if (parts_sorter.monotonic_start_time() < monotonic_start_time_) {
632 monotonic_start_time_ = parts_sorter.monotonic_start_time();
633 realtime_start_time_ = parts_sorter.realtime_start_time();
634 }
635 }
636}
Austin Schuh8f52ed52020-11-30 23:12:39 -0800637
Austin Schuh0ca51f32020-12-25 21:51:45 -0800638std::vector<const LogParts *> NodeMerger::Parts() const {
639 std::vector<const LogParts *> p;
640 p.reserve(parts_sorters_.size());
641 for (const LogPartsSorter &parts_sorter : parts_sorters_) {
642 p.emplace_back(&parts_sorter.parts());
643 }
644 return p;
645}
646
Austin Schuh8f52ed52020-11-30 23:12:39 -0800647Message *NodeMerger::Front() {
648 // Return the current Front if we have one, otherwise go compute one.
649 if (current_ != nullptr) {
Austin Schuhb000de62020-12-03 22:00:40 -0800650 Message *result = current_->Front();
651 CHECK_GE(result->timestamp, last_message_time_);
652 return result;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800653 }
654
655 // Otherwise, do a simple search for the oldest message, deduplicating any
656 // duplicates.
657 Message *oldest = nullptr;
658 sorted_until_ = monotonic_clock::max_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800659 for (LogPartsSorter &parts_sorter : parts_sorters_) {
660 Message *m = parts_sorter.Front();
Austin Schuh8f52ed52020-11-30 23:12:39 -0800661 if (!m) {
Austin Schuhd2f96102020-12-01 20:27:29 -0800662 sorted_until_ = std::min(sorted_until_, parts_sorter.sorted_until());
Austin Schuh8f52ed52020-11-30 23:12:39 -0800663 continue;
664 }
665 if (oldest == nullptr || *m < *oldest) {
666 oldest = m;
Austin Schuhd2f96102020-12-01 20:27:29 -0800667 current_ = &parts_sorter;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800668 } else if (*m == *oldest) {
669 // Found a duplicate. It doesn't matter which one we return. It is
670 // easiest to just drop the new one.
Austin Schuhd2f96102020-12-01 20:27:29 -0800671 parts_sorter.PopFront();
Austin Schuh8f52ed52020-11-30 23:12:39 -0800672 }
673
674 // PopFront may change this, so compute it down here.
Austin Schuhd2f96102020-12-01 20:27:29 -0800675 sorted_until_ = std::min(sorted_until_, parts_sorter.sorted_until());
Austin Schuh8f52ed52020-11-30 23:12:39 -0800676 }
677
Austin Schuhb000de62020-12-03 22:00:40 -0800678 if (oldest) {
679 CHECK_GE(oldest->timestamp, last_message_time_);
680 last_message_time_ = oldest->timestamp;
681 } else {
682 last_message_time_ = monotonic_clock::max_time;
683 }
684
Austin Schuh8f52ed52020-11-30 23:12:39 -0800685 // Return the oldest message found. This will be nullptr if nothing was
686 // found, indicating there is nothing left.
687 return oldest;
688}
689
690void NodeMerger::PopFront() {
691 CHECK(current_ != nullptr) << "Popping before calling Front()";
692 current_->PopFront();
693 current_ = nullptr;
694}
695
Austin Schuhd2f96102020-12-01 20:27:29 -0800696TimestampMapper::TimestampMapper(std::vector<LogParts> parts)
697 : node_merger_(std::move(parts)),
Austin Schuhd2f96102020-12-01 20:27:29 -0800698 message_{.channel_index = 0xffffffff,
699 .queue_index = 0xffffffff,
700 .monotonic_event_time = monotonic_clock::min_time,
701 .realtime_event_time = realtime_clock::min_time,
702 .remote_queue_index = 0xffffffff,
703 .monotonic_remote_time = monotonic_clock::min_time,
704 .realtime_remote_time = realtime_clock::min_time,
705 .data = SizePrefixedFlatbufferVector<MessageHeader>::Empty()} {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800706 for (const LogParts *part : node_merger_.Parts()) {
707 if (!configuration_) {
708 configuration_ = part->config;
709 } else {
710 CHECK_EQ(configuration_.get(), part->config.get());
711 }
712 }
713 const Configuration *config = configuration_.get();
Austin Schuhd2f96102020-12-01 20:27:29 -0800714 // Only fill out nodes_data_ if there are nodes. Otherwise everything gets
715 // pretty simple.
716 if (configuration::MultiNode(config)) {
717 nodes_data_.resize(config->nodes()->size());
718 const Node *my_node = config->nodes()->Get(node());
719 for (size_t node_index = 0; node_index < nodes_data_.size(); ++node_index) {
720 const Node *node = config->nodes()->Get(node_index);
721 NodeData *node_data = &nodes_data_[node_index];
722 node_data->channels.resize(config->channels()->size());
723 // We should save the channel if it is delivered to the node represented
724 // by the NodeData, but not sent by that node. That combo means it is
725 // forwarded.
726 size_t channel_index = 0;
727 node_data->any_delivered = false;
728 for (const Channel *channel : *config->channels()) {
729 node_data->channels[channel_index].delivered =
730 configuration::ChannelIsReadableOnNode(channel, node) &&
Austin Schuhb3dbb6d2021-01-02 17:29:35 -0800731 configuration::ChannelIsSendableOnNode(channel, my_node) &&
732 (my_node != node);
Austin Schuhd2f96102020-12-01 20:27:29 -0800733 node_data->any_delivered = node_data->any_delivered ||
734 node_data->channels[channel_index].delivered;
735 ++channel_index;
736 }
737 }
738
739 for (const Channel *channel : *config->channels()) {
740 source_node_.emplace_back(configuration::GetNodeIndex(
741 config, channel->source_node()->string_view()));
742 }
743 }
744}
745
746void TimestampMapper::AddPeer(TimestampMapper *timestamp_mapper) {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800747 CHECK(configuration::MultiNode(configuration()));
Austin Schuhd2f96102020-12-01 20:27:29 -0800748 CHECK_NE(timestamp_mapper->node(), node());
749 CHECK_LT(timestamp_mapper->node(), nodes_data_.size());
750
751 NodeData *node_data = &nodes_data_[timestamp_mapper->node()];
752 // Only set it if this node delivers to the peer timestamp_mapper. Otherwise
753 // we could needlessly save data.
754 if (node_data->any_delivered) {
755 LOG(INFO) << "Registering on node " << node() << " for peer node "
756 << timestamp_mapper->node();
757 CHECK(timestamp_mapper->nodes_data_[node()].peer == nullptr);
758
759 timestamp_mapper->nodes_data_[node()].peer = this;
760 }
761}
762
763void TimestampMapper::FillMessage(Message *m) {
764 message_ = {
765 .channel_index = m->channel_index,
766 .queue_index = m->queue_index,
767 .monotonic_event_time = m->timestamp,
768 .realtime_event_time = aos::realtime_clock::time_point(
769 std::chrono::nanoseconds(m->data.message().realtime_sent_time())),
770 .remote_queue_index = 0xffffffff,
771 .monotonic_remote_time = monotonic_clock::min_time,
772 .realtime_remote_time = realtime_clock::min_time,
773 .data = std::move(m->data)};
774}
775
776TimestampedMessage *TimestampMapper::Front() {
777 // No need to fetch anything new. A previous message still exists.
778 switch (first_message_) {
779 case FirstMessage::kNeedsUpdate:
780 break;
781 case FirstMessage::kInMessage:
782 return &message_;
783 case FirstMessage::kNullptr:
784 return nullptr;
785 }
786
787 if (nodes_data_.empty()) {
788 // Simple path. We are single node, so there are no timestamps to match!
789 CHECK_EQ(messages_.size(), 0u);
790 Message *m = node_merger_.Front();
791 if (!m) {
792 first_message_ = FirstMessage::kNullptr;
793 return nullptr;
794 }
795 // Fill in message_ so we have a place to associate remote timestamps, and
796 // return it.
797 FillMessage(m);
798
799 CHECK_GE(message_.monotonic_event_time, last_message_time_);
800 last_message_time_ = message_.monotonic_event_time;
801 first_message_ = FirstMessage::kInMessage;
802 return &message_;
803 }
804
805 // We need to only add messages to the list so they get processed for messages
806 // which are delivered. Reuse the flow below which uses messages_ by just
807 // adding the new message to messages_ and continuing.
808 if (messages_.empty()) {
809 if (!Queue()) {
810 // Found nothing to add, we are out of data!
811 first_message_ = FirstMessage::kNullptr;
812 return nullptr;
813 }
814
815 // Now that it has been added (and cannibalized), forget about it upstream.
816 node_merger_.PopFront();
817 }
818
819 Message *m = &(messages_.front());
820
821 if (source_node_[m->channel_index] == node()) {
822 // From us, just forward it on, filling the remote data in as invalid.
823 FillMessage(m);
824 CHECK_GE(message_.monotonic_event_time, last_message_time_);
825 last_message_time_ = message_.monotonic_event_time;
826 first_message_ = FirstMessage::kInMessage;
827 return &message_;
828 } else {
829 // Got a timestamp, find the matching remote data, match it, and return it.
830 Message data = MatchingMessageFor(*m);
831
832 // Return the data from the remote. The local message only has timestamp
833 // info which isn't relevant anymore once extracted.
834 message_ = {
835 .channel_index = m->channel_index,
836 .queue_index = m->queue_index,
837 .monotonic_event_time = m->timestamp,
838 .realtime_event_time = aos::realtime_clock::time_point(
839 std::chrono::nanoseconds(m->data.message().realtime_sent_time())),
840 .remote_queue_index = m->data.message().remote_queue_index(),
841 .monotonic_remote_time =
842 monotonic_clock::time_point(std::chrono::nanoseconds(
843 m->data.message().monotonic_remote_time())),
844 .realtime_remote_time = realtime_clock::time_point(
845 std::chrono::nanoseconds(m->data.message().realtime_remote_time())),
846 .data = std::move(data.data)};
847 CHECK_GE(message_.monotonic_event_time, last_message_time_);
848 last_message_time_ = message_.monotonic_event_time;
849 first_message_ = FirstMessage::kInMessage;
850 return &message_;
851 }
852}
853
854void TimestampMapper::PopFront() {
855 CHECK(first_message_ != FirstMessage::kNeedsUpdate);
856 first_message_ = FirstMessage::kNeedsUpdate;
857
858 if (nodes_data_.empty()) {
859 // We are thin wrapper around node_merger. Call it directly.
860 node_merger_.PopFront();
861 } else {
862 // Since messages_ holds the data, drop it.
863 messages_.pop_front();
864 }
865}
866
867Message TimestampMapper::MatchingMessageFor(const Message &message) {
Austin Schuhd2f96102020-12-01 20:27:29 -0800868 // Figure out what queue index we are looking for.
869 CHECK(message.data.message().has_remote_queue_index());
870 const uint32_t remote_queue_index =
871 message.data.message().remote_queue_index();
872
873 CHECK(message.data.message().has_monotonic_remote_time());
874 CHECK(message.data.message().has_realtime_remote_time());
875
876 const monotonic_clock::time_point monotonic_remote_time(
877 std::chrono::nanoseconds(message.data.message().monotonic_remote_time()));
878 const realtime_clock::time_point realtime_remote_time(
879 std::chrono::nanoseconds(message.data.message().realtime_remote_time()));
880
Austin Schuhfecf1d82020-12-19 16:57:28 -0800881 TimestampMapper *peer = nodes_data_[source_node_[message.channel_index]].peer;
882
883 // We only register the peers which we have data for. So, if we are being
884 // asked to pull a timestamp from a peer which doesn't exist, return an empty
885 // message.
886 if (peer == nullptr) {
887 return Message{
888 .channel_index = message.channel_index,
889 .queue_index = remote_queue_index,
890 .timestamp = monotonic_remote_time,
891 .data = SizePrefixedFlatbufferVector<MessageHeader>::Empty()};
892 }
893
894 // The queue which will have the matching data, if available.
895 std::deque<Message> *data_queue =
896 &peer->nodes_data_[node()].channels[message.channel_index].messages;
897
Austin Schuhd2f96102020-12-01 20:27:29 -0800898 peer->QueueUntil(monotonic_remote_time);
899
900 if (data_queue->empty()) {
901 return Message{
902 .channel_index = message.channel_index,
903 .queue_index = remote_queue_index,
904 .timestamp = monotonic_remote_time,
905 .data = SizePrefixedFlatbufferVector<MessageHeader>::Empty()};
906 }
907
Austin Schuhd2f96102020-12-01 20:27:29 -0800908 if (remote_queue_index < data_queue->front().queue_index ||
909 remote_queue_index > data_queue->back().queue_index) {
910 return Message{
911 .channel_index = message.channel_index,
912 .queue_index = remote_queue_index,
913 .timestamp = monotonic_remote_time,
914 .data = SizePrefixedFlatbufferVector<MessageHeader>::Empty()};
915 }
916
Austin Schuh993ccb52020-12-12 15:59:32 -0800917 // The algorithm below is constant time with some assumptions. We need there
918 // to be no missing messages in the data stream. This also assumes a queue
919 // hasn't wrapped. That is conservative, but should let us get started.
920 if (data_queue->back().queue_index - data_queue->front().queue_index + 1u ==
921 data_queue->size()) {
922 // Pull the data out and confirm that the timestamps match as expected.
923 Message result = std::move(
924 (*data_queue)[remote_queue_index - data_queue->front().queue_index]);
925
926 CHECK_EQ(result.timestamp, monotonic_remote_time)
927 << ": Queue index matches, but timestamp doesn't. Please investigate!";
928 CHECK_EQ(realtime_clock::time_point(std::chrono::nanoseconds(
929 result.data.message().realtime_sent_time())),
930 realtime_remote_time)
931 << ": Queue index matches, but timestamp doesn't. Please investigate!";
932 // Now drop the data off the front. We have deduplicated timestamps, so we
933 // are done. And all the data is in order.
934 data_queue->erase(data_queue->begin(),
935 data_queue->begin() + (1 + remote_queue_index -
936 data_queue->front().queue_index));
937 return result;
938 } else {
939 auto it = std::find_if(data_queue->begin(), data_queue->end(),
940 [remote_queue_index](const Message &m) {
941 return m.queue_index == remote_queue_index;
942 });
943 if (it == data_queue->end()) {
944 return Message{
945 .channel_index = message.channel_index,
946 .queue_index = remote_queue_index,
947 .timestamp = monotonic_remote_time,
948 .data = SizePrefixedFlatbufferVector<MessageHeader>::Empty()};
949 }
950
951 Message result = std::move(*it);
952
953 CHECK_EQ(result.timestamp, monotonic_remote_time)
954 << ": Queue index matches, but timestamp doesn't. Please investigate!";
955 CHECK_EQ(realtime_clock::time_point(std::chrono::nanoseconds(
956 result.data.message().realtime_sent_time())),
957 realtime_remote_time)
958 << ": Queue index matches, but timestamp doesn't. Please investigate!";
959
960 data_queue->erase(it);
961
962 return result;
963 }
Austin Schuhd2f96102020-12-01 20:27:29 -0800964}
965
966void TimestampMapper::QueueUntil(monotonic_clock::time_point t) {
967 if (queued_until_ > t) {
968 return;
969 }
970 while (true) {
971 if (!messages_.empty() && messages_.back().timestamp > t) {
972 queued_until_ = std::max(queued_until_, messages_.back().timestamp);
973 return;
974 }
975
976 if (!Queue()) {
977 // Found nothing to add, we are out of data!
978 queued_until_ = monotonic_clock::max_time;
979 return;
980 }
981
982 // Now that it has been added (and cannibalized), forget about it upstream.
983 node_merger_.PopFront();
984 }
985}
986
987bool TimestampMapper::Queue() {
988 Message *m = node_merger_.Front();
989 if (m == nullptr) {
990 return false;
991 }
992 for (NodeData &node_data : nodes_data_) {
993 if (!node_data.any_delivered) continue;
994 if (node_data.channels[m->channel_index].delivered) {
995 // TODO(austin): This copies the data... Probably not worth stressing
996 // about yet.
997 // TODO(austin): Bound how big this can get. We tend not to send massive
998 // data, so we can probably ignore this for a bit.
999 node_data.channels[m->channel_index].messages.emplace_back(*m);
1000 }
1001 }
1002
1003 messages_.emplace_back(std::move(*m));
1004 return true;
1005}
1006
1007std::string TimestampMapper::DebugString() const {
1008 std::stringstream ss;
1009 ss << "node " << node() << " [\n";
1010 for (const Message &message : messages_) {
1011 ss << " " << message << "\n";
1012 }
1013 ss << "] queued_until " << queued_until_;
1014 for (const NodeData &ns : nodes_data_) {
1015 if (ns.peer == nullptr) continue;
1016 ss << "\nnode " << ns.peer->node() << " remote_data [\n";
1017 size_t channel_index = 0;
1018 for (const NodeData::ChannelData &channel_data :
1019 ns.peer->nodes_data_[node()].channels) {
1020 if (channel_data.messages.empty()) {
1021 continue;
1022 }
Austin Schuhb000de62020-12-03 22:00:40 -08001023
Austin Schuhd2f96102020-12-01 20:27:29 -08001024 ss << " channel " << channel_index << " [\n";
1025 for (const Message &m : channel_data.messages) {
1026 ss << " " << m << "\n";
1027 }
1028 ss << " ]\n";
1029 ++channel_index;
1030 }
1031 ss << "] queued_until " << ns.peer->queued_until_;
1032 }
1033 return ss.str();
1034}
1035
Austin Schuhee711052020-08-24 16:06:09 -07001036std::string MaybeNodeName(const Node *node) {
1037 if (node != nullptr) {
1038 return node->name()->str() + " ";
1039 }
1040 return "";
1041}
1042
Brian Silvermanf51499a2020-09-21 12:49:08 -07001043} // namespace aos::logger