From bda237b3685beb026e94f923af10e99082aa23cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Thu, 3 May 2018 11:39:39 +0200 Subject: [PATCH] logger: ensure that fsync is called at least every second In case a log ends abruptly, we will know that we have everything up to the last second. A test showed that CPU load and the amount of logging drops are unaffected by this. --- src/modules/logger/log_writer_file.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/modules/logger/log_writer_file.cpp b/src/modules/logger/log_writer_file.cpp index 05993a18a9..29c6f19f40 100644 --- a/src/modules/logger/log_writer_file.cpp +++ b/src/modules/logger/log_writer_file.cpp @@ -42,6 +42,9 @@ #include #endif /* __PX4_NUTTX */ +using namespace time_literals; + + namespace px4 { namespace logger @@ -229,6 +232,7 @@ void LogWriterFile::run() int poll_count = 0; int written = 0; + hrt_abstime last_fsync = hrt_absolute_time(); while (true) { size_t available = 0; @@ -263,12 +267,15 @@ void LogWriterFile::run() written = ::write(_fd, read_ptr, available); perf_end(_perf_write); + const hrt_abstime now = hrt_absolute_time(); + /* call fsync periodically to minimize potential loss of data */ - if (++poll_count >= 100) { + if (++poll_count >= 100 || now - last_fsync > 1_s) { perf_begin(_perf_fsync); ::fsync(_fd); perf_end(_perf_fsync); poll_count = 0; + last_fsync = now; } if (written < 0) {