Files
MicrOBU/obu-firmware/main/heap_diagnostics.cpp
T

95 lines
3.9 KiB
C++
Raw Normal View History

// a station build that runs out of heap resets the board without a word on the framed log (the panic text
// goes to the raw console only). The failed-allocation hook records each failure in RTC memory that survives a
// software reset, and the next boot reports it with the reset reason. The hook must not allocate: it only writes
// the record and prints with esp_rom_printf.
#include "heap_diagnostics.hpp"
#include <esp_attr.h>
#include <esp_heap_caps.h>
#include <esp_log.h>
#include <esp_rom_sys.h>
#include <esp_system.h>
#include <cstdint>
namespace microbu::heap_diagnostics {
namespace {
const char* TAG = "heap";
constexpr std::uint32_t kMagic = 0x48454150; // "HEAP"
struct Record {
std::uint32_t magic;
std::uint32_t failures; // failed allocations since the record was last reported
std::uint32_t size; // of the last failed allocation
std::uint32_t caps;
std::uint32_t free; // free heap of those caps at that moment
std::uint32_t largest; // largest free block of those caps at that moment
char function[32];
std::uint32_t first_size, first_free, first_largest; // the first failure since the last report
};
RTC_NOINIT_ATTR Record record;
volatile unsigned failed_since_boot = 0;
void on_failed_allocation(size_t size, uint32_t caps, const char* function_name) {
failed_since_boot = failed_since_boot + 1;
if (record.magic != kMagic) { record = {}; record.magic = kMagic; }
if (record.failures++ == 0) {
record.first_size = size;
record.first_free = heap_caps_get_free_size(caps);
record.first_largest = heap_caps_get_largest_free_block(caps);
}
record.size = size;
record.caps = caps;
record.free = heap_caps_get_free_size(caps);
record.largest = heap_caps_get_largest_free_block(caps);
std::size_t i = 0;
for (; function_name && function_name[i] && i + 1 < sizeof(record.function); ++i) record.function[i] = function_name[i];
record.function[i] = '\0';
esp_rom_printf("heap: allocation of %u bytes (caps 0x%x) failed in %s: free %u, largest block %u\n",
unsigned(size), unsigned(caps), function_name ? function_name : "?", unsigned(record.free),
unsigned(record.largest));
}
const char* reason_name(esp_reset_reason_t reason) {
switch (reason) {
case ESP_RST_POWERON: return "power-on";
case ESP_RST_EXT: return "external pin";
case ESP_RST_SW: return "software (esp_restart)";
case ESP_RST_PANIC: return "panic/exception (abort, std::terminate)";
case ESP_RST_INT_WDT: return "interrupt watchdog";
case ESP_RST_TASK_WDT: return "task watchdog";
case ESP_RST_WDT: return "other watchdog";
case ESP_RST_DEEPSLEEP: return "deep sleep";
case ESP_RST_BROWNOUT: return "brownout";
case ESP_RST_SDIO: return "SDIO";
case ESP_RST_USB: return "USB peripheral";
case ESP_RST_JTAG: return "JTAG";
default: return "unknown";
}
}
} // namespace
unsigned failed_allocations() { return failed_since_boot; }
void install() {
heap_caps_register_failed_alloc_callback(on_failed_allocation);
}
void report_boot() {
const auto reason = esp_reset_reason();
ESP_LOGI(TAG, "reset reason: %s (%d)", reason_name(reason), int(reason));
if (reason == ESP_RST_POWERON || record.magic != kMagic) { // RTC memory is random after power-on
record = {};
record.magic = kMagic;
return;
}
if (record.failures) {
ESP_LOGW(TAG, "before this reset %u allocation(s) failed; first: %u bytes, free %u, largest block %u; "
"last: %u bytes (caps 0x%x) in %s, free %u, largest block %u",
unsigned(record.failures), unsigned(record.first_size), unsigned(record.first_free),
unsigned(record.first_largest), unsigned(record.size), unsigned(record.caps), record.function,
unsigned(record.free), unsigned(record.largest));
}
record.failures = 0;
}
} // namespace microbu::heap_diagnostics