Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 1 | #include "aos/events/logging/logfile_utils.h" |
| 2 | |
| 3 | #include <fcntl.h> |
| 4 | #include <limits.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/uio.h> |
| 8 | |
| 9 | #include <vector> |
| 10 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 11 | #include "aos/configuration.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 12 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 13 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 14 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 15 | #include "gflags/gflags.h" |
| 16 | #include "glog/logging.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 17 | |
| 18 | DEFINE_int32(flush_size, 1000000, |
| 19 | "Number of outstanding bytes to allow before flushing to disk."); |
| 20 | |
| 21 | namespace aos { |
| 22 | namespace logger { |
| 23 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 24 | namespace chrono = std::chrono; |
| 25 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 26 | DetachedBufferWriter::DetachedBufferWriter(std::string_view filename) |
| 27 | : fd_(open(std::string(filename).c_str(), |
| 28 | O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 0774)) { |
| 29 | PCHECK(fd_ != -1) << ": Failed to open " << filename; |
| 30 | } |
| 31 | |
| 32 | DetachedBufferWriter::~DetachedBufferWriter() { |
| 33 | Flush(); |
| 34 | PLOG_IF(ERROR, close(fd_) == -1) << " Failed to close logfile"; |
| 35 | } |
| 36 | |
| 37 | void DetachedBufferWriter::QueueSizedFlatbuffer( |
| 38 | flatbuffers::FlatBufferBuilder *fbb) { |
| 39 | QueueSizedFlatbuffer(fbb->Release()); |
| 40 | } |
| 41 | |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 42 | void DetachedBufferWriter::WriteSizedFlatbuffer( |
| 43 | absl::Span<const uint8_t> span) { |
| 44 | // Cheat aggressively... Write out the queued up data, and then write this |
| 45 | // data once without buffering. It is hard to make a DetachedBuffer out of |
| 46 | // this data, and we don't want to worry about lifetimes. |
| 47 | Flush(); |
| 48 | iovec_.clear(); |
| 49 | iovec_.reserve(1); |
| 50 | |
| 51 | struct iovec n; |
| 52 | n.iov_base = const_cast<uint8_t *>(span.data()); |
| 53 | n.iov_len = span.size(); |
| 54 | iovec_.emplace_back(n); |
| 55 | |
| 56 | const ssize_t written = writev(fd_, iovec_.data(), iovec_.size()); |
| 57 | |
| 58 | PCHECK(written == static_cast<ssize_t>(n.iov_len)) |
| 59 | << ": Wrote " << written << " expected " << n.iov_len; |
| 60 | } |
| 61 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 62 | void DetachedBufferWriter::QueueSizedFlatbuffer( |
| 63 | flatbuffers::DetachedBuffer &&buffer) { |
| 64 | queued_size_ += buffer.size(); |
| 65 | queue_.emplace_back(std::move(buffer)); |
| 66 | |
| 67 | // Flush if we are at the max number of iovs per writev, or have written |
| 68 | // enough data. Otherwise writev will fail with an invalid argument. |
| 69 | if (queued_size_ > static_cast<size_t>(FLAGS_flush_size) || |
| 70 | queue_.size() == IOV_MAX) { |
| 71 | Flush(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void DetachedBufferWriter::Flush() { |
| 76 | if (queue_.size() == 0u) { |
| 77 | return; |
| 78 | } |
| 79 | iovec_.clear(); |
| 80 | iovec_.reserve(queue_.size()); |
| 81 | size_t counted_size = 0; |
| 82 | for (size_t i = 0; i < queue_.size(); ++i) { |
| 83 | struct iovec n; |
| 84 | n.iov_base = queue_[i].data(); |
| 85 | n.iov_len = queue_[i].size(); |
| 86 | counted_size += n.iov_len; |
| 87 | iovec_.emplace_back(std::move(n)); |
| 88 | } |
| 89 | CHECK_EQ(counted_size, queued_size_); |
| 90 | const ssize_t written = writev(fd_, iovec_.data(), iovec_.size()); |
| 91 | |
| 92 | PCHECK(written == static_cast<ssize_t>(queued_size_)) |
| 93 | << ": Wrote " << written << " expected " << queued_size_; |
| 94 | |
| 95 | queued_size_ = 0; |
| 96 | queue_.clear(); |
| 97 | // TODO(austin): Handle partial writes in some way other than crashing... |
| 98 | } |
| 99 | |
| 100 | flatbuffers::Offset<MessageHeader> PackMessage( |
| 101 | flatbuffers::FlatBufferBuilder *fbb, const Context &context, |
| 102 | int channel_index, LogType log_type) { |
| 103 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset; |
| 104 | |
| 105 | switch (log_type) { |
| 106 | case LogType::kLogMessage: |
| 107 | case LogType::kLogMessageAndDeliveryTime: |
| 108 | data_offset = |
| 109 | fbb->CreateVector(static_cast<uint8_t *>(context.data), context.size); |
| 110 | break; |
| 111 | |
| 112 | case LogType::kLogDeliveryTimeOnly: |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | MessageHeader::Builder message_header_builder(*fbb); |
| 117 | message_header_builder.add_channel_index(channel_index); |
| 118 | message_header_builder.add_queue_index(context.queue_index); |
| 119 | message_header_builder.add_monotonic_sent_time( |
| 120 | context.monotonic_event_time.time_since_epoch().count()); |
| 121 | message_header_builder.add_realtime_sent_time( |
| 122 | context.realtime_event_time.time_since_epoch().count()); |
| 123 | |
| 124 | switch (log_type) { |
| 125 | case LogType::kLogMessage: |
| 126 | message_header_builder.add_data(data_offset); |
| 127 | break; |
| 128 | |
| 129 | case LogType::kLogMessageAndDeliveryTime: |
| 130 | message_header_builder.add_data(data_offset); |
| 131 | [[fallthrough]]; |
| 132 | |
| 133 | case LogType::kLogDeliveryTimeOnly: |
| 134 | message_header_builder.add_monotonic_remote_time( |
| 135 | context.monotonic_remote_time.time_since_epoch().count()); |
| 136 | message_header_builder.add_realtime_remote_time( |
| 137 | context.realtime_remote_time.time_since_epoch().count()); |
| 138 | message_header_builder.add_remote_queue_index(context.remote_queue_index); |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | return message_header_builder.Finish(); |
| 143 | } |
| 144 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 145 | SpanReader::SpanReader(std::string_view filename) |
| 146 | : fd_(open(std::string(filename).c_str(), O_RDONLY | O_CLOEXEC)) { |
| 147 | PCHECK(fd_ != -1) << ": Failed to open " << filename; |
| 148 | } |
| 149 | |
| 150 | absl::Span<const uint8_t> SpanReader::ReadMessage() { |
| 151 | // Make sure we have enough for the size. |
| 152 | if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) { |
| 153 | if (!ReadBlock()) { |
| 154 | return absl::Span<const uint8_t>(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Now make sure we have enough for the message. |
| 159 | const size_t data_size = |
| 160 | flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) + |
| 161 | sizeof(flatbuffers::uoffset_t); |
| 162 | while (data_.size() < consumed_data_ + data_size) { |
| 163 | if (!ReadBlock()) { |
| 164 | return absl::Span<const uint8_t>(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // And return it, consuming the data. |
| 169 | const uint8_t *data_ptr = data_.data() + consumed_data_; |
| 170 | |
| 171 | consumed_data_ += data_size; |
| 172 | |
| 173 | return absl::Span<const uint8_t>(data_ptr, data_size); |
| 174 | } |
| 175 | |
| 176 | bool SpanReader::MessageAvailable() { |
| 177 | // Are we big enough to read the size? |
| 178 | if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | // Then, are we big enough to read the full message? |
| 183 | const size_t data_size = |
| 184 | flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) + |
| 185 | sizeof(flatbuffers::uoffset_t); |
| 186 | if (data_.size() < consumed_data_ + data_size) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | bool SpanReader::ReadBlock() { |
| 194 | if (end_of_file_) { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | // Appends 256k. This is enough that the read call is efficient. We don't |
| 199 | // want to spend too much time reading small chunks because the syscalls for |
| 200 | // that will be expensive. |
| 201 | constexpr size_t kReadSize = 256 * 1024; |
| 202 | |
| 203 | // Strip off any unused data at the front. |
| 204 | if (consumed_data_ != 0) { |
| 205 | data_.erase(data_.begin(), data_.begin() + consumed_data_); |
| 206 | consumed_data_ = 0; |
| 207 | } |
| 208 | |
| 209 | const size_t starting_size = data_.size(); |
| 210 | |
| 211 | // This should automatically grow the backing store. It won't shrink if we |
| 212 | // get a small chunk later. This reduces allocations when we want to append |
| 213 | // more data. |
| 214 | data_.resize(data_.size() + kReadSize); |
| 215 | |
| 216 | ssize_t count = read(fd_, &data_[starting_size], kReadSize); |
| 217 | data_.resize(starting_size + std::max(count, static_cast<ssize_t>(0))); |
| 218 | if (count == 0) { |
| 219 | end_of_file_ = true; |
| 220 | return false; |
| 221 | } |
| 222 | PCHECK(count > 0); |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | MessageReader::MessageReader(std::string_view filename) |
| 228 | : span_reader_(filename) { |
| 229 | // Make sure we have enough to read the size. |
| 230 | absl::Span<const uint8_t> config_data = span_reader_.ReadMessage(); |
| 231 | |
| 232 | // Make sure something was read. |
| 233 | CHECK(config_data != absl::Span<const uint8_t>()); |
| 234 | |
| 235 | // And copy the config so we have it forever. |
| 236 | configuration_ = std::vector<uint8_t>(config_data.begin(), config_data.end()); |
| 237 | |
| 238 | max_out_of_order_duration_ = std::chrono::nanoseconds( |
| 239 | flatbuffers::GetSizePrefixedRoot<LogFileHeader>(configuration_.data()) |
| 240 | ->max_out_of_order_duration()); |
| 241 | } |
| 242 | |
| 243 | std::optional<FlatbufferVector<MessageHeader>> MessageReader::ReadMessage() { |
| 244 | absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage(); |
| 245 | if (msg_data == absl::Span<const uint8_t>()) { |
| 246 | return std::nullopt; |
| 247 | } |
| 248 | |
| 249 | FlatbufferVector<MessageHeader> result{std::vector<uint8_t>( |
| 250 | msg_data.begin() + sizeof(flatbuffers::uoffset_t), msg_data.end())}; |
| 251 | |
| 252 | const monotonic_clock::time_point timestamp = monotonic_clock::time_point( |
| 253 | chrono::nanoseconds(result.message().monotonic_sent_time())); |
| 254 | |
| 255 | newest_timestamp_ = std::max(newest_timestamp_, timestamp); |
| 256 | return result; |
| 257 | } |
| 258 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 259 | SortedMessageReader::SortedMessageReader( |
| 260 | const std::vector<std::string> &filenames) |
| 261 | : filenames_(filenames), |
| 262 | log_file_header_(FlatbufferDetachedBuffer<LogFileHeader>::Empty()) { |
| 263 | CHECK(NextLogFile()) << ": filenames is empty. Need files to read."; |
| 264 | |
| 265 | log_file_header_ = CopyFlatBuffer(message_reader_->log_file_header()); |
| 266 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 267 | channels_.resize(configuration()->channels()->size()); |
| 268 | |
| 269 | QueueMessages(); |
| 270 | } |
| 271 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 272 | bool SortedMessageReader::NextLogFile() { |
| 273 | if (next_filename_index_ == filenames_.size()) { |
| 274 | return false; |
| 275 | } |
| 276 | message_reader_ = |
| 277 | std::make_unique<MessageReader>(filenames_[next_filename_index_]); |
| 278 | |
| 279 | // We can't support the config diverging between two log file headers. See if |
| 280 | // they are the same. |
| 281 | if (next_filename_index_ != 0) { |
| 282 | // Since we copied before, we need to copy again to guarantee that things |
| 283 | // didn't get re-ordered. |
| 284 | const FlatbufferDetachedBuffer<LogFileHeader> new_log_file_header = |
| 285 | CopyFlatBuffer(message_reader_->log_file_header()); |
| 286 | CHECK_EQ(new_log_file_header.size(), log_file_header_.size()); |
| 287 | CHECK(memcmp(new_log_file_header.data(), log_file_header_.data(), |
| 288 | log_file_header_.size()) == 0) |
| 289 | << ": Header is different between log file chunks " |
| 290 | << filenames_[next_filename_index_] << " and " |
| 291 | << filenames_[next_filename_index_ - 1] << ", this is not supported."; |
| 292 | } |
| 293 | |
| 294 | ++next_filename_index_; |
| 295 | return true; |
| 296 | } |
| 297 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 298 | void SortedMessageReader::EmplaceDataBack( |
| 299 | FlatbufferVector<MessageHeader> &&new_data) { |
| 300 | const monotonic_clock::time_point timestamp = monotonic_clock::time_point( |
| 301 | chrono::nanoseconds(new_data.message().monotonic_sent_time())); |
| 302 | const size_t channel_index = new_data.message().channel_index(); |
| 303 | CHECK_LT(channel_index, channels_.size()); |
| 304 | |
| 305 | if (channels_[channel_index].data.size() == 0) { |
| 306 | channels_[channel_index].oldest_timestamp = timestamp; |
| 307 | PushChannelHeap(timestamp, channel_index); |
| 308 | } |
| 309 | channels_[channel_index].data.emplace_back(std::move(new_data)); |
| 310 | } |
| 311 | |
| 312 | namespace { |
| 313 | |
| 314 | bool ChannelHeapCompare( |
| 315 | const std::pair<monotonic_clock::time_point, int> first, |
| 316 | const std::pair<monotonic_clock::time_point, int> second) { |
| 317 | if (first.first > second.first) { |
| 318 | return true; |
| 319 | } else if (first.first == second.first) { |
| 320 | return first.second > second.second; |
| 321 | } else { |
| 322 | return false; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | } // namespace |
| 327 | |
| 328 | void SortedMessageReader::PushChannelHeap(monotonic_clock::time_point timestamp, |
| 329 | int channel_index) { |
| 330 | channel_heap_.push_back(std::make_pair(timestamp, channel_index)); |
| 331 | |
| 332 | // The default sort puts the newest message first. Use a custom comparator to |
| 333 | // put the oldest message first. |
| 334 | std::push_heap(channel_heap_.begin(), channel_heap_.end(), |
| 335 | ChannelHeapCompare); |
| 336 | } |
| 337 | |
| 338 | void SortedMessageReader::QueueMessages() { |
| 339 | while (true) { |
| 340 | // Don't queue if we have enough data already. |
| 341 | // When a log file starts, there should be a message from each channel. |
| 342 | // Those messages might be very old. Make sure to read a chunk past the |
| 343 | // starting time. |
| 344 | if (channel_heap_.size() > 0 && |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 345 | message_reader_->newest_timestamp() > |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 346 | std::max(oldest_message().first, monotonic_start_time()) + |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 347 | message_reader_->max_out_of_order_duration()) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 348 | break; |
| 349 | } |
| 350 | |
| 351 | if (std::optional<FlatbufferVector<MessageHeader>> msg = |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 352 | message_reader_->ReadMessage()) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 353 | EmplaceDataBack(std::move(msg.value())); |
| 354 | } else { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 355 | if (!NextLogFile()) { |
| 356 | break; |
| 357 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | std::tuple<monotonic_clock::time_point, int, FlatbufferVector<MessageHeader>> |
| 363 | SortedMessageReader::PopOldestChannel() { |
| 364 | std::pair<monotonic_clock::time_point, int> oldest_channel_data = |
| 365 | channel_heap_.front(); |
| 366 | std::pop_heap(channel_heap_.begin(), channel_heap_.end(), |
| 367 | &ChannelHeapCompare); |
| 368 | channel_heap_.pop_back(); |
| 369 | |
| 370 | struct ChannelData &channel = channels_[oldest_channel_data.second]; |
| 371 | |
| 372 | FlatbufferVector<MessageHeader> front = std::move(channel.front()); |
| 373 | |
| 374 | channel.data.pop_front(); |
| 375 | |
| 376 | // Re-push it and update the oldest timestamp. |
| 377 | if (channel.data.size() != 0) { |
| 378 | const monotonic_clock::time_point timestamp = monotonic_clock::time_point( |
| 379 | chrono::nanoseconds(channel.front().message().monotonic_sent_time())); |
| 380 | PushChannelHeap(timestamp, oldest_channel_data.second); |
| 381 | channel.oldest_timestamp = timestamp; |
| 382 | } else { |
| 383 | channel.oldest_timestamp = monotonic_clock::min_time; |
| 384 | } |
| 385 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 386 | if (oldest_channel_data.first > message_reader_->queue_data_time()) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 387 | QueueMessages(); |
| 388 | } |
| 389 | |
| 390 | return std::make_tuple(oldest_channel_data.first, oldest_channel_data.second, |
| 391 | std::move(front)); |
| 392 | } |
| 393 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 394 | } // namespace logger |
| 395 | } // namespace aos |