Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 1 | #include "aos/events/logging/logfile_utils.h" |
| 2 | |
| 3 | #include <fcntl.h> |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 4 | #include <sys/stat.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/uio.h> |
| 7 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | #include <climits> |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 10 | |
Austin Schuh | e4fca83 | 2020-03-07 16:58:53 -0800 | [diff] [blame] | 11 | #include "absl/strings/escaping.h" |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 12 | #include "aos/configuration.h" |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 13 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 14 | #include "aos/util/file.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 15 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 16 | #include "gflags/gflags.h" |
| 17 | #include "glog/logging.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 18 | |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 19 | #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 Schuh | 7fbf5a7 | 2020-09-21 16:28:13 -0700 | [diff] [blame] | 31 | DEFINE_int32(flush_size, 128000, |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 32 | "Number of outstanding bytes to allow before flushing to disk."); |
| 33 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 34 | namespace aos::logger { |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 36 | namespace chrono = std::chrono; |
| 37 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 38 | DetachedBufferWriter::DetachedBufferWriter( |
| 39 | std::string_view filename, std::unique_ptr<DetachedBufferEncoder> encoder) |
| 40 | : filename_(filename), encoder_(std::move(encoder)) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 41 | 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 Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | DetachedBufferWriter::~DetachedBufferWriter() { |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 56 | 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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 60 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 63 | DetachedBufferWriter::DetachedBufferWriter(DetachedBufferWriter &&other) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 64 | *this = std::move(other); |
| 65 | } |
| 66 | |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 67 | // 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 70 | DetachedBufferWriter &DetachedBufferWriter::operator=( |
| 71 | DetachedBufferWriter &&other) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 72 | std::swap(filename_, other.filename_); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 73 | std::swap(encoder_, other.encoder_); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 74 | std::swap(fd_, other.fd_); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 75 | 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 77 | std::swap(iovec_, other.iovec_); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 78 | 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 85 | return *this; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 88 | void DetachedBufferWriter::QueueSpan(absl::Span<const uint8_t> span) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 89 | 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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 97 | 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 Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 101 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 102 | // First, flush everything. |
| 103 | while (encoder_->queue_size() > 0u) { |
| 104 | Flush(); |
| 105 | } |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 106 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 107 | // 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 Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 111 | HandleWriteReturn(written, span.size()); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 112 | UpdateStatsForWrite(end - start, written, 1); |
| 113 | } else { |
| 114 | encoder_->Encode(CopySpanAsDetachedBuffer(span)); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 115 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 116 | |
| 117 | FlushAtThreshold(); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 120 | void 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 Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 139 | void DetachedBufferWriter::Flush() { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 140 | const auto queue = encoder_->queue(); |
| 141 | if (queue.empty()) { |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 142 | return; |
| 143 | } |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 144 | 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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 152 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 153 | iovec_.clear(); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 154 | const size_t iovec_size = std::min<size_t>(queue.size(), IOV_MAX); |
| 155 | iovec_.resize(iovec_size); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 156 | size_t counted_size = 0; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 157 | 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 Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 161 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 162 | |
| 163 | const auto start = aos::monotonic_clock::now(); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 164 | const ssize_t written = writev(fd_, iovec_.data(), iovec_.size()); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 165 | const auto end = aos::monotonic_clock::now(); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 166 | HandleWriteReturn(written, counted_size); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 167 | |
| 168 | encoder_->Clear(iovec_size); |
| 169 | |
| 170 | UpdateStatsForWrite(end - start, written, iovec_size); |
| 171 | } |
| 172 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 173 | void 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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 190 | void 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 | |
| 203 | void 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 Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | flatbuffers::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 Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 221 | case LogType::kLogRemoteMessage: |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 222 | data_offset = fbb->CreateVector( |
| 223 | static_cast<const uint8_t *>(context.data), context.size); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 224 | 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 Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 232 | |
| 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 Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 252 | |
| 253 | switch (log_type) { |
| 254 | case LogType::kLogMessage: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 255 | case LogType::kLogRemoteMessage: |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 256 | 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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 275 | SpanReader::SpanReader(std::string_view filename) : filename_(filename) { |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 276 | 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 Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | absl::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 Schuh | e4fca83 | 2020-03-07 16:58:53 -0800 | [diff] [blame] | 300 | 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 Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 309 | 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 Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 323 | bool SpanReader::ReadBlock() { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 324 | // 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 Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 326 | constexpr size_t kReadSize = 256 * 1024; |
| 327 | |
| 328 | // Strip off any unused data at the front. |
| 329 | if (consumed_data_ != 0) { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 330 | data_.erase_front(consumed_data_); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 331 | 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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 339 | data_.resize(starting_size + kReadSize); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 340 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 341 | const size_t count = |
| 342 | decoder_->Read(data_.begin() + starting_size, data_.end()); |
| 343 | data_.resize(starting_size + count); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 344 | if (count == 0) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 345 | return false; |
| 346 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 351 | std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader( |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 352 | std::string_view filename) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 353 | SpanReader span_reader(filename); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 354 | absl::Span<const uint8_t> config_data = span_reader.ReadMessage(); |
| 355 | |
| 356 | // Make sure something was read. |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 357 | if (config_data == absl::Span<const uint8_t>()) { |
| 358 | return std::nullopt; |
| 359 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 360 | |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 361 | // And copy the config so we have it forever, removing the size prefix. |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 362 | ResizeableBuffer data; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 363 | data.resize(config_data.size()); |
| 364 | memcpy(data.data(), config_data.begin(), data.size()); |
Austin Schuh | e09beb1 | 2020-12-11 20:04:27 -0800 | [diff] [blame] | 365 | SizePrefixedFlatbufferVector<LogFileHeader> result(std::move(data)); |
| 366 | if (!result.Verify()) { |
| 367 | return std::nullopt; |
| 368 | } |
| 369 | return result; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 372 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage( |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 373 | std::string_view filename, size_t n) { |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 374 | 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 Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 380 | if (data_span == absl::Span<const uint8_t>()) { |
| 381 | return std::nullopt; |
| 382 | } |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 385 | // And copy the config so we have it forever, removing the size prefix. |
| 386 | ResizeableBuffer data; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 387 | data.resize(data_span.size()); |
| 388 | memcpy(data.data(), data_span.begin(), data.size()); |
Austin Schuh | e09beb1 | 2020-12-11 20:04:27 -0800 | [diff] [blame] | 389 | SizePrefixedFlatbufferVector<MessageHeader> result(std::move(data)); |
| 390 | if (!result.Verify()) { |
| 391 | return std::nullopt; |
| 392 | } |
| 393 | return result; |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 396 | MessageReader::MessageReader(std::string_view filename) |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 397 | : span_reader_(filename), |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 398 | raw_log_file_header_( |
| 399 | SizePrefixedFlatbufferVector<LogFileHeader>::Empty()) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 400 | // Make sure we have enough to read the size. |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 401 | absl::Span<const uint8_t> header_data = span_reader_.ReadMessage(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 402 | |
| 403 | // Make sure something was read. |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 404 | CHECK(header_data != absl::Span<const uint8_t>()) |
| 405 | << ": Failed to read header from: " << filename; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 406 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 407 | // And copy the header data so we have it forever. |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 408 | ResizeableBuffer header_data_copy; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 409 | header_data_copy.resize(header_data.size()); |
| 410 | memcpy(header_data_copy.data(), header_data.begin(), header_data_copy.size()); |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 411 | raw_log_file_header_ = |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 412 | SizePrefixedFlatbufferVector<LogFileHeader>(std::move(header_data_copy)); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 413 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 414 | max_out_of_order_duration_ = |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 415 | chrono::nanoseconds(log_file_header()->max_out_of_order_duration()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 416 | |
| 417 | VLOG(1) << "Opened " << filename << " as node " |
| 418 | << FlatbufferToJson(log_file_header()->node()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 421 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> |
| 422 | MessageReader::ReadMessage() { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 423 | 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 Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 428 | ResizeableBuffer result_buffer; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 429 | 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 Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 432 | |
| 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 Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 437 | VLOG(2) << "Read from " << filename() << " data " << FlatbufferToJson(result); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 438 | return std::move(result); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 441 | PartsMessageReader::PartsMessageReader(LogParts log_parts) |
| 442 | : parts_(std::move(log_parts)), message_reader_(parts_.parts[0]) {} |
| 443 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 444 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 445 | PartsMessageReader::ReadMessage() { |
| 446 | while (!done_) { |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 447 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> message = |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 448 | message_reader_.ReadMessage(); |
| 449 | if (message) { |
| 450 | newest_timestamp_ = message_reader_.newest_timestamp(); |
Austin Schuh | 32f6849 | 2020-11-08 21:45:51 -0800 | [diff] [blame] | 451 | const monotonic_clock::time_point monotonic_sent_time( |
| 452 | chrono::nanoseconds(message->message().monotonic_sent_time())); |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 453 | // 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 Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 457 | if (monotonic_sent_time > |
| 458 | parts_.monotonic_start_time + max_out_of_order_duration()) { |
| 459 | after_start_ = true; |
| 460 | } |
| 461 | if (after_start_) { |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 462 | CHECK_GE(monotonic_sent_time, |
| 463 | newest_timestamp_ - max_out_of_order_duration()) |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 464 | << ": Max out of order exceeded. " << parts_ << ", start time is " |
| 465 | << parts_.monotonic_start_time << " currently reading " |
| 466 | << filename(); |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 467 | } |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 468 | return message; |
| 469 | } |
| 470 | NextLog(); |
| 471 | } |
Austin Schuh | 32f6849 | 2020-11-08 21:45:51 -0800 | [diff] [blame] | 472 | newest_timestamp_ = monotonic_clock::max_time; |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 473 | return std::nullopt; |
| 474 | } |
| 475 | |
| 476 | void 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 Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 485 | bool 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 | |
| 501 | bool Message::operator>=(const Message &m2) const { return !(*this < m2); } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 502 | bool Message::operator==(const Message &m2) const { |
| 503 | return timestamp == m2.timestamp && channel_index == m2.channel_index && |
| 504 | queue_index == m2.queue_index; |
| 505 | } |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 506 | |
| 507 | std::ostream &operator<<(std::ostream &os, const Message &m) { |
| 508 | os << "{.channel_index=" << m.channel_index |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 509 | << ", .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 | |
| 519 | std::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 | } |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 533 | if (m.monotonic_timestamp_time != monotonic_clock::min_time) { |
| 534 | os << ", .monotonic_timestamp_time=" << m.monotonic_timestamp_time; |
| 535 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 536 | if (m.data.Verify()) { |
| 537 | os << ", .data=" |
| 538 | << aos::FlatbufferToJson(m.data, |
| 539 | {.multi_line = false, .max_vector_size = 1}); |
| 540 | } |
| 541 | os << "}"; |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 542 | return os; |
| 543 | } |
| 544 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 545 | LogPartsSorter::LogPartsSorter(LogParts log_parts) |
| 546 | : parts_message_reader_(log_parts) {} |
| 547 | |
| 548 | Message *LogPartsSorter::Front() { |
| 549 | // Queue up data until enough data has been queued that the front message is |
| 550 | // sorted enough to be safe to pop. This may do nothing, so we should make |
| 551 | // sure the nothing path is checked quickly. |
| 552 | if (sorted_until() != monotonic_clock::max_time) { |
| 553 | while (true) { |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 554 | if (!messages_.empty() && messages_.begin()->timestamp < sorted_until() && |
| 555 | sorted_until() >= monotonic_start_time()) { |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 556 | break; |
| 557 | } |
| 558 | |
| 559 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> m = |
| 560 | parts_message_reader_.ReadMessage(); |
| 561 | // No data left, sorted forever, work through what is left. |
| 562 | if (!m) { |
| 563 | sorted_until_ = monotonic_clock::max_time; |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | messages_.insert( |
| 568 | {.channel_index = m.value().message().channel_index(), |
| 569 | .queue_index = m.value().message().queue_index(), |
| 570 | .timestamp = monotonic_clock::time_point(std::chrono::nanoseconds( |
| 571 | m.value().message().monotonic_sent_time())), |
| 572 | .data = std::move(m.value())}); |
| 573 | |
| 574 | // Now, update sorted_until_ to match the new message. |
| 575 | if (parts_message_reader_.newest_timestamp() > |
| 576 | monotonic_clock::min_time + |
| 577 | parts_message_reader_.max_out_of_order_duration()) { |
| 578 | sorted_until_ = parts_message_reader_.newest_timestamp() - |
| 579 | parts_message_reader_.max_out_of_order_duration(); |
| 580 | } else { |
| 581 | sorted_until_ = monotonic_clock::min_time; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // Now that we have enough data queued, return a pointer to the oldest piece |
| 587 | // of data if it exists. |
| 588 | if (messages_.empty()) { |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 589 | last_message_time_ = monotonic_clock::max_time; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 590 | return nullptr; |
| 591 | } |
| 592 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 593 | CHECK_GE(messages_.begin()->timestamp, last_message_time_) |
| 594 | << DebugString() << " reading " << parts_message_reader_.filename(); |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 595 | last_message_time_ = messages_.begin()->timestamp; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 596 | return &(*messages_.begin()); |
| 597 | } |
| 598 | |
| 599 | void LogPartsSorter::PopFront() { messages_.erase(messages_.begin()); } |
| 600 | |
| 601 | std::string LogPartsSorter::DebugString() const { |
| 602 | std::stringstream ss; |
| 603 | ss << "messages: [\n"; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 604 | int count = 0; |
| 605 | bool no_dots = true; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 606 | for (const Message &m : messages_) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 607 | if (count < 15 || count > static_cast<int>(messages_.size()) - 15) { |
| 608 | ss << m << "\n"; |
| 609 | } else if (no_dots) { |
| 610 | ss << "...\n"; |
| 611 | no_dots = false; |
| 612 | } |
| 613 | ++count; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 614 | } |
| 615 | ss << "] <- " << parts_message_reader_.filename(); |
| 616 | return ss.str(); |
| 617 | } |
| 618 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 619 | NodeMerger::NodeMerger(std::vector<LogParts> parts) { |
| 620 | CHECK_GE(parts.size(), 1u); |
| 621 | const std::string part0_node = parts[0].node; |
| 622 | for (size_t i = 1; i < parts.size(); ++i) { |
| 623 | CHECK_EQ(part0_node, parts[i].node) << ": Can't merge different nodes."; |
| 624 | } |
| 625 | for (LogParts &part : parts) { |
| 626 | parts_sorters_.emplace_back(std::move(part)); |
| 627 | } |
| 628 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 629 | node_ = configuration::GetNodeIndex(configuration(), part0_node); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 630 | |
| 631 | monotonic_start_time_ = monotonic_clock::max_time; |
| 632 | realtime_start_time_ = realtime_clock::max_time; |
| 633 | for (const LogPartsSorter &parts_sorter : parts_sorters_) { |
| 634 | if (parts_sorter.monotonic_start_time() < monotonic_start_time_) { |
| 635 | monotonic_start_time_ = parts_sorter.monotonic_start_time(); |
| 636 | realtime_start_time_ = parts_sorter.realtime_start_time(); |
| 637 | } |
| 638 | } |
| 639 | } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 640 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 641 | std::vector<const LogParts *> NodeMerger::Parts() const { |
| 642 | std::vector<const LogParts *> p; |
| 643 | p.reserve(parts_sorters_.size()); |
| 644 | for (const LogPartsSorter &parts_sorter : parts_sorters_) { |
| 645 | p.emplace_back(&parts_sorter.parts()); |
| 646 | } |
| 647 | return p; |
| 648 | } |
| 649 | |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 650 | Message *NodeMerger::Front() { |
| 651 | // Return the current Front if we have one, otherwise go compute one. |
| 652 | if (current_ != nullptr) { |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 653 | Message *result = current_->Front(); |
| 654 | CHECK_GE(result->timestamp, last_message_time_); |
| 655 | return result; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | // Otherwise, do a simple search for the oldest message, deduplicating any |
| 659 | // duplicates. |
| 660 | Message *oldest = nullptr; |
| 661 | sorted_until_ = monotonic_clock::max_time; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 662 | for (LogPartsSorter &parts_sorter : parts_sorters_) { |
| 663 | Message *m = parts_sorter.Front(); |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 664 | if (!m) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 665 | sorted_until_ = std::min(sorted_until_, parts_sorter.sorted_until()); |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 666 | continue; |
| 667 | } |
| 668 | if (oldest == nullptr || *m < *oldest) { |
| 669 | oldest = m; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 670 | current_ = &parts_sorter; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 671 | } else if (*m == *oldest) { |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 672 | // Found a duplicate. If there is a choice, we want the one which has the |
| 673 | // timestamp time. |
| 674 | if (!m->data.message().has_monotonic_timestamp_time()) { |
| 675 | parts_sorter.PopFront(); |
| 676 | } else if (!oldest->data.message().has_monotonic_timestamp_time()) { |
| 677 | current_->PopFront(); |
| 678 | current_ = &parts_sorter; |
| 679 | oldest = m; |
| 680 | } else { |
| 681 | CHECK_EQ(m->data.message().monotonic_timestamp_time(), |
| 682 | oldest->data.message().monotonic_timestamp_time()); |
| 683 | parts_sorter.PopFront(); |
| 684 | } |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | // PopFront may change this, so compute it down here. |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 688 | sorted_until_ = std::min(sorted_until_, parts_sorter.sorted_until()); |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 691 | if (oldest) { |
| 692 | CHECK_GE(oldest->timestamp, last_message_time_); |
| 693 | last_message_time_ = oldest->timestamp; |
| 694 | } else { |
| 695 | last_message_time_ = monotonic_clock::max_time; |
| 696 | } |
| 697 | |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 698 | // Return the oldest message found. This will be nullptr if nothing was |
| 699 | // found, indicating there is nothing left. |
| 700 | return oldest; |
| 701 | } |
| 702 | |
| 703 | void NodeMerger::PopFront() { |
| 704 | CHECK(current_ != nullptr) << "Popping before calling Front()"; |
| 705 | current_->PopFront(); |
| 706 | current_ = nullptr; |
| 707 | } |
| 708 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 709 | TimestampMapper::TimestampMapper(std::vector<LogParts> parts) |
| 710 | : node_merger_(std::move(parts)), |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 711 | timestamp_callback_([](TimestampedMessage *) {}) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 712 | for (const LogParts *part : node_merger_.Parts()) { |
| 713 | if (!configuration_) { |
| 714 | configuration_ = part->config; |
| 715 | } else { |
| 716 | CHECK_EQ(configuration_.get(), part->config.get()); |
| 717 | } |
| 718 | } |
| 719 | const Configuration *config = configuration_.get(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 720 | // Only fill out nodes_data_ if there are nodes. Otherwise everything gets |
| 721 | // pretty simple. |
| 722 | if (configuration::MultiNode(config)) { |
| 723 | nodes_data_.resize(config->nodes()->size()); |
| 724 | const Node *my_node = config->nodes()->Get(node()); |
| 725 | for (size_t node_index = 0; node_index < nodes_data_.size(); ++node_index) { |
| 726 | const Node *node = config->nodes()->Get(node_index); |
| 727 | NodeData *node_data = &nodes_data_[node_index]; |
| 728 | node_data->channels.resize(config->channels()->size()); |
| 729 | // We should save the channel if it is delivered to the node represented |
| 730 | // by the NodeData, but not sent by that node. That combo means it is |
| 731 | // forwarded. |
| 732 | size_t channel_index = 0; |
| 733 | node_data->any_delivered = false; |
| 734 | for (const Channel *channel : *config->channels()) { |
| 735 | node_data->channels[channel_index].delivered = |
| 736 | configuration::ChannelIsReadableOnNode(channel, node) && |
Austin Schuh | b3dbb6d | 2021-01-02 17:29:35 -0800 | [diff] [blame] | 737 | configuration::ChannelIsSendableOnNode(channel, my_node) && |
| 738 | (my_node != node); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 739 | node_data->any_delivered = node_data->any_delivered || |
| 740 | node_data->channels[channel_index].delivered; |
| 741 | ++channel_index; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | for (const Channel *channel : *config->channels()) { |
| 746 | source_node_.emplace_back(configuration::GetNodeIndex( |
| 747 | config, channel->source_node()->string_view())); |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | void TimestampMapper::AddPeer(TimestampMapper *timestamp_mapper) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 753 | CHECK(configuration::MultiNode(configuration())); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 754 | CHECK_NE(timestamp_mapper->node(), node()); |
| 755 | CHECK_LT(timestamp_mapper->node(), nodes_data_.size()); |
| 756 | |
| 757 | NodeData *node_data = &nodes_data_[timestamp_mapper->node()]; |
| 758 | // Only set it if this node delivers to the peer timestamp_mapper. Otherwise |
| 759 | // we could needlessly save data. |
| 760 | if (node_data->any_delivered) { |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 761 | VLOG(1) << "Registering on node " << node() << " for peer node " |
| 762 | << timestamp_mapper->node(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 763 | CHECK(timestamp_mapper->nodes_data_[node()].peer == nullptr); |
| 764 | |
| 765 | timestamp_mapper->nodes_data_[node()].peer = this; |
| 766 | } |
| 767 | } |
| 768 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 769 | void TimestampMapper::QueueMessage(Message *m) { |
| 770 | matched_messages_.emplace_back(TimestampedMessage{ |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 771 | .channel_index = m->channel_index, |
| 772 | .queue_index = m->queue_index, |
| 773 | .monotonic_event_time = m->timestamp, |
| 774 | .realtime_event_time = aos::realtime_clock::time_point( |
| 775 | std::chrono::nanoseconds(m->data.message().realtime_sent_time())), |
| 776 | .remote_queue_index = 0xffffffff, |
| 777 | .monotonic_remote_time = monotonic_clock::min_time, |
| 778 | .realtime_remote_time = realtime_clock::min_time, |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 779 | .monotonic_timestamp_time = monotonic_clock::min_time, |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 780 | .data = std::move(m->data)}); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | TimestampedMessage *TimestampMapper::Front() { |
| 784 | // No need to fetch anything new. A previous message still exists. |
| 785 | switch (first_message_) { |
| 786 | case FirstMessage::kNeedsUpdate: |
| 787 | break; |
| 788 | case FirstMessage::kInMessage: |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 789 | return &matched_messages_.front(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 790 | case FirstMessage::kNullptr: |
| 791 | return nullptr; |
| 792 | } |
| 793 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 794 | if (matched_messages_.empty()) { |
| 795 | if (!QueueMatched()) { |
| 796 | first_message_ = FirstMessage::kNullptr; |
| 797 | return nullptr; |
| 798 | } |
| 799 | } |
| 800 | first_message_ = FirstMessage::kInMessage; |
| 801 | return &matched_messages_.front(); |
| 802 | } |
| 803 | |
| 804 | bool TimestampMapper::QueueMatched() { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 805 | if (nodes_data_.empty()) { |
| 806 | // Simple path. We are single node, so there are no timestamps to match! |
| 807 | CHECK_EQ(messages_.size(), 0u); |
| 808 | Message *m = node_merger_.Front(); |
| 809 | if (!m) { |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 810 | return false; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 811 | } |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 812 | // Enqueue this message into matched_messages_ so we have a place to |
| 813 | // associate remote timestamps, and return it. |
| 814 | QueueMessage(m); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 815 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 816 | CHECK_GE(matched_messages_.back().monotonic_event_time, last_message_time_); |
| 817 | last_message_time_ = matched_messages_.back().monotonic_event_time; |
| 818 | |
| 819 | // We are thin wrapper around node_merger. Call it directly. |
| 820 | node_merger_.PopFront(); |
| 821 | timestamp_callback_(&matched_messages_.back()); |
| 822 | return true; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | // We need to only add messages to the list so they get processed for messages |
| 826 | // which are delivered. Reuse the flow below which uses messages_ by just |
| 827 | // adding the new message to messages_ and continuing. |
| 828 | if (messages_.empty()) { |
| 829 | if (!Queue()) { |
| 830 | // Found nothing to add, we are out of data! |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 831 | return false; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | // Now that it has been added (and cannibalized), forget about it upstream. |
| 835 | node_merger_.PopFront(); |
| 836 | } |
| 837 | |
| 838 | Message *m = &(messages_.front()); |
| 839 | |
| 840 | if (source_node_[m->channel_index] == node()) { |
| 841 | // From us, just forward it on, filling the remote data in as invalid. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 842 | QueueMessage(m); |
| 843 | CHECK_GE(matched_messages_.back().monotonic_event_time, last_message_time_); |
| 844 | last_message_time_ = matched_messages_.back().monotonic_event_time; |
| 845 | messages_.pop_front(); |
| 846 | timestamp_callback_(&matched_messages_.back()); |
| 847 | return true; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 848 | } else { |
| 849 | // Got a timestamp, find the matching remote data, match it, and return it. |
| 850 | Message data = MatchingMessageFor(*m); |
| 851 | |
| 852 | // Return the data from the remote. The local message only has timestamp |
| 853 | // info which isn't relevant anymore once extracted. |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 854 | matched_messages_.emplace_back(TimestampedMessage{ |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 855 | .channel_index = m->channel_index, |
| 856 | .queue_index = m->queue_index, |
| 857 | .monotonic_event_time = m->timestamp, |
| 858 | .realtime_event_time = aos::realtime_clock::time_point( |
| 859 | std::chrono::nanoseconds(m->data.message().realtime_sent_time())), |
| 860 | .remote_queue_index = m->data.message().remote_queue_index(), |
| 861 | .monotonic_remote_time = |
| 862 | monotonic_clock::time_point(std::chrono::nanoseconds( |
| 863 | m->data.message().monotonic_remote_time())), |
| 864 | .realtime_remote_time = realtime_clock::time_point( |
| 865 | std::chrono::nanoseconds(m->data.message().realtime_remote_time())), |
Austin Schuh | 8bf1e63 | 2021-01-02 22:41:04 -0800 | [diff] [blame] | 866 | .monotonic_timestamp_time = |
| 867 | monotonic_clock::time_point(std::chrono::nanoseconds( |
| 868 | m->data.message().monotonic_timestamp_time())), |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 869 | .data = std::move(data.data)}); |
| 870 | CHECK_GE(matched_messages_.back().monotonic_event_time, last_message_time_); |
| 871 | last_message_time_ = matched_messages_.back().monotonic_event_time; |
| 872 | // Since messages_ holds the data, drop it. |
| 873 | messages_.pop_front(); |
| 874 | timestamp_callback_(&matched_messages_.back()); |
| 875 | return true; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | void TimestampMapper::QueueUntil(monotonic_clock::time_point queue_time) { |
| 880 | while (last_message_time_ <= queue_time) { |
| 881 | if (!QueueMatched()) { |
| 882 | return; |
| 883 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
| 887 | void TimestampMapper::PopFront() { |
| 888 | CHECK(first_message_ != FirstMessage::kNeedsUpdate); |
| 889 | first_message_ = FirstMessage::kNeedsUpdate; |
| 890 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 891 | matched_messages_.pop_front(); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | Message TimestampMapper::MatchingMessageFor(const Message &message) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 895 | // Figure out what queue index we are looking for. |
| 896 | CHECK(message.data.message().has_remote_queue_index()); |
| 897 | const uint32_t remote_queue_index = |
| 898 | message.data.message().remote_queue_index(); |
| 899 | |
| 900 | CHECK(message.data.message().has_monotonic_remote_time()); |
| 901 | CHECK(message.data.message().has_realtime_remote_time()); |
| 902 | |
| 903 | const monotonic_clock::time_point monotonic_remote_time( |
| 904 | std::chrono::nanoseconds(message.data.message().monotonic_remote_time())); |
| 905 | const realtime_clock::time_point realtime_remote_time( |
| 906 | std::chrono::nanoseconds(message.data.message().realtime_remote_time())); |
| 907 | |
Austin Schuh | fecf1d8 | 2020-12-19 16:57:28 -0800 | [diff] [blame] | 908 | TimestampMapper *peer = nodes_data_[source_node_[message.channel_index]].peer; |
| 909 | |
| 910 | // We only register the peers which we have data for. So, if we are being |
| 911 | // asked to pull a timestamp from a peer which doesn't exist, return an empty |
| 912 | // message. |
| 913 | if (peer == nullptr) { |
| 914 | return Message{ |
| 915 | .channel_index = message.channel_index, |
| 916 | .queue_index = remote_queue_index, |
| 917 | .timestamp = monotonic_remote_time, |
| 918 | .data = SizePrefixedFlatbufferVector<MessageHeader>::Empty()}; |
| 919 | } |
| 920 | |
| 921 | // The queue which will have the matching data, if available. |
| 922 | std::deque<Message> *data_queue = |
| 923 | &peer->nodes_data_[node()].channels[message.channel_index].messages; |
| 924 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 925 | peer->QueueUnmatchedUntil(monotonic_remote_time); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 926 | |
| 927 | if (data_queue->empty()) { |
| 928 | return Message{ |
| 929 | .channel_index = message.channel_index, |
| 930 | .queue_index = remote_queue_index, |
| 931 | .timestamp = monotonic_remote_time, |
| 932 | .data = SizePrefixedFlatbufferVector<MessageHeader>::Empty()}; |
| 933 | } |
| 934 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 935 | if (remote_queue_index < data_queue->front().queue_index || |
| 936 | remote_queue_index > data_queue->back().queue_index) { |
| 937 | return Message{ |
| 938 | .channel_index = message.channel_index, |
| 939 | .queue_index = remote_queue_index, |
| 940 | .timestamp = monotonic_remote_time, |
| 941 | .data = SizePrefixedFlatbufferVector<MessageHeader>::Empty()}; |
| 942 | } |
| 943 | |
Austin Schuh | 993ccb5 | 2020-12-12 15:59:32 -0800 | [diff] [blame] | 944 | // The algorithm below is constant time with some assumptions. We need there |
| 945 | // to be no missing messages in the data stream. This also assumes a queue |
| 946 | // hasn't wrapped. That is conservative, but should let us get started. |
| 947 | if (data_queue->back().queue_index - data_queue->front().queue_index + 1u == |
| 948 | data_queue->size()) { |
| 949 | // Pull the data out and confirm that the timestamps match as expected. |
| 950 | Message result = std::move( |
| 951 | (*data_queue)[remote_queue_index - data_queue->front().queue_index]); |
| 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 | // Now drop the data off the front. We have deduplicated timestamps, so we |
| 960 | // are done. And all the data is in order. |
| 961 | data_queue->erase(data_queue->begin(), |
| 962 | data_queue->begin() + (1 + remote_queue_index - |
| 963 | data_queue->front().queue_index)); |
| 964 | return result; |
| 965 | } else { |
| 966 | auto it = std::find_if(data_queue->begin(), data_queue->end(), |
| 967 | [remote_queue_index](const Message &m) { |
| 968 | return m.queue_index == remote_queue_index; |
| 969 | }); |
| 970 | if (it == data_queue->end()) { |
| 971 | return Message{ |
| 972 | .channel_index = message.channel_index, |
| 973 | .queue_index = remote_queue_index, |
| 974 | .timestamp = monotonic_remote_time, |
| 975 | .data = SizePrefixedFlatbufferVector<MessageHeader>::Empty()}; |
| 976 | } |
| 977 | |
| 978 | Message result = std::move(*it); |
| 979 | |
| 980 | CHECK_EQ(result.timestamp, monotonic_remote_time) |
| 981 | << ": Queue index matches, but timestamp doesn't. Please investigate!"; |
| 982 | CHECK_EQ(realtime_clock::time_point(std::chrono::nanoseconds( |
| 983 | result.data.message().realtime_sent_time())), |
| 984 | realtime_remote_time) |
| 985 | << ": Queue index matches, but timestamp doesn't. Please investigate!"; |
| 986 | |
| 987 | data_queue->erase(it); |
| 988 | |
| 989 | return result; |
| 990 | } |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Austin Schuh | 79b3094 | 2021-01-24 22:32:21 -0800 | [diff] [blame^] | 993 | void TimestampMapper::QueueUnmatchedUntil(monotonic_clock::time_point t) { |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 994 | if (queued_until_ > t) { |
| 995 | return; |
| 996 | } |
| 997 | while (true) { |
| 998 | if (!messages_.empty() && messages_.back().timestamp > t) { |
| 999 | queued_until_ = std::max(queued_until_, messages_.back().timestamp); |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | if (!Queue()) { |
| 1004 | // Found nothing to add, we are out of data! |
| 1005 | queued_until_ = monotonic_clock::max_time; |
| 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | // Now that it has been added (and cannibalized), forget about it upstream. |
| 1010 | node_merger_.PopFront(); |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | bool TimestampMapper::Queue() { |
| 1015 | Message *m = node_merger_.Front(); |
| 1016 | if (m == nullptr) { |
| 1017 | return false; |
| 1018 | } |
| 1019 | for (NodeData &node_data : nodes_data_) { |
| 1020 | if (!node_data.any_delivered) continue; |
| 1021 | if (node_data.channels[m->channel_index].delivered) { |
| 1022 | // TODO(austin): This copies the data... Probably not worth stressing |
| 1023 | // about yet. |
| 1024 | // TODO(austin): Bound how big this can get. We tend not to send massive |
| 1025 | // data, so we can probably ignore this for a bit. |
| 1026 | node_data.channels[m->channel_index].messages.emplace_back(*m); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | messages_.emplace_back(std::move(*m)); |
| 1031 | return true; |
| 1032 | } |
| 1033 | |
| 1034 | std::string TimestampMapper::DebugString() const { |
| 1035 | std::stringstream ss; |
| 1036 | ss << "node " << node() << " [\n"; |
| 1037 | for (const Message &message : messages_) { |
| 1038 | ss << " " << message << "\n"; |
| 1039 | } |
| 1040 | ss << "] queued_until " << queued_until_; |
| 1041 | for (const NodeData &ns : nodes_data_) { |
| 1042 | if (ns.peer == nullptr) continue; |
| 1043 | ss << "\nnode " << ns.peer->node() << " remote_data [\n"; |
| 1044 | size_t channel_index = 0; |
| 1045 | for (const NodeData::ChannelData &channel_data : |
| 1046 | ns.peer->nodes_data_[node()].channels) { |
| 1047 | if (channel_data.messages.empty()) { |
| 1048 | continue; |
| 1049 | } |
Austin Schuh | b000de6 | 2020-12-03 22:00:40 -0800 | [diff] [blame] | 1050 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame] | 1051 | ss << " channel " << channel_index << " [\n"; |
| 1052 | for (const Message &m : channel_data.messages) { |
| 1053 | ss << " " << m << "\n"; |
| 1054 | } |
| 1055 | ss << " ]\n"; |
| 1056 | ++channel_index; |
| 1057 | } |
| 1058 | ss << "] queued_until " << ns.peer->queued_until_; |
| 1059 | } |
| 1060 | return ss.str(); |
| 1061 | } |
| 1062 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1063 | std::string MaybeNodeName(const Node *node) { |
| 1064 | if (node != nullptr) { |
| 1065 | return node->name()->str() + " "; |
| 1066 | } |
| 1067 | return ""; |
| 1068 | } |
| 1069 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 1070 | } // namespace aos::logger |