#include "diagnostics/runtime_diagnostics_service.h" #include #include #include #include namespace borla { namespace { constexpr const char* kTag = "RuntimeDiagnostics2"; constexpr UBaseType_t kLowStackWarningBytes = 512; constexpr UBaseType_t kIpcLowStackWarningBytes = 384; bool IsIpcTask(const char* name) { return name != nullptr && (std::strcmp(name, "ipc0") == 0 || std::strcmp(name, "ipc1") == 0); } } // namespace RuntimeDiagnosticsService::~RuntimeDiagnosticsService() { running_.store(false); if (task_ != nullptr) { vTaskDeleteWithCaps(task_); task_ = nullptr; } heap_caps_free(task_statuses_); task_statuses_ = nullptr; } bool RuntimeDiagnosticsService::Initialize() { if (task_ != nullptr && task_statuses_ != nullptr) { return true; } task_statuses_ = static_cast(heap_caps_calloc(kMaximumTasks, sizeof(TaskStatus_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)); if (task_statuses_ == nullptr) { ESP_LOGE(kTag, "task snapshot buffer allocation failed"); return false; } running_.store(true); const BaseType_t result = xTaskCreateWithCaps( &RuntimeDiagnosticsService::TaskEntry, "runtime_diag2", task_stack_budget::kRuntimeDiagnosticsBytes, this, 2, &task_, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); if (result != pdPASS) { running_.store(false); task_ = nullptr; heap_caps_free(task_statuses_); task_statuses_ = nullptr; ESP_LOGE(kTag, "diagnostics worker creation failed"); return false; } ESP_LOGI(kTag, "worker started stack_bytes=%u memory=PSRAM", static_cast( task_stack_budget::kRuntimeDiagnosticsBytes)); return true; } void RuntimeDiagnosticsService::TaskEntry(void* context) { static_cast(context)->Run(); } [[noreturn]] void RuntimeDiagnosticsService::Run() { uint32_t snapshot_due_ms = kInitialDelayMs; while (running_.load()) { vTaskDelay(pdMS_TO_TICKS(kFrameStatsPeriodMs)); if (!running_.load()) { break; } IAudioEngine* audio = audio_.load(); if (audio != nullptr) { // Returns false and logs nothing while the microphone is off. audio->LogAudioFrameStats(); } elapsed_since_snapshot_ms_ += kFrameStatsPeriodMs; if (elapsed_since_snapshot_ms_ < snapshot_due_ms) { continue; } elapsed_since_snapshot_ms_ = 0; LogSnapshot(); ++snapshots_logged_; snapshot_due_ms = snapshots_logged_ == 1 ? kSecondSnapshotDelayMs : kSnapshotPeriodMs; } for (;;) { vTaskSuspend(nullptr); } } void RuntimeDiagnosticsService::SetAudioEngine(IAudioEngine* audio) { audio_.store(audio); } void RuntimeDiagnosticsService::LogSnapshot() { IAudioEngine* audio = audio_.load(); if (audio != nullptr && esp_log_level_get(kTag) >= ESP_LOG_DEBUG) { // Cumulative capture/voice counters. Steady-state they repeat the // per-window "duplex window closed" line, so they are debug-only; the // dynamic level check keeps them one menuconfig switch away. audio->LogAudioMetrics("periodic"); } const size_t internal_free = heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); const size_t internal_minimum = heap_caps_get_minimum_free_size( MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); const size_t internal_largest = heap_caps_get_largest_free_block( MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); const size_t psram_free = heap_caps_get_free_size(MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); const size_t psram_minimum = heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); const size_t psram_largest = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); // A heap that is holding steady is not news. INFO is reserved for the two // states that demand eyes: the internal low-water mark just sank (a new // worst case happened since the last report), or free space is inside the // zone where lwIP/TLS allocations start failing — the exact failure that // once dropped the WebSocket mid-conversation (free=4415/min=531). constexpr size_t kInternalFreeAlarmBytes = 20 * 1024; const bool degraded = internal_minimum < reported_internal_minimum_ || internal_free < kInternalFreeAlarmBytes; if (degraded) { reported_internal_minimum_ = internal_minimum; ESP_LOGI(kTag, "heap internal free=%u min=%u largest=%u; " "psram free=%u min=%u largest=%u", static_cast(internal_free), static_cast(internal_minimum), static_cast(internal_largest), static_cast(psram_free), static_cast(psram_minimum), static_cast(psram_largest)); } else { ESP_LOGD(kTag, "heap internal free=%u min=%u largest=%u; " "psram free=%u min=%u largest=%u", static_cast(internal_free), static_cast(internal_minimum), static_cast(internal_largest), static_cast(psram_free), static_cast(psram_minimum), static_cast(psram_largest)); } const UBaseType_t expected = uxTaskGetNumberOfTasks(); if (expected > kMaximumTasks) { ESP_LOGW(kTag, "task snapshot skipped: tasks=%u capacity=%u", static_cast(expected), static_cast(kMaximumTasks)); return; } const UBaseType_t count = uxTaskGetSystemState(task_statuses_, kMaximumTasks, nullptr); // The stack budgets were tuned from these lines and have been stable // since; ~30 healthy rows per snapshot were drowning the conversation // log. Healthy tasks report at DEBUG now — only a LOW watermark, the one // thing that demands action, stays visible by default. ESP_LOGD(kTag, "task snapshot count=%u", static_cast(count)); for (UBaseType_t index = 0; index < count; ++index) { const TaskStatus_t& status = task_statuses_[index]; const UBaseType_t warning_threshold = IsIpcTask(status.pcTaskName) ? kIpcLowStackWarningBytes : kLowStackWarningBytes; const bool low = status.usStackHighWaterMark < warning_threshold; if (low) { ESP_LOGW(kTag, "task=%s state=%u priority=%u stack_free_bytes=%u LOW", status.pcTaskName, static_cast(status.eCurrentState), static_cast(status.uxCurrentPriority), static_cast(status.usStackHighWaterMark)); } else { ESP_LOGD(kTag, "task=%s state=%u priority=%u stack_free_bytes=%u", status.pcTaskName, static_cast(status.eCurrentState), static_cast(status.uxCurrentPriority), static_cast(status.usStackHighWaterMark)); } } } } // namespace borla