From a9fac0c0ff8f16c75ba858a43a0aa7f1e0a981ba Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Wed, 19 Jul 2023 20:29:45 +0200 Subject: [PATCH] Berry add metrics for memory allocation/deallocation/reallocation --- CHANGELOG.md | 1 + lib/libesp32/berry/src/be_debuglib.c | 34 ++++++++++++++++++++++++++++ lib/libesp32/berry/src/be_mem.c | 9 ++++++++ lib/libesp32/berry/src/be_vm.c | 3 +++ lib/libesp32/berry/src/be_vm.h | 3 +++ 5 files changed, 50 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7523fed7f3d2..b18483b0ce45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file. - Matter mini-profiler (#19075) - Berry `_class` can be used in `static var` initialization code (#19088) - Berry add `energy.update_total()` to call `EnergyUpdateTotal()` from energy driver +- Berry add metrics for memory allocation/deallocation/reallocation ### Breaking Changed diff --git a/lib/libesp32/berry/src/be_debuglib.c b/lib/libesp32/berry/src/be_debuglib.c index 49ee027b0fe3..502935b6c59b 100644 --- a/lib/libesp32/berry/src/be_debuglib.c +++ b/lib/libesp32/berry/src/be_debuglib.c @@ -171,11 +171,41 @@ static int m_counters(bvm *vm) map_insert(vm, "try", vm->counter_try); map_insert(vm, "raise", vm->counter_exc); map_insert(vm, "objects", vm->counter_gc_kept); + map_insert(vm, "mem_alloc", vm->counter_mem_alloc); + map_insert(vm, "mem_free", vm->counter_mem_free); + map_insert(vm, "mem_realloc", vm->counter_mem_realloc); be_pop(vm, 1); be_return(vm); } #endif +static int m_allocs(bvm *vm) { +#if BE_USE_PERF_COUNTERS + be_pushint(vm, vm->counter_mem_alloc); + be_return(vm); +#else + be_return_nil(vm); +#endif +} + +static int m_frees(bvm *vm) { +#if BE_USE_PERF_COUNTERS + be_pushint(vm, vm->counter_mem_free); + be_return(vm); +#else + be_return_nil(vm); +#endif +} + +static int m_reallocs(bvm *vm) { +#if BE_USE_PERF_COUNTERS + be_pushint(vm, vm->counter_mem_realloc); + be_return(vm); +#else + be_return_nil(vm); +#endif +} + #if !BE_USE_PRECOMPILED_OBJECT be_native_module_attr_table(debug) { be_native_module_function("attrdump", m_attrdump), @@ -208,6 +238,10 @@ module debug (scope: global, depend: BE_USE_DEBUG_MODULE) { top, func(m_top) varname, func(m_varname), BE_DEBUG_VAR_INFO upvname, func(m_upvname), BE_DEBUG_VAR_INFO + // individual counters + allocs, func(m_allocs) + frees, func(m_frees) + reallocs, func(m_reallocs) } @const_object_info_end */ #include "../generate/be_fixed_debug.h" diff --git a/lib/libesp32/berry/src/be_mem.c b/lib/libesp32/berry/src/be_mem.c index 3657fd951222..36e30866a7fc 100644 --- a/lib/libesp32/berry/src/be_mem.c +++ b/lib/libesp32/berry/src/be_mem.c @@ -60,12 +60,18 @@ BERRY_API void* be_realloc(bvm *vm, void *ptr, size_t old_size, size_t new_size) while (1) { /* Case 1: new allocation */ +#if BE_USE_PERF_COUNTERS + vm->counter_mem_alloc++; +#endif if (!ptr || (old_size == 0)) { block = malloc_from_pool(vm, new_size); } /* Case 2: deallocate */ else if (new_size == 0) { +#if BE_USE_PERF_COUNTERS + vm->counter_mem_free++; +#endif if (ptr == NULL) { return NULL; } /* safeguard */ #if BE_USE_DEBUG_GC memset(ptr, 0xFF, old_size); /* fill the structure with invalid pointers */ @@ -76,6 +82,9 @@ BERRY_API void* be_realloc(bvm *vm, void *ptr, size_t old_size, size_t new_size) /* Case 3: reallocate with a different size */ else if (new_size && old_size) { // TODO we already know they are not null TODO +#if BE_USE_PERF_COUNTERS + vm->counter_mem_realloc++; +#endif if (new_size <= POOL32_SIZE || old_size <=POOL32_SIZE) { /* complex case with different pools */ if (new_size <= POOL16_SIZE && old_size <= POOL16_SIZE) { diff --git a/lib/libesp32/berry/src/be_vm.c b/lib/libesp32/berry/src/be_vm.c index 4c21866e4d1b..b234308d0ce8 100644 --- a/lib/libesp32/berry/src/be_vm.c +++ b/lib/libesp32/berry/src/be_vm.c @@ -512,6 +512,9 @@ BERRY_API bvm* be_vm_new(void) vm->counter_exc = 0; vm->counter_gc_kept = 0; vm->counter_gc_freed = 0; + vm->counter_mem_alloc = 0; + vm->counter_mem_free = 0; + vm->counter_mem_realloc = 0; #endif return vm; } diff --git a/lib/libesp32/berry/src/be_vm.h b/lib/libesp32/berry/src/be_vm.h index 711d96ed15bf..ba05579bfef4 100644 --- a/lib/libesp32/berry/src/be_vm.h +++ b/lib/libesp32/berry/src/be_vm.h @@ -119,6 +119,9 @@ struct bvm { uint32_t counter_exc; /* counter for raised exceptions */ uint32_t counter_gc_kept; /* counter for objects scanned by last gc */ uint32_t counter_gc_freed; /* counter for objects freed by last gc */ + uint32_t counter_mem_alloc; /* counter for memory allocations */ + uint32_t counter_mem_free; /* counter for memory frees */ + uint32_t counter_mem_realloc; /* counter for memory reallocations */ uint32_t micros_gc0; uint32_t micros_gc1;