Get AllocationAlarm working

This commit is contained in:
Scott Shawcroft 2020-07-16 19:01:43 -07:00
parent 518d909b2c
commit a1e4814a27
No known key found for this signature in database
GPG Key ID: 9349BC7E64B1921E
10 changed files with 40 additions and 40 deletions

7
main.c
View File

@ -64,6 +64,10 @@
#include "shared-module/displayio/__init__.h" #include "shared-module/displayio/__init__.h"
#endif #endif
#if CIRCUITPY_MEMORYMONITOR
#include "shared-module/memorymonitor/__init__.h"
#endif
#if CIRCUITPY_NETWORK #if CIRCUITPY_NETWORK
#include "shared-module/network/__init__.h" #include "shared-module/network/__init__.h"
#endif #endif
@ -198,6 +202,9 @@ void cleanup_after_vm(supervisor_allocation* heap) {
#if CIRCUITPY_DISPLAYIO #if CIRCUITPY_DISPLAYIO
reset_displays(); reset_displays();
#endif #endif
#if CIRCUITPY_MEMORYMONITOR
memorymonitor_reset();
#endif
filesystem_flush(); filesystem_flush();
stop_mp(); stop_mp();
free_memory(heap); free_memory(heap);

View File

@ -34,7 +34,7 @@
#include "supervisor/shared/safe_mode.h" #include "supervisor/shared/safe_mode.h"
#if CIRCUITPY_MEMORYMONITOR #if CIRCUITPY_MEMORYMONITOR
#include "shared-module/memorymonitor/AllocationSize.h" #include "shared-module/memorymonitor/__init__.h"
#endif #endif
#if MICROPY_ENABLE_GC #if MICROPY_ENABLE_GC
@ -658,7 +658,7 @@ void *gc_alloc(size_t n_bytes, bool has_finaliser, bool long_lived) {
#endif #endif
#if CIRCUITPY_MEMORYMONITOR #if CIRCUITPY_MEMORYMONITOR
memorymonitor_allocationsizes_track_allocation(end_block - start_block + 1); memorymonitor_track_allocation(end_block - start_block + 1);
#endif #endif
return ret_ptr; return ret_ptr;
@ -915,7 +915,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
#endif #endif
#if CIRCUITPY_MEMORYMONITOR #if CIRCUITPY_MEMORYMONITOR
memorymonitor_allocationsizes_track_allocation(new_blocks); memorymonitor_track_allocation(new_blocks);
#endif #endif
return ptr_in; return ptr_in;
@ -948,7 +948,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
#endif #endif
#if CIRCUITPY_MEMORYMONITOR #if CIRCUITPY_MEMORYMONITOR
memorymonitor_allocationsizes_track_allocation(new_blocks); memorymonitor_track_allocation(new_blocks);
#endif #endif
return ptr_in; return ptr_in;

View File

@ -76,6 +76,8 @@ STATIC mp_obj_t memorymonitor_allocationalarm_make_new(const mp_obj_type_t *type
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);
} }
// TODO: Add .countdown(count) to skip allocations and alarm on something after the first.
//| def __enter__(self) -> memorymonitor.AllocationAlarm: //| def __enter__(self) -> memorymonitor.AllocationAlarm:
//| """Enables the alarm.""" //| """Enables the alarm."""
//| ... //| ...

View File

