From: Charles Manning Date: Fri, 19 Jun 2020 01:52:21 +0000 (+1200) Subject: Add malloc monitoring to get a sense of RAM use X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs2.git;a=commitdiff_plain;h=0ca67da389c41a10ba51b85905a2437669c0471d Add malloc monitoring to get a sense of RAM use 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 --- diff --git a/direct/test-framework/FrameworkRules.mk b/direct/test-framework/FrameworkRules.mk index d56dcf6..d11673b 100644 --- a/direct/test-framework/FrameworkRules.mk +++ b/direct/test-framework/FrameworkRules.mk @@ -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 diff --git a/direct/test-framework/basic-tests/dtest.c b/direct/test-framework/basic-tests/dtest.c index 1a98bcb..8b58762 100644 --- a/direct/test-framework/basic-tests/dtest.c +++ b/direct/test-framework/basic-tests/dtest.c @@ -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(¤t, &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"); diff --git a/direct/test-framework/yaffs_osglue.c b/direct/test-framework/yaffs_osglue.c index ca5efc0..2cc18bb 100644 --- a/direct/test-framework/yaffs_osglue.c +++ b/direct/test-framework/yaffs_osglue.c @@ -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 +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); } diff --git a/direct/yaffs_osglue.h b/direct/yaffs_osglue.h index 5325e52..d2300a7 100644 --- a/direct/yaffs_osglue.h +++ b/direct/yaffs_osglue.h @@ -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);