Skip to content

Commit

Permalink
[Zephyr] Support heap watermark for non-default malloc (#19385)
Browse files Browse the repository at this point in the history
* [Zephyr] Support heap watermark for non-default malloc

When non-default malloc implementation, based on Zephyr's
sys_heap is used, support CurrentHeapHighWatermark attribute
and ResetWatermarks command. This is because sys_heap
exposes max_allocated_bytes statistic.

Signed-off-by: Damian Krolik <[email protected]>

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
Damian-Nordic and restyled-commits authored Jun 9, 2022
1 parent d9cfd7e commit ac8bdbf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
26 changes: 19 additions & 7 deletions src/platform/Zephyr/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ inline DiagnosticDataProviderImpl::DiagnosticDataProviderImpl() : mBootReason(De
ChipLogDetail(DeviceLayer, "Boot reason: %u", static_cast<uint16_t>(mBootReason));
}

bool DiagnosticDataProviderImpl::SupportsWatermarks()
{
#ifdef CONFIG_CHIP_MALLOC_SYS_HEAP
return true;
#else
return false;
#endif
}

CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapFree(uint64_t & currentHeapFree)
{
#ifdef CONFIG_CHIP_MALLOC_SYS_HEAP
Expand Down Expand Up @@ -153,14 +162,17 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & cu
Malloc::Stats stats;
ReturnErrorOnFailure(Malloc::GetStats(stats));

// TODO: use the maximum usage once that is implemented in Zephyr
currentHeapHighWatermark = stats.used;
currentHeapHighWatermark = stats.maxUsed;
return CHIP_NO_ERROR;
#elif CHIP_DEVICE_CONFIG_HEAP_STATISTICS_MALLINFO
// ARM newlib does not provide a way to obtain the peak heap usage, so for now just return
// the amount of memory allocated from the system which should be an upper bound of the peak
// usage provided that the heap is not very fragmented.
currentHeapHighWatermark = mallinfo().arena;
#else
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif
}

CHIP_ERROR DiagnosticDataProviderImpl::ResetWatermarks()
{
#ifdef CONFIG_CHIP_MALLOC_SYS_HEAP
Malloc::ResetMaxStats();
return CHIP_NO_ERROR;
#else
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
Expand Down
2 changes: 2 additions & 0 deletions src/platform/Zephyr/DiagnosticDataProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ class DiagnosticDataProviderImpl : public DiagnosticDataProvider

// ===== Methods that implement the PlatformManager abstract interface.

bool SupportsWatermarks() override;
CHIP_ERROR GetCurrentHeapFree(uint64_t & currentHeapFree) override;
CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed) override;
CHIP_ERROR GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) override;
CHIP_ERROR ResetWatermarks() override;

CHIP_ERROR GetRebootCount(uint16_t & rebootCount) override;
CHIP_ERROR GetUpTime(uint64_t & upTime) override;
Expand Down
10 changes: 8 additions & 2 deletions src/platform/Zephyr/SysHeapMalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,18 @@ CHIP_ERROR GetStats(Stats & stats)
sys_heap_runtime_stats sysHeapStats;
ReturnErrorOnFailure(System::MapErrorZephyr(sys_heap_runtime_stats_get(&sHeap, &sysHeapStats)));

stats.free = sysHeapStats.free_bytes;
stats.used = sysHeapStats.allocated_bytes;
stats.free = sysHeapStats.free_bytes;
stats.used = sysHeapStats.allocated_bytes;
stats.maxUsed = sysHeapStats.max_allocated_bytes;

return CHIP_NO_ERROR;
}

void ResetMaxStats()
{
(void) sys_heap_runtime_stats_reset_max(&sHeap);
}

#endif // CONFIG_SYS_HEAP_RUNTIME_STATS

} // namespace Malloc
Expand Down
2 changes: 2 additions & 0 deletions src/platform/Zephyr/SysHeapMalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ struct Stats
{
size_t free;
size_t used;
size_t maxUsed;
};

void * Malloc(size_t size);
void * Calloc(size_t num, size_t size);
void * Realloc(void * mem, size_t size);
void Free(void * mem);
CHIP_ERROR GetStats(Stats & stats);
void ResetMaxStats();

} // namespace Malloc
} // namespace DeviceLayer
Expand Down

0 comments on commit ac8bdbf

Please sign in to comment.