blob: a5ca08459ef62c3a2ccb0cdf0ef9591966318c28 [file] [log] [blame]
Austin Schuha36c8902019-12-30 18:07:15 -08001#include "aos/events/logging/logfile_utils.h"
2
3#include <fcntl.h>
4#include <limits.h>
5#include <sys/stat.h>
6#include <sys/types.h>
7#include <sys/uio.h>
8
9#include <vector>
10
Austin Schuh05b70472020-01-01 17:11:17 -080011#include "aos/configuration.h"
Austin Schuha36c8902019-12-30 18:07:15 -080012#include "aos/events/logging/logger_generated.h"
Austin Schuhfa895892020-01-07 20:07:41 -080013#include "aos/flatbuffer_merge.h"
Austin Schuha36c8902019-12-30 18:07:15 -080014#include "flatbuffers/flatbuffers.h"
Austin Schuh05b70472020-01-01 17:11:17 -080015#include "gflags/gflags.h"
16#include "glog/logging.h"
Austin Schuha36c8902019-12-30 18:07:15 -080017
18DEFINE_int32(flush_size, 1000000,
19 "Number of outstanding bytes to allow before flushing to disk.");
20
21namespace aos {
22namespace logger {
23
Austin Schuh05b70472020-01-01 17:11:17 -080024namespace chrono = std::chrono;
25
Austin Schuha36c8902019-12-30 18:07:15 -080026DetachedBufferWriter::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
32DetachedBufferWriter::~DetachedBufferWriter() {
33 Flush();
34 PLOG_IF(ERROR, close(fd_) == -1) << " Failed to close logfile";
35}
36
37void DetachedBufferWriter::QueueSizedFlatbuffer(
38 flatbuffers::FlatBufferBuilder *fbb) {
39 QueueSizedFlatbuffer(fbb->Release());
40}
41
Austin Schuhde031b72020-01-10 19:34:41 -080042void 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 Schuha36c8902019-12-30 18:07:15 -080062void 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
75void 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
100flatbuffers::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 Schuh05b70472020-01-01 17:11:17 -0800145SpanReader::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
150absl::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
176bool 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
193bool 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
227MessageReader::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
243std::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 Schuhfa895892020-01-07 20:07:41 -0800259SortedMessageReader::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 Schuh05b70472020-01-01 17:11:17 -0800267 channels_.resize(configuration()->channels()->size());
268
269 QueueMessages();
270}
271
Austin Schuhfa895892020-01-07 20:07:41 -0800272bool 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 Schuh05b70472020-01-01 17:11:17 -0800298void 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
312namespace {
313
314bool 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
328void 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
338void 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 Schuhfa895892020-01-07 20:07:41 -0800345 message_reader_->newest_timestamp() >
Austin Schuh05b70472020-01-01 17:11:17 -0800346 std::max(oldest_message().first, monotonic_start_time()) +
Austin Schuhfa895892020-01-07 20:07:41 -0800347 message_reader_->max_out_of_order_duration()) {
Austin Schuh05b70472020-01-01 17:11:17 -0800348 break;
349 }
350
351 if (std::optional<FlatbufferVector<MessageHeader>> msg =
Austin Schuhfa895892020-01-07 20:07:41 -0800352 message_reader_->ReadMessage()) {
Austin Schuh05b70472020-01-01 17:11:17 -0800353 EmplaceDataBack(std::move(msg.value()));
354 } else {
Austin Schuhfa895892020-01-07 20:07:41 -0800355 if (!NextLogFile()) {
356 break;
357 }
Austin Schuh05b70472020-01-01 17:11:17 -0800358 }
359 }
360}
361
362std::tuple<monotonic_clock::time_point, int, FlatbufferVector<MessageHeader>>
363SortedMessageReader::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 Schuhfa895892020-01-07 20:07:41 -0800386 if (oldest_channel_data.first > message_reader_->queue_data_time()) {
Austin Schuh05b70472020-01-01 17:11:17 -0800387 QueueMessages();
388 }
389
390 return std::make_tuple(oldest_channel_data.first, oldest_channel_data.second,
391 std::move(front));
392}
393
Austin Schuha36c8902019-12-30 18:07:15 -0800394} // namespace logger
395} // namespace aos