Skip to content
Snippets Groups Projects
Commit 460402e0 authored by Ban Siesta's avatar Ban Siesta
Browse files

sdlog2: checks and warnings to make sure there is space left on the SD card

parent b6f6a997
No related branches found
No related tags found
No related merge requests found
......@@ -39,12 +39,14 @@
*
* @author Lorenz Meier <lm@inf.ethz.ch>
* @author Anton Babushkin <anton.babushkin@me.com>
* @author Ban Siesta <bansiesta@gmail.com>
*/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/prctl.h>
#include <sys/statfs.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment