ESP32: add CPUUsageTracker – core-0 load averages (1/5/15 min)

Track CPU utilisation on core 0 using a FreeRTOS tick hook that
increments per-tick idle/busy counters, sampled every 5s by an
esp_timer callback.  Exponential moving averages with Linux-style
time constants (1 / 5 / 15 min) are maintained in software:

  DECAY1  = exp(-5/60)  ≈ 0.9200  (1-minute window)
  DECAY5  = exp(-5/300) ≈ 0.9835  (5-minute window)
  DECAY15 = exp(-5/900) ≈ 0.9945  (15-minute window)

Core 1 is excluded intentionally: it runs the Arduino loopTask at
100% load for LoRa packet processing, so its figure is always 1.0
and carries no diagnostic value.  All other tasks are pinned to
core 0 by task_pinning.c, so core-0 load reflects the true system
utilisation.

New files:
  arch/esp32/CPUUsageTracker.h   – class declaration
  arch/esp32/CPUUsageTracker.cpp – tick hook + esp_timer sampling

Integration:
  MyMesh::begin() calls _cpu_tracker.begin() on ESP32 builds.
  formatStatsReply() emits "load_avg":[<1m>,<5m>,<15m>] in the
  compact stats JSON payload.
  formatWebStatsSummaryJson() adds the same field to the web-panel
  stats endpoint under core.load_avg.
  The web-panel HUD gains a "Load Avg" metric tile showing all
  three values side-by-side; the core-metrics grid is widened from
  4 to 5 columns.  The tile is rendered conditionally so older
  firmware (or non-ESP32 builds) that omit the field degrade
  gracefully.
  CPUUsageTracker.cpp is added to the esp32_base build_src_filter
  in platformio.ini.
This commit is contained in:
Valentin V. Bartenev
2026-05-09 17:26:27 +03:00
parent ce510738d5
commit a6f01d544e
6 changed files with 114 additions and 3 deletions
+7 -2
View File
@@ -1269,6 +1269,9 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
void MyMesh::begin(FILESYSTEM *fs, ArchiveStorage* archive) {
mesh::Mesh::begin();
#if defined(ESP32)
_cpu_tracker.begin();
#endif
_fs = fs;
_archive = archive;
last_millis = millis();
@@ -1529,9 +1532,10 @@ void MyMesh::removeNeighbor(const uint8_t *pubkey, int key_len) {
void MyMesh::formatStatsReply(char *reply, size_t reply_size) {
snprintf(reply,
reply_size,
"{\"battery_mv\":%u,\"uptime_secs\":%u,\"errors\":%u,\"queue_len\":%u}",
"{\"battery_mv\":%u,\"uptime_secs\":%u,\"load_avg\":[%.2f,%.2f,%.2f],\"errors\":%u,\"queue_len\":%u}",
getBatteryMilliVolts(true),
_ms->getMillis() / 1000,
_cpu_tracker.getLoadAvg1(), _cpu_tracker.getLoadAvg5(), _cpu_tracker.getLoadAvg15(),
_err_flags,
_mgr->getOutboundTotal());
}
@@ -2527,7 +2531,7 @@ bool MyMesh::formatWebStatsSummaryJson(char* reply, size_t reply_size) {
"\"archive\":{\"logical\":\"%s\",\"available\":%s,\"path\":\"%s\",\"type\":\"%s\","
"\"total_bytes\":%llu,\"used_bytes\":%llu},"
"\"core\":{\"battery_mv\":%u,\"battery_pct\":%d,\"battery_display_pct\":%d,\"battery_min_mv\":%u,\"battery_max_mv\":%u,"
"\"uptime_secs\":%lu,\"errors\":%u,\"queue_len\":%u,"
"\"uptime_secs\":%lu,\"load_avg\":[%.2f,%.2f,%.2f],\"errors\":%u,\"queue_len\":%u,"
"\"external_power\":%s,\"charging\":%s,\"vbus\":%s},"
"\"radio\":{\"noise_floor\":%d,\"last_rssi\":%.2f,\"last_snr\":%.2f,\"tx_air_secs\":%lu,\"rx_air_secs\":%lu},"
"\"packets\":{\"recv\":%u,\"sent\":%u,\"flood_tx\":%u,\"direct_tx\":%u,\"flood_rx\":%u,\"direct_rx\":%u,"
@@ -2560,6 +2564,7 @@ bool MyMesh::formatWebStatsSummaryJson(char* reply, size_t reply_size) {
battery_min_mv,
battery_max_mv,
static_cast<unsigned long>(uptime_millis / 1000),
_cpu_tracker.getLoadAvg1(), _cpu_tracker.getLoadAvg5(), _cpu_tracker.getLoadAvg15(),
_err_flags,
static_cast<unsigned>(_mgr->getOutboundTotal()),
board.isExternalPowered() ? "true" : "false",