From ca82ed07c982bdb1a1d7b9c262edd7c142709927 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Wed, 3 Feb 2021 20:35:01 +0100 Subject: [PATCH 1/2] Publish more precise Free Heap metric --- tasmota/support_command.ino | 5 +++-- tasmota/support_esp32.ino | 26 +------------------------- tasmota/support_tasmota.ino | 5 +++-- tasmota/xdrv_01_webserver.ino | 9 ++++++--- 4 files changed, 13 insertions(+), 32 deletions(-) diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index 3c26923c5..4a9484a72 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -477,7 +477,8 @@ void CmndStatus(void) } if ((0 == payload) || (4 == payload)) { - Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS4_MEMORY "\":{\"" D_JSON_PROGRAMSIZE "\":%d,\"" D_JSON_FREEMEMORY "\":%d,\"" D_JSON_HEAPSIZE "\":%d,\"" + float freeMem = ((float)ESP_getFreeHeap()) / 1024; + Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS4_MEMORY "\":{\"" D_JSON_PROGRAMSIZE "\":%d,\"" D_JSON_FREEMEMORY "\":%d,\"" D_JSON_HEAPSIZE "\":%1_f,\"" #ifdef ESP32 D_JSON_PSRMAXMEMORY "\":%d,\"" D_JSON_PSRFREEMEMORY "\":%d,\"" #endif // ESP32 @@ -486,7 +487,7 @@ void CmndStatus(void) ",\"" D_JSON_FLASHCHIPID "\":\"%06X\"" #endif // ESP8266 ",\"FlashFrequency\":%d,\"" D_JSON_FLASHMODE "\":%d"), - ESP_getSketchSize()/1024, ESP.getFreeSketchSpace()/1024, ESP_getFreeHeap()/1024, + ESP_getSketchSize()/1024, ESP.getFreeSketchSpace()/1024, &freeMem, #ifdef ESP32 ESP.getPsramSize()/1024, ESP.getFreePsram()/1024, #endif // ESP32 diff --git a/tasmota/support_esp32.ino b/tasmota/support_esp32.ino index 525482974..a17dda4ef 100644 --- a/tasmota/support_esp32.ino +++ b/tasmota/support_esp32.ino @@ -47,29 +47,6 @@ uint32_t ESP_getFreeHeap(void) { return ESP.getFreeHeap(); } -uint32_t ESP_getMaxAllocHeap(void) { -/* - From libraries.rst - ESP.getMaxFreeBlockSize() returns the largest contiguous free RAM block in - the heap, useful for checking heap fragmentation. **NOTE:** Maximum - ``malloc()``able block will be smaller due to memory manager overheads. - - From HeapMetric.ino - ESP.getMaxFreeBlockSize() does not indicate the amount of memory that is - available for use in a single malloc call. It indicates the size of a - contiguous block of (raw) memory before the umm_malloc overhead is removed. - - It should also be pointed out that, if you allow for the needed overhead in - your malloc call, it could still fail in the general case. An IRQ handler - could have allocated memory between the time you call - ESP.getMaxFreeBlockSize() and your malloc call, reducing the available - memory. -*/ - uint32_t free_block_size = ESP.getMaxFreeBlockSize(); - if (free_block_size > 100) { free_block_size -= 100; } - return free_block_size; -} - void ESP_Restart(void) { // ESP.restart(); // This results in exception 3 on restarts on core 2.3.0 ESP.reset(); @@ -431,8 +408,7 @@ uint32_t ESP_getSketchSize(void) { } uint32_t ESP_getFreeHeap(void) { -// return ESP.getFreeHeap(); - return ESP.getMaxAllocHeap(); + return ESP.getFreeHeap(); } uint32_t ESP_getMaxAllocHeap(void) { diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 0761e504d..18217fcbd 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -658,6 +658,7 @@ void MqttShowPWMState(void) void MqttShowState(void) { char stemp1[TOPSZ]; + float freeMem = ((float)ESP_getFreeHeap()) / 1024; ResponseAppendTime(); ResponseAppend_P(PSTR(",\"" D_JSON_UPTIME "\":\"%s\",\"UptimeSec\":%u"), GetUptime().c_str(), UpTime()); @@ -669,8 +670,8 @@ void MqttShowState(void) #endif // USE_ADC_VCC #endif // ESP8266 - ResponseAppend_P(PSTR(",\"" D_JSON_HEAPSIZE "\":%d,\"SleepMode\":\"%s\",\"Sleep\":%u,\"LoadAvg\":%u,\"MqttCount\":%u"), - ESP_getFreeHeap()/1024, GetTextIndexed(stemp1, sizeof(stemp1), Settings.flag3.sleep_normal, kSleepMode), // SetOption60 - Enable normal sleep instead of dynamic sleep + ResponseAppend_P(PSTR(",\"" D_JSON_HEAPSIZE "\":%1_f,\"SleepMode\":\"%s\",\"Sleep\":%u,\"LoadAvg\":%u,\"MqttCount\":%u"), + &freeMem, GetTextIndexed(stemp1, sizeof(stemp1), Settings.flag3.sleep_normal, kSleepMode), // SetOption60 - Enable normal sleep instead of dynamic sleep TasmotaGlobal.sleep, TasmotaGlobal.loop_load_avg, MqttConnectCount()); for (uint32_t i = 1; i <= TasmotaGlobal.devices_present; i++) { diff --git a/tasmota/xdrv_01_webserver.ino b/tasmota/xdrv_01_webserver.ino index 51dfc11f7..047d97984 100644 --- a/tasmota/xdrv_01_webserver.ino +++ b/tasmota/xdrv_01_webserver.ino @@ -2063,7 +2063,7 @@ void HandleInformation(void) char stopic[TOPSZ]; - int freeMem = ESP_getFreeHeap(); + float freeMem = ((float)ESP_getFreeHeap()) / 1024; WSContentStart_P(PSTR(D_INFORMATION)); // Save 1k of code space replacing table html with javascript replace codes @@ -2178,13 +2178,16 @@ void HandleInformation(void) WSContentSend_P(PSTR("}1" D_PROGRAM_FLASH_SIZE "}2%d kB"), ESP.getFlashChipSize() / 1024); 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); - WSContentSend_P(PSTR("}1" D_FREE_MEMORY "}2%d kB"), freeMem / 1024); #ifdef ESP32 + int32_t freeMaxMem = 100 - (int32_t)(ESP_getMaxAllocHeap() * 100 / ESP_getFreeHeap()); + WSContentSend_P(PSTR("}1" D_FREE_MEMORY "}2%1_f kB (frag. %d%%)"), &freeMem, freeMaxMem); if (psramFound()) { 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); } -#endif +#else // ESP32 + WSContentSend_P(PSTR("}1" D_FREE_MEMORY "}2%1_f kB"), &freeMem); +#endif // ESP32 WSContentSend_P(PSTR("")); WSContentSend_P(HTTP_SCRIPT_INFO_END); From bf26ba3427dbc70fed9bdee163da3385f4d4bc86 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Wed, 3 Feb 2021 20:46:20 +0100 Subject: [PATCH 2/2] Reduce code size --- tasmota/support_command.ino | 2 +- tasmota/support_esp32.ino | 8 ++++++++ tasmota/support_tasmota.ino | 2 +- tasmota/xdrv_01_webserver.ino | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index 4a9484a72..14e327046 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -477,7 +477,7 @@ void CmndStatus(void) } if ((0 == payload) || (4 == payload)) { - float freeMem = ((float)ESP_getFreeHeap()) / 1024; + float freeMem = ESP_getFreeHeap1024(); Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS4_MEMORY "\":{\"" D_JSON_PROGRAMSIZE "\":%d,\"" D_JSON_FREEMEMORY "\":%d,\"" D_JSON_HEAPSIZE "\":%1_f,\"" #ifdef ESP32 D_JSON_PSRMAXMEMORY "\":%d,\"" D_JSON_PSRFREEMEMORY "\":%d,\"" diff --git a/tasmota/support_esp32.ino b/tasmota/support_esp32.ino index a17dda4ef..c2206dd89 100644 --- a/tasmota/support_esp32.ino +++ b/tasmota/support_esp32.ino @@ -47,6 +47,10 @@ uint32_t ESP_getFreeHeap(void) { return ESP.getFreeHeap(); } +float ESP_getFreeHeap1024(void) { + return ((float)ESP_getFreeHeap()) / 1024; +} + void ESP_Restart(void) { // ESP.restart(); // This results in exception 3 on restarts on core 2.3.0 ESP.reset(); @@ -411,6 +415,10 @@ uint32_t ESP_getFreeHeap(void) { return ESP.getFreeHeap(); } +float ESP_getFreeHeap1024(void) { + return ((float)ESP_getFreeHeap()) / 1024; +} + uint32_t ESP_getMaxAllocHeap(void) { // largest block of heap that can be allocated at once uint32_t free_block_size = ESP.getMaxAllocHeap(); diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 18217fcbd..5826e2327 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -658,7 +658,7 @@ void MqttShowPWMState(void) void MqttShowState(void) { char stemp1[TOPSZ]; - float freeMem = ((float)ESP_getFreeHeap()) / 1024; + float freeMem = ESP_getFreeHeap1024(); ResponseAppendTime(); ResponseAppend_P(PSTR(",\"" D_JSON_UPTIME "\":\"%s\",\"UptimeSec\":%u"), GetUptime().c_str(), UpTime()); diff --git a/tasmota/xdrv_01_webserver.ino b/tasmota/xdrv_01_webserver.ino index 047d97984..12ee2d6cd 100644 --- a/tasmota/xdrv_01_webserver.ino +++ b/tasmota/xdrv_01_webserver.ino @@ -2063,7 +2063,7 @@ void HandleInformation(void) char stopic[TOPSZ]; - float freeMem = ((float)ESP_getFreeHeap()) / 1024; + float freeMem = ESP_getFreeHeap1024(); WSContentStart_P(PSTR(D_INFORMATION)); // Save 1k of code space replacing table html with javascript replace codes