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

This commit is contained in:
s-hadinger 2023-07-19 20:41:41 +02:00 committed by GitHub
parent 6382c2806c
commit 0b59cda02a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 0 deletions

View File

@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
- Matter mini-profiler (#19075) - Matter mini-profiler (#19075)
- Berry `_class` can be used in `static var` initialization code (#19088) - Berry `_class` can be used in `static var` initialization code (#19088)
- Berry add `energy.update_total()` to call `EnergyUpdateTotal()` from energy driver - Berry add `energy.update_total()` to call `EnergyUpdateTotal()` from energy driver
- Berry add metrics for memory allocation/deallocation/reallocation
### Breaking Changed ### Breaking Changed

View File

@ -171,11 +171,41 @@ static int m_counters(bvm *vm)
map_insert(vm, "try", vm->counter_try); map_insert(vm, "try", vm->counter_try);
map_insert(vm, "raise", vm->counter_exc); map_insert(vm, "raise", vm->counter_exc);
map_insert(vm, "objects", vm->counter_gc_kept); 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_pop(vm, 1);
be_return(vm); be_return(vm);
} }
#endif #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 #if !BE_USE_PRECOMPILED_OBJECT
be_native_module_attr_table(debug) { be_native_module_attr_table(debug) {
be_native_module_function("attrdump", m_attrdump), be_native_module_function("attrdump", m_attrdump),
@ -208,6 +238,10 @@ module debug (scope: global, depend: BE_USE_DEBUG_MODULE) {
top, func(m_top) top, func(m_top)
varname, func(m_varname), BE_DEBUG_VAR_INFO varname, func(m_varname), BE_DEBUG_VAR_INFO
upvname, func(m_upvname), 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 */ @const_object_info_end */
#include "../generate/be_fixed_debug.h" #include "../generate/be_fixed_debug.h"

View File

@ -60,12 +60,18 @@ BERRY_API void* be_realloc(bvm *vm, void *ptr, size_t old_size, size_t new_size)
while (1) { while (1) {
/* Case 1: new allocation */ /* Case 1: new allocation */
#if BE_USE_PERF_COUNTERS
vm->counter_mem_alloc++;
#endif
if (!ptr || (old_size == 0)) { if (!ptr || (old_size == 0)) {
block = malloc_from_pool(vm, new_size); block = malloc_from_pool(vm, new_size);
} }
/* Case 2: deallocate */ /* Case 2: deallocate */
else if (new_size == 0) { else if (new_size == 0) {
#if BE_USE_PERF_COUNTERS
vm->counter_mem_free++;
#endif
if (ptr == NULL) { return NULL; } /* safeguard */ if (ptr == NULL) { return NULL; } /* safeguard */
#if BE_USE_DEBUG_GC #if BE_USE_DEBUG_GC
memset(ptr, 0xFF, old_size); /* fill the structure with invalid pointers */ 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 */ /* Case 3: reallocate with a different size */
else if (new_size && old_size) { // TODO we already know they are not null TODO 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) { if (new_size <= POOL32_SIZE || old_size <=POOL32_SIZE) {
/* complex case with different pools */ /* complex case with different pools */
if (new_size <= POOL16_SIZE && old_size <= POOL16_SIZE) { if (new_size <= POOL16_SIZE && old_size <= POOL16_SIZE) {

View File

@ -512,6 +512,9 @@ BERRY_API bvm* be_vm_new(void)
vm->counter_exc = 0; vm->counter_exc = 0;
vm->counter_gc_kept = 0; vm->counter_gc_kept = 0;
vm->counter_gc_freed = 0; vm->counter_gc_freed = 0;
vm->counter_mem_alloc = 0;
vm->counter_mem_free = 0;
vm->counter_mem_realloc = 0;
#endif #endif
return vm; return vm;
} }

View File

@ -119,6 +119,9 @@ struct bvm {
uint32_t counter_exc; /* counter for raised exceptions */ uint32_t counter_exc; /* counter for raised exceptions */
uint32_t counter_gc_kept; /* counter for objects scanned by last gc */ 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_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_gc0;
uint32_t micros_gc1; uint32_t micros_gc1;