Skip to content

Commit

Permalink
fix: Resolve heap timer related issue
Browse files Browse the repository at this point in the history
- Made the timer optional with a set timer flag
- Added support for canceling the timer
- Updated method descriptions for better clarity
  • Loading branch information
pimpalemahesh authored and esp committed Dec 20, 2024
1 parent 4ac601c commit b0bddcf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
45 changes: 37 additions & 8 deletions src/platform/ESP32/ESP32Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ using namespace ::chip::DeviceLayer::Internal;
using chip::DeviceLayer::Internal::DeviceNetworkInfo;

#ifndef CONFIG_HEAP_LOG_INTERVAL
#define CONFIG_HEAP_LOG_INTERVAL 300000
#define CONFIG_HEAP_LOG_INTERVAL 86400000
#endif // CONFIG_HEAP_LOG_INTERVAL

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
Expand Down Expand Up @@ -453,6 +453,7 @@ CHIP_ERROR ESP32Utils::InitWiFiStack(void)
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI

#ifdef CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE

void LogHeapDataCallback(chip::System::Layer * systemLayer, void * appState)
{
// Internal RAM (default heap)
Expand All @@ -473,10 +474,18 @@ void LogHeapDataCallback(chip::System::Layer * systemLayer, void * appState)
MATTER_LOG_METRIC(kMetricHeapExternalFree, external_free);
MATTER_LOG_METRIC(kMetricHeapExternalMinFree, external_min_free);
MATTER_LOG_METRIC(kMetricHeapExternalLargestBlock, external_largest_free_block);
#endif
#endif // CONFIG_SPIRAM

// Reschedule the timer for the next interval
systemLayer->StartTimer(chip::System::Clock::Milliseconds32(CONFIG_HEAP_LOG_INTERVAL), LogHeapDataCallback, nullptr);
if (appState != nullptr)
{
static bool setTimer = static_cast<bool *>(appState);
CHIP_ERROR err =
SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(CONFIG_HEAP_LOG_INTERVAL), LogHeapDataCallback, &setTimer);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to reschedule heap log timer");
}
}
}

void FailedAllocCallback(size_t size, uint32_t caps, const char * function_name)
Expand All @@ -486,17 +495,37 @@ void FailedAllocCallback(size_t size, uint32_t caps, const char * function_name)
}

// Function to initialize and start periodic heap logging
void ESP32Utils::LogHeapInfo()
void ESP32Utils::LogHeapInfo(bool setTimer)
{
if (CONFIG_HEAP_LOG_INTERVAL <= 0)
{
setTimer = false;
return;
}

static bool timerState = setTimer;

if (!setTimer)
{
SystemLayer().CancelTimer(LogHeapDataCallback, &timerState);
}

esp_err_t err = heap_caps_register_failed_alloc_callback(FailedAllocCallback);
if (err != ESP_OK)
{
ChipLogError(DeviceLayer, "Failed to register callback. Error: 0x%08x", err);
}
LogHeapDataCallback(&SystemLayer(), nullptr);

// Start the periodic logging using SystemLayer
SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(CONFIG_HEAP_LOG_INTERVAL), LogHeapDataCallback, nullptr);
LogHeapDataCallback(&SystemLayer(), &timerState);
if (setTimer)
{
CHIP_ERROR error = SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(CONFIG_HEAP_LOG_INTERVAL),
LogHeapDataCallback, &timerState);
if (error != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to schedule heap log timer. Error: %s", chip::ErrorStr(error));
}
}
}

const char * StateToString(eTaskState state)
Expand Down
19 changes: 18 additions & 1 deletion src/platform/ESP32/ESP32Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,24 @@ class ESP32Utils
static CHIP_ERROR InitWiFiStack(void);

#ifdef CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
static void LogHeapInfo();
/**
* @brief Logs the current heap usage information.
*
* This function logs details about the current heap usage and can optionally set up
* a periodic timer to log heap usage at regular intervals.
*
* @param setTimer Indicates whether to set a periodic timer for logging. Default is false.
*/
static void LogHeapInfo(bool setTimer = false);

/**
* @brief Captures and logs information about the current task snapshots.
*
* This function retrieves a snapshot of all running tasks and logs details about
* their states.
*
* @return CHIP_ERROR Returns a CHIP_ERROR code indicating the success or failure of the operation.
*/
static CHIP_ERROR LogTaskSnapshotInfo();
#endif // CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE

Expand Down

0 comments on commit b0bddcf

Please sign in to comment.