Skip to content

Commit

Permalink
[ESP32] Added GetThreadMetrics Implementation to DiagnosticDataProvid…
Browse files Browse the repository at this point in the history
…er (#36609)

* esp32: implement thread metrics for software diagnostics cluster

* esp32: resolve repeated return statement issue
  • Loading branch information
pimpalemahesh authored Dec 6, 2024
1 parent 22d490a commit 88148b5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/platform/ESP32/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TaskStatus_t> 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<ThreadMetrics *>(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)
{
Expand Down
2 changes: 2 additions & 0 deletions src/platform/ESP32/DiagnosticDataProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 88148b5

Please sign in to comment.