From 88148b5b98b250fdaf6ded98dee8a821e3b8ff2b Mon Sep 17 00:00:00 2001 From: Mahesh <92411857+pimpalemahesh@users.noreply.github.com> Date: Fri, 6 Dec 2024 12:08:51 +0530 Subject: [PATCH] [ESP32] Added GetThreadMetrics Implementation to DiagnosticDataProvider (#36609) * esp32: implement thread metrics for software diagnostics cluster * esp32: resolve repeated return statement issue --- .../ESP32/DiagnosticDataProviderImpl.cpp | 50 +++++++++++++++++++ .../ESP32/DiagnosticDataProviderImpl.h | 2 + 2 files changed, 52 insertions(+) diff --git a/src/platform/ESP32/DiagnosticDataProviderImpl.cpp b/src/platform/ESP32/DiagnosticDataProviderImpl.cpp index b937d1423bb42f..32fc5243bb1c34 100644 --- a/src/platform/ESP32/DiagnosticDataProviderImpl.cpp +++ b/src/platform/ESP32/DiagnosticDataProviderImpl.cpp @@ -292,6 +292,56 @@ void DiagnosticDataProviderImpl::ReleaseNetworkInterfaces(NetworkInterface * net } } +CHIP_ERROR DiagnosticDataProviderImpl::GetThreadMetrics(ThreadMetrics ** threadMetricsOut) +{ +#ifdef CONFIG_FREERTOS_USE_TRACE_FACILITY + ThreadMetrics * head = nullptr; + uint32_t arraySize = uxTaskGetNumberOfTasks(); + + Platform::ScopedMemoryBuffer taskStatusArray; + VerifyOrReturnError(taskStatusArray.Calloc(arraySize), CHIP_ERROR_NO_MEMORY); + + uint32_t dummyRunTimeCounter; + arraySize = uxTaskGetSystemState(taskStatusArray.Get(), arraySize, &dummyRunTimeCounter); + + for (uint32_t i = 0; i < arraySize; i++) + { + auto thread = static_cast(Platform::MemoryCalloc(1, sizeof(ThreadMetrics))); + VerifyOrReturnError(thread, CHIP_ERROR_NO_MEMORY, ReleaseThreadMetrics(head)); + + Platform::CopyString(thread->NameBuf, taskStatusArray[i].pcTaskName); + thread->name.Emplace(CharSpan::fromCharString(thread->NameBuf)); + thread->id = taskStatusArray[i].xTaskNumber; + thread->stackFreeMinimum.Emplace(taskStatusArray[i].usStackHighWaterMark); + + // Todo: Calculate stack size and current free stack value and assign. + thread->stackFreeCurrent.ClearValue(); + thread->stackSize.ClearValue(); + + thread->Next = head; + head = thread; + } + + *threadMetricsOut = head; + + return CHIP_NO_ERROR; +#else + return CHIP_ERROR_NOT_IMPLEMENTED; +#endif +} + +void DiagnosticDataProviderImpl::ReleaseThreadMetrics(ThreadMetrics * threadMetrics) +{ +#ifdef CONFIG_FREERTOS_USE_TRACE_FACILITY + while (threadMetrics) + { + ThreadMetrics * del = threadMetrics; + threadMetrics = threadMetrics->Next; + Platform::MemoryFree(del); + } +#endif +} + #if CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiBssId(MutableByteSpan & BssId) { diff --git a/src/platform/ESP32/DiagnosticDataProviderImpl.h b/src/platform/ESP32/DiagnosticDataProviderImpl.h index b0ee2bc2014f5b..1b92503431d11d 100644 --- a/src/platform/ESP32/DiagnosticDataProviderImpl.h +++ b/src/platform/ESP32/DiagnosticDataProviderImpl.h @@ -50,6 +50,8 @@ class DiagnosticDataProviderImpl : public DiagnosticDataProvider CHIP_ERROR GetNetworkInterfaces(NetworkInterface ** netifpp) override; void ReleaseNetworkInterfaces(NetworkInterface * netifp) override; + CHIP_ERROR GetThreadMetrics(ThreadMetrics ** threadMetricsOut) override; + void ReleaseThreadMetrics(ThreadMetrics * threadMetrics) override; #if CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_ERROR GetWiFiBssId(MutableByteSpan & BssId) override;