diff --git a/src/platform/ESP32/ESP32Utils.cpp b/src/platform/ESP32/ESP32Utils.cpp index a62fd460c7b14c..f2bde483dbad25 100644 --- a/src/platform/ESP32/ESP32Utils.cpp +++ b/src/platform/ESP32/ESP32Utils.cpp @@ -37,6 +37,8 @@ #ifdef CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE #include "esp_heap_caps.h" +#include +#include #include #include #include @@ -53,6 +55,9 @@ constexpr MetricKey kMetricHeapInternalLargestBlock = "internal_largest_free"; constexpr MetricKey kMetricHeapExternalFree = "external_free"; constexpr MetricKey kMetricHeapExternalMinFree = "external_min_free"; constexpr MetricKey kMetricHeapExternalLargestBlock = "external_largest_block"; + +// Task runtime +constexpr MetricKey kMetricTaskName = "runtime"; #endif // CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE using namespace ::chip::DeviceLayer::Internal; @@ -489,12 +494,63 @@ void ESP32Utils::LogHeapInfo() { ESP_LOGE(TAG, "Failed to register callback. Error: 0x%08x", err); } - // Log immediately LogHeapDataCallback(&SystemLayer(), nullptr); // Start the periodic logging using SystemLayer SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(CONFIG_HEAP_LOG_INTERVAL), LogHeapDataCallback, nullptr); } + + +const char * StateToString(eTaskState state) +{ + switch (state) + { + case eRunning: + return "Running"; + case eReady: + return "Ready"; + case eBlocked: + return "Blocked"; + case eSuspended: + return "Suspended"; + case eDeleted: + return "Deleted"; + default: + return "Unknown"; + } +} + +CHIP_ERROR ESP32Utils::LogTaskSnapshotInfo() +{ +#ifdef CONFIG_FREERTOS_USE_TRACE_FACILITY + uint32_t arraySize = uxTaskGetNumberOfTasks(); + + chip::Platform::ScopedMemoryBuffer taskStatusArray; + VerifyOrReturnError(taskStatusArray.Calloc(arraySize), CHIP_ERROR_NO_MEMORY); + + uint32_t dummyRunTimeCounter; + arraySize = uxTaskGetSystemState(taskStatusArray.Get(), arraySize, &dummyRunTimeCounter); + + auto GenerateMetricName = [&](const char * task_name, const char * suffix) { + std::string metricName = task_name; + metricName += "_"; + metricName += suffix; + return metricName; + }; + + for (int i = 0; i < arraySize; i++) + { + TaskStatus_t task = taskStatusArray[i]; + MATTER_TRACE_INSTANT(GenerateMetricName(task.pcTaskName, "state").c_str(), StateToString(task.eCurrentState)); + MATTER_TRACE_INSTANT(GenerateMetricName(task.pcTaskName, "stack_start_address").c_str(), + std::to_string(reinterpret_cast(task.pxStackBase)).c_str()); +#ifdef CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS + MATTER_LOG_METRIC(kMetricTaskName, static_cast(task.ulRunTimeCounter)); +#endif // CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS + } +#endif // CONFIG_FREERTOS_USE_TRACE_FACILITY + return CHIP_NO_ERROR; +} #endif // CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE struct netif * ESP32Utils::GetNetif(const char * ifKey) diff --git a/src/platform/ESP32/ESP32Utils.h b/src/platform/ESP32/ESP32Utils.h index 294cf76d1e9c6d..34fce1a1cc2933 100644 --- a/src/platform/ESP32/ESP32Utils.h +++ b/src/platform/ESP32/ESP32Utils.h @@ -51,6 +51,7 @@ class ESP32Utils #ifdef CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE static void LogHeapInfo(); + static CHIP_ERROR LogTaskSnapshotInfo(); #endif // CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE static CHIP_ERROR MapError(esp_err_t error);