Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Berry add metrics for memory allocation/deallocation/reallocation #19150

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 34 additions & 0 deletions lib/libesp32/berry/src/be_debuglib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions lib/libesp32/berry/src/be_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions lib/libesp32/berry/src/be_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/libesp32/berry/src/be_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down