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