Fix negative heap fragmentation

This commit is contained in:
Theo Arends 2021-10-08 11:52:50 +02:00
parent 3bb9d4854f
commit eaeacbfa93
4 changed files with 19 additions and 17 deletions

View File

@ -2422,10 +2422,8 @@ void AddLogData(uint32_t loglevel, const char* log_data, const char* log_data_pa
snprintf_P(mxtime, sizeof(mxtime), PSTR("%s-%03d"),
mxtime, ESP_getFreeHeap1024());
#else
uint32_t freemem = ESP_getFreeHeap();
int32_t free_maxmem = 100 - (int32_t)(ESP_getMaxAllocHeap() * 100 / freemem);
snprintf_P(mxtime, sizeof(mxtime), PSTR("%s-%03d/%02d"),
mxtime, freemem / 1024, free_maxmem);
mxtime, ESP_getFreeHeap1024(), ESP_getHeapFragmentation());
#endif
}
strcat(mxtime, " ");

View File

@ -398,6 +398,12 @@ uint32_t ESP_getMaxAllocHeap(void) {
return free_block_size;
}
int32_t ESP_getHeapFragmentation(void) {
int32_t free_maxmem = 100 - (int32_t)(ESP_getMaxAllocHeap() * 100 / ESP_getFreeHeap());
if (free_maxmem < 0) { free_maxmem = 0; }
return free_maxmem;
}
void ESP_Restart(void) {
ESP.restart();
}

View File

@ -2439,8 +2439,7 @@ void HandleInformation(void)
WSContentSend_P(PSTR("}1" D_PROGRAM_SIZE "}2%d kB"), ESP_getSketchSize() / 1024);
WSContentSend_P(PSTR("}1" D_FREE_PROGRAM_SPACE "}2%d kB"), ESP.getFreeSketchSpace() / 1024);
#ifdef ESP32
int32_t freeMaxMem = 100 - (int32_t)(ESP_getMaxAllocHeap() * 100 / ESP_getFreeHeap());
WSContentSend_PD(PSTR("}1" D_FREE_MEMORY "}2%1_f kB (" D_FRAGMENTATION " %d%%)"), &freemem, freeMaxMem);
WSContentSend_PD(PSTR("}1" D_FREE_MEMORY "}2%1_f kB (" D_FRAGMENTATION " %d%%)"), &freemem, ESP_getHeapFragmentation());
if (UsePSRAM()) {
WSContentSend_P(PSTR("}1" D_PSR_MAX_MEMORY "}2%d kB"), ESP.getPsramSize() / 1024);
WSContentSend_P(PSTR("}1" D_PSR_FREE_MEMORY "}2%d kB"), ESP.getFreePsram() / 1024);

View File

@ -27,7 +27,7 @@ const uint32_t BERRY_MAX_LOGS = 16; // max number of print output recorded whe
/*********************************************************************************************\
* Return C callback from index
*
*
\*********************************************************************************************/
extern "C" {
int32_t l_get_cb(struct bvm *vm);
@ -47,11 +47,11 @@ extern "C" {
/*********************************************************************************************\
* Native functions mapped to Berry functions
*
*
* log(msg:string [,log_level:int]) ->nil
*
*
* import tasmota
*
*
* tasmota.get_free_heap() -> int
* tasmota.publish(topic:string, payload:string[, retain:bool]) -> nil
* tasmota.cmd(command:string) -> string
@ -59,12 +59,12 @@ extern "C" {
* tasmota.millis([delay:int]) -> int
* tasmota.time_reached(timer:int) -> bool
* tasmota.yield() -> nil
*
*
* tasmota.get_light([index:int = 0]) -> map
* tasmota.get_power([index:int = 0]) -> bool
* tasmota.set_power(idx:int, power:bool) -> bool or nil
* tasmota.set_light(idx:int, values:map) -> map
*
*
\*********************************************************************************************/
extern "C" {
// Berry: `tasmota.publish(topic, payload [,retain]) -> nil``
@ -97,7 +97,7 @@ extern "C" {
}
be_raise(vm, kTypeError, nullptr);
}
// Berry: `tasmota.publish_result(payload:string, subtopic:string) -> nil``
//
int32_t l_publish_result(struct bvm *vm);
@ -201,8 +201,7 @@ extern "C" {
map_insert_int(vm, "program", ESP_getSketchSize() / 1024);
map_insert_int(vm, "program_free", ESP.getFreeSketchSpace() / 1024);
map_insert_int(vm, "heap_free", ESP_getFreeHeap() / 1024);
int32_t freeMaxMem = 100 - (int32_t)(ESP_getMaxAllocHeap() * 100 / ESP_getFreeHeap());
map_insert_int(vm, "frag", freeMaxMem);
map_insert_int(vm, "frag", ESP_getHeapFragmentation());
if (UsePSRAM()) {
map_insert_int(vm, "psram", ESP.getPsramSize() / 1024);
map_insert_int(vm, "psram_free", ESP.getFreePsram() / 1024);
@ -360,7 +359,7 @@ extern "C" {
ResponseCmndDone();
be_return_nil(vm);
}
int32_t l_respCmndError(bvm *vm);
int32_t l_respCmndError(bvm *vm) {
ResponseCmndError();
@ -488,9 +487,9 @@ extern "C" {
/*********************************************************************************************\
* Native functions mapped to Berry functions
*
*
* log(msg:string [,log_level:int]) ->nil
*
*
\*********************************************************************************************/
extern "C" {
// Berry: `log(msg:string [,log_level:int]) ->nil`