Skip to content

Commit

Permalink
esp32: add freertos task related diagnostics to platform
Browse files Browse the repository at this point in the history
  • Loading branch information
pimpalemahesh authored and esp committed Dec 17, 2024
1 parent 117e2b1 commit c8dde4c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 42 deletions.
44 changes: 4 additions & 40 deletions src/platform/ESP32/ESP32Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@

#ifdef CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
#include "esp_heap_caps.h"
#include <lib/support/CHIPMemString.h>
#include <string.h>
#include <system/SystemTimer.h>
#include <tracing/macros.h>
#include <tracing/metric_event.h>

using namespace chip::DeviceLayer;
using namespace ::chip::Platform;
#endif // CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE

using namespace ::chip::DeviceLayer::Internal;
Expand Down Expand Up @@ -235,7 +238,6 @@ const char * ESP32Utils::WiFiModeToStr(wifi_mode_t wifiMode)
}
}

#ifdef CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
const char * ESP32Utils::WiFiDisconnectReasonToStr(uint16_t reason)
{
switch (reason)
Expand Down Expand Up @@ -287,44 +289,6 @@ const char * ESP32Utils::WiFiDisconnectReasonToStr(uint16_t reason)
}
}

void LogHeapDataCallback(chip::System::Layer * systemLayer, void * appState)
{
// Internal RAM (default heap)
uint32_t internal_free = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
uint32_t internal_largest_free_block = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
uint32_t internal_min_free = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);

MATTER_LOG_METRIC(chip::Tracing::kMetricHeapInternalFree, internal_free);
MATTER_LOG_METRIC(chip::Tracing::kMetricHeapInternalMinFree, internal_min_free);
MATTER_LOG_METRIC(chip::Tracing::kMetricHeapInternalLargestBlock, internal_largest_free_block);

#ifdef CONFIG_SPIRAM
// External RAM (if PSRAM is enabled)
uint32_t external_free = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
uint32_t external_largest_free_block = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);
uint32_t external_min_free = heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM);

MATTER_LOG_METRIC(chip::Tracing::kMetricHeapExternalFree, external_free);
MATTER_LOG_METRIC(chip::Tracing::kMetricHeapExternalMinFree, external_min_free);
MATTER_LOG_METRIC(chip::Tracing::kMetricHeapExternalLargestBlock, external_largest_free_block);
#endif

// Reschedule the timer for the next interval
systemLayer->StartTimer(chip::System::Clock::Milliseconds32(CONFIG_HEAP_LOG_INTERVAL), LogHeapDataCallback, nullptr);
}

// Function to initialize and start periodic heap logging
void ESP32Utils::LogHeapInfo()
{
// Log immediately
LogHeapDataCallback(&SystemLayer(), nullptr);

// Start the periodic logging using SystemLayer
SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(CONFIG_HEAP_LOG_INTERVAL), LogHeapDataCallback, nullptr);
}

#endif // CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE

struct netif * ESP32Utils::GetStationNetif(void)
{
return GetNetif(kDefaultWiFiStationNetifKey);
Expand Down Expand Up @@ -516,7 +480,7 @@ 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
Expand Down
3 changes: 1 addition & 2 deletions src/platform/ESP32/ESP32Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ class ESP32Utils
static CHIP_ERROR ClearWiFiStationProvision(void);
static CHIP_ERROR InitWiFiStack(void);

#ifdef CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
static const char * WiFiDisconnectReasonToStr(uint16_t reason);
static void LogHeapInfo();
#endif // CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
static CHIP_ERROR LogTaskSnapshotInfo();

static CHIP_ERROR MapError(esp_err_t error);
static void RegisterESP32ErrorFormatter();
Expand Down

0 comments on commit c8dde4c

Please sign in to comment.