From fcdf209ac37c7f4dda78d99574663b9c0a3a6da0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 8 May 2025 18:45:57 -0500 Subject: [PATCH] wip --- esphome/components/api/api_server.cpp | 67 ++++++++++++++++++++++----- heap_trace_example.yaml | 2 + 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index 91dcc5a991..678e8323a9 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -41,22 +41,65 @@ extern "C" void dump_task_heap_info() { ESP_LOGI("HEAP", "Total blocks: %u", info.total_blocks); ESP_LOGI("HEAP", "-------------------------------------"); - // Get information about running tasks - char buffer[128]; + // Get information about running tasks with a much larger buffer to prevent overflow + // The FreeRTOS functions don't provide a way to check buffer size requirements in advance + static char buffer[2048]; + + // Zero out the buffer for safety + memset(buffer, 0, sizeof(buffer)); + + // Get task list vTaskList(buffer); - ESP_LOGI("HEAP", "Task Information:"); - ESP_LOGI("HEAP", "Name State Priority Stack Num"); - ESP_LOGI("HEAP", "-------------------------------------"); - ESP_LOGI("HEAP", "%s", buffer); + // Check if buffer has valid content + if (buffer[0] != '\0') { + ESP_LOGI("HEAP", "Task Information:"); + ESP_LOGI("HEAP", "Name State Priority Stack Num"); + ESP_LOGI("HEAP", "-------------------------------------"); + + // Process the buffer line by line to add the log prefix to each line + char *line = strtok(buffer, "\n\r"); + int count = 0; + while (line != nullptr && strlen(line) > 0 && count < 20) { + ESP_LOGI("HEAP", "%s", line); + line = strtok(nullptr, "\n\r"); + count++; + } + } else { + ESP_LOGE("HEAP", "Could not get task information"); + } + ESP_LOGI("HEAP", "-------------------------------------"); - // Additional runtime statistics about tasks - vTaskGetRunTimeStats(buffer); - ESP_LOGI("HEAP", "Task Runtime Statistics:"); - ESP_LOGI("HEAP", "Name Time Percentage"); - ESP_LOGI("HEAP", "-------------------------------------"); - ESP_LOGI("HEAP", "%s", buffer); + // Runtime statistics - use a separate section with a different buffer to avoid corruption + static char stats_buffer[2048]; + memset(stats_buffer, 0, sizeof(stats_buffer)); + + // Get runtime stats + vTaskGetRunTimeStats(stats_buffer); + + // Check if buffer has valid content + if (stats_buffer[0] != '\0') { + ESP_LOGI("HEAP", "Task Runtime Statistics:"); + ESP_LOGI("HEAP", "Name Time Percentage"); + ESP_LOGI("HEAP", "-------------------------------------"); + + // Process the runtime stats buffer line by line safely + char *line = strtok(stats_buffer, "\n\r"); + int count = 0; + // Limit to 20 lines to prevent buffer overruns + while (line != nullptr && count < 20) { + // Skip empty lines + if (strlen(line) > 0) { + ESP_LOGI("HEAP", "%s", line); + } + line = strtok(nullptr, "\n\r"); + count++; + } + } else { + ESP_LOGE("HEAP", "Could not get task runtime statistics"); + } + ESP_LOGI("HEAP", "-------------------------------------"); } #endif diff --git a/heap_trace_example.yaml b/heap_trace_example.yaml index 376a67c552..5be937e23e 100644 --- a/heap_trace_example.yaml +++ b/heap_trace_example.yaml @@ -19,6 +19,8 @@ api: standalone: true # Number of trace records to keep (more records = more memory usage) num_records: 100 + # Enable task statistics tracking (shows task names, stack usage, etc.) + task_tracking: true # Enable OTA updates ota: