blob: 30f6181f8d6f8f4f1eeb6c775cd15720aba518ac [file] [log] [blame]
Alexei Strots01395492023-03-20 13:59:56 -07001#include "aos/events/logging/log_backend.h"
2
3#include <dirent.h>
4
5#include <filesystem>
6
Alexei Strotsf08b8fb2023-04-21 19:46:08 -07007#include "absl/strings/match.h"
Alexei Strots01395492023-03-20 13:59:56 -07008#include "absl/strings/str_cat.h"
Alexei Strots01395492023-03-20 13:59:56 -07009#include "glog/logging.h"
10
Alexei Strotsf08b8fb2023-04-21 19:46:08 -070011#include "aos/events/logging/file_operations.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070012#include "aos/util/file.h"
13
Alexei Strots01395492023-03-20 13:59:56 -070014DEFINE_bool(direct, false,
15 "If true, write using O_DIRECT and write 512 byte aligned blocks "
16 "whenever possible.");
Austin Schuh3ebaf782023-04-07 16:03:28 -070017DEFINE_bool(
18 sync, false,
19 "If true, sync data to disk as we go so we don't get too far ahead. Also "
20 "fadvise that we are done with the memory once it hits disk.");
Alexei Strots01395492023-03-20 13:59:56 -070021
Alexei Strotsa0b99d72023-04-11 15:12:42 -070022DEFINE_uint32(queue_reserve, 32, "Pre-reserved size of write queue.");
23
Alexei Strots01395492023-03-20 13:59:56 -070024namespace aos::logger {
25namespace {
26constexpr const char *kTempExtension = ".tmp";
Alexei Strotsa0b99d72023-04-11 15:12:42 -070027
28// Assuming that kSector is power of 2, it aligns address to the left size.
29inline size_t AlignToLeft(size_t value) {
30 return value & (~(FileHandler::kSector - 1));
31}
32
33inline bool IsAligned(size_t value) {
34 return value % FileHandler::kSector == 0;
35}
36
37inline bool IsAlignedStart(const absl::Span<const uint8_t> span) {
38 return (reinterpret_cast<size_t>(span.data()) % FileHandler::kSector) == 0;
39}
40
41inline bool IsAlignedLength(const absl::Span<const uint8_t> span) {
42 return (span.size() % FileHandler::kSector) == 0;
43}
44
45} // namespace
46
47logger::QueueAligner::QueueAligner() {
48 aligned_queue_.reserve(FLAGS_queue_reserve);
49}
50
51void logger::QueueAligner::FillAlignedQueue(
52 const absl::Span<const absl::Span<const uint8_t>> &queue) {
53 aligned_queue_.clear();
54
Austin Schuh790ec9c2023-05-03 13:47:15 -070055 size_t queue_index = 0;
Alexei Strotsa0b99d72023-04-11 15:12:42 -070056 for (const auto &span : queue) {
Austin Schuh790ec9c2023-05-03 13:47:15 -070057 ++queue_index;
Alexei Strotsa0b99d72023-04-11 15:12:42 -070058 // Generally, every span might have 3 optional parts (i.e. 2^3 cases):
59 // 1. unaligned prefix - from start till first aligned block.
60 // 2. aligned main - block with aligned start and size
61 // 3. unaligned suffix - block with aligned start, and size less than one
62 // sector. If size of the span is less than 1 sector, let's call it prefix.
63
64 auto *data = span.data();
65 size_t size = span.size();
66 const auto start = reinterpret_cast<size_t>(data);
67 VLOG(2) << "Consider span starting at " << std::hex << start
68 << " with size " << size;
69
Austin Schuh790ec9c2023-05-03 13:47:15 -070070 CHECK_GT(size, 0u)
71 << ": Nobody should be sending empty messages. Queue index "
72 << (queue_index - 1) << " out of " << queue.size();
Alexei Strotsa0b99d72023-04-11 15:12:42 -070073
74 const auto next_aligned =
75 IsAligned(start) ? start : AlignToLeft(start) + FileHandler::kSector;
76 const auto prefix_size = next_aligned - start;
77 VLOG(2) << "Calculated prefix size " << std::hex << prefix_size;
78
79 if (prefix_size >= size) {
80 // size of prefix >= size of span - alignment is not possible, accept the
81 // whole span
82 VLOG(2) << "Only prefix found";
83 CHECK_GT(size, 0u);
84 aligned_queue_.emplace_back(data, size, false);
85 continue;
86 }
87 CHECK_LT(prefix_size, FileHandler::kSector)
88 << ": Wrong calculation of 'next' aligned position";
89 if (prefix_size > 0) {
90 // Cut the prefix and move to the main part.
91 VLOG(2) << "Cutting prefix at " << std::hex << start << " of size "
92 << prefix_size;
93 aligned_queue_.emplace_back(data, prefix_size, false);
94 data += prefix_size;
95 size -= prefix_size;
96 CHECK(data <= span.data() + span.size()) << " :Boundaries after prefix";
97 }
98
99 if (IsAligned(size)) {
100 // the rest is aligned.
101 VLOG(2) << "Returning aligned main part";
102 CHECK_GT(size, 0u);
103 aligned_queue_.emplace_back(data, size, true);
104 continue;
105 }
106
107 const auto aligned_size = AlignToLeft(size);
108 CHECK(aligned_size < size) << ": Wrong calculation of 'main' size";
109 if (aligned_size > 0) {
110 VLOG(2) << "Cutting main part starting " << std::hex
111 << reinterpret_cast<size_t>(data) << " of size " << aligned_size;
112 aligned_queue_.emplace_back(data, aligned_size, true);
113
114 data += aligned_size;
115 size -= aligned_size;
116 CHECK(data <= span.data() + span.size()) << " :Boundaries after main";
117 }
118
119 VLOG(2) << "Cutting suffix part starting " << std::hex
120 << reinterpret_cast<size_t>(data) << " of size " << size;
121 CHECK_GT(size, 0u);
122 aligned_queue_.emplace_back(data, size, false);
123 }
Alexei Strots01395492023-03-20 13:59:56 -0700124}
125
126FileHandler::FileHandler(std::string filename)
127 : filename_(std::move(filename)), supports_odirect_(FLAGS_direct) {}
128
129FileHandler::~FileHandler() { Close(); }
130
131WriteCode FileHandler::OpenForWrite() {
132 iovec_.reserve(10);
133 if (!aos::util::MkdirPIfSpace(filename_, 0777)) {
134 return WriteCode::kOutOfSpace;
135 } else {
136 fd_ = open(filename_.c_str(), O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 0774);
137 if (fd_ == -1 && errno == ENOSPC) {
138 return WriteCode::kOutOfSpace;
139 } else {
140 PCHECK(fd_ != -1) << ": Failed to open " << filename_ << " for writing";
141 VLOG(1) << "Opened " << filename_ << " for writing";
142 }
143
144 flags_ = fcntl(fd_, F_GETFL, 0);
Alexei Strotsbc082d82023-05-03 08:43:42 -0700145 PCHECK(flags_ >= 0) << ": Failed to get flags for " << filename_;
Alexei Strots01395492023-03-20 13:59:56 -0700146
147 EnableDirect();
148
149 CHECK(std::filesystem::exists(filename_));
150
151 return WriteCode::kOk;
152 }
153}
154
155void FileHandler::EnableDirect() {
156 if (supports_odirect_ && !ODirectEnabled()) {
157 const int new_flags = flags_ | O_DIRECT;
158 // Track if we failed to set O_DIRECT. Note: Austin hasn't seen this call
159 // fail. The write call tends to fail instead.
160 if (fcntl(fd_, F_SETFL, new_flags) == -1) {
Alexei Strotsbc082d82023-05-03 08:43:42 -0700161 PLOG(WARNING) << "Failed to set O_DIRECT on " << filename_;
Alexei Strots01395492023-03-20 13:59:56 -0700162 supports_odirect_ = false;
163 } else {
Alexei Strotsbc082d82023-05-03 08:43:42 -0700164 VLOG(1) << "Enabled O_DIRECT on " << filename_;
Alexei Strots01395492023-03-20 13:59:56 -0700165 flags_ = new_flags;
166 }
167 }
168}
169
170void FileHandler::DisableDirect() {
171 if (supports_odirect_ && ODirectEnabled()) {
172 flags_ = flags_ & (~O_DIRECT);
173 PCHECK(fcntl(fd_, F_SETFL, flags_) != -1) << ": Failed to disable O_DIRECT";
Alexei Strotsbc082d82023-05-03 08:43:42 -0700174 VLOG(1) << "Disabled O_DIRECT on " << filename_;
Alexei Strots01395492023-03-20 13:59:56 -0700175 }
176}
177
178WriteResult FileHandler::Write(
179 const absl::Span<const absl::Span<const uint8_t>> &queue) {
180 iovec_.clear();
Austin Schuh3ebaf782023-04-07 16:03:28 -0700181 CHECK_LE(queue.size(), static_cast<size_t>(IOV_MAX));
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700182
183 queue_aligner_.FillAlignedQueue(queue);
184 CHECK_LE(queue_aligner_.aligned_queue().size(), static_cast<size_t>(IOV_MAX));
Alexei Strots01395492023-03-20 13:59:56 -0700185
186 // Ok, we now need to figure out if we were aligned, and if we were, how much
187 // of the data we are being asked to write is aligned.
188 //
Austin Schuh3ebaf782023-04-07 16:03:28 -0700189 // When writing with O_DIRECT, the kernel only will accept writes where the
190 // offset into the file is a multiple of kSector, the data is aligned to
191 // kSector in memory, and the length being written is a multiple of kSector.
192 // Some of the callers use an aligned ResizeableBuffer to generate 512 byte
193 // aligned buffers for this code to find and use.
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700194 bool was_aligned = IsAligned(total_write_bytes_);
195 VLOG(1) << "Started " << (was_aligned ? "aligned" : "unaligned")
196 << " at offset " << total_write_bytes_ << " on " << filename();
Austin Schuh9e8df9e2023-05-03 08:28:29 -0700197
Alexei Strotscaf17d32023-04-03 22:31:11 -0700198 // Walk through aligned queue and batch writes based on aligned flag
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700199 for (const auto &item : queue_aligner_.aligned_queue()) {
200 if (was_aligned != item.aligned) {
201 // Switching aligned context. Let's flush current batch.
202 if (!iovec_.empty()) {
203 // Flush current queue if we need.
204 const auto code = WriteV(iovec_, was_aligned);
Austin Schuh3ebaf782023-04-07 16:03:28 -0700205 if (code == WriteCode::kOutOfSpace) {
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700206 // We cannot say anything about what number of messages was written
207 // for sure.
Austin Schuh3ebaf782023-04-07 16:03:28 -0700208 return {
209 .code = code,
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700210 .messages_written = queue.size(),
Austin Schuh3ebaf782023-04-07 16:03:28 -0700211 };
212 }
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700213 iovec_.clear();
Austin Schuh3ebaf782023-04-07 16:03:28 -0700214 }
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700215 // Write queue is flushed. WriteV updates the total_write_bytes_.
216 was_aligned = IsAligned(total_write_bytes_) && item.aligned;
Alexei Strots01395492023-03-20 13:59:56 -0700217 }
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700218 iovec_.push_back(
219 {.iov_base = const_cast<uint8_t *>(item.data), .iov_len = item.size});
Alexei Strots01395492023-03-20 13:59:56 -0700220 }
221
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700222 WriteCode result_code = WriteCode::kOk;
223 if (!iovec_.empty()) {
224 // Flush current queue if we need.
225 result_code = WriteV(iovec_, was_aligned);
226 }
Alexei Strots01395492023-03-20 13:59:56 -0700227 return {
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700228 .code = result_code,
Austin Schuh3ebaf782023-04-07 16:03:28 -0700229 .messages_written = queue.size(),
Alexei Strots01395492023-03-20 13:59:56 -0700230 };
231}
232
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700233WriteCode FileHandler::WriteV(const std::vector<struct iovec> &iovec,
234 bool aligned) {
Alexei Strots01395492023-03-20 13:59:56 -0700235 // Configure the file descriptor to match the mode we should be in. This is
236 // safe to over-call since it only does the syscall if needed.
237 if (aligned) {
238 EnableDirect();
239 } else {
240 DisableDirect();
241 }
242
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700243 VLOG(2) << "Flushing queue of " << iovec.size() << " elements, "
244 << (aligned ? "aligned" : "unaligned");
245
246 CHECK_GT(iovec.size(), 0u);
Alexei Strots01395492023-03-20 13:59:56 -0700247 const auto start = aos::monotonic_clock::now();
Alexei Strots01395492023-03-20 13:59:56 -0700248
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700249 // Validation of alignment assumptions.
Austin Schuh9e8df9e2023-05-03 08:28:29 -0700250 if (aligned) {
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700251 CHECK(IsAligned(total_write_bytes_))
Austin Schuh9e8df9e2023-05-03 08:28:29 -0700252 << ": Failed after writing " << total_write_bytes_
253 << " to the file, attempting aligned write with unaligned start.";
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700254
255 for (const auto &iovec_item : iovec) {
Austin Schuh9e8df9e2023-05-03 08:28:29 -0700256 absl::Span<const uint8_t> data(
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700257 reinterpret_cast<const uint8_t *>(iovec_item.iov_base),
258 iovec_item.iov_len);
259 VLOG(2) << " iov_base " << static_cast<void *>(iovec_item.iov_base)
260 << ", iov_len " << iovec_item.iov_len;
Austin Schuh9e8df9e2023-05-03 08:28:29 -0700261 CHECK(IsAlignedStart(data) && IsAlignedLength(data));
Austin Schuh9e8df9e2023-05-03 08:28:29 -0700262 }
263 }
264
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700265 // Calculation of expected written size.
266 size_t counted_size = 0;
267 for (const auto &iovec_item : iovec) {
268 CHECK_GT(iovec_item.iov_len, 0u);
269 counted_size += iovec_item.iov_len;
Austin Schuh3ebaf782023-04-07 16:03:28 -0700270 }
271
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700272 VLOG(2) << "Going to write " << counted_size;
273 CHECK_GT(counted_size, 0u);
274
275 const ssize_t written = writev(fd_, iovec.data(), iovec.size());
276 VLOG(2) << "Wrote " << written << ", for iovec size " << iovec.size();
277
278 const auto end = aos::monotonic_clock::now();
279 if (written == -1 && errno == ENOSPC) {
280 return WriteCode::kOutOfSpace;
281 }
282 PCHECK(written >= 0) << ": write failed, got " << written;
283 if (written < static_cast<ssize_t>(counted_size)) {
284 // Sometimes this happens instead of ENOSPC. On a real filesystem, this
285 // never seems to happen in any other case. If we ever want to log to a
286 // socket, this will happen more often. However, until we get there, we'll
287 // just assume it means we ran out of space.
288 return WriteCode::kOutOfSpace;
289 }
Austin Schuh3ebaf782023-04-07 16:03:28 -0700290
Austin Schuh9e8df9e2023-05-03 08:28:29 -0700291 if (FLAGS_sync) {
Alexei Strots01395492023-03-20 13:59:56 -0700292 // Flush asynchronously and force the data out of the cache.
293 sync_file_range(fd_, total_write_bytes_, written, SYNC_FILE_RANGE_WRITE);
294 if (last_synced_bytes_ != 0) {
295 // Per Linus' recommendation online on how to do fast file IO, do a
296 // blocking flush of the previous write chunk, and then tell the kernel to
297 // drop the pages from the cache. This makes sure we can't get too far
298 // ahead.
299 sync_file_range(fd_, last_synced_bytes_,
300 total_write_bytes_ - last_synced_bytes_,
301 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE |
302 SYNC_FILE_RANGE_WAIT_AFTER);
303 posix_fadvise(fd_, last_synced_bytes_,
304 total_write_bytes_ - last_synced_bytes_,
305 POSIX_FADV_DONTNEED);
Alexei Strots01395492023-03-20 13:59:56 -0700306 }
Austin Schuh3c4029f2023-04-13 12:09:55 -0700307 last_synced_bytes_ = total_write_bytes_;
Alexei Strots01395492023-03-20 13:59:56 -0700308 }
309
Alexei Strots01395492023-03-20 13:59:56 -0700310 total_write_bytes_ += written;
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700311 if (aligned) {
312 written_aligned_ += written;
313 }
Alexei Strotsbc082d82023-05-03 08:43:42 -0700314 WriteStatistics()->UpdateStats(end - start, written, iovec.size());
Alexei Strots01395492023-03-20 13:59:56 -0700315 return WriteCode::kOk;
316}
317
318WriteCode FileHandler::Close() {
319 if (!is_open()) {
320 return WriteCode::kOk;
321 }
322 bool ran_out_of_space = false;
323 if (close(fd_) == -1) {
324 if (errno == ENOSPC) {
325 ran_out_of_space = true;
326 } else {
327 PLOG(ERROR) << "Closing log file failed";
328 }
329 }
330 fd_ = -1;
331 VLOG(1) << "Closed " << filename_;
332 return ran_out_of_space ? WriteCode::kOutOfSpace : WriteCode::kOk;
333}
334
335FileBackend::FileBackend(std::string_view base_name)
336 : base_name_(base_name), separator_(base_name_.back() == '/' ? "" : "_") {}
337
Alexei Strotsbc082d82023-05-03 08:43:42 -0700338std::unique_ptr<LogSink> FileBackend::RequestFile(std::string_view id) {
Alexei Strots01395492023-03-20 13:59:56 -0700339 const std::string filename = absl::StrCat(base_name_, separator_, id);
340 return std::make_unique<FileHandler>(filename);
341}
342
Alexei Strotsf08b8fb2023-04-21 19:46:08 -0700343std::vector<std::string> FileBackend::ListFiles() const {
344 std::filesystem::path directory(base_name_);
345 if (!is_directory(directory)) {
346 directory = directory.parent_path();
347 }
348 internal::LocalFileOperations operations(directory.string());
349 std::vector<std::string> files;
350 operations.FindLogs(&files);
351
352 std::vector<std::string> names;
353 const std::string prefix = absl::StrCat(base_name_, separator_);
354 for (const auto &file : files) {
355 CHECK(absl::StartsWith(file, prefix));
356 names.push_back(file.substr(prefix.size()));
357 }
358 return names;
359}
360
361std::unique_ptr<DataDecoder> FileBackend::GetDecoder(
362 std::string_view id) const {
363 const std::string filename = absl::StrCat(base_name_, separator_, id);
364 CHECK(std::filesystem::exists(filename));
365 return std::make_unique<DummyDecoder>(filename);
366}
367
Alexei Strots01395492023-03-20 13:59:56 -0700368RenamableFileBackend::RenamableFileBackend(std::string_view base_name)
369 : base_name_(base_name), separator_(base_name_.back() == '/' ? "" : "_") {}
370
Alexei Strotsbc082d82023-05-03 08:43:42 -0700371std::unique_ptr<LogSink> RenamableFileBackend::RequestFile(
Alexei Strots01395492023-03-20 13:59:56 -0700372 std::string_view id) {
373 const std::string filename =
374 absl::StrCat(base_name_, separator_, id, temp_suffix_);
375 return std::make_unique<RenamableFileHandler>(this, filename);
376}
377
378void RenamableFileBackend::EnableTempFiles() {
379 use_temp_files_ = true;
380 temp_suffix_ = kTempExtension;
381}
382
383bool RenamableFileBackend::RenameLogBase(std::string_view new_base_name) {
384 if (new_base_name == base_name_) {
385 return true;
386 }
387 CHECK(old_base_name_.empty())
388 << "Only one change of base_name is supported. Was: " << old_base_name_;
389
390 std::string current_directory = base_name_;
391 std::string new_directory(new_base_name);
392
393 auto current_path_split = current_directory.rfind("/");
394 CHECK(current_path_split != std::string::npos)
395 << "Could not find / in the current directory path";
396 auto new_path_split = new_directory.rfind("/");
397 CHECK(new_path_split != std::string::npos)
398 << "Could not find / in the new directory path";
399
400 CHECK(new_base_name.substr(new_path_split) ==
401 current_directory.substr(current_path_split))
402 << "Rename of file base from " << current_directory << " to "
403 << new_directory << " is not supported.";
404
405 current_directory.resize(current_path_split);
406 new_directory.resize(new_path_split);
407 DIR *dir = opendir(current_directory.c_str());
408 if (dir) {
409 closedir(dir);
410 const int result = rename(current_directory.c_str(), new_directory.c_str());
411 if (result != 0) {
412 PLOG(ERROR) << "Unable to rename " << current_directory << " to "
413 << new_directory;
414 return false;
415 }
416 } else {
417 // Handle if directory was already renamed.
418 dir = opendir(new_directory.c_str());
419 if (!dir) {
420 LOG(ERROR) << "Old directory " << current_directory
421 << " missing and new directory " << new_directory
422 << " not present.";
423 return false;
424 }
425 closedir(dir);
426 }
427 old_base_name_ = base_name_;
428 base_name_ = std::string(new_base_name);
429 separator_ = base_name_.back() == '/' ? "" : "_";
430 return true;
431}
432
433WriteCode RenamableFileBackend::RenameFileAfterClose(
434 std::string_view filename) {
435 // Fast check that we can skip rename.
436 if (!use_temp_files_ && old_base_name_.empty()) {
437 return WriteCode::kOk;
438 }
439
440 std::string current_filename(filename);
441
442 // When changing the base name, we rename the log folder while there active
443 // buffer writers. Therefore, the name of that active buffer may still refer
444 // to the old file location rather than the new one.
445 if (!old_base_name_.empty()) {
446 auto offset = current_filename.find(old_base_name_);
447 if (offset != std::string::npos) {
448 current_filename.replace(offset, old_base_name_.length(), base_name_);
449 }
450 }
451
452 std::string final_filename = current_filename;
453 if (use_temp_files_) {
454 CHECK(current_filename.size() > temp_suffix_.size());
455 final_filename = current_filename.substr(
456 0, current_filename.size() - temp_suffix_.size());
457 }
458
459 int result = rename(current_filename.c_str(), final_filename.c_str());
460
461 bool ran_out_of_space = false;
462 if (result != 0) {
463 if (errno == ENOSPC) {
464 ran_out_of_space = true;
465 } else {
466 PLOG(FATAL) << "Renaming " << current_filename << " to " << final_filename
467 << " failed";
468 }
469 } else {
470 VLOG(1) << "Renamed " << current_filename << " -> " << final_filename;
471 }
472 return ran_out_of_space ? WriteCode::kOutOfSpace : WriteCode::kOk;
473}
474
475WriteCode RenamableFileBackend::RenamableFileHandler::Close() {
476 if (!is_open()) {
477 return WriteCode::kOk;
478 }
479 if (FileHandler::Close() == WriteCode::kOutOfSpace) {
480 return WriteCode::kOutOfSpace;
481 }
482 if (owner_->RenameFileAfterClose(filename()) == WriteCode::kOutOfSpace) {
483 return WriteCode::kOutOfSpace;
484 }
485 return WriteCode::kOk;
486}
Alexei Strotsa0b99d72023-04-11 15:12:42 -0700487
Austin Schuh3ebaf782023-04-07 16:03:28 -0700488} // namespace aos::logger