Add malloc monitoring to get a sense of RAM use
authorCharles Manning <cdhmanning@gmail.com>
Fri, 19 Jun 2020 01:52:21 +0000 (13:52 +1200)
committerCharles Manning <cdhmanning@gmail.com>
Fri, 19 Jun 2020 01:52:21 +0000 (13:52 +1200)
Note this should only be monitoring the malloc usage by Yaffs, not the
malloc usage in things like flash simulators which are not relevant in
real systems.

Signed-off-by: Charles Manning <cdhmanning@gmail.com>
direct/test-framework/FrameworkRules.mk
direct/test-framework/basic-tests/dtest.c
direct/test-framework/yaffs_osglue.c
direct/yaffs_osglue.h

index d56dcf63c2a744edc11e9a85a7dcb618b4ad8341..d11673bf3fd7a12c57b43c26b090e0194ecdb29e 100644 (file)
@@ -19,6 +19,7 @@
 
 CFLAGS =      -DCONFIG_YAFFS_DIRECT -DCONFIG_YAFFS_YAFFS2  -DCONFIG_YAFFS_DEFINES_TYPES
 CFLAGS +=     -DCONFIG_YAFFS_PROVIDE_DEFS -DCONFIG_YAFFSFS_PROVIDE_VALUES
+CFLAGS +=     -DCONFIG_YAFFS_MONITOR_MALLOC
 CFLAGS +=    -Wall -g $(EXTRA_COMPILE_FLAGS) -Wstrict-aliasing -Werror
 #CFLAGS +=    -fno-strict-aliasing
 CFLAGS +=    -O0
index 1a98bcb18d17ced144f73d389893af8bb05e0f7d..8b58762351bd4fa86938cdeea7a65f7216952a71 100644 (file)
@@ -3406,11 +3406,33 @@ void rmdir_test2(void)
        try_rmdir("/nand/z/");
 }
 
+
+void dump_yaffs_malloc_usage(void)
+{
+       unsigned current;
+       unsigned high_water;
+
+       yaffsfs_get_malloc_values(&current, &high_water);
+
+       printf("\n"
+              "***************************************\n"
+              "Dump of yaffs malloc usage during run\n"
+              "Currently allocated : %u bytes\n"
+              "High water allocated : %u bytes\n"
+              "Note, this is not accurate on all platforms\n",
+               current, high_water);
+
+}
+
+
 int random_seed;
 int simulate_power_failure;
 
 int main(int argc, char *argv[])
 {
+
+       atexit(dump_yaffs_malloc_usage);
+
        (void) argc;
        (void) argv;
 
@@ -3454,7 +3476,7 @@ int main(int argc, char *argv[])
        //long_test_on_path("/ram2k");
        // long_test_on_path("/flash");
        //simple_rw_test("/flash/flash");
-       //fill_disk_test("/flash/flash");
+       //fill_disk_test("/nand");
        // rename_over_test("/flash");
        //lookup_test("/flash");
        //freespace_test("/flash/flash");
index ca5efc069e366f87479544923cb1bc6f7f6596a1..2cc18bbdcb6bdec25eafef419df5caac894ed42a 100644 (file)
@@ -165,6 +165,48 @@ u32 yaffsfs_CurrentTime(void)
  * Functions to allocate and free memory.
  */
 
+
+static unsigned malloc_high_water;
+static unsigned malloc_currently_allocated;
+
+#ifdef CONFIG_YAFFS_MONITOR_MALLOC
+
+#include <malloc.h>
+static void yaffsfs_malloc_add(void *ptr)
+{
+       unsigned this_size = malloc_usable_size(ptr);
+       malloc_currently_allocated += this_size;
+
+       if (malloc_currently_allocated > malloc_high_water)
+               malloc_high_water = malloc_currently_allocated;
+}
+
+static void yaffsfs_malloc_remove(void *ptr)
+{
+       unsigned this_size = malloc_usable_size(ptr);
+       malloc_currently_allocated -= this_size;
+}
+#else
+static void yaffsfs_malloc_add(void *ptr)
+{
+       (void)ptr;
+}
+
+static void yaffsfs_malloc_remove(void *ptr)
+{
+       (void)ptr;
+}
+#endif
+
+void yaffsfs_get_malloc_values(unsigned *current, unsigned *high_water)
+{
+       if (current)
+               *current = malloc_currently_allocated;
+       if(high_water)
+               *high_water = malloc_high_water;
+}
+
+
 #ifdef CONFIG_YAFFS_TEST_MALLOC
 
 static int yaffs_kill_alloc = 0;
@@ -182,6 +224,7 @@ void *yaffsfs_malloc(size_t size)
        this = malloc(size);
        if(this)
                total_malloced += size;
+       yaffsfs_malloc_add(this);
        return this;
 }
 
@@ -189,13 +232,19 @@ void *yaffsfs_malloc(size_t size)
 
 void *yaffsfs_malloc(size_t size)
 {
-       return malloc(size);
+       void *this = malloc(size);
+       yaffsfs_malloc_add(this);
+       return this;
 }
 
 #endif
 
+
+
+
 void yaffsfs_free(void *ptr)
 {
+       yaffsfs_malloc_remove(ptr);
        free(ptr);
 }
 
index 5325e52fc51b884b0049995fdb331519636a0f82..d2300a7b9c9e3eddfdb94405f59b3fa4e9ffb17b 100644 (file)
@@ -34,6 +34,9 @@ void yaffsfs_SetError(int err);
 void *yaffsfs_malloc(size_t size);
 void yaffsfs_free(void *ptr);
 
+void yaffsfs_get_malloc_values(unsigned *current, unsigned *high_water);
+
+
 int yaffsfs_CheckMemRegion(const void *addr, size_t size, int write_request);
 
 void yaffsfs_OSInitialisation(void);