systematically fetch heap mem settings, before allocating buffers so console and web show same results

This commit is contained in:
Proddy
2023-01-11 23:34:27 +01:00
parent 97451d2b7a
commit 3dc2ab54ac
4 changed files with 43 additions and 12 deletions

View File

@@ -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);

View File

@@ -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<EMSESP_JSON_SIZE_MEDIUM> doc;
JsonObject json = doc.to<JsonObject>();
@@ -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);

View File

@@ -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

View File

@@ -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);