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:
@@ -11,14 +11,10 @@ class CPUUsageTracker {
|
||||
public:
|
||||
void begin();
|
||||
|
||||
float getLoadAvg1() const { return _avg1; }
|
||||
float getLoadAvg5() const { return _avg5; }
|
||||
float getLoadAvg15() const { return _avg15; }
|
||||
float getCore0Util() const { return _sma_avg; }
|
||||
|
||||
private:
|
||||
static constexpr float DECAY1 = 0.920044415f; // exp(-5/60)
|
||||
static constexpr float DECAY5 = 0.983471454f; // exp(-5/300)
|
||||
static constexpr float DECAY15 = 0.994459848f; // exp(-5/900)
|
||||
static constexpr uint8_t SMA_WINDOW = 64; // power of 2 — enables & mask
|
||||
|
||||
static volatile uint32_t s_idle_ticks;
|
||||
static volatile uint32_t s_busy_ticks;
|
||||
@@ -27,11 +23,15 @@ private:
|
||||
static void IRAM_ATTR s_tick_hook();
|
||||
static void s_sample_cb(void* arg);
|
||||
|
||||
uint32_t _last_idle = 0;
|
||||
uint32_t _last_busy = 0;
|
||||
float _avg1 = 0.0f;
|
||||
float _avg5 = 0.0f;
|
||||
float _avg15 = 0.0f;
|
||||
uint32_t _last_idle = 0;
|
||||
uint32_t _last_busy = 0;
|
||||
|
||||
uint8_t _sma_buf[SMA_WINDOW] = {};
|
||||
uint16_t _sma_sum = 0;
|
||||
uint8_t _sma_idx = 0;
|
||||
|
||||
volatile float _sma_avg = 0.0f;
|
||||
|
||||
esp_timer_handle_t _timer = nullptr;
|
||||
|
||||
void _onSample();
|
||||
|
||||
Reference in New Issue
Block a user