@ -82,6 +82,7 @@ STATIC mp_obj_t memorymonitor_allocationsize_make_new(const mp_obj_type_t *type,
//| ... //| ...
//| //|
STATIC mp_obj_t memorymonitor_allocationsize_obj___enter__(mp_obj_t self_in) { STATIC mp_obj_t memorymonitor_allocationsize_obj___enter__(mp_obj_t self_in) {
common_hal_memorymonitor_allocationsize_clear(self_in);
common_hal_memorymonitor_allocationsize_resume(self_in); common_hal_memorymonitor_allocationsize_resume(self_in);
return self_in; return self_in;
} }
@ -99,48 +100,13 @@ STATIC mp_obj_t memorymonitor_allocationsize_obj___exit__(size_t n_args, const m
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(memorymonitor_allocationsize___exit___obj, 4, 4, memorymonitor_allocationsize_obj___exit__); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(memorymonitor_allocationsize___exit___obj, 4, 4, memorymonitor_allocationsize_obj___exit__);
//| def pause(self) -> None:
//| """Pause allocation tracking"""
//| ...
//|
STATIC mp_obj_t memorymonitor_allocationsize_obj_pause(mp_obj_t self_in) {
memorymonitor_allocationsize_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_memorymonitor_allocationsize_pause(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationsize_pause_obj, memorymonitor_allocationsize_obj_pause);
//| def resume(self) -> None:
//| """Resumes allocation tracking."""
//| ...
//|
STATIC mp_obj_t memorymonitor_allocationsize_obj_resume(mp_obj_t self_in) {
common_hal_memorymonitor_allocationsize_resume(self_in);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationsize_resume_obj, memorymonitor_allocationsize_obj_resume);
//| def clear(self) -> Any:
//| """Clears all captured pulses"""
//| ...
//|
STATIC mp_obj_t memorymonitor_allocationsize_obj_clear(mp_obj_t self_in) {
memorymonitor_allocationsize_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_memorymonitor_allocationsize_clear(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationsize_clear_obj, memorymonitor_allocationsize_obj_clear);
//| bytes_per_block: int = ... //| bytes_per_block: int = ...
//| """Number of bytes per block""" //| """Number of bytes per block"""
//| //|
STATIC mp_obj_t memorymonitor_allocationsize_obj_get_bytes_per_block(mp_obj_t self_in) { STATIC mp_obj_t memorymonitor_allocationsize_obj_get_bytes_per_block(mp_obj_t self_in) {
memorymonitor_allocationsize_obj_t *self = MP_OBJ_TO_PTR(self_in); memorymonitor_allocationsize_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_bool(common_hal_memorymonitor_allocationsize_get_bytes_per_block(self)); return MP_OBJ_NEW_SMALL_INT(common_hal_memorymonitor_allocationsize_get_bytes_per_block(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationsize_get_bytes_per_block_obj, memorymonitor_allocationsize_obj_get_bytes_per_block); MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationsize_get_bytes_per_block_obj, memorymonitor_allocationsize_obj_get_bytes_per_block);

View File

@ -38,6 +38,11 @@ void common_hal_memorymonitor_allocationalarm_construct(memorymonitor_allocation
} }
void common_hal_memorymonitor_allocationalarm_pause(memorymonitor_allocationalarm_obj_t* self) { void common_hal_memorymonitor_allocationalarm_pause(memorymonitor_allocationalarm_obj_t* self) {
// Check to make sure we aren't already paused. We can be if we're exiting from an exception we
// caused.
if (self->previous == NULL) {
return;
}
*self->previous = self->next; *self->previous = self->next;
self->next = NULL; self->next = NULL;
self->previous = NULL; self->previous = NULL;
@ -62,6 +67,10 @@ void memorymonitor_allocationalarms_allocation(size_t block_count) {
// Hold onto next in case we remove the alarm from the list. // Hold onto next in case we remove the alarm from the list.
memorymonitor_allocationalarm_obj_t* next = alarm->next; memorymonitor_allocationalarm_obj_t* next = alarm->next;
if (block_count >= alarm->minimum_block_count) { if (block_count >= alarm->minimum_block_count) {
// Uncomment the breakpoint below if you want to use a C debugger to figure out the C
// call stack for an allocation.
// asm("bkpt");
// Pause now because we may alert when throwing the exception too.
common_hal_memorymonitor_allocationalarm_pause(alarm); common_hal_memorymonitor_allocationalarm_pause(alarm);
alert_count++; alert_count++;
} }
@ -71,3 +80,7 @@ void memorymonitor_allocationalarms_allocation(size_t block_count) {
mp_raise_memorymonitor_AllocationError(translate("Attempt to allocate %d blocks"), block_count); mp_raise_memorymonitor_AllocationError(translate("Attempt to allocate %d blocks"), block_count);
} }
} }
void memorymonitor_allocationalarms_reset(void) {
MP_STATE_VM(active_allocationalarms) = NULL;
}

View File

@ -45,5 +45,6 @@ typedef struct _memorymonitor_allocationalarm_obj_t {
} memorymonitor_allocationalarm_obj_t; } memorymonitor_allocationalarm_obj_t;
void memorymonitor_allocationalarms_allocation(size_t block_count); void memorymonitor_allocationalarms_allocation(size_t block_count);
void memorymonitor_allocationalarms_reset(void);
#endif // MICROPY_INCLUDED_SHARED_MODULE_MEMORYMONITOR_ALLOCATIONALARM_H #endif // MICROPY_INCLUDED_SHARED_MODULE_MEMORYMONITOR_ALLOCATIONALARM_H

View File

@ -85,3 +85,7 @@ void memorymonitor_allocationsizes_track_allocation(size_t block_count) {
as = as->next; as = as->next;
} }
} }
void memorymonitor_allocationsizes_reset(void) {
MP_STATE_VM(active_allocationsizes) = NULL;
}

View File

@ -46,5 +46,6 @@ typedef struct _memorymonitor_allocationsize_obj_t {
} memorymonitor_allocationsize_obj_t; } memorymonitor_allocationsize_obj_t;
void memorymonitor_allocationsizes_track_allocation(size_t block_count); void memorymonitor_allocationsizes_track_allocation(size_t block_count);
void memorymonitor_allocationsizes_reset(void);
#endif // MICROPY_INCLUDED_SHARED_MODULE_MEMORYMONITOR_ALLOCATIONSIZE_H #endif // MICROPY_INCLUDED_SHARED_MODULE_MEMORYMONITOR_ALLOCATIONSIZE_H

View File

@ -32,3 +32,8 @@ void memorymonitor_track_allocation(size_t block_count) {
memorymonitor_allocationalarms_allocation(block_count); memorymonitor_allocationalarms_allocation(block_count);
memorymonitor_allocationsizes_track_allocation(block_count); memorymonitor_allocationsizes_track_allocation(block_count);
} }
void memorymonitor_reset(void) {
memorymonitor_allocationalarms_reset();
memorymonitor_allocationsizes_reset();
}

View File

@ -30,5 +30,6 @@
#include <stddef.h> #include <stddef.h>
void memorymonitor_track_allocation(size_t block_count); void memorymonitor_track_allocation(size_t block_count);
void memorymonitor_reset(void);
#endif // MICROPY_INCLUDED_MEMORYMONITOR___INIT___H #endif // MICROPY_INCLUDED_MEMORYMONITOR___INIT___H