blob: f4a6251b1b45b6f83214bc7c5b1551e34c9bba88 [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
33 // Coppies a span. The span must have a longer lifetime than the coppier is
34 // 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
58 // Encodes and enqueues the given data encoder.
59 virtual void Encode(Copier *copy) = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070060
61 // If this returns true, the encoder may be bypassed by writing directly to
62 // the file.
63 virtual bool may_bypass() const { return false; }
64
65 // Finalizes the encoding process. After this, queue_size() represents the
66 // full extent of data which will be written to this file.
67 //
68 // Encode may not be called after this method.
69 virtual void Finish() = 0;
70
71 // Clears the first n encoded buffers from the queue.
72 virtual void Clear(int n) = 0;
73
Austin Schuh48d10d62022-10-16 22:19:23 -070074 // Returns a view of the queue of encoded buffers. Valid until any other
75 // method on this class is called.
76 virtual absl::Span<const absl::Span<const uint8_t>> queue() = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070077
78 // Returns the total number of of bytes currently queued up.
79 virtual size_t queued_bytes() const = 0;
80
81 // Returns the cumulative number of bytes which have been queued. This
82 // includes data which has been removed via Clear.
83 virtual size_t total_bytes() const = 0;
84
85 // Returns the number of elements in the queue.
86 virtual size_t queue_size() const = 0;
87};
88
89// This class does not encode the data. It just claims ownership of the raw data
90// and queues it up as is.
Austin Schuh48d10d62022-10-16 22:19:23 -070091class DummyEncoder final : public DataEncoder {
Brian Silvermanf51499a2020-09-21 12:49:08 -070092 public:
Austin Schuh48d10d62022-10-16 22:19:23 -070093 DummyEncoder(size_t max_buffer_size);
Austin Schuhc41603c2020-10-11 16:17:37 -070094 DummyEncoder(const DummyEncoder &) = delete;
95 DummyEncoder(DummyEncoder &&other) = delete;
96 DummyEncoder &operator=(const DummyEncoder &) = delete;
97 DummyEncoder &operator=(DummyEncoder &&other) = delete;
Brian Silvermanf51499a2020-09-21 12:49:08 -070098 ~DummyEncoder() override = default;
99
Austin Schuh48d10d62022-10-16 22:19:23 -0700100 bool HasSpace(size_t request) const final;
101 void Encode(Copier *copy) final;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700102 bool may_bypass() const final { return true; }
103 void Finish() final {}
104 void Clear(int n) final;
Austin Schuh48d10d62022-10-16 22:19:23 -0700105 absl::Span<const absl::Span<const uint8_t>> queue() final;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700106 size_t queued_bytes() const final;
107 size_t total_bytes() const final { return total_bytes_; }
Austin Schuh48d10d62022-10-16 22:19:23 -0700108 size_t queue_size() const final {
109 return input_buffer_.size() != 0 ? 1u : 0u;
110 }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700111
112 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700113 size_t total_bytes_ = 0;
Austin Schuh48d10d62022-10-16 22:19:23 -0700114
Austin Schuhbed6eeb2023-02-04 11:42:03 -0800115 // A class which uses aligned_alloc to allocate sector aligned blocks of
116 // memory.
117 class AlignedReallocator {
118 public:
119 static void *Realloc(void *old, size_t old_size, size_t new_capacity) {
120 void *new_memory = std::aligned_alloc(512, new_capacity);
121 if (old) {
122 memcpy(new_memory, old, old_size);
123 free(old);
124 }
125 return new_memory;
126 }
127 };
128
129 AllocatorResizeableBuffer<AlignedReallocator> input_buffer_;
Austin Schuh48d10d62022-10-16 22:19:23 -0700130 std::vector<absl::Span<const uint8_t>> return_queue_;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700131};
132
133// Interface to decode chunks of data. Implementations of this interface will
134// manage opening, reading, and closing the file stream.
135class DataDecoder {
136 public:
137 virtual ~DataDecoder() = default;
138
139 // Reads data into the given range. Returns the number of bytes read.
140 //
141 // Returns less than end-begin if all bytes have been read. Otherwise, this
142 // will always fill the whole range.
143 virtual size_t Read(uint8_t *begin, uint8_t *end) = 0;
Tyler Chatow2015bc62021-08-04 21:15:09 -0700144
145 // Returns the underlying filename, for debugging purposes.
146 virtual std::string_view filename() const = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700147};
148
149// Simply reads the contents of the file into the target buffer.
150class DummyDecoder final : public DataDecoder {
151 public:
152 explicit DummyDecoder(std::string_view filename);
Austin Schuhc41603c2020-10-11 16:17:37 -0700153 DummyDecoder(const DummyDecoder &) = delete;
154 DummyDecoder(DummyDecoder &&other) = delete;
155 DummyDecoder &operator=(const DummyDecoder &) = delete;
156 DummyDecoder &operator=(DummyDecoder &&other) = delete;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700157 ~DummyDecoder() override;
158
159 size_t Read(uint8_t *begin, uint8_t *end) final;
Tyler Chatow2015bc62021-08-04 21:15:09 -0700160 std::string_view filename() const final { return filename_; }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700161
162 private:
Tyler Chatow2015bc62021-08-04 21:15:09 -0700163 const std::string filename_;
164
Brian Silvermanf51499a2020-09-21 12:49:08 -0700165 // File descriptor for the log file.
166 int fd_;
167
168 // Cached bit for if we have reached the end of the file. Otherwise we will
169 // hammer on the kernel asking for more data each time we send.
170 bool end_of_file_ = false;
171};
172
173} // namespace aos::logger
174
175#endif // AOS_EVENTS_LOGGING_BUFFER_ENCODER_H_