feat: report main-loop health in memStatus

memStatus now carries loopRate (loop() passes per second) and loopMaxUs
(the longest pass, in microseconds) over the window since the last
broadcast. Only a broadcast closes the window, so a client connecting
does not wipe a slow pass before Home Assistant has seen it.

/discovery is formatted on the web server's task, so its memory block
reports the last closed window through atomics instead of reading the
counters the loop task is still filling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cjkas 2026-10-18 13:53:30 +02:00
parent 96b00b70f4
commit ce505e93f6
6 changed files with 122 additions and 1 deletions

View file

@ -152,3 +152,20 @@ heap_emit_t heapEmitTarget(uint8_t num, uint32_t sinceLastMs, uint32_t intervalM
if(sinceLastMs >= intervalMs) return heap_emit_t::broadcast;
return roomHasClients ? heap_emit_t::room : heap_emit_t::none;
}
void LoopStats::record(uint32_t passUs) {
_passes++;
if(passUs > _maxUs) _maxUs = passUs;
}
uint32_t LoopStats::rate(uint32_t nowMs) const {
// The unsigned subtraction keeps the window right across a millis() wrap.
const uint32_t elapsedMs = nowMs - _startMs;
if(elapsedMs == 0) return 0;
return (uint32_t)((uint64_t)_passes * 1000 / elapsedMs);
}
void LoopStats::publish(uint32_t nowMs) {
_publishedRate.store(this->rate(nowMs), std::memory_order_relaxed);
_publishedMaxUs.store(_maxUs, std::memory_order_relaxed);
_passes = 0;
_maxUs = 0;
_startMs = nowMs;
}