Redesign approach for CPU usage tracking
The previous implementation maintained three exponential moving averages (1/5/15-minute) of CPU busy-fraction, sampled every 5 seconds. This had two problems: 1. Wrong semantics: the metric was labelled "load_avg" (a Unix concept measuring run-queue depth), but the tracker actually measures CPU busy-time as a fraction of FreeRTOS ticks — i.e. utilization, not load. The name was misleading. 2. Poor responsiveness: the 1-minute EMA (DECAY = exp(-5/60) ≈ 0.920) has a ~60-second time constant. On a live web panel, this made the metric appear frozen, especially during short bursts of activity (e.g. an HTTP request from the panel itself). This commit replaces the EMA with a 64-sample simple moving average (SMA) over a compact uint8_t circular buffer. The sample interval is 60 s / 64 = 937,500 µs, so the window covers exactly 60 seconds: Algorithm: - Every 937,500 µs (= 60 s / 64), the FreeRTOS tick delta (busy vs idle ticks on core 0) is computed and stored as a uint8_t (0–255 = 0–100% utilization). - The circular buffer holds 64 samples, spanning exactly 60 seconds (64 × 937,500 µs = 60,000,000 µs). The timer interval is derived directly from the window size: 60 000 000 / SMA_WINDOW µs. - The index wraps with a bitwise AND (& 63) instead of modulo, since 64 is a power of 2. - The running sum is a uint16_t (max 64 × 255 = 16 320, fits easily). - The result _sma_avg is a volatile float written atomically by the esp_timer task (core 0) and read from the main loop (core 1). No spinlock is needed: on Xtensa LX7, a 32-bit aligned float store is a single instruction. The 60-second window smooths out short bursts (e.g. WiFi/HTTP spikes) while reacting to sustained load changes within ~10–15 seconds. Naming: - The public API is now getCore0Util() returning a float in [0.0, 1.0]. The name explicitly identifies which core is measured (core 0, which runs WiFi, MQTT, HTTP, and LwIP — see task_pinning.c). - JSON keys: "load_avg" (array) → "core0_util" (scalar float, percent) - HistorySample field: load_avg1_pct → core0_util_pct - StatsHistory series key: "cpu_load" → "core0_util" - Web panel label: "Load Avg" → "Core0 Util" The history snapshot (once per minute) reads the same _sma_avg value, which at that point represents the rolling average of the last 60 seconds — exactly one history interval.
This commit is contained in:
@@ -1532,10 +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,\"load_avg\":[%.2f,%.2f,%.2f],\"errors\":%u,\"queue_len\":%u}",
|
||||
"{\"battery_mv\":%u,\"uptime_secs\":%u,\"core0_util\":%.1f,\"errors\":%u,\"queue_len\":%u}",
|
||||
getBatteryMilliVolts(true),
|
||||
_ms->getMillis() / 1000,
|
||||
_cpu_tracker.getLoadAvg1(), _cpu_tracker.getLoadAvg5(), _cpu_tracker.getLoadAvg15(),
|
||||
_cpu_tracker.getCore0Util() * 100.0f,
|
||||
_err_flags,
|
||||
_mgr->getOutboundTotal());
|
||||
}
|
||||
@@ -1941,7 +1941,7 @@ void MyMesh::updateStatsHistory(unsigned long now_ms) {
|
||||
sample.heap_min = ESP.getMinFreeHeap();
|
||||
sample.psram_free = ESP.getFreePsram();
|
||||
sample.psram_min = ESP.getMinFreePsram();
|
||||
sample.load_avg1_pct = (uint8_t)(_cpu_tracker.getLoadAvg1() * 100.0f + 0.5f);
|
||||
sample.core0_util_pct = (uint8_t)(_cpu_tracker.getCore0Util() * 100.0f + 0.5f);
|
||||
#endif
|
||||
if (board.isExternalPowered()) sample.flags |= HISTORY_FLAG_EXTERNAL_POWER;
|
||||
if (board.isCharging()) sample.flags |= HISTORY_FLAG_CHARGING;
|
||||
@@ -2532,7 +2532,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,\"load_avg\":[%.2f,%.2f,%.2f],\"errors\":%u,\"queue_len\":%u,"
|
||||
"\"uptime_secs\":%lu,\"core0_util\":%.1f,\"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,"
|
||||
@@ -2565,7 +2565,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(),
|
||||
_cpu_tracker.getCore0Util() * 100.0f,
|
||||
_err_flags,
|
||||
static_cast<unsigned>(_mgr->getOutboundTotal()),
|
||||
board.isExternalPowered() ? "true" : "false",
|
||||
|
||||
Reference in New Issue
Block a user