blob: ed5bef63419f687e2222257b2e4f69f35bd2d074 [file] [log] [blame]
Brian Silvermanf51499a2020-09-21 12:49:08 -07001#ifndef AOS_EVENTS_LOGGING_BUFFER_ENCODER_H_
2#define AOS_EVENTS_LOGGING_BUFFER_ENCODER_H_
3
4#include "absl/types/span.h"
Austin Schuh48d10d62022-10-16 22:19:23 -07005#include "aos/containers/resizeable_buffer.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -07006#include "aos/events/logging/logger_generated.h"
Tyler Chatow2015bc62021-08-04 21:15:09 -07007#include "flatbuffers/flatbuffers.h"
Austin Schuh48d10d62022-10-16 22:19:23 -07008#include "glog/logging.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -07009
10namespace aos::logger {
11
Austin Schuh48d10d62022-10-16 22:19:23 -070012// Interface to encode data as it is written to a file.
13class DataEncoder {
Brian Silvermanf51499a2020-09-21 12:49:08 -070014 public:
Austin Schuh48d10d62022-10-16 22:19:23 -070015 virtual ~DataEncoder() = default;
Brian Silvermanf51499a2020-09-21 12:49:08 -070016
Austin Schuh48d10d62022-10-16 22:19:23 -070017 // Interface to copy data into a buffer.
18 class Copier {
19 public:
20 Copier(size_t size) : size_(size) {}
21
22 // Returns the data this will write.
23 size_t size() const { return size_; }
24
25 // Writes size() bytes to data, and returns the data written.
Austin Schuh71a40d42023-02-04 21:22:22 -080026 [[nodiscard]] virtual size_t Copy(uint8_t *data, size_t start_byte,
27 size_t end_byte) = 0;
Austin Schuh48d10d62022-10-16 22:19:23 -070028
29 private:
30 size_t size_;
31 };
32
Austin Schuh8bdfc492023-02-11 12:53:13 -080033 // Copies a span. The span must have a longer lifetime than the coppier is
Austin Schuh48d10d62022-10-16 22:19:23 -070034 // being used.
35 class SpanCopier : public Copier {
36 public:
37 SpanCopier(absl::Span<const uint8_t> data)
38 : Copier(data.size()), data_(data) {
Austin Schuh71a40d42023-02-04 21:22:22 -080039 CHECK(data_.data() != nullptr);
Austin Schuh48d10d62022-10-16 22:19:23 -070040 }
41
Austin Schuh71a40d42023-02-04 21:22:22 -080042 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
43 DCHECK_LE(start_byte, end_byte);
44 DCHECK_LE(end_byte, data_.size());
45
46 std::memcpy(data, data_.data() + start_byte, end_byte - start_byte);
47 return end_byte - start_byte;
Austin Schuh48d10d62022-10-16 22:19:23 -070048 }
49
50 private:
51 const absl::Span<const uint8_t> data_;
52 };
53
54 // Returns true if there is space in the buffer for the next request, or if
55 // the output needs to be flushed.
56 virtual bool HasSpace(size_t request) const = 0;
57
Austin Schuh8bdfc492023-02-11 12:53:13 -080058 // Returns the space available.
59 virtual size_t space() const = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070060
Austin Schuh8bdfc492023-02-11 12:53:13 -080061 // Encodes and enqueues the given data encoder. Starts at the start byte
62 // (which must be a multiple of 8 bytes), and goes as far as it can. Returns
63 // the amount encoded.
64 virtual size_t Encode(Copier *copy, size_t start_byte) = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070065
66 // Finalizes the encoding process. After this, queue_size() represents the
67 // full extent of data which will be written to this file.
68 //
69 // Encode may not be called after this method.
70 virtual void Finish() = 0;
71
72 // Clears the first n encoded buffers from the queue.
73 virtual void Clear(int n) = 0;
74
Austin Schuh48d10d62022-10-16 22:19:23 -070075 // Returns a view of the queue of encoded buffers. Valid until any other
76 // method on this class is called.
77 virtual absl::Span<const absl::Span<const uint8_t>> queue() = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070078
79 // Returns the total number of of bytes currently queued up.
80 virtual size_t queued_bytes() const = 0;
81
82 // Returns the cumulative number of bytes which have been queued. This
83 // includes data which has been removed via Clear.
84 virtual size_t total_bytes() const = 0;
85
86 // Returns the number of elements in the queue.
87 virtual size_t queue_size() const = 0;
88};
89
90// This class does not encode the data. It just claims ownership of the raw data
91// and queues it up as is.
Austin Schuh48d10d62022-10-16 22:19:23 -070092class DummyEncoder final : public DataEncoder {
Brian Silvermanf51499a2020-09-21 12:49:08 -070093 public:
Austin Schuh8bdfc492023-02-11 12:53:13 -080094 DummyEncoder(size_t max_message_size, size_t buffer_size = 128 * 1024);
Austin Schuhc41603c2020-10-11 16:17:37 -070095 DummyEncoder(const DummyEncoder &) = delete;
96 DummyEncoder(DummyEncoder &&other) = delete;
97 DummyEncoder &operator=(const DummyEncoder &) = delete;
98 DummyEncoder &operator=(DummyEncoder &&other) = delete;
Brian Silvermanf51499a2020-09-21 12:49:08 -070099 ~DummyEncoder() override = default;
100
Austin Schuh48d10d62022-10-16 22:19:23 -0700101 bool HasSpace(size_t request) const final;
Austin Schuh8bdfc492023-02-11 12:53:13 -0800102 size_t space() const final;
103 size_t Encode(Copier *copy, size_t start_byte) final;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700104 void Finish() final {}
105 void Clear(int n) final;
Austin Schuh48d10d62022-10-16 22:19:23 -0700106 absl::Span<const absl::Span<const uint8_t>> queue() final;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700107 size_t queued_bytes() const final;
108 size_t total_bytes() const final { return total_bytes_; }
Austin Schuh48d10d62022-10-16 22:19:23 -0700109 size_t queue_size() const final {
110 return input_buffer_.size() != 0 ? 1u : 0u;
111 }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700112
113 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700114 size_t total_bytes_ = 0;
Austin Schuh48d10d62022-10-16 22:19:23 -0700115
Austin Schuhbed6eeb2023-02-04 11:42:03 -0800116 // A class which uses aligned_alloc to allocate sector aligned blocks of
117 // memory.
118 class AlignedReallocator {
119 public:
120 static void *Realloc(void *old, size_t old_size, size_t new_capacity) {
121 void *new_memory = std::aligned_alloc(512, new_capacity);
122 if (old) {
123 memcpy(new_memory, old, old_size);
124 free(old);
125 }
126 return new_memory;
127 }
128 };
129
130 AllocatorResizeableBuffer<AlignedReallocator> input_buffer_;
Austin Schuh48d10d62022-10-16 22:19:23 -0700131 std::vector<absl::Span<const uint8_t>> return_queue_;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700132};
133
134// Interface to decode chunks of data. Implementations of this interface will
135// manage opening, reading, and closing the file stream.
136class DataDecoder {
137 public:
138 virtual ~DataDecoder() = default;
139
140 // Reads data into the given range. Returns the number of bytes read.
141 //
142 // Returns less than end-begin if all bytes have been read. Otherwise, this
143 // will always fill the whole range.
144 virtual size_t Read(uint8_t *begin, uint8_t *end) = 0;
Tyler Chatow2015bc62021-08-04 21:15:09 -0700145
146 // Returns the underlying filename, for debugging purposes.
147 virtual std::string_view filename() const = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700148};
149
150// Simply reads the contents of the file into the target buffer.
151class DummyDecoder final : public DataDecoder {
152 public:
153 explicit DummyDecoder(std::string_view filename);
Austin Schuhc41603c2020-10-11 16:17:37 -0700154 DummyDecoder(const DummyDecoder &) = delete;
155 DummyDecoder(DummyDecoder &&other) = delete;
156 DummyDecoder &operator=(const DummyDecoder &) = delete;
157 DummyDecoder &operator=(DummyDecoder &&other) = delete;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700158 ~DummyDecoder() override;
159
160 size_t Read(uint8_t *begin, uint8_t *end) final;
Tyler Chatow2015bc62021-08-04 21:15:09 -0700161 std::string_view filename() const final { return filename_; }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700162
163 private:
Tyler Chatow2015bc62021-08-04 21:15:09 -0700164 const std::string filename_;
165
Brian Silvermanf51499a2020-09-21 12:49:08 -0700166 // File descriptor for the log file.
167 int fd_;
168
169 // Cached bit for if we have reached the end of the file. Otherwise we will
170 // hammer on the kernel asking for more data each time we send.
171 bool end_of_file_ = false;
172};
173
174} // namespace aos::logger
175
176#endif // AOS_EVENTS_LOGGING_BUFFER_ENCODER_H_