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
+11 -1
View File
@@ -494,11 +494,13 @@ const char kWebPanelAppHtml[] PROGMEM = R"HTML(
.metric-grid-4 { grid-template-columns:repeat(4,minmax(0,1fr)); }
.core-grid { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:12px; }
.core-grid .hud-row { align-content:start; }
.core-metrics { grid-template-columns:repeat(4,minmax(0,1fr)); }
.core-metrics { grid-template-columns:repeat(5,minmax(0,1fr)); }
.metric { background:rgba(255,255,255,.45); border:1px solid var(--border); border-radius:12px; padding:10px; }
:root[data-theme="dark"] .metric { background:rgba(0,0,0,.16); }
.metric-label { font-size:11px; color:var(--text-muted); text-transform:uppercase; letter-spacing:.06em; }
.metric-value { margin-top:4px; font-size:16px; font-weight:700; color:var(--text); }
.metric-spread { display:flex; justify-content:space-between; margin-top:4px; }
.metric-spread span { font-size:16px; font-weight:700; color:var(--text); }
.events-table-wrap { overflow-x:auto; border:1px solid var(--border); border-radius:12px; }
.events-table { width:100%; border-collapse:collapse; }
.events-table th, .events-table td { padding:10px 12px; text-align:left; font-size:13px; }
@@ -1457,6 +1459,14 @@ const char kWebPanelAppHtml[] PROGMEM = R"HTML(
<div class="metric-grid core-metrics">
${renderMetric("Uptime", formatDuration(core.uptime_secs))}
${renderMetric("Battery", (core.battery_mv || 0) + " mV")}
${Array.isArray(core.load_avg) ? `<div class="metric">
<div class="metric-label">Load Avg</div>
<div class="metric-spread">
<span>${core.load_avg[0].toFixed(2)}</span>
<span>${core.load_avg[1].toFixed(2)}</span>
<span>${core.load_avg[2].toFixed(2)}</span>
</div>
</div>` : ""}
${renderMetric("Queue", String(core.queue_len ?? 0))}
${renderMetric("Errors", String(core.errors ?? 0))}
</div>