Files
MeshCoreTel-firmware/arch/esp32/CPUUsageTracker.cpp
T
Valentin V. Bartenev a9273f02bb 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.
2026-05-10 06:26:08 +03:00

55 regels
1.5 KiB
C++

#ifdef ESP32
#include "CPUUsageTracker.h"
#include "esp_freertos_hooks.h"
volatile uint32_t CPUUsageTracker::s_idle_ticks = 0;
volatile uint32_t CPUUsageTracker::s_busy_ticks = 0;
TaskHandle_t CPUUsageTracker::s_idle_handle = nullptr;
void IRAM_ATTR CPUUsageTracker::s_tick_hook() {
if (xTaskGetCurrentTaskHandle() == s_idle_handle) {
s_idle_ticks++;
} else {
s_busy_ticks++;
}
}
void CPUUsageTracker::s_sample_cb(void* arg) {
static_cast<CPUUsageTracker*>(arg)->_onSample();
}
void CPUUsageTracker::_onSample() {
const uint32_t busy = s_busy_ticks;
const uint32_t idle = s_idle_ticks;
const uint32_t db = busy - _last_busy;
const uint32_t di = idle - _last_idle;
_last_busy = busy;
_last_idle = idle;
const uint32_t total = db + di;
const float sample = (total > 0) ? (float)db / (float)total : 0.0f;
const uint8_t s8 = (uint8_t)(sample * 255.0f + 0.5f);
_sma_sum -= _sma_buf[_sma_idx];
_sma_buf[_sma_idx] = s8;
_sma_sum += s8;
_sma_idx = (_sma_idx + 1) & (SMA_WINDOW - 1);
_sma_avg = (float)_sma_sum * (1.0f / (SMA_WINDOW * 255.0f));
}
void CPUUsageTracker::begin() {
s_idle_handle = xTaskGetIdleTaskHandleForCPU(0);
esp_register_freertos_tick_hook_for_cpu(s_tick_hook, 0);
esp_timer_create_args_t args = {
.callback = s_sample_cb,
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "cpu_sample"
};
esp_timer_create(&args, &_timer);
esp_timer_start_periodic(_timer, 60000000ULL / SMA_WINDOW); // SMA_WINDOW samples per minute
}
#endif