From a1be83cda92c2c6b709c8238497b1be414e03f1b Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 28 Sep 2018 11:27:01 -0600 Subject: [PATCH] apps/examples/fstest: Add logic to dump logic content of SPIFFS --- examples/fstest/Kconfig | 6 +-- examples/fstest/fstest_main.c | 79 +++++++++++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/examples/fstest/Kconfig b/examples/fstest/Kconfig index 28505184c..d00b98440 100644 --- a/examples/fstest/Kconfig +++ b/examples/fstest/Kconfig @@ -46,10 +46,10 @@ config EXAMPLES_FSTEST_SPIFFS depends on FS_SPIFFS ---help--- SPIFFS garbage collection and integrity checking will be performed - after a write fails with ENOSPC. + after each pass through the test and if a write fails with ENOSPC. - If this option is not selected then the SPIFFS FLASH will likely - become full before you finish the test. + If this option is not selected then the SPIFFS FLASH will be be + reclaimed on-demand when there is no longer any available FLASH. config EXAMPLES_FSTEST_VERBOSE bool "Verbose output" diff --git a/examples/fstest/fstest_main.c b/examples/fstest/fstest_main.c index 0444f92b4..b09409ec2 100644 --- a/examples/fstest/fstest_main.c +++ b/examples/fstest/fstest_main.c @@ -275,14 +275,26 @@ static void fstest_freefile(FAR struct fstest_filedesc_s *file) } /**************************************************************************** - * Name: fstest_gc + * Name: fstest_gc and fstest_gc_withfd ****************************************************************************/ #ifdef CONFIG_EXAMPLES_FSTEST_SPIFFS -static int fstest_gc(int fd, size_t nbytes) +static int fstest_gc_withfd(int fd, size_t nbytes) { int ret; +#ifdef CONFIG_SPIFFS_DUMP + /* Dump the logic content of FLASH before garbage collection */ + + printf("SPIFFS Content (before GC):\n"); + + ret = ioctl(fd, FIOC_DUMP, (unsigned long)nbytes); + if (ret < 0) + { + printf("ERROR: ioctl(FIOC_DUMP) failed: %d\n", errno); + } +#endif + /* Perform SPIFFS garbage collection */ printf("SPIFFS Garbage Collection: %lu bytes\n", (unsigned long)nbytes); @@ -314,10 +326,60 @@ static int fstest_gc(int fd, size_t nbytes) } } +#ifdef CONFIG_SPIFFS_DUMP + /* Dump the logic content of FLASH after garbage collection */ + + printf("SPIFFS Content (After GC):\n"); + + ret = ioctl(fd, FIOC_DUMP, (unsigned long)nbytes); + if (ret < 0) + { + printf("ERROR: ioctl(FIOC_DUMP) failed: %d\n", errno); + } +#endif + + return ret; +} + +static int fstest_gc(size_t nbytes) +{ + FAR struct fstest_filedesc_s *file; + int ret = OK; + int fd; + int i; + + /* Find the first valid file */ + + for (i = 0; i < CONFIG_EXAMPLES_FSTEST_MAXOPEN; i++) + { + file = &g_files[i]; + if (file->name != NULL && !file->deleted) + { + /* Open the file for reading */ + + fd = open(file->name, O_RDONLY); + if (fd < 0) + { + printf("ERROR: Failed to open file for reading: %d\n", errno); + ret = ERROR; + } + else + { + /* Use this file descriptor to support the garbage collection */ + + ret = fstest_gc_withfd(fd, nbytes); + close(fd); + } + + break; + } + } + return ret; } #else -# define fstest_gc(f,n) (-ENOSYS) +# define fstest_gc_withfd(f,n) (-ENOSYS) +# define fstest_gc(n) (-ENOSYS) #endif /**************************************************************************** @@ -390,7 +452,7 @@ static inline int fstest_wrfile(FAR struct fstest_filedesc_s *file) /* Try to recover some space on the FLASH */ - ret2 = fstest_gc(fd, nbytestowrite); + ret2 = fstest_gc_withfd(fd, nbytestowrite); if (ret2 >= 0) { continue; @@ -429,11 +491,6 @@ static inline int fstest_wrfile(FAR struct fstest_filedesc_s *file) offset += nbyteswritten; } - /* Try to recover some space on the FLASH to write another file of this - * size. - */ - - (void)fstest_gc(fd, file->len); close(fd); return OK; } @@ -958,6 +1015,10 @@ int fstest_main(int argc, char *argv[]) printf(" Free File Nodes: %ld\n", (long)buf.f_ffree); } + /* Perform garbage collection, integrity checks */ + + (void)fstest_gc(buf.f_bfree); + /* Show memory usage */ fstest_loopmemusage();