blob: 4b3ccf3d66bd542901e817b862e00616ffd4e6ae [file] [log] [blame]
Brian Silvermanf51499a2020-09-21 12:49:08 -07001#include "aos/events/logging/buffer_encoder.h"
2
3#include <fcntl.h>
4#include <sys/stat.h>
5#include <sys/types.h>
6
Austin Schuh48d10d62022-10-16 22:19:23 -07007#include "aos/flatbuffers.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -07008#include "glog/logging.h"
9
10namespace aos::logger {
11
Austin Schuh48d10d62022-10-16 22:19:23 -070012DummyEncoder::DummyEncoder(size_t max_buffer_size) {
13 // TODO(austin): This is going to end up writing > 128k chunks, not 128k
14 // chunks exactly. If we really want to, we could make it always write 128k
15 // chunks by only exposing n * 128k chunks as we go. This might improve write
16 // performance, then again, it might have no effect if the kernel is combining
17 // writes...
18 constexpr size_t kWritePageSize = 128 * 1024;
19 // Round up to the nearest page size.
20 input_buffer_.reserve(
21 ((max_buffer_size + kWritePageSize - 1) / kWritePageSize) *
22 kWritePageSize);
23 return_queue_.resize(1);
24}
Brian Silvermanf51499a2020-09-21 12:49:08 -070025
Austin Schuh48d10d62022-10-16 22:19:23 -070026bool DummyEncoder::HasSpace(size_t request) const {
27 return request + input_buffer_.size() < input_buffer_.capacity();
28}
29
30void DummyEncoder::Encode(Copier *copy) {
31 DCHECK(HasSpace(copy->size()));
32 const size_t input_buffer_initial_size = input_buffer_.size();
33
34 input_buffer_.resize(input_buffer_initial_size + copy->size());
Austin Schuh71a40d42023-02-04 21:22:22 -080035 const size_t written_size = copy->Copy(
36 input_buffer_.data() + input_buffer_initial_size, 0, copy->size());
Austin Schuh48d10d62022-10-16 22:19:23 -070037 DCHECK_EQ(written_size, copy->size());
38
39 total_bytes_ += written_size;
Brian Silvermanf51499a2020-09-21 12:49:08 -070040}
41
42void DummyEncoder::Clear(const int n) {
43 CHECK_GE(n, 0);
44 CHECK_LE(static_cast<size_t>(n), queue_size());
Austin Schuh48d10d62022-10-16 22:19:23 -070045 if (n != 0) {
46 input_buffer_.resize(0u);
47 }
Brian Silvermanf51499a2020-09-21 12:49:08 -070048}
49
Austin Schuh48d10d62022-10-16 22:19:23 -070050absl::Span<const absl::Span<const uint8_t>> DummyEncoder::queue() {
51 if (input_buffer_.size() != 0) {
52 return_queue_[0] =
53 absl::Span<const uint8_t>(input_buffer_.data(), input_buffer_.size());
54 return return_queue_;
55 } else {
56 return absl::Span<const absl::Span<const uint8_t>>();
Brian Silvermanf51499a2020-09-21 12:49:08 -070057 }
Brian Silvermanf51499a2020-09-21 12:49:08 -070058}
59
Austin Schuh48d10d62022-10-16 22:19:23 -070060size_t DummyEncoder::queued_bytes() const { return input_buffer_.size(); }
Brian Silvermanf51499a2020-09-21 12:49:08 -070061
62DummyDecoder::DummyDecoder(std::string_view filename)
Tyler Chatow2015bc62021-08-04 21:15:09 -070063 : filename_(filename), fd_(open(filename_.c_str(), O_RDONLY | O_CLOEXEC)) {
Brian Silvermanf51499a2020-09-21 12:49:08 -070064 PCHECK(fd_ != -1) << ": Failed to open " << filename;
65}
66
67DummyDecoder::~DummyDecoder() {
68 int status = close(fd_);
69 if (status != 0) {
70 PLOG(ERROR) << "DummyDecoder: Failed to close file";
71 }
72}
73
74size_t DummyDecoder::Read(uint8_t *begin, uint8_t *end) {
75 if (end_of_file_) {
76 return 0;
77 }
78 const ssize_t count = read(fd_, begin, end - begin);
79 PCHECK(count >= 0) << ": Failed to read from file";
80 if (count == 0) {
81 end_of_file_ = true;
82 }
83 return static_cast<size_t>(count);
84}
85
86} // namespace aos::logger