Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 1 | #include "aos/events/logging/buffer_encoder.h" |
| 2 | |
| 3 | #include <fcntl.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <sys/types.h> |
| 6 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 7 | #include "aos/flatbuffers.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 8 | #include "glog/logging.h" |
| 9 | |
| 10 | namespace aos::logger { |
| 11 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 12 | DummyEncoder::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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 25 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 26 | bool DummyEncoder::HasSpace(size_t request) const { |
| 27 | return request + input_buffer_.size() < input_buffer_.capacity(); |
| 28 | } |
| 29 | |
| 30 | void 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 Schuh | 71a40d4 | 2023-02-04 21:22:22 -0800 | [diff] [blame] | 35 | const size_t written_size = copy->Copy( |
| 36 | input_buffer_.data() + input_buffer_initial_size, 0, copy->size()); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 37 | DCHECK_EQ(written_size, copy->size()); |
| 38 | |
| 39 | total_bytes_ += written_size; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void DummyEncoder::Clear(const int n) { |
| 43 | CHECK_GE(n, 0); |
| 44 | CHECK_LE(static_cast<size_t>(n), queue_size()); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 45 | if (n != 0) { |
| 46 | input_buffer_.resize(0u); |
| 47 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 50 | absl::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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 57 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 60 | size_t DummyEncoder::queued_bytes() const { return input_buffer_.size(); } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 61 | |
| 62 | DummyDecoder::DummyDecoder(std::string_view filename) |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 63 | : filename_(filename), fd_(open(filename_.c_str(), O_RDONLY | O_CLOEXEC)) { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 64 | PCHECK(fd_ != -1) << ": Failed to open " << filename; |
| 65 | } |
| 66 | |
| 67 | DummyDecoder::~DummyDecoder() { |
| 68 | int status = close(fd_); |
| 69 | if (status != 0) { |
| 70 | PLOG(ERROR) << "DummyDecoder: Failed to close file"; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | size_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 |