#pragma once #include "core/task_stack_budget.h" #include "ports/audio_engine.h" #include #include #include #include namespace borla { // Periodically reports heap health and every FreeRTOS task's remaining stack. // The snapshot buffer and worker stack live in PSRAM so diagnostics do not run // heavy task enumeration/logging on the shared esp_timer task. class RuntimeDiagnosticsService final { public: RuntimeDiagnosticsService() = default; ~RuntimeDiagnosticsService(); RuntimeDiagnosticsService(const RuntimeDiagnosticsService&) = delete; RuntimeDiagnosticsService& operator=(const RuntimeDiagnosticsService&) = delete; bool Initialize(); // Optional. When set, each snapshot also emits the voice frontend // counters, which are the only way to see AEC reference misses. void SetAudioEngine(IAudioEngine* audio); void LogSnapshot(); private: static constexpr size_t kMaximumTasks = 64; static constexpr uint32_t kInitialDelayMs = 5 * 1000; static constexpr uint32_t kSecondSnapshotDelayMs = 25 * 1000; static constexpr uint32_t kSnapshotPeriodMs = 5 * 60 * 1000; // The worker now ticks fast and emits the audio frame statistics on every // tick, because the capture DSP chain has to be watched while a // conversation is happening, not once every five minutes. The tick is // silent whenever the microphone is off, so an idle device stays quiet. // The heavy heap/task snapshot keeps its original cadence. static constexpr uint32_t kFrameStatsPeriodMs = 2 * 1000; static void TaskEntry(void* context); [[noreturn]] void Run(); std::atomic audio_{nullptr}; TaskHandle_t task_ = nullptr; TaskStatus_t* task_statuses_ = nullptr; size_t snapshots_logged_ = 0; uint32_t elapsed_since_snapshot_ms_ = 0; // Low-water mark of the internal heap at the last INFO-level heap report. // The heap line only earns INFO when this drops (or free space is // critically low); a steady heap reports at DEBUG. size_t reported_internal_minimum_ = SIZE_MAX; std::atomic running_{false}; }; } // namespace borla