Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "buzz/buzz.h"
#include "configuration.h"
#include "main.h"
#include "memory/MemAudit.h"
#include "meshUtils.h"
#include "power/PowerHAL.h"
#include "power/SGM41562.h"
Expand Down Expand Up @@ -1236,12 +1237,23 @@ void Power::logHeapUsage()
// The first line has no earlier sample to difference against
const int32_t delta = lastHeapLogTime ? (int32_t)(heapFree - lastHeapLogFree) : 0;

// min only ever falls: one step down is a transient alloc, repeated new lows are a leak.
// A steady min with a shrinking largest block is fragmentation. Empty where unsupported.
char detail[64] = "";
const uint32_t minFree = memGet.getMinFreeHeap();
const uint32_t maxAlloc = memGet.getMaxAllocHeap();
if (minFree || maxAlloc)
snprintf(detail, sizeof(detail), ", min %u, largest block %u", minFree, maxAlloc);

const uint32_t psramTotal = memGet.getPsramSize();
if (psramTotal)
LOG_INFO("Heap: %u/%u bytes free (%d since last), PSRAM: %u/%u bytes free", heapFree, heapTotal, delta,
LOG_INFO("Heap: %u/%u bytes free (%d since last)%s, PSRAM: %u/%u bytes free", heapFree, heapTotal, delta, detail,
memGet.getFreePsram(), psramTotal);
else
LOG_INFO("Heap: %u/%u bytes free (%d since last)", heapFree, heapTotal, delta);
LOG_INFO("Heap: %u/%u bytes free (%d since last)%s", heapFree, heapTotal, delta, detail);

// Which tagged subsystem moved since boot
memaudit::logBreakdown("periodic");

lastHeapLogFree = heapFree;
lastHeapLogTime = millis();
Expand Down
26 changes: 26 additions & 0 deletions src/memGet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ uint32_t MemGet::getHeapSize()
#endif
}

/**
* Returns the lowest the free heap has ever been since boot.
* @return uint32_t Low watermark in bytes, or 0 if the platform can't report it.
*/
uint32_t MemGet::getMinFreeHeap()
{
#ifdef ARCH_ESP32
return ESP.getMinFreeHeap();
#else
return 0;
#endif
}

/**
* Returns the largest contiguous block malloc() could still return.
* @return uint32_t Block size in bytes, or 0 if the platform can't report it.
*/
uint32_t MemGet::getMaxAllocHeap()
{
#ifdef ARCH_ESP32
return ESP.getMaxAllocHeap();
#else
return 0;
#endif
}

/**
* Returns the amount of free psram memory in bytes.
*
Expand Down
4 changes: 4 additions & 0 deletions src/memGet.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class MemGet
public:
uint32_t getFreeHeap();
uint32_t getHeapSize();
// Lowest free heap seen since boot, or 0 where the platform can't report it
uint32_t getMinFreeHeap();
// Largest block malloc() could still return, or 0 where the platform can't report it
uint32_t getMaxAllocHeap();
uint32_t getFreePsram();
uint32_t getPsramSize();
};
Expand Down
Loading