From 460402e07ac84315a32cc42dae295c88a6297924 Mon Sep 17 00:00:00 2001 From: Ban Siesta Date: Sun, 4 Jan 2015 13:31:18 +0000 Subject: sdlog2: checks and warnings to make sure there is space left on the SD card --- src/modules/sdlog2/sdlog2.c | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'src/modules/sdlog2/sdlog2.c') diff --git a/src/modules/sdlog2/sdlog2.c b/src/modules/sdlog2/sdlog2.c index 2ce3d0097..da181dad5 100644 --- a/src/modules/sdlog2/sdlog2.c +++ b/src/modules/sdlog2/sdlog2.c @@ -39,12 +39,14 @@ * * @author Lorenz Meier * @author Anton Babushkin + * @author Ban Siesta */ #include #include #include #include +#include #include #include #include @@ -158,6 +160,7 @@ static const int MIN_BYTES_TO_WRITE = 512; static bool _extended_logging = false; +static const char *mountpoint = "/fs/microsd"; static const char *log_root = "/fs/microsd/log"; static int mavlink_fd = -1; struct logbuffer_s lb; @@ -185,6 +188,9 @@ static bool log_name_timestamp = false; /* helper flag to track system state changes */ static bool flag_system_armed = false; +/* flag if warning about MicroSD card being almost full has already been sent */ +static bool space_warning_sent = false; + static pthread_t logwriter_pthread = 0; static pthread_attr_t logwriter_attr; @@ -244,6 +250,11 @@ static bool file_exist(const char *filename); static int file_copy(const char *file_old, const char *file_new); +/** + * Check if there is still free space available + */ +static int check_free_space(void); + static void handle_command(struct vehicle_command_s *cmd); static void handle_status(struct vehicle_status_s *cmd); @@ -547,6 +558,7 @@ static void *logwriter_thread(void *arg) pthread_mutex_unlock(&logbuffer_mutex); if (available > 0) { + /* do heavy IO here */ if (available > MAX_WRITE_CHUNK) { n = MAX_WRITE_CHUNK; @@ -584,6 +596,12 @@ static void *logwriter_thread(void *arg) if (++poll_count == 10) { fsync(log_fd); poll_count = 0; + + /* check if space is available, if not stop everything */ + if (check_free_space() != OK) { + logwriter_should_exit = true; + main_thread_should_exit = true; + } } } @@ -607,6 +625,7 @@ void sdlog2_start_log() errx(1, "error creating log dir"); } + /* initialize statistics counter */ log_bytes_written = 0; start_time = hrt_absolute_time(); @@ -899,6 +918,12 @@ int sdlog2_thread_main(int argc, char *argv[]) } + + if (check_free_space() != OK) { + errx(1, "error MicroSD almost full"); + } + + /* create log root dir */ int mkdir_ret = mkdir(log_root, S_IRWXU | S_IRWXG | S_IRWXO); @@ -1822,6 +1847,31 @@ int file_copy(const char *file_old, const char *file_new) return OK; } +int check_free_space() +{ + /* use statfs to determine the number of blocks left */ + FAR struct statfs statfs_buf; + if (statfs(mountpoint, &statfs_buf) != OK) { + warnx("could not determine statfs"); + return ERROR; + } + + /* use a threshold of 4 MiB */ + if (statfs_buf.f_bavail < (int)(4*1024*1024/statfs_buf.f_bsize)) { + warnx("no more space on MicroSD (less than 4 MiB)"); + mavlink_log_critical(mavlink_fd, "[sdlog2] no more space left on MicroSD"); + return ERROR; + + /* use a threshold of 100 MiB to send a warning */ + } else if (!space_warning_sent && statfs_buf.f_bavail < (int)(100*1024*1024/statfs_buf.f_bsize)) { + warnx("space on MicroSD running out (less than 100MiB)"); + mavlink_log_critical(mavlink_fd, "[sdlog2] space on MicroSD running out"); + space_warning_sent = true; + } + + return OK; +} + void handle_command(struct vehicle_command_s *cmd) { int param; -- cgit v1.2.3