diff --git a/lib/framework/SystemStatus.cpp b/lib/framework/SystemStatus.cpp index b8323a04f..61e347211 100644 --- a/lib/framework/SystemStatus.cpp +++ b/lib/framework/SystemStatus.cpp @@ -12,13 +12,16 @@ SystemStatus::SystemStatus(AsyncWebServer * server, SecurityManager * securityMa } void SystemStatus::systemStatus(AsyncWebServerRequest * request) { + emsesp::EMSESP::system_.refreshHeapMem(); // refresh free heap and max alloc heap + AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_ESP_STATUS_SIZE); JsonObject root = response->getRoot(); + root["emsesp_version"] = EMSESP_APP_VERSION; root["esp_platform"] = EMSESP_PLATFORM; root["cpu_freq_mhz"] = ESP.getCpuFreqMHz(); - root["max_alloc_heap"] = ESP.getMaxAllocHeap() / 1024; - root["free_heap"] = ESP.getFreeHeap() / 1024; + root["max_alloc_heap"] = emsesp::EMSESP::system_.getMaxAllocMem(); + root["free_heap"] = emsesp::EMSESP::system_.getHeapMem(); root["sdk_version"] = ESP.getSdkVersion(); root["flash_chip_size"] = ESP.getFlashChipSize() / 1024; root["flash_chip_speed"] = ESP.getFlashChipSpeed(); @@ -33,7 +36,7 @@ void SystemStatus::systemStatus(AsyncWebServerRequest * request) { root["free_psram"] = ESP.getFreePsram() / 1024; } const esp_partition_t * factory_partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL); - root["has_loader"] = factory_partition != NULL; + root["has_loader"] = factory_partition != NULL; response->setLength(); request->send(response); diff --git a/src/system.cpp b/src/system.cpp index 969956567..58d72ce87 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -55,9 +55,11 @@ static constexpr uint8_t NUM_LANGUAGES = sizeof(languages) / sizeof(const char * uuid::log::Logger System::logger_{F_(system), uuid::log::Facility::KERN}; // init statics -PButton System::myPButton_; -bool System::restart_requested_ = false; -bool System::test_set_all_active_ = false; +PButton System::myPButton_; +bool System::restart_requested_ = false; +bool System::test_set_all_active_ = false; +uint32_t System::max_alloc_mem_; +uint32_t System::heap_mem_; // find the index of the language // 0 = EN, 1 = DE, etc... @@ -387,10 +389,13 @@ void System::start() { setCpuFrequencyMhz(160); #endif } + + // get current memory values fstotal_ = LittleFS.totalBytes() / 1024; // read only once, it takes 500 ms to read psram_ = ESP.getPsramSize() / 1024; appused_ = ESP.getSketchSize() / 1024; appfree_ = ESP.getFreeSketchSpace() / 1024 - appused_; + refreshHeapMem(); // refresh free heap and max alloc heap #endif EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { @@ -621,8 +626,8 @@ bool System::heartbeat_json(JsonObject & output) { } #ifndef EMSESP_STANDALONE - output["freemem"] = ESP.getFreeHeap() / 1024; // kilobytes - output["max_alloc"] = ESP.getMaxAllocHeap() / 1024; // kilobytes + output["freemem"] = getHeapMem(); + output["max_alloc"] = getMaxAllocMem(); #endif #ifndef EMSESP_STANDALONE @@ -643,6 +648,8 @@ void System::send_heartbeat() { return; } + refreshHeapMem(); // refresh free heap and max alloc heap + StaticJsonDocument doc; JsonObject json = doc.to(); @@ -879,13 +886,15 @@ void System::show_users(uuid::console::Shell & shell) { } void System::show_system(uuid::console::Shell & shell) { + refreshHeapMem(); // refresh free heap and max alloc heap + shell.println("System:"); shell.printfln(" Board profile: %s", board_profile().c_str()); shell.printfln(" Uptime: %s", uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3).c_str()); #ifndef EMSESP_STANDALONE shell.printfln(" SDK version: %s", ESP.getSdkVersion()); shell.printfln(" CPU frequency: %lu MHz", ESP.getCpuFreqMHz()); - shell.printfln(" Free heap/Max alloc: %lu KB / %lu KB", ((uint32_t)ESP.getFreeHeap() / 1024), (ESP.getMaxAllocHeap() / 1024)); + shell.printfln(" Free heap/Max alloc: %lu KB / %lu KB", getHeapMem(), getMaxAllocMem()); shell.printfln(" App used/free: %lu KB / %lu KB", appUsed(), appFree()); uint32_t FSused = LittleFS.usedBytes() / 1024; shell.printfln(" FS used/free: %lu KB / %lu KB", FSused, FStotal() - FSused); @@ -1140,9 +1149,9 @@ bool System::command_info(const char * value, const int8_t id, JsonObject & outp node["uptime"] = uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3); node["uptime (seconds)"] = uuid::get_uptime_sec(); #ifndef EMSESP_STANDALONE - node["free mem"] = ESP.getFreeHeap() / 1024; // kilobytes - node["max alloc"] = ESP.getMaxAllocHeap() / 1024; // kilobytes - node["free app"] = EMSESP::system_.appFree(); // kilobytes + node["free mem"] = getHeapMem(); + node["max alloc"] = getMaxAllocMem(); + node["free app"] = EMSESP::system_.appFree(); // kilobytes #endif node["reset reason"] = EMSESP::system_.reset_reason(0) + " / " + EMSESP::system_.reset_reason(1); diff --git a/src/system.h b/src/system.h index 353820f7e..9078e0745 100644 --- a/src/system.h +++ b/src/system.h @@ -230,6 +230,20 @@ class System { return appused_; } + // memory in kb + static uint32_t getMaxAllocMem() { + return max_alloc_mem_; + } + static uint32_t getHeapMem() { + return heap_mem_; + } + static void refreshHeapMem() { +#ifndef EMSESP_STANDALONE + max_alloc_mem_ = ESP.getMaxAllocHeap() / 1024; + heap_mem_ = ESP.getFreeHeap() / 1024; +#endif + } + static bool test_set_all_active() { return test_set_all_active_; } @@ -241,6 +255,8 @@ class System { static uuid::log::Logger logger_; static bool restart_requested_; static bool test_set_all_active_; // force all entities in a device to have a value + static uint32_t max_alloc_mem_; + static uint32_t heap_mem_; // button static PButton myPButton_; // PButton instance diff --git a/src/web/WebAPIService.cpp b/src/web/WebAPIService.cpp index 666c76467..5f1c38d6f 100644 --- a/src/web/WebAPIService.cpp +++ b/src/web/WebAPIService.cpp @@ -100,6 +100,9 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject & input) { } } + // capture current heap memory before allocating the large return buffer + emsesp::EMSESP::system_.refreshHeapMem(); + // output json buffer size_t buffer = EMSESP_JSON_SIZE_XXXLARGE; auto * response = new PrettyAsyncJsonResponse(false, buffer);