mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-31 18:47:21 +08:00
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.
This commit is contained in:
@@ -42,6 +42,9 @@
|
|||||||
#include <systemlib/hardfault_log.h>
|
#include <systemlib/hardfault_log.h>
|
||||||
#endif /* __PX4_NUTTX */
|
#endif /* __PX4_NUTTX */
|
||||||
|
|
||||||
|
using namespace time_literals;
|
||||||
|
|
||||||
|
|
||||||
namespace px4
|
namespace px4
|
||||||
{
|
{
|
||||||
namespace logger
|
namespace logger
|
||||||
@@ -229,6 +232,7 @@ void LogWriterFile::run()
|
|||||||
|
|
||||||
int poll_count = 0;
|
int poll_count = 0;
|
||||||
int written = 0;
|
int written = 0;
|
||||||
|
hrt_abstime last_fsync = hrt_absolute_time();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
size_t available = 0;
|
size_t available = 0;
|
||||||
@@ -263,12 +267,15 @@ void LogWriterFile::run()
|
|||||||
written = ::write(_fd, read_ptr, available);
|
written = ::write(_fd, read_ptr, available);
|
||||||
perf_end(_perf_write);
|
perf_end(_perf_write);
|
||||||
|
|
||||||
|
const hrt_abstime now = hrt_absolute_time();
|
||||||
|
|
||||||
/* call fsync periodically to minimize potential loss of data */
|
/* 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);
|
perf_begin(_perf_fsync);
|
||||||
::fsync(_fd);
|
::fsync(_fd);
|
||||||
perf_end(_perf_fsync);
|
perf_end(_perf_fsync);
|
||||||
poll_count = 0;
|
poll_count = 0;
|
||||||
|
last_fsync = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (written < 0) {
|
if (written < 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user