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 | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 7 | #include "absl/log/check.h" |
| 8 | #include "absl/log/log.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 9 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 10 | #include "aos/flatbuffers.h" |
| 11 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 12 | namespace aos::logger { |
| 13 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 14 | DummyEncoder::DummyEncoder(size_t /*max_message_size*/, size_t buffer_size) { |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 15 | // Round up to the nearest page size. |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 16 | input_buffer_.reserve(buffer_size); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 17 | return_queue_.resize(1); |
| 18 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 19 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 20 | size_t DummyEncoder::space() const { |
| 21 | return input_buffer_.capacity() - input_buffer_.size(); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 24 | bool DummyEncoder::HasSpace(size_t request) const { return request <= space(); } |
| 25 | |
Maxwell Gumley | d26e629 | 2024-04-24 10:45:07 -0600 | [diff] [blame] | 26 | size_t DummyEncoder::Encode(Copier *copy, size_t start_byte, |
| 27 | std::chrono::nanoseconds * /*encode_duration*/) { |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 28 | const size_t input_buffer_initial_size = input_buffer_.size(); |
| 29 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 30 | size_t expected_write_size = |
| 31 | std::min(input_buffer_.capacity() - input_buffer_initial_size, |
| 32 | copy->size() - start_byte); |
| 33 | input_buffer_.resize(input_buffer_initial_size + expected_write_size); |
| 34 | const size_t written_size = |
| 35 | copy->Copy(input_buffer_.data() + input_buffer_initial_size, start_byte, |
| 36 | expected_write_size + start_byte); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 37 | |
| 38 | total_bytes_ += written_size; |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 39 | |
| 40 | return written_size; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void DummyEncoder::Clear(const int n) { |
| 44 | CHECK_GE(n, 0); |
| 45 | CHECK_LE(static_cast<size_t>(n), queue_size()); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 46 | if (n != 0) { |
| 47 | input_buffer_.resize(0u); |
| 48 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 51 | absl::Span<const absl::Span<const uint8_t>> DummyEncoder::queue() { |
| 52 | if (input_buffer_.size() != 0) { |
| 53 | return_queue_[0] = |
| 54 | absl::Span<const uint8_t>(input_buffer_.data(), input_buffer_.size()); |
| 55 | return return_queue_; |
| 56 | } else { |
| 57 | return absl::Span<const absl::Span<const uint8_t>>(); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 58 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 61 | size_t DummyEncoder::queued_bytes() const { return input_buffer_.size(); } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 62 | |
| 63 | DummyDecoder::DummyDecoder(std::string_view filename) |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 64 | : filename_(filename), fd_(open(filename_.c_str(), O_RDONLY | O_CLOEXEC)) { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 65 | PCHECK(fd_ != -1) << ": Failed to open " << filename; |
| 66 | } |
| 67 | |
| 68 | DummyDecoder::~DummyDecoder() { |
| 69 | int status = close(fd_); |
| 70 | if (status != 0) { |
| 71 | PLOG(ERROR) << "DummyDecoder: Failed to close file"; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | size_t DummyDecoder::Read(uint8_t *begin, uint8_t *end) { |
| 76 | if (end_of_file_) { |
| 77 | return 0; |
| 78 | } |
| 79 | const ssize_t count = read(fd_, begin, end - begin); |
| 80 | PCHECK(count >= 0) << ": Failed to read from file"; |
| 81 | if (count == 0) { |
| 82 | end_of_file_ = true; |
| 83 | } |
| 84 | return static_cast<size_t>(count); |
| 85 | } |
| 86 | |
| 87 | } // namespace aos::logger |