From 482b31bc1903d2bc2ff4b5e42341c47ce9f01e6a Mon Sep 17 00:00:00 2001 From: "Valentin V. Bartenev" Date: Sun, 10 May 2026 07:09:03 +0300 Subject: [PATCH] Slightly optimize CPU util tracking by defer float conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously _onSample() computed and stored a volatile float _sma_avg on every sample (64 times/minute on core 0). The getter simply returned that pre-computed value. This commit inverts the responsibility: - _sma_sum is now volatile uint16_t — the raw integer sum is the cross-core shared state, written atomically by core 0 (single S16I instruction on Xtensa LX7) and read by core 1. - The float conversion (uint16_t → float multiply) is moved into getCore0Util(), which is called rarely (once/minute for history, on-demand for web requests). The multiply now happens on the core that actually needs the result. - _sma_avg is removed entirely, saving 4 bytes and one volatile float store per sample from the hot path. - The SMA update is rewritten as a single combined expression: const uint16_t last = _sma_buf[_sma_idx]; _sma_buf[_sma_idx] = s8; _sma_sum = (_sma_sum - last) + s8; This produces a single write to _sma_sum instead of two (decrement then increment), which is cleaner when _sma_sum is volatile. The cross-core contract is unchanged: core 0 writes _sma_sum once per sample; core 1 reads it in getCore0Util(). A 16-bit aligned store on Xtensa LX7 is a single instruction, so no spinlock is needed. --- arch/esp32/CPUUsageTracker.cpp | 5 ++--- arch/esp32/CPUUsageTracker.h | 7 ++++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/esp32/CPUUsageTracker.cpp b/arch/esp32/CPUUsageTracker.cpp index 84441ea4..8cb7ff61 100644 --- a/arch/esp32/CPUUsageTracker.cpp +++ b/arch/esp32/CPUUsageTracker.cpp @@ -30,11 +30,10 @@ void CPUUsageTracker::_onSample() { 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]; + const uint16_t last = _sma_buf[_sma_idx]; _sma_buf[_sma_idx] = s8; - _sma_sum += s8; + _sma_sum = (_sma_sum - last) + s8; _sma_idx = (_sma_idx + 1) & (SMA_WINDOW - 1); - _sma_avg = (float)_sma_sum * (1.0f / (SMA_WINDOW * 255.0f)); } void CPUUsageTracker::begin() { diff --git a/arch/esp32/CPUUsageTracker.h b/arch/esp32/CPUUsageTracker.h index eea394d0..d951c640 100644 --- a/arch/esp32/CPUUsageTracker.h +++ b/arch/esp32/CPUUsageTracker.h @@ -11,7 +11,9 @@ class CPUUsageTracker { public: void begin(); - float getCore0Util() const { return _sma_avg; } + float getCore0Util() const { + return (float)_sma_sum * (1.0f / (SMA_WINDOW * 255.0f)); + } private: static constexpr uint8_t SMA_WINDOW = 64; // power of 2 — enables & mask @@ -27,10 +29,9 @@ private: 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; + volatile uint16_t _sma_sum = 0; esp_timer_handle_t _timer = nullptr;