blob: b3cea6c88c7e1bd10329c10bb60ba42cb1bcddf1 [file] [log] [blame]
Alexei Strots01395492023-03-20 13:59:56 -07001#ifndef AOS_EVENTS_LOGGING_LOG_BACKEND_H_
2#define AOS_EVENTS_LOGGING_LOG_BACKEND_H_
3
4#include <fcntl.h>
5#include <sys/types.h>
6#include <sys/uio.h>
7
8#include <memory>
9#include <string>
10#include <vector>
11
12#include "absl/types/span.h"
13#include "aos/time/time.h"
14
15namespace aos::logger {
16
17class WriteStats {
18 public:
19 // The maximum time for a single write call, or 0 if none have been performed.
20 std::chrono::nanoseconds max_write_time() const { return max_write_time_; }
21 // The number of bytes in the longest write call, or -1 if none have been
22 // performed.
23 int max_write_time_bytes() const { return max_write_time_bytes_; }
24 // The number of buffers in the longest write call, or -1 if none have been
25 // performed.
26 int max_write_time_messages() const { return max_write_time_messages_; }
27 // The total time spent in write calls.
28 std::chrono::nanoseconds total_write_time() const {
29 return total_write_time_;
30 }
31 // The total number of writes which have been performed.
32 int total_write_count() const { return total_write_count_; }
33 // The total number of messages which have been written.
34 int total_write_messages() const { return total_write_messages_; }
35 // The total number of bytes which have been written.
36 int total_write_bytes() const { return total_write_bytes_; }
37
38 void ResetStats() {
39 max_write_time_ = std::chrono::nanoseconds::zero();
40 max_write_time_bytes_ = -1;
41 max_write_time_messages_ = -1;
42 total_write_time_ = std::chrono::nanoseconds::zero();
43 total_write_count_ = 0;
44 total_write_messages_ = 0;
45 total_write_bytes_ = 0;
46 }
47
48 void UpdateStats(aos::monotonic_clock::duration duration, ssize_t written,
49 int iovec_size) {
50 if (duration > max_write_time_) {
51 max_write_time_ = duration;
52 max_write_time_bytes_ = written;
53 max_write_time_messages_ = iovec_size;
54 }
55 total_write_time_ += duration;
56 ++total_write_count_;
57 total_write_messages_ += iovec_size;
58 total_write_bytes_ += written;
59 }
60
61 private:
62 std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero();
63 int max_write_time_bytes_ = -1;
64 int max_write_time_messages_ = -1;
65 std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero();
66 int total_write_count_ = 0;
67 int total_write_messages_ = 0;
68 int total_write_bytes_ = 0;
69};
70
71// Currently, all write operations only cares about out-of-space error. This is
72// a simple representation of write result.
73enum class WriteCode { kOk, kOutOfSpace };
74
75struct WriteResult {
76 WriteCode code = WriteCode::kOk;
77 size_t messages_written = 0;
78};
79
Alexei Strotsa0b99d72023-04-11 15:12:42 -070080// Source for iovec with additional flag that pointer and size of data is
81// aligned and be ready for O_DIRECT operation.
82struct AlignedIovec {
83 const uint8_t *data;
84 size_t size;
85 bool aligned;
86
87 AlignedIovec(const uint8_t *data, size_t size, bool aligned)
88 : data(data), size(size), aligned(aligned) {}
89};
90
91// Converts queue of pieces to write to the disk to the queue where every
92// element is either aligned for O_DIRECT operation or marked as not aligned.
93class QueueAligner {
94 public:
95 QueueAligner();
96
97 // Reads input queue and fills with aligned and unaligned pieces. It is easy
98 // to deal with smaller pieces and batch it during the write operation.
99 void FillAlignedQueue(
100 const absl::Span<const absl::Span<const uint8_t>> &queue);
101
102 const std::vector<AlignedIovec> &aligned_queue() const {
103 return aligned_queue_;
104 }
105
106 private:
107 std::vector<AlignedIovec> aligned_queue_;
108};
109
Alexei Strots01395492023-03-20 13:59:56 -0700110// FileHandler is a replacement for bare filename in log writing and reading
111// operations.
112//
113// There are a couple over-arching constraints on writing to keep track of.
114// 1) The kernel is both faster and more efficient at writing large, aligned
115// chunks with O_DIRECT set on the file. The alignment needed is specified
116// by kSector and is file system dependent.
117// 2) Not all encoders support generating round multiples of kSector of data.
118// Rather than burden the API for detecting when that is the case, we want
119// DetachedBufferWriter to be as efficient as it can at writing what given.
120// 3) Some files are small and not updated frequently. They need to be
121// flushed or we will lose data on power off. It is most efficient to write
122// as much as we can aligned by kSector and then fall back to the non direct
123// method when it has been flushed.
124// 4) Not all filesystems support O_DIRECT, and different sizes may be optimal
125// for different machines. The defaults should work decently anywhere and
126// be tunable for faster systems.
Alexei Strots01395492023-03-20 13:59:56 -0700127class FileHandler {
128 public:
129 // Size of an aligned sector used to detect when the data is aligned enough to
130 // use O_DIRECT instead.
131 static constexpr size_t kSector = 512u;
132
133 explicit FileHandler(std::string filename);
134 virtual ~FileHandler();
135
136 FileHandler(const FileHandler &) = delete;
137 FileHandler &operator=(const FileHandler &) = delete;
138
139 // Try to open file. App will crash if there are other than out-of-space
140 // problems with backend media.
141 virtual WriteCode OpenForWrite();
142
143 // Close the file handler.
144 virtual WriteCode Close();
145
146 // This will be true until Close() is called, unless the file couldn't be
147 // created due to running out of space.
148 bool is_open() const { return fd_ != -1; }
149
150 // Peeks messages from queue and writes it to file. Returns code when
151 // out-of-space problem occurred along with number of messages from queue that
152 // was written.
Austin Schuh3ebaf782023-04-07 16:03:28 -0700153 //
154 // The spans can be aligned or not, and can have any lengths. This code will
155 // write faster if the spans passed in start at aligned addresses, and are
156 // multiples of kSector long (and the data written so far is also a multiple
157 // of kSector length).
Alexei Strots01395492023-03-20 13:59:56 -0700158 virtual WriteResult Write(
159 const absl::Span<const absl::Span<const uint8_t>> &queue);
160
161 // TODO (Alexei): it is rather leaked abstraction.
162 // Path to the concrete log file.
163 std::string_view filename() const { return filename_; }
164
165 int fd() const { return fd_; }
166
167 // Get access to statistics related to the write operations.
168 WriteStats *WriteStatistics() { return &write_stats_; }
169
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700170 // Number of bytes written in aligned mode. It is mostly for testing.
171 size_t written_aligned() const { return written_aligned_; }
172
Alexei Strots01395492023-03-20 13:59:56 -0700173 private:
174 // Enables O_DIRECT on the open file if it is supported. Cheap to call if it
175 // is already enabled.
176 void EnableDirect();
177 // Disables O_DIRECT on the open file if it is supported. Cheap to call if it
178 // is already disabld.
179 void DisableDirect();
180
181 bool ODirectEnabled() const { return !!(flags_ & O_DIRECT); }
182
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700183 // Writes a chunk of iovecs. aligned is true if all the data is kSector byte
184 // aligned and multiples of it in length.
185 WriteCode WriteV(const std::vector<struct iovec> &iovec, bool aligned);
Alexei Strots01395492023-03-20 13:59:56 -0700186
187 const std::string filename_;
188
189 int fd_ = -1;
190
191 // List of iovecs to use with writev. This is a member variable to avoid
192 // churn.
193 std::vector<struct iovec> iovec_;
194
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700195 QueueAligner queue_aligner_;
196
Alexei Strots01395492023-03-20 13:59:56 -0700197 int total_write_bytes_ = 0;
198 int last_synced_bytes_ = 0;
199
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700200 size_t written_aligned_ = 0;
201
Alexei Strots01395492023-03-20 13:59:56 -0700202 bool supports_odirect_ = true;
203 int flags_ = 0;
204
205 WriteStats write_stats_;
206};
207
208// Class that decouples log writing and media (file system or memory). It is
209// handy to use for tests.
210class LogBackend {
211 public:
212 virtual ~LogBackend() = default;
213
214 // Request file-like object from the log backend. It maybe a file on a disk or
215 // in memory. id is usually generated by log namer and looks like name of the
216 // file within a log folder.
217 virtual std::unique_ptr<FileHandler> RequestFile(std::string_view id) = 0;
218};
219
220// Implements requests log files from file system.
221class FileBackend : public LogBackend {
222 public:
223 // base_name is the path to the folder where log files are.
224 explicit FileBackend(std::string_view base_name);
225 ~FileBackend() override = default;
226
227 // Request file from a file system. It is not open yet.
228 std::unique_ptr<FileHandler> RequestFile(std::string_view id) override;
229
230 private:
231 const std::string base_name_;
232 const std::string_view separator_;
233};
234
235// Provides a file backend that supports renaming of the base log folder and
236// temporary files.
237class RenamableFileBackend : public LogBackend {
238 public:
239 // Adds call to rename, when closed.
240 class RenamableFileHandler final : public FileHandler {
241 public:
242 RenamableFileHandler(RenamableFileBackend *owner, std::string filename)
243 : FileHandler(std::move(filename)), owner_(owner) {}
244 ~RenamableFileHandler() final = default;
245
Alexei Strotscaf17d32023-04-03 22:31:11 -0700246 // Closes and if needed renames file.
Alexei Strots01395492023-03-20 13:59:56 -0700247 WriteCode Close() final;
248
249 private:
250 RenamableFileBackend *owner_;
251 };
252
253 explicit RenamableFileBackend(std::string_view base_name);
254 ~RenamableFileBackend() = default;
255
256 // Request file from a file system. It is not open yet.
257 std::unique_ptr<FileHandler> RequestFile(std::string_view id) override;
258
259 // TODO (Alexei): it is called by Logger, and left here for compatibility.
260 // Logger should not call it.
Alexei Strotscaf17d32023-04-03 22:31:11 -0700261 std::string_view base_name() const { return base_name_; }
Alexei Strots01395492023-03-20 13:59:56 -0700262
263 // If temp files are enabled, then this will write files with the .tmp
264 // suffix, and then rename them to the desired name after they are fully
265 // written.
266 //
267 // This is useful to enable incremental copying of the log files.
268 //
269 // Defaults to writing directly to the final filename.
270 void EnableTempFiles();
271
272 // Moves the current log location to the new name. Returns true if a change
273 // was made, false otherwise.
274 // Only renaming the folder is supported, not the file base name.
275 bool RenameLogBase(std::string_view new_base_name);
276
277 private:
278 // This function called after file closed, to adjust file names in case of
279 // base name was changed or temp files enabled.
280 WriteCode RenameFileAfterClose(std::string_view filename);
281
282 std::string base_name_;
283 std::string_view separator_;
284
285 bool use_temp_files_ = false;
286 std::string_view temp_suffix_;
287
288 std::string old_base_name_;
289};
290
291} // namespace aos::logger
292
293#endif // AOS_EVENTS_LOGGING_LOG_BACKEND_H_