blob: a155bb550e0ec4962366ad036e0aff30fc6522b4 [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"
Tyler Chatow2015bc62021-08-04 21:15:09 -07005#include "flatbuffers/flatbuffers.h"
Austin Schuh48d10d62022-10-16 22:19:23 -07006#include "glog/logging.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -07007
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include "aos/containers/resizeable_buffer.h"
9#include "aos/events/logging/logger_generated.h"
10
Brian Silvermanf51499a2020-09-21 12:49:08 -070011namespace aos::logger {
12
Austin Schuh48d10d62022-10-16 22:19:23 -070013// Interface to encode data as it is written to a file.
14class DataEncoder {
Brian Silvermanf51499a2020-09-21 12:49:08 -070015 public:
Austin Schuh48d10d62022-10-16 22:19:23 -070016 virtual ~DataEncoder() = default;
Brian Silvermanf51499a2020-09-21 12:49:08 -070017
Austin Schuhd9336bc2024-04-29 18:41:23 -070018 // Size of an aligned sector used to detect when the data is aligned enough to
19 // use O_DIRECT instead.
20 static constexpr size_t kSector = 512u;
21
Austin Schuh48d10d62022-10-16 22:19:23 -070022 // Interface to copy data into a buffer.
23 class Copier {
24 public:
25 Copier(size_t size) : size_(size) {}
26
27 // Returns the data this will write.
28 size_t size() const { return size_; }
29
30 // Writes size() bytes to data, and returns the data written.
Austin Schuh71a40d42023-02-04 21:22:22 -080031 [[nodiscard]] virtual size_t Copy(uint8_t *data, size_t start_byte,
32 size_t end_byte) = 0;
Austin Schuh48d10d62022-10-16 22:19:23 -070033
34 private:
35 size_t size_;
36 };
37
Austin Schuh8bdfc492023-02-11 12:53:13 -080038 // Copies a span. The span must have a longer lifetime than the coppier is
Austin Schuh48d10d62022-10-16 22:19:23 -070039 // being used.
40 class SpanCopier : public Copier {
41 public:
42 SpanCopier(absl::Span<const uint8_t> data)
43 : Copier(data.size()), data_(data) {
Austin Schuh71a40d42023-02-04 21:22:22 -080044 CHECK(data_.data() != nullptr);
Austin Schuh48d10d62022-10-16 22:19:23 -070045 }
46
Austin Schuh71a40d42023-02-04 21:22:22 -080047 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
48 DCHECK_LE(start_byte, end_byte);
49 DCHECK_LE(end_byte, data_.size());
50
51 std::memcpy(data, data_.data() + start_byte, end_byte - start_byte);
52 return end_byte - start_byte;
Austin Schuh48d10d62022-10-16 22:19:23 -070053 }
54
55 private:
56 const absl::Span<const uint8_t> data_;
57 };
58
59 // Returns true if there is space in the buffer for the next request, or if
60 // the output needs to be flushed.
61 virtual bool HasSpace(size_t request) const = 0;
62
Austin Schuh8bdfc492023-02-11 12:53:13 -080063 // Returns the space available.
64 virtual size_t space() const = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070065
Austin Schuh8bdfc492023-02-11 12:53:13 -080066 // Encodes and enqueues the given data encoder. Starts at the start byte
67 // (which must be a multiple of 8 bytes), and goes as far as it can. Returns
68 // the amount encoded.
69 virtual size_t Encode(Copier *copy, size_t start_byte) = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070070
71 // Finalizes the encoding process. After this, queue_size() represents the
72 // full extent of data which will be written to this file.
73 //
74 // Encode may not be called after this method.
75 virtual void Finish() = 0;
76
77 // Clears the first n encoded buffers from the queue.
78 virtual void Clear(int n) = 0;
79
Austin Schuh48d10d62022-10-16 22:19:23 -070080 // Returns a view of the queue of encoded buffers. Valid until any other
81 // method on this class is called.
82 virtual absl::Span<const absl::Span<const uint8_t>> queue() = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070083
84 // Returns the total number of of bytes currently queued up.
85 virtual size_t queued_bytes() const = 0;
86
87 // Returns the cumulative number of bytes which have been queued. This
88 // includes data which has been removed via Clear.
89 virtual size_t total_bytes() const = 0;
90
91 // Returns the number of elements in the queue.
92 virtual size_t queue_size() const = 0;
93};
94
95// This class does not encode the data. It just claims ownership of the raw data
96// and queues it up as is.
Austin Schuh48d10d62022-10-16 22:19:23 -070097class DummyEncoder final : public DataEncoder {
Brian Silvermanf51499a2020-09-21 12:49:08 -070098 public:
Austin Schuh8bdfc492023-02-11 12:53:13 -080099 DummyEncoder(size_t max_message_size, size_t buffer_size = 128 * 1024);
Austin Schuhc41603c2020-10-11 16:17:37 -0700100 DummyEncoder(const DummyEncoder &) = delete;
101 DummyEncoder(DummyEncoder &&other) = delete;
102 DummyEncoder &operator=(const DummyEncoder &) = delete;
103 DummyEncoder &operator=(DummyEncoder &&other) = delete;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700104 ~DummyEncoder() override = default;
105
Austin Schuh48d10d62022-10-16 22:19:23 -0700106 bool HasSpace(size_t request) const final;
Austin Schuh8bdfc492023-02-11 12:53:13 -0800107 size_t space() const final;
108 size_t Encode(Copier *copy, size_t start_byte) final;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700109 void Finish() final {}
110 void Clear(int n) final;
Austin Schuh48d10d62022-10-16 22:19:23 -0700111 absl::Span<const absl::Span<const uint8_t>> queue() final;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700112 size_t queued_bytes() const final;
113 size_t total_bytes() const final { return total_bytes_; }
Austin Schuh48d10d62022-10-16 22:19:23 -0700114 size_t queue_size() const final {
115 return input_buffer_.size() != 0 ? 1u : 0u;
116 }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700117
118 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700119 size_t total_bytes_ = 0;
Austin Schuh48d10d62022-10-16 22:19:23 -0700120
Austin Schuhd9336bc2024-04-29 18:41:23 -0700121 AllocatorResizeableBuffer<aos::AlignedReallocator<kSector>> input_buffer_;
Austin Schuh48d10d62022-10-16 22:19:23 -0700122 std::vector<absl::Span<const uint8_t>> return_queue_;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700123};
124
125// Interface to decode chunks of data. Implementations of this interface will
126// manage opening, reading, and closing the file stream.
127class DataDecoder {
128 public:
129 virtual ~DataDecoder() = default;
130
131 // Reads data into the given range. Returns the number of bytes read.
132 //
133 // Returns less than end-begin if all bytes have been read. Otherwise, this
134 // will always fill the whole range.
135 virtual size_t Read(uint8_t *begin, uint8_t *end) = 0;
Tyler Chatow2015bc62021-08-04 21:15:09 -0700136
137 // Returns the underlying filename, for debugging purposes.
138 virtual std::string_view filename() const = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700139};
140
141// Simply reads the contents of the file into the target buffer.
142class DummyDecoder final : public DataDecoder {
143 public:
144 explicit DummyDecoder(std::string_view filename);
Austin Schuhc41603c2020-10-11 16:17:37 -0700145 DummyDecoder(const DummyDecoder &) = delete;
146 DummyDecoder(DummyDecoder &&other) = delete;
147 DummyDecoder &operator=(const DummyDecoder &) = delete;
148 DummyDecoder &operator=(DummyDecoder &&other) = delete;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700149 ~DummyDecoder() override;
150
151 size_t Read(uint8_t *begin, uint8_t *end) final;
Tyler Chatow2015bc62021-08-04 21:15:09 -0700152 std::string_view filename() const final { return filename_; }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700153
154 private:
Tyler Chatow2015bc62021-08-04 21:15:09 -0700155 const std::string filename_;
156
Brian Silvermanf51499a2020-09-21 12:49:08 -0700157 // File descriptor for the log file.
158 int fd_;
159
160 // Cached bit for if we have reached the end of the file. Otherwise we will
161 // hammer on the kernel asking for more data each time we send.
162 bool end_of_file_ = false;
163};
164
165} // namespace aos::logger
166
167#endif // AOS_EVENTS_LOGGING_BUFFER_ENCODER_H_