Browse Source

Reduce data manager SD card wear and tear

When the data manager was first designed each file record contained a 2
byte header and an 126 byte data section, resulting in a record length
of 128 bytes. Along the way it was decided to add 2 spare bytes to the
record header, but regrettably the data section was not correspondingly
reduced in size so we end up with a record length of 130 bytes. This is
bad since it does not align with SD card flash sectors and results in
more erase/write flash cycles than necessary thus reducing the SD cards
life.

This update reduced the data section of the data manager to 124,
resulting in an optimal record length of 128 bytes.

In order to avoid the reuse of data previously written data in the old
format, which could result in catastrophic misinterpretation, the data
manager file is checked at startup. If it is found to be in the old
format, it is deleted and recreated with in the new record length. In
this case previously stored data is lost, but that is far safer than the
unpredictable result of using the old file.
sbg
Jean Cyr 11 years ago
parent
commit
bf4558c31b
  1. 19
      src/modules/dataman/dataman.c
  2. 2
      src/modules/dataman/dataman.h

19
src/modules/dataman/dataman.c

@ -44,7 +44,9 @@ @@ -44,7 +44,9 @@
#include <stdlib.h>
#include <fcntl.h>
#include <systemlib/systemlib.h>
#include <systemlib/err.h>
#include <queue.h>
#include <string.h>
#include "dataman.h"
@ -594,6 +596,20 @@ task_main(int argc, char *argv[]) @@ -594,6 +596,20 @@ task_main(int argc, char *argv[])
sem_init(&g_work_queued_sema, 1, 0);
/* See if the data manage file exists and is a multiple of the sector size */
g_task_fd = open(k_data_manager_device_path, O_RDONLY | O_BINARY);
if (g_task_fd >= 0) {
/* File exists, check its size */
int file_size = lseek(g_task_fd, 0, SEEK_END);
if ((file_size % k_sector_size) != 0) {
warnx("Incompatible data manager file %s, resetting it", k_data_manager_device_path);
close(g_task_fd);
unlink(k_data_manager_device_path);
}
else
close(g_task_fd);
}
/* Open or create the data manager file */
g_task_fd = open(k_data_manager_device_path, O_RDWR | O_CREAT | O_BINARY);
@ -603,7 +619,7 @@ task_main(int argc, char *argv[]) @@ -603,7 +619,7 @@ task_main(int argc, char *argv[])
return -1;
}
if (lseek(g_task_fd, max_offset, SEEK_SET) != max_offset) {
if ((unsigned)lseek(g_task_fd, max_offset, SEEK_SET) != max_offset) {
close(g_task_fd);
warnx("Could not seek data manager file %s", k_data_manager_device_path);
sem_post(&g_init_sema); /* Don't want to hang startup */
@ -776,4 +792,3 @@ dataman_main(int argc, char *argv[]) @@ -776,4 +792,3 @@ dataman_main(int argc, char *argv[])
exit(1);
}

2
src/modules/dataman/dataman.h

@ -79,7 +79,7 @@ extern "C" { @@ -79,7 +79,7 @@ extern "C" {
} dm_reset_reason;
/* Maximum size in bytes of a single item instance */
#define DM_MAX_DATA_SIZE 126
#define DM_MAX_DATA_SIZE 124
/* Retrieve from the data manager store */
__EXPORT ssize_t

Loading…
Cancel
Save