Skip to content

Commit

Permalink
Monitor and log memory heap fragmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dkerr64 committed Jul 28, 2024
1 parent 81fc078 commit 2ab9df2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
32 changes: 19 additions & 13 deletions src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,29 @@ void printSavedLog(Print &outputDev)
// These are defined in the linker script, and filled in by the elf2bin.py util
extern "C" uint32_t __crc_len;
extern "C" uint32_t __crc_val;
// Memory stats from the web.cpp file
extern "C" uint32_t free_heap;
extern "C" uint32_t max_block;
extern "C" uint8_t max_frag;
void printMessageLog(Print &outputDev)
{
ESP.getHeapStats(&free_heap, &max_block, &max_frag);
outputDev.write("Firmware Version: ");
outputDev.write(AUTO_VERSION);
outputDev.println();
outputDev.write("Flash CRC: 0x");
outputDev.println(__crc_val, 16);
outputDev.write("Flash Length: ");
outputDev.println(__crc_len);
outputDev.write("Free heap: ");
outputDev.println(free_heap);
outputDev.write("Max malloc size: ");
outputDev.println(max_block);
outputDev.write("Fragmentation pct: ");
outputDev.println(max_frag);
outputDev.println();
if (msgBuffer)
{
static char buf[20] = "";
outputDev.write("Firmware Version: ");
outputDev.write(AUTO_VERSION);
outputDev.println();
outputDev.write("Flash CRC: ");
sprintf(buf, " 0x%08X", __crc_val);
outputDev.write(buf);
outputDev.println();
outputDev.write("Flash Length: ");
sprintf(buf, "0x%X (%u)", __crc_len, __crc_len);
outputDev.write(buf);
outputDev.println();
outputDev.println();
if (msgBuffer->wrapped != 0)
{
outputDev.write(&msgBuffer->buffer[msgBuffer->head], sizeof(msgBuffer->buffer) - msgBuffer->head);
Expand Down
39 changes: 30 additions & 9 deletions src/web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ const char www_pw_required_file[] = "www_pw_required_file";
// Control automatic reboot
uint32_t rebootSeconds; // seconds between reboots
const char system_reboot_timer[] = "system_reboot_timer";
uint32_t min_heap = 0xffffffff;
// Track our memory usage
Ticker memoryMonitor;
uint32_t free_heap = 65535;
uint32_t min_heap = 65535;
uint32_t max_block = 65535;
uint8_t max_frag = 0;
uint32_t min_block = 65535;
uint8_t min_frag = 0;

// Control WiFi physical layer mode
WiFiPhyMode_t wifiPhyMode = (WiFiPhyMode_t)0;
Expand Down Expand Up @@ -281,12 +288,21 @@ void web_loop()
return;
}
server.handleClient();
}

uint32_t free_heap = system_get_free_heap_size();
void memory_ticker()
{
ESP.getHeapStats(&free_heap, &max_block, &max_frag);
if (free_heap < min_heap)
{
min_heap = free_heap;
RINFO("Free HEAP dropped to %d", min_heap);
RINFO("Minimum free heap dropped to %d", min_heap);
}
if (max_block < min_block)
{
min_block = max_block;
min_frag = max_frag;
RINFO("Maximum malloc block size dropped to %d (%d%% fragmented)", min_block, min_frag);
}
}

Expand Down Expand Up @@ -351,6 +367,8 @@ void setup_web()
subscription[i].clientIP = INADDR_NONE;
subscription[i].clientUUID.clear();
}
// monitor memory heap once a second...
memoryMonitor.attach_scheduled(1.0, memory_ticker);
RINFO("HTTP server started");
return;
}
Expand Down Expand Up @@ -557,12 +575,13 @@ void handle_status()
ADD_BOOL(json, "garageObstructed", garage_door.obstructed);
ADD_BOOL(json, "passwordRequired", passwordReq);
ADD_INT(json, "rebootSeconds", rebootSeconds);
uint32_t free_heap = system_get_free_heap_size();
if (free_heap < min_heap)
min_heap = free_heap;
ADD_INT(json, "freeHeap", free_heap);
ADD_INT(json, "minHeap", min_heap);
ADD_INT(json, "minStack", ESP.getFreeContStack());
ADD_INT(json, "maxBlock", max_block);
ADD_INT(json, "maxFrag", max_frag);
ADD_INT(json, "minBlock", min_block);
ADD_INT(json, "minFrag", min_frag);
ADD_INT(json, "crashCount", crashCount);
ADD_INT(json, "wifiPhyMode", wifiPhyMode);
ADD_INT(json, "wifiPower", wifiPower);
Expand Down Expand Up @@ -781,9 +800,6 @@ void SSEheartbeat(SSESubscription *s)
// Serial.printf("Heartbeat\n");
if (!s)
return;
uint32_t free_heap = system_get_free_heap_size();
if (free_heap < min_heap)
min_heap = free_heap;

if (!(s->clientIP))
return;
Expand Down Expand Up @@ -814,6 +830,11 @@ void SSEheartbeat(SSESubscription *s)
ADD_INT(json, "upTime", millis());
ADD_INT(json, "freeHeap", free_heap);
ADD_INT(json, "minHeap", min_heap);
ADD_INT(json, "minStack", ESP.getFreeContStack());
ADD_INT(json, "maxBlock", max_block);
ADD_INT(json, "maxFrag", max_frag);
ADD_INT(json, "minBlock", min_block);
ADD_INT(json, "minFrag", min_frag);
ADD_STR(json, "wifiRSSI", (std::to_string(WiFi.RSSI()) + " dBm").c_str());
ADD_BOOL(json, "checkFlashCRC", flashCRC);
END_JSON(json);
Expand Down
12 changes: 8 additions & 4 deletions src/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,14 @@
</p>
<br>
<table style="border-collapse: collapse">
<tr>
<td style="width: 30%; padding: 0px">freeHeap:&nbsp;<span id="freeHeap">0</span></td>
<td style="width: 30%; padding: 0px">minHeap:&nbsp;<span id="minHeap">0</span></td>
<td style="width: 30%; padding: 0px">minStack:&nbsp;<span id="minStack">0</span></td>
<tr id="freeMemRow">
<td colspan="2" style="width: 100%; padding: 0px">freeHeap:&nbsp;<span id="freeHeap">0</span>&nbsp;</td>
<td colspan="2" style="width: 100%; padding: 0px; text-align: center;">minHeap:&nbsp;<span id="minHeap">0</span>&nbsp;</td>
<td colspan="2" style="width: 100%; padding: 0px; text-align: right;">minStack:&nbsp;<span id="minStack">0</span>&nbsp;</td>
</tr>
<tr id="memFragRow" style="display: none;">
<td colspan="3" style="width: 100%; padding: 0px">maxBlock:&nbsp;<span id="maxBlock">0</span>&nbsp;(<span id="maxFrag">0</span>%)&nbsp;</td>
<td colspan="3" style="width: 100%; padding: 0px; text-align: right;">minBlock:&nbsp;<span id="minBlock">0</span>&nbsp;(<span id="minFrag">0</span>%)&nbsp;</td>
</tr>
</table>
<table style="border-collapse: collapse">
Expand Down
4 changes: 4 additions & 0 deletions src/www/status.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"rebootSeconds": 0,
"freeHeap": 20000,
"minHeap": 10000,
"maxBlock": 19000,
"minBlock": 18000,
"maxFrag": 5,
"minFrag": 10,
"minStack": 2000,
"crashCount": 1,
"wifiPhyMode": 0,
Expand Down

0 comments on commit 2ab9df2

Please sign in to comment